var req; //variable used to establish an XML Http Request object

//this javascript function establishes a socket connection in which  
//php script communication takes place - it passes user input and 
//communicates back XML document results built by the php script
function loadXMLDoc(url) {
		    // branch for native XMLHttpRequest object
		    if (window.XMLHttpRequest) {
		        req = new XMLHttpRequest();
		        req.onreadystatechange = processReqChange;
		        req.open("GET", url, true);
		        req.send(null);
		    // branch for IE/Windows ActiveX version
		    } else if (window.ActiveXObject) {
		        req = new ActiveXObject("Microsoft.XMLHTTP");
		        
		        if (req) {
		        	
		            req.onreadystatechange = processReqChange;
		            req.open("GET", url, true);
		            req.send();
		        }
		        
		    }
		    
		}
		
//this JavaScript function processes the response from the php
//script - basically it parses the XML response into useable strings 		
function processReqChange() {
    // only if req shows "complete"
    if (req.readyState == 4) {  //redyState of 4 siginifies a completed script
        // only if "OK"
        if (req.status == 200) {  //status of 200 means that we have no errors in communication
        	
          // ...processing statements go here...
           
          //we are assigning our xml document to an object so that
          //we can parse the individual tags into useable variables 
	      response = req.responseXML.documentElement; 

	      method = response.getElementsByTagName('method')[0].firstChild.data; //this is the name of the original calling function
	      count = response.getElementsByTagName('count')[0].firstChild.data; //this is the value of the select that we are going to populate with data
	      id = response.getElementsByTagName('id')[0].firstChild.data;  //this is the primary keys of the sub-categories the value of our options
	      result = response.getElementsByTagName('result')[0].firstChild.data;  //this is the name of the sub-categories and is used as the display in our options
	      //now that we have parsed the data we must pass it back to our original calling function by using the eval method. 
	      eval(method + '(\'\', count, result, id)');
      
        } else {  //if status not 200 then we need to alert the error
            alert("There was a problem retrieving the XML data:\n" + req.statusText);  
        }
    } //end our "only process when finished" check
} //end processing function
		
