﻿glblShipmentEntered = false;

function checkRelated(childControl, parentControl)
{

    parentCtl = document.getElementById(parentControl);
    
    if (parentCtl.value == '')
    {
        alert("Please enter a value for Total Pieces first.");
        parentCtl.focus();
    }
}

function checkTotalPieces(control)
{
    
    if (isNaN(control.value))
    {
        alert("Please enter a valid number for Total Pieces.");
        control.style.backgroundColor = '#FFF8C6';
        control.focus();
    }
    else
    {
        control.style.backgroundColor = '#FFFFFF';
    }
}

function checkTotalWeight(control)
{
    
    if (isNaN(control.value))
    {
        alert("Please enter a valid number for Total Weight.")
        control.style.backgroundColor = '#FFF8C6';
        control.focus();
    }
    else
    {
        control.style.backgroundColor = '#FFFFFF';
    }
}

function checkDeliveryZip(control)
{
    if (control.value == '')
    {
        alert("Please enter a Zip Code for delivery.")
        control.style.backgroundColor = '#FFF8C6';
        control.focus();
    }
    else
    {
        control.style.backgroundColor = '#FFFFFF';
    }
}

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
    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', true);
                   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;
        }
    }
}

function valInternationalShipmentInfo(source, args)
{

    //debugger;

    errorMessage = '';

    serviceLevel = source.id.substring(source.id.indexOf('val') + 3, source.id.indexOf('TotalPieces'));
    
    totalPieces = document.getElementById(source.id.replace('val', 'txt'));
    totalWeight = document.getElementById(totalPieces.id.replace('TotalPieces', 'TotalWeight'));
    deliveryZip = document.getElementById(totalPieces.id.replace('TotalPieces', 'DeliveryZip')); 
    dCountry = source.id.replace('val', 'ddl');
    deliveryCountry = document.getElementById(dCountry.replace('TotalPieces', 'DeliveryCountry'));

    totalPiecesValid = (totalPieces.value != '') ? true : false;
    totalWeightValid = (totalWeight.value != '') ? true : false;
    deliveryZipValid = (deliveryZip.value != '') ? true : false;
    deliveryCountryValid = (deliveryCountry.options[deliveryCountry.selectedIndex].value != '') ? true : false;
 
    if (!totalPiecesValid && !totalWeightValid && (!deliveryZipValid || !deliveryCountryValid))
    {
        args.IsValid = true;
    }
    else if (totalPiecesValid && totalWeightValid && (deliveryZipValid || deliveryCountryValid))
    {
        args.IsValid = true;
        glblShipmentEntered = true;
    }
    else
    {
        if (!totalPiecesValid)
        {
            errorMessage = 'Total Pieces';
        }
        
        if (!totalWeightValid)
        {
            if (errorMessage != '')
            {
                errorMessage += ' , Total Weight';
            }
            else
            {
                errorMessage = 'Total Weight';
            }
        }
            
        if (!deliveryZipValid  && !deliveryCountryValid)
        {
            if (errorMessage != '')
            {
                errorMessage += ' , Delivery Zip or Delivery Country';
            }
            else
            {
                errorMessage = 'Delivery Zip or Delivery Country';
            }
        }
        
        errorMessage += ' required for ' + serviceLevel + ' service level.';    
        
        source.errormessage = errorMessage;
        
        totalPieces.style.backgroundColor = totalPiecesValid ? '#FFFFFF' : '#FFF8C6';
        totalWeight.style.backgroundColor = totalWeightValid ? '#FFFFFF' : '#FFF8C6';
        deliveryZip.style.backgroundColor = deliveryZipValid ? '#FFFFFF' : '#FFF8C6';
        deliveryCountry.style.backgroundColor = deliveryCountryValid ? '#FFFFFF' : '#FFF8C6';
    
        args.IsValid = false;
    }
}

function valShipmentInfo(source, args)
{

    //debugger;
    
    errorMessage = '';

    serviceLevel = source.id.substring(source.id.indexOf('val') + 3, source.id.indexOf('TotalPieces'));
    
    totalPieces = document.getElementById(source.id.replace('val', 'txt'));
    totalWeight = document.getElementById(totalPieces.id.replace('TotalPieces', 'TotalWeight'));
    deliveryZip = document.getElementById(totalPieces.id.replace('TotalPieces', 'DeliveryZip')); 

    totalPiecesValid = (totalPieces.value != '') ? true : false;
    totalWeightValid = (totalWeight.value != '') ? true : false;
    deliveryZipValid = (deliveryZip.value != '') ? true : false;
 
    if (!totalPiecesValid && !totalWeightValid && !deliveryZipValid)
    {
        args.IsValid = true;
    }
    else if (totalPiecesValid && totalWeightValid && deliveryZipValid)
    {
        args.IsValid = true;
        glblShipmentEntered = true;
    }
    else
    {
        if (!totalPiecesValid)
        {
            errorMessage = 'Total Pieces';
        }
        
        if (!totalWeightValid)
        {
            if (errorMessage != '')
            {
                errorMessage += ' , Total Weight';
            }
            else
            {
                errorMessage = 'Total Weight';
            }
        }
            
        if (!deliveryZipValid)
        {
            if (errorMessage != '')
            {
                errorMessage += ' , Delivery Zip';
            }
            else
            {
                errorMessage = 'Delivery Zip';
            }
        }
        
        errorMessage += ' required for ' + serviceLevel + ' service level.';    
        
        source.errormessage = errorMessage;
        
        totalPieces.style.backgroundColor = totalPiecesValid ? '#FFFFFF' : '#FFF8C6';
        totalWeight.style.backgroundColor = totalWeightValid ? '#FFFFFF' : '#FFF8C6';
        deliveryZip.style.backgroundColor = deliveryZipValid ? '#FFFFFF' : '#FFF8C6';
    
        args.IsValid = false;
    }
}

function valTime(source, args)
{
    // Get form name
    controlHour = source.id.replace('val', 'ddl');
    controlHour = controlHour.replace('Time', 'Hour');
    
    controlMinute = source.id.replace('val', 'ddl');
    controlMinute = controlMinute.replace('Time', 'Minute');
    
    hour = document.getElementById(controlHour);
    minute = document.getElementById(controlMinute);
    
    if (hour.value != '' && minute.value != '')
    {
        args.IsValid = true;
        hour.style.backgroundColor = '#FFFFFF';
        minute.style.backgroundColor = '#FFFFFF';
    }
    else
    {
        args.IsValid = false;
        hour.style.backgroundColor = '#FFF8C6';
        minute.style.backgroundColor = '#FFF8C6';
    }
}

function deactivateValidators()
{
    for (i = 0; i < Page_Validators.length; i++)
    {
        ValidatorEnable(Page_Validators[i], false);
    }
}

function activateValidators()
{

    // 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', 'Zip'));
                if (requestorState.options.length > 1)
                {
                    ValidatorEnable(Page_Validators[i], true);
                    ValidatorEnable(valRequestorZip, true);
                }
            }
            else
            {
                ValidatorEnable(Page_Validators[i], true);
            }
        }
        
        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);
            }
        }
        
        
    }
}

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 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 (copyDestination != '' && !isCopy)
    {
        destinationControlArray = eval(copyDestination + 'CountryState');
        
        populateState(countryCode, destinationControlArray, true);
        
        copyValue(controlArray[0]);
        copyValue(controlArray[1]);
        
    }
}

