// JavaScript Document

function setStyle(x)
{
document.getElementById(x).style.background="#EEEEEE";
//document.getElementById(x).value = "";
	if(document.getElementById(x).value == "username" || document.getElementById(x).value == "password"){
		document.getElementById(x).value = "";
	}
}
function setStyle_off(x, value)
{
document.getElementById(x).style.background="#ffffff";

	if(document.getElementById(x).value == ""){
	  document.getElementById(x).value = x;
	}
}

//this isn't being used at the moment
function goto(x){
	//alert(x);
	location.href = "productcatalogue/index.php?id=";
	document.write(location.href);
}

/*function validate_form(){
	
	var theForm = document.form[0]

   for(i=0; i<theForm.elements.length; i++){
   var alertText = ""
   alertText += "Element Type: " + theForm.elements[i].type + "\n"

      if(theForm.elements[i].type == "text" || theForm.elements[i].type == "textarea" || theForm.elements[i].type == "button"){
      alertText += "Element Value: " + theForm.elements[i].value + "\n"
      }
      else if(theForm.elements[i].type == "checkbox"){
      alertText += "Element Checked? " + theForm.elements[i].checked + "\n"
      }
      else if(theForm.elements[i].type == "select-one"){
      alertText += "Selected Option's Text: " + theForm.elements[i].options[theForm.elements[i].selectedIndex].text + "\n"
      }
   alert(alertText)
   }
   
}*/

function prompt_delete_item(sku, group){
	
	var path = "products_edit.php?item=" + sku
	var answer = confirm ("Are you sure you want to delete this item?...")
	if (answer){
		//document.write(location.href = "products_edit.php?item=" + sku + "");
		window.location = "products_edit.php?del=" + sku + "&group=" + group + ""
		//alert(sku);
	
	}
}

function prompt_delete_admin(){
	
	//var path = "" + page + "?" + get_var + "=" + id
	var answer = confirm ("Are you sure you want to delete this item?...")
	if (answer){
		//document.write(location.href = "product_group.php?delete_group");
		//window.location = "" + page + "?" + get_var;
		//alert(sku);
		document.forms[0].submit();
	
	}
}

function prompt_delete_href(message, go_to){
	
	var answer = confirm(message);
	if(answer){
		window.location=go_to;
	}		
}

function alert_test(){
	
	alert("hey");	
}

function submit_form(){
	var theForm = document.forms[0]
	theForm.submit();
}

function submit_cart_form(){
	var theForm = document.forms[1];
	theForm.submit();
}


function validate_form(){
	
	var theForm = document.forms[0]
	var alertText = ""
	var postageText = ""
	var giftboxText = ""
    var payprefText = ""
	
   for(i=0; i<theForm.elements.length; i++){
	
		if(theForm.elements[i].name != "voucher_code_1" && 
			theForm.elements[i].name != "voucher_code_2" && 
			theForm.elements[i].name != "voucher_code_3"){
		  
		  if(theForm.elements[i].type == "text" &&  theForm.elements[i].value == ""){
		  	alertText += "" + theForm.elements[i].name + " is required \n"
		  }
		  
		   if((theForm.elements[i].type == "radio" && theForm.elements[i].name == "postage_method") && 
			(theForm.elements[i+1].type == "radio" && theForm.elements[i+1].name == "postage_method") && 
			(theForm.elements[i].checked == false && theForm.elements[i+1].checked == false )  ){
		  	
			postageText += "" + theForm.elements[i].name + " is required \n"
		  }
		  
		  if((theForm.elements[i].type == "radio" && theForm.elements[i].name == "gift_box") && 
			(theForm.elements[i+1].type == "radio" && theForm.elements[i+1].name == "gift_box") && 
			(theForm.elements[i].checked == false && theForm.elements[i+1].checked == false )  ){
		  	
			giftboxText += "" + theForm.elements[i].name + " is required \n"
		  }
		  
		   if((theForm.elements[i].type == "radio" && theForm.elements[i].name == "pay_preference") && 
			(theForm.elements[i+1].type == "radio" && theForm.elements[i+1].name == "pay_preference") && 
			(theForm.elements[i+2].type == "radio" && theForm.elements[i+2].name == "pay_preference") && 
			(theForm.elements[i+3].type == "radio" && theForm.elements[i+3].name == "pay_preference") && 
			(theForm.elements[i].checked == false && theForm.elements[i+1].checked == false 
			&& theForm.elements[i+2].checked == false && theForm.elements[i+3].checked == false)  ){
		  
		  	
			payprefText += "" + theForm.elements[i].name + " is required \n"
		  }
		  
		}
   }
   	if(alertText != ""){
   		alert("all fields marked with \'*\' are required")
   	}else if(postageText != ""){
	   alert("please select a \'postage method\'")
	}else if(giftboxText != ""){
	   alert("please select a \'gift box\' requirement")
	}else if(payprefText != ""){
	   alert("please select a \'payment preference\' or select \'Add my Gift Card Code\' if you are paying with a Gift Card")
	}else{
		theForm.submit();
	}
}


