$(document).ready(function() {
	var msgchanged = 0;
	function checkPayment() {
		var errn = 0;
		var errorPayment  = { "tr_cardholder" : 'You need to specify a valid Card Holdername (your name as it appears in your card)!',
					"tr_name" : "You need to specify a valid First Name!",
					"tr_lastname" : "You need to specify a valid Last Name!",
					"tr_email" : "You need to specify a valid e-mail!",
					"tr_email2" : "You need fill e-mail and Confirm e-mail with the same e-mail address!",
					"tr_card" : "You need to specify a valid card number!!"
				}
		var errorShipping = { "fnameship" : "You need to specify a valid First name for shipping!",
				    	"lnameship" : "You need to specify a valid Last Name for shipping!",
					"address1" : "You need to specify a valid Address for shipping!",
			 		"city" :"You need to specify a valid City for shipping!",
	//				state : "You need to specify a valid State or Province for shipping!",
					"zipcode" : "You need to specify a valid Zipcode for shipping!"	
		}

		var errorGift = { "gift_name" : "You need to specify your friend's name!",
				"gift_email" : "You need to specify your friend's e-mail!"
		}
	
		var errormsgs = "";
		var emailerrormsgs = "";
		var shipping = $("#shipment").is(":visible");

		for(var field in errorPayment) {
			var fieldval = $("#" + field  + "").val();
			if(field == 'tr_email'){
				emailanterior = fieldval; 
			}
			if(field == 'tr_email2'){			
	
				emailatual = fieldval; 
				if( emailanterior != emailatual){
					errormsgs += errorPayment[field] + "<br>";	
					fieldval = 'x';
					++errn;
				}
			}
			
			if(fieldval == '' || (field == 'tr_email' && !checkMailCart(fieldval))  || (field == 'tr_card' && isNaN(fieldval))) { 			
			errormsgs += errorPayment[field] + "<br>"; 
			++errn;

			}
		}
	
		if(shipping) { 
			for(var field in errorShipping) {
			
			var fieldval = $("#" + field  + "").val();
			
			if(fieldval == '') {

			errormsgs += errorShipping[field] + "<br>"; 
			++errn;

				}
			}
		}
		if($("#gift").length > 0) {
			for(var field in errorGift) {
			var fieldval = $("#"+field + "").val();
			if(fieldval == '' || (field == 'gift_email' && !checkMailCart(fieldval))) {
			errormsgs += errorGift[field] + "<br>";
			++errn;

				}
		        }
		}	
		return (errn > 0) ? "Please correct the following errors:<br><p style=\"font-weight:italic; color:red;\">" + errormsgs + "</p>" : -1;
		
	}	

	if($("#shipment").is(":visible")) {

		$("#register").click(function() {
		$("#register").append('<input type="hidden" name="total" value="'+$("#realtotal").html().replace("$","") + '" /><input type="hidden" name="shipcost" value="'+$("#shipcost").html().replace("$","") +'" /><input type="hidden" name="prodtotal" value="'+$("#prodtotal").html().replace("$","") +'" /><input type="hidden" name="shipmeth" value="'+$("#shipmeth").val()+'" /><input type="hidden" name="estimateddays" value="'+$("#estimateddays").html()+'" /><input type="hidden" name="address1" value="'+$("#address1").val()+'" /><input type="hidden" name="address2" value="'+$("#address2").val()+'" /><input type="hidden" name="pobox" value="'+$("#zipcode").html()+'" /><input type="hidden" name="country_ship" value="'+$("#country_ship").val()+'" /><input type="hidden" name="cdprice" value="'+$("#cdprice").html().replace("$","")+'" /><input type="hidden" name="lnameship" value="'+$("#lnameship").val()+'" /><input type="hidden" name="fnameship" value="'+$("#fnameship").val()+'"><input type="hidden" name="country_iso" value="'+$("#country").val()+'" />');
		$("#register").submit(); 
		})
	}


	$("#continue").click(function() {
                var gift = 0;
	
                if($("#gift_opt").is(":checked")) gift = 1;
                if ($("#check").val() == 'true') {

	//	var err = "Correct the following errors: \n";
		var errormsgs = checkPayment();
		
		if(errormsgs != -1) { 
		
		$("#buttons").height(300);
		$("#error_msgs").html(errormsgs); 
		$("#error_cont").slideDown('fast');
		return false;
		 }
		else {
				
			$("#error_cont").hide();
			$("#buttons").height(20);
		}
		}
		$("#payment_form").append('<input type="hidden" id="total" name="total" value="'+$("#realtotal").html().replace("$","") + '" /> '+
			'<input type="hidden" id="gift_opt" value="'+gift+'" name="gift_opt"');
		// Swiftcd checked
		if ($("#swiftcd").is(":checked")) {
			$("#payment_form").append('<input type="hidden" name="shipcost" value="'+$("#shipcost").html().replace("$","") +'" /><input type="hidden" name="prodtotal" value="'+$("#prodtotal").html().replace("$","") +'" /><input type="hidden" name="shipmeth" value="'+$("#shipmeth").val()+'" /><input type="hidden" name="estimateddays" value="'+$("#estimateddays").html()+'" /><input type="hidden" name="address1" value="'+$("#address1").val()+'" /><input type="hidden" name="address2" value="'+$("#address2").val()+'" /><input type="hidden" name="zipcode" value="'+$("#zipcode").val()+'" /><input type="hidden" name="country_ship" value="'+$("#country_ship").val()+'" /><input type="hidden" name="cdprice" value="'+$("#cdprice").html().replace("$","")+'" /><input type="hidden" name="lnameship" value="'+$("#lnameship").val()+'" /><input type="hidden" name="fnameship" value="'+$("#fnameship").val()+'" /><input type="hidden" name="country" value="'+$("#country").val()+'" /><input type="hidden" name="shipmeth" value="'+$("#shipmeth").val()+'" /><input type="hidden" name="swiftcd" value="'+$("#swiftcd").val()+'" />');
		}
		$("#continue").attr({'src': 'images/bt/pleasewait.gif'});
	/*	$("#continue").attr({'disabled': 'disabled'});
		$("#country").attr({'disabled': 'disabled'});
		$("#swiftcd").attr({'disabled': 'disabled'});
		$("#shipmeth").attr({'disabled': 'disabled'});
*/
		if($("#state").is("select")) {
			$("#payment_form").append('<input type="hidden" name="state_iso" value="'+$("#state").val()+'" />');
		} else {
			$("#payment_form").append('<input type="hidden" name="state_name" value="'+$("#state").val()+'" />');
		}
		if($("#shipment").is(":visible")) {
			$("#payment_form").append('<input type="hidden" name="country_ship" value="'+$("#country_ship").val()+'" />');
		}
		if($("#gift").length > 0 && msgchanged == 0) {
			if(!confirm("Are you sure you don't want to send a message to your friend?")) {
				return false;
			}
		} 
		$("#payment_form").submit();
			
		
		});

		$("#swiftcd").click(function() {
			var itemnum = parseFloat($("#itemnum").val());
			var total = parseFloat($("#prodtotal").html().replace("$",""));
			var cdprice = (parseFloat($("#cdprice").html().replace("$","")));
			var shipcost = (parseFloat($("#shipcost").html().replace("$","")));
			var prodtotal = parseFloat($("#prodtotal").html().replace("$",""));
			if($("#shipment").is(":hidden")) {
				var newcdprice = parseFloat($("#cdprice").html().replace("$","") * itemnum).toFixed(2);
				var newshipcost = parseFloat($("#shipcost").html().replace("$","") * itemnum).toFixed(2);
				$("#cdprice").html('$' + newcdprice);
				$("#shipcost").html('$' + newshipcost);
				$("#shipment").slideDown("slow");
				$("#trtotal").slideDown("slow");
				$("#swiftcdbox").slideDown("slow");
				$("#trcdprice").slideDown("slow");
				$("#trshipcost").slideDown("slow");
				$("#addr").slideDown("slow");
				total = parseFloat(total) + parseFloat(newcdprice) + parseFloat(newshipcost);
				total = total.toFixed(2);
				$("#realtotal").html("$"+total);
			} else {
				$("#addr").slideUp("slow");
				$("#shipment").slideUp("slow");			
				$("#swiftcdbox").slideUp("slow");			
				$("#trtotal").slideUp("slow");
				$("#trcdprice").slideUp("slow");
				$("#trshipcost").slideUp("slow");
				$("#realtotal").html("$"+prodtotal.toFixed(2));
				$("#cdprice").html('$' + (cdprice /itemnum).toFixed(2));
				$("#shipcost").html('$' + (shipcost / itemnum).toFixed(2));
			}
		})
		$("#gift_msg").focus(function() {
			msgchanged = 1;
		})
		$("#country_ship, #country").change(function() {
			var shipmeth = $("#shipmeth").val();
			var itemnum = parseFloat($("#itemnum").val());
			$("#country").val($(this).val());
			if($(this).val() != 'US') {
				
				$("#stateinput").html('<input type="text" name="state" id="state" onchange="javascript:this.value=this.value.toUpperCase();">');
				$.post('swiftAjax.php',
			{	country : 'non-us', ship_method : shipmeth, iso : $("#country").val() }, function(ret) {
				var splitted = ret.split("|");
				$("#shipmeth").html(splitted[0]);
				$("#estimateddays").html('<center>' +splitted[2] + '</center>');
				$("#shipcost").html('$' + parseFloat(splitted[1].replace("$","") * itemnum).toFixed(2));
				var tr = $("#country").val();
				var tr_html = $("#country").html();
		//		$("#country_ship").html(tr_html);
		//		$("#stateinput").css({'display': 'block'});
		//		$("#stateselect").css({'display': 'none'});

				
				var total = parseFloat($("#prodtotal").html().replace("$",""));
				var cdprice = parseFloat($("#cdprice").html().replace("$",""));
				var shipcost = parseFloat($("#shipcost").html().replace("$",""));
				shipcost.toFixed(2);
				total = (parseFloat(total) + parseFloat(cdprice) + parseFloat(shipcost));
				total = total.toFixed(2);
				$("#realtotal").html("$"+total);
				$("#shipmeth").val("1");
			}
		)} else {	
				$("#stateinput").html('<center><img src="images/cartloading.gif"></center>');
				$.post('swiftAjax.php',
			{	country : 'us', ship_method : shipmeth, iso : 'US', usa_states : 1 }, function(ret) {
				var splitted = ret.split("|");
				//$("#shipmeth").html(splitted[0]);
				
				$("#estimateddays").html('<center>' +splitted[2] + '</center>');
				$("#shipcost").html('$' + parseFloat(splitted[1].replace("$","") * itemnum).toFixed(2));
				var tr = $("#country").val();
				var tr_html = $("#country").html();
			//	$("#country_ship").html(tr_html);
			//	$("#country_ship").val(tr);
			//	$("#stateinput").css({'display': 'block'});
			//	$("#stateselect").css({'display': 'none'});
				$("#stateinput").html(ret);
				var total = parseFloat($("#prodtotal").html().replace("$",""));
				var cdprice = parseFloat($("#cdprice").html().replace("$",""));
				var shipcost = parseFloat($("#shipcost").html().replace("$",""));
				total = parseFloat(total) + parseFloat(cdprice) + parseFloat(shipcost);
				total = total.toFixed(2);
				$("#realtotal").html("$"+total);			
				$("#shipmeth").val("1");
			}
			)}
		})
		
		$("#shipmeth").change(function() {
			var shipmeth = $("#shipmeth").val();
			if($("#country").val() != 'US') {
				$.post('swiftAjax.php',
			{	country : 'non-us', ship_method : shipmeth, iso : $("#country").val() }, function(ret) {
				var splitted = ret.split("|");
				$("#shipmeth").html(splitted[0]);
				$("#estimateddays").html(splitted[2]);
				$("#shipcost").html('$' + parseFloat(splitted[1].replace("$","") * itemnum).toFixed(2));
				var tr = $("#country").val();
				var tr_html = $("#country").html();
				$("#country_ship").html(tr_html);
				$("#country_ship").val(tr);
				$("#stateinput").css({'display': 'block'});
				$("#stateselect").css({'display': 'none'});
				var total = parseFloat($("#prodtotal").html().replace("$",""));
				var cdprice = parseFloat($("#cdprice").html().replace("$",""));
				var shipcost = parseFloat($("#shipcost").html().replace("$",""));
				total = parseFloat(total) + parseFloat(cdprice) + parseFloat(shipcost);
				total = total.toFixed(2);
				$("#realtotal").html('$'+total);
				$("#shipmeth").val("1");
			}
		)} else {
				$.post('swiftAjax.php',
			{	country : 'us', ship_method : shipmeth, iso : $("#country").val(), us : '1' }, function(ret) {
				var splitted = ret.split("|");
				$("#shipmeth").html(splitted[0]);
				$("#estimateddays").html(splitted[2]);
				$("#shipcost").html('$' + parseFloat(splitted[1].replace("$","") * itemnum).toFixed(2));
				var tr = $("#country").val();
				var tr_html = $("#country").html();
				$("#country_ship").html(tr_html);
				$("#country_ship").val(tr);
				$("#stateinput").css({'display': 'block'});
				$("#stateselect").css({'display': 'none'});
				var total = parseFloat($("#prodtotal").html().replace("$",""));
				var cdprice = parseFloat($("#cdprice").html().replace("$",""));
				var shipcost = 	parseFloat($("#shipcost").html().replace("$",""));
				total = parseFloat(total) + parseFloat(cdprice) + parseFloat(shipcost);
				total = total.toFixed(2);
				$("#realtotal").html('$'+total);
				$("#shipmeth").val("1");
			}
		)}
	})

});
