/*
Zhaohuastudio PHP Ecommerce
Copyright: Zhaohuastudio.com
Author: Jiayuanhua
This notice MUST stay intact for legal use
*/
function notValidName(str){
	if(str == ""){
			return true;				
	}
	if(str != ""){
		if(str.Trim() == ""){
			return true;				
		}
	}
}
String.prototype.Trim = function(){ 
	return this.replace(/(^\s*)|(\s*$)/g, ""); 
}
var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i;
function validateEmail(email_value){
	return emailfilter.test(email_value);
}
function viewImg(str){
	show("loadingContainer");
	document.getElementById("largeImage").onload	= function(){
		hide("loadingContainer");
	}
	document.getElementById("largeImage").src		= str;
}
function show(str){
	if(document.getElementById(str)){
		document.getElementById(str).style.display	= 'block';
	}
}
function hide(str){
	if(document.getElementById(str)){
		document.getElementById(str).style.display	= 'none';
	}
}
function setValue(str,str1){
	document.getElementById(str).value				= str1;
}
function setHTML(str,str1){
	if(document.getElementById(str)){
		document.getElementById(str).innerHTML		= str1;
	}
}
function enable(str){
	document.getElementById(str).disabled			= false;
}
function disable(str){
	document.getElementById(str).disabled			= true;
}
function getValue(str){
	return document.getElementById(str).value;
}
function getHTML(str){
	if(document.getElementById(str)){
		return document.getElementById(str).innerHTML;
	}
}
function cleanValue(str){
	document.getElementById(str).value				= '';
}
function cleanHTML(str){
	document.getElementById(str).innerHTML			= '';
}
function number_format(str){
	return Math.round(parseFloat(str)*100)/100;
}
function exist(str){
	if(document.getElementById(str)){
		return true;
	}else{
		return false;
	}
}
function status(str){
	return document.getElementById(str).style.display;
}
function init(str){
	if(str=="giftCertificateContainer"){
		show("giftCertificateContainer");
		cleanValue("giftCertificate_input");
		show("giftCertificateBtn");
		cleanHTML("giftCertificateResult");
	}
}
function showLanguageList(lc){
	for(var i=1; i<=lc; i++){
		show("languageItem_"+i);
	}
}
function hideLanguageList(lc){
	for(var i=1; i<=lc; i++){
		hide("languageItem_"+i);
	}
}
function setLanguage(language,language_url){
	var xmlHttp = GetXmlHttpObject();
	var url		= "setLanguage.php";
	var params	= "language="+language+"&sid="+Math.random();
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
			window.location.href = language_url;
		}
	}
	xmlHttp.send(params);
}
function showCurrencyList(cc){
	for(var i=1; i<=cc; i++){
		show("currencyItem_"+i);
	}
}
function hideCurrencyList(cc){
	for(var i=1; i<=cc; i++){
		hide("currencyItem_"+i);
	}
}
function setCurrency(currency,language_url){
	var xmlHttp = GetXmlHttpObject();
	var url		= "setCurrency.php";
	var params	= "currency="+currency+"&sid="+Math.random();
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
			window.location.href = language_url;
		}
	}
	xmlHttp.send(params);
}
function addFee(total_price){
	var c = getValue("payCount"); 
	for(var i=1; i<=c; i++){
		if(document.getElementById(i+"_payment_method").checked){
			if(document.getElementById(i+"_payment_method_fee")){
				var fee		= getValue(i+"_payment_method_fee");
				total_price = number_format(total_price*(1+parseFloat(fee)));
			}
		}
	}
	return number_format(total_price);
}
function getProductPrice(product_id,n){
	var xmlHttp	= GetXmlHttpObject();
	var url		= "getProductPrice.php";
	var params	= "product_id="+product_id;
	if(n!=''){
		for(var i=1; i<=n; i++){
			params += "&product_option_"+i+"_select="+getValue("product_option_"+i+"_select");
		}
	}else{
		params += "&product_option_select="+getValue("product_option_select");
	}
	params += "&sid="+Math.random();
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			for(var i=1; i<=n; i++){
				enable("product_option_"+i+"_select");
			}
			var str	= xmlHttp.responseText;
			var arr	= str.split("|");
			setValue("cart_product_option_str",		arr[0]);
			setValue("deposit_product_option_str",	arr[0]);
			setHTML("product_dimension",			arr[1]);
			setHTML("product_weight",				arr[2]);
			setHTML("product_price",				arr[3]);
			setHTML("product_new_price",			arr[4]);
			setHTML("product_second_price",			arr[5]);
			setHTML("product_new_price_save",		arr[6]);
			setHTML("product_second_price_save",	arr[7]);
			setHTML("product_stock",				arr[8]);
			if(arr[1].Trim()!='' && arr[8]>0){
				show("product_dimension_container");
			}else{
				hide("product_dimension_container");
			}
			if(arr[2].Trim()!='' && arr[8]>0){
				show("product_weight_container");
			}else{
				hide("product_weight_container");
			}
			if(arr[3]!='0.00' && (arr[8]>0 || (arr[9]=='1' && arr[10]!='0.00'))){
				show("product_price_container");
			}else{
				hide("product_price_container");
			}
			if(arr[4]!='0.00' && parseFloat(arr[3])>parseFloat(arr[4]) && (arr[8]>0 || (arr[9]=='1' && arr[10]!='0.00'))){
				show("product_new_price_container");
			}else{
				hide("product_new_price_container");
			}
			if(arr[5]!='0.00' && (arr[8]>0 || (arr[9]=='1' && arr[10]!='0.00'))){
				show("product_second_price_container");
			}else{
				hide("product_second_price_container");
			}
			if(arr[8]>0){
				show("product_stock_container");
				hide("out_of_stock_container");
				show("add_to_cart_container");
			}else{
				hide("product_stock_container");
				show("out_of_stock_container");
				hide("add_to_cart_container");
			}
			if(arr[9]=='1' && arr[10]!='0.00'){
				show("deposit_container");
				setHTML("product_deposit",	arr[10]);
			}else{
				hide("deposit_container");
			}
		}else{
			for(var i=1; i<=n; i++){
				disable("product_option_"+i+"_select");
			}
		}
	}
	xmlHttp.send(params);
}
function getList(obj,type,containerA,containerB){
	var xmlHttp	= GetXmlHttpObject();
	var url		= "getList.php";
	var params	= "id="+obj.value+"&type="+type+"&shipping_status="+getValue("shipping_status");
	params	   += "&sid="+Math.random();
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			var str	= xmlHttp.responseText;
			var arr = str.split("|");
			if(type=='LS'){
				setHTML(containerA,	arr[0]);
				setHTML(containerB,	arr[1]);
			}else{
				setHTML(containerA,	arr[0]);
			}
			enable("country_select");
			enable("location_select");
			enable("shipping_method_select");
		}else{
			if(getValue("shipping_status")=='1'){
				hideShippingPrice();
			}
			disable("country_select");
			disable("location_select");
			disable("shipping_method_select");
		}
	}
	xmlHttp.send(params);
}
function getShippingPrice(){
	if(getValue("shipping_method_select")!=0){
		var xmlHttp				= GetXmlHttpObject();
		var url					= "getShippingPrice.php";
		var location_id			= getValue("location_select");
		var shipping_method_id	= getValue("shipping_method_select");
		var params				= "location_id="+location_id+"&shipping_method_id="+shipping_method_id;
		params				   += "&sid="+Math.random();
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.onreadystatechange=function(){
			if(xmlHttp.readyState==4){
				var str	= xmlHttp.responseText;
				var arr = str.split("|");
				var location_rate					= 1+parseFloat(arr[1]);
				var item_total_price				= number_format(getValue("item_total_price_ori")*location_rate);
				var shipping_total_price			= 0.00;
				if(arr[0]!='MANUAL'){
					shipping_total_price			= parseFloat(arr[0]);
					if(arr[2]=='1'){
						shipping_total_price		= number_format(shipping_total_price*location_rate);
					}
				}
				var total_price						= addFee(item_total_price+shipping_total_price);
				setValue("location_rate",			arr[1]);
				setValue("apply_to_shipping_price",	arr[2]);
				setValue("item_total_price",		item_total_price);
				setValue("shipping_total_price",	shipping_total_price);
				setValue("total_price",				total_price);

				show("shippingPriceContainer");				
				setHTML("shippingPrice",			arr[0]);
				if(arr[0]!='MANUAL'){
					show("currencySign");
					show("shippingPrice");
					hide("shippingPriceComment");
					cleanValue("MANUAL_CALCULATE");
				}else{
					hide("currencySign");
					hide("shippingPrice");
					show("shippingPriceComment");
					setHTML("shippingPriceComment",getValue("MANUAL"));
					setValue("MANUAL_CALCULATE",'1');
				}
				init("giftCertificateContainer");
				show("totalPriceContainer");
				setHTML("totalPrice",				total_price);
				if(location_rate>1){
					show("tax");
				}else{
					hide("tax");
				}
				setHTML("totalPriceSave",			getValue("total_price_save_ori"));
				show("submitContainer");
			}else{
				hideShippingPrice();
			}
		}
		xmlHttp.send(params);
	}else{
		hideShippingPrice();
	}
}
function hideShippingPrice(){
	hide("shippingPriceContainer");
	hide("giftCertificateContainer");
	hide("totalPriceContainer");
	hide("submitContainer");
}
function checkSignIn(str){
	var email	= document.getElementById(str).email.value;
	if(notValidName(email) || !validateEmail(email)){
		setHTML("signInTxt",getValue("email_error"));
		return;
	}
	document.getElementById(str).submit();
}
function checkRegister(str){
	var xmlHttp	= GetXmlHttpObject();
	var email	= document.getElementById(str).email.value;
	if(notValidName(email) || !validateEmail(email)){
		setHTML("registerTxt",getValue("email_error"));
		return;
	}
	var response_field		= getValue("recaptcha_response_field");	
	var challenge_field		= getValue("recaptcha_challenge_field");
	var params				= "response_field="+response_field+"&challenge_field="+challenge_field+"&sid="+Math.random();
	xmlHttp.open("POST","checkcaptcha.php",true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			if(xmlHttp.responseText == 'valid'){
				document.getElementById(str).submit();
			}else{
				setHTML("registerTxt",getValue("recaptcha_error"));
			}
		}
	}
	xmlHttp.send(params);
}
function checkProfile(str){
	var email	= document.getElementById(str).email_input.value;
	if(notValidName(email) || !validateEmail(email)){
		alert(getValue("email_error"));
		return;
	}else{
		document.getElementById(str).submit();
	}
}
function checkPassword(str){
	var errorStr	= "";
	var pwd			= getValue("pwd");
	var pwd_con		= getValue("pwd_con");
	if(notValidName(pwd) || pwd.length < 6 ){
		errorStr = errorStr + getValue("pwd_str") + " \n";
	}
	if(pwd != pwd_con){
		errorStr = errorStr + getValue("pwd_str1") + " \n";
	}
	if(errorStr != ""){
		alert(errorStr);
		return false;
	}else{
		document.getElementById(str).submit();
	}
}
function checkOrderView(str){
	document.getElementById(str).submit();
}
function checkPost(str,type,id,message_control){
	var xmlHttp	= GetXmlHttpObject();
	var comment_input = getValue("comment_input");
	if(notValidName(comment_input)){
		setHTML("postMsg",getValue("post_error"));
	}else{
		var url		= "post.php";
		var params	= "&type="+type+"&id="+id+"&comment_input="+comment_input+"&message_control="+message_control+"&sid="+Math.random();
		xmlHttp.open("POST",url,true);
		xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		xmlHttp.setRequestHeader("Content-length", params.length);
		xmlHttp.onreadystatechange = function(){
			if(xmlHttp.readyState==4){
				setHTML("postMsg",xmlHttp.responseText);
			}
		}
		xmlHttp.send(params);
	}
}
function applyGiftCertificate(){
	var xmlHttp	= GetXmlHttpObject();
	var code	= getValue("giftCertificate_input");
	if(notValidName(code)){
		setHTML("giftCertificateResult",getValue("giftCertificate_error"));
		return;
	}
	var url		= "applyGiftCertificate.php";
	var params	= "code="+code;
	params	   += "&sid="+Math.random();
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			var str	= xmlHttp.responseText;
			var arr = str.split("|");
			if(arr[0]=='valid'){
				var value = parseFloat(arr[1]);
				var up_to = parseFloat(arr[2]);
				var item_total_price				= parseFloat(getValue("item_total_price_ori"));
				var total_price						= parseFloat(getHTML("totalPrice"));
				var total_price_save				= parseFloat(getHTML("totalPriceSave"));
				if(item_total_price<up_to){
					setHTML("giftCertificateResult",getValue("giftCertificate_invalid"));
				}else{
					setHTML("totalPrice",			number_format(total_price		- value));
					setValue("total_price",			number_format(total_price		- value));
					setHTML("totalPriceSave",		number_format(total_price_save	+ value));
					setValue("total_price_save",	number_format(total_price_save	+ value));
					setHTML("giftCertificateResult",getValue("giftCertificate_success"));
					hide("giftCertificateBtn");
				}
			}else{
				setHTML("giftCertificateResult",	getValue("giftCertificate_invalid"));
			}
		}
	}
	xmlHttp.send(params);
}
function selectPayment(shipping){
	var item_total_price		= number_format(getValue("item_total_price_ori"));
	var location_rate			= number_format(getValue("location_rate"));
	var shipping_total_price	= number_format(getValue("shipping_total_price"));
	var total_price_save		= number_format(getValue("total_price_save_ori"));
	if(shipping=='0'){
		if(getValue("AGENCY_ENABLED")=='0'){
			hide("shippingContainer");
		}else{
			show("shippingContainer");
		}
		hide("shippingAddress");
		init("giftCertificateContainer");
		show("totalPriceContainer");
		setValue("total_price",			addFee(item_total_price));
		setHTML("totalPrice",			addFee(item_total_price));
		hide("tax");
		show("submitContainer");
	}else{
		show("shippingContainer");
		show("shippingAddress");
		if(getValue("shipping_method_select")!='0'){
			init("giftCertificateContainer");
			show("totalPriceContainer");
			item_total_price			= number_format(item_total_price*(1+location_rate));
			if(getValue("MANUAL_CALCULATE")==''){
				setValue("total_price",	addFee(item_total_price+shipping_total_price));
				setHTML("totalPrice",	addFee(item_total_price+shipping_total_price));
			}else{
				setValue("total_price",	addFee(item_total_price));
				setHTML("totalPrice",	addFee(item_total_price));
			}
			if(getValue("location_rate")!='0.00'){
				show("tax");
			}else{
				hide("tax");
			}
			show("submitContainer");
		}else{
			hide("giftCertificateContainer");
			hide("totalPriceContainer");
			hide("submitContainer");
		}
	}
	setValue("total_price_save",total_price_save);
	setHTML("totalPriceSave",	total_price_save);
}
function submitOrder(str,str1){
	var contact_email	= getValue("contactEmail_input");
	var contact_phone	= getValue("contactPhone_input");
	if(exist("shippingContainer") && status("shippingContainer")=='block' && getValue("shipping_method_select")=='0'){
		alert(str);
		return;
	}
	if(exist("shippingAddress") && status("shippingAddress")=='block' && notValidName(getValue("contactAddress_input"))){
		alert(str);
		return;
	}
	if(notValidName(contact_email) || notValidName(contact_phone)){
		alert(str);
		return;
	}
	if(!validateEmail(contact_email)){
		alert(str1);
		return;
	}
	submitForm("order_form");
}
function submitForm(str){
	document.getElementById(str).submit();
}
function setPos(){
	if(document.getElementById("popUpContainer")){
		var scrollW = document.documentElement.scrollWidth;
		var scrollH = document.documentElement.scrollHeight;
		var scrollT = document.documentElement.scrollTop?document.documentElement.scrollTop:document.body.scrollTop;
		document.getElementById("popUpContainer").style.left	= (document.body.clientWidth-600)/2 + "px";
		document.getElementById("popUpContainer").style.top		= (scrollT+100) + "px";
		document.getElementById("overlay").style.Width			= scrollW + "px";
		document.getElementById("overlay").style.height			= scrollH + 100 + "px";
	}
}
function hotPopUp(id){
	setPos();
	show("popUpContainer");
	setHTML("popUpContent","<div class=\"loadingTxt\">"+getValue("loadingTxt")+"</div>");
	show("overlay");
	var xmlHttp	= GetXmlHttpObject();
	var url		= "getProduct.php";
	var params	= "&product_id="+id+"&sid="+Math.random();
	xmlHttp.open("POST",url,true);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.onreadystatechange = function(){
		if(xmlHttp.readyState==4){
			setHTML("popUpContent",xmlHttp.responseText);
			window.setTimeout("setPos()", 1000);
		}
	}
	xmlHttp.send(params);
}
function clearPopUp(){
	cleanHTML("popUpContent");
	hide("popUpContainer");
	hide("overlay");
}
function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
		xmlHttp=new XMLHttpRequest();
	}catch (e){		
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
function checkQuote(){
	var quote_name		= getValue("quoteName_input");
	var quote_email		= getValue("quoteEmail_input");
	var quote_subject	= getValue("quoteSubject_input");
	var quote_content	= getValue("quoteContent_input");
	if(notValidName(quote_name) ||notValidName(quote_email) || !validateEmail(quote_email) || notValidName(quote_subject) || notValidName(quote_content)){
	}else{
		document.getElementById("quote_form").target = '_self';
		setValue("act",'quote');
		setValue("str",'');
		submitForm("quote_form");
	}
}
function ajaxUpload(str,url_action,id_element){
	var nv = getValue(id_element);
	var ov = getValue(id_element+"_ov");
	if(nv!=ov){
		form = document.getElementById(str);
		if(document.getElementById(id_element+"_iframe")==null){
			var iframe = document.createElement("iframe");
			iframe.setAttribute("id",	id_element+"_iframe");
			iframe.setAttribute("name",	id_element+"_iframe");
			iframe.setAttribute("width","0");
			iframe.setAttribute("height","0");
			iframe.setAttribute("border","0");
			iframe.setAttribute("style","width:0;height:0;border:none;");
			form.parentNode.appendChild(iframe);
		}
		window.frames[id_element+'_iframe'].name = id_element+"_iframe";
		form.setAttribute("target",id_element+"_iframe");
		form.setAttribute("action",url_action);
		form.setAttribute("method","post");
		form.setAttribute("enctype","multipart/form-data");
		setHTML(id_element+"_area",'<img src="images/loader_light_blue.gif" style="padding-top:3px;">');
		setValue(id_element+"_ov",nv);
		setValue("str",id_element);
		form.submit();
	}
}
window.onresize = setPos;
function clickIE(){
	if(document.all){
		return false;
	}
}
function clickNS(e){
	if (document.layers||(document.getElementById&&!document.all)){
		if (e.which==2||e.which==3) {
			return false;
		}
	}
}
if (document.layers){
	document.captureEvents(Event.MOUSEDOWN);
	document.onmousedown = clickNS;
}else{
	document.onmouseup = clickNS;
	document.oncontextmenu = clickIE;
}
document.oncontextmenu = new Function("return false")
