
function validateAddresses() {
    try {
        //billing
        var s_error = "Please check required fields: ";
        var sBadBillingFields = "";
        sBadBillingFields = isEmpty('Ecom_BillTo_Postal_Name_First', 'First Name', sBadBillingFields);
        sBadBillingFields = isEmpty('s_addressee', 'Addressee', sBadBillingFields);
        sBadBillingFields = isEmpty('s_addressee', 'Addressee', sBadBillingFields);
        sBadBillingFields = isEmpty('s_addressee', 'Addressee', sBadBillingFields);
        sBadBillingFields = isEmpty('s_addressee', 'Addressee', sBadBillingFields);
        sBadBillingFields = isEmpty('s_address_street1', 'Street address', sBadBillingFields);
        sBadBillingFields = isEmpty('s_address_city', 'City', sBadBillingFields);
        
       
    } catch (Error) {
        return true;
    }
}

function getSelectValue(id) /*:String*/ {
    //returns the value in the list, otherwise empty string
    try {
        var itemvalue = "";
        var formField = document.getElementById(id);
        if (!formField) return "";
        if (formField.selectedIndex >= 0) {
            return formField[formField.selectedIndex].value;
        } else return "";
    } catch (Error) {
        return "";
    }
}

function getRadioInputValue(elementID)  /*:String*/{

    // return the value of the radio button that is checked
    // return an empty string if none are checked, or
    // there are no radio buttons
    try {
        var oneRadioObj = document.getElementById(elementID);
        if (!oneRadioObj) return "";
        var radioname = oneRadioObj.name;
        var suggestion_srcRadioObj = document.getElementsByName(radioname);
        if (!suggestion_srcRadioObj) return "";
        var radioLength = suggestion_srcRadioObj.length;
        if (radioLength == undefined) {
            if (suggestion_srcRadioObj.checked) {
                return suggestion_srcRadioObj.value;
            } else {
                return "";
            }
        } else if (radioLength > 0) {
            for (var i = 0; i < radioLength; i++) {
                if (suggestion_srcRadioObj[i].checked) {
                    return suggestion_srcRadioObj[i].value;
                }
            }
        } else return "";
    } catch (e) { return ""; }
}

function getCheckBoxValue(elementID)  /*:String*/{

    // return the value of the checkbox that is checked
    // return an empty string if not checked or doesn't exist
    try {
        var checkboxObj = document.getElementById(elementID);
        if (!checkboxObj) return "";
        if (checkboxObj.checked) {
            return checkboxObj.value;
        } else {
            return "";
        }
    } catch (e) { return ""; }
}

