/*-----------------------------------------------------------------------
Created by: Rajendra
Created date: 16 Oct 2008
File description: Shopping Cart Javascript Functions to Add/Update/Remove product from shopping cart
Special instructions-notes:Java script
Tables used: none
Stored procedures: none
Triggers used: none
-----------------------------------------------------------------------*/
function RemoveFromCart( action, item_id )
{
	var tmp_rootpath_div = tmp_rootpath.replace("registration/","");
	var conf = confirm( "Do you really want to remove an item?" );
	if( conf )
	{
		document.frmcart1.hidn_item_count.value = item_id;
		document.frmcart1.hidn_cart_action.value = 'REMOVE';
		document.frmcart1.action = tmp_rootpath+"shopping_cart.php";
		document.frmcart1.submit();
	}
}

function UpdateFromCart( action, item_id )
{
	var tmp_rootpath_div = tmp_rootpath.replace( "registration/", "" );
	var conf = confirm( "Do you really want to update item quantity?" );
	if( conf )
	{
		document.frmcart1.hidn_item_count.value = item_id;
		document.frmcart1.hidn_cart_action.value = 'UPDATE';
		document.frmcart1.action = tmp_rootpath+"shopping_cart.php";
		document.frmcart1.submit();
	}
}

function applyPromo(url)
{
	if( url == 'shopping_cart' )
	{
		var page_url = 'shopping_cart.php';
	}
	else if( url == 'shopping_cart_prescription' )
	{
		var page_url = 'shopping_cart_prescription.php';
	}
	else if( url == 'shopping_cart_auto_delivery' )
	{
		var page_url = 'shopping_cart_auto_delivery.php';
	}
	
	if(isWhitespace(document.getElementById('pcode').value))
	{
		alert("Enter Promotion Code");		
	}
	else
	{
		document.frm_data.hid_promo_code.value = document.getElementById('pcode').value;
		document.frm_data.action = tmp_rootpath + page_url;
		document.frm_data.submit();
	}	
}

function removePromo(url)
{
	if( url == 'shopping_cart' )
	{
		var page_url = 'shopping_cart.php';
	}
	else if( url == 'shopping_cart_prescription' )
	{
		var page_url = 'shopping_cart_prescription.php';
	}
	else if( url == 'shopping_cart_auto_delivery' )
	{
		var page_url = 'shopping_cart_auto_delivery.php';
	}
	document.frm_data.hid_promo_apply.value = '-1';
	document.frm_data.action = tmp_rootpath + page_url;
	document.frm_data.submit();
}

function empty_basket(url)
{
	if( url == 'shopping_cart' )
	{
		var page_url = 'shopping_cart.php';
	}
	else if( url == 'shopping_cart_prescription' )
	{
		var page_url = 'shopping_cart_prescription.php';
	}
	else if( url == 'shopping_cart_auto_delivery' )
	{
		var page_url = 'shopping_cart_auto_delivery.php';
	}
	
	document.frm_data.hid_custom.value = 'empty';
	document.frm_data.action = tmp_rootpath + page_url;
	document.frm_data.submit();
}

function removeAutoDelivery(id)
{
	document.frm_data.hid_custom.value = 'remove_ad';
	document.frm_data.hid_custom_1.value = id;
	document.frm_data.action = "shopping_cart_auto_delivery.php";
	document.frm_data.submit();
}
// Code For Get MOUSE POSITION
var IE = document.all?true:false
if (!IE){ document.captureEvents(Event.MOUSEMOVE);}
document.onmouseup = getMouseXY;
function getMouseXY(e) {
	var tempX = 0;
	var tempY = 0;	
	if (IE) { 
		if(document.documentElement){
			tempX = event.clientX + document.documentElement.scrollLeft;
			tempY = event.clientY + document.documentElement.scrollTop;
		}else{
			tempX = event.clientX + document.body.scrollLeft;
			tempY = event.clientY + document.body.scrollTop;
		}
	} else { 
		tempX = e.pageX
		tempY = e.pageY
	}  
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}
	
	var mousePosition = "0,0";
	if(tempX>0 && tempY>0){
		mousePosition = tempX+","+tempY;		
	} 	
	if(document.getElementById("MouseXY")){		
		document.getElementById("MouseXY").value = mousePosition;
	}	
	return true
}
function doCart(action, product_id, div_id, y, w, h, url )
{	
var Left=0;
	if(document.getElementById("MouseXY")){		
		var mousePosition = document.getElementById("MouseXY").value;
	}else{		
		var mousePosition = "";	
	}	
	
	
	if(mousePosition!="")
	{
		var tmpXYArray = mousePosition.split(',');		
		var mouseXpos = parseInt(tmpXYArray[0]);
		var mouseYpos = parseInt(tmpXYArray[1]);
		if(mouseXpos>0 && mouseYpos>0){				
			Left = parseInt(mouseXpos)-100;
			y	 = parseInt(mouseYpos)-500;	
		}	
	}
	//alert("X="+Left+"\nY="+y); return false;
	//alert(product_id);
	Left = 700;
	y = 10;
	var tmp_rootpath_div = tmp_rootpath.replace("registration/","");
	var xmlHttp;
	if( document.getElementById('qty_sel') )
		var qty_sel = document.getElementById('qty_sel').value;
	else	
		var qty_sel = '';

	xmlHttp = GetXmlHttpObject()
	if ( xmlHttp == null )
	{
		alert ("Browser does not support HTTP Request")
		return
	}
	var url = tmp_rootpath_div+'do_cart.php?cart_action='+action+'&product_id='+product_id+'&quantity='+qty_sel+'&setCartFlg=1';	

	xmlHttp.onreadystatechange = stateChanged
	xmlHttp.open( 'GET', url, true )
	xmlHttp.send( null );
	function stateChanged()
	{
		if ( xmlHttp.readyState == 4 || xmlHttp.readyState == "complete" )
		{
			var objDetails = xmlHttp.responseText;
			var ajax_response = objDetails.split('_');
			alert_msg = ajax_response[1];
			var basket_qty = ajax_response[2];
			var basket_tot_price = parseFloat(ajax_response[3]).toFixed(2);

			document.getElementById('div_show_cart').innerHTML = '<a href='+tmp_rootpath+'shopping_cart.php>View Basket ('+basket_qty+') | &pound;&nbsp;'+basket_tot_price+'</a>';
			
			//alert(objDetails.substr(0,5000));
			

			putIF( 'bubble_div', Left, y, w, h, '', alert_msg );
			window.scrollTo(0,0);
		}
	}
	function GetXmlHttpObject()
	{
		var objXMLHttp = null
		if ( window.XMLHttpRequest )
		{
			objXMLHttp = new XMLHttpRequest()
		}
		else if ( window.ActiveXObject )
		{
			objXMLHttp = new ActiveXObject( "Microsoft.XMLHTTP" )
		}
		return objXMLHttp;
	}	
}