//This is our processing function that is called on the select's onchange event
//It is expecting 4 valuse to be passed into the function, input is the primary key of the 
//category/subcategory selected in the current select, number is the number or the current select
//it is used to determine which additional selects need to be adjusted to keep our data correct, 
//response is our ';' seperated string of subcategory names this value is empty in our initial 
//call to this function, and finally id is our ';' seperated string of subcategory primary keys
//it is also empty in our initial call 		
function getsubcatsX(input, number, response, id) { //subcat processing function
	
	//current ajax linitations prevent recursion from being employed to 
	//create an unlimited number of subcategory selects - the limitation
	//involves the need to reference previous and next selects while still
	//allowing a user to choose a select at random - typically this meant 
	//that each ajax call required is own processing javascript function
	//as well as it's own processing php script - I was able to over come this 
	//limitation in part by passing all processing through a loop that labels
	//each of the selects when they are created - I pass that label number into 
	//my processing function which then runs its own loop to adjust the setting
	//of all of the selects based on the select whose change triggered the
	//javascript function. ultimately what this means is that in order to
	//prevent an infinate loop we have to set the ceiling for our loop counter
	//hense max_selects below.  Understand that 15 is a completely arbitrary 
	//number I could have set it to 1,000 or 1,000,000 if I had wanted to.
	//I chose 15 because I have yet to see a cart with 15 subcategories
	//and I figure easier to change the value than it is to waste
	//cpu and memory resources 
	
	//*******NOTE*********** - IF YOU CHANGE THIS NUMBER TO FACILITATE A 
	//CUSTOMERS REQUEST TO INCREASE THE NUMBER OF SUBCATEGORIES YOU MUST
	//ALSO INCREASE THE VAR "$max_sub_cats" IN THE "dispalyCategories" METHOD 
	//OF OUR PHP CLASS "Store" the file is named Store_Class.php
	var max_selects = 15; //to avoid possible errors do not change this value without reading comments above 
	
	//number is the current select that shopper just changed tot trigger this function call
	//therefore we need to adjust all selects below this one - meaning we need to adjust 
	//all selects with a higher subcat number - hence we add one to the current select and then 
	//process all selects with a value between count and max_selects
	var count = parseInt(number)+1;   
	if (response == ''){ 
		
	  	//starting over so disable all lower-level(high number)subcat selects and reset values
	   	for (var x = count; x <= max_selects; x++) {
			
			//first we can hide the subcategories from dispalying
			var item = document.getElementById("subcat"+x);
			if (item != null){
				item.style.display='none';
			}
			//next we remove all option data from the selects
			var sitem = document.getElementById("subcategory"+x);
			if (sitem != null){
				sitem.disabled = true;
				sitem.options.length=0;//value="";
			}
	   	}
	}//end if no response
	if (response != ''){ 
		 //necessary to restate this to keep user from being able to choose a 
	  	//category out of turn and there by throwing off ajax responses
  		for (var x = count; x <= max_selects; x++) {
	   		
  			//first we can hide the subcategories from dispalying by turning off display on div tags
  			//our div tags are labeled with id=subcat1 through id=subcat15
	   		var item = document.getElementById("subcat"+x);
	   		if (item != null){
	   			item.style.display='none';
	   		}
	   		//next we remove all option data from the selects
	   		//our selects are labbeled with id=subcategory1 through subcategory15
	   		var sitem = document.getElementById("subcategory"+x);
	   		if (sitem != null){
	   			sitem.disabled = true;
	   			sitem.value="";
	   		}
	   }
	   	//this is where we land when we have data from the database
	   	//we know this because response == 0 is the trigger set by 
	   	//our php function when our database query yeilds no results
	    if(response != 0) {
	    	//here we are resetting all of the lengths of all lower level selects
	    	//this keeps the latest round of returned data from being appended
	    	//to our select and thereby screwing up the whole system
	    	 for (var x = count; x <= max_selects; x++) {
	   			var sitem = document.getElementById("subcategory"+x);
		   		if (sitem != null){
		   			sitem.options.length = 0;
		   		}
	   		}
	    	var nameArray = response.split(";");  //this is the select options display - *category names
	    	var idArray = id.split(";");  //this is the select options value  - *category prikeys
	    	
	    	var index = 0; //this is our counter to track the number of options this select will have
	    	
	    	var current_select = document.getElementById("subcategory"+number); //this is the object of the current select the one we need to populate
	    	//THIS IS VERY IMPORTANT - if we do not set this options value to -1 
	    	//it will default to 0 and 0 represents all main level categories
	    	//if we remove this line of code and someone selects the first option
	    	//in our select our javascript passes the prikey of 0 as teh parent category
	    	//and our next select gets populated with main level categories and we 
	    	//start over again - except we are out of order a value of -1 however
	    	//simply makes the system believe that there are no further subcategories
	    	//avaliable, and that is fine
	    	current_select.options[index] = new Option('View All',-1);
	    	//could have used either array.length here they are always the same
	    	while(index < nameArray.length) {
 				//at this point we are looping through  our responses and building our list of options
			 	current_select.options[index+1] = new Option(nameArray[index],idArray[index]);
			 	index ++; //incermenting our counter
			 		
			 } // end while we have responses
			 //now that we have built our select we have to enable it
			 current_select.disabled = false;
			 
			 //And finally we need to turn or show the div tag for the current subcat display so that the select is visable
			 //***IMPORTANT NOTE:  at this point in processing "number" is actually equivilent to our original count.  To understand why
			 //you have to trace the call and logic of this function.  The first call to this function which occours onChange of the select
			 //when that event occours and none of this processing occours because response is blank.  So we add 1 to our number and assign it to count 
			 //we then call loadXMLDoc via the else clause below and we pass the value of count in the querystring.  Count gets passed around through 
			 //our other javascript functions and our php script.  Finally processReqChange() through the eval call passes count back to getsubcatsX as 
			 //the second parameter in our function which you can see above is number. Hence when we reach this point our value for number is actually
			 //our original value of count which of course is our original number +1 
			 var tag_display = document.getElementById("subcat"+number)
			 tag_display.style.display='block';
	    }
	    
	  } else { //again process is empty or 0
	    // Input mode
	    //we need to set up our query string	
	 	url = '../getsubcategory.php?q=' + input +'&count='+ count;
	    //and pass it to our ajax processing function
	    loadXMLDoc(url);
	  }	
}//end get category functions


