//<!--
// JavaScript Document

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

//dropping password strength js here since this should be global
//ValidatePasswordInfo is called onsubmit to run through the rules clientside
//Optional parameters: (pass in '' for these if not using)
//	-sOldPassId - old password to check, this will enable the check to make sure new password is different than old.
//	-sErrorSpanId - HTML element where the error message will be written, an alert will be show regardless of this parameter being used.
function ValidatePasswordInfo(sFNameId, sLNameId, sOldPassId, sNewPassId, sNewPassConfirmId, sErrorSpanId)
{	var sFname = document.getElementById(sFNameId).value.toUpperCase().replace(/ /g,"");
	var sLname = document.getElementById(sLNameId).value.toUpperCase().replace(/ /g,"");
	var sOldPass;
	if(sOldPassId != '') sOldPass = document.getElementById(sOldPassId).value.toUpperCase().replace(/ /g,"");
	var sNewPass = document.getElementById(sNewPassId).value.toUpperCase().replace(/ /g,"");
	var sNewPassConfirm = document.getElementById(sNewPassConfirmId).value.toUpperCase().replace(/ /g,"");
	var sErrorMessages = '';
	//1-new pass must equal confirm pass
	if(sNewPass != sNewPassConfirm) sErrorMessages += 'New password must equal confirm new password.\n';
	//2-new pass must not equal fname
	if(sNewPass == sFname) sErrorMessages += 'New password must not equal first name (' + sFname + ').\n';
	//3-new pass must not equal lname
	if(sNewPass == sLname) sErrorMessages += 'New password must not equal last name (' + sLname + ').\n';
	//4-new pass must be at least 8 characters
	if(sNewPass.length < 8) sErrorMessages += 'New password must be at least 8 characters.\n';
	//5-new password should be new (optional check)
	if(sOldPassId != '') if(sNewPass == sOldPass) sErrorMessages += 'New password must not equal Old password.\n';
	
	//write to error span and show the alert
	if(sErrorSpanId != '') document.getElementById(sErrorSpanId).innerHTML=sErrorMessages;
	if(sErrorMessages != '')
	{	alert(sErrorMessages);
		return false;
	}
	return true;
}
//NoPOBox check is called from paymthd to ensure the ship-to address is not a po box when UPS is the ship method
var addrckNoPostBox = false; //when true the validpostaladdr function will deny po boxes
var addrck_streetid; //the following variables will store html element names to retrieve later
var addrck_cityid;
var addrck_stateid;
var addrck_zipid;
var addrck_destid;
function ValidPostalAddrNoPoBox(form_, streetid_, cityid_, stateid_, zipid_, destination_)
{   addrckNoPostBox = true;
    //disable po box check if the user didnt pick usps for shipping meth.
    try{
        var shipmeth = document.pymthd.X4;
        if(shipmeth)
            if(shipmeth.options[shipmeth.selectedIndex].value == "05") addrckNoPostBox = false;
    }
    catch(e){}
    ValidPostalAddr(form_, streetid_, cityid_, stateid_, zipid_, destination_);
}
//address validation
function ValidPostalAddr(form_, streetid_, cityid_, stateid_, zipid_, destination_)
{   //store passed in element identifiers
    addrck_streetid=streetid_;
    addrck_cityid=cityid_;
    addrck_stateid=stateid_;
    addrck_zipid=zipid_;
    addrck_destid=destination_;
    //find necessary elements on the page
    var oldStreet = document.getElementById(streetid_);
    var oldCity = document.getElementById(cityid_);
    var oldState = document.getElementById(stateid_);
    var oldZip = document.getElementById(zipid_);
    //make sure elements were found
    if(!(form_ && oldStreet && oldCity && oldState && oldZip))
    {   //elements not found, allow user to continue since we cannot properly validate inputs
        //alert("address check debug: submitting form, unable to find all html elements.");
        form_.submit();
        return;
    }
    //unhighlight the fields
    addrChkToggleFieldBackgrounds(false, oldStreet, oldCity, oldState, oldZip);
    
    //check for blanks
    if(oldStreet.value.replace(/ /,"")=="" || oldCity.value.replace(/ /,"")=="" || oldState.value.replace(/ /,"")=="" || oldZip.value.replace(/ /,"")=="")
    {	addrChkAllowResubmit();
	    addrChkToggleFieldBackgrounds(true, oldStreet, oldCity, oldState, oldZip);
	    alert("Please enter values for all address fields.");
        return;
    }
    //if no-po box is enabled then deny po box addresses
    if(addrckNoPostBox == true)
    {   var reg = new RegExp("^p(ost)?[\ \|\.]*o(ffice)?[\ \|\.]*(box)?.*","i");
        if(reg.test(oldStreet.value) == true)
        {   oldStreet.style.backgroundColor='#ffff99';
            alert("Shipping address must be a physical address for UPS shipping. Please enter a street address that is not a PO box.");
            oldStreet.focus();
            return;
        }
    }
    //see if an override was selected
    var or = document.getElementById("orAddrC");
    if(or)
    {   if(or.value=="1") 
        {
            //alert("address check debug: submitting form, override was selected.");
            form_.submit();            
            return;
        }
    }

    var Url;
    if(encodeURIComponent)
        Url = "/secure/validAddress.asp?s=" + encodeURIComponent(oldStreet.value) + "&c=" + encodeURIComponent(oldCity.value) + "&t=" + encodeURIComponent(oldState.value) + "&z=" + encodeURIComponent(oldZip.value) + "&dd=" + encodeURIComponent(destination_);
    else
        Url = "/secure/validAddress.asp?s=" + oldStreet.value + "&c=" + oldCity.value + "&t=" + oldState.value + "&z=" + oldZip.value + "&dd=" + encodeURIComponent(destination_);
        
    var DivId = destination_;
    var AJAX;
    try
    {   // Provide the XMLHttpRequest class for IE 5.x-6.x:
        if( typeof XMLHttpRequest == "undefined" ) XMLHttpRequest = function() {
          try { return new ActiveXObject("Msxml2.XMLHTTP.6.0") } catch(e) {}
          try { return new ActiveXObject("Msxml2.XMLHTTP.3.0") } catch(e) {}
          try { return new ActiveXObject("Msxml2.XMLHTTP") } catch(e) {}
          try { return new ActiveXObject("Microsoft.XMLHTTP") } catch(e) {}
          throw new Error( "This browser does not support XMLHttpRequest." )
        };
        AJAX = new XMLHttpRequest(); 
    }
    catch(e)
    {   //unsupported, allow the users to continue
        form_.submit();
        return;
    }
    AJAX.onreadystatechange = function()
    {   if(AJAX.readyState == 4 && AJAX.status == 200)
        {   //write out response to the page
            document.getElementById(DivId).innerHTML = AJAX.responseText;  
            //find the new elements from the response
            var valStatus = document.getElementById("modStat");
            var newStreet = document.getElementById("modStreet");
            var newCity = document.getElementById("modCity");
            var newState = document.getElementById("modState");
            var newZip = document.getElementById("modZip");
            //make sure elements were found
            if(valStatus && newStreet && newCity && newState && newZip)
            {   //overwrite inputs with returned values if status=t
                if(valStatus.value == "t")
                {   oldStreet.value = newStreet.value;
                    oldCity.value = newCity.value;
                    oldState.value = newState.value;
                    oldZip.value = newZip.value;
                    //alert("updated address" + newStreet.value);
		            //alert("address check debug: addr validated, disabling submit for testing.")
                    form_.submit();
                    return;
                }
                else
                {   //address not validated, notify the user
                    document.getElementById(DivId).className = "qas_mod";
                    document.getElementById(DivId).style.display = "";
                    oldStreet.focus();
		            addrChkAllowResubmit();
		            addrChkToggleFieldBackgrounds(true, oldStreet, oldCity, oldState, oldZip);
                    alert("Please review the address fields.");
                    return;
                }
            }
            else
            {   //error locating response fields, allow user to continue
                //alert("address check debug: submitting form, error processing xmlresponse." + Url);
                form_.submit();
                return;
            }
        }
    }
    AJAX.open("get", Url, true);
    AJAX.send(null);
}
function addrChkAllowResubmit()
{   try
    {   submitcount--; //decrement submit count so the user can try again, some of the affected pages disable the submit button using this variable.
    }
    catch(e){}
}
function addrChkToggleFieldBackgrounds(bHighlight_, Street_, City_, State_, Zip_)
{   if(bHighlight_)
    {   Street_.style.backgroundColor='#ffff99';
	    City_.style.backgroundColor='#ffff99';
	    State_.style.backgroundColor='#ffff99';
	    Zip_.style.backgroundColor='#ffff99';	
    }
    else
    {   Street_.style.backgroundColor='transparent';
	    City_.style.backgroundColor='transparent';
	    State_.style.backgroundColor='transparent';
	    Zip_.style.backgroundColor='transparent';	
    }		    
}
//user clicked the override button, set the hidden val and hide the address warning
function addrChkOverride(destination_)
{   var orf = document.getElementById("orAddrC");
    var d = document.getElementById(destination_);
    if(orf && d) 
    {	orf.value = 1;
        d.style.display = "none";
    }
    //allow submit if post box check is not enabled
    return !addrckNoPostBox;
}
//pages using asynchronous validation will directly call this when a recommended address is clicked
//recap page will call the helper function addrChk_Select_StoreFields first
function addrChk_Select(street_, city_, state_, zip_)
{   var Streete = document.getElementById(addrck_streetid);
    var Citye = document.getElementById(addrck_cityid);
    var Statee = document.getElementById(addrck_stateid);
    var Zipe = document.getElementById(addrck_zipid);
    //make sure elements were found
    if(Streete && Citye && Statee && Zipe)
    {   //populate fields and hide the div
        Streete.value=street_;
        Citye.value=city_;
        Statee.value=state_;
        Zipe.value=zip_;
    }
    if(addrck_destid!="")
    {   var Deste = document.getElementById(addrck_destid);
        if(Deste)
            Deste.style.display = "none";
    }
}
//called from recap page to assign html element identifiers before calling the select function
function addrChk_Select_StoreFields(sid_, cid_, stid_, zid_, street_, city_, state_, zip_)
{   addrck_streetid=sid_;
    addrck_cityid=cid_;
    addrck_stateid=stid_;
    addrck_zipid=zid_;
    addrck_destid="";
    //call function to assign values
    addrChk_Select(street_, city_, state_, zip_);
}
//-->
