hostObj=new Object();
if(location.hostname == "localhost" || location.hostname == "lin"){
	hostObj.folder="/coolideashop";
	hostObj.url = location.protocol + "//" + location.hostname + hostObj.folder;
}else if(location.hostname == "www.dezextion.com"){
	hostObj.folder="/demo-shop";
	hostObj.url = location.protocol + "//" + location.hostname + hostObj.folder;
}else{
	hostObj.folder="";
	hostObj.url = location.protocol + "//" + location.hostname + hostObj.folder;
}

function del(varUrl)
{
	if (window.confirm("Are you sure to delete?")==true)
	{
		window.open(varUrl,"_self")
	}
}
function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function btn_number(frm_name,number,type){
	var cur_number = parseInt(document.getElementById(frm_name).num_item.value);
		switch (type){
			case "+" :
				document.getElementById(frm_name).num_item.value = cur_number + number ;
				break;
			case "-" :
				var result_number = cur_number - number ;
				if(result_number > 0){
					document.getElementById(frm_name).num_item.value = cur_number - number ;
				}
				break;
		}
}

function display_cart(page_mode){
	var URL = hostObj.url + "/product/script-cart.php";
	var data = "page_mode=" + page_mode + "&mode=display" ;
	var div = "basket_detail";
	ajaxLoad("post",URL,data,div);
}

function add_cart(frm,frm_name){
		
	var URL = hostObj.url + "/product/script-cart.php";
	
	if(frm.num_item.value == "" || frm.num_item.value.search("[^0-9]") >= 0){
		alert("Please fill number format.");
		frm.num_item.select();
		return false;		
	}
	
	if(frm.num_item == ""){
		var num_item = 1;
	}else{
		var num_item = parseInt(frm.num_item.value);
	}
	var product_price = frm.product_price.value;
	var page_mode = frm.page_mode.value;
	var mode = "add";
	
	var data = getFormData(frm_name);
	var data = data + "&mode=" + mode;
	var div = "basket_detail";
		//alert(URL);
		var old_item = parseInt(document.getElementById("basket_item").value);
		var old_price = parseInt(document.getElementById("basket_price").value);

		document.getElementById("basket_item").value = old_item + num_item;
		document.getElementById("basket_price").value = old_price + (num_item * product_price);
		document.getElementById("span_basket_item").innerHTML = addCommas(old_item + num_item);
		document.getElementById("span_basket_price").innerHTML = addCommas(old_price + (num_item * product_price));
		ajaxLoad("post",URL,data,div);
		
		//animatedcollapse.toggle('basket_detail');
		
		//var pa = function(){};
		if(page_mode == "checkout"){
			//setTimeout(load_cart("shopping"),1000);
			load_cart("shopping");
			//var set2 = setTimeout(load_cart("confirm"),1000);
			load_cart("confirm");
			//var set3 = setTimeout(check_cart("confirm"),1000);
			check_cart("confirm");
			animatedcollapse.show('div_order_step');
		}
}

function del_cart(array_key,num_item,product_price,page_mode){
	var mode = "delete";
	var URL = hostObj.url + "/product/script-cart.php";
	var data = "mode=" + mode + "&array_key=" + array_key + "&num_item=" + num_item + "&product_price=" + product_price + "&page_mode=" + page_mode;
	var div = "basket_detail";
		var old_item = parseInt(document.getElementById("basket_item").value);
		var old_price = parseInt(document.getElementById("basket_price").value);

		document.getElementById("basket_item").value = old_item - num_item;
		document.getElementById("basket_price").value = old_price - (num_item * product_price);
		document.getElementById("span_basket_item").innerHTML = addCommas(old_item - num_item);
		document.getElementById("span_basket_price").innerHTML = addCommas(old_price - (num_item * product_price));
		ajaxLoad("post",URL,data,div);
		
		if(page_mode == "checkout"){
			//alert("Romove item.");
			load_cart("shopping");
			load_cart("confirm");
			check_cart("confirm");
		} 
		
}

function unset_cart(page_mode,condition){
	
		if (window.confirm("Are you sure to delete?")==true){
			var mode = "unset";
			var URL = hostObj.url + "/product/script-cart.php";
			var data = "mode=" + mode + "&page_mode=" + page_mode;
			var div = "basket_detail";			  
				document.getElementById("basket_item").value = 0;
				document.getElementById("basket_price").value = 0;
				document.getElementById("span_basket_item").innerHTML = 0;
				document.getElementById("span_basket_price").innerHTML = 0;
				ajaxLoad("post",URL,data,div);
				
				if(page_mode == "checkout"){
					alert("ตะกร้าไม่มีสินค้าแล้ว");
					load_cart("shopping");
					load_cart("confirm");
					check_cart("confirm");
				} 
		}
		  
}

