$(document).ready(function(){ 
		var CART = "../js/cart.htm";  //<!--  TEST Script -->
		$.post(CART, { cmd:"load"},function(data){$("#shopcart").html(data);},"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); /*css("display","block");  */
			$.post(CART, { cmd:"additem",itemid: itemid,formdata:formdata },function(data){$("#shopcart").html(data);},"html");
			$(".checkout").show();		
			event.preventDefault();
		});/* === */			
		$(".location").live('click',function() { 
			var $loc = $(this).val();
			$.post(CART, {  cmd:"picklocation",userlocation:$loc } ,function(data){$("#shopcart").html(data);},"html");		
			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) { 
			var $pswd=$("#userpassword:input").val(); 
			$.post(CART, {cmd:"signinuser", password:$pswd} ,function(data){
					$(".checkout").show();
					$("#logininfolabel").html(data);
					$("#logininfo").hide();
					$("#registersection").hide();$("#registersection").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();
		});/* === */
		$("#signoutuser").live('click',function(event) { 
			$.post(CART, {cmd:"signoutuser"} ,function(data){
				$("#logininfo").html(data);
				$(".checkout").hide();
				$("#ccsection").html("").hide();
				$("#shipsection").html("").hide();
				$("#billsection").html("").hide();
				$("#confirmation").html("").hide();
				$("#favoritesection").html("").hide();
				$("#sharesection").html("").hide();
				$.post(CART, { cmd:"load" },function(data){$("#shopcart").html(data);},"html");
				$("#logininfolabel").html("Sign In Here to complete your order.");
				$("#logininfo").hide(5000);
			},"html");
			event.preventDefault();
		});/* === */	
		$("#forgetthiscomputer").live('click',function(event) { 
			$.post(CART, {cmd:"forgetthiscomputer"} ,function(data){
				$(".checkout").hide();
				$("#ccsection").html("").hide();
				$("#shipsection").html("").hide();
				$("#billsection").html("").hide();
				$("#confirmation").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();
		});/* === */	 		 
		$("#thisRegister").live('submit',function(event) { 
				var $first = $("#firstusername").val(); $first = jQuery.trim($first);
				var $last = $("#lastusername").val(); $last = jQuery.trim($last);
				var $zip = $("#zipusername").val(); $zip = jQuery.trim($zip);
				var $username = $first+" "+$last+" "+$zip;  
			 	var $bname = $first+" "+$last; 
			 	var $bzip = $zip; 
				var $useremail =$("#regisuseremail").val();
				var $userpassword =$("#regisuserpassword").val();
				$.post(CART, {  username:$username,bname:$bname,bzip:$bzip,useremail:$useremail,userpassword:$userpassword,cmd:"registeruser"} ,function(data){
					$("#logininfo").html(data);
					$(".checkout").show();
					$("#registersection").hide();$("#registersection").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:"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});  //alert("Help Me");
				$.post(CART, {"useraccount":$useraccount,"useremail":$useremail,"username":$username,"userpassword":$userpassword,"cmd":"lookupuser"} ,function(data){
					//alert(data.errorgt);
				if (data.errorgt=="Found"){
					//alert("lookup user 3");
						$(".checkout").show();
						$("#logininfolabel").html("Sign Out");
						$("#logininfo").hide();
						$("#registersection").hide();$("#registersection").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");
						//alert("lookup user 4");
					}else{ //alert("lookup user NOT found");
					      $("#logininfolabel").html(data.errorgt);
								$.post(CART, { cmd:"logininfo"},function(data){
									$("#logininfo").html(data).show();
								},"html");
					}
				},"json");
				event.preventDefault();
		});/* === */	 
/*		$("#emailpassword").live('click',function() { 
				$.post(CART, { cmd:"emailpassword"} ,function(data){$("#logininfo").html(data);},"html");
				event.preventDefault();
				return false;
		}); *//* === */	 	
		$("#lookuppassword2email").live('click',function() { 
				var $useremaildata =$("#lookupuser2email").val(); //alert($useremail);
				$.post(CART, {  useremaildata:$useremaildata,cmd:"lookuppassword2email"} ,function(data){$("#logininfo").html(data);},"html");
				//event.preventDefault();
				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() { 
		//	 alert("Enter Copy Billing Address");
			$("#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(); // alert(formdata);
			$.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"); 
//			$.post(CART, { cmd:"load" },function(data){$("#shopcart").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  //////////////////////////
	 	$("form[name=PlaceOrder]").live('submit',function(event) {
			var formdata=$(this).serialize(); 
			$.post(CART, {"formdata":formdata,"cmd":"PlaceOrder"} ,function(data){
						if (data.orderOK){
					$("#shopcart").html(data.orderPlaced);
					$.post(CART, {
						tax:data.tax,taxlabel:data.taxlabel,shipping:data.shipping,shippinglabel:data.shippinglabel,total:data.total,SpecialRequest:data.SpecialRequest,
						item:data.item,userdata:data.userdata,ccdata:data.ccdata,shipdata:data.shipdata,cmd:"ConfirmOrder"} ,function(data){
							$("#confirmation").html(data).show();
					},"html");
					$("#registersection").html(' <br><center><input type="button" value="Return to Customer Information" onClick="window.location.reload()"></center>').show();
					$(".checkout").hide(); $("#billsection").hide();$("#shipsection").hide();$("#ccsection").hide();$("#favoritesection").hide();$("#favoriteslabel").hide();$("#shareptslabel").hide();$("#sharesection").hide();
				}else{ 
					alert(data.orderError);
				}

			},"json"); 
			event.preventDefault();
		});		
///////////////// PAYPAL ////////////////			
		$(".PP").click(function(){  
			$(this).hide();
			$(this).css({"background-color":"yellow","color":"red","text-align":"center","font-size":"20px","border":"2px solid red"});
			$(this).html("Please wait for PayPal");
			$(this).show(2000);
			$.post(CART,{cmd:"PPSetExpressCheckout",CUSTOMER:CUSTOMER,CONFIRMATION:CONFIRMATION},function(data){
				location.href = data; 	// Send User to the Paypal Login Page
			}); 
		});
		$("#finishpurchaseformPayPal").live('submit',function(event) {
			//alert("finishpurchase for PayPal was pushed");
			$("#confirmpage").hide();$("#confirmsuccess").hide();
			$("#confirmsuccess").css({"background-color":"yellow","color":"red","text-align":"center","font-size":"20px","border":"2px solid red"});			
			$("#confirmsuccess").html("Waiting for PayPal");
			$("#confirmsuccess").show(2000); 
			var formdata=$(this).serialize();
			$.post(CART, {formdata:formdata,cmd:"finishpurchasePayPal"} ,function(data){
				$("#confirmsuccess").html(data); },"html");
				//$("#confirmsuccess").show();			
			event.preventDefault();
		});				
	
		$("#cartcommentsform").live('submit',function(event) {
			//alert("cartcommentsform was pushed");
			var formdata=$("#cartcomments").val();
			$.post(CART, {formdata:formdata,cmd:"comment"} ,function(data){
				$("#commentdiv").html("<h3>Thank You for your feedback.</h3>");
			},"html"); 
			event.preventDefault();
		});
}); 
