// JavaScript Document
function select_color(select_id,id)
{
	
	var value=document.getElementById(id);
	value.selected=true;

}
function switch_image(imgid,imgsrc)
{
	var img=document.getElementById(imgid);
	img.src=imgsrc;
}

function validate_order()
{
	var quantity=document.getElementById('quantity').value;
	var color=document.getElementById('select_color').value;
	var size=document.getElementById('size').value;
	
	if (quantity=="")
	{
		alert('Please specify the quantity of your order.');
		return false;
	}
	else if (color=="")
	{
		alert('Please select a color');
		return false;
	}
	else if (size=='')
	{
		alert('Please specify the size of your order.');
		return false;
	}
	else
	{
		return true;	
	}
}
/*
function add_to_cart(sku)
{
	var url="cart_handler.php"; //  should be https://www.hautegeneration.com/cart_handler.php for livesite
	var qty=document.getElementById("quantity").value;
	var color=document.getElementById("select_color").value;
	var size=document.getElementById("size").value;
	
	url=url+'?act=add';
	url=url+'&sku='+sku;
	url=url+'&qty='+qty;
	url=url+'&color='+color;
	url=url+'&size='+size;
	window.open(url, "p2_target");	
	
	
}
*/

function add_to_cart(sku)
{
	var http = new getXMLObject(); 
	var url = "addtocart.php";
	var qty=document.getElementById("quantity").value;
	var color=document.getElementById("select_color").value;
	var size=document.getElementById("size").value;
	
	url=url+'?token='+Math.random();
	url=url+'&sku='+sku;
	url=url+'&qty='+qty;
	url=url+'&color='+color;
	url=url+'&size='+size;
	url=url+'&act=add';
	
	if(http)
	{
		http.onreadystatechange = function(){
			if(http.readyState == 4)
			{
				if(http.status==200)
				{
					if(trim(http.responseText)=='TRUE')
					{
						window.location='http://www.hautegeneration.com/mybag.php';
						//window.location='mybag.php';
					}
					else 
					{
						alert('Error : An error occurred while processing your request.');
					}
				}
				
			}
		}
		http.open("GET",url,true);
		http.send(null);
	}
}

function get_sub(id,value) {
	var url ="cart_handler.php?update_qty="+value+"&key="+id;
  	window.open(url, "cart_target");
}

function addtowishlist(sku,size,color) 
{
	var url ="wishlist_handler.php";	
	url=url+'?act=addtowishlist&size='+size+'&color='+color+'&sku='+sku;
  	window.open(url, "p2_target");
}

function del_wish_item(sku) 
{
	var url ="wishlist_handler.php?act=del_wish&sku="+sku;

  	window.open(url, "wish_iframe");
}

function share_wish() 
{alert('sad');

}

function replace_image(div,img1,img2)
{
	
	var img=document.getElementById(div); 
	
	if (img.src.search(img1)==-1){
		img2=img1;
	}

	if (img.src.search(img2)!=-1){ img.src=img1; }
	else { img.src=img2; }
}




function deleteCartItem(pid)
{
	var act = 'delete';
	var url = 'cart_handler.php?act='+act;
	var url = url+'&id='+pid;
	
	window.open(url,"cart_iframe");
}

function updateSubTotal()
{
	
	
	var shipping =document.getElementById("shipping").innerHTML*1;	
	var subtotal = document.getElementById("sub_total").innerHTML*1;
	var order_total = document.getElementById("order_total");
	var zipcode = document.getElementById('zipcode').value;
	
	if((zipcode >= 90001 ) && (zipcode <=96162)) {
		document.getElementById('tax').innerHTML = (subtotal*0.0925).toFixed(2);
		document.getElementById('tax_h').value = (subtotal*0.0925).toFixed(2);
	}
	else
	{
		document.getElementById('tax').innerHTML = '0.00';
		document.getElementById('tax_h').value = '0.00';
	}
	
	var tax = document.getElementById("tax").innerHTML*1;
	var total=shipping+tax+subtotal;
	
	order_total.innerHTML=total.toFixed(2);
	document.getElementById('order_total_h').value=total.toFixed(2);
	
	
}



function validateCheckout()
{
	var shippingMethod=document.getElementById("shippingMethod").value;	
	var zipcode=document.getElementById("zipcode").value;
	var order_total=document.getElementById("order_total_h").value;
	var pp = document.getElementById("pcodeProcess").value;
	var zp = document.getElementById("zipProcess").value;
	var sp = document.getElementById("shipProcess").value;
	
	if(document.getElementById("sub_total").innerHTML=='0.00' || document.getElementById("cart_count").value=='0.00')
	{ 
		alert('You cannot checkout with empty cart.'); 
		return false; 
	}
	else if(shippingMethod=='')
	{ 
		alert('Please select a shipping method.'); 
		return false; 
	}
	else if(document.getElementById("order_total").innerHTML=='0.00')
	{
		alert('You cannot checkout with empty cart.'); 
		return false; 
	}
	else if(zipcode=='')
	{ 
		alert('Please enter a zipcode.'); 
		return false;
	}
	else if(order_total=='0.00')
	{ 
		alert('Your shopping cart is empty.');
		return false; 
	}
	else if(pp == 1 || zp==1 || sp == 1 )
	{ 
		alert('Please wait for current process to finish.'); 
		return false; 
	}
	else
	{	
		return true;		
	}
}

