function hardRefresh(){	
	window.location = '/shopping-cart.php';
}
function refresh_cart(){
	$.ajax({
		type: "GET",
		url: "shopping-cart.php",
		data: "action=refresh_cart",
		success: function(html){
			$("#divShoppingCart").html(html);
		}
	});
}

function checkCart(){
	ret_url = document.frmCheckout.elements['site_url'].value;
	$.ajax({
		type: "GET",
		url: "secure-checkout.php",
		data: "action=check_cart&ret_url="+ret_url,
		success: function(msg){
			if(msg == 1){
				checkForm('process');
			}else{
				window.location = msg+'/shopping-cart.php';
			}
		}
	});
}

function reviewOrder(){
	var str = $("#frmCheckout").serialize();
	$.ajax({
		type: "GET",
		url: "review-order.php",
		data: "action=review_cartInfo&"+str,
		success: function(msg){	
			if(msg == 'review'){		
			window.location = 'review-order.php';
			}
		}
	});
}

function updatecustomerInfo(){
	var str = $("#frmCheckout").serialize();
	$.ajax({
		type: "GET",
		url: "secure-checkout.php",
		data: "action=edit_checkoutInfo&"+str,
		success: function(msg){	
			if(msg == 'edit'){		
			window.location = 'secure-checkout.php';
			}
		}
	});
}

function updateItems(key,qty){
	$.ajax({
		type: "GET",
		url: "shopping-cart.php",
		data: "action=update_cart_items&key="+key+"&qty="+qty,
		success: function(html){
			$("#divShoppingCart").html(html);
			hardRefresh();
		}
	});
}

function toggleSavings(savingForm){
	$(".savingForm").slideUp("fast");
	$("#"+savingForm).slideDown("slow");
}

function closeSavings(savingForm){
	$(".savingForm").slideUp("fast");
}

function redeemCard(cardtype, card_number){
	$(".savingForm").slideUp("fast");
	$.ajax({
		type: "GET",
		url: "shopping-cart.php",
		data: "action=redeem_card&cardtype="+cardtype+"&card_number="+card_number,
		success: function(html){
			if(html == '1'){
				refresh_cart();
			}else{
				$("#divSavingMessage").html(html);
				$("#divSavingMessage").slideDown("slow");
			}
		}
	});
}

function countrySelect(country){
	$.ajax({
		type: "GET",
		url: "shopping-cart.php",
		data: "action=update_country&country="+country,
		success: function(html){
			$("#divShoppingCart").html(html);
			hardRefresh();
		}
	});
}

function display_Trackwarning(){
	$("#trackWarning").slideDown("slow");
}

function close_Trackwarning(){
	$("#trackWarning").slideUp("fast");
}

function shipoptionSelect(shipoption){
	$.ajax({
		type: "GET",
		url: "shopping-cart.php",
		data: "action=update_ship_option&shipping_option="+shipoption,
		success: function(html){
			$("#divShoppingCart").html(html);
			hardRefresh();
		}
	});
}

function toggleShipping(){
	if(document.frmCheckout.elements['different_shipping'].checked == true){
		$("#divShippingAddress").slideDown("slow");
		different_shipping = 1;
	}else{
		$("#divShippingAddress").slideUp("slow");
		different_shipping = 0;
	}
	
	country = document.frmCheckout.elements['shipping_country'].value;
	
	$.ajax({
		type: "GET",
		url: "secure-checkout.php",
		data: "action=update_different_shipping&different_shipping="+different_shipping+"&country="+country,
		success: function(html){
			$("#cartSummary").html(html);
		}
	});
}

