function isArray(obj) {
	//alert(obj.constructor.toString());
   	if (obj.constructor.toString().indexOf("native") == -1)
      	return false;
   	else
        return true;
}

function checkAll(field) {
	if(field == undefined) {
		alert("Uh, seriously Steph?  What are you trying to check?");
	}
	else if(isArray(field)) {
		field.checked = true
		var state = true;
		for (i = 0; i < field.length; i++) {
			if(field[i].checked == false) {
				state = false;
				break;
			}
		}
		if(state != true) {
			for (j = 0; j < field.length; j++) {
				field[j].checked = true;
			} 
		} else {
			for (k = 0; k < field.length; k++) {
				field[k].checked = false;
			}
		}
	} else {
		alert("C'mon Steph, just click the check box!");
	}
}

function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}

function handleError(errorMessage, url, line) {
	alert('There has been a javascript error: \n\n' + errorMessage + ' \n\nPage: ' + url + ' \n\nLine: ' + line);
    	return true;
}


function prodSearch(inStock){
	if(inStock==-1) {
		alert('One or more of the items in your order is no longer available.  Please select a different item.');
		return false;
	} else {
		return true;
	}
}

function validateQuant(selection,name){
	
	var itemName = name+'|num_stock';	
	var stock = document.getElementById(itemName).value;
	
	if(stock!=0 && (stock-selection.value)<0){ 
		selection.value=stock;
		alert('You have requested an amount greater than what is in stock.');
	}	
}

function validatePPQuant(selection){
	
	if(selection.value>1){
		selection.value=1;
		alert('Please order one Perpetual Plaque at a time.');
		return false;
	}
	return true;
}

function largeQuant(selection){
	
	if(selection.value>20){
		alert('For large orders you can call Customer Service for help with engraving!  This is an option, it is NOT REQUIRED.');
		return false;
	}
	return true;
}

function validateEngravingApproval(){

	if(document.engravingApproval.value==''){
	
		alert('Please approve your engravings.');
		return false;
	}
	return true;
}

function validateShipping(){
	
	if(isNaN(document.shippingFORM.numberofitems.value) || isNaN(document.shippingFORM.zipcode.value)){
		
		alert('Please input numbers only.');
		return false;
		
	}else{
		if(document.shippingFORM.numberofitems.value==""){
			alert('Please enter an estimated number of items.');
		return false;
		}
	
		if(document.shippingFORM.zipcode.value==""){
			alert('Please enter a delivery Zip Code.');
		return false;
		}
		
	}
	
	return true;
}

function validateSideShipping(){

	if(isNaN(document.sideshippingFORM.numberofitems.value) || isNaN(document.sideshippingFORM.zipcode.value)){
		
		alert('Please input valid numbers.');
		return false;	
		
	}else{
		
		if(document.sideshippingFORM.numberofitems.value==""){
			alert('Please enter an esitmated number of items.');
		return false;
		}
	
		if(document.sideshippingFORM.zipcode.value==""){
			alert('Please enter a Zip Code.');
		return false;
		}
		
	}
	
return true;
}


function validateConfirm(){

	if(document.theFORM.ccNumber.value==""){
		//alert('Please enter a credit card number.');
	//return false;
	}

return true;
}

