var oXmlHttp = zXmlHttp.createRequest();
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
// JScript gives us Conditional compilation, we can cope with old IE versions.
// and security blocked creation of the objects.
try {
oXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
} catch (E) {
oXmlHttp = false;
}
}
@end @*/

function addToCart(item_id, item_qty, item_price, item_name, msgDivId){
	var url = "cartOperation.php?action=addToCart&item_id="+item_id+"&item_qty="+item_qty+"&item_price="+item_price+"&item_name="+item_name;
	oXmlHttp.open("GET", url, true);
	//alert(url);
	$$(msgDivId).innerHTML = "";
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				var res_str = oXmlHttp.responseText;
				var res_array = res_str.split('||');
				$$('itemCount').innerHTML = res_array[0];
				$$(msgDivId).innerHTML = res_array[1];
			} else {
				alert("An error occurred: " + oXmlHttp.statusText); //statusText is not always accurate
			}
		}            
	};
	oXmlHttp.send(null);
}

function doUpdateCart(cartContainerTabId, msgDivId){
	//alert('');
	var ids = "";
	var qtys = "";
	var len = $$('frm').elements.length;
	for(var i=0; i<len; i++){
		if($$('frm').elements[i].name=='qtys[]'){
			if(isWhitespace($$('frm').elements[i].value)){
				alert('Quantity should not be blank');
				$$('frm').elements[i].focus();
				return false;
			}
			else if(!isPositiveInteger($$('frm').elements[i].value)){
				alert('Quantity should be positive integer');
				$$('frm').elements[i].focus();
				return false;
			}
			else{
				qtys += (qtys == "") ? $$('frm').elements[i].value : ","+$$('frm').elements[i].value;
				ids += (ids == "") ? $$('frm').elements[i+1].value : ","+$$('frm').elements[i+1].value;
			}
		}
	}
	if(ids != ""){
		var url = "cartOperation.php";
		var params = "action=updateCart&ids="+ids+"&qtys="+qtys+"&order_comments="+$$('order_comments').value;
		oXmlHttp.open("POST", url, true);
		//alert(url);
		$$(msgDivId).innerHTML = "";
		oXmlHttp.onreadystatechange = function () {
			if (oXmlHttp.readyState == 4) {
				if (oXmlHttp.status == 200) {
					var res_str = oXmlHttp.responseText;
					var res_array = res_str.split('||');
					$$('itemCount').innerHTML = res_array[0];
					$$(msgDivId).innerHTML = res_array[1];
					$$(cartContainerTabId).innerHTML = res_array[2];
				} else {
					alert("An error occurred: " + oXmlHttp.statusText); //statusText is not always accurate
				}
			}            
		};
		oXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		oXmlHttp.setRequestHeader("Content-length", params.length);
		oXmlHttp.setRequestHeader("Connection", "close");
		oXmlHttp.send(params);
	}
}

function goToCheckOut(msgDivId){
	if($$('order_shipping_area').value == '0.00')
	{
		alert("Please Select Parcel Size");
		$$('order_shipping_area').focus();
		return;
	}
	
	var url = "cartOperation.php?action=checkOut";
	//alert(url);
	oXmlHttp.open("GET", url, true);
	$$(msgDivId).innerHTML = "";
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				var res_str = oXmlHttp.responseText;
				var res_array = res_str.split('||');
				if(res_array[0] == "Yes"){ $$('frm').submit(); }
				else { $$(msgDivId).innerHTML = res_array[1]; }
			} else {
				alert("An error occurred: " + oXmlHttp.statusText); //statusText is not always accurate
			}
		}            
	};
	oXmlHttp.send(null);
}

function doContinueShopping(){
	var url = "cartOperation.php";
	var params = "action=continueShopping&order_comments="+$$('order_comments').value;
	oXmlHttp.open("POST", url, true);
	//alert(url);
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				var res_str = oXmlHttp.responseText;
				location.href = res_str;
			} else {
				alert("An error occurred: " + oXmlHttp.statusText); //statusText is not always accurate
			}
		}            
	};
	oXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	oXmlHttp.setRequestHeader("Content-length", params.length);
	oXmlHttp.setRequestHeader("Connection", "close");
	oXmlHttp.send(params);
}

