// ------------------------------------------------------------
// remember to change url address when moving to another server
// ------------------------------------------------------------

var xmlhttp;
var site="http://www.citrolife.com.au/";

function GetXmlHttpObject(){
	if (window.XMLHttpRequest){
		return new XMLHttpRequest();
	}
	if (window.ActiveXObject){
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	return null;
}

// product list
function changePrice(volume, product){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	
	var url=site+"assets/js/ajax/checkPrice.php";
	url=url+"?volume="+volume+"&product="+product;
	
	xmlhttp.onreadystatechange=function () {
		if (xmlhttp.readyState == 4) {
			setPrice(volume,product);
		}
	};
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function setPrice(volume,product){
	document.getElementById("price"+product).innerHTML = '$'+xmlhttp.responseText;
}

//product details
function changePriceSkuWeight(volume, product){
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	
	var url=site+"assets/js/ajax/checkPriceSkuWeight.php";
	url=url+"?volume="+volume+"&product="+product;
	
	xmlhttp.onreadystatechange=function () {
		if (xmlhttp.readyState == 4) {
			setPriceSkuWeight();
		}
	};
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function setPriceSkuWeight(){
	var response = xmlhttp.responseText;
	var output = response.split("::");
	document.getElementById("sku").innerHTML = output[0];
	document.getElementById("weight").innerHTML = output[1] + " kg";
	document.getElementById("price").innerHTML = "$" + output[2];
}

function changeContent(content){
	var descriptionCSS = $('#productDetails').css('display');
	var directionsCSS = $('#productDirections').css('display');
	var applicationsCSS = $('#productApplications').css('display');
	
	
	if(content == "description"){
		if(directionsCSS == "block"){
			$("#directionsButton").removeClass('selected');
			$("#directionsButton").html('<a href="#" onclick="changeContent(\'directions\')"></a>');
			$("#productDirections").css("display","none");
		}else if(applicationsCSS == "block"){
			$("#applicationsButton").removeClass('selected');
			$("#applicationsButton").html('<a href="#" onclick="changeContent(\'applications\')"></a>');
			$("#productApplications").css("display","none");
		}
		$("#descriptionButton").addClass('selected');
		$("#descriptionButton").html('');
		$("#productDetails").css("display","block");
		
	}else if(content == "directions"){
		if(descriptionCSS == "block"){
			$("#descriptionButton").removeClass('selected');
			$("#descriptionButton").html('<a href="#" onclick="changeContent(\'description\')"></a>');
			$("#productDetails").css("display","none");			
		}else if(applicationsCSS == "block"){
			$("#applicationsButton").removeClass('selected');
			$("#applicationsButton").html('<a href="#" onclick="changeContent(\'applications\')"></a>');
			$("#productApplications").css("display","none");
		}
		$("#directionsButton").addClass('selected');
		$("#directionsButton").html('');
		$("#productDirections").css("display","block");

	}else if(content == "applications"){
		if(descriptionCSS == "block"){
			$("#descriptionButton").removeClass('selected');
			$("#descriptionButton").html('<a href="#" onclick="changeContent(\'description\')"></a>');
			$("#productDetails").css("display","none");			
		}else if(directionsCSS == "block"){
			$("#directionsButton").removeClass('selected');
			$("#directionsButton").html('<a href="#" onclick="changeContent(\'directions\')"></a>');
			$("#productDirections").css("display","none");
		}
		$("#applicationsButton").addClass('selected');
		$("#applicationsButton").html('');
		$("#productApplications").css("display","block");
	}
}

function clearField(field){
	field.value = "";
	field.style.color = "#666666";
	field.style.textAlign = "left";
}
function clearPasswordField(field){
	field.value = "";
	field.type = "password";
	field.style.color = "#666666";
	field.style.textAlign = "left";
}

// SHOPPING CART
function updateProduct(id, quantity, i){
	$.ajaxSetup({
   		cache: false
	});

	$.blockUI({ 
		message: $('#displayBoxContainer'), 
		css: { 
			padding:0, 
			margin:0, 
			border:'none', 
			top:  ($(window).height() - 64) /2 + 'px', 
			left: ($(window).width() - 64) /2 + 'px',
			width: 0
		} ,
		fadeIn: 400, 
		fadeOut: 400, 
		overlayCSS:  { 
			backgroundColor:'#000', 
			opacity:0.7 
		}
    });
	
	if(quantity != 0){
		
		$.get(site+"cart/update_cart/"+id+"/"+quantity, function(){
			$('#subtotal'+i).load(site+"cart/subtotal/"+id, function(){
				$('#totalPrice').load(site+"cart/total", function(){
					$.unblockUI();
				})
			})
		})
	}else{
		$.get(site+"cart/update_cart/"+id+"/"+quantity, function(){
			$('#totalPrice').load(site+"cart/total", function(){
				$('#TOTAL').load(site+"cart/total_items", function(){
					$('#product'+i).remove();
					$.unblockUI();
				})
			})
		})
	}
}

function toggleShippingAddress(){
	$("#shipping_address").toggle();
}	

var currentApp = "household";

function showApplication(nextApp){
	if(nextApp != currentApp){
		document.getElementById(currentApp).style.display = "none";
		document.getElementById(nextApp).style.display = "block";
		currentApp = nextApp;
	}
	return false;
	
}

function get_delivery_radio_value(){
	for (var i=0; i < document.paypalform.delivery.length; i++){
   		if (document.paypalform.delivery[i].checked){
      		var rad_val = document.paypalform.delivery[i].value;
			return rad_val;
      	}
   	}
}
function get_payment_radio_value(){
	for (var i=0; i < document.paypalform.payment.length; i++){
   		if (document.paypalform.payment[i].checked){
      		var rad_val = document.paypalform.payment[i].value;
			return rad_val;
      	}
   	}
}

function setPayPal(subtotal, shipping1, shipping2, shipping3){
		
	if(payment != "paypal"){
		
		$.blockUI({ 
			message: $('#displayBoxContainer'), 
			css: { 
				padding:0, 
				margin:0, 
				border:'none', 
				top:  ($(window).height() - 64) /2 + 'px', 
				left: ($(window).width() - 64) /2 + 'px',
				width: 0
			} ,
			fadeIn: 400, 
			fadeOut: 400, 
			overlayCSS:  { 
				backgroundColor:'#000', 
				opacity:0.7 
			}
		});
		
		
		selectedDelivery = get_delivery_radio_value();
		
		if(selectedDelivery == "STANDARD"){
			shipping = shipping1;
			delivery = "regular";
		}else if(selectedDelivery == "EXPRESS"){
			shipping = shipping2;
			delivery = "express";
		}else if(selectedDelivery == "EXP_PLT"){
			shipping = shipping3;
			delivery = "platinum";
		}
		
		var t = setTimeout(function(){
			surcharge = (subtotal + shipping) * 0.025;
			total = subtotal + shipping + surcharge;
			gst = total / 11;			

			var formatted_shipping = new NumberFormat(shipping).toFormatted();
			var formatted_surcharge = new NumberFormat(surcharge).toFormatted();
			var formatted_total = new NumberFormat(total).toFormatted();
			var formatted_gst = new NumberFormat(gst).toFormatted();
			
			document.getElementById("OVERVIEW_SHIPPING").innerHTML = '$'+formatted_shipping;
			document.getElementById("OVERVIEW_SURCHARGE_AREA").style.display="table-row"
			document.getElementById("OVERVIEW_SURCHARGE").innerHTML = '$'+ formatted_surcharge;
			document.getElementById("OVERVIEW_TOTAL").innerHTML = '$'+formatted_total;
			document.getElementById("OVERVIEW_GST").innerHTML = '$'+formatted_gst;
			
			payment = "paypal";
			
			$.unblockUI();
		}, 1500);
	}else{
		payment = "paypal";
	}
}

function setDebit(subtotal, shipping1, shipping2, shipping3){

	if(payment != "debit"){
		
		if(delivery != "none"){
			
			$.blockUI({ 
				message: $('#displayBoxContainer'), 
				css: { 
					padding:0, 
					margin:0, 
					border:'none', 
					top:  ($(window).height() - 64) /2 + 'px', 
					left: ($(window).width() - 64) /2 + 'px',
					width: 0
				} ,
				fadeIn: 400, 
				fadeOut: 400, 
				overlayCSS:  { 
					backgroundColor:'#000', 
					opacity:0.7 
				}
			});
			
			selectedDelivery = get_delivery_radio_value();
			
			if(selectedDelivery == "STANDARD"){
				shipping = shipping1;
				delivery = "regular";
			}else if(selectedDelivery == "EXPRESS"){
				shipping = shipping2;
				delivery = "express";
			}else if(selectedDelivery == "EXP_PLT"){
				shipping = shipping3;
				delivery = "platinum";
			}
			
			var t = setTimeout(function(){
				total = subtotal + shipping;
				gst = total / 11;
				
				var formatted_shipping = new NumberFormat(shipping).toFormatted();
				var formatted_total = new NumberFormat(total).toFormatted();
				var formatted_gst = new NumberFormat(gst).toFormatted();
			
				document.getElementById("OVERVIEW_SURCHARGE_AREA").style.display="none"
				document.getElementById("OVERVIEW_SHIPPING").innerHTML = '$'+formatted_shipping;
				document.getElementById("OVERVIEW_TOTAL").innerHTML = '$'+formatted_total;
				document.getElementById("OVERVIEW_GST").innerHTML = '$'+formatted_gst;
				
				payment = "debit";
				
				$.unblockUI();
			}, 1500);

	
		}else{
			delivery = "none";
		}
		
	}else{
		payment = "debit";
	}
}
function setRegular(subtotal, shipping){
	if(delivery != "regular"){
		$.blockUI({ 
			message: $('#displayBoxContainer'), 
			css: { 
				padding:0, 
				margin:0, 
				border:'none', 
				top:  ($(window).height() - 64) /2 + 'px', 
				left: ($(window).width() - 64) /2 + 'px',
				width: 0
			} ,
			fadeIn: 400, 
			fadeOut: 400, 
			overlayCSS:  { 
				backgroundColor:'#000', 
				opacity:0.7 
			}
		});
		
		
		selectedPayment = get_payment_radio_value();
		
		if(selectedPayment == "paypal"){
			payment = "paypal";
		}else if(selectedPayment == "debit"){
			payment = "debit";
		}
		
		if(payment == "paypal"){
			
			var t = setTimeout(function(){
				surcharge = (subtotal + shipping) * 0.025;
				total = subtotal + shipping + surcharge;
				gst = total / 11;
				
				var formatted_shipping = new NumberFormat(shipping).toFormatted();
				var formatted_surcharge = new NumberFormat(surcharge).toFormatted();
				var formatted_total = new NumberFormat(total).toFormatted();
				var formatted_gst = new NumberFormat(gst).toFormatted();
				
				document.getElementById("OVERVIEW_SHIPPING").innerHTML = '$'+formatted_shipping;
				document.getElementById("OVERVIEW_SURCHARGE_AREA").style.display="table-row"
				document.getElementById("OVERVIEW_SURCHARGE").innerHTML = '$'+ formatted_surcharge;
				document.getElementById("OVERVIEW_TOTAL").innerHTML = '$'+formatted_total;
				document.getElementById("OVERVIEW_GST").innerHTML = '$'+formatted_gst;
			
			
				$.unblockUI();
			}, 1500);
		}else if(payment == "debit"){
			var t = setTimeout(function(){
				total = subtotal + shipping;
				gst = total / 11;
				
				var formatted_shipping = new NumberFormat(shipping).toFormatted();
				var formatted_total = new NumberFormat(total).toFormatted();
				var formatted_gst = new NumberFormat(gst).toFormatted();
			
				document.getElementById("OVERVIEW_SURCHARGE_AREA").style.display="none"
				document.getElementById("OVERVIEW_SHIPPING").innerHTML = '$'+formatted_shipping;
				document.getElementById("OVERVIEW_TOTAL").innerHTML = '$'+formatted_total;
				document.getElementById("OVERVIEW_GST").innerHTML = '$'+formatted_gst;

				$.unblockUI();
			}, 1500);
		}
		
		delivery = "regular";
		
	}else{
		delivery = "regular";
	}
}
function setExpress(subtotal, shipping){
	if(delivery != "express"){
		$.blockUI({ 
			message: $('#displayBoxContainer'), 
			css: { 
				padding:0, 
				margin:0, 
				border:'none', 
				top:  ($(window).height() - 64) /2 + 'px', 
				left: ($(window).width() - 64) /2 + 'px',
				width: 0
			} ,
			fadeIn: 400, 
			fadeOut: 400, 
			overlayCSS:  { 
				backgroundColor:'#000', 
				opacity:0.7 
			}
		});
		
		
		selectedPayment = get_payment_radio_value();
		
		if(selectedPayment == "paypal"){
			payment = "paypal";
		}else if(selectedPayment == "debit"){
			payment = "debit";
		}
		
		if(payment == "paypal"){
			
			var t = setTimeout(function(){
				surcharge = (subtotal + shipping) * 0.025;
				total = subtotal + shipping + surcharge;
				gst = total / 11;
				var formatted_shipping = new NumberFormat(shipping).toFormatted();
				var formatted_surcharge = new NumberFormat(surcharge).toFormatted();
				var formatted_total = new NumberFormat(total).toFormatted();
				var formatted_gst = new NumberFormat(gst).toFormatted();
				
				document.getElementById("OVERVIEW_SHIPPING").innerHTML = '$'+formatted_shipping;
				document.getElementById("OVERVIEW_SURCHARGE_AREA").style.display="table-row"
				document.getElementById("OVERVIEW_SURCHARGE").innerHTML = '$'+ formatted_surcharge;
				document.getElementById("OVERVIEW_TOTAL").innerHTML = '$'+formatted_total;
				document.getElementById("OVERVIEW_GST").innerHTML = '$'+formatted_gst;
				$.unblockUI();
			}, 1500);
		}else if(payment == "debit"){
			var t = setTimeout(function(){
				total = subtotal + shipping;
				gst = total / 11;
				var formatted_shipping = new NumberFormat(shipping).toFormatted();
				var formatted_total = new NumberFormat(total).toFormatted();
				var formatted_gst = new NumberFormat(gst).toFormatted();
			
				document.getElementById("OVERVIEW_SURCHARGE_AREA").style.display="none"
				document.getElementById("OVERVIEW_SHIPPING").innerHTML = '$'+formatted_shipping;
				document.getElementById("OVERVIEW_TOTAL").innerHTML = '$'+formatted_total;
				document.getElementById("OVERVIEW_GST").innerHTML = '$'+formatted_gst;
				$.unblockUI();
			}, 1500);
		}
		
		delivery = "express";
	
	}else{
		delivery = "express";
	}
}
function setPlatinum(subtotal, shipping){
	if(delivery != "platinum"){
		$.blockUI({ 
			message: $('#displayBoxContainer'), 
			css: { 
				padding:0, 
				margin:0, 
				border:'none', 
				top:  ($(window).height() - 64) /2 + 'px', 
				left: ($(window).width() - 64) /2 + 'px',
				width: 0
			} ,
			fadeIn: 400, 
			fadeOut: 400, 
			overlayCSS:  { 
				backgroundColor:'#000', 
				opacity:0.7 
			}
		});
		
		
		selectedPayment = get_payment_radio_value();
		
		if(selectedPayment == "paypal"){
			payment = "paypal";
		}else if(selectedPayment == "debit"){
			payment = "debit";
		}
		
		if(payment == "paypal"){
			
			var t = setTimeout(function(){
				surcharge = (subtotal + shipping) * 0.025;
				total = subtotal + shipping + surcharge;
				gst = total / 11;
				var formatted_shipping = new NumberFormat(shipping).toFormatted();
				var formatted_surcharge = new NumberFormat(surcharge).toFormatted();
				var formatted_total = new NumberFormat(total).toFormatted();
				var formatted_gst = new NumberFormat(gst).toFormatted();
				
				document.getElementById("OVERVIEW_SHIPPING").innerHTML = '$'+formatted_shipping;
				document.getElementById("OVERVIEW_SURCHARGE_AREA").style.display="table-row"
				document.getElementById("OVERVIEW_SURCHARGE").innerHTML = '$'+ formatted_surcharge;
				document.getElementById("OVERVIEW_TOTAL").innerHTML = '$'+formatted_total;
				document.getElementById("OVERVIEW_GST").innerHTML = '$'+formatted_gst;
				$.unblockUI();
			}, 1500);
		}else if(payment == "debit"){
			var t = setTimeout(function(){
				total = subtotal + shipping;
				gst = total / 11;
				var formatted_shipping = new NumberFormat(shipping).toFormatted();
				var formatted_total = new NumberFormat(total).toFormatted();
				var formatted_gst = new NumberFormat(gst).toFormatted();
			
				document.getElementById("OVERVIEW_SURCHARGE_AREA").style.display="none"
				document.getElementById("OVERVIEW_SHIPPING").innerHTML = '$'+formatted_shipping;
				document.getElementById("OVERVIEW_TOTAL").innerHTML = '$'+formatted_total;
				document.getElementById("OVERVIEW_GST").innerHTML = '$'+formatted_gst;
				$.unblockUI();
			}, 1500);
		}
		
		delivery = "platinum";
	
	}else{
		delivery = "platinum";
	}
}


function addToMailingList(){
	var name = $("#newsletter_name").attr("value");
	var email = $("#newsletter_email").attr("value");
	
	xmlhttp=GetXmlHttpObject();
	if (xmlhttp==null){
	  alert ("Browser does not support HTTP Request");
	  return;
	}
	
	var url=site+"assets/js/ajax/add_to_mailing_list.php";
	url=url+"?name="+name+"&email="+email;
	
	xmlhttp.onreadystatechange=function () {
		if (xmlhttp.readyState == 4) {
			addedToList();
		}
	};
	xmlhttp.open("GET",url,true);
	xmlhttp.send(null);
}

function addedToList(){
	if(xmlhttp.responseText == "all_empty"){
		newsletter = "Please enter your name and email address.";
	}else if(xmlhttp.responseText == "empty_name"){
		newsletter = 'Please enter your name.';
	}else if(xmlhttp.responseText == "empty_email"){
		newsletter = 'Please enter your email.';
	}else if(xmlhttp.responseText == "wrong_email"){
		newsletter = 'Please enter valid email address.';
	}else if(xmlhttp.responseText == "email_exists"){
		newsletter = "Email already exists in our database.";
	}else if(xmlhttp.responseText == "ok"){
		newsletter = "Thank you for submitting your details.";
	}else if(xmlhttp.responseText == "not_ok"){
		newsletter = "There were problems processing your data. Please try again.";
	}
		
	$.blockUI({ 
		message: newsletter, 
		css: { 
			padding: 10, 
			opacity: 0.9,
			color: '#ff5507', 
			border: 'none'
		}, 
		timeout:2000
	});
}

function getSelectedValue(id){
	var selectMenu = document.getElementById(id);
	var selectedOption = selectMenu.options[selectMenu.selectedIndex];
	return selectedOption.value;
}

function setShippingAddress(id){
	var checked = document.getElementById('different_shipping').checked;
	if(checked == true){
		document.getElementById(id).style.display = 'block';
	}else{
		document.getElementById(id).style.display = 'none';
	}
}

function setState(which){
	var country;
	if(which == "billing"){
		country = getSelectedValue('billing_country');
		if(country == 9){
			document.getElementById('billing-state-input').style.display = 'none';
			document.getElementById('billing-american-select').style.display = 'none';
			document.getElementById('billing-australian-select').style.display = 'block';
			document.getElementById('billing-state-star').style.display = 'inline';
			document.getElementById('error-billing-state-input').style.display = 'none';
			document.getElementById('error-billing-state-american').style.display = 'none';
			document.getElementById('error-billing-state-australian').style.display = 'block';
			
		}else if(country == 186){
			document.getElementById('billing-state-input').style.display = 'none';
			document.getElementById('billing-american-select').style.display = 'block';
			document.getElementById('billing-australian-select').style.display = 'none';
			document.getElementById('billing-state-star').style.display = 'inline';
			document.getElementById('error-billing-state-input').style.display = 'none';
			document.getElementById('error-billing-state-american').style.display = 'block';
			document.getElementById('error-billing-state-australian').style.display = 'none';
		}else{
			document.getElementById('billing-state-input').style.display = 'block';
			document.getElementById('billing-american-select').style.display = 'none';
			document.getElementById('billing-australian-select').style.display = 'none';
			document.getElementById('billing-state-star').style.display = 'none';
			document.getElementById('error-billing-state-input').style.display = 'block';
			document.getElementById('error-billing-state-american').style.display = 'none';
			document.getElementById('error-billing-state-australian').style.display = 'none';
		}
	}else if(which == "shipping"){
		country = getSelectedValue('shipping_country');
		if(country == 9){
			document.getElementById('shipping-state-input').style.display = 'none';
			document.getElementById('shipping-american-select').style.display = 'none';
			document.getElementById('shipping-australian-select').style.display = 'block';
			document.getElementById('shipping-state-star').style.display = 'inline';
			document.getElementById('error-shipping-state-input').style.display = 'block';
			document.getElementById('error-shipping-state-american').style.display = 'none';
			document.getElementById('error-shipping-state-australian').style.display = 'none';
			
		}else if(country == 186){
			document.getElementById('shipping-state-input').style.display = 'none';
			document.getElementById('shipping-american-select').style.display = 'block';
			document.getElementById('shipping-australian-select').style.display = 'none';
			document.getElementById('shipping-state-star').style.display = 'inline';
			document.getElementById('error-shipping-state-input').style.display = 'block';
			document.getElementById('error-shipping-state-american').style.display = 'none';
			document.getElementById('error-shipping-state-australian').style.display = 'none';
		}else{
			document.getElementById('shipping-state-input').style.display = 'block';
			document.getElementById('shipping-american-select').style.display = 'none';
			document.getElementById('shipping-australian-select').style.display = 'none';
			document.getElementById('shipping-state-star').style.display = 'none';
			document.getElementById('error-shipping-state-input').style.display = 'block';
			document.getElementById('error-shipping-state-american').style.display = 'none';
			document.getElementById('error-shipping-state-australian').style.display = 'none';
		}
	}
}