//the toggle function is used to hide and dispaly subcategory list in a display of all product categories
//the function expects to be passed an id which identifies a subcategory list.  if that subcategory list is 
//visable then a call to toggle hides the sublist, if the list is hidden toggle dispalys the list.
//This function is used in the php Store class private method categoryList 
function toggle(id){
	
    ul = "ul_" + id; //this is the id of the list being acted upon
    tog = "toggle_" + id; //this is the id of the label holding our toggle char
    ulElement = document.getElementById(ul); //object holding the info about the list we wish to act upon
    
    if (ulElement){ //if we have a match
	    if (ulElement.className == 'closed'){ //if the list is "closed" or hidden
	    	//then we open the dispaly (ie display no longer hidden)
	    	ulElement.className = "open";
	    	//here we use innerHTML to change our toggle character when the sub list is open
	    	document.getElementById(tog).innerHTML = "-";
	    	//we also increase our font size becaus a 10px - does not look good
	    	var item = document.getElementById(tog);
	    	item.style.fontSize ="13px";
	    }
	    else{
	    	//our list is open so this toggle onclick call is a request to close the nested list
	        ulElement.className = "closed";
	        //we change our toggle character back to the plus
	        document.getElementById(tog).innerHTML = "+";
	        //we also reset the size of our toggle character - a 10px + is plenty large enough and looks like a 13px -
	        var item = document.getElementById(tog);
	        item.style.fontSize ="10px";
	    }//end else list is open so close it
    }//end if we found the document object
}//end our toggle function




//this function is used in our ajax buildCategories method to 
//automaticlly submit the sub-category selected in our ajax selects
//this autoSubmit function passes the sub-cat info to our dispaly module   
function autoSubmit(){
	document.categoryinfo.submit();
} 

// The next few functions set teh various form fields that need to be displayed based on the country a customer selects time prevents proper comments, come back and hit this code with explaination 		
function setStateFields() {
	
	if(document.forms.billing.billing_country.value == 'United States') {
		
		document.getElementById('usstate').style.display='block';		
		document.getElementById('intlstate').style.display='none';
		document.getElementById('canprov').style.display='none';
		
	}
    else if(document.forms.billing.billing_country.value == 'Canada') {

		document.getElementById('canprov').style.display='block';
		document.getElementById('intlstate').style.display='none';
		document.getElementById('usstate').style.display='none';

	}
    else { 
		
		document.getElementById('intlstate').style.display='block';
        	document.getElementById('canprov').style.display='none';
		document.getElementById('usstate').style.display='none'
    }
}

var req2; //variable used to establish an XML Http Request object

//this javascript function establishes a socket connection in which  
//php script communication takes place - it passes user input and 
//communicates back XML document results built by the php script
function loadXMLDoc2(url) {
	
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
    
        req2 = new XMLHttpRequest();
        req2.onreadystatechange = processReqChange2;
        req2.open("GET", url, true);
        req2.send(null);
        
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
    	
        req2 = new ActiveXObject("Microsoft.XMLHTTP");
        
        if (req2) {
        	
            req2.onreadystatechange = processReqChange2;
            req2.open("GET", url, true);
            req2.send();
        }
        
    }    
}
//this JavaScript function processes the response from the php
//script - basically it parses the XML response into useable strings 		
function processReqChange2() {
    // only if req2 shows "complete"
     
    if (req2.readyState == 4) {  //redyState of 4 siginifies a completed script
        if (req2.status == 200) {  //status of 200 means that we have no errors in communication
        	
          //we are assigning our xml document to an object so that
          //we can parse the individual tags into useable variables 
	      response = req2.responseXML.documentElement;
	      method = response.getElementsByTagName('method')[0].firstChild.data; //this is the name of the original calling function
	      result = response.getElementsByTagName('result')[0].firstChild.data;  //this is the name of the country code 
	     
	      //now that we have parsed the data we must pass it back to our original calling function by using the eval method. 
	      eval(method + '(\'\', result)');
      
        }else{  //if status not 200 then we need to alert the error
            alert("There was a problem retrieving the XML data:\n" + req2.statusText);  
        }
    } //end our "only process when finished" check
} //end processing function

function getCountryCode(input, response) {
	
	if (response != ''){ 
		if(response != 0) {
			//we found a match in our database
			document.getElementById('bfcc').value = "+" + response;		
			document.getElementById('bcc').value = "+" + response;
			//document.billing.billing_country_code.value = "+" + response;
			//document.billing.billing_fax_country_code.value = "+" + response;
		}
	}else{  //initial function call
		url = '../getcountrycode.php?var=' + input;  
		
		loadXMLDoc2(url);
	}
}