function doEmptyCart(cartContainerTabId, msgDivId){
	var url = "cartOperation.php?action=emptyCart";
	if(confirm("Are sure to make the cart empty?")){
		oXmlHttp.open("GET", url, true);
		//alert(url);
		$$(msgDivId).innerHTML = "";
		oXmlHttp.onreadystatechange = function () {
			if (oXmlHttp.readyState == 4) {
				if (oXmlHttp.status == 200) {
					var res_str = oXmlHttp.responseText;
					var res_array = res_str.split('||');
					$$('itemCount').innerHTML = res_array[0];
					$$(msgDivId).innerHTML = res_array[1];
					$$(cartContainerTabId).innerHTML = res_array[2];
					$$('order_comments').value = "";
				} else {
					alert("An error occurred: " + oXmlHttp.statusText); //statusText is not always accurate
				}
			}            
		};
		oXmlHttp.send(null);
	}
}

function deleteSingleItem(item_id, cartContainerTabId, msgDivId){
	var url = "cartOperation.php?action=deleteItem&item_id="+item_id;
	if(confirm("Are sure to delete the item from the cart?")){
		oXmlHttp.open("GET", url, true);
		//alert(url);
		$$(msgDivId).innerHTML = "";
		oXmlHttp.onreadystatechange = function () {
			if (oXmlHttp.readyState == 4) {
				if (oXmlHttp.status == 200) {
					var res_str = oXmlHttp.responseText;
					var res_array = res_str.split('||');
					$$('itemCount').innerHTML = res_array[0];
					$$(msgDivId).innerHTML = res_array[1];
					$$(cartContainerTabId).innerHTML = res_array[2];
				} else {
					alert("An error occurred: " + oXmlHttp.statusText); //statusText is not always accurate
				}
			}            
		};
		oXmlHttp.send(null);
	}
}

function setTax(taxPerc){
	$$('txCountry').innerHTML=taxPerc;
	var url = "cartOperation.php";
	var params = "action=setTax&taxPerc="+taxPerc;
	oXmlHttp.open("POST", url, true);
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				var res_str = oXmlHttp.responseText;
				var res_array = res_str.split(':');
				$$('taxContainer').innerHTML = res_array[0];
				$$('gtContainer').innerHTML = res_array[1];
			} else {
				alert("An error occurred: " + oXmlHttp.statusText); //statusText is not always accurate
			}
		}            
	};
	oXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	oXmlHttp.setRequestHeader("Content-length", params.length);
	oXmlHttp.setRequestHeader("Connection", "close");
	oXmlHttp.send(params);
}

function setShipping(shippingCharge){
	$$('shippingCharge').innerHTML=shippingCharge;
	var url = "cartOperation.php"
	var params = "action=setShipping&shippingCharge="+shippingCharge;
	oXmlHttp.open("POST", url, true);
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				var res_str = oXmlHttp.responseText;
				var res_array = res_str.split(':');
				$$('taxContainer').innerHTML = res_array[0];
				$$('gtContainer').innerHTML = res_array[1];
				$$('shipContainer').innerHTML = res_array[2];
			} else {
				alert("An error occurred: " + oXmlHttp.statusText); //statusText is not always accurate
			}
		}            
	};
	oXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	oXmlHttp.setRequestHeader("Content-length", params.length);
	oXmlHttp.setRequestHeader("Connection", "close");
	oXmlHttp.send(params);
}

function memberLoginValidation(){
	if(isWhitespace($$('member_username').value))
	{
		alert("Please Enter User Name");
		$$('member_username').focus();
		return false;
	}
	if(isWhitespace($$('member_password').value))
	{
		alert("Please Enter Password");
		$$('member_password').focus();
		return false;
	}
	return true;
}

