﻿//Global vars---------------------------------------------------------------------------------------------------

var intMin23mBrandedTapeOrder = 1;
var intMin4mBrandedTapeOrder = 1;

var fltStandard23mTapePrice = 26.5;
var fltStandard4mTapePrice = 25.75;

//--------------------------------------------------------------------------------------------------------------

function SinglePriceUpdatePrice()
{
    var lblPrice = document.getElementById('lblPrice');
    var intQty = document.getElementById('txtQuantity').value;

    //only calulate price if qty is a number, otherwise it'll error
    if(IsNumeric(intQty))
    {
        var hUnitPrice = document.getElementById('hUnitPrice');
        
       lblPrice.innerHTML = '&pound;' + (hUnitPrice.value * intQty).toFixed(2);
    }
}

function ValidateMinQty(intMinQty, strControlId)
{
    var intQty = document.getElementById(strControlId).value;
    
    if(!IsNumeric(intQty))
    {
        alert('Please enter a vaild number');
        return false;
    }
    //intQty != 0, allow 0 because this has the same functionallity as delete
    else if((intQty < intMinQty) && intQty != 0)
    {
        alert('You must order a minimum of ' + intMinQty);
        return false;
    }
    else
        return true;
}

function SubmitPostToBasket(strSection)
{
    //validate minimum orders when ordering branded tapes
    //validate qty > 0 and is a number
    
    var strSelectedTape = document.getElementById('ddlTapeColours').value;
    var strQty = document.getElementById('txtQuantity').value;
    var intQty;
    var blnValid = true;

    if(IsNumeric(strQty) && strQty.length > 0)
    {
        
        intQty = parseInt(strQty);
        
        if((strSection == "gl20" || strSection == "gl25") && strSelectedTape == 'tape-branded')
        {
            if(intQty < intMin4mBrandedTapeOrder)
            {
                alert('You must order at least ' + intMin4mBrandedTapeOrder);
                blnValid = false;
            }    
        }
        else if (strSection == "gl45" && strSelectedTape == 'tape-branded')
        {
            if(intQty < intMin23mBrandedTapeOrder)
            {
                alert('You must order at least ' + intMin23mBrandedTapeOrder);
                blnValid = false;
            }    
        }
        else
        {
            if(intQty <= 0)
            {
                alert('You must order at least 1');
                blnValid = false;
            }
        }
    }
    else
    {
        alert('Please enter a valid quantity');
        blnValid = false;
    }

    //if invalid, clear value and set focus
    if(!blnValid)
    {
        var txtQuantity = document.getElementById('txtQuantity');
        txtQuantity.value = '';
        txtQuantity.focus();
    }
    
    return blnValid;
}

function UpdateOrginationCosts()
{
//disabled in branded tape update\\
//    var lblPrice = document.getElementById('lblPrice');
//    
//    var fltUnitPrice = parseFloat(document.getElementById('hUnitPrice').value);
//    var intQty = parseInt(document.getElementById('txtQuantity').value);
//    var fltOrginationCost = parseFloat(document.getElementById('ddlOrginationCosts').value);
//    
//    var fltPrice = parseFloat((fltUnitPrice * intQty) + fltOrginationCost);

//    lblPrice.innerHTML = '&pound;' + fltPrice.toFixed(2);
}

function UpdatePostPrice()
{
    var dblDiscount = 0.0;
    var lblPrice = document.getElementById('lblPrice');
    var intQty = document.getElementById('txtQuantity').value;

    //only calulate price if qty is a number, otherwise it'll error
    if(IsNumeric(intQty))
    {
        var hUnitPrice = document.getElementById('hUnitPrice');
        
        hUnitPrice.value = aryPostColours[GetIndexFromSafeName(aryPostColours, document.getElementById('ddlPostColours').value)][1];

        //if dummy tape selected
        if(document.getElementById('ddlTapeColours').value == 'tape-dummy')
            dblDiscount = -6.0;
           
        //if branded selected    
        if(document.getElementById('ddlTapeColours').value == 'tape-branded')
            UpdateOrginationCosts();
        else
            UpdateDisplayPrice(dblDiscount);
    }
}

//only used on tape cassette page
function UpdateTapePrice()
{
    var lblPrice = document.getElementById('lblPrice');
    var intQty = document.getElementById('txtQuantity').value;
    var strTapeLength = document.getElementById('ddlTapeLength').value;

    //only calulate price if qty is a number, otherwise it'll error
    if(IsNumeric(intQty))
    {
        var hUnitPrice = document.getElementById('hUnitPrice');
        
        if(strTapeLength == '2.3m')
            hUnitPrice.value = ary23mTapeColours[GetIndexFromSafeName(ary23mTapeColours, document.getElementById('ddlTapeColours').value)][1];
        else if(strTapeLength == '4m')
            hUnitPrice.value = ary4mTapeColours[GetIndexFromSafeName(ary4mTapeColours, document.getElementById('ddlTapeColours').value)][1];
        else if(strTapeLength == '1.5m')
            hUnitPrice.value = ary15mRopeColours[GetIndexFromSafeName(ary15mRopeColours, document.getElementById('ddlTapeColours').value)][1];
        
        lblPrice.innerHTML = '&pound;' + (hUnitPrice.value * intQty).toFixed(2);
    }
}

function preChangePost()
{
    var strSelectedPostSafeName = document.getElementById('ddlPostColours').value;
    var intSelectedIndex = GetIndexToSelectFromSafeName('ddlPostColours', strSelectedPostSafeName);
    var strSelectedPostName = document.getElementById('ddlPostColours')[intSelectedIndex].text;

    ChangePost(strSelectedPostSafeName, strSelectedPostName);
}

