﻿glblShipmentEntered = false;

function checkNumber(control, fieldName)
{
    if (isNaN(control.value))
    {
        alert('Please enter a valid number for ' + fieldName + '.');
        control.style.backgroundColor = '#FFF8C6';
        control.focus();
    }
    else
    {
        control.style.backgroundColor = '#FFFFFF';
    }
}

function checkRelated(childControl, parentControl)
{

    parentCtl = document.getElementById(parentControl);
    
    if (parentCtl.value == '')
    {
        alert("Please enter a value for Total Pieces first.");
        parentCtl.focus();
    }
}

function copyValue(controlID)
{

    // The value of copyDestination is expected to be checked
    // BEFORE this function is called
    //debugger;
    destinationArray = eval(copyDestination);
    
    originControl = document.getElementById(controlID);
    destinationControl = document.getElementById(destinationArray[controlID][0]);
    controlType = destinationArray[controlID][1];
    
    switch (controlType) 
    {
        case 'text':
            destinationControl.value = originControl.value;
            break;
        case 'ddl':
            destinationControl.selectedIndex = originControl.selectedIndex;
            break;
    }
}

function toggleShipperCopy(copyOnOff)
{
    // Set copyDestination value here, because it is used later
    // by the country/state combo
    //debugger;
    copyDestination = copyOnOff ? 'Shipper' : '';
    
    for (ctl in Shipper)
    {
        originControl = document.getElementById(ctl);
        destinationControl = document.getElementById(Shipper[ctl][0]);
        controlType = Shipper[ctl][1];
        
        switch (controlType)
        {
            case 'text':
                destinationControl.value = copyOnOff ? originControl.value : '';
                destinationControl.disabled = copyOnOff ? true : false;
                break;
            case 'ddl':
                destinationControl.selectedIndex = copyOnOff ? originControl.selectedIndex : 0;
                
                
                if (destinationControl.id.indexOf('Country') != -1)
                {
                   changeCountry('ShipperCountryState', false, false);
                   destinationControl.disabled = copyOnOff ? true : false;
                }
                else
                {
                    // State control will always be disabled on a toggle
                    // Either disabled because it's being copied from Requestor,
                    // or disabled because no country has been selected
                    destinationControl.disabled = true;
                }
                break;
        }
    }
    
    // Check to see if the other check box is checked
    // If so, turn it off
    if (copyToConsignee.checked && copyOnOff)
    {        
        toggleConsigneeCopy(false);
        copyToConsignee.checked = false;
        copyDestination = 'Shipper';
    }

    
}

function toggleConsigneeCopy(copyOnOff)
{
    
    // Set copyDestination value here, because it is used later
    // by the country/state combo
    copyDestination = copyOnOff ? 'Consignee' : '';
    
    for (ctl in Consignee)
    {
        originControl = document.getElementById(ctl);
        destinationControl = document.getElementById(Consignee[ctl][0]);
        controlType = Consignee[ctl][1];
        
        switch (controlType)
        {
            case 'text':
                destinationControl.value = copyOnOff ? originControl.value : '';
                destinationControl.disabled = copyOnOff ? true : false;
                break;
            case 'ddl':
                destinationControl.selectedIndex = copyOnOff ? originControl.selectedIndex : 0;
                
                
                if (destinationControl.id.indexOf('Country') != -1)
                {
                   changeCountry('ConsigneeCountryState', false, false);
                   destinationControl.disabled = copyOnOff ? true : false;
                }
                else
                {
                    // State control will always be disabled on a toggle
                    // Either disabled because it's being copied from Requestor,
                    // or disabled because no country has been selected
                    destinationControl.disabled = true;
                }
                break;
        }
    }
    
        // Check to see if the other check box is checked
    // If so, turn it off
    if (copyToShipper.checked && copyOnOff)
    {        
        toggleShipperCopy(false);
        copyToShipper.checked = false;
        copyDestination = 'Consignee';
    }
}

function deactivateValidators()
{
    for (i = 0; i < Page_Validators.length; i++)
    {
        ValidatorEnable(Page_Validators[i], false);
    }
}