function clear_cart(page_mode){
			var mode = "unset";
			var URL = hostObj.url + "/product/script-cart.php";
			var data = "mode=" + mode + "&page_mode=" + page_mode;
			var div = "basket_detail";			  
				document.getElementById("basket_item").value = 0;
				document.getElementById("basket_price").value = 0;
				document.getElementById("span_basket_item").innerHTML = 0;
				document.getElementById("span_basket_price").innerHTML = 0;
				ajaxLoad("post",URL,data,div);
}

function load_cart(mode){
	var URL = hostObj.url + "/product/script-load-cart.php";
	var data = "mode=" + mode;
	if(mode == "shopping"){
		var div = "div_cart_shop";
	}else if(mode == "confirm"){
		var div = "div_cart_confirm";	
	}
		ajaxLoad("post",URL,data,div);
	
}

function form_valid(mode,input_value,el_name,sub_value){
	var URL = hostObj.url + "/user/script-form-valid.php";
	var form_data = "mode=" + mode + "&input_value=" + input_value + "&sub_value=" + sub_value + "&el_name=" + el_name;
	var div = "div_profile_info_report";
	$.post(URL, form_data,
		function(data){

		}
	);
}

function user_login(frm){
	var URL = hostObj.url + "/user/script-login.php";
	var form_data = $("#"+frm).serialize();
	var div = "div_user_report";
	$.post(URL, form_data,
		function(data){
			 	//alert("Data Loaded: " + data);
				//proceed(data);
				//$("#"+div).html(data);
		}
	);
		//ajaxLoad("post",URL,data,div);

		/*var form_data = $("#form_user").serialize();
		var username =$("#user_name").val();
		var password = $("#user_pass").val();
		var referer = $("#referer").val();
		var formhash = $("#formhash").val();
		//alert(formhash);
		$.post(hostObj.url + "/zone/board/logging.php", "formhash="+ formhash +"&referer="+ referer +"&loginfield=username&username="+ username +"&password="+ password + "&action=login&loginsubmit=yes&questionid=0&answer=",
			function(data){
			 //alert("Data Loaded: " + data);
			//$("#result").text(data);
			});		*/
}

function user_logout(page_mode){
	var URL = hostObj.url + "/user/script-logout.php";
	var form_data = "page_mode=" + page_mode ;
	var div = "";
	$.post(URL, form_data,
		function(data){

		}
	);
		//alert(formhash);
		/*$.post(hostObj.url + "/zone/board/logging.php", "action=logout&formhash="+ formhash,
			function(data){
			//alert("Data Loaded: " + data);
			//$("#result").text(data);
			});	*/		
}

function user_register(frm){
	var URL = hostObj.url + "/user/script-register.php";
	var form_data = $("#"+frm).serialize();
	var div = "div_register_report";
	//alert(form_data);
	$.post(URL, form_data,
			function(data){
			 //alert("Data Loaded: " + data);
			//$("#+"div).text(data);
			});
		
	/*var username =$("#txt_name").val();
	var password = $("#txt_pass").val();
	var password2 = $("#confirm_pass").val();
	var email = $("#txt_email").val();
	var referer = $("#referer").val();
	var formhash = $("#formhash").val();
	var regis_mode = $("#regis_mode").val();
	var form_data = "formhash="+ formhash +"&referer="+ referer +"&username="+ username +"&password="+ password + "&password2="+ password2 +"&email="+ email +"&regsubmit=yes";
		//alert(form_data);
		$.post(hostObj.url + "/zone/board/register.php", form_data,
			function(data){
				if(regis_mode != ""){
					window.location.href = referer;
				}
			 //alert("Data Loaded: " + data);
			//$("#result").text(data);
			});	*/
		
}

function update_profile(frm){
	
	var data = getFormData(frm);
	var URL = hostObj.url + "/user/script-update-profile.php";
	var div = "div_profile_report";
		ajaxLoad("post",URL,data,div);
}

function update_shipping(frm){
	
	var data = getFormData(frm);
	var URL = hostObj.url + "/user/script-update-shipping.php";
	var div = "div_shipping_report";
		ajaxLoad("post",URL,data,div);
}

function load_shipping(user_id){
	
	var URL = hostObj.url + "/user/script-load-shipping.php";
	var data = "user_id=" + user_id;
	var div = "div_shipping_report";
		ajaxLoad("post",URL,data,div);
	
}

