//Gets the browser specific XmlHttpRequest Object

var other_value = 999999;

function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest(); //Not IE
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP"); //IE
	} else {
		//Display your error message here. 
		//and inform the user they might want to upgrade
		//their browser.
		alert("Your browser doesn't support the XmlHttpRequest object.  Better upgrade to Firefox.");
	}
}	


//Get our browser specific XmlHttpRequest object.
var receiveReq = getXmlHttpRequestObject();		



//function for checking the email on get_contact_detail page
function control_home_page_image(val) {
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", 'ajax-php/home_page_imgl.php', true);
		receiveReq.onreadystatechange = handle_control_home_page_image; 
		receiveReq.send(null);
	}			
}

function handle_control_home_page_image() {
	if (receiveReq.readyState == 4) {
		if(receiveReq.responseText != "")
		{
			document.getElementById('home_image_div').innerHTML = receiveReq.responseText;
		}
	}
}


//function for checking the email on get_contact_detail page
function ajax_add_to_cart(id)
{
			
	//alert("khushwant");
	//document.getElementById("id_progress").style.display = "";
	var error = "no";
	var msg = "Following error occure in processing the order\n\n";
	var product_id = id;
	var quantity = document.getElementById("qty" + id).value;
	
	//alert(product_id);
	//var size_ids = document.getElementById("size_ids").value;
	//var border_color_ids = document.getElementById("border_color_ids").value;
	//var background_color_ids = document.getElementById("background_color_ids").value;
	//var width = document.getElementById("width").value;
	//var height = document.getElementById("height").value;
	//var shape_id = document.getElementById("shape_id").value;
	//var border_color = document.getElementById("border_color").value;
	//var ground_color = document.getElementById("ground_color").value;
	
	/*if(size_ids == "")
	{
		error = "yes";
		msg += "Please select the Size\n";
	}*/
	
	
	/*if(border_color_ids == "" && background_color_ids != other_value)
	{
		error = "yes";
		msg += "Please select the Border Color\n";
	}*/	
	
	/*if(background_color_ids == "" && border_color_ids != other_value)
	{
		error = "yes";
		msg += "Please select the Ground Color\n";
	}*/
	
	/*if(size_ids == other_value)
	{
		if(width == "")
		{
			error = "yes";
			msg += "Please enter your custom Width\n";
		}		
		if(height == "")
		{
			error = "yes";
			msg += "Please enter your custom Height\n";
		}		
	}*/
	
	/*if(border_color_ids == other_value || background_color_ids == other_value)
	{
		if(border_color == "")
		{
			error = "yes";
			msg += "Please enter your custom Border Color\n";
		}		
		if(ground_color == "")
		{
			error = "yes";
			msg += "Please enter your custom Ground Color\n";
		}		
	}	*/
	
	if(quantity == 0)
	{
		error = "yes";
		msg += "Please Enter the Quantity\n";
	}

	if(error == "yes")
	{
		alert(msg);
		return false;
	}
	
	//confirm to order with the specified quantity
	var what = confirm("Are you sure to add " + quantity + " of this Item to your Cart?");
	if(what == false)
	{
		return false;
	}

	var pars = "quantity=" + quantity + "&product_id=" + product_id;
	//document.getElementById("id_progress").innerHTML = "Loading ...";
	
	
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", 'ajax-php/add_to_cart.php?' + pars, true);
		receiveReq.onreadystatechange = handle_ajax_add_to_cart; 
		receiveReq.send(null);
	}
	
	return true;
	
}


function handle_ajax_add_to_cart() {
	if (receiveReq.readyState == 4) {
		if(receiveReq.responseText != "")
		{
			//alert(receiveReq.responseText);
			/*var category_id = document.getElementById("category_id").value;
			
			//document.getElementById("size_ids").value = "";
			//document.getElementById("border_color_ids").value = "";
			//document.getElementById("background_color_ids").value = "";
			document.getElementById("border_color_ids").disabled = false;
			document.getElementById("background_color_ids").disabled = false;
			document.getElementById("width").value = "";
			document.getElementById("height").value = "";
			document.getElementById("shape_id").value = "";
			document.getElementById("border_color").value = "";
			document.getElementById("ground_color").value = "";
			
			document.getElementById("id_custom_size").style.display = "none";
			document.getElementById("id_custom_color").style.display = "none";

			document.getElementById("id_progress").innerHTML = "<br /><br /><br /><br />Item has been added to your Shopping Cart";
			document.getElementById("id_progress").innerHTML += "<br>If you want to modify ";
			document.getElementById("id_progress").innerHTML += "<a href='cart.php?cat_id=" + category_id + "'>Click Here</a><br /> OR ";
			document.getElementById("id_progress").innerHTML += "<a href='product.php?cat_id=" + category_id + "'>Continue Shopping</a><br /><br /><br /><br />";
			
			
			document.getElementById("palate_body").style.display = "none";*/
						
			//document.getElementById('id_product').innerHTML = receiveReq.responseText;
			alert("Item is successfully added to your Cart.");
		}
	}
	return true;
}