function changeFocus(){
	window.focus();	
}



// The next few functions set teh various form fields that need to be displayed based on the country a customer selects time prevents proper comments, come back and hit this code with explaination 		
function setShippingStateFields() {
	
	if(document.forms.billing.shipping_country.value == 'United States') {
		
		document.getElementById('shipusstate').style.display='block';		
		document.getElementById('shipintlstate').style.display='none';
		document.getElementById('shipcanprov').style.display='none';
		
	}
    else if(document.forms.billing.shipping_country.value == 'Canada') {

		document.getElementById('shipcanprov').style.display='block';
		document.getElementById('shipintlstate').style.display='none';
		document.getElementById('shipusstate').style.display='none';

	}
    else { 
		
		document.getElementById('shipintlstate').style.display='block';
        	document.getElementById('shipcanprov').style.display='none';
		document.getElementById('shipusstate').style.display='none'
    }
}

var req3; //variable used to establish an XML Http Request object

//this javascript function establishes a socket connection in which  
//php script communication takes place - it passes user input and 
//communicates back XML document results built by the php script
function loadXMLDoc3(url) {
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest) {
    	
        req3 = new XMLHttpRequest();
        req3.onreadystatechange = processReqChange3;
        req3.open("GET", url, true);
        req3.send(null);
        
    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
    	
        req3 = new ActiveXObject("Microsoft.XMLHTTP");
        
        if (req3) {
        	
            req3.onreadystatechange = processReqChange3;
            req3.open("GET", url, true);
            req3.send();
        }
        
    }    
}
//this JavaScript function processes the response from the php
//script - basically it parses the XML response into useable strings 		
function processReqChange3() {
    // only if req3 shows "complete"
    if (req3.readyState == 4) {  //redyState of 4 siginifies a completed script
        if (req3.status == 200) {  //status of 200 means that we have no errors in communication
        
          //we are assigning our xml document to an object so that
          //we can parse the individual tags into useable variables 
	      response = req3.responseXML.documentElement;
	      method = "getShippingCountryCode"; //this is the name of the original calling function
	      result = response.getElementsByTagName('result')[0].firstChild.data;  //this is the name of the country code 
	      
	      //now that we have parsed the data we must pass it back to our original calling function by using the eval method. 
	      eval(method + '(\'\', result)');
      
        }else{  //if status not 300 then we need to alert the error
            alert("There was a problem retrieving the XML data:\n" + req3.statusText);  
        }
    } //end our "only process when finished" check
} //end processing function

function getShippingCountryCode(input, response) {
	
	if (response != ''){ 
		if(response != 0) {  //we found a match in our database
			document.getElementById('sfcc').value = "+" + response;		
			document.getElementById('scc').value = "+" + response;
		}
	}else{  //initial function call
		url = '../getcountrycode.php?var=' + input;  
		loadXMLDoc3(url);
	}
}


function same_info() {

	document.forms.billing.shipping_fname.value = document.forms.billing.billing_fname.value;
	document.forms.billing.shipping_lname.value = document.forms.billing.billing_lname.value;	
	document.forms.billing.shipping_comapny.value = document.forms.billing.billing_company.value;
	document.forms.billing.shipping_country.options.value = document.forms.billing.billing_country.options.value;
	document.forms.billing.shipping_address1.value = document.forms.billing.billing_address1.value;
	document.forms.billing.shipping_address2.value = document.forms.billing.billing_address2.value;
	document.forms.billing.shipping_city.value = document.forms.billing.billing_city.value;
	document.forms.billing.shipping_istate.value = document.forms.billing.billing_istate.value;
	document.forms.billing.shipping_ustate.options.value = document.forms.billing.billing_ustate.options.value;
	document.forms.billing.shipping_prov.options.value = document.forms.billing.billing_prov.options.value;
	document.forms.billing.shipping_postal.value = document.forms.billing.billing_postal.value;
	document.forms.billing.shipping_phone.value = document.forms.billing.billing_phone.value;
	document.forms.billing.shipping_fax.value = document.forms.billing.billing_fax.value;

	document.forms.billing.shipping_country_code.value = document.forms.billing.billing_country_code.value;
	document.forms.billing.shipping_fax_country_code.value = document.forms.billing.billing_fax_country_code.value;

}