function validateCheckout(){

	if(document.theFORM.firstName.value==""){
		alert('Please enter a Billing First Name.');
	return false;
	}

	if(document.theFORM.sfirstName.value==""){
		alert('Please enter a Shipping First Name.');
	return false;
	}
	
	
	if(document.theFORM.lastName.value==""){
		alert('Please enter a Billing Last Name.');
	return false;
	}

	if(document.theFORM.slastName.value==""){
		alert('Please enter a Shipping Last Name.');
	return false;
	}
	
	if(document.theFORM.address1.value==""){
		alert('Please enter a Billing Address.');
	return false;
	}
	
	if(document.theFORM.saddress1.value==""){
		alert('Please enter a Shipping Address.');
	return false;
	}
	
	if(document.theFORM.city.value==""){
		alert('Please enter a Billing City.');
	return false;
	}	
	
	if(document.theFORM.scity.value==""){
		alert('Please enter a Shipping City.');
	return false;
	}	
	
	if(document.theFORM.state.value=="SEL"){
		alert('Please enter a Billing State.');
	return false;
	}	

	if(document.theFORM.sstate.value=="SEL"){
		alert('Please enter a Shipping State.');
	return false;
	}
		
	if(document.theFORM.zip.value==""){
		alert('Please enter a Billing Zip.');
	return false;
	}	

	if(document.theFORM.szip.value==""){
		alert('Please enter a Shipping Zip.');
	return false;
	}	

	if(document.theFORM.phone.value==""){
		alert('Please enter a Billing Phone.');
	return false;
	}	

	if(document.theFORM.sphone.value==""){
		alert('Please enter a Shipping Phone.');
	return false;
	}	

	if(document.theFORM.email.value==""){
		alert('Please enter a Billing E-mail.');
	return false;
	}	
	
	if(document.theFORM.email.value!=""){
	var x = document.theFORM.email.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(x)){ 
			alert('Please add a valid Billing E-mail.');
			return false;
		}
	}

	if(document.theFORM.semail.value==""){
		alert('Please enter a Shipping E-mail.');
	return false;
	}	
	
	if(document.theFORM.semail.value!=""){
	var x = document.theFORM.semail.value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!filter.test(x)){ 
			alert('Please add a valid Shipping E-mail.');
			return false;
		}
	}

return true;
}


function populateSubImprints(key, q, maininput, line, set){

	for(i=0; i<q; i++){

		var subelementname=key +"|individualline" + line + "|" + eval(i + set);
		//alert(subelementname);
		//alert(maininput);
	
		var mainInput = document.getElementById(maininput);
		var subElement = document.getElementById(subelementname);
		
		if(mainInput.value!=''){//if the masterline formfield field is NULL then don't execute this script	
			subElement.value = mainInput.value;
		}
			
	}

}



function autoClear(key, q, maininput, line, set){

//	alert("key: "+key);
//	alert("q: " + q);
//	alert("maininput: " + maininput); 
//	alert("line: " + line);
//	alert("set: " + set) ;
	
	
	for(i=0; i<q; i++){

		if(set!=''){		
			var currentline=eval(i) + eval(set);
		}else{
			
			var currentline=eval(i);
		
		}
		
		var subelementname=key +"|individualline" + line + "|" + currentline;			
		//alert("i:" + i);
		//alert("set: " + set);
		//alert("suben:" + subelementname);
		
		var mainInput = document.getElementById(maininput);
		var subElement = document.getElementById(subelementname);
		
		if( mainInput.value!=''){//only update the subimprints if the mainline is not blank
		
			subElement.value = '';
		
		}
			
	}
	
	//once they type in the box, we lock it on the subimprints
	unlockSubImprints(key, q, maininput, line, set);

}

function autoWrite(key, q, maininput, line, set){

//	alert("key: "+key);
//	alert("q: " + q);
//	alert("maininput: " + maininput); 
//	alert("line: " + line);
//	alert("set: " + set) ;
	
	
	for(i=0; i<q; i++){

		if(set!=''){		
			var currentline=eval(i) + eval(set);
		}else{			
			var currentline=eval(i);		
		}
		
		var subelementname=key +"|individualline" + line + "|" + currentline;			
		//alert("i:" + i);
		//alert("set: " + set);
		//alert("suben:" + subelementname);
		
		var mainInput = document.getElementById(maininput);
		var subElement = document.getElementById(subelementname);
		
		//if( mainInput.value!=''){//only update the subimprints if the mainline is not blank
		
			subElement.value = mainInput.value;
		
		//}
			
	}
	
	//once they type in the box, we lock it on the subimprints
	lockSubImprints(key, q, maininput, line, set);
}

