/********************************************************************************************
	Author 		: V V VIJESH
	Date		: 09-Nov-2010
	Purpose		: Shopping cart related functionalites
	Updated By	: 
	Date		: 
********************************************************************************************/
var xml_http_cart;
var id;
function addto_cart(product_id)
{
	if (product_id.length == 0)
	{ 
		return;
	}
	if(document.getElementById('varient'))
		product_variant_id = document.getElementById('varient').value;
	else
		product_variant_id = 0;
	if(document.getElementById('colorPalette'))
	{
		data = document.getElementById('colorPalette').value;
		dataArr = data.split("~");
		data = dataArr[0];
	}
	else
		data = 0;
	id = product_variant_id;
	var quantity = 1;
	
	xml_http_cart = getHTTPObject();
	if (xml_http_cart==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="addto_cart.php";

	url = url + "?product_id=" + product_id + "&product_variant_id=" + product_variant_id + "&quantity=" + quantity + "&colorId="+ data  ;
	//alert(url);
	url = url+"&stid="+Math.random();
	xml_http_cart.onreadystatechange	= addto_cart_onready;
	xml_http_cart.open("GET",url,true);
	xml_http_cart.send(null);
} 
	
function addto_cart_onready()
{ 
	if (xml_http_cart.readyState == 4 || xml_http_cart.readyState == "complete")
	{
		//alert(xml_http_cart.responseText );
		if(xml_http_cart.responseText != '')
		{
			document.getElementById('model_message').innerHTML 	= xml_http_cart.responseText;
			//document.getElementById('quantity_' + id ).value	= '';
			update_shoppingcart_item();
			update_shoppingcart_total();
			Popup.showModal('modal',null,null,{'screenColor':'#99ff99','screenOpacity':.6});
		}
	} 
}

function addWishList(product_id)
{
	if (product_id.length == 0)
	{ 
		return;
	}
	if(document.getElementById('varient'))
		product_variant_id = document.getElementById('varient').value;
	else
		product_variant_id = 0;
	if(document.getElementById('colorPalette'))
	{
		data = document.getElementById('colorPalette').value;
		dataArr = data.split("~");
		data = dataArr[0];
	}
	else
		data = 0;
	id = product_variant_id;
	var quantity = 1;
	
	xml_http_cart = getHTTPObject();
	if (xml_http_cart==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	var url="addWishList.php";

	url = url + "?product_id=" + product_id + "&product_variant_id=" + product_variant_id + "&quantity=" + quantity + "&colorId="+ data  ;
	//alert(url);
	url = url+"&stid="+Math.random();
	xml_http_cart.onreadystatechange	= addWishList_onready;
	xml_http_cart.open("GET",url,true);
	xml_http_cart.send(null);
} 
	
function addWishList_onready()
{ 
	if (xml_http_cart.readyState == 4 || xml_http_cart.readyState == "complete")
	{
		//alert(xml_http_cart.responseText );
		if(xml_http_cart.responseText != '')
		{
			var OriginalString = xml_http_cart.responseText;
			document.getElementById('model_message').innerHTML 	= xml_http_cart.responseText;
			var StrippedString = OriginalString.replace(/(<([^>]+)>)/ig,"");//htstring.replace(/(<([^>]+)>)/ig,""); 
			alert(xml_http_cart.responseText);
			location.href = 'wishlist-new.php';
			//Popup.showModal('modal',null,null,{'screenColor':'#99ff99','screenOpacity':.6});
		}
	} 
}


function quick_order(action)
{
	id = action;
	var total_item = document.getElementById('shoppingcart_total_item' ).value;
	var product_id = document.getElementById('product' ).value;
	
	if(total_item > 0 && action == 'checkout' && product_id == 0)
	{
		location.href='shipping-details.php';
		return;	
	}
	
	
	if (product_id == 0)
	{ 
		alert("Please select Product.");
		document.getElementById('product').focus();
		return false;	
	}
	var quantity = document.getElementById('quantity' ).value;
	if(quantity == "")
	{
		alert("Please enter Quantity.");
		document.getElementById('quantity').focus();
		return false;	
	}
	
	var re = /^[0-9]*$/;

	if (document.getElementById('quantity').length != 0)
	{
		if (!re.test(document.getElementById('quantity').value))
		{
			alert ("Quantity must be numeric!");
			document.getElementById('quantity').focus();
			return false;	
		}
	}	
	
	xml_http_cart = getHTTPObject();
	if (xml_http_cart==null)
	{
		alert ("Browser does not support HTTP Request");
		return false;
	}
	
	var url="quick_order.php";

	url = url + "?product_id=" + product_id + "&quantity=" + quantity;
	//alert(url);
	url = url+"&stid="+Math.random();
	xml_http_cart.onreadystatechange	= quick_order_onready;
	xml_http_cart.open("GET",url,true);
	xml_http_cart.send(null);
} 
	
function quick_order_onready()
{ 
	if (xml_http_cart.readyState == 4 || xml_http_cart.readyState == "complete")
	{
		if(xml_http_cart.responseText != '')
		{
			if(id == 'add')
			{
				document.getElementById('model_message').innerHTML 	= xml_http_cart.responseText;
				document.getElementById('quantity').value	= '';
				update_shoppingcart_item();
				update_shoppingcart_total();
				Popup.showModal('modal',null,null,{'screenColor':'#99ff99','screenOpacity':.6});
			}
			else if(id == 'checkout')
			{
				var response = xml_http_cart.responseText;
				document.getElementById('quick_order_model_message').innerHTML 	= xml_http_cart.responseText;
				update_shoppingcart_item();
				update_shoppingcart_total();
				if(response.search('already') > 0)
				{
					document.getElementById('quick_order_model_button').onclick = function() { Popup.hide('quick_order_modal'); }
					document.getElementById('quick_order_model_button').value = "OK";
					Popup.showModal('quick_order_modal',null,null,{'screenColor':'#99ff99','screenOpacity':.6});
				}
				else
				{
					location.href='shipping-details.php';
				}
			}
		}
	} 
}

var xml_http_cart_item;
function update_shoppingcart_item()
{
	xml_http_cart_item = getHTTPObject();
	if (xml_http_cart_item==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	var url="update_shoppingcart.php";

	url = url + "?action=item";
	//alert(url);
	url = url+"&stid="+Math.random();
	xml_http_cart_item.onreadystatechange	= update_shoppingcart_item_onready;
	xml_http_cart_item.open("GET",url,true);
	xml_http_cart_item.send(null);
} 

function update_shoppingcart_item_onready()
{ 
	if (xml_http_cart_item.readyState == 4 || xml_http_cart_item.readyState == "complete")
	{
		if(xml_http_cart_item.responseText != '')
		{
			document.getElementById('shoppingcart_total_item').value	= xml_http_cart_item.responseText;
			if(document.getElementById('top_menu_item'))
				document.getElementById('top_menu_item').innerHTML			= xml_http_cart_item.responseText;
		}
	} 
}
var xml_http_cart_total;
function update_shoppingcart_total()
{
	xml_http_cart_total = getHTTPObject();
	if (xml_http_cart_total==null)
	{
		alert ("Browser does not support HTTP Request");
		return;
	}
	
	var url="update_shoppingcart.php";

	url = url + "?action=total";
	//alert(url);
	url = url+"&stid="+Math.random();
	xml_http_cart_total.onreadystatechange	= update_shoppingcart_total_onready;
	xml_http_cart_total.open("GET",url,true);
	xml_http_cart_total.send(null);
} 
function update_shoppingcart_total_onready()
{ 
	if (xml_http_cart_total.readyState == 4 || xml_http_cart_total.readyState == "complete")
	{
		if(xml_http_cart_total.responseText != '' && document.getElementById('top_menu_total'))
		{
			document.getElementById('top_menu_total').innerHTML 	= xml_http_cart_total.responseText;
		}
	} 
}