var req4;

	function loadXMLDoc4(url) {

	    // branch for native XMLHttpRequest object
	    if (window.XMLHttpRequest) {
	    	
	        req4 = new XMLHttpRequest();
	        req4.onreadystatechange = processReqChange4;
	        req4.open("GET", url, true);
	        req4.send(null);
	        
	    // branch for IE/Windows ActiveX version
	    } else if (window.ActiveXObject) {
	    	
	        req4 = new ActiveXObject("Microsoft.XMLHTTP");
	        
	        if (req4) {
	            req4.onreadystatechange = processReqChange4;
	            req4.open("GET", url, true);
	            req4.send();
	        }
	        
	    }
	    
	} // end load XML function
	
	function processReqChange4() {
		
	    if (req4.readyState == 4) {
	    	
	        // only if "OK"
	        
	        if (req4.status == 200) {
	       
	          // ...processing statements go here...
	            
		      response = req4.responseXML.documentElement;
					
		      method = response.getElementsByTagName('method')[0].firstChild.data;
	
		      result = response.getElementsByTagName('result')[0].firstChild.data;
				
		      eval(method + '(\'\', result)');
	      
	        } else {
	        	
	            alert("There was a problem retrieving the XML data:\n" + req4.statusText);
	            
	        }
	    }
	    
	}// end function
	
	
	function checkName(input, response) {

		  if (response != ''){ 
		
		   	if(response == 1) {  //we found a match in our database
				alert("That account name is already in our system.\n Please choose another."); 
				document.billing.username.value = ""; //reset account name
				document.billing.username.focus(); //set form focus
				
				return;
			}if(response ==0) {
				
			}
			
		  } else {  //initial function call
		  
		    // Input mode
		    	
		 	url = '../checkusername.php?q='+input;  
		    loadXMLDoc4(url);
		    
		  }
		
		}	
		
		
		function checkPassword(){
		
				if (document.billing.password.value.length >= 8){
					var regex2 = /\w/;  //we need at least one alphabetic characters 
					if (regex2.test(document.billing.password.value) == true){  //does contain  letter
						
						var regex3 = /\d/;  //we need at least one numseic character
						if (regex3.test(document.billing.password.value) == true){  //does contain numeric letter
							
							var regex4 = /\W/;  //we need at least one non-alphabetic characters 
							if (regex4.test(document.billing.password.value) == true){  //does contain lowercase letter
								
								
								
							}else{  //no  non-alphabetic characters 
								alert("INVALID PASSWORD: Your Password must contain at least one non-alphanumeric character"); 
								document.billing.password.value = ""; //reset password 
								document.billing.confirm_password.value = ""; //reset password  2
								document.billing.password.focus(); //set form focus
							}
							
						}else{//no  numeric characters 
							alert("INVALID PASSWORD: Your Password must contain at least one numeric character"); 
								document.billing.password.value = ""; //reset password 
								document.billing.confirm_password.value = ""; //reset password 2
								document.billing.password.focus(); //set form focus
						}
						
					}else{//no  uppercase characters 
						alert("INVALID PASSWORD: Your Password must contain at least one alphabetic character"); 
						document.billing.password.value = ""; //reset password 
						document.billing.confirm_password.value = ""; //reset password 2
						document.billing.password.focus(); //set form focus
					}
					
				}else{ //less than 8 characters
					alert("INVALID PASSWORD: Your Password must contain at least 8 characters"); 
						document.billing.password.value = ""; //reset password 
						document.billing.confirm_password.value = ""; //reset password 2
						document.billing.password.focus(); //set form focus
					}
			}
		






