// $Id: order_entry.js,v 1.12.2.23 2007/08/30 10:49:09 max Exp $

var number_of_line_items_per_page = 20;
var oeproducts  = new Array('productid','quantity','product_options','price');
var gKeyCode;

var scriptNode = null;

var iCurrentAdd = 0;

var oCurrentFocus = null;
var iCurrentFocus = 0;

var CurrentProduct = '';
      
function AddBlankEntry() {
  var prod_text;

  if(document.getElementById('qty_' + iCurrentFocus).value == '')
	document.getElementById('qty_' + iCurrentFocus).value="0";

  var firstdescr;
  firstdescr=document.getElementById('desc_0').innerHTML;
  if (firstdescr.replace(/^\s+|\s+$/g,"") == '&nbsp;&nbsp;&nbsp;&nbsp;')
	alert("Unable to retrieve product information.  Try refreshing page to correct problem, or contact administrator.");

  if (iCurrentAdd == number_of_line_items_per_page - 1)
  {
      iCurrentAdd = iCurrentFocus;
	document.getElementById('prod_' + iCurrentAdd).focus();
	alert("You have reached the page limit.  To continue, first add these products to the cart.");
	return false;
  }
  if (iCurrentFocus < iCurrentAdd) {
	iCurrentFocus+=1;
	document.getElementById('row_' + iCurrentFocus).style.display = "";
  }
  else {
	iCurrentAdd+=1;
	document.getElementById('row_' + iCurrentAdd).style.display = "";
  }
  document.getElementById('prod_' + iCurrentAdd).focus();
  return false;
}

function get_product_info(tmpProductCode) {
	var dhead;
	dhead = document.getElementsByTagName('head')[0];
	if(scriptNode) dhead.removeChild(scriptNode);
	scriptNode=document.createElement('script');
	scriptNode.type='text/javascript';
	scriptNode.src = xcart_web_dir + '/modules/Quick_Entry/order_entry.php?mode=productinfo&productcode=' + tmpProductCode + '&irow=' + iCurrentFocus;
	dhead.appendChild(scriptNode);
	return;
}

function get_wholesale_price(tmpProductCode, tmpqty) {
	var dhead;
	dhead = document.getElementsByTagName('head')[1];
	if(scriptNode) dhead.removeChild(scriptNode);
	scriptNode=document.createElement('script');
	scriptNode.type='text/javascript';
	scriptNode.src = xcart_web_dir + '/modules/Quick_Entry/order_entry.php?mode=wholesale&productcode=' + tmpProductCode + '&quantity=' + tmpqty + '&irow=' + iCurrentFocus;
	dhead.appendChild(scriptNode);
	return;
}

function update_order_entry_fields(irow) {
	var iquantity=0;
	document.getElementById('productid3_' + irow).value = product_info[0];
	document.getElementById('desc_' + irow).innerHTML = product_info[1];
	if (product_info[2] == "0.00")
		document.getElementById('price_' + irow).innerHTML = "Call";
	else
		document.getElementById('price_' + irow).innerHTML = price_format(product_info[2]);
	document.getElementById('uom_' + irow).innerHTML = product_info[3];
	document.getElementById('shipdate_' + irow).value = product_info[4];
	document.getElementById('product_options3_' + irow).value = product_info[5];
	if (document.getElementById('productid3_' + irow).value == ''){
			alert ("Invalid product.  Try again");
			document.getElementById('prod_' + irow).focus();
	}
  	tmpqty = document.getElementById('qty_' + iCurrentFocus).value;
	document.getElementById('tot_' + irow).innerHTML = price_format(document.getElementById('price_' + irow).innerHTML  * tmpqty);
}

function update_order_entry_price(irow,iquantity) {
	if (product_info[0]== "0.00")
		document.getElementById('price_' + irow).innerHTML = "Call";
	else
		document.getElementById('price_' + irow).innerHTML = price_format(product_info[0]);
	document.getElementById('tot_' + irow).innerHTML = price_format(document.getElementById('price_' + irow).innerHTML  * iquantity);
}
function UpdateFavourites(){
	
}

function UpdateFocus(iLineItem, oField) {
  var tmpProductCode;
  iCurrentFocus = iLineItem;
  oCurrentFocus = oField;
  tmpProductCode = document.getElementById('prod_' + iCurrentFocus).value;
  if (tmpProductCode != "") {
	CurrentProduct = tmpProductCode;
	get_product_info(tmpProductCode);
  }

  document.getElementById('qty_' + iCurrentFocus).value="0";
  document.getElementById('tot_' + iCurrentFocus).innerHTML = price_format(0);
}

