﻿// JScript File

function ShowToolTip( objectParentName, objectName, e )
{
    var tooltipParent = document.getElementById( objectParentName );
    var tooltip = document.getElementById( objectName );
    tooltip.className = "showcallout";
    
    var aryPosition = ObjectPosition(tooltipParent);
    
    var browser     = navigator.appName;
    var b_version   = navigator.appVersion;
    var version     = parseFloat(b_version);    
    
    //Check Browser
    //FireFox 3     browser = Netscape,                     version = 5
    //IE 6          browser = Microsoft Internet Explorer,  version = 4
    //Chrome 2      browser = Netscape,                     version = 5
    //Safari 4 Beta browser = Netscape,                     version = 5
    //Opera 9       browser = Opera,                        version = 9.64     
    
    if ( browser == "Microsoft Internet Explorer" && version == 4 )
    {
        tooltip.style.left  =   130;
        //tooltip.style.top   =   0;  // fix for IE6  
    }
    else
    {
        tooltip.style.left  =   aryPosition[0];
        tooltip.style.top   =   aryPosition[1]; 
    }
}

function ObjectPosition(obj) {
    var curleft = 0;
      var curtop = 0;
      if (obj.offsetParent) {
            do {
                  curleft += obj.offsetLeft;
                  curtop += obj.offsetTop;
            } while (obj = obj.offsetParent);
      }
      return [curleft,curtop];
}

function HideToolTip( objectName, e )
{
    var tooltip =document.getElementById( objectName );
    tooltip.className="hidecallout";
}

function format(str)
{
  for(i = 1; i < arguments.length; i++)
  {
    str = str.replace( "{" + (i-1) + "}" , arguments[i]);
  }
   return str;
}

function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}

// Populates the country selected with the counties from the country list
function populateCountry(defaultCountry, countryList, selectID, inputID, selectCountryText, otherText) {

    // Customized: Aug 28, 2011 - Added to have US selected by default
    if (defaultCountry == null || defaultCountry.length == 0) {
        defaultCountry = "US";
    }
      
    var isFound = false;
    var countryLineArray = countryList.split('|');  // Split into lines
    var selObj = document.getElementById(selectID);

    selObj.options[0] = new Option(selectCountryText, '');
    selObj.selectedIndex = 0;

    for (var loop = 0; loop < countryLineArray.length - 1; loop++) {
        lineArray = countryLineArray[loop].split(':');
        countryCode = TrimString(lineArray[0]);
        countryName = TrimString(lineArray[1]);
        if (countryCode != '') {
            selObj.options[loop + 1] = new Option(countryName, countryCode);
        }
        if (defaultCountry == countryCode) {
            selObj.selectedIndex = loop + 1;
            isFound = true;
        }
    }

    var countryText = document.getElementById(inputID);

    selObj.options[countryLineArray.length] = new Option(otherText, "OT");
    if (defaultCountry != "" && !isFound) {
        selObj.selectedIndex = countryLineArray.length;
        countryText.style.display = '';
        countryText.value = defaultCountry;
    }
    else {
        countryText.style.display = 'none';
    }
}