function validate_info(email, where){	
	
	var error_string = "";
	var error_fields = new Array();
	
	if (window.document.billing.billing_fname.value == "") {
		error_string += "You must provide your Billing First Name.\n";
		error_fields[error_fields.length] = "billing_fname";
	}
	
	if (window.document.billing.billing_lname.value == "") {
		error_string += "You must provide your Billing Last Name.\n";
		error_fields[error_fields.length] = "billing_lname";
	}
	
	
	
	if (window.document.billing.billing_address1.value == "") {
		error_string += "You must provide your Billing Address.\n";
		error_fields[error_fields.length] = "billining_address";
	}
	
	if (window.document.billing.billing_city.value == "") {
		error_string += "You must provide your Billing City.\n";
		error_fields[error_fields.length] = "billing_city";
	}
	
	
	if (!window.document.billing.billing_country.selectedIndex > 0)
	{
	error_string += "You must indicate your Billing Country.\n";
	error_fields[error_fields.length] = "billing_country";
	}
	
	n = document.billing.billing_country.selectedIndex;
	
	
	if (window.document.billing.billing_country[n].value == "United States")
	{
		if (!window.document.billing.billing_ustate.selectedIndex > 0)
		{
		error_string += "You must indicate your Billing State.\n";
		error_fields[error_fields.length] = "billing_ustate";
        }
    }

    if (window.document.billing.billing_country[n].value == "Canada")
	{
		if (!window.document.billing.billing_prov.selectedIndex > 0)
		{
		error_string += "You must indicate your Billing Provience.\n";
		error_fields[error_fields.length] = "billing_prov";
		}
	}

	if (window.document.billing.billing_country[n].value != "United States" && window.document.billing.billing_country[n].value != "Canada")
	{
    	if (window.document.billing.billing_istate.value == "") {
    		error_string += "You must provide your Billing State/Provience.\n";
    		error_fields[error_fields.length] = "billing_istate";
    	}
    }
	
    if (window.document.billing.billing_postal.value == "") {
		error_string += "You must provide your Billing Postal Code.\n";
		error_fields[error_fields.length] = "billing_postal";
	}
    
	if (window.document.billing.billing_phone.value == "") {
		error_string += "You must provide your Billing Phone Number.\n";
		error_fields[error_fields.length] = "billing_phone";
		}		

			
	if (window.document.billing.email.value == "") {
		error_string += "You must provide your Email Address.\n";
		error_fields[error_fields.length] = "email";
	}
		

	if (window.document.billing.shipping_fname.value == "") {
		error_string += "You must provide your Shipping First Name.\n";
		error_fields[error_fields.length] = "shipping_fname";
	}
	
	if (window.document.billing.shipping_lname.value == "") {
		error_string += "You must provide your Shipping Last Name.\n";
		error_fields[error_fields.length] = "shipping_lname";
	}
	
	
	
	if (window.document.billing.shipping_address1.value == "") {
		error_string += "You must provide your Shipping Address.\n";
		error_fields[error_fields.length] = "billining_address";
	}
	
	if (window.document.billing.shipping_city.value == "") {
		error_string += "You must provide your Shipping City.\n";
		error_fields[error_fields.length] = "shipping_city";
	}
	
	
	if (!window.document.billing.shipping_country.selectedIndex > 0)
	{
		error_string += "You must indicate your Shipping Country.\n";
		error_fields[error_fields.length] = "shipping_country";
	}
	
	n = document.billing.shipping_country.selectedIndex;
	
	
	if (window.document.billing.shipping_country[n].value == "United States")
	{
		if (!window.document.billing.shipping_ustate.selectedIndex > 0)
		{
		error_string += "You must indicate your Shipping State.\n";
		error_fields[error_fields.length] = "shipping_ustate";
        }
    }

    if (window.document.billing.shipping_country[n].value == "Canada")
	{
		if (!window.document.billing.shipping_prov.selectedIndex > 0)
		{
		error_string += "You must indicate your Shipping Provience.\n";
		error_fields[error_fields.length] = "shipping_prov";
		}
	}

	if (window.document.billing.shipping_country[n].value != "United States" && window.document.billing.shipping_country[n].value != "Canada")
	{
    	if (window.document.billing.shipping_istate.value == "") {
    		error_string += "You must provide your Shipping State/Provience.\n";
    		error_fields[error_fields.length] = "shipping_istate";
    	}
    }
    
    if (window.document.billing.shipping_postal.value == "") {
		error_string += "You must provide your Shipping Postal Code.\n";
		error_fields[error_fields.length] = "shipping_postal";
	}
		
	
	if(where == "R"){
	

		if (window.document.billing.username.value == "") {
			error_string += "You must provide your Account Username\n";
			error_fields[error_fields.length] = "username";
		}
		
		if (window.document.billing.password.value == "") {
			error_string += "You must provide an Account Password\n";
			error_fields[error_fields.length] = "password";
		}
		
		if (document.billing.password.value != window.document.billing.confirm_password.value) {
			error_string += "Your Passwords Do Not Match\n";
			error_fields[error_fields.length] = "password";
		}
		
		if(window.document.billing.security_question.selectedIndex < 1) {
			error_string += "You must select a Security Question\n";
			error_fields[error_fields.length] = "security_question";
		}
		
		if (document.billing.security_answer.value == "") {
			error_string += "You must provide an answer to the Security Question\n";
			error_fields[error_fields.length] = "security_answer";
		}
			
	}
		
	
	var emailok = "";
	var the_at = email.indexOf("@");
	var the_dot = email.lastIndexOf(".");
	var a_space = email.indexOf(" ");
	
	
	if ((the_at != -1) &&
	(the_at != 0) &&
	(the_dot != -1) &&
	(the_dot > the_at +1) &&
	(the_dot < email.length -1) &&
	(a_space == -1))
	{
	emailok == "1";
	} else {
	emailok == "";
	error_string += "Your email address is not in the correct format.\n";
	error_fields[error_fields.length] = "email";
	}
	
	if (error_string == "") {
		//no errors or omissions
		document.billing.submit();
	
	} else {
		
		error_string = "We found the following omissions in your form: \n" + error_string;
	
		var thefield = error_fields[0];
		
		alert(error_string);
	
		document.forms['billing'][thefield].focus();
		
	}

} // end function

