﻿/*****************************************************************************
/* Title:       PublicScripts.js                                                   
/* Author:      Tim Sargent
/* Description: Contains client-side scripts used with Customer Tools
/* History:     21 Feb 08 timsar - Created
/*
/* Copyright 2008 Mainfreight, Inc. All Rights Reserved
/*****************************************************************************/

function populateState(countryCode, controlArray, isCopy)
{

    stateControl = document.getElementById(controlArray[1]);
    
    stateControl.options.length  = null;
    
    if (countryCode == 'US' || countryCode == 'CA')
    {
        stateArray = eval(countryCode + 'StatesArray');
        
        for (i = 0; i < stateArray.length - 1; i++)
        {
            eval('stateControl.options[i] = new Option' + stateArray[i]);
        }
        
        if (isCopy)
            stateControl.disabled = true;
        else
            stateControl.disabled = false;
    }
    else
    {
        eval('stateControl.options[0] = new Option("- State Not Required -", "")');
        
        stateControl.disabled = true;
    }
    
    displayRequiredFields(controlArray[2], ((countryCode == 'US' || countryCode == 'CA') ? true : false));
    displayRequiredFields(controlArray[3], ((countryCode == 'US' || countryCode == 'CA') ? true : false));
    
    stateControl.selectedIndex = 0;
}

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]);
        
    }
}

function reloadDropDownValues(control, controlValue)
{
    
    for (i = 0; i < control.options.length - 1; i++)
    {
        if (control.options[i].value == controlValue)
            control.selectedIndex = i;
    }
}

function changeState(controlID)
{

    stateControl = document.getElementById(controlID);
    
    populateHidden(stateControl.id + 'Value', stateControl.options[stateControl.selectedIndex].value);
    populateHidden(stateControl.id + 'Text', stateControl.options[stateControl.selectedIndex].text);    
}

function trimInput(control)
{

    control.value = control.value.replace(/^\s*/, '');
    control.value = control.value.replace(/\s*$/, '');
}

function displayRequiredFields(controlName, display)
{
    
    requiredField = document.getElementById(controlName);
    requiredField.style.visibility = display ? 'visible' : 'hidden';
}

/*************************************************************************/
/* function populateHidden(control, value)                               */
/*                                                                       */
/* Takes the passed in control (a hidden field) and sets it's value to   */
/* the value argument.  This function is used to store selections from   */
/* drop down lists that are dynamically populated via client-side Java-  */
/* Script.                                                               */
/*                                                                       */
/* History:                                                              */
/*     29 Jan 08 timsar - Created.                                       */
/*************************************************************************/
function populateHidden(control, value)
{
    objControl = document.getElementById(control);
    objControl.value = value;
}
    
/*************************************************************************/
/* function highlightError(control, message)                             */
/*                                                                       */
/* Displays the error message for a failed validation, and highlights    */
/* the field that failed.                                                */
/*                                                                       */
/* History:                                                              */
/*     29 Jan 08 timsar - Created.                                       */
/*************************************************************************/
function highlightError(control, message)
{
    alert(message);
        
    document.getElementById(control).style.backgroundColor = "#FFF8C6";
   
}

/*************************************************************************/
/* function checkZip(control, countryControl, zipCode)                   */
/*                                                                       */
/* Validates that the zip code for the US is a 5 digit number, and that  */
/* the zip code for Canada is a 6 or 7 character string formatted as     */
/* ANA NAN.  The space is optional.  Zip code is not required for other  */
/* countries.                                                            */
/*                                                                       */
/* History:                                                              */
/*     29 Jan 08 timsar - Created.                                       */
/*************************************************************************/     
function checkZip(control, countryControl, zipCode)
{
    country = document.getElementById(countryControl);
    countryCode = country.options[country.selectedIndex].value;
    reString = ''
    valid = true;
        
    switch (countryCode)
    {
        case 'US':
            reString = '^\\d{5}$';
            errorMessage = 'US Zip Code must be 5 digits.';
            break;
        case 'CA':
            if (zipCode.length == 7)
            {
                reString = '^[A-Z]\\d[A-Z]\\s\\d[A-Z]\\d$';
            }
            else if (zipCode.length == 6)
            {
                reString = '^[A-Z]\\d[A-Z]\\d[A-Z]\\d$';
            }
            else
            {
                reString = '';
            }
            errorMessage = 'Canadian Zip Code must be A#A #A# format. The space in the middle is optional.';  
            break;
        default:
            reString = '';
            break;
    }
        
    if (reString != '')
    {
        zipRegEx = new RegExp(reString);
        if (!zipCode.match(zipRegEx))
        {
            highlightError(control, errorMessage);
            valid = false;
        }
    }
          
    return valid;
}

