﻿// JScript File

function IsAlphaNumeric(e)
{
    // trap and cancel keys that are not expected
    var key = 0;  // collect key code
    
    if (window.event) 
        key = window.event.keyCode;
    else if (e) 
        key = e.which;
        
    //backspace(46) del(8), left(37), right(39), tab(9), space(32)
    if ( key == 0 || key == 46 || key == 8 || key == 37 || key == 39 || key == 9 || key == 32)
        return true;
    //0-9:(48-57)
    else if ( key >= 48 && key <= 57 )
        return true;        
    //A-Z 
    else if (key >= 65 && key <= 90 )
        return true;
    //a-z
    else if (key >= 97 && key <= 122 )
        return true;
    else 
        return false;
        
}

function IsAddressChar(e)
{
    // trap and cancel keys that are not expected
    var key = 0;  // collect key code
    
    if (window.event) 
        key = window.event.keyCode;
    else if (e) 
        key = e.which;
        
    //backspace(46) del(8), left(37), right(39), tab(9), space(32)
    if ( key == 0 || key == 46 || key == 8 || key == 37 || key == 39 || key == 9 || key == 32)
        return true;
    //hyphen.  Period, comma, apostrophe 
    //39('), 44(,), 45(-), 46(.)
    else if (key == 39 || key == 44 || key == 45 || key == 46 ) 
        return 
    
    //0-9:(48-57)
    else if ( key >= 48 && key <= 57 )
        return true;        
    //A-Z 
    else if (key >= 65 && key <= 90 )
        return true;
    //a-z
    else if (key >= 97 && key <= 122 )
        return true;
    else 
        return false;
        
}



function GetDDListCountrySelectedValue(ddlID)
{
   
    var ddl =  document.getElementById(ddlID);
    return ddl.options[ddl.selectedIndex].value;
    //return ddl.selectedIndex;
}

function SetDDListCountrySelectedIndexAndContext(ddlCountryID, ddlProvinceID, txtProvinceID, txtPostalCodeID, lblProvinceID, lblPostalcodeID)
{
    var ddlCountry = document.getElementById(ddlCountryID);
    var countryID = ddlCountry.options[ddlCountry.selectedIndex].value; 
    
    //save to client side hidden field
    var hiddenCountryID = document.getElementById("hiddenCountryID");
    hiddenCountryID.value = countryID; 
    
    return countryID + "|" + ddlProvinceID + "|" + txtProvinceID + "|" + txtPostalCodeID + "|" + lblProvinceID + "|" + lblPostalcodeID ; 
    
}

function  OnDDListCountrySelectedIndexChangedCallback(result,context)
{
         
    //"'" + this.ddlistCountry.SelectedValue + "|" + this.ddlistProvince.ClientID + "|" + this.txtProvince.ClientID + "|" + this.lblProvince.ClientID + "|" + this.lblPostalcode.ClientID + "'");
    var IDs = context.split("|");
    if ( IDs.length != 6) 
        return;
        
    var countryID = IDs[0];     //countryID
    var ddlProvince = document.getElementById(IDs[1]);
    var txtProvince = document.getElementById(IDs[2]);
    var txtPostalCode = document.getElementById(IDs[3]);
    var lblProvince = document.getElementById(IDs[4]);
    var lblPostalcode = document.getElementById(IDs[5]);

    //ProvinceName + "|" + ProvinceID + "$"
    //Canada/US/Select
    if ( countryID == "32" || countryID == "1" || countryID == "0")
    {
        var items = result.split("$"); 
        ddlProvince.length = items.length; 
        
        for ( var i = 0 ; i < items.length; i++)
        {
            var pair = items[i].split("|");
            ddlProvince.options[i].text = pair[0];
            ddlProvince.options[i].value = pair[1];        
        } 
        ddlProvince.selectedIndex = 0;
        
        //setup controls
        ddlProvince.style.display = '';
        txtProvince.style.display = 'none'; 
        
        if (lblProvince.innerHTML.indexOf("*") < 0 )
            lblProvince.innerHTML = "*" + lblProvince.innerHTML;
        
        if (lblPostalcode.innerHTML.indexOf("*") < 0 )
            lblPostalcode.innerHTML = "*" + lblPostalcode.innerHTML
    }    
    else
    {
         //setup controls
        ddlProvince.style.display = 'none';
        txtProvince.style.display = ''; 
        txtPostalCode.value = ""; 
        
        var index1 = lblProvince.innerHTML.indexOf("*")
        var index2 = lblPostalcode.innerHTML.indexOf("*")

        if (index1 >= 0 )
            lblProvince.innerHTML = lblProvince.innerHTML.substr(index1 + 1);
        if (index2 >= 0 )
            lblPostalcode.innerHTML = lblPostalcode.innerHTML.substr(index2 + 1);
   
    
    }   
        

      
}



function setAddressControlStartupStatus(txtProvinceID)
{
       
    var txtProvince = document.getElementById(txtProvinceID);
    if (txtProvince != null )
    {
        txtProvince.style.display = 'none';
    }
}
