$(document).ready(function(){
		// Remember that valid JSON code requires that input variables are double quoted "variable":"value"
		var CART = "../js/cart5.htm";
		var SIGNOUT = '<div class="signout"><input type="submit" name="signoutuser" id="signoutuser" value="Sign Out">&nbsp;&nbsp;<input type="button" name="forgetthiscomputer" id="forgetthiscomputer" value="Do Not Remember me on this computer!"  ></div> ';
		$.post(CART,{cmd:"load"},function(data){$("#shopcart").html(data);/*alert("Help Me");*/},"html");
		jQuery.validator.addMethod("phone", function(phone_number, element){
			  phone_number = phone_number.replace(/\s+/g, ""); // replace all white space with no space
				return this.optional(element) || phone_number.length > 9 && phone_number.match(/^[0-9-\(\)\+\,\.]{9,18}$/); //A valid phone number only uses numbers, - , ( ) . or +
		}, "Please enter a valid phone number.");
		/* === */ // alert("Good Morning");
$(":button.plus").live('click',function(){var itemid = $(this).parent().attr("id");var x = +1;	$.post(CART,{cmd:"plus",itemid: itemid, iqty:x.toFixed(0) },function(data){$("#shopcart").html(data);},"html");});
/* === */
$(":button.minus").live('click',function(){	var itemid = $(this).parent().attr("id");var x = -1;$.post(CART,{cmd:"minus",itemid: itemid, iqty:x.toFixed(0) } ,function(data){$("#shopcart").html(data);},"html");});
/* === */
$(".qty").live('change',function(){var itemid = $(this).parent().attr("id");var qty = $("#"+itemid+">.qty"); var x = +qty.val();if (isNaN(x)){alert("You just entered something besides a number in the quantity field"); $(this).css("border","4px solid red");}else{
	$(this).css("border","0px solid red").css("font-size","200%");$.post(CART,{cmd:"changeqty",itemid: itemid, iqty:x.toFixed(0) } ,function(data){$("#shopcart").html(data);},"html");}});
/* === */
$(".addtocart").live('submit',function(event){var thisprod=$(this).attr("id");var formdata=$(this).serialize();var itemid = new Date().getTime();$("#"+thisprod+ "> div.added").show(2000);$.post(CART,{cmd:"additem",itemid: itemid,formdata:formdata },function(data){$("#shopcart").html(data);},"html");$(".checkout").show();$(".immediatePP").show();event.preventDefault();});
/* === */
$(".location").live('click',function(){var $loc = $(this).val();	$.post(CART,{cmd:"picklocation",userlocation:$loc } ,function(data){$("#shopcart").html(data);},"html");
	if ($loc=="APO/FPO"){ alert("If you select this and are not shipping to an APO/FPO mail box you will delay your shipment while we contact you to arrange the correct shipping cost!");}
	alert('You have chosen '+$loc+' as your Shipping Region' );	});
/* === */
$(".droplabel").hover( function (){ $(this).addClass('highlighted');},function(){$(this).removeClass('highlighted'); });$(".droplabel").click(function(){var cstmsection=$(this).next().attr("id");	$("#"+cstmsection).toggle();var cstmshow = $("#"+cstmsection).css("display");if(cstmshow=="block"){ $.post(CART,{cmd:cstmsection },function(data){$("#"+cstmsection).html(data);},"html");}});
/* === */
$("#thisLogin").live('submit',function(event){/*thisLogin is in login.inc-called if we know the user*/ 
	var $pswd=$("#userpassword:input").val();
	$.post(CART, {cmd:"signinuser", password:$pswd} ,function(data){	$(".checkout").show();$("#pporcc").hide();$(".immediatePP").show();$("#logininfolabel").html(data);$("#registersection").html(SIGNOUT).show();$("#logininfo").hide().html("");
		$.post(CART,{cmd:"load"},function(data){$("#shopcart").html(data);},"html");
		$.post(CART,{cmd:"baddress"} ,function(data){$("#billsection").html(data).show();},"html");
		$.post(CART,{cmd:"ccard"} ,function(data){$("#ccsection").html(data).show();},"html");
		$.post(CART,{cmd:"saddress"} ,function(data){$("#shipsection").html(data).show();},"html");
		$.post(CART,{cmd:"getSharePts"} ,function(data){$("#shareptslabel").html(data);},"html");
		$.post(CART,{cmd:"sharesection"} ,function(data){$("#sharesection").html(data);},"html");
	},"html");	event.preventDefault();
});
/* === */
$("#thisLookup").live('submit',function(event){
	var $useraccount =$("#lookupuserid").val();
	var $useremail =$("#lookupuseremail").val();
	var $username =$("#lookupusername").val();
	var $userpassword =$("#lookupuserpassword").val();
	$.ajaxSetup ({ cache: false});
	$.post(CART, {"useraccount":$useraccount,"useremail":$useremail,"username":$username,"userpassword":$userpassword,"cmd":"lookupuser"} ,function(data){
			if (data.errorgt=="Found"){ 
				$(".checkout").show(); $("#pporcc").hide();$(".immediatePP").show();$("#logininfolabel").html(data.usergreeting);$("#registersection").html(SIGNOUT).show();$("#logininfo").hide().html("");
				$.post(CART,{cmd:"load"},function(data){$("#shopcart").html(data);},"html");
				$.post(CART,{cmd:"baddress"} ,function(data){$("#billsection").html(data).show();},"html");
				$.post(CART,{cmd:"ccard"} ,function(data){$("#ccsection").html(data).show();},"html");
				$.post(CART,{cmd:"saddress"} ,function(data){$("#shipsection").html(data).show();},"html");
				$.post(CART,{cmd:"getSharePts"} ,function(data){$("#shareptslabel").html(data);},"html");
				$.post(CART,{cmd:"sharesection"} ,function(data){$("#sharesection").html(data);},"html");
			}else{ 
			    $("#logininfolabel").html(data.errorgt);$.post(CART,{cmd:"logininfo"},function(data){$("#logininfo").html(data).show();	},"html");
			}  
	},"json"); 
	event.preventDefault();
});
/* === */
$("#signoutuser").live('click',function(event){
	$.post(CART,{cmd:"signoutuser"},function(data){$(".checkout").hide(); $("#pporcc").show();$(".immediatePP").show();$("#ccsection").html("").hide();$("#shipsection").html("").hide();	$("#billsection").html("").hide();
		$("#favoritesection").html("").hide();$("#sharesection").html("").hide();$("#logininfolabel").html("Sign In Here to complete your order.");
	$.post(CART,{cmd:"logininfo"},function(data){$("#logininfo").html(data).show();},"html");$("#registersection").hide().html("");
	$.post(CART,{cmd:"load" },function(data){$("#shopcart").html(data);},"html");},"html");	event.preventDefault();
});
/* === */
//$("#signoutusersalesorder").live('click',function(event){$.post(CART, {cmd:"signoutuser"} ,function(data){location.href = CUSTOMER;/*Send User to the Customer Page*/},"html");event.preventDefault();});
/* === */
$("#forgetthiscomputer").live('click',function(event){
	$.post(CART, {cmd:"forgetthiscomputer"} ,function(data){$(".checkout").hide(); $("#pporcc").show();$("#ccsection").html("").hide();$("#shipsection").html("").hide();$("#billsection").html("").hide();
		$("#favoritesection").html("").hide();	$("#sharesection").html("").hide();$("#logininfolabel").html("Sign In Here to complete your order.");
	$.post(CART,{cmd:"logininfo"},function(data){$("#logininfo").html(data).show();},"html");
	$.post(CART,{cmd:"registersection"},function(data){$("#registersection").html(data).show();},"html");
	$.post(CART,{cmd:"load" },function(data){$("#shopcart").html(data);},"html");
	},"html");	event.preventDefault();
});
/* === */
//$("#forgetthiscomputer").live('click',function(event){$.post(CART, {cmd:"forgetthiscomputer"} ,function(data){location.href = CUSTOMER;/*Send User to the Customer Page*/},"html");event.preventDefault();});
/* === */
$("#thisRegister").live('submit',function(event){
	var $formdata=$(this).serialize();
	$.post(CART,{"formdata":$formdata,"cmd":"registeruser"} ,function(data){
	if (data.main){
		$("#logininfolabel").html(data.main);
		$("#logininfo").hide();
		$(".checkout").show();  $("#pporcc").hide();$(".immediatePP").show();
		$("#registersection").html(SIGNOUT).show();
		$.post(CART,{cmd:"load"},function(data){$("#shopcart").html(data);},"html");
		$.post(CART,{cmd:"baddress"} ,function(data){$("#billsection").html(data).show();},"html");
		$.post(CART,{cmd:"ccard"} ,function(data){$("#ccsection").html(data).show();},"html");
		$.post(CART,{cmd:"saddress"} ,function(data){$("#shipsection").html(data).show();},"html");
		$.post(CART,{cmd:"sharesection"} ,function(data){$("#sharesection").html(data);},"html");
	}else{
		$("#registersection").hide();
     	$("#logininfolabel").html(data.errorgt);
			$.post(CART,{cmd:"logininfo"},function(data){
				$("#logininfo").html(data).show();
			},"html");
		}
	},"json");
		event.preventDefault();
});
$("#lookuppassword2email").live('click',function(){var $useremaildata =$("#lookupuser2email").val();$.post(CART,{useremaildata:$useremaildata,cmd:"lookuppassword2email"} ,function(data){$("#logininfo").html(data);},"html");return false;});
/* === */
$("#thisBillingAddress").live('submit',function(event){var formdata=$(this).serialize();/*alert(formdata);*/$.post(CART,{formdata:formdata,cmd:"updatebilling"},function(data){$("#billsection").html(data);$.post(CART,{cmd:"saddress"} ,function(data){$("#shipsection").html(data).show();},"html");},"html");event.preventDefault();});
/* === */
///////////////  Shipping Functions ///////////////////
$(":button.EditThisAddress").live('click',function(event){	var $ID=$(this).attr('id');	$.post(CART,{cmd:"EditThisAddress",thisId: $ID},function(data){$("#shipsection").html(data);},"html");	event.preventDefault();});
/* === */
$(":button.removeshipping").live('click',function(event){	var $ID=$(this).attr('id'); $ID=$ID.slice(14);	$.post(CART,{cmd:"removeshipping",thisId: $ID},function(data){$.post(CART,{cmd:"saddress"} ,function(data){$("#shipsection").html(data).show();},"html");},"html");event.preventDefault();});
/* === */
$("input[name=chosenship]").live('click',function(){var $ID=$(this).val();$.post(CART,{cmd:"chooseshipaddress",thisId: $ID},function(data){$("#shopcart").html(data);},"html");});
/* === */
$("#addshiplocation").live('click',function(){$("#shipForm").show();	$("#addshiplocation").hide();$("#sname").focus();	});
/* === */
$("#copyshiplocation").live('click',function(){$("#copyshiplocation").hide();$("#thisShippingAddress :text").each(function(){var $value=$(this).attr('alt');	$(this).val($value);});});
/* === */
$("#thisShippingAddress").live('submit',function(event){var formdata=$(this).serialize();$.post(CART, {"formdata":formdata,"cmd":"updateshipping"}  ,function(data){$.post(CART,{cmd:"saddress"} ,function(data){$("#shipsection").html(data).show();},"html");},"json");event.preventDefault();});
/* === */
$('input[name=shipregion]:radio').live('click',function(){var $userregion =$(this).val();$("#sregion").val($userregion);
	if ($userregion=="Idaho" || $userregion=="U.S." ){ $("#Postal").show();$("#scountry").val("").hide();$("#shipCountry").hide(); }else{$("#scountry").show();$("#shipCountry").show(); $("#PostalNote").attr("checked",false); $("#Postal").hide();}
	if ($userregion=="Canada"){ $("#scountry").val("Canada");}
	if ($userregion=="Australia"){ $("#scountry").val("Australia");}
	$.post(CART,{cmd:"picklocation",userlocation:$userregion } ,function(data){$("#shopcart").html(data);},"html");
});/* === */
$("#PostalNote").live('click',function(){var $notes=$("#snotes").val();$("#snotes").val("Ship via U.S. Postal Service  "+$notes);	});/* === */
/////// Credit Cards  ////////////////////////
$(":button.removecreditcard").live('click',function(){
	var $ID=$(this).attr('id'); $ID=$ID.slice(16);	$.post(CART,{cmd:"removecreditcard",thisId: $ID},function(data){$("#ccsection").html(data);},"html");});
	$("input[name=chosencc]:radio").live('click',function(){var $ID=$(this).val();$.post(CART,{cmd:"choosecreditcard",thisId: $ID});});
	$("#addcc").live('click',function(event){$("#ccForm").show();$("#addcc").hide();	$("#cctype").focus();});
	$("#thisCreditCard").live('submit',function(event){var formdata=$(this).serialize();$.post(CART, {formdata:formdata,cmd:"addcreditcard"}  ,function(data){$("#ccsection").html(data);$.post(CART,{cmd:"ccard"} ,function(data){$("#ccsection").html(data).show();},"html");},"html");	event.preventDefault();
});/* === */
//////////////// Favorites  /////////////
$(".removefromfavorites").live('click',function(event){var proditem =$(this).attr("name");$.post(CART,{cmd:"removeFav",proditem:proditem },function(data){$("#favoritesection").html(data);$("#favoritesection").hide(5000);},"html");	event.preventDefault();});
///////  Share Points  /////////////////////////
$("#thisShare").live('submit',function(event){var formdata=$(this).serialize();$.post(CART, {formdata:formdata,cmd:"updateFriend"}  ,function(data){$("#sharesection").html(data);},"html");event.preventDefault();});
///////  Coupons  /////////////////////////
$("#thisCoupon").live('submit',function(event){var formdata=$(this).serialize();$.post(CART, {formdata:formdata,cmd:"applycoupon"}  ,function(data){	$("#couponsection").html(data);$.post(CART,{cmd:"load" },function(data){$("#shopcart").html(data);},"html");	},"html");event.preventDefault();	});
///////  Place Order  //////////////////////////
$(".PlaceOrder").live('click',function(event){
	var SPECIALREQUEST=$("#SpecialRequest1").val()+" "+$("#SpecialRequest2").val();
	var SALESREP=$("#SalesRep").val();
	$.post(CART, {"cmd":"PlaceOrder","SpecialRequest":SPECIALREQUEST,"SalesRep":SALESREP} ,function(data){
		if (data.orderOK){
			location.href = SALESRECEIPT+"?sale=new";
		}else{
			alert(data.orderError);
			location.href = CUSTOMER;
		}
	},"json");
event.preventDefault();});
///////////////// PAYPAL ////////////////
$(".immediatePP").live('click',function(event){
	$(this).hide();$(this).css({"background-color":"yellow","color":"red","text-align":"center","font-size":"20px","border":"2px solid red"});$(this).html("Going to PayPal to finish your order.");
	$(this).show(2000);  // CUSTOMER & CONFIRMATION are set in SecureHeader.tpl for each production website
	var SPECIALREQUEST=$("#SpecialRequest1").val()+" "+$("#SpecialRequest2").val();
	$.post(CART,{"cmd":"immediatePPSetExpressCheckout","specialRequest":SPECIALREQUEST,CUSTOMER:CUSTOMER,CONFIRMATION:CONFIRMATION},function(data){
		if (data.orderOK){
			//alert(data.ppRespo);
			location.href = data.ppRespo;
		}else{
			alert("You don't have anything in your cart to checkout!");
			location.href = CUSTOMER;
		}
	},"json");
event.preventDefault();});
////////////////////  Cart Comment  /////////
$("#cartcommentsform").live('submit',function(event){var formdata=$("#cartcomments").val();	$.post(CART, {formdata:formdata,cmd:"comment"} ,function(data){$("#commentdiv").html("<h3>Thank You for your feedback.</h3>");},"html");event.preventDefault();	});
});/// End document ready