function refill(key, q, maininput, line, set){

//	alert("key: "+key);
//	alert("q: " + q);
//	alert("maininput: " + maininput); 
//	alert("line: " + line);
//	alert("set: " + set) ;
	
	
	for(i=0; i<q; i++){

		if(set!=''){		
			var currentline=eval(i) + eval(set);
		}else{
			
			var currentline=eval(i);
		
		}
		
		var subelementname=key +"|individualline" + line + "|" + currentline;			
		//alert("i:" + i);
		//alert("set: " + set);
		//alert("suben:" + subelementname);
		
		var mainInput = document.getElementById(maininput);
		var subElement = document.getElementById(subelementname);
		
		if( (mainInput.value!='') && (subElement.value == mainInput.value)){//only update the subimprints if the mainline is not blank
						
			subElement.value = mainInput.value;
			
		}else if((mainInput.value!='') && (subElement.value != mainInput.value)){
			
			//alert(subelementname);
			unlockSingleSubImprints(subelementname);
		
		}
				
	}

}


function unlockSubImprints(key, q, maininput, line, set){

	for(i=0; i<q; i++){

		
		if(set!=''){		
			var currentline=eval(i) + eval(set);
		}else{
			
			var currentline=eval(i);
		
		}
		
		var subelementname=key +"|individualline" + line + "|" + currentline;
		
		var mainInput = document.getElementById(maininput);
		var subElement = document.getElementById(subelementname);
		
		//subelementname=key +"|individualline" + line + "|" + (i + set);
		//alert(subelementname);
		subElement.readOnly=false;
		subElement.style.backgroundColor="white";
		
	}

	//populateSubImprints(key, q, maininput, line);

}

function lockSubImprints(key, q, maininput, line, set){

	for(i=0; i<q; i++){

		//alert(set);
		if(set!=''){		
			var currentline=eval(i) + eval(set);
		}else{
			var currentline=eval(i);
		
		}
		
		var subelementname=key +"|individualline" + line + "|" + currentline;
		
		var mainInput = document.getElementById(maininput);
		var subElement = document.getElementById(subelementname);
		
		if( mainInput.value != ''){//only update the subimprints if the mainline is not blank
			subElement.readOnly=true;
			subElement.style.backgroundColor="#EFEFEF";
		}
		
	}

}

function unlockSingleSubImprints(maininput){

		//alert(maininput);
		var subelementname=maininput;
		//alert(subelementname);
		
		var mainInput = document.getElementById(maininput);
		var subElement = document.getElementById(subelementname);

		subElement.readOnly=false;
		subElement.style.backgroundColor="#FFFF99";
	
}

function showDiv(DIVNAME){

	var divtoshow=document.getElementById(DIVNAME);

	if (divtoshow.style.display=="block"){
		divtoshow.style.display="none";
	}else{
		divtoshow.style.display="block";
	}
}


function hideDiv(DIVNAME){

	var divtohide=document.getElementById(DIVNAME);
	divtohide.style.display="none";

}

function showAdminDiv(DIVNAME){

	divtoshow=document.getElementById(DIVNAME);

	//if (divtoshow.style.display=="block"){
	//	divtoshow.style.display="none";
	//}else{
		divtoshow.style.display="block";
	//}
}


function hideAdminDiv(DIVNAME){

	divtohide=document.getElementById(DIVNAME);
	divtohide.style.display="none";

}

function ShippingEqualsBilling(){

	//alert('shipping is billing.');
	document.theFORM.sfirstName.value=document.theFORM.firstName.value;
	document.theFORM.slastName.value=document.theFORM.lastName.value;
	document.theFORM.saddress1.value=document.theFORM.address1.value;
	document.theFORM.saddress2.value=document.theFORM.address2.value;
	document.theFORM.scity.value=document.theFORM.city.value;
	document.theFORM.sstate.value=document.theFORM.state.value;
	document.theFORM.szip.value=document.theFORM.zip.value;
	document.theFORM.scompany.value=document.theFORM.company.value;
	document.theFORM.sphone.value=document.theFORM.phone.value;
	document.theFORM.sfax.value=document.theFORM.fax.value;
	document.theFORM.semail.value=document.theFORM.email.value;
}