function validate_payment(){	
	
	var error_string = "";
	var error_fields = new Array();
	
	
	if (document.billing.cc_lname.value == "") {
			error_string += "You must provide the Card Holders Last Name\n";
			error_fields[error_fields.length] = "cc_lname";	
	}	
	if (document.billing.cc_fname.value == "") {
			error_string += "You must provide the Card Holders First Name\n";
			error_fields[error_fields.length] = "cc_fname";
		
	}	
	if(document.billing.cc_card.selectedIndex < 1) {
			error_string += "You must select a Card\n";
			error_fields[error_fields.length] = "cc_card";
	}
	if (document.billing.cc_number.value == "") {
			error_string += "You must provide your Credit Card Number\n";
			error_fields[error_fields.length] = "cc_number";
	}
		
	if(document.billing.cc_month.selectedIndex < 1) {
			error_string += "You must select the card's expiration month\n";
			error_fields[error_fields.length] = "cc_month";
	}
	if(document.billing.cc_year.selectedIndex < 1) {
			error_string += "You must select the card's expiration year\n";
			error_fields[error_fields.length] = "cc_year";
	}
	if (document.billing.cc_code.value == "") {
			error_string += "You must provide your card's security code\n";
			error_fields[error_fields.length] = "cc_code";
	}
		
	var now = new Date();
	if (document.billing.cc_month.options[document.billing.cc_month.selectedIndex].value <= (now.getMonth())
		&& document.billing.cc_year.options[document.billing.cc_year.selectedIndex].value <= now.getFullYear()) {
			error_string += "You Entered an expiration date in the past\n";
			error_fields[error_fields.length] = "cc_month";
	}
		
	if(isNaN(document.billing.cc_number.value)){
		error_string += "You must enter a Valid credit card number\n";
		error_fields[error_fields.length] = "cc_number";
	}
	

	if (error_string == "") {
		//no errors or omissions
		document.billing.submit();
	
	} else {
		
		error_string = "We found the following omissions in your form: \n" + error_string;
	
		var thefield = error_fields[0];
		
		alert(error_string);
	
		document.forms['billing'][thefield].focus();
		
	}

} // end function


function getcvvwindow() {
	var pwindow = window.open("../sec_code.php","pwindow","location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no,width=575,height=250");
	pwindow.moveTo(20,20);
}

function setccName(){

	var fname_msg =  document.getElementById('bfn').value; 
	if (document.getElementById('bfn').value.length != ""){
		document.getElementById('ccfn').value = fname_msg;
		show('msg_div');
	}
	
	var lname_msg = document.getElementById('bln').value; 
	if (document.getElementById('bln').value.length != ""){
		document.getElementById('ccln').value = lname_msg;
		show('msg_div');
	}
}