function load_product(type,sub_cat_id,page,div_id){
		var URL = hostObj.url + "/product/script-load-product-list.php";
		var data = "type=" + type + "&product_sub_cat_id=" + sub_cat_id + "&page=" + page  + "&div_id=" + div_id;
		var div = "div_product_list_" + String(div_id);
			ajaxLoad("post",URL,data,div);
}

function load_comment(type,key_id,page,div_id){
		var URL = hostObj.url + "/user/script-load-comment.php";
		var data = "type=" + type + "&key_id=" + key_id + "&page=" + page  + "&div_id=" + div_id;
		var div = "div_comment_list_" + String(div_id);
			ajaxLoad("post",URL,data,div);
}

function login_focus(){
	document.getElementById("btn_login").style.borderColor = "red";
	document.getElementById("user_pass").style.borderColor = "red";
	document.getElementById("user_name").style.borderColor = "red";
	document.getElementById("user_name").select();
}

function order_step(step){
	for(var i=1; i<=4; i++){
		var img_el = String("img_step_" + i);
		var span_el = String("span_step_" + i);
		
		if(i == step){
			document.getElementById(img_el).src = hostObj.url + "/images/icon-16x16/next.gif";
			document.getElementById(span_el).className = "color_pink";
		}else{
			document.getElementById(img_el).src = hostObj.url + "/images/spacer.gif";
			document.getElementById(span_el).className = "color_grey";
		}
	}
		//animatedcollapse.show('div_order_step');
}

function check_init(order_mode){
	var URL = hostObj.url + "/product/script-check-init.php";
	var data = "order_mode=" + order_mode;
	var div = "";
		ajaxLoad("post",URL,data,div);
}

function check_cart(mode){
	var URL = hostObj.url + "/product/script-check-cart.php";
	var data = "mode=" + mode;
	var div = "div_order_report";
		ajaxLoad("post",URL,data,div);
}

function order_confirm(frm){	
	var data = getFormData(frm);
	var URL = hostObj.url + "/product/script-order-confirm.php";
	var div = "div_thank_report";
		ajaxLoad("post",URL,data,div);
		animatedcollapse.hide('div_order_step');
		//animatedcollapse.show('div_order_thank');
}

function load_invoice(order_id,div_id){
	
	var URL = hostObj.url + "/user/script-load-order-invoice.php";
	var data = "order_id=" + order_id;
	var div = div_id;
		ajaxLoad("post",URL,data,div);
	
}

function list_province(province_name){	
	var data = "province_name=" + province_name;
	var URL = hostObj.url + "/user/script-list-province.php";
	var div = "";
	
		ajaxLoad("post",URL,data,div);
}
function list_box(list_info,display,div_list){
	document.getElementById(div_list).innerHTML = list_info;	
	document.getElementById(div_list).style.display = display;
}
function hide_list(div_list){	
	document.getElementById(div_list).style.display = "none";	
}
function select_list_province(el){
	document.getElementById("shipping_address_province").value = el.innerHTML;	
	hide_list("div_list_box");
}

function focus_login(el,option,val){ // for username & password text show
	if(option == 1){ // on focus input
		el_val = document.getElementById(el).value;
		if(el_val == val){
			document.getElementById(el).value = "";
		}
	}
	if(option == 0){ // on blur input
		el_val = document.getElementById(el).value;
		if(el_val == ""){
			document.getElementById(el).value = val;
		}
	}
	if(option == 3){ // on focus input password
		//document.getElementById(el).type = "password" ;
		el_val = document.getElementById(el).value;
		if(el_val == val){			
			document.getElementById(el).value = "";
		}
	}
}

function focus_alert(el){ // for check form false
	el.className = "input_text_alert";
}

function ms_over_list(el){
	el.className = "list_2";
	el.style.cursor = "pointer";
}
function ms_out_list(el){
	el.className = "list_1";
}

$(document).ready(function(){
				   
		$("input[type=text],input[type=password],textarea,select").focus(function () {
			$(this).addClass('input_text_focus');
		});
		$("input[type=text],input[type=password],textarea,select").blur(function () {
			$(this).removeClass('input_text_focus');
			$(this).removeClass('input_text_alert');
		});
		
		
		$(".list_1").hover(
			function () {
				$(this).removeClass('list_1');
				$(this).addClass('list_2');
			}, 
			function () {
				$(this).removeClass('list_2');
				$(this).addClass("list_1").remove();
			}
		);
});