function valStateControl(source, args)
{
    // Grab the proper section name
    var section = source.id.substring(0, source.id.indexOf('State'));
    
    country = document.getElementById(section.replace('val', 'ddl') + 'Country');
    state = document.getElementById(section.replace('val', 'ddl') + 'State');
    
    if (country.options[country.selectedIndex].value == 'US' || country.options[country.selectedIndex].value == 'CA')
    {        
        if (state.options[state.selectedIndex].value == '')
        {
            args.IsValid = false;
        }
        else
        {
            args.IsValid = true;
        }   
    }
    else
    {
        args.IsValid = true;
    }
    
    state.style.backgroundColor = args.IsValid ? '#FFFFFF' : '#FFF8C6';
} 

/* Custom Validators */

function valZipCode(source, args)
{
    args.IsValid = true;
    errorMessage = '';
    //debugger;
    // Grab the proper section name
    var section = source.id.substring(0, source.id.indexOf('Zip'));
    
    country = document.getElementById(section.replace('val', 'ddl') + 'Country');
    countryCode = country.options[country.selectedIndex].value;
    zip = document.getElementById(source.id.replace('val', 'txt'));
    zipCode = zip.value;
    
    if (countryCode == 'US' || countryCode == 'CA')
    {
        if (zip.value == '')
        {
           args.IsValid = false;
        }
        else
        {
            switch (countryCode)
            {
                case 'US':
                    reString = '^\\d{5}$';
                    errorMessage = 'US Zip Code must be 5 digits.';
                    break;
                case 'CA':
                    if (zipCode.length == 7)
                    {
                        reString = '^[A-Z]\\d[A-Z]\\s\\d[A-Z]\\d$';
                    }
                    else if (zipCode.length == 6)
                    {
                        reString = '^[A-Z]\\d[A-Z]\\d[A-Z]\\d$';
                    }
                    else
                    {
                        reString = '';
                    }
                    errorMessage = 'Canadian Zip Code must be A#A #A# format. The space in the middle is optional.';  
                    break;
                default:
                    reString = '';
                    break;
            }
        
            if (reString != '')
            {
                zipRegEx = new RegExp(reString);
                
                if (!zipCode.match(zipRegEx))
                {
                    args.IsValid = false;
                    source.errormessage = errorMessage;
                }
                
            }
        }
    }
        
    zip.style.backgroundColor = args.IsValid ? '#FFFFFF' : '#FFF8C6';
}

function valTextBox(source, args)
{
    //debugger;
    textBox = document.getElementById(source.id.replace('val', 'txt'));
    if (textBox.value == '')
    {
        args.IsValid = false;
    }
    else
    {
        args.IsValid = true;
    }
    
    textBox.style.backgroundColor = args.IsValid ? '#FFFFFF' : '#FFF8C6';

}

function valDropDownList(source, args)
{
    dropDownList = document.getElementById(source.id.replace('val', 'ddl'));
    
    if (dropDownList.options[dropDownList.selectedIndex].value == '')
    {
        args.IsValid = false;
    }
    else
    {
        args.IsValid = true;
    }
    
    dropDownList.style.backgroundColor = args.IsValid ? '#FFFFFF' : '#FFF8C6';
} 

function activateRequestValidators()
{

    //debugger;
    // Loop through the Page_Validators array
    for (i = 0; i < Page_Validators.length; i++)
    {
        if (Page_Validators[i].id.indexOf('State') != -1)
        {
            state = document.getElementById(Page_Validators[i].id.replace('val', 'ddl'));
            valZip = document.getElementById(Page_Validators[i].id.replace('State', 'Zip'));
            if (state.options.length > 1)
            {
                ValidatorEnable(Page_Validators[i], true);
                ValidatorEnable(valZip, true);
            }
        }
        else
        {
            ValidatorEnable(Page_Validators[i], true);
        }
    }
}

function checkEmail(control, value)
{
    
    //debugger;
    emailControl = document.getElementById(control);
    // Minimum of one letter, @, one letter, then .com, .net, .org or .gov
    reString = '\\w+(\\.\\w+)*@(\\w+\\.)+(com|net|org|gov)';
    
    emailRegEx = new RegExp(reString);
    
    if (!value.match(emailRegEx))
    {
        highlightError(control, 'E-mail format is not valid.');
        return false;
    }
    else
    {
        return true;
    }

}

function checkPhone(control, value)
{
    //debugger;
    phoneControl = document.getElementById(control);
    
    value = value.replace(/ /g, '');
    value = value.replace(/-/g, '');
    value = value.replace(/\)/g, '');
    value = value.replace(/\(/g, '');
    
    if (isNaN(value))
    {
        highlightError(control, 'Phone number must be numeric.');
        return false;
    }
    else
    {
        return true;
    }
}

function valCompanyAndName(source, args)
{
    //debugger;
    company = document.getElementById(source.id.replace('val', 'txt'));
    contactName = document.getElementById(company.id.replace('Company', 'Contact'));
    
    if (company.value == '' && contactName.value == '')
    {
        company.style.backgroundColor = '#FFF8C6';
        contactName.style.backgroundColor = '#FFF8C6';
        
        args.IsValid = false;
    }
    else
    {
        args.IsValid = true;
    }
}