function memberValidation(){
	if(isWhitespace($$('member_username').value))
	{
		alert("Please Enter User Name");
		$$('member_username').focus();
		return false;
	}
	if(isWhitespace($$('member_password').value) || ($$('member_password').value.length<5))
	{
		alert("Please Enter Password with Minimum 5 Character Length");
		$$('member_password').focus();
		return false;
	}
	if(isWhitespace($$('retype_password').value))
	{
		alert("Please Retype Password");
		$$('retype_password').focus();
		return false;
	}
	if($$('retype_password').value != $$('member_password').value)
	{
		alert("Password Mismatch");
		$$('retype_password').focus();
		return false;
	}
	if(isWhitespace($$('member_firstname').value))
	{
		alert("Please Enter First Name");
		$$('member_firstname').focus();
		return false;
	}
	if(isWhitespace($$('member_lastname').value))
	{
		alert("Please Enter Last Name");
		$$('member_lastname').focus();
		return false;
	}
	if(isWhitespace($$('member_address').value))
	{
		alert("Please Enter Street Address");
		$$('member_address').focus();
		return false;
	}
	if(isWhitespace($$('member_city').value))
	{
		alert("Please Enter City");
		$$('member_city').focus();
		return false;
	}
	if(isWhitespace($$('member_state').value))
	{
		alert("Please Enter State");
		$$('member_state').focus();
		return false;
	}
	if(isWhitespace($$('member_postcode').value))
	{
		alert("Please Enter Post Code");
		$$('member_postcode').focus();
		return false;
	}
	if(isWhitespace($$('member_country_id').value))
	{
		alert("Please Enter Select Country");
		$$('member_country_id').focus();
		return false;
	}
	if(isWhitespace($$('member_phone').value))
	{
		alert("Please Enter Phone");
		$$('member_phone').focus();
		return false;
	}
	if(isWhitespace($$('member_email').value))
	{
		alert("Please Enter Email");
		$$('member_email').focus();
		return false;
	}
	else if(!isEmail($$('member_email').value)){
		alert("Please Enter Valid Email");
		$$('member_email').focus();
		return false;
	}
	return true;
}


function checkoutValidation(){
	if(isWhitespace($$('order_billing_first_name').value))
	{
		alert("Please Enter First Name");
		$$('order_billing_first_name').focus();
		return false;
	}
	if(isWhitespace($$('order_billing_last_name').value))
	{
		alert("Please Enter Last Name");
		$$('order_billing_last_name').focus();
		return false;
	}
	if(isWhitespace($$('order_billing_phone').value))
	{
		alert("Please Enter Phone");
		$$('order_billing_phone').focus();
		return false;
	}
	if(isWhitespace($$('order_billing_email').value))
	{
		alert("Please Enter Email");
		$$('order_billing_email').focus();
		return false;
	}
	else if(!isEmail($$('order_billing_email').value)){
		alert("Please Enter Valid Email");
		$$('order_billing_email').focus();
		return false;
	}
	if(isWhitespace($$('cardType').value))
	{
		alert("Please Select Credit Card Type");
		$$('cardType').focus();
		return false;
	}
	if(isWhitespace($$('cardNumber').value))
	{
		alert("Please Enter Credit Card Number");
		$$('cardNumber').focus();
		return false;
	}
	if(isWhitespace($$('cardVerificationNumber').value))
	{
		alert("Please Enter CCV Number");
		$$('cardVerificationNumber').focus();
		return false;
	}
	if(isWhitespace($$('cardExpiryMonth').value))
	{
		alert("Please Enter Card Expiry Month");
		$$('cardExpiryMonth').focus();
		return false;
	}
	if(isWhitespace($$('cardExpiryYear').value))
	{
		alert("Please Enter Card Expiry Year");
		$$('cardExpiryYear').focus();
		return false;
	}
	if(isWhitespace($$('order_billing_address').value))
	{
		alert("Please Enter Billing Address");
		$$('order_billing_address').focus();
		return false;
	}
	if(isWhitespace($$('order_billing_city').value))
	{
		alert("Please Enter Billing City");
		$$('order_billing_city').focus();
		return false;
	}
	if(isWhitespace($$('order_billing_state').value))
	{
		alert("Please Enter Billing State");
		$$('order_billing_state').focus();
		return false;
	}
	if(isWhitespace($$('order_billing_zip').value))
	{
		alert("Please Enter Billing Post Code");
		$$('order_billing_zip').focus();
		return false;
	}
	if(isWhitespace($$('order_billing_country').value))
	{
		alert("Please Select Billing Country");
		$$('order_billing_country').focus();
		return false;
	}
	if(isWhitespace($$('order_shipping_address').value))
	{
		alert("Please Enter Shipping Address");
		$$('order_shipping_address').focus();
		return false;
	}
	if(isWhitespace($$('order_shipping_city').value))
	{
		alert("Please Enter Shipping City");
		$$('order_shipping_city').focus();
		return false;
	}
	if(isWhitespace($$('order_shipping_state').value))
	{
		alert("Please Enter Shipping State");
		$$('order_shipping_state').focus();
		return false;
	}
	if(isWhitespace($$('order_shipping_zip').value))
	{
		alert("Please Enter Shipping Post Code");
		$$('order_shipping_zip').focus();
		return false;
	}
	if(isWhitespace($$('order_shipping_country').value))
	{
		alert("Please Select Shipping Country");
		$$('order_shipping_country').focus();
		return false;
	}
	return true;
}