//function for checking the email on get_contact_detail page
function ajax_remove_cart_items()
{
	
	var count_product = document.getElementById("count_product").value;
	var product_id_str = "";
	var total_price_id = 0;
	
	
	for(i=0;i<count_product;i++)
	{
		if(document.getElementById("chk" + i).checked == true)
		{
			//product_id_str += document.getElementById("chk" + i).value + ",";
			product_id_str += i + ",";
			total_price_id += document.getElementById("qty" + i).value * document.getElementById("price" + i).value;
			//document.getElementById("tr" + i).style.display = "none";
			
			//uncheck the removed field
			document.getElementById("chk" + i).checked = false;
			
		}
	}
	
	if(product_id_str == "")
	{
		alert("Please select atleast one product to remove from Cart.");
		return false;
	}
	
	

	
	var str_len = product_id_str.length;
	product_id_str = product_id_str.substr(0, str_len - 1); 

	//document.getElementById("total_price_id").innerHTML = document.getElementById("total_price_id").innerHTML - total_price_id;
	
	/*var target = "id_updated";
	var url = 'ajax-php/remove_cart.php';
	
	//alert(pars);
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});*/
	
	var pars = 'product_id_str='+product_id_str; 
	
	//alert(pars);
	//return false;
	
	/*if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", 'ajax-php/remove_cart.php?' + pars, true);
		receiveReq.onreadystatechange = handle_ajax_remove_cart_items; 
		receiveReq.send(null);
	}*/
	
	window.location = "cart.php?mode=remove&" + pars;

}

function handle_ajax_remove_cart_items() {
	if (receiveReq.readyState == 4) {
		if(receiveReq.responseText != "")
		{
			document.getElementById("id_updated").innerHTML = receiveReq.responseText;
		}
	}
}



function ajax_update_cart_items()
{
	//document.getElementById("id_updated").innerHTML = '<img src="images/ajax-loader.gif" border="0" /> <font color="#000000">Loading...</font>';

	var count_product = document.getElementById("count_product").value;
	var product_qty_str = "";
	var total_price_id = 0;
//alert(count_product);
	
	for(i=0;i<count_product;i++)
	{
		if(document.getElementById("tr" + i).style.display != "none")
		{
			if(i < (count_product-1))
			{
				product_qty_str += document.getElementById("qty" + i).value + ",";
				//document.getElementById("total_id" + i).innerHTML = document.getElementById("qty" + i).value * document.getElementById("price" + i).value;
				total_price_id += document.getElementById("qty" + i).value * document.getElementById("price" + i).value;
			}
			else
			{
				product_qty_str += document.getElementById("qty" + i).value;
				//document.getElementById("total_id" + i).innerHTML = document.getElementById("qty" + i).value * document.getElementById("price" + i).value;
				total_price_id += document.getElementById("qty" + i).value * document.getElementById("price" + i).value;
			}
		}
	}
	
	//document.getElementById("total_price_id").innerHTML = total_price_id;
	
	/*var target = "id_updated";
	var url = 'ajax-php/update_cart.php';
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});*/
	
	var pars = 'product_qty_str='+product_qty_str; 
	/*if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", 'ajax-php/update_cart.php?' + pars, true);
		receiveReq.onreadystatechange = handle_ajax_update_cart_items; 
		receiveReq.send(null);
	}*/
	//alert(pars);
	window.location = "cart.php?mode=update&" + pars;
}

function handle_ajax_update_cart_items() {
	//alert(receiveReq.responseText);
	if (receiveReq.readyState == 4) {
		if(receiveReq.responseText != "")
		{
			document.getElementById("id_updated").innerHTML = receiveReq.responseText;
		}
	}
}

//function for product page
/*function ajax_show_product(index,cat_id)
{
	alert(index + " " + cat_id);
	var target = "id_product";
	var url = 'product.inc.php';
	//document.getElementById(target).innerHTML = '';
	var pars = 'index=' + index + "&cat_id=" + cat_id; 
	alert(pars);
	var myAjax = new Ajax.Updater(target, url, {method: 'get', parameters: pars});
}*/


function ajax_show_product(index,cat_id) {
	//document.getElementById("id_tab").innerHTML = "Loading ...";
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", 'product_tab.inc.php?index=' + index + "&cat_id=" + cat_id, true);
		receiveReq.onreadystatechange = handle_ajax_show_product; 
		receiveReq.send(null);
	}			
}

function handle_ajax_show_product() {
	if (receiveReq.readyState == 4) {
		if(receiveReq.responseText != "")
		{
			//alert(receiveReq.responseText);
			document.getElementById('id_tab').innerHTML = receiveReq.responseText;
		}
	}
}


function ajax_call_product(product_id) {
	//document.getElementById("id_product").innerHTML = "Loading ...";
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", 'product_call.inc.php?product_id=' + product_id, true);
		receiveReq.onreadystatechange = handle_ajax_call_product; 
		receiveReq.send(null);
	}			
}

function handle_ajax_call_product() {
	if (receiveReq.readyState == 4) {
		if(receiveReq.responseText != "")
		{
			//alert(receiveReq.responseText);
			document.getElementById('id_product').innerHTML = receiveReq.responseText;
		}
	}
}


function ajax_show_demo_product(index) {
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", 'demo_catalog_tab.inc.php?index=' + index, true);
		receiveReq.onreadystatechange = handle_ajax_show_demo_product; 
		receiveReq.send(null);
	}			
}

function handle_ajax_show_demo_product() {
	if (receiveReq.readyState == 4) {
		if(receiveReq.responseText != "")
		{
			document.getElementById('id_tab').innerHTML = receiveReq.responseText;
		}
	}
}

function ajax_call_demo_product(product_id) {
	//alert(product_id);
	if (receiveReq.readyState == 4 || receiveReq.readyState == 0) {
		receiveReq.open("GET", 'demo_catalog_call.inc.php?product_id=' + product_id, true);
		receiveReq.onreadystatechange = handle_ajax_call_demo_product; 
		receiveReq.send(null);
	}			
}

function handle_ajax_call_demo_product() {
	if (receiveReq.readyState == 4) {
		if(receiveReq.responseText != "")
		{	
			document.getElementById('id_product').innerHTML = receiveReq.responseText;
		}
	}
}