// Customized: Aug 28, 2011 - Added to have the US states selected by default.
function selectCountryStateValues(countryCode) {
    if (countryCode == "US") {
        var countryText = document.getElementById('ctl00_uxPlaceHolder_uxCustomerRegister_uxCountryState_uxCountryText');
        var countryHidden = document.getElementById('ctl00_uxPlaceHolder_uxCustomerRegister_uxCountryState_uxCountryHidden');
        countryText.style.display = 'none';
        countryHidden.value = countryCode;
        populateState('', 'US:AL:Alabama|US:AK:Alaska|US:AZ:Arizona|US:AR:Arkansas|US:CA:California|US:CO:Colorado|US:CT:Connecticut|US:DE:Delaware|US:DC:District Of Columbia|US:FL:Florida|US:GA:Georgia|US:HI:Hawaii|US:ID:Idaho|US:IL:Illinois|US:IN:Indiana|US:IA:Iowa|US:KS:Kansas|US:KY:Kentucky|US:LA:Louisiana|US:ME:Maine|US:MD:Maryland|US:MA:Massachusetts|US:MI:Michigan|US:MN:Minnesota|US:MS:Mississippi|US:MO:Missouri|US:MT:Montana|US:NE:Nebraska|US:NV:Nevada|US:NH:New Hampshire|US:NJ:New Jersey|US:NM:New Mexico|US:NY:New York|US:NC:North Carolina|US:ND:North Dakota|US:OH:Ohio|US:OK:Oklahoma|US:OR:Oregon|US:PA:Pennsylvania|US:RI:Rhode Island|US:SC:South Carolina|US:SD:South Dakota|US:TN:Tennessee|US:TX:Texas|US:UT:Utah|US:VT:Vermont|US:VA:Virginia|US:WA:Washington|US:WV:West Virginia|US:WI:Wisconsin|US:WY:Wyoming|', 'ctl00_uxPlaceHolder_uxCustomerRegister_uxCountryState_uxStateDrop', 'ctl00_uxPlaceHolder_uxCustomerRegister_uxCountryState_uxStateText', countryCode, 'Select...', 'Other...');
        populateState('', 'US:AL:Alabama|US:AK:Alaska|US:AZ:Arizona|US:AR:Arkansas|US:CA:California|US:CO:Colorado|US:CT:Connecticut|US:DE:Delaware|US:DC:District Of Columbia|US:FL:Florida|US:GA:Georgia|US:HI:Hawaii|US:ID:Idaho|US:IL:Illinois|US:IN:Indiana|US:IA:Iowa|US:KS:Kansas|US:KY:Kentucky|US:LA:Louisiana|US:ME:Maine|US:MD:Maryland|US:MA:Massachusetts|US:MI:Michigan|US:MN:Minnesota|US:MS:Mississippi|US:MO:Missouri|US:MT:Montana|US:NE:Nebraska|US:NV:Nevada|US:NH:New Hampshire|US:NJ:New Jersey|US:NM:New Mexico|US:NY:New York|US:NC:North Carolina|US:ND:North Dakota|US:OH:Ohio|US:OK:Oklahoma|US:OR:Oregon|US:PA:Pennsylvania|US:RI:Rhode Island|US:SC:South Carolina|US:SD:South Dakota|US:TN:Tennessee|US:TX:Texas|US:UT:Utah|US:VT:Vermont|US:VA:Virginia|US:WA:Washington|US:WV:West Virginia|US:WI:Wisconsin|US:WY:Wyoming|', 'ctl00_uxPlaceHolder_uxCustomerRegister_uxShippingCountryState_uxStateDrop', 'ctl00_uxPlaceHolder_uxCustomerRegister_uxShippingCountryState_uxStateText', countryCode, 'Select...', 'Other...');
    }
}

// Customized: Aug 28, 2011 - Added to validate ZIP when US is selected.
function validateZip(zipElement) {

    var shippingZipId = "ctl00_uxPlaceHolder_uxCustomerRegister_uxShippingZip";
    var shippinCountry = document.getElementById('ctl00_uxPlaceHolder_uxCustomerRegister_uxShippingCountryState_uxCountryDrop');
    var countryHidden = document.getElementById('ctl00_uxPlaceHolder_uxCustomerRegister_uxCountryState_uxCountryHidden');

    if (countryHidden.value == "US" && zipElement.id != shippingZipId && zipElement != null && zipElement.value.length > 0) {
        zipElement.value = zipElement.value.replace(/[^0-9\-]/g, '');
    }
    else if (shippinCountry != null && shippinCountry.options[shippinCountry.selectedIndex].value == "US" && zipElement != null && zipElement.value.length > 0) {
        zipElement.value = zipElement.value.replace(/[^0-9\-]/g, '');
    }
}

function populateState(defaultState, stateList, selectID, inputID, countrySelected, selectStateText, otherText ) {
  var selObj = document.getElementById(selectID);
  var inputObj = document.getElementById(inputID);
  var foundState = false;
  // Empty options just in case new drop down is shorter
  if ( selObj.type == 'select-one' ) {
    for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    selObj.options[0] = new Option(selectStateText,'');
    selObj.selectedIndex = 0;
  }
  // Populate the drop down with states from the selected country
  var stateLineArray = stateList.split("|");  // Split into lines
  var optionCntr = 1;
  var isSetState = false;
  for (var loop = 0; loop < stateLineArray.length - 1; loop++) 
  {
      lineArray = stateLineArray[loop].split(":");
      countryCode  = TrimString(lineArray[0]);
      stateCode    = TrimString(lineArray[1]);
      stateName    = TrimString(lineArray[2]);
      if (countrySelected == countryCode && countryCode != '' ) 
      {
          if ( stateCode != '' )
          {
            selObj.options[optionCntr] = new Option(stateName, stateCode);
          }
          // See if it's selected from a previous post
          if ( stateCode == defaultState && countryCode == countrySelected ) 
          {
            isSetState = true;
            selObj.selectedIndex = optionCntr;
          }
          foundState = true;
          optionCntr++
      }
  }
  
  if ( ! foundState ) 
  {
      selObj.style.display = 'none';
      inputObj.style.display = '';
      inputObj.value = defaultState;
  }
  else
  {
      selObj.options[selObj.options.length] = new Option( otherText, 'OT' );
      
      if( defaultState != '' && !isSetState )
      {
        selObj.style.display = '';
        selObj.selectedIndex = selObj.options.length - 1;
        inputObj.style.display = '';
        inputObj.value = defaultState;
      }
      else
      {
        selObj.style.display = '';
        inputObj.style.display = 'none';
      }
  }  
}