function ClearShipping(){

	document.theFORM.sfirstName.value="";
	document.theFORM.slastName.value="";
	document.theFORM.saddress1.value="";
	document.theFORM.saddress2.value="";
	document.theFORM.scity.value="";
	document.theFORM.sstate.value="";
	document.theFORM.szip.value="";
	document.theFORM.scompany.value="";
	document.theFORM.sphone.value="";
	document.theFORM.sfax.value="";
	document.theFORM.semail.value="";


}

function AutoCatalogSubmit(formname){

	eval("document." + formname + ".submit()");

}

function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}


function ConfirmInput(){
}

function ShowText(selection,sub){ 
	if(sub){  
		// find index of current form element
		for (var i = 0; i < document.theCart.elements.length; i++) {
			if (document.theCart.elements[i].name == selection.name) {
				var selectionIndex=i;
			}
		}												
		// find specific form element and adjust index
		var splitArray = selection.name.split('|');
		if(splitArray[1]=='fontsize' || splitArray[1]=='sub_fontsize'){
			selectionIndex += 3;
		}
		if(splitArray[1]=='fontface' || splitArray[1]=='sub_fontface'){
			selectionIndex += 2;
		}
		// find line number of current element
		var line = splitArray[2];							
		// find form values
		var sizeSelect=document.theCart[selectionIndex-3];
		var size=sizeSelect.options[sizeSelect.selectedIndex].value;
		var faceSelect=document.theCart[selectionIndex-2];
		var face=faceSelect.options[faceSelect.selectedIndex].value;
		var styleB=document.theCart[selectionIndex-1].checked;
		var styleI=document.theCart[selectionIndex].checked;
		// choose right image
		ChangeGIF(size,face,styleB,styleI,line);
	}
}

function ChangeGIF(size,face,styleB,styleI,line){	
	// if line var has brackets, remove
	if(line.search(/\[/)) {   
		line = line.substr(0,1);
	}  
	// hide all <div>
	hideDiv('st'+line);hideDiv('stb'+line);hideDiv('sti'+line);hideDiv('stbi'+line);
	hideDiv('mt'+line);hideDiv('mtb'+line);hideDiv('mti'+line);hideDiv('mtbi'+line);
	hideDiv('sa'+line);hideDiv('sab'+line);hideDiv('sai'+line);hideDiv('sabi'+line);
	hideDiv('ma'+line);hideDiv('mab'+line);hideDiv('mai'+line);hideDiv('mabi'+line);
	// show correct <div>
	if(size=="s" && face=="t" && styleB && styleI){
		showDiv('stbi'+line);
	}		
	else if(size=="s" && face=="t" && styleB){
		showDiv('stb'+line);
	}		
	else if(size=="s" && face=="t" && styleI){
		showDiv('sti'+line);
	}		
	else if(size=="s" && face=="t"){
		showDiv('st'+line);
	}		
	else if(size=="m" && face=="t" && styleB && styleI){
		showDiv('mtbi'+line);
	}		
	else if(size=="m" && face=="t" && styleB){
		showDiv('mtb'+line);
	}		
	else if(size=="m" && face=="t" && styleI){
		showDiv('mti'+line);
	}		
	else if(size=="m" && face=="t"){
		showDiv('mt'+line);
	}		
	else if(size=="s" && face=="a" && styleB && styleI){
		showDiv('sabi'+line);
	}		
	else if(size=="s" && face=="a" && styleB){
		showDiv('sab'+line);
	}		
	else if(size=="s" && face=="a" && styleI){
		showDiv('sai'+line);
	}		
	else if(size=="s" && face=="a"){
		showDiv('sa'+line);
	}		
	else if(size=="m" && face=="a" && styleB && styleI){
		showDiv('mabi'+line);
	}		
	else if(size=="m" && face=="a" && styleB){
		showDiv('mab'+line);
	}		
	else if(size=="m" && face=="a" && styleI){
		showDiv('mai'+line);
	}		
	else {
		showDiv('ma'+line);
	}		
}