function pres_doCart(action, itemid)
{
	var proceed = 1;
	var cart_form = document.frmcart;
	if (!isWhitespace(action) && !isWhitespace(itemid))
	{
		var pid=document.getElementById('pid_'+itemid).value;		
		var qty=document.getElementById('qty_'+itemid).value;
		var to_id = 0;
		if(document.getElementById('to_id_'+itemid))		//to_id_ is Temp Order ID
		{
			to_id=document.getElementById('to_id_'+itemid).value;
		}
		
		//if Action is to Add/Update a cart listing then validate qty else 
		//if user want to remove an item from cart then no need to validate qty field
		if(action != 'REMOVE')
		{
			if(!validateQty(qty))
			{
				proceed = 0;
			}
		}
			
		if(proceed == 1)
		{
			cart_form.action = "pres_do_cart.php";
			cart_form.cart_action.value = action;
			cart_form.cart_pid.value = pid;
			cart_form.cart_qty.value = qty;
			cart_form.to_id.value = to_id;
			cart_form.submit();
		}
	}
}

function pres_doCartnew(action, itemid)
{
	var proceed = 1;

	if ((action=='ADD')&&(itemid>0))
	{
		
		cart_form = document.frmcart;
		var pid=document.getElementById('pid_'+itemid).value;		
		var qty=document.getElementById('qty_'+itemid).value;
		var to_id = 0;
		if(document.getElementById('to_id_'+itemid))		//to_id_ is Temp Order ID
		{
			to_id=document.getElementById('to_id_'+itemid).value;
		}		
		
		
		//if Action is to Add/Update a cart listing then validate qty else 
		//if user want to remove an item from cart then no need to validate qty field
		if(action != 'REMOVE')
		{
			if(!validateQty(qty))
			{
				proceed = 0;
			}
		}
		if(proceed == 1)
		{
			
			cart_form.action = "pres_do_cart.php";
			cart_form.cart_action.value = action;
			cart_form.cart_pid.value = pid;
			cart_form.cart_qty.value = qty;
			cart_form.to_id.value = to_id;

			cart_form.submit();
		}
	}
}
function validateQty(qty)
{	
	if(isWhitespace(qty))
	{
		alert("Please Enter Quantity");
		return false;
	}
	else if(qty==0)
	{
		alert("Product Quantity can not be 0");
		return false;
	}
	else if(!isCharsInBag(qty,"0123456789"))
	{
		alert("Enter Only Numeric Value for Quantity");
		return false;
	}
	return true;
}

function remove_wl(itemid)
{
	if (confirm('Do you really want to remove this product?'))
	{
		var pid = document.getElementById('pid_'+itemid).value;
		document.frmremovewl.wl_pid.value = pid;
		document.frmremovewl.action = "do_wishlist.php";
		document.frmremovewl.submit();
	}
}
function negvalue()
{
	
	var qty = document.getElementById('qty_1').value;
	if(isNaN(qty))
	{
		alert("Enter Only Numeric Value for Quantity");
	}
}

function check_pet_practitioner()
{
	if(document.pet_prodcut.uk_practitioner)
	{
		if(document.pet_prodcut.uk_practitioner.checked != true)
		{
			alert("Please confirm you have or intend to obtain a prescription signed by a Vet. We cannot supply prescription medicine without one.");
			document.pet_prodcut.uk_practitioner.focus();
			//document.focus('uk_practitioner');
			return false;
		}
		else
		{
			document.pet_prodcut.submit(); 
			return true;	
		}
	}
	else
	{
		document.pet_prodcut.submit(); 
		return true;
	}	
	//return false;
}