function show(c) {
		if (document.getElementById && document.getElementById(c)!= null)
		node = document.getElementById(c).style.display='block';
		else if (document.layers && document.layers[c]!= null)
		document.layers[c].display = 'block';
		else if (document.all)          
		document.all[c].style.display = 'block';              
	}
	
function checkMethod(){
	if(document.forms.billing.found_by.value == 'Other') {
		document.getElementById('othermethod').style.display='block';
	}else{
		document.getElementById('othermethod').style.display='none';
	}
}



//////////////////////////////////////////////////////////////
//////////This begins functions of the registered customer

function checkAnswer(answer){

	if ((window.document.security.answer.value.toLowerCase()) != answer){
		alert("You have incorrectly answered your security question.  Please try again.");
		window.document.security.answer.value = "";
		window.document.security.answer.focus();
	}else{
		window.document.security.submit();
	}
}

function checkPassword(){
	if (window.document.newpassword.newpass.value.length < 5 ){
		alert("The Passwords must contain at least 5 characters.  Please enter a different password.");
		window.document.newpassword.newpass.value = "";
		window.document.newpassword.vnewpass.value = "";
		window.document.newpassword.newpass.focus();
	}else if (window.document.newpassword.newpass.value != window.document.newpassword.vnewpass.value){
		alert("The Passwords you entered do not match.  Please try again.");
		window.document.newpassword.newpass.value = "";
		window.document.newpassword.vnewpass.value = "";
		window.document.newpassword.newpass.focus();
	}else{
		document.newpassword.submit();
	}
}







function checkVariants(id, name, g){
	var error_string = "";
	var error_fields = new Array();
	
		
			var varaints = id.split(";");  //
			var names = name.split(";");  //
			var index = 0; //this is our counter to track the number of options this select will have
		    	
		
		    	//could have used either array.length here they are always the same
		    	while(index < varaints.length) {
		    		var item = document.getElementById(varaints[index])
		    		if(item.selectedIndex < 1) {
					error_string += "You must select a " +names[index]+ " \n";
					error_fields[error_fields.length] = varaints[index];
				} 
				index++;  	
		    	}
		    
		    if (error_string == "") {
		    var ment = document.getElementById(g);
				ment.submit();
				
			} else {
		
				error_string = "Before you can purchase this product:\n" + error_string;
	
				var thefield = error_fields[0];
		
				alert(error_string);
	
			//document.forms['billing'][thefield].focus();
		
			}
		}

		
function getPwindow(site, product, rule) {
	//alert("http://"+ site + "/comment.php?product_id="+ product + "&comments=" +rule+"");
	var pwindow = window.open("http://"+ site + "/comment.php?product_id="+ product + "&comments=" +rule+"","pwindow","location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no,width=325,height=475");
	reload_flag = 1;
}



	










var req5;

	function loadXMLDoc5(url) {

	    // branch for native XMLHttpRequest object
	    if (window.XMLHttpRequest) {
	    	
	        req5 = new XMLHttpRequest();
	        req5.onreadystatechange = processReqChange5;
	        req5.open("GET", url, true);
	        req5.send(null);
	        
	    // branch for IE/Windows ActiveX version
	    } else if (window.ActiveXObject) {
	    	
	        req5 = new ActiveXObject("Microsoft.XMLHTTP");
	        
	        if (req5) {
	            req5.onreadystatechange = processReqChange5;
	            req5.open("GET", url, true);
	            req5.send();
	        }
	        
	    }
	    
	} // end load XML function
	
	function processReqChange5() {
		
	    if (req5.readyState == 4) {
	    	
	        // only if "OK"
	        
	        if (req5.status == 200) {
	       
	          // ...processing statements go here...
	            
		      response = req5.responseXML.documentElement;
					
		      method = response.getElementsByTagName('method')[0].firstChild.data;
			//alert(method); 
		      result = response.getElementsByTagName('result')[0].firstChild.data;
				
		      eval(method + '(\'\',  \'\', result)');
	      
	        } else {
	        	
	            alert("There was a problem retrieving the XML data:\n" + req5.statusText);
	            
	        }
	    }
	    
	}// end function
	
	
	function alertIt(val, input, response) {

		  if (response != ''){ 
		
		   
				alert(response); 
				
		  } else {  //initial function call
		  
		    // Input mode
		    	
		 	url = '../couponprocess.php?q='+input;  
		    loadXMLDoc5(url);
		    
		  }
		
	}	