function ChangePost(strSelectedPostSafeName, strSelectedPostName)
{
    var flash = document.getElementById('sotester');
    var flashreturn = flash.gotoBarrier(strSelectedPostSafeName, strSelectedPostName);
    
    //select dropdown box
    var intSelectedIndex = GetIndexToSelectFromSafeName('ddlPostColours', strSelectedPostSafeName);
    document.getElementById('ddlPostColours')[intSelectedIndex].selected = true;
    
    //update price
    UpdatePostPrice()
}

function preChangeTape(strSection)
{
    var strSelectedTapeSafeName = document.getElementById('ddlTapeColours').value;
    var intSelectedIndex = GetIndexToSelectFromSafeName('ddlTapeColours', strSelectedTapeSafeName);
    var strSelectedTapeName = document.getElementById('ddlTapeColours')[intSelectedIndex].text;
    
    ChangeTape(strSelectedTapeSafeName, strSelectedTapeName, strSection);
}

function ChangeTape(strSelectedTapeSafeName, strSelectedTapeName, strSection)
{   
    if(strSection == 'cassette' || strSection == 'gl20' || strSection == 'gl25' || strSection == 'gl45' || strSection == 'vip' || strSection == 'vip-rope')
    {
        var flash = document.getElementById('sotester');
        var flashreturn = flash.gotoTape(strSelectedTapeSafeName, strSelectedTapeName);
    }

    //select dropdown box
    var intSelectedIndex;
    
    if(strSelectedTapeName == 'Branded')
    {
        intSelectedIndex = GetIndexToSelectFromName('ddlTapeColours', strSelectedTapeName);
        
        //show OrginationCosts ddl
        if(strSection != 'cassette')
        {
            //disabled in branded tape update\\
            //document.getElementById('OrginationCosts').style.display = 'block';
            //UpdateOrginationCosts();
        }
    }
    else
    {
        intSelectedIndex = GetIndexToSelectFromSafeName('ddlTapeColours', strSelectedTapeSafeName);
        
        //hide OrginationCosts ddl
        if(strSection == 'gl20' || strSection == 'gl25' || strSection == 'gl45')
            document.getElementById('OrginationCosts').style.display = 'none';
    }
    document.getElementById('ddlTapeColours')[intSelectedIndex].selected = true;
      
    if(strSection == 'cassette' || strSection == 'vip' || strSection == 'vip-rope')
    {
        var hUnitPrice = document.getElementById('hUnitPrice');
        var hRopeUnitPrice = document.getElementById('hRopeUnitPrice');
        
        //set price according to length
        if(strSection == 'vip')
            hRopeUnitPrice.value = ary15mRopeColours[GetIndexFromSafeName(ary15mRopeColours, document.getElementById('ddlTapeColours').value)][1];
        else if(strSection == 'vip-rope')
            hUnitPrice.value = ary15mRopeColours[GetIndexFromSafeName(ary15mRopeColours, document.getElementById('ddlTapeColours').value)][1];
        else if(document.getElementById('ddlTapeLength').value == '2.3m')
            hUnitPrice.value = ary23mTapeColours[GetIndexFromSafeName(ary23mTapeColours, document.getElementById('ddlTapeColours').value)][1];
        else
            hUnitPrice.value = ary4mTapeColours[GetIndexFromSafeName(ary4mTapeColours, document.getElementById('ddlTapeColours').value)][1];

        UpdateDisplayPrice(0);
    }
    //gl20 or gl25 - minus £6 if dummy cassette selected, add orgination cost if branded, otherwise is regular price
    else if(strSection == 'gl20' || strSection == 'gl25' || strSection == 'gl45')
        UpdatePostPrice();
}

function UpdateDisplayPrice(dblDiscount)
{
    var intQty = document.getElementById('txtQuantity').value;
    
    if(IsNumeric(intQty))
    {
        //var only used on VIP section
        //hRopeUnitPrice used for rope on vip barrier section, not rope section
        var dblRopeUnitPrice;
        
        try
        {
            dblRopeUnitPrice = parseFloat(document.getElementById('hRopeUnitPrice').value);
        }
        catch (ex)
        {
            dblRopeUnitPrice = 0;
        }
        
        var dblUnitPrice = parseFloat(document.getElementById('hUnitPrice').value);
        var lblPrice = document.getElementById('lblPrice');
        intQty = parseInt(document.getElementById('txtQuantity').value);
        lblPrice.innerHTML = '&pound;' + (((dblRopeUnitPrice + dblUnitPrice + dblDiscount) * intQty).toFixed(2));
    }
}

function GetIndexFromSafeName(ary, strSafeName)
{
    for(var i = 0; i < ary.length; i++)
    {
        if(ary[i][0] == strSafeName)
            return i;
    }
    
    return -1;
}

function GetIndexToSelectFromSafeName(ddl, value)
{
    var objDdl = document.getElementById(ddl);
    
    for(var i = 0; i < objDdl.length; i++)
    {
        if(objDdl[i].value == value)
            return i;
    }
    
    return 0;
}

function GetIndexToSelectFromName(ddl, value)
{
    var objDdl = document.getElementById(ddl);
    
    for(var i = 0; i < objDdl.length; i++)
    {
        if(objDdl[i].text == value)
            return i;
    }
    
    return 0;
}

function IsNumeric(str)
{
    var strValidChars = "0123456789";
    var blnIsNumber = true;
    var strChar;

    for (var i = 0; i < str.length && blnIsNumber; i++) 
    { 
        strChar = str.charAt(i);
        
        if(strValidChars.indexOf(strChar) == -1) 
            blnIsNumber = false;
    }
    
    return blnIsNumber;
}