function isEmpty(id, friendlyName, errText){
    try {
        //billing
        var formField = document.getElementById(id);
        var fieldType = formField.type;
        var value = "";
        if (fieldType == "text" || fieldType == "hidden" || fieldType == "password") {
            value=formField.value;
        } else if (fieldType == "checkbox") {
            value=(formField.checked)?formField.value:"";
        } else {
            formField.innerHTML = "";
        }

        
        var fieldValue=document.getElementById(id).value+"";
        if (fieldValue != "") {
            errText = (errText == "") ? errText + friendlyName : ", " + friendlyName;
        }  
       return errText;
    } catch (Error) {
        return errText;
    }
}
	 function checkSameAddress() {
	     try{
	     //if addresses are the same, only submit one
	     //else make sure IDs are diff
	     var sameaddressID=true;
	     var addressesMatchOK = (
	        compareTextField('s_attention','s_ship_attention') &&
	        compareTextField('s_addressee','s_ship_addressee') &&
            compareTextField('Ecom_BillTo_Postal_Street_Line1', 'Ecom_ShipTo_Postal_Street_Line1') &&
            compareTextField('Ecom_BillTo_Postal_Street_Line2', 'Ecom_ShipTo_Postal_Street_Line2') &&
            compareTextField('Ecom_BillTo_Postal_Street_Line3', 'Ecom_ShipTo_Postal_Street_Line3') &&
            compareTextField('Ecom_BillTo_Postal_City', 'Ecom_ShipTo_Postal_City') &&
            compareTextField('Ecom_BillTo_Postal_StateProv', 'Ecom_ShipTo_Postal_StateProv') &&
            compareTextField('Ecom_BillTo_Postal_PostalCode', 'Ecom_ShipTo_Postal_PostalCode') &&
            document.address.s_ship_state.selectedIndex == document.address.s_bill_state.selectedIndex &&
            document.address.Ecom_ShipTo_Postal_CountryCode.selectedIndex == document.address.Ecom_BillTo_Postal_CountryCode.selectedIndex
            );

	     var shipaddressIsEmpty = (
	        document.getElementById('s_ship_addressee').value == "" ||
	        document.getElementById('Ecom_ShipTo_Postal_Street_Line1').value == "" ||
	        document.getElementById('Ecom_ShipTo_Postal_City').value == "" ||
	        document.getElementById('s_ship_addressee').value == ""
	        );
	        	        	        	        
         
         sameaddressID=compareTextField('i_bill_addr_id','i_ship_addr_id');
         if ((addressesMatchOK && sameaddressID) || shipaddressIsEmpty) {
             //mark billing as shipping & billing
             //mark shipping as shipping & billing
             document.address.b_isDefaultShippingYes.checked = true;
             document.address.b_isDefaultBillingYes.Checked = true;
             document.address.i_ship_addr_id.value = "0";
             document.address.b_ship_isDefaultShippingNo.checked = true;
             document.address.b_ship_isDefaultBillingNo.Checked = true;
             clearshippingaddress();
             return true;
         } else if (!addressesMatchOK && sameaddressID && !shipaddressIsEmpty) {
             document.address.i_ship_addr_id.value = "0";  //make a new address

             //one billing
             if (document.address.b_isDefaultBillingYes.checked) {
                 document.address.b_ship_isDefaultBillingNo.checked = true;
             } else if (document.address.b_ship_isDefaultBillingYes.checked) {
                 document.address.b_isDefaultBillingNo.checked = true;
             } else {
                 document.address.b_isDefaultBillingYes.checked = true;
                 document.address.b_ship_isDefaultBillingNo.checked = false;
             }

             //one shipping
             if (document.address.b_ship_isDefaultShippingYes.checked) {
                 document.address.b_isDefaultShippingNo.checked = true;
             } else if (document.address.b_isDefaultShippingYes.checked) {
                 document.address.b_ship_isDefaultShippingNo.checked = true;
             } else {
                 document.address.b_ship_isDefaultShippingYes.checked = true;
                 document.address.b_isDefaultShippingNo.checked = false;
             }
             return true;
         } else {
             return false;
         }     
	     } catch(Error){
	        return true;
	     }
	 }
	 
	 function copybilltoship() {
	 try{
	    //copy only if the shipping is currently blank
	    //if (document.address.s_ship_label=="" || document.address.Ecom_BillTo_Postal_Street_Line1.value=="") {
	    //    document.address.s_ship_label="Shipping";
            // copies all billing information to shipping information
            //copyfield('s_attention', 's_ship_attention');
            //copyfield('s_addressee', 's_ship_addressee');
            //copyfield('Ecom_BillTo_Postal_Street_Line1', 'Ecom_ShipTo_Postal_Street_Line1');
            //copyfield('Ecom_BillTo_Postal_Street_Line2', 'Ecom_ShipTo_Postal_Street_Line2');
            //copyfield('Ecom_BillTo_Postal_Street_Line3', 'Ecom_ShipTo_Postal_Street_Line3');
            //copyfield('Ecom_BillTo_Postal_City', 'Ecom_ShipTo_Postal_City');
            //copyfield('Ecom_BillTo_Postal_StateProv', 'Ecom_ShipTo_Postal_StateProv');
            //copyfield('Ecom_BillTo_Postal_PostalCode', 'Ecom_ShipTo_Postal_PostalCode');
            //document.address.s_ship_state.selectedIndex = document.address.s_bill_state.selectedIndex;
            //document.address.Ecom_ShipTo_Postal_CountryCode.selectedIndex = document.address.Ecom_BillTo_Postal_CountryCode.selectedIndex;
        //}
     } catch(Error){
     }
    }

    function clearshippingaddress(){
      try{
        clearfield('s_ship_attention');
        clearfield('s_ship_addressee');
        clearfield('s_ship_label');
        clearfield('s_ship_phone');
        clearfield('s_ship_street1');
        clearfield('s_ship_street2');
        clearfield('s_ship_city');
        clearfield('s_ship_postalcode');
        clearfield('s_ship_state');
        clearfield('s_ship_stateprov');
        //clearfield.address.s_ship_state.selectedIndex = document.address.s_bill_state.selectedIndex;
        //clearfield.address.Ecom_ShipTo_Postal_CountryCode.selectedIndex = document.address.Ecom_BillTo_Postal_CountryCode.selectedIndex;
     } catch(Error){
     }
    }
    function checkDefaultShipping(id){
        try{
            var formField = document.getElementById(id);
            var fieldType = (document.getElementById(id))?formField.type:"";
            if (fieldType=="checkbox"){
                formField.checked= true;
            }
        }catch(Error){
        }
    }
    function updateaddress(){
     try{
        var s_company=(document.getElementById('Ecom_BillTo_Postal_Company'))? document.getElementById('Ecom_BillTo_Postal_Company').value : "";
        var s_addressee=document.getElementById('s_addressee').value;
        var s_attention=document.getElementById('s_attention').value;
        var fname=document.getElementById('Ecom_BillTo_Postal_Name_First').value;
        var lname=document.getElementById('Ecom_BillTo_Postal_Name_Last').value;
        if (s_company=="" && s_addressee==""){
            document.getElementById('s_addressee').value=fname+" " + lname;
            document.getElementById('s_attention').value="";
        } else if (s_addressee==""){
            document.getElementById('s_attention').value=fname+" " + lname;
            document.getElementById('s_addressee').value=s_company;
        }
     } catch(Error){
     }    
    }

    function getfieldvalue(id) /*String */  {
        //returns the value of this field or empty string if not found or no value
        try {
            var value = "";
            var formField = document.getElementById(id);
            if (!formField) return "";
            var fieldType = formField.type;
            if (fieldType == "text" || fieldType == "hidden" || fieldType == "password") {
                value=formField.value;
            } else if (fieldType == "radio") {
                value = getRadioInputValue(id);
            } else if (fieldType == "checkbox") {
                value = getCheckBoxValue(id);
            } else if (fieldType == "select-one") {
                value = getSelectValue(id);
            } else {
                value = formField.innerHTML;
            }
            return value;
        } catch (Error) {
            return "";
        }
    }


    function clearfield(id){
      try{
        var formField = document.getElementById(id);
        var fieldType = formField.type;
        if (fieldType == "text" || fieldType == "hidden" || fieldType == "password") {
            formField.value = "";
        } else if (fieldType=="checkbox"){
            formField.checked= false;
        }else {
            formField.innerHTML= "";
        }

      } catch(Error){
      }
    }