function activateValidators()
{
    //debugger;

    // Loop through the Page_Validators array
    for (i = 0; i < Page_Validators.length; i++)
    {
        if (Page_Validators[i].id.indexOf('Requestor') != -1)
        {
            if (Page_Validators[i].id.indexOf('RequestorState') != -1)
            {
                requestorState = document.getElementById(Page_Validators[i].id.replace('val', 'ddl'));
                valRequestorZip = document.getElementById(Page_Validators[i].id.replace('State', 'ZipCode'));
                if (requestorState.options.length > 1)
                {
                    ValidatorEnable(Page_Validators[i], true);
                    ValidatorEnable(valRequestorZip, true);
                }
            }
            else
            {
                ValidatorEnable(Page_Validators[i], true);
            }
        }
        
        //debugger;
        if (!copyToShipper.checked && Page_Validators[i].id.indexOf('Shipper') != -1)
        {
            if (Page_Validators[i].id.indexOf('State') != -1)
            {
                shipperState = document.getElementById(Page_Validators[i].id.replace('val', 'ddl'));
                if (shipperState.options.length > 1)
                {
                    ValidatorEnable(Page_Validators[i], true);
                }           
            }
            else
            {
                ValidatorEnable(Page_Validators[i], true);
            }
        }
        
        if (!copyToConsignee.checked && Page_Validators[i].id.indexOf('Consignee') != -1)
        {
            if (Page_Validators[i].id.indexOf('State') != -1)
            {
                consigneeState = document.getElementById(Page_Validators[i].id.replace('val', 'ddl'));
                if (consigneeState.options.length > 1)
                {
                    ValidatorEnable(Page_Validators[i], true);
                }
            }
            else
            {
                ValidatorEnable(Page_Validators[i], true);
            }
        }
    }
}

function checkForShipment(source, args)
{
    //debugger;

    if (glblShipmentEntered)
    {
        args.IsValid = true;
    }
    else
    {
        source.errormessage = 'At least one shipment must be entered.';
        args.IsValid = false;
    }

}

function checkAmount(control, value)
{
    if (value.indexOf(',') != -1)
    {
        control.value = control.value.replace(/,/g, '');
    }
    if (value.indexOf('.') != -1)
    {
        control.value = control.value.substring(0, control.value.indexOf('.'));
    }


}

/*************************************************************************/
/* function checkDate(control, date)                                     */
/*                                                                       */
/* Validates that the date is in a MM/DD/YY format and not in the past.  */
/*                                                                       */
/* History:                                                              */
/*     29 Jan 08 timsar - Created.                                       */
/*************************************************************************/        
function checkDate(control, date)
{
    reString = "\\d{1,2}/\\d{1,2}/\\d{2,4}";

    dateRegEx = new RegExp(reString);
    
    if (!date.match(dateRegEx))
    {
        highlightError(control, "Please enter the date in MM/DD/YY format.");
        return false;
    }
    
    var today = new Date();
        
    var components = date.split("/");
    
    month = components[0];
    day = components[1];
    year = components[2];
    
    if (year.length <= 2)    
        pickupdate = new Date("20" + year, month - 1, day);
    else
        pickupdate = new Date(year, month - 1, day);
        
    if (pickupdate >= today)
    {
        document.getElementById(control).style.backgroundColor = '#FFFFFF';
        return true;
    }
    else 
    {
        highlightError(control, "Requested date must be later than today.");
        return false;
    } 
}

