// JavaScript Document
function loadProdInfo(formname)	{
	prodcode = formname.prodcode.value;
	if(prodcode != null && prodcode != '')	{		
		params = 'prodcode=' + prodcode;
		navigateGo('process.sales.cfm','msgdiv',params,processProdInfo,formname);
	}	else	{
		formname.prodcode.value = '';	
		formname.prodcode.focus();
	}
}

function processProdInfo(nav_response,comp2fill,formname)	{
	if(nav_response.indexOf('DISPLAY_0RDER_TABL3') == 0)	{
		nav_response = nav_response.replace('DISPLAY_0RDER_TABL3','');
		document.getElementById('salesarea').innerHTML = nav_response;
		document.getElementById('changefield').innerHTML = '';
		formname.prodcode.value = '';
		formname.prodcode.focus();
		total = document.getElementById('totall').value;
			if(total != '' && total != '0' && total != null)	{
				document.getElementById('printbutton').disabled = false;
				document.getElementById('saveorderbutton').disabled = false;
			}
	}	else	{
		location.href = './';
	}
}

function handleProdEnter (event, formname) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if (keyCode == 13) {
		loadProdInfo(formname);
		return false;
	} 
	else
	return true;
}

function loadTotal(total)	{
	document.getElementById('total_field').innerHTML = total;	
}

function doNewOrder()	{
	formname = '';
	params = 'neworder=1';
	pageAction('process.sales.cfm',params,'msgdiv',processNewOrder);	
}

function processNewOrder(nav_response,comp2fill)	{
	if(nav_response.indexOf('DISPLAY_0RDER_TABL3') == 0)	{
		nav_response = nav_response.replace('DISPLAY_0RDER_TABL3','');
		document.getElementById('salesarea').innerHTML = nav_response;
		document.getElementById('prodcode').value = '';
		document.getElementById('prodcode').focus();
		document.getElementById('printbutton').disabled = true;
		document.getElementById('reprintbutton').disabled = true;
		document.getElementById('saveorderbutton').disabled = true;	
		document.getElementById('changefield').innerHTML = '';
	}	else	{
		location.href = './';	
	}
}

function doOrderPrint()	{
	total = document.getElementById('totall').value;
	if(total != '' && total != '0' && total != null)	{
		tendered = prompt('Cash Tendered...','');	
		if(isNaN(tendered))	{
			alert('Invalid Amount');	
		}	else if((tendered*1) < (total*1))	{
			alert('Invalid Amount');	
		}	else	{
			change = (tendered*1) - (total*1);	
			change = Math.round(change*100)/100;
			params = 'tendered='+tendered+'&change='+change;
			document.getElementById('changefield').innerHTML = 'CHANGE : <del>N</del> ' + change;
			pageAction('process.print.cfm',params,'receipt',processSaleTender);	
		}
	}
}

function processSaleTender(nav_response,comp2fill)	{
		if(nav_response == 'DATABASE_ERROR')	{
			alert('ERROR');
		}	else {
				document.getElementById(comp2fill).innerHTML = nav_response;
				document.getElementById('printbutton').disabled = true;
				document.getElementById('saveorderbutton').disabled = true;	
				window.print();
				document.getElementById('reprintbutton').disabled = false;
		}	
}

function doOrderReprint()	{
	window.print();
}

function changeColour(elementId) {
	var interval = 500;
	var colour1 = "red", colour2 = "black";
		if (document.getElementById) {
			var ele = document.getElementById(elementId);
				if(ele == null)	{}
				else	{
					if(ele.style.color == colour1)	{
						ele.style.color = colour2;
					}	else	{
						ele.style.color = colour1;
					}
					setTimeout("changeColour('" + elementId + "')", interval);
				}
		}
}

function doLookup(path){
	productname = prompt("Pls Enter The Item Name","");
	
	if(productname == null)	{}
	else	{
	var spage = path + '?productname=' + productname;
	showPopWin(spage, 850, 500, null);
	document.getElementById('prodcode').value = ''
	}
} 

function doProdLookup(prodcode)	{
	window.top.hidePopWin();	
	parent.document.getElementById('prodcode').value = prodcode;
	parent.document.getElementById('go').click();	
}

function doProdRemove(prodrow, prodname)	{
	formname = document.getElementById('cashierform');
	
	if(confirm('Remove ' + prodname + ' ?'))	{
		if(prodrow != null && prodrow != '')	{		
			params = 'prodrow=' + prodrow;
			navigateGo('process.sales.cfm','msgdiv',params,processProdInfo,formname);
		}	else	{
			formname.prodcode.value = '';	
			formname.prodcode.focus();
		}	
	}	else	{
			formname.prodcode.value = '';	
			formname.prodcode.focus();		
	}
}

function  doQuantityUpdate(prodrow)	{
	newquan = prompt('New Quantiy...','');
	formname = document.getElementById('cashierform');
	
	if(isNaN(newquan))	{
		alert('INVALID VALUE !!!');		
		formname.prodcode.value = '';	
		formname.prodcode.focus();
	}	else	{
			if(prodrow != null && prodrow != '')	{		
				params = 'prodrow=' + prodrow + '&newquan=' + newquan;
				formname = document.getElementById('cashierform');
				navigateGo('process.sales.cfm','msgdiv',params,processProdInfo,formname);
			}	else	{
				formname.prodcode.value = '';	
				formname.prodcode.focus();
			}
	}	
}

function doProdSave()	{
	total = document.getElementById('totall').value;
	if(total != '' && total != '0' && total != null)	{
		params = '';
		pageAction('process.save.cfm',params,'receipt',processOrderSave);	
	}		
}

function processOrderSave(nav_response,comp2fill)	{
		if(nav_response == 'DATABASE_ERROR')	{
			alert('ERROR');
		}	else {
				document.getElementById('changefield').innerHTML = 'ORDER SAVED';
				document.getElementById('printbutton').disabled = true;
				document.getElementById('saveorderbutton').disabled = true;	
				document.getElementById('reprintbutton').disabled = true;
		}	
		formname = document.getElementById('cashierform');
		formname.prodcode.value = '';	
		formname.prodcode.focus();
}

function doLogout()	{
	pageAction('process.logout.cfm','','',processLogout);	
}

function processLogout(nav_response,comp2fill)	{
	location.href = './';
}	