	var pid = new Array();
	var pname = new Array();
	var attribute = new Array();
	var attributeid = new Array();
	var quantity = new Array();
	var taxrate = new Array();
	var qtyleft = new Array();
	var price = new Array();
	var taxtotal = 0;
	var subtotal = 0;
	var intotal = 0;
	var currentFocusField;

	function currentfocus(field)
	{
		currentFocusField = field;
	}
	
	function keypad(key)
	{
		if(key =='<')
		{
			currentFocusField.value = currentFocusField.value.substring(0,currentFocusField.value.length-1);
		}
		else{
			currentFocusField.value = currentFocusField.value+key;
		}
		//var e = jQuery.Event("keydown", { keyCode: key});
		//jQuery("body").trigger(e);
	}
	
	function togglekp()
	{
		var height = isNaN(window.innerHeight) ? window.clientHeight : window.innerHeight;
		mheight = height - 450;
	
		if(document.getElementById('keypadrow').style['display'] == '')
		{
			document.getElementById('itemstable').style['max-height'] = (height-200)+'px';
			document.getElementById('keypadrow').style['display'] = 'none';
		}else{
			
			document.getElementById('itemstable').style['max-height'] = mheight+'px';
			document.getElementById('keypadrow').style['display'] = '';
		}
	}
	
	function additem(a_pid,a_name,a_price,a_attribute,a_attributeid,a_qtyleft,a_taxrate)
	{
		pid.push(a_pid);
		pname.push(a_name);
		price.push(a_price);
		quantity.push(1);
		attribute.push(a_attribute);
		attributeid.push(a_attributeid);
		qtyleft.push(a_qtyleft);
		taxrate.push(a_taxrate);
		
		refreshtbl();
		
	}
	
	function refreshtbl()
	{
		var showname = '';
		var showqty = '';
		var showatt = '';
		var showprice = '';
		var showtotal = '';
		var showremove = '';
		var border = '';
		for (i=0;i<pid.length;i++)
		{
			if(i != pid.length-1){
				border = 'border-bottom:2px solid #000';
			}else{
				border = '';
			}
			if(pid[i] != ''){
				showname = showname +'<div style="table-row;width:100%;'+border+'"><div style="display: table-cell;vertical-align: middle;height:30px;">'+pname[i]+'</div></div>';
				showatt = showatt +'<div style="table-row;width:100%;'+border+'"><div style="display: table-cell;vertical-align: middle;height:30px;">'+attribute[i]+'</div></div>';
				if(quantity[i]>qtyleft[i]){
					showqty = showqty +'<div style="table-row;width:100%;'+border+'"><div style="display: table-cell;vertical-align: middle;white-space: nowrap;height:30px;"><font color=red><b>'+quantity[i]+'</b>&nbsp;&nbsp;</font></div><div style="display: table-cell;vertical-align: middle;white-space: nowrap;height:30px;"><a href="#" onclick="addqty('+i+');return false;"><img src="images/pos/add.png" width="25px"></a>&nbsp;<a href="#"onclick="subqty('+i+');return false;"><img src="images/pos/remove.png" width="25px"></a></div></div>';
				}else{
					showqty = showqty +'<div style="table-row;width:100%;'+border+'"><div style="display: table-cell;vertical-align: middle;white-space: nowrap;height:30px;">'+quantity[i]+'&nbsp;&nbsp;</div><div style="display: table-cell;vertical-align: middle;white-space: nowrap;height:30px;"><a href="#" onclick="addqty('+i+');return false;"><img src="images/pos/add.png" width="25px"></a>&nbsp;<a href="#"onclick="subqty('+i+');return false;"><img src="images/pos/remove.png" width="25px"></a></div></div>';
				}
				showprice = showprice+'<div style="table-row;width:100%;'+border+'"><div style="display: table-cell;vertical-align: middle;height:30px;">'+currencysymbol+''+price[i]+'</div></div>';
				showremove = showremove +'<div style="table-row;width:100%;'+border+'"><div style="display: table-cell;vertical-align: middle;height:30px;"><a href="#" onclick="removeproduct('+i+');return false;"><img src="images/pos/cross.png" width="25px"></a></div></div>';
				
				showtotal = showtotal +'<div style="table-row;width:100%;'+border+'"><div style="display: table-cell;vertical-align: middle;height:30px;">'+currencysymbol+''+(price[i]*quantity[i]).toFixed(2)+'</div></div>';
			}
		}
		document.getElementById('pnameshow').innerHTML = showname;
		document.getElementById('pattshow').innerHTML = showatt;
		document.getElementById('pqtyshow').innerHTML = showqty;
		document.getElementById('premoveshow').innerHTML = showremove;
		document.getElementById('ppriceshow').innerHTML = showprice;
		document.getElementById('ptotalshow').innerHTML = showtotal;
		
		updatetax();
		updatesubtotal();
		updateprice();
		updateqty();
	}
	
	function updatetax()
	{
		taxtotal = 0;
	
		for (i=0;i<pid.length;i++)
		{
			taxtotal = eval(taxtotal + eval(taxrate[i]*price[i]/100*quantity[i]));
		}
		taxtotal = taxtotal.toFixed(2);
		document.getElementById('taxtotal').innerHTML = taxtotal;
	}
	
	function updateqty()
	{
		totalqty = 0;
	
		for (i=0;i<pid.length;i++)
		{
			if(quantity[i] != '')
			{
				totalqty += quantity[i];
			}
		}
		document.getElementById('totalqty').innerHTML = totalqty;
	
	}
	
	function updatesubtotal()
	{
		subtotal = 0;
	
		for (i=0;i<pid.length;i++)
		{
			subtotal = eval(subtotal + eval(price[i]*quantity[i]));
		}
		subtotal = subtotal.toFixed(2);
		document.getElementById('subtotal').innerHTML = subtotal;
		document.getElementById('ftotal').value = subtotal;
		document.getElementById('totalamount').innerHTML = subtotal;
		document.getElementById('totalamount2').innerHTML = "$"+subtotal;
		document.getElementById('totalamount3').innerHTML = "$"+subtotal;
	}
	
	function updateprice()
	{
		if(document.getElementById('discount').value != '')
		{
			intotal = eval(subtotal) + eval(taxtotal);
			intotal = eval(intotal - document.getElementById('discount').value);
		}else{
			intotal = eval(subtotal) + eval(taxtotal);
		}
		intotal = intotal.toFixed(2);
		document.getElementById('totalamount').innerHTML = intotal;
		//document.getElementById('').innerHTML = intotal;
		//document.getElementById('finalprice').innerHTML = intotal;
	}
	
	function updatechange()
	{
	try{
		var recieve = document.getElementById('recievedcash').value;
		var change = recieve-intotal;
		change = change.toFixed(2);
		if(change<0)
		{
			change = "<font color=red>"+change+"</font>";
		}	
		document.getElementById('changetotal').innerHTML = change;
	}catch(err){}
	}
	
	function addqty(count)
	{
		
		quantity[count] = quantity[count]+1;
		refreshtbl();
	}
	
	function subqty(count)
	{
		if(quantity[count]>0){
			quantity[count] = quantity[count]-1;
			refreshtbl()
		}
	}
	
	function removeproduct(count)
	{	
		pid[count] = '';
		pname[count] = '';
		attribute[count] = '';
		attributeid[count] = '';
		qtyleft[count] = '';
		quantity[count] = '';
		price[count] = '';
		taxrate[count] = '';
		refreshtbl();
	}
	
	function clearall()
	{
			document.getElementById('discount').value = 0;
			pid = [];
			pname = [];
			attribute = [];
			attributeid = [];
			quantity = [];
			price = [];
			subtotal = 0;
			intotal = 0;
			refreshtbl();
			
	}
	
	function processall()
	{
		var xcor = screen.width/2;
		var ycor = screen.height/2;
	
		document.getElementById('finalprice').innerHTML = document.getElementById('invoicetotal').innerHTML;
		document.getElementById('complete').style.display = 'block';
		document.getElementById('complete').style.left = xcor-150;
		document.getElementById('complete').style.top = ycor-200;
		document.getElementById('mainbody').style.opacity = '0.2';
		document.getElementById('mainbody').style.filter = 'alpha(opacity=20)';
		document.getElementById('recievedcash').focus();
	}

	
	function submitorder(type,exact)
	{
		var pidsender = '';
		var namesender = '';
		var attsender = '';
		var taxsender = '';
		var qtysender = '';
		var pricesender = '';
		
		updatechange();
		
		for (i=0;i<pid.length;i++)
		{
			if(pid[i] != ''){
				pidsender = pidsender + ';' + pid[i];
				attsender = attsender + ';' + attributeid[i];
				qtysender = qtysender + ';' + quantity[i];
				pricesender = pricesender + ';' + price[i];
				taxsender = taxsender + ';' + taxrate[i];
				if(pid[i] != 'NI'){
					namesender = namesender + ';-';
				}else{
					namesender = namesender + ';' + pname[i];
				}
			}
		}
		
		document.datasender.pidlist.value = pidsender.substring(1);
		document.datasender.attlist.value = attsender.substring(1);
		document.datasender.qtylist.value = qtysender.substring(1);
		document.datasender.pricelist.value = pricesender.substring(1);
		document.datasender.namelist.value = namesender.substring(1);
		document.datasender.taxlist.value = taxsender.substring(1);
		
		//document.datasender.paymentmode.value = document.getElementById('paymentmodeselect').options[document.getElementById('paymentmodeselect').selectedIndex].value;
		
		if(type == 1)
		{
			document.datasender.paymentmode.value = "Cash";
		}else if(type == 2)
		{
			document.datasender.paymentmode.value = "NETS";
		}else{
			document.datasender.paymentmode.value = "Cash_NETS";
		}
		
		document.datasender.commentsend.value = "";
		document.datasender.discountgiven.value = document.getElementById('discount').value;
		
		if(exact == 1)
		{
			if(type == 1)
			{	
				document.getElementById('totalamount2').value = document.getElementById('ftotal').value;
				document.getElementById('totalamount3').value = 0;
				document.datasender.cashrecieved.value = document.getElementById('ftotal').value;
				document.datasender.netsrecieved.value = 0;
			}else if(type == 2){
				document.getElementById('totalamount3').value = document.getElementById('ftotal').value;
				document.getElementById('totalamount2').value = 0;
				document.datasender.netsrecieved.value = document.getElementById('ftotal').value;
				document.datasender.cashrecieved.value = 0;
			}
		}else{
			
			
			
			if(type == 1)
			{	
				document.datasender.cashrecieved.value = document.getElementById('totalamount2').value;
			}else if(type == 2){
				document.datasender.netsrecieved.value = document.getElementById('totalamount3').value;
			}else{
				//both
				document.datasender.cashrecieved.value = document.getElementById('totalamount2').value;
				document.datasender.netsrecieved.value = document.getElementById('totalamount3').value;
			}
			
			if(document.getElementById('totalamount2').value == "")
			{
				document.getElementById('totalamount2').value = 0;
				document.datasender.cashrecieved.value = 0;
			}
			
			if(document.getElementById('totalamount3').value == "")
			{
				document.getElementById('totalamount3').value = 0;
				document.datasender.netsrecieved.value = 0;
			}
			
		
		}
		document.datasender.orderstatus.value = 6;
		document.datasender.branch.value = "b1";
		
		
		
		document.datasender.setstatus.value = 'off';
		
		
		//document.datasender.customer.value = '0';
		var recieve = 0;
		if(exact == 1)
		{
			recieve = document.getElementById('ftotal').value;
			
		}else{
			
		
			recieve = document.getElementById('totalamount2').value + document.getElementById('totalamount3').value;
		
		}
		
		if(document.getElementById('totalamount2').value ==''){
			var r=confirm("Recieved Cash is Empty, proceed with 0 cash recieved ?");
			if (r==true){
				document.datasender.submit();
				resetformnow();
			}
		}
		
		if(document.getElementById('totalamount3').value ==''){
			var r=confirm("Recieved Nets is Empty, proceed with 0 nets recieved ?");
			if (r==true){
				document.datasender.submit();
				resetformnow();
			}
		}
		
		else if(eval(recieve - intotal)<0){
			var r=confirm("Recieved Amount is less that the total order cost, proceed ?");
			if (r==true){
				document.datasender.submit();
				resetformnow();
			}
		}
		
		else{	
			document.datasender.submit();
			resetformnow();
		}
		
		if(print == '1')
		{
			alert('need to print');
		}
	}
	function cancelsubmit()
	{

		document.getElementById('complete').style.display = 'none';
		document.getElementById('mainbody').style.opacity = '1';
		document.getElementById('mainbody').style.filter = 'alpha(opacity=100)';
	}
	
	function addnoninvshow()
	{
		var xcor = screen.width/2;
		var ycor = screen.height/2;
		document.getElementById('niname').value = '';
		document.getElementById('niprice').value = '';
		document.getElementById('nitax').value = '0';
		document.getElementById('adhocproduct').style.display = 'block';
		document.getElementById('adhocproduct').style.left = xcor-200;
		document.getElementById('adhocproduct').style.top = ycor-250;
		document.getElementById('mainbody').style.opacity = '0.2';
		document.getElementById('mainbody').style.filter = 'alpha(opacity=20)';
	}
	function addnoninvc()
	{
		document.getElementById('adhocproduct').style.display = 'none';
		document.getElementById('mainbody').style.opacity = '1';
		document.getElementById('mainbody').style.filter = 'alpha(opacity=100)';
	}

	function addnoninv()
	{
		//Validation
		if(validation())
		{
			pid.push("NI");
			pname.push(document.getElementById('niname').value);
			price.push(document.getElementById('niprice').value);
			quantity.push(1);
			attribute.push('-');
			taxrate.push(document.getElementById('nitax').value);
			attributeid.push('[NA]');
			qtyleft.push('9999');
			refreshtbl();
			addnoninvc();
		}
	}
	
	function validation()
	{
		if(document.getElementById('niname').value == '' 
				|| document.getElementById('niprice').value == ''
				|| document.getElementById('nitax').value == ''	
		){
				alert("Empty fields found");
				return false;
		}else{
			var niproduct = document.getElementById('niname').value;
			var niprice = document.getElementById('niprice').value;
			var nitax = document.getElementById('nitax').value;
			if(!validatefield(niprice,1)){
				alert("Price should contain decimal numbers only");
				return false;
			}
			if(!validatefield(nitax,1)){
				alert("Tax should contain decimal numbers only");
				return false;
			}
			if(!validatefield(niproduct,2)){
				alert("Special characters !@#$%^&*()+=-[]\';,./{}|\":<>? not allowed in product name");
				return false;
			}
		}
		return true;
	}
	
	function validatefield(sText,type)
	{
		var ValidChars = "";
		if(type ==1){
			ValidChars = "0123456789.";
		}else if(type ==2){
			ValidChars = "!@#$%^&*()+=-[]\';,./{}|\":<>?";
		}
		var returnname=true;
		var Char;
		for (i = 0; i < sText.length && returnname == true; i++) 
		{ 
			Char = sText.charAt(i);
			if (ValidChars.indexOf(Char) == -1 && type==1) 
			{
				returnname = false;
			}else if(ValidChars.indexOf(Char) != -1 && type==2){
				returnname = false;
			} 
		}
		return returnname;
   }
   
	function customerlistshow()
	{
		var xcor = screen.width/2;
		var ycor = screen.height/2;
		document.getElementById('customerframe').src = 'pos_customer.php';
		document.getElementById('customerselect').style.display = 'block';
		document.getElementById('customerselect').style.left = xcor-300;
		document.getElementById('customerselect').style.top = ycor-350;
		document.getElementById('mainbody').style.opacity = '0.2';
		document.getElementById('mainbody').style.filter = 'alpha(opacity=20)';
	}
	
	function customerlistc()
	{
		document.getElementById('customerselect').style.display = 'none';
		document.getElementById('mainbody').style.opacity = '1';
		document.getElementById('mainbody').style.filter = 'alpha(opacity=100)';
	}
	
	function customerchange(type,cid,pname)
	{
		if(type=='dd')
		{
			document.datasender.customer.value = document.getElementById('customerlist').value;
			var w = document.getElementById('customerlist').selectedIndex;
			document.getElementById('cusname').innerHTML = document.getElementById('customerlist').options[w].text;
		}else if(type=='search'){
			document.datasender.customer.value = cid;
			document.getElementById('cusname').innerHTML = pname;
		}
		customerlistc();
	}
	
	function resetformnow()
	{
	document.getElementById('cusname').innerHTML = "In-Store Customer";
		document.getElementById('customer').value = 0;
		document.getElementById('discount').value = 0;
		//document.getElementById('recievedcash').value = 0;
		document.getElementById('_iframe').src ="";
		document.getElementById('productsearch').value = "";
		document.getElementById('totalamount2').value = "";
		document.getElementById('totalamount3').value = "";
		
		
		pid = [];
		pname = [];
		attribute = [];
		attributeid = [];
		quantity = [];
		price = [];
		taxrate = [];
		qtyleft = [];
		subtotal = 0;
		intotal = 0;
		
		refreshtbl();
	}
	
	function setCookie(value)
	{
	var now = new Date();
	var time = now.getTime();
	time += 315360000000;
	now.setTime(time);
	document.cookie = 
    'branch=' + value + 
    '; expires=' + now.toGMTString() + 
    '; path=/';
	
	document.getElementById('branchsearch').value = value;
	//document.getElementById('branchsearchbc').value = value;
	}
	
	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}
	