function validate_form_single_page(){
	
	var theForm = document.forms[0];
	var alertText = "";
	var postageText = "";
	var giftboxText = "";
    var payprefText = "";
	var same_address = false;
	//var cc_selected = false;
	//var cc_void = false;
	var cmo_selected = false;
	var giftcard_selected = false;
	var giftcard_void = false;
	var promocode_selected = false;
	var promocode_void = false;
	var emails_dont_match = false;
	var emails_dont_match_s = false;
	
   for(i=0; i<theForm.elements.length; i++){
	
		if(theForm.elements[i].name != "voucher_code_1" && 
			theForm.elements[i].name != "voucher_code_2" && 
			theForm.elements[i].name != "voucher_code_3" && 
			theForm.elements[i].name != "promotion_code" && 
			theForm.elements[i].name != "cc_name" && 
			theForm.elements[i].name != "cc_num" && 
			theForm.elements[i].name != "cc_code"
			){
		 
		 /* validate email address */
		 if(theForm.elements[i].name == "email" && theForm.elements[i+1].name == "email_confirm" &&  (theForm.elements[i].value != theForm.elements[i+1].value )){
		  		emails_dont_match = true;
		 }
		 
		 /* validate shipping email address */
		 if(theForm.elements[i].name == "s_email" && theForm.elements[i+1].name == "s_email_confirm" &&  (theForm.elements[i].value != theForm.elements[i+1].value )){
		  		emails_dont_match_s = true;
		 }
		 
		if(theForm.elements[i].type == "checkbox" && theForm.elements[i].name == "addresses_are_same" && theForm.elements[i].checked == true){
				same_address = true;
				//alertText += "" + theForm.elements[i].name.substr(0,2) + " is not checked same address: "+ same_address+ "\n";
		}
			
		if(theForm.elements[i].type == "text" &&  theForm.elements[i].value == "" && same_address == true && theForm.elements[i].name.substr(0,2) != 's_'){
		  		alertText += "" + theForm.elements[i].id + "  " + theForm.elements[i].name + " is required \n"
		
		
		}else if(theForm.elements[i].type == "text" &&  theForm.elements[i].value == "" && same_address == false && theForm.elements[i].name.substr(0,2) == 's_'){
				alertText += "" + theForm.elements[i].id + " is required \n"
		
		
		}else if(theForm.elements[i].type == "text" &&  theForm.elements[i].value == "" && same_address == false){
				alertText += "" + theForm.elements[i].id + " is required \n"
		}
				
		/* GIFT BOX */
		  if((theForm.elements[i].type == "checkbox" && theForm.elements[i].name == "gift_box") && 
			(theForm.elements[i+1].type == "textarea" && theForm.elements[i+1].name == "gift_box_message") && 
			(theForm.elements[i].checked == true && theForm.elements[i+1].value == "" )  ){
		  		
				alertText += "" + theForm.elements[i+1].id + " is required \n";
		  }
		  
		  /* CREDIT CARD */
		/*  if(theForm.elements[i].type == "radio" && theForm.elements[i].value == "creditcard" && theForm.elements[i].checked == true ){
			  //alertText += "" + theForm.elements[i].value + " is selected \n";
			  cc_selected = true;
			} */
			
			/* CHEQUE OR MONEY ORDER */
		  if(theForm.elements[i].type == "radio" && theForm.elements[i].value == "cmo" && theForm.elements[i].checked == true ){
			  //alertText += "" + theForm.elements[i].value + " is selected \n";
			  cmo_selected = true;
			}
		  
		  /* GIFT CARD */
		  if(theForm.elements[i].type == "radio" && theForm.elements[i].value == "giftcard" && theForm.elements[i].checked == true ){
			  //alertText += "" + theForm.elements[i].value + " is selected \n";
			  giftcard_selected = true;
			}
		
		/* PROMO CODE */
		  if(theForm.elements[i].type == "radio" && theForm.elements[i].value == "promocode" && theForm.elements[i].checked == true ){
			  //alertText += "" + theForm.elements[i].value + " is selected \n";
			  promocode_selected = true;
		}
			
		/*if((theForm.elements[i].type == "radio" && theForm.elements[i].name == "pay_preference") && 
			(theForm.elements[i+1].type == "radio" && theForm.elements[i+1].name == "pay_preference") && 
			(theForm.elements[i+2].type == "radio" && theForm.elements[i+2].name == "pay_preference") && 
			(theForm.elements[i+3].type == "radio" && theForm.elements[i+3].name == "pay_preference") && 
			(theForm.elements[i].checked == false && theForm.elements[i+1].checked == false 
			&& theForm.elements[i+2].checked == false && theForm.elements[i+3].checked == false)  
			){*/
		  if((theForm.elements[i].type == "radio" && theForm.elements[i].name == "pay_preference") && 
			(theForm.elements[i+1].type == "radio" && theForm.elements[i+1].name == "pay_preference") && 
			(theForm.elements[i+2].type == "radio" && theForm.elements[i+2].name == "pay_preference") && 
			(theForm.elements[i].checked == false && theForm.elements[i+1].checked == false 
			&& theForm.elements[i+2].checked == false)  
			){
			payprefText += "" + theForm.elements[i].name + " is required \n";
		  }
		  
		}
		/* if any of the credit card fields are not filled */
		/*else if((theForm.elements[i].name == "cc_name" || theForm.elements[i].name == "cc_num" || theForm.elements[i].name == "cc_code") && theForm.elements[i].value == ""){
			
			cc_void = true;
		}*/
		/* if any of the gift card fields are not filled */
		else if((theForm.elements[i].name == "voucher_code_1" || theForm.elements[i].name == "voucher_code_2" || theForm.elements[i].name == "voucher_code_3") && theForm.elements[i].value == ""){
			
			giftcard_void = true;
		}
		/* if any of the gift card fields are not filled */
		else if(theForm.elements[i].name == "promocode" && theForm.elements[i].value == ""){
			
			promocode_void = true;
		}
   }
   
 
   /* report if either billing or shipping emails don't match */
    if(emails_dont_match == true || (emails_dont_match_s == true && same_address == false)){
		alertText += "Verify your email addresses match\n";   
   }
   /* if a credit card text field is blank and the credit card radio button is selected */
   /*if(cc_void == true && cc_selected == true){
		alertText += "\nAll credit card fields must be completed \n";   
   }*/
   /* if a gift card text field is blank and the credit card radio button is selected */
    if(giftcard_void == true && giftcard_selected == true){
		alertText += "\nAll gift card fields must be completed \n";   
   }
    /* if the promocode text field is blank and the credit card radio button is selected */
    if(promocode_void == true && promocode_selected == true){
		alertText += "\nThe promocode field must be completed \n";   
   }
   
    /* report if no pay preference was selected */
  /* if(cc_selected == false && cmo_selected == false && giftcard_selected == false && promocode_selected == false){*/
  if(cmo_selected == false && giftcard_selected == false && promocode_selected == false){
	   alertText += "A Pay Preference must be chosen\n"; 
   }
    
   if(alertText != ""){
   		alert("all fields marked with \'*\' are required \n\n" + alertText)
   	}else if(postageText != ""){
	   alert("please select a \'postage method\'")
	}else if(giftboxText != ""){
	   alert("please select a \'gift box\' requirement")
	}else if(payprefText != ""){
	   alert("please select a \'payment preference\' or select \'Add my Gift Card Code\' if you are paying with a Gift Card")
	}else{
		theForm.submit();
	}
}

//var popwin = null;
function menuPopUp(mylink)
{
//if (! window.focus)return true;
var href;
//if (popwin && !popwin.closed) popwin.close();
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;

window.popwin = open(mylink, 'popwin', 'width=500,height=500,scrollbars=yes, menubar=0');
popwin.focus();

}


function imagePopUp(image, mylink, width, height)
{
//if (! window.focus)return true;
var href;
//if (popwin && !popwin.closed) popwin.close();
if (typeof(mylink) == 'string')
   href=mylink;
else
   href=mylink.href;
   
   

window.popwin = open(mylink + '?sku=' + image, 'popwin', 'width=' + width + ', height=' + height + ',scrollbars=yes, menubar=0');
popwin.focus();

}