function checkForm(passport){
	var strErr = '';
	if(document.frmCheckout.elements['fname'].value == ''){
		strErr += 'Please enter first name. \n';
	}
	if(document.frmCheckout.elements['lname'].value == ''){
		strErr += 'Please enter last name. \n';
	}
	if(document.frmCheckout.elements['email'].value == ''){
		strErr += 'Please enter email. \n';
	}
	if(document.frmCheckout.elements['phone'].value == ''){
		strErr += 'Please enter phone. \n';
	}
	if(document.frmCheckout.elements['address'].value == ''){
		strErr += 'Please enter address. \n';
	}
	if(document.frmCheckout.elements['city'].value == ''){
		strErr += 'Please enter city. \n';
	}
	if(document.frmCheckout.elements['state'].value == ''){
		strErr += 'Please enter state. \n';
	}
	if(document.frmCheckout.elements['zip'].value == ''){
		strErr += 'Please enter zip/postal code. \n';
	}
	if(document.frmCheckout.elements['country'].value == ''){
		strErr += 'Please select country. \n';
	}
	if(document.frmCheckout.elements['different_shipping'].checked == true){
		if(document.frmCheckout.elements['shipping_fname'].value == ''){
			strErr += 'Please enter shipping first name. \n';
		}
		if(document.frmCheckout.elements['shipping_lname'].value == ''){
			strErr += 'Please enter shipping last name. \n';
		}
		if(document.frmCheckout.elements['shipping_address'].value == ''){
			strErr += 'Please enter shipping address. \n';
		}
		if(document.frmCheckout.elements['shipping_city'].value == ''){
			strErr += 'Please enter shipping city. \n';
		}
		if(document.frmCheckout.elements['shipping_state'].value == ''){
			strErr += 'Please enter shipping state. \n';
		}
		if(document.frmCheckout.elements['shipping_zip'].value == ''){
			strErr += 'Please enter shipping zip/postal code. \n';
		}
		if(document.frmCheckout.elements['shipping_country'].value == ''){
			strErr += 'Please select shipping country. \n';
		}	
	}
	
	if(document.frmCheckout.elements['cc_type'].value == ''){
		strErr += 'Please select credit card type. \n';
	}
	if(document.frmCheckout.elements['cc_owner'].value == ''){
		strErr += 'Please enter credit card owner. \n';
	}
	if(document.frmCheckout.elements['cc_number'].value == ''){
		strErr += 'Please enter credit card number. \n';
	}
	if(document.frmCheckout.elements['cc_month'].value == ''){
		strErr += 'Please enter expiration month. \n';
	}
	if(document.frmCheckout.elements['cc_year'].value == ''){
		strErr += 'Please enter expiration year. \n';
	}
	if(document.frmCheckout.elements['cc_cvv'].value == ''){
		strErr += 'Please enter CVV. \n';
	}
	
	if(strErr != ''){
		alert(strErr);
	}else{
		if(passport == 'process'){
		processPayment();
		}else{
		reviewOrder();
		}
	}
}

function processPayment(){
	displayProgress();
	var str = $("#frmCheckout").serialize();
	url = document.frmCheckout.elements['site_url'].value;
	
	$.ajax({
		type: "GET",
		url: "secure-checkout.php",
		data: "action=process_payment&"+str,
		success: function(msg){
			if(msg == 'error'){
				window.location = 'secure-checkout.php';
			}else{
				window.location = url+'/order-receipt.php?action=confirm_order&receipt='+msg;
			}
		}
	});
}

function changeShipping(country,cfield){
	$.ajax({
		type: "GET",
		url: "secure-checkout.php",
		data: "action=update_checkout_country&country="+country+"&cfield="+cfield,
		success: function(html){
			$("#cartSummary").html(html);
			if(country != 'United States'){
				display_Trackwarning();
			}else{
				close_Trackwarning();
			}
		}
	});		
}

function displayProgress(){
	var A = document.getElementById("progress_bar");
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		var C = window.innerWidth;
		var D = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		var C = document.documentElement.clientWidth;
		var D = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		var C = document.body.clientWidth;
		var D = document.body.clientHeight;
	}

	//must display block first to have clientWidth and clientHeight
	//A.style.display = "block";

	//var elementWidth = A.clientWidth;
	//var elementHeight = A.clientHeight;
	
	var elementWidth = 300;
	var elementHeight = 100;
	
	A.style.top = String(Math.round((D-elementHeight)/2))+"px";
	A.style.left = String(Math.round((C-elementWidth)/2))+"px";
	
	$("#progress_bar").fadeIn("slow");
}

function hideProgress(){
	$("#progress_bar").fadeOut("slow");
}

function showChart(){
	$("#shippingchart").slideToggle("slow");
}