/*
    function setCountry(sCountryValue) {
        try {
            //set dropdown to entry with option value sCountryValue
            with (document.address.Ecom_BillTo_Postal_CountryCode) {
                for (i = 0; i < length; i++)
                    if (options[i].value == sCountryValue) {
                    selectedIndex = i;
                }
            }
        } catch (e) { }
    }
    */
    function select_listitem_with_valueOK(id,itemvalue){
      try{
        //returns true if it finds the value in the list, otherwise false
        var formField = document.getElementById(id);
        with (formField) {
            for (var i = 0; i < length; i++)
                if (options[i].value == itemvalue) {
                selectedIndex = i;
                return true;
            }
        }
        return false;
      } catch(Error){
        return false;
      }
    }

    function update_countries_selected() {
        try {
            var shipcountry = document.address.s_ship_country.value;
            with (document.address.Ecom_ShipTo_Postal_CountryCode) {
                for (i = 0; i < length; i++)
                    if (options[i].value == shipcountry) {
                    selectedIndex = i;
                }
            }

            var billcountry = document.address.s_address_country.value;
            with (document.address.Ecom_BillTo_Postal_CountryCode) {
                for (i = 0; i < length; i++)
                    if (options[i].value == billcountry) {
                    selectedIndex = i;
                }
            }
        } catch (e) { }
    }
    
    function change(id, newClass) {
        try {
            var identity = document.getElementById(id);
            identity.className = (identity.className != newClass) ? newClass : newClass;
        } catch (Error) {
        }
	}
	    
	function changeProdPic(id,s_product_id,picSize){
        try {
            var pic = document.getElementById(id);
            if (picSize=="B") {
				pic.src="/graphics/product/B"+s_product_id.substr(0,7)+".jpg";
				pic.width=750;
				pic.height=750;
            } else if (picSize=="P") {
				pic.src="/graphics/product/P"+s_product_id.substr(0,7)+".jpg";
				pic.width=250;
				pic.height=250;
            } else if (picSize=="S") {
				pic.src="/graphics/product/S"+s_product_id.substr(0,7)+".jpg";
				pic.width=125;
				pic.height=125;
            } else if (picSize=="TP") {
				pic.src="/graphics/product/smallprod/TP"+s_product_id.substr(0,7)+".jpg";
				pic.width=80;
				pic.height=80;
            } 
            
        } catch (Error) {
        }
	}
	
	function changeSRC(id, newPic) {
        try {
            var pic = document.getElementById(id);
            if (pic.src != newPic) pic.src = newPic;
        } catch (Error) {
        }
	}
	function changeSIZE(id, newH, newW) {
        try {
            var pic = document.getElementById(id);
            if (newH>0) pic.height = newH;
            if (newW>0) pic.width = newW;
        } catch (Error) {
        }
	}

	     function copyfield(idSource,idDestination){
	         try{
	            var sourceField = document.getElementById(idSource);
	            var newValue=sourceField.value;
	            var destinationField = document.getElementById(idDestination);
	            if (destinationField.type==sourceField.type){
	                var fieldType = destinationField.type;
	                if (fieldType=="text"){
	                    destinationField.value = newValue;
	                } else if (fieldType=="checkbox"){
	                    destinationField.checked= (destinationField.value==newValue || newValue==true || newValue=="true" || newValue==1 || newValue=="TRUE");
	                }else if (destinationField.nodeName=="SPAN"){
	                    destinationField.innerHTML= sourceField.innerHTML;
	                }
	            }
	         } catch(Error) {
	         }
	     }
	     function copyfield_iff_destination_empty(idSource,idDestination){
	         try{
	            var destinationField = document.getElementById(idDestination);
	            if (destinationField.type=="text"){
	               if (destinationField.value==''){
	                   copyfield(idSource,idDestination);
	               }
	            
	            }
	         }catch(Error){
	         }
	     }
	     function cv(id, newValue) {
	       //try{
	         id=id+"";
	         newValue=newValue+"";
	         if (id.substr(0,2)=="b_") {
	           //boolean, pick the right field
	           id = (newValue==true)?id+"Yes" : id + "No";
	         }
	         var formField = document.getElementById(id);
	         //need to check for select/checkboxes/radio fields here
	         var fieldType = formField.type;
	         if (fieldType=="text"){
	            formField.value = newValue;
	         } else if (fieldType=="checkbox"){
	            formField.checked= (formField.value==newValue || formField.value.toUpperCase()==newValue.toUpperCase() || newValue==true || newValue=="true" || newValue==1 || newValue=="TRUE");
	         }else if (formField.nodeName=="SPAN"){
	            formField.innerHTML= newValue;
	         }
	       //} catch(Error) {
	       //}
	     }
	     function checkform(obj)
         {
             if (obj.options[obj.options.selectedIndex].value == "0") {
                 if (obj.ID == "i_bill_addr_id1") {
                     change('new_billto', 'FormReq');
                 } else if (obj.ID == "i_mail_addr_id1") {
                     change('new_shipto', 'FormReq');
                 }
             }
         }
         function checkemail(obj,warningID,matchedText,mismatchedText){
            try{
                var sEmail=obj.value;
                var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
                var regex = new RegExp(emailReg);
                if (!regex.test(sEmail)){
                    cv(warningID, mismatchedText)
                } else {
                    cv(warningID, matchedText)
                }
                return true;
            }catch(Error){
            }
         }
         function compareTextField(id,id2){
             try {
            var formField = document.getElementById(id);
            var formField2 = document.getElementById(id2);
	         //need to check for select/checkboxes/radio fields here
            var fieldType = formField.type;
            //make sure not 0
	         return (formField.value == formField2.value && formField2.value != "0");
            }catch(Error){
                return false;
            }
         }
         