function valShipmentLine(source, args)
{
    //debugger;

    errorMessage = '';
    
    lineNumber = source.id.substring(source.id.indexOf('val'), source.id.indexOf('txtTotalPieces'));
    
    pieces = document.getElementById(lineNumber + 'txtPieces');
    weight = document.getElementById(lineNumber + 'txtWeight');
    dimLength = document.getElementById(lineNumber + 'txtLength');
    dimWidth = document.getElementById(lineNumber + 'txtWidth');
    dimHeight = document.getElementById(lineNumber + 'txtHeight');
    
    piecesValid = (pieces.value != '0' && pieces.value != '') ? true : false;
    weightValid = (weight.value != '0' && weight.value != '') ? true : false;
    dimLengthValid = (dimLength.value != '0' && dimLength.value != '') ? true : false;
    dimWidthValid = (dimWidth.value != '0' && dimWidth.value != '') ? true : false;
    dimHeightValid = (dimHeight.value != '0' && dimHeight.value != '') ? true : false;
    
    if (!piecesValid && !weightValid && !dimLengthValid && !dimWidthValid && !dimHeightValid) 
    {
        args.IsValid = true;
    }
    else if (piecesValid && weightValid && dimLengthValid && dimWidthValid && dimHeightValid) 
    {
        args.IsValid = true;
        glblShipmentEntered = true;
    }
    else
    {
        source.errormessage = 'All fields are required for each line of shipment dimensions.';
        
        pieces.style.backgroundColor = piecesValid ? '#FFFFFF' : '#FFF8C6';
        weight.style.backgroundColor = weightValid ? '#FFFFFF' : '#FFF8C6';
        dimLength.style.backgroundColor = dimLengthValid ? '#FFFFFF' : '#FFF8C6';
        dimWidth.style.backgroundColor = dimWidthValid ? '#FFFFFF' : '#FFF8C6';
        dimHeight.style.backgroundColor = dimHeightValid ? '#FFFFFF' : '#FFF8C6';
        
        args.IsValid = false;
    }
}

function changeQuoteType(quoteTypeControl, serviceLevelControl, paymentTypeControl, reload)
{

    quoteType = quoteTypeControl.options[quoteTypeControl.selectedIndex].value;
    serviceLevel = document.getElementById(serviceLevelControl);
    paymentType = document.getElementById(paymentTypeControl);
    
    populateHidden(quoteTypeControl.id + "Text", quoteType);
    
    if (quoteType == 'Domestic')
    {
        serviceLevelArray = eval('D_ServiceLevels');
        paymentTypeArray = eval('D_PaymentTypes'); 
    }
    else
    {
        serviceLevelArray = eval('I_ServiceLevels');
        paymentTypeArray = eval('I_PaymentTypes'); 
    }

    serviceLevel.options.length = null;
    paymentType.options.length = null;
    
    for (i = 0; i < serviceLevelArray.length; i++)
    {
        eval('serviceLevel.options[i] = new Option' + serviceLevelArray[i]);
    }
    
    for (i = 0; i < paymentTypeArray.length; i++)
    {
        eval('paymentType.options[i] = new Option' + paymentTypeArray[i]);
    }
    
    if (reload)
    {
        reloadDropDownValues(serviceLevel, document.getElementById(serviceLevel.id + "Text").value);
        reloadDropDownValues(paymentType, document.getElementById(paymentType.id + "Text").value);
    }
    
    populateHidden(serviceLevel.id + "Text", serviceLevel.options[serviceLevel.selectedIndex].text);
    populateHidden(paymentType.id + "Text", paymentType.options[paymentType.selectedIndex].text);
}

function changeCountry(objArray, isCopy, reload)
{
    //debugger;

    controlArray = eval(objArray);
    
    countryControl = document.getElementById(controlArray[0]);
    //stateControl = document.getElementById(controlArray[1]);
    
    if (reload)
    {
        countryValue = document.getElementById(controlArray[0] + 'Value');
        stateValue = document.getElementById(controlArray[1] + 'Value');
    }
    
    countryCode = reload ? countryValue.value : countryControl.options[countryControl.selectedIndex].value;
    
    populateState(countryCode, controlArray, false);
    
    stateControl = document.getElementById(controlArray[1]);
    
    if (reload)
    {
        reloadDropDownValues(countryControl, countryCode);
        reloadDropDownValues(stateControl, stateValue.value);
    }  
    
    populateHidden(controlArray[0] + 'Value', countryCode);
    populateHidden(controlArray[0] + 'Text', countryControl.options[countryControl.selectedIndex].text); 
    
    populateHidden(controlArray[1] + 'Value', stateControl.options[stateControl.selectedIndex].value);
    populateHidden(controlArray[1] + 'Text', stateControl.options[stateControl.selectedIndex].text);
    
    //debugger;
    
    if (isCopy)
    {
        destinationControlArray = eval(copyDestination + 'CountryState');
        
        populateState(countryCode, destinationControlArray, true);
        
        copyValue(controlArray[0]);
        copyValue(controlArray[1]);
        
    }
}