function checkUncheck(){
	if($$('chkShip').checked == true){
		$$('order_shipping_address2').value = $$('order_billing_address2').value;
		$$('order_shipping_address').value = $$('order_billing_address').value;
		$$('order_shipping_city').value = $$('order_billing_city').value;
		$$('order_shipping_state').value = $$('order_billing_state').value;
		$$('order_shipping_zip').value = $$('order_billing_zip').value;
		$$('order_shipping_country').value = $$('order_billing_country').value;
	} else {
		$$('order_shipping_address2').value = "";
		$$('order_shipping_address').value = "";
		$$('order_shipping_city').value = "";
		$$('order_shipping_state').value = "";
		$$('order_shipping_zip').value = "";
		$$('order_shipping_country').value = "";
	}

}

function forgotPasswordValidation(){
	if(isWhitespace($$('member_email').value))
	{
		alert("Please Enter Email");
		$$('member_email').focus();
		return false;
	}
	else if(!isEmail($$('member_email').value)){
		alert("Please Enter Valid Email");
		$$('member_email').focus();
		return false;
	}
	return true;
}

function newsletterSubscriptionValidation(){
	if(isWhitespace($$('listid').value))
	{
		alert("Please Select Mailing List");
		$$('listid').focus();
		return false;
	}
	if(isWhitespace($$('firstname').value))
	{
		alert("Please Enter First Name");
		$$('firstname').focus();
		return false;
	}
	if(isWhitespace($$('lastname').value))
	{
		alert("Please Enter Last Name");
		$$('lastname').focus();
		return false;
	}
	if(isWhitespace($$('email').value))
	{
		alert("Please Enter Email");
		$$('email').focus();
		return false;
	}
	else if(!isEmail($$('email').value)){
		alert("Please Enter Valid Email");
		$$('email').focus();
		return false;
	}
	return true;
}

function validateCoupon(){
	if(isWhitespace($$('coupon_code').value)){
		alert("Please Enter Voucher Code");
		$$('coupon_code').focus();
		return;
	}
	else if($$('coupon_status').value == 'Invalid'){
		alert("Please Enter Valid Voucher Code");
		$$('coupon_code').focus();
		return;
	}
	else
	{
		var xmlhttp=false;
	
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
		// JScript gives us Conditional compilation, we can cope with old IE versions.
		// and security blocked creation of the objects.
		try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
		try {
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
		xmlhttp = false;
		}
		}
		@end @*/
	
		if (!xmlhttp && typeof XMLHttpRequest!='undefined')
		{
			xmlhttp = new XMLHttpRequest();
		}
		if (xmlhttp) 
		{ 
			var query = "action=applyCoupon&coupon_code="+document.getElementById('coupon_code').value;
			xmlhttp.open('POST', 'cartOperation.php'); 
			xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
			xmlhttp.send(query); 
			xmlhttp.onreadystatechange = function() 
			{ 
				if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
				{
						var res_str = xmlhttp.responseText;
						var res_array = res_str.split(':');
						$$('taxContainer').innerHTML = res_array[0];
						$$('gtContainer').innerHTML = res_array[1];
						$$('disPrecentage').innerHTML = res_array[2];
						$$('disAmmount').innerHTML = res_array[3];		
				}
			}
			delete xmlhttp;
		}		
	}
}

function setCouponStatus(val){
	if ((val == null) || (val == "") || (val == "undefined")) return;
	var url = "check_coupon.php?coupon_code=" + val;
	oXmlHttp.open("GET", url, true);
	//alert(url);
	oXmlHttp.onreadystatechange = function () {
		if (oXmlHttp.readyState == 4) {
			if (oXmlHttp.status == 200) {
				var res_str = oXmlHttp.responseText;
				//alert(res_str);
				var res_array = res_str.split('||');
				$$('coupon_status').value = res_array[0];
				//if(!((res_array[1] == null) || (res_array[1] == "") || (res_array[1] == "undefined")))
					//alert(res_array[1]);
			} else {
				alert("An error occurred: " + oXmlHttp.statusText); //statusText is not always accurate
			}
		}            
	};
	oXmlHttp.send(null);
}