function UpdateQty(iLineItem, oField) {
  var tmpqty;
  var tmpProductCode;
  iCurrentFocus = iLineItem;
  oCurrentFocus = oField;
  tmpProductCode = document.getElementById('prod_' + iCurrentFocus).value;
  tmpqty = document.getElementById('qty_' + iCurrentFocus).value;
  if (tmpProductCode != "" && (tmpqty > 1 || gKeyCode == 8)) {
	CurrentProduct = tmpProductCode;
	get_wholesale_price(tmpProductCode, tmpqty);
  }else{
	firstdescr=document.getElementById('desc_' + iCurrentFocus).innerHTML;
	if (firstdescr.replace(/^\s+|\s+$/g,"") != '&nbsp;&nbsp;&nbsp;&nbsp;')
		document.getElementById('tot_' + iCurrentFocus).innerHTML = price_format(document.getElementById('price_' + iCurrentFocus).innerHTML  * tmpqty );
  }
}

function GetPostDataForm1() {
// Get Product Details
  j = 0;
  for (i = 0; i<number_of_line_items_per_page; i++){
	tmpSource = eval("document.form3.prod_" + i);
	if (tmpSource.value != "") {
		tmpTarget = eval("document.form1.prod1_" + j);
		tmpTarget.value = tmpSource.value;
		tmpSource = eval("document.form3.qty_" + i);
		tmpTarget = eval("document.form1.qty1_" + j);
		tmpTarget.value = tmpSource.value;
		tmpSource = eval("document.form3.shipdate_" + i);
		tmpTarget = eval("document.form1.shipdate1_" + j);
		tmpTarget.value = tmpSource.value;
		j+=1;
	}
  }
  document.form1.ItemCount.value = j;
}

function GetPostDataForm2() {
// Add to Basket
var i;
var j;
var tmpSource;
var tmpTarget;
j = 0;
for (i = 0; i<number_of_line_items_per_page; i++){
	tmpSource = eval("document.form3.prod_" + i);
	if (tmpSource.value != ""){
		tmpSource = eval("document.form3.productid3_" + i);
		tmpTarget = eval("document.form2.productid_" + j);
		tmpTarget.value = tmpSource.value;
		tmpTarget = eval("document.form2.amount_" + j);
		tmpSource = eval("document.form3.qty_" + i);
		tmpTarget.value = tmpSource.value;
		tmpTarget = eval("document.form2.shipdate_" + i);
		tmpSource = eval("document.form3.shipdate_" + j);
		tmpTarget.value = tmpSource.value;
		tmpTarget = eval("document.form2.product_options_" + i);
		tmpSource = eval("document.form3.product_options3_" + j);
		tmpTarget.value = tmpSource.value;
		j+=1;
	}
}
document.form2.ItemCount.value = j;
}

function GetPostDataForm4() {
// Add to Quick List
j = 0;
for (i = 0; i<number_of_line_items_per_page; i++){
	tmpSource = eval("document.form3.prod_" + i);
	if (tmpSource.value != ""){
		tmpTarget = eval("document.form4.prod_" + j);
		tmpTarget.value = tmpSource.value;
		tmpTarget = eval("document.form4.qty_" + j);
		tmpSource = eval("document.form3.qty_" + i);
		tmpTarget.value = tmpSource.value;
		tmpTarget = eval("document.form4.shipdate_" + i);
		tmpSource = eval("document.form3.shipdate_" + j);
		tmpTarget.value = tmpSource.value;

		j+=1;
	}
}
document.form4.ItemCount.value = j;
}

function ClearEntry(iLineItem){ 
   tmpTarget = eval("document.form3.prod_" + iLineItem); 
   tmpTarget.value = ""; 
   tmpTarget = eval("document.form3.qty_" + iLineItem); 
   tmpTarget.value = "";

	if (iCurrentAdd != 0) {
		document.getElementById('row_' + iLineItem).style.display = "none";
	}
	document.getElementById('productid3_' + iLineItem).value = "";
	document.getElementById('desc_' + iLineItem).innerHTML="";
	document.getElementById('price_' + iLineItem).innerHTML="";
	document.getElementById('uom_' + iLineItem).innerHTML="";
	document.getElementById('tot_' + iLineItem).innerHTML="";
   	if (iCurrentAdd>0)
		iCurrentAdd-=1;   

	document.getElementById('prod_' + iLineItem).focus();
} 

function CheckTab(iLineItem, keypressed){
	var theid;
	var firstdescr;
	if (iLineItem==iCurrentAdd)
	{
		if(!oCurrentFocus)
			theid = 1;
		if(oCurrentFocus)
			theid = oCurrentFocus.id;
		if (String.fromCharCode(keypressed) == "\t" && theid == document.getElementById('qty_' + iLineItem).id)
		{
			firstdescr=document.getElementById('desc_' + iLineItem).innerHTML;
			if (firstdescr.replace(/^\s+|\s+$/g,"") == '&nbsp;&nbsp;&nbsp;&nbsp;') {
				setTimeout('AddBlankEntry()',1000);
			} else {
				AddBlankEntry();
			}
			document.getElementById('qty_' + iLineItem).focus();
			return true;
		}
	}
	return true;
}

function doKeyPressed(e) {
	if (!e) e = window.event;
	if (e.keyCode) gKeyCode = e.keyCode;
	else if (e.which) gKeyCode = e.which;
	if (gKeyCode) CheckTab(iCurrentFocus, gKeyCode);
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();
	
}
window.document.onkeydown = doKeyPressed;