function showcheckbtn()
{
	document.getElementById("checkbtn").innerHTML='<input type="image" src="images/shop-btn-proceedchk.jpg" name="checkout" width="225" height="27" border="0" onclick="return validateCheckout();" />';	
}
function hidecheckbtn()
{
	document.getElementById("checkbtn").innerHTML='<i>..Please Wait</i>';
}

function showsecode()
{
	var sec = document.getElementById("seccodeimage");
	if(sec.style.display=='none'){ sec.style.display='block';}
	else{sec.style.display='none';}
}

//---------- TRIM FUNCTION ---------------------
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
//------------------------------------------------^
function process(id,val)
{
	document.getElementById(id).value=val;	
}

function getTax()
{	
	process("zipProcess",1);
	var subtotal = document.getElementById("sub_total_h").value*1;
	var zipcode = document.getElementById("zipcode").value;
	if((zipcode >= 90001 ) && (zipcode <=96162)) 
	{
		document.getElementById('tax').innerHTML = (subtotal*0.0925).toFixed(2);
		document.getElementById('tax_h').value = (subtotal*0.0925).toFixed(2);
	}
	else
	{
		document.getElementById('tax').innerHTML = '0.00';
		document.getElementById('tax_h').value = '0.00';
	}	
	process("zipProcess",0);
	getOrderTotal();
}

//AJAX FUNCTIONS
//XML OBJECT
function getXMLObject(){
   var xmlHttp = false;
   try {
     xmlHttp = new ActiveXObject("Msxml2.XMLHTTP")  // For Old Microsoft Browsers
   }
   catch (e) {
     try {
       xmlHttp = new ActiveXObject("Microsoft.XMLHTTP")  // For Microsoft IE 6.0+
     }
     catch (e2) {
       xmlHttp = false   // No Browser accepts the XMLHTTP Object then false
     }
   }
   if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
     xmlHttp = new XMLHttpRequest();        //For Mozilla, Opera Browsers
   }
   return xmlHttp;  // Mandatory Statement returning the ajax object created
}


function update_quantity(qty,id)
{
	var http = new getXMLObject();
	var url = 'cart_handler.php';
		url = url + '?act=update_qty';
		url = url + '&qty='+qty;
		url = url + '&id='+id;
	if(http)
	{
		http.onreadystatechange = function(){
			if(http.readyState == 1)
			{
				
			}
			if(http.readyState == 4)
			{
				
				if(http.status==200)
				{
					if(trim(http.responseText)=='true')
					{
						window.location='mybag.php';	
					}
					else 
					{
						alert(http.responseText);
					}
				}
				
			}
		}
		http.open("GET",url,true);
		http.send(null);
	}
}

function getPromoCode()
{   
	get_subtotal();
	var http = new getXMLObject();	
	var subtotal = document.getElementById("sub_total_h").value;	
	var code = document.getElementById("promoCode").value;
	var url = 'promo_code.php';
		url = url + '?code='+code;
		url = url + '&subtotal='+subtotal;
		url = url + '&sid='+Math.random();
	
		if(http && code != '')
		{
			http.onreadystatechange = function(){
				if(http.readyState == 1)
				{
					process("pcodeProcess",1);
				}
				if(http.readyState == 4)
				{   
					process("pcodeProcess",0);
					if(http.status==200)
					{	
						document.getElementById("sub_total").innerHTML = http.responseText;
						document.getElementById("sub_total_h").value = http.responseText;
						getOrderTotal();	
						show_discount(code);
						getTax();
					}	
				}
			}
			http.open("GET",url,true);
			http.send(null);
		}
	
}

