function checkAjax() {var ajaxRequest;try {ajaxRequest=new XMLHttpRequest();}catch(e){try{ajaxRequest=new ActiveXObject("Msxml2.XMLHTTP");}catch(e){try{ajaxRequest=new ActiveXObject("Microsoft.XMLHTTP");}catch(e){alert("Your browser does not support AJAX! Please upgrade your browser.");return false;}}}return ajaxRequest;}
function checkLogin(customer_id) {
	if(customer_id=='') {
		window.location='/login.php?lastOrder=1';
	}
}

function lastOrder(customer_id) {
	checkLogin(customer_id);
	var postRequest=checkAjax();
	var Qforms=document.getElementById('quickOrder');
	var selects=Qforms.getElementsByTagName('select');
	var inputs=Qforms.getElementsByTagName('input');
	var textarea=Qforms.getElementsByTagName('textarea');
	var query='';
	var parent=Qforms.parentNode;
	postRequest.onreadystatechange=function() {
		if(postRequest.readyState==4){
			parent.innerHTML=postRequest.responseText;
		}
	}
	for(i=0;i<selects.length;i++) {
		if(selects[i].name) {
			var option=encodeURIComponent(selects[i].getElementsByTagName('option')[selects[i].selectedIndex].value);
			query+=selects[i].name + '=' + option + '&';
		}
	}
	for(i=0;i<inputs.length;i++) {
		if(inputs[i].name) {
			query+=inputs[i].name + '=' + encodeURIComponent(inputs[i].value) + '&';
		}
	}
	for(i=0;i<textarea.length;i++) {
		if(textarea[i].name) {
			query+=textarea[i].name + '=' + encodeURIComponent(textarea[i].value) + '&';
		}
	}
	query=query.replace(/\&$/,'');
	postRequest.open('POST','/quick_order.ajax.php?customer_id=' + customer_id + '&lastOrder=1',true);
	postRequest.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	postRequest.send(query);
}

function showMore(totalCount) {
	var postRequest=checkAjax();
	var theTable = document.getElementById('quickOrderItems');
	var theInner = theTable.innerHTML;
	var newHTML = '';
	for(i=totalCount;i<totalCount+3;i++) {
		addRowToTable();
		//newHTML += '<tr><td><input type="text" name="model[' + i + ']" /></td><td><input type="text" name="qty[' + i + ']" class="quickOrderQty" /></td></tr>';
	}
	//theInner += newHTML;
	//theTable.innerHTML = theInner;
}

function addRowToTable()
{
  var tbl = document.getElementById('quickOrderItems');
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var iteration = lastRow;
  var row = tbl.insertRow(lastRow);
  
  // left cell
  var cellLeft = row.insertCell(0);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'model[' + iteration +']';
  cellLeft.appendChild(el);
  
  // right cell
  var cellRight = row.insertCell(1);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'qty[' + iteration +']';
  el.className = 'quickOrderQty';
  
  cellRight.appendChild(el);
}