/*function get_subtotal()
{
	var subtotal=0;
	var subt=0;
	var tcount = document.getElementsByTagName("span").length;
	for(x=0; x<tcount;x++)
	{
		try
		{ 
			if(document.getElementsByTagName("span")[x].className == 'item_subtotal') {
				subt = document.getElementsByTagName("span")[x].innerHTML*1;
			}
			else{ subt = 0; }
		}
		catch(e)
		{
			subt=0;	
		}
		subtotal=subtotal + (subt*1);
		
	}
	subtotal = subtotal*1;
	
	document.getElementById("sub_total").innerHTML = subtotal.toFixed(2);
	document.getElementById("sub_total_h").value = subtotal.toFixed(2); 	
}

function get_subtotal()
{
	var subtotal=0;
	var subt=0;
	var tcount = document.getElementsByClassName("subtotal").length;
	for(x=0; x<tcount;x++)
	{
		try
		{ 
			subt=document.getElementsByClassName("subtotal")[x].innerHTML;
		}
		catch(e)
		{
			subt=0;	
		}
		subtotal=subtotal + (subt*1);
	}
	subtotal = subtotal*1;
	
	document.getElementById("sub_total").innerHTML = subtotal.toFixed(2);
	document.getElementById("sub_total_h").value = subtotal.toFixed(2); 	
}*/

function updateShipping()
{
	var http = new getXMLObject();
	var method = document.getElementById("shippingMethod").value;
	var subtotal=document.getElementById("sub_total").innerHTML;	
	var url='shipping.php';
		url=url+'?method='+method;
		url=url+'&subTotal='+subtotal;
	if(http)
	{
		http.onreadystatechange = function(){
			if(http.readyState == 1)
			{
				process("shipProcess",1);
			}
			if(http.readyState == 4)
			{   
				if(http.status==200)
				{
					document.getElementById("shipping").innerHTML = http.responseText;
					document.getElementById("shipping_h").value = http.responseText;
					getOrderTotal();
					process("shipProcess",0);
				}
				
			}
		}
		http.open("GET",url,true);
		http.send(null);
	}
		
}
function show_discount(code)
{
	var http = new getXMLObject();
	var url='promo_code_ext.php';
		url=url+'?code='+code;
		url=url+'&sid='+Math.random();
	if(trim(code)!='')
	{
		if(http)
		{
			http.onreadystatechange = function(){
				if(http.readyState == 1)
				{
					return false;
				}
				if(http.readyState == 4)
				{   
					if(http.status==200)
					{
						document.getElementById("dc").innerHTML = http.responseText;
						return false;
					}
					
				}
			}
			http.open("GET",url,true);
			http.send(null);
		}
	}
	else {document.getElementById("dc").innerHTML = '';}
}
function getOrderTotal()
{
	 var tax = document.getElementById("tax").innerHTML*1;
	 var shipping = document.getElementById("shipping").innerHTML*1;
	 var sub_total = document.getElementById("sub_total").innerHTML*1;
	 var ordertotal=tax+shipping+sub_total;
	 
	 document.getElementById("order_total_h").value=ordertotal.toFixed(2);
	 document.getElementById("order_total").innerHTML=ordertotal.toFixed(2);
}

function addEventHandler(oNode, sEvt, fFunc, bCaptures){
	if (typeof (window.attachEvent) != 'undefined')
		oNode.attachEvent('on' + sEvt, fFunc);
	else
		oNode.addEventListener(sEvt, fFunc, bCaptures);
}



function checkInv(id,sku)
{
	var http = new getXMLObject();
	var c = document.getElementById("select_color").value;
	var s = document.getElementById("size").value;
	var q = document.getElementById("quantity").value;
	var url	= 'stock_checker_handler.php?id='+id+'&qty='+q+'&color='+c;
		url = url+'&sid='+Math.random();
		url = url+'&stockinfo='+Math.random();

	if(http)
	{
		http.onreadystatechange = function(){
			if(http.readyState == 1)
			{
				document.getElementById("stk_msg_out").innerHTML ='<i>Please Wait..</i>';
			}
			if(http.readyState == 4)
			{   
				document.getElementById("stk_msg_out").innerHTML ='';
				if(http.status==200)
				{
					var ret = trim(http.responseText);
					if(ret!='false'){alert(ret);}
					else{
						alert(ret);
						add_to_cart(sku);
					}
				}
				
			}
		}
		http.open("POST",url,true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.send("size="+s);
	}
}

function checkInv_cart(pid,id,c,s,q)
{
	var http = new getXMLObject();
	var url	= 'stock_checker_handler.php?id='+pid+'&qty='+q+'&color='+c;
		url = url+'&sid='+Math.random();
		url = url+'&stockinfo='+Math.random();

	if(http)
	{
		http.onreadystatechange = function(){
			if(http.readyState == 1)
			{
				process("pcodeProcess",1);
			}
			if(http.readyState == 4)
			{   
				process("pcodeProcess",0);
				if(http.status==200)
				{
					var ret = trim(http.responseText);
					if(ret!='false'){
						alert(ret);
						document.getElementById("qty"+id).value = document.getElementById("qty2"+id).value;
					}
					else{
						//alert(ret);
						update_quantity(q,id);
					}
				}
				
			}
		}
		http.open("POST",url,true);
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.send("size="+s);
	}
}