   var http_request = false;
   function makePOSTRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
      
      http_request.onreadystatechange = alertContents;
      http_request.open('POST', url, true);
      http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      http_request.setRequestHeader("Content-length", parameters.length);
      http_request.setRequestHeader("Connection", "close");
      http_request.send(parameters);
   }

   function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            //alert(http_request.responseText);
            result = http_request.responseText;
            document.getElementById('response').innerHTML = result;            
         } else {
            alert('There was a problem with the request.');
         }
      }
   }
   ////////////////function to convert undefined to blanks////////////////////////////////////
   function cleanoutput(strClean){
	   if (String(strClean) == "undefined"){
			strClean = "";   
	   }
	   
	   return strClean;
   }   
   ////////////////function to open new browser window////////////////////////////////////
   function MM_openBrWindow(theURL,winName,features) 
   { //v2.0
  
  window.open(theURL,winName,features);
}
/////////////////////////////////function to populate the update customer form//////////////////////////////
	function getclientdetails(obj){
		//first we clear down the available areas box and re-populate the first box.
		/*  for (i=0;i<document.getElementById('trueareas').options.length;i++) {
			 var elOptNew = document.createElement('option');
			 elOptNew.text = document.getElementById('trueareas').options[i].text;
			 elOptNew.value = document.getElementById('trueareas').options[i].value;
			 var elSel = document.getElementById('availableareas');
			 try {
			   document.getElementById('availableareas').add(elOptNew, null); // standards compliant; doesn't work in IE
			 }
			 catch(ex) {
			   document.getElementById('availableareas').add(elOptNew); // IE only
			 }			  
		  }
		  */
		  //clear the update box
		 document.getElementById('result').innerHTML="";  
		 document.getElementById('trueareas').options.length = 0;
		
		
		
		
		//query database and return xml putting the values in ready for editing
		  xmlhand = function(xml) {
			res = XMLParse.xml2ObjArray(xml, 'record');
			obj.elements['custNAME'].value = cleanoutput(res[0].cust_NAME);			
			obj.elements['custADD1'].value = cleanoutput(res[0].cust_ADD1);
			obj.elements['custADD2'].value = cleanoutput(res[0].cust_ADD2);
			obj.elements['custADD3'].value = cleanoutput(res[0].cust_ADD3);
			obj.elements['custADD4'].value = cleanoutput(res[0].cust_ADD4);
			obj.elements['custTOWN'].value = cleanoutput(res[0].cust_TOWN);
			obj.elements['custPOSTCODE'].value = cleanoutput(res[0].cust_POSTCODE);
			obj.elements['custPHONE'].value = cleanoutput(res[0].cust_PHONE);
			obj.elements['custUSERNAME'].value = cleanoutput(res[0].cust_USERNAME);
			obj.elements['custPASSWORD'].value = cleanoutput(res[0].cust_PASSWORD);
			obj.elements['Submit'].disabled = false;
			//now we shuffle the area rights around
			
			for(i2=0;i2<obj.elements['areacombo'].length;i2++){
				//we have to trim the single trailing whitespace.
				
				if (res[0].cust_REGION.substr(0,6) == obj.elements['areacombo'].options[i2].value){
					obj.elements['areacombo'].selectedIndex = i2;
				}
			}
			var combolength = document.getElementById('availableareas').options.length;
		for (i=0;i<=9;i++) {
		   if (res[0].cust_AREAS.indexOf(document.getElementById('availableareas').options[i].value) != "-1"){ 
				 //we have found a match so take it out of the original list and put it in the areas.
				 var elOptNew = document.createElement('option');
				 //alert (i);
				 elOptNew.text = document.getElementById('availableareas').options[i].text;
				 elOptNew.value = document.getElementById('availableareas').options[i].value;
				 
				 var elSel = document.getElementById('trueareas');
				 try {
				   document.getElementById('trueareas').add(elOptNew, null); // standards compliant; doesn't work in IE
				 }
				 catch(ex) {
				   document.getElementById('trueareas').add(elOptNew); // IE only
				 }
				 //document.getElementById('availableareas').options[0] = null;
			}
		}
		// now we need to delete from the other box
		//document.getElementById('availableareas').selectedIndex = -1;

			
			//select the right choice for the area
			

		  }
		  var xmlajax = new Ajax();
		  xmlajax.setMimeType('text/xml');
		  xmlajax.doPost('phpscripts/getcustomers.php','ID='+obj.elements['clientid'].value, xmlhand, 'xml');
	}

  ///////////////////////////////save client details, called for submission of client application.////////////////////
   function savedata(obj) {
   	 var poststr = '';
	 for(i=0; i<obj.elements.length; i++){
	 	if (i < obj.elements.length){
			poststr = poststr + obj.elements[i].name + "=" + obj.elements[i].value + "&";
			obj.elements[i].disabled = true;
		} else {
			poststr = poststr + obj.elements[i].name + "=" + obj.elements[i].value;
			obj.elements[i].disabled = true;
		}
   	 }
      makePOSTRequest('phpscripts/saveclientdetails.php', poststr);
	  poststr = '';
   }
  ///////////////////////////////////////////This is the update function for client details////////////////////
   function updateClientdetails(obj) {
	  //this is called from the form that charlotte uses update client details.
	  	strhand = function(str) {
				  document.getElementById('result').innerHTML=str;  
		  }


	  var areas = "";
	  var PostData = formData2QueryString(obj);
	  for (var i = 0; i < obj.elements['trueareas'].options.length; i++) {
			 areas = areas+","+obj.elements['trueareas'].options[i].value;
	  }
	  //now strip the leading character
	  areas = areas.substring(1,areas.length);
	  PostData = PostData+"&AREAS="+areas;
	  obj.elements['Submit'].disabled = true;
	  var strajax = new Ajax();
	  strajax.setMimeType('text/text');
	  strajax.doPost('phpscripts/updateclientdetails.php',PostData, strhand, 'text');

   }

  ///////////////////////////////////////////This is called to authorise a new client.////////////////////
   function authClient(obj,strID) {
	  //this is called from the form that charlotte uses to authorise and submit details to the database for new customers.
   	  var poststr = '';
	  poststr = "region="+obj.elements['region'].value+"&";
	  poststr += "customernumber="+obj.elements['customernumber'].value+"&ID="+strID;
	  for (var i = 0; i < obj.elements['trueareas'].options.length; i++) {
			 poststr = poststr+"&"+obj.elements['trueareas'].options[i].text+"="+obj.elements['trueareas'].options[i].value;
	  }
	  obj.elements['Submit'].disabled = true;
      makePOSTRequest('phpscripts/authclientupdate.php', poststr);
	  //alert (poststr);
   }
  ///////////////////////////////////////////this is to amend staff.///////////////////////////////////////////
     function staff(obj) {
	   //this is called from the form that charlotte uses amend staff.
   	  var poststr = '';
	  poststr = "staff_NAME="+obj.elements['name'].value;
	  poststr = poststr+"&staff_POSITION="+obj.elements['position'].value;
	  poststr = poststr+"&staff_GROUP="+obj.elements['group'].value;
	  poststr = poststr+"&staff_AREA="+obj.elements['areacombo'].value;
	  poststr = poststr+"&staff_PHONE="+obj.elements['phone'].value;
	  //obj.elements['Submit'].disabled = true;
	  var i = 0;
	  //alert(obj.length);
      makePOSTRequest('phpscripts/addstaff.php', poststr);
	  //clear the text boxes
	  for (i=0; i<obj.length; i++) 
	  {
			if (obj.elements[i].type == "text"){
					obj.elements[i].value = '';
			}
	  }
   }
  ///////////////////////////////////////////this is to transfer areas from one listbox to another////////////////////////////////
   function addArea(){
	   for (i=0;i<document.getElementById('availableareas').options.length;i++) {
		   if (document.getElementById('availableareas').options[i].selected){ 
			 var elOptNew = document.createElement('option');
			 elOptNew.text = document.getElementById('availableareas').options[i].text;
			 elOptNew.value = document.getElementById('availableareas').options[i].value;
			 var elSel = document.getElementById('trueareas');
			 try {
			   document.getElementById('trueareas').add(elOptNew, null); // standards compliant; doesn't work in IE
			 }
			 catch(ex) {
			   document.getElementById('trueareas').add(elOptNew); // IE only
			 }
			}
		}
		document.getElementById('availableareas').selectedIndex = -1;
   }
  ///////////////////////////////////////////this deletes an entry from the area listbox///////////////////////////////////////////
     function deleteArea(objFrom,objTo){
		for (var i = 0; i < objFrom.options.length; i++) {
		   if (objFrom.options[i].selected){ 
				objFrom.options[i] = null;
			}
		}
		objFrom.selectedIndex = -1;
   }
  ///////////////////////////////////////////this function enables the submit button///////////////////////////////////////////
	function enableSubmit(obj){
		obj.disabled = false;	
	}
  ///////////////////////////////////////////this gets the selected member of staff///////////////////////////////////////////
	function get_staff(Frmobj,obj){
		//query database and return xml putting the values in ready for editing
		  xmlhand = function(xml) {
			res = XMLParse.xml2ObjArray(xml, 'record');
			Frmobj.elements['name'].value = cleanoutput(res[0].staff_NAME);
			Frmobj.elements['position'].value = cleanoutput(res[0].staff_POSITION);
			Frmobj.elements['group'].value = cleanoutput(res[0].staff_GROUP);
			Frmobj.elements['phone'].value = cleanoutput(res[0].staff_PHONE);
			Frmobj.elements['email'].value = cleanoutput(res[0].staff_EMAIL);
			Frmobj.elements['editbutton'].disabled = false;
			Frmobj.elements['deletebutton'].disabled = false;
			get_photo(res[0].PK_STAFF);
			//select the right choice for the area
			for(i=0;i<Frmobj.elements['areacombo'].length;i++){
				if (res[0].staff_AREA == Frmobj.elements['areacombo'].options[i].value){
					Frmobj.elements['areacombo'].selectedIndex = i;
				}
			}
			//select the right choice for the groups
			for(i=0;i<Frmobj.elements['group'].length;i++){
				if (res[0].staff_GROUP == Frmobj.elements['group'].options[i].value){
					Frmobj.elements['group'].selectedIndex = i;
				}
			}
		  }
		  var xmlajax = new Ajax();
		  xmlajax.setMimeType('text/xml');
		  xmlajax.doPost('phpscripts/getstaff.php','ID='+obj.options[obj.selectedIndex].value, xmlhand, 'xml');
	}
  ///////////////////////////////////////////this gets the selected member of staff///////////////////////////////////////////
	function get_groups(Frmobj,obj){
		//query database and return xml putting the values in ready for editing
		  xmlhand = function(xml) {
			res = XMLParse.xml2ObjArray(xml, 'record');
			Frmobj.elements['groupname'].value = res[0].group_NAME;
			Frmobj.elements['descriptions'].value = res[0].group_DESCRIPTION;
			Frmobj.elements['groupeditbutton'].disabled = false;
			Frmobj.elements['groupdeletebutton'].disabled = false;
		  }
		  var xmlajax = new Ajax();
		  xmlajax.setMimeType('text/xml');
		  xmlajax.doPost('phpscripts/getgroups.php','ID='+obj.options[obj.selectedIndex].value, xmlhand, 'xml');
	}
  ///////////////////////////////////////////this clears out all text boxes on a form.///////////////////////////////////////////
	function clear_textboxes(obj){
		//frag the text boxes on a form
		for (var i = 0; i < obj.length; i++){
				if (obj.elements[i].type == "text"){
					obj.elements[i].value = "";
				}
		 }
	}
  //////////////////////////////////////This gets the staff picture and echos it to the div///////////////////////////////////////
	function get_photo(photoID){
		  //commit changes to the database and return status code.
		  strhand = function(str) {
				  document.getElementById('photo').innerHTML=str;  
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = "ID="+photoID
		  strajax.doPost('phpscripts/echophoto.php',PostData, strhand, 'text');
	}
		///////////////////////////////// This is the function for the sampled on the distro page/////////////////////////	
	function changedistrosampled(index,key){
          var dateformat = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
		  //commit changes to the database and return status code.
		  strhand = function(str) {
			 //alert(str);
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  day = document.getElementById('SAMPLED'+index).value.substring(0,2);
		  month = document.getElementById('SAMPLED'+index).value.substring(3,5);
		  year = document.getElementById('SAMPLED'+index).value.substring(6,10);
		  //added on 15th June 2008, client wished for year to autocomplete if only two or three digits were input.
		  year = year * 1;
		  yearlength = String(year);
		  if (yearlength.length <= 3){
			  	year = year + 2000;
		  }
		  UserDate = day+"/"+month+"/"+year;
		  if(!dateformat.test(UserDate)){		
		  		//if we get here this is not a date so quit out.
		  		day = "dd";
		  		month = "mm";
		  		year = "yyyy";
		  		//return;
		  }	  
		  strajax.doPost('phpscripts/changedistrosampled.php',"day="+day+"&month="+month+"&year="+year+"&key="+key, strhand, 'text');
	}

	///////////////////////////////// This is the function for the quoted on the distro page/////////////////////////	
	function changedistroquoted(index,key){
		  //commit changes to the database and return status code.
          var dateformat = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
		  //commit changes to the database and return status code.
		  strhand = function(str) {
			 //alert(str);
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  day = document.getElementById('QUOTED'+index).value.substring(0,2);
		  month = document.getElementById('QUOTED'+index).value.substring(3,5);
		  year = document.getElementById('QUOTED'+index).value.substring(6,10);
		  //added on 15th June 2008, client wished for year to autocomplete if only two or three digits were input.
		  year = year * 1;
		  yearlength = String(year);
		  if (yearlength.length <= 3){
			  	year = year + 2000;
		  }
		  UserDate = day+"/"+month+"/"+year;
		  if(!dateformat.test(UserDate)){		
		  		//if we get here this is not a date so quit out.
		  		day = "dd";
		  		month = "mm";
		  		year = "yyyy";

			  	//return;
		  }	  
		  strajax.doPost('phpscripts/changedistroquoted.php',"day="+day+"&month="+month+"&year="+year+"&key="+key, strhand, 'text');
	}
	///////////////////////////////// This is the function for the contacted on the distro page/////////////////////////	
	function changedistrocontacted(index,key){
		 //check to see if the value is correct
          var dateformat = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/
		  /*if(!dateformat.test(document.getElementById('CONTACTED'+index).value)){		
		  		//if we get here this is not a date so quit out.
		  		return;
		  }*/
		  //commit changes to the database and return status code.
		  strhand = function(str) {
			 //alert(str);
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  day = document.getElementById('CONTACTED'+index).value.substring(0,2);
		  month = document.getElementById('CONTACTED'+index).value.substring(3,5);
		  year = document.getElementById('CONTACTED'+index).value.substring(6,10);
		  //added on 15th June 2008, client wished for year to autocomplete if only two or three digits were input.
		  year = year * 1;
		  yearlength = String(year);
		  if (yearlength.length <= 3){
			  	year = year + 2000;
		  }
		  UserDate = day+"/"+month+"/"+year;
		  if(!dateformat.test(UserDate)){		
		  		//if we get here this is not a date so quit out.
		  		day = "dd";
		  		month = "mm";
		  		year = "yyyy";
		  		//return;
		  }	  
		 //alert("Submitting: "+day+" "+month+" "+year);
		  strajax.doPost('phpscripts/changedistrocontacted.php',"day="+day+"&month="+month+"&year="+year+"&key="+key, strhand, 'text');
	}
	///////////////////////////////// This is the function for the comments on the distro page/////////////////////////	
	function changedistrocomments(index,key){
		  //commit changes to the database and return status code.
		  strhand = function(str) {
			 //alert(index+" "+key);
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  strajax.doPost('phpscripts/changedistrocomments.php',"key="+key+"&value="+document.getElementById('COMMENTS'+index).value, strhand, 'text');
	}
	///////////////////////////////// This is the function for the PA on the distro page/////////////////////////	
	function changedistropa(index,key){
		  if (document.getElementById('PA'+index).value != parseFloat(document.getElementById('PA'+index).value)){
			alert("not numeric");
			document.getElementById('PA'+index).value = document.getElementById('PA'+index).value.substring(0,document.getElementById('PA'+index).value.length -1);
			//return;
		  }
		  //commit changes to the database and return status code.
		  strhand = function(str) {
			 //alert(index+" "+key);
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  strajax.doPost('phpscripts/changedistropa.php',"key="+key+"&value="+document.getElementById('PA'+index).value, strhand, 'text');
	}

	////////////////////This changes the status of the distributor for the client in the distro reports area //////////////
	function changesecured(index,key){
		  strhand = function(str) {
			 //alert(str);
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var strPost = document.getElementById('SECURED'+index).value;
		  //alert (strPost);
		  strajax.doPost('phpscripts/changesecured.php',"value="+strPost+"&key="+key, strhand, 'text');
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////
  ///////////////////////////////////////////This submits the changes made to a staff record///////////////////////////////////////
	function update_staff(obj){
		  //commit changes to the database and return status code.
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;  
			obj.elements['editbutton'].disabled = true
			obj.elements['deletebutton'].disabled = true
			populatestaff(document.getElementById('staffnames'));
			document.getElementById('photo').innerHTML='';
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = formData2QueryString(obj);
		  clear_textboxes(obj);
		  obj.elements['staffnames'].selectedIndex = -1;
		  strajax.doPost('phpscripts/updatestaff.php',PostData, strhand, 'text');
	}
  ///////////////////////////////////////////This submits the changes made to a group record///////////////////////////////////////
	function update_group(obj){
		  //commit changes to the database and return status code.
		  strhand = function(str) {
			document.getElementById('response2').innerHTML=str;  
			obj.elements['groupeditbutton'].disabled = true
			obj.elements['groupdeletebutton'].disabled = true
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = "ID="+objForm.elements['groupnames'].value+"&NAME="+objForm.elements['groupname'].value+"&DESCRIPTIONS="+objForm.elements['descriptions'].value
		  //alert(PostData);
		  clear_textboxes(obj);
		  obj.elements['groupnames'].selectedIndex = -1;
		  strajax.doPost('phpscripts/updategroup.php',PostData, strhand, 'text');
	}
  ///////////////////////////////////////////This submits the changes made to a staff record///////////////////////////////////////
	function deletesavedorder(strID){
		  //commit changes to the database and return status code.
		  strhand = function(str) {
				  //then we have to redirect the window as they are now logged in.
				  window.location = "orders.php";
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = "U="+strID;
		  strajax.doPost('phpscripts/deleteorder.php',PostData, strhand, 'text');
	}
  ///////////////////////////////////////////This submits the changes made to a staff record///////////////////////////////////////
	function loginscript(obj){
		  //commit changes to the database and return status code.
		  strhand = function(str) {
			  if (str != "Error: Incorrect Login"){
				  //then we have to redirect the window as they are now logged in.
				  window.location = str;
			  } else {
				  document.getElementById('response').innerHTML=str;  
			  }
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = "U="+obj.elements['USERNAME'].value+"&P="+obj.elements['PASSWORD'].value
		  clear_textboxes(obj);
		  strajax.doPost('phpscripts/loginscript.php',PostData, strhand, 'text');
	}
  ///////////////////////////////////////////this is to delete a staff record.///////////////////////////////////////////
	function delete_staff(obj){
		strhand = function(str) {
			document.getElementById('response').innerHTML=str;  
			obj.elements['editbutton'].disabled = true;
			obj.elements['deletebutton'].disabled = true;
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = formData2QueryString(obj);
		  clear_textboxes(obj);
		  obj.elements['staffnames'].options[obj.elements['staffnames'].selectedIndex] = null;
		  obj.elements['areacombo'].selectedIndex = 0;
		  obj.elements['staffnames'].selectedIndex = -1;
		  strajax.doPost('phpscripts/deletestaff.php',PostData, strhand, 'text');

	}
  ///////////////////////////////////////////this is to delete a staff record.///////////////////////////////////////////
	function delete_group(obj){
		strhand = function(str) {
			document.getElementById('response2').innerHTML=str;  
			obj.elements['groupeditbutton'].disabled = true;
			obj.elements['groupdeletebutton'].disabled = true;
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = formData2QueryString(obj);
		  clear_textboxes(obj);
		  obj.elements['groupnames'].options[obj.elements['groupnames'].selectedIndex] = null;
		  strajax.doPost('phpscripts/deletegroup.php',PostData, strhand, 'text');

	}
  ///////////////////////////////////////////this populates the staf list box.///////////////////////////////////////////
	function populatestaff(obj){
		//grabs all the staff and populates the select box on the right side of the form.
		//first clear it if it is populated
		obj.selectedIndex = -1;
		if (obj.length>0){
			obj.length=0;
		}
		  xmlhand = function(xml) {
				res = XMLParse.xml2ObjArray(xml, 'record');
				for (var i = 0;i<res.length;i++){
					obj.options[obj.length] = new Option(res[i].staff_NAME,res[i].PK_STAFF);
				}
		  }
		  var xmlajax = new Ajax();
		  xmlajax.setMimeType('text/xml');
		  xmlajax.doGet('phpscripts/populatestaff.php', xmlhand, 'xml');
	}
  ///////////////////////////////////////////this populates the groups list box.///////////////////////////////////////////
	function populategroups(obj){
		//grabs all the groups and populates the select box on the right side of the form.
		//first clear it if it is populated
		obj.selectedIndex = -1;
		var objlen = obj.length;
		if (obj.length>0){
				obj.length=0;
			}
		  xmlhand = function(xml) {
				res = XMLParse.xml2ObjArray(xml, 'record');
				for (var i = 0;i<res.length;i++){
					obj.options[obj.length] = new Option(res[i].group_NAME,res[i].PK_GROUPS);
				}
				//alert("this should now be blank");
		  }
		  var xmlajax = new Ajax();
		  xmlajax.setMimeType('text/xml');
		  xmlajax.doGet('phpscripts/populategroup.php', xmlhand, 'xml');
	}
  ///////////////////////////////////////////this populates the list box for the groups//////////////////////////////////////////
	function populategroup(obj){
		obj.selectedIndex = -1;
		//clear out the object first
		if (obj.length>0){
			obj.length=0;
		}
		  xmlhand = function(xml) {
				res = XMLParse.xml2ObjArray(xml, 'record');
				for (var i = 0;i<res.length;i++){
					obj.options[obj.length] = new Option(res[i].group_NAME,res[i].PK_GROUPS);
				}
		  }
		  var xmlajax = new Ajax();
		  xmlajax.setMimeType('text/xml');
		  xmlajax.doGet('phpscripts/populategroup.php', xmlhand, 'xml');

	}
  ///////////////////////////////////////////this initialises the staff form.///////////////////////////////////////////
	function init_form(objStaff,objGroups){
		populatestaff(objStaff);
		populategroup(objGroups);
		populategroups(document.getElementById('groupnames'));
	}
  ///////////////////////////////////////////this enables the submit on the staff form.///////////////////////////////////////////
	function submitenable(){
		objForm = document.getElementById('staffform');
		if(objForm.elements['editbutton'].disabled == true){
			if (objForm.elements['name'].value !='' && objForm.elements['position'].value !='' && objForm.elements['group'].value !='' && objForm.elements['phone'].value !='' && objForm.elements['email'].value !=''){
				objForm.elements['savebutton'].disabled = false;
			}
		}
	}
  ///////////////////////////////////////////this enables the submit on the group form.///////////////////////////////////////////
	function groupsubmitenable(){
		objForm = document.getElementById('groupsform');
		if(objForm.elements['groupeditbutton'].disabled == true){
			if (objForm.elements['groupname'].value !='' && objForm.elements['descriptions'].value !=''){
				objForm.elements['groupsavebutton'].disabled = false;
			}
		}
	}
	
	function wait_commit(){
		document.getElementById('response').innerHTML= "Waiting....";
		
	}
  ///////////////////////////////////////////this adds a new staff record///////////////////////////////////////////
	function add_staff(objForm){
		objForm.elements['savebutton'].disabled = true;
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;
			init_form(init_form(document.getElementById('staffnames'),document.getElementById('group')));
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = formData2QueryString(objForm);
		  clear_textboxes(objForm);
		  strajax.doPost('phpscripts/addstaff.php',PostData, strhand, 'text');
	  }
  ///////////////////////////////////////////this initialises the distro reports form///////////////////////////////////////////
	function init_distro(){
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = "";
		  strajax.doPost('phpscripts/initdistro.php',PostData, strhand, 'text');
	  }
	    ///////////////////////////////////////////this initialises the distro reports form///////////////////////////////////////////
	function init_staff_distro(){
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = "";
		  strajax.doPost('phpscripts/initstaffdistro.php',PostData, strhand, 'text');
	  }
  ///////////////////////////////////////////this initialises the distro reports form///////////////////////////////////////////
	function fetch_distro(objForm){
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = formData2QueryString(objForm);
		  strajax.doPost('phpscripts/fetchdistro.php',PostData, strhand, 'text');
	  }
	    ///////////////////////////////////////////this initialises the distro reports form///////////////////////////////////////////
	function fetch_staff_distro(objForm){
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = formData2QueryString(objForm);
		  strajax.doPost('phpscripts/fetchstaffdistro.php',PostData, strhand, 'text');
	  }
	  
	  	    ///////////////////////////////////////////this initialises the log reports form///////////////////////////////////////////
	function staff_visit_log(objForm,type){
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = formData2QueryString(objForm) + "&type=" + type;
		  strajax.doPost('phpscripts/fetchstafflogs.php',PostData, strhand, 'text');
	  }
	  

///////////////////////////////////////////this adds a new staff record///////////////////////////////////////////
	function add_group(objForm){
		objForm.elements['groupsavebutton'].disabled = true;
		  strhand = function(str) {
			document.getElementById('response2').innerHTML=str;  
			populategroups(document.getElementById('groupnames'));
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = '';
		  PostData +="NAME="+objForm.elements['groupname'].value+"&DESCRIPTIONS="+objForm.elements['descriptions'].value
		  clear_textboxes(objForm);
		  strajax.doPost('phpscripts/addgroup.php',PostData, strhand, 'text');	
	  }
  ///////////////////////////////////////////this gets the stock lisr//////////////////////////////////////////
	function resetdrawingsform(objForm){
		document.getElementById('productcode').value = '';
		getdrawings(objForm);	
	}
  ///////////////////////////////////////////this gets the stock lisr//////////////////////////////////////////
	function getdrawings(obj){
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;  
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = formData2QueryString(obj);
		  strajax.doPost('phpscripts/getdrawings.php',PostData, strhand, 'text');
	}
  ///////////////////////////////////////////this gets the stock lisr//////////////////////////////////////////
	function resetstaffstockform(objForm){
		document.getElementById('productcode').value = '';
		getstaffstock(objForm);	
	}
  ///////////////////////////////////////////this gets the stock lisr//////////////////////////////////////////
	function resetstockform(objForm){
		document.getElementById('productcode').value = '';
		getstock(objForm);	
	}
  ///////////////////////////////////////////this gets the stock lisr//////////////////////////////////////////
	function getstock(obj){
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;  
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = formData2QueryString(obj);
		  strajax.doPost('phpscripts/getstock.php',PostData, strhand, 'text');
	}
	  ///////////////////////////////////////////this gets the stock lisr//////////////////////////////////////////
	function resetproductsform(objForm){
		document.getElementById('productcode').value = '';
		document.getElementById('response').innerHTML = '';	
	}
  ///////////////////////////////////////////this gets the stock lisr//////////////////////////////////////////
	function getproducts(obj){
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;  
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = formData2QueryString(obj);
		  strajax.doPost('phpscripts/getproducts.php',PostData, strhand, 'text');
	}  ///////////////////////////////////////////this gets the list of opportunities///////////////////////////////////////////
		function getopportunities(){
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;  
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  strajax.doGet('phpscripts/getopportunities.php', strhand, 'text');
	}
		  ///////////////////////////////////////////this gets the list of opportunities///////////////////////////////////////////
		function getexportopportunities(){
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;  
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  strajax.doGet('phpscripts/getexportopportunities.php', strhand, 'text');
	}

  ///////////////////////////////////////////this gets the list of opportunities///////////////////////////////////////////
		function requestpassword(obj){
		  strhand = function(str) {
			document.getElementById('response').innerHTML="Thankyou, your password has been emailed to the address given";  
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  strPostData = "USERNAME="+obj.elements['USERNAME'].value;
		  strajax.doPost('phpscripts/requestpassword.php', strPostData, strhand, 'text');
	}
  ///////////////////////////////////////////this gets the list of stock for charlotte/////////////////////////////////////
		function getstaffstock(obj){
		  document.getElementById('response').innerHTML="Searching...."; 
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;  
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var PostData = formData2QueryString(obj);
		  strajax.doPost('phpscripts/getstaffstock.php', PostData, strhand, 'text');
	}
	  ///////////////////////////////////////////this gets the list of stock for charlotte/////////////////////////////////////
		function getordersummary(){
			getcompletedorders();
 			getsavedorders();
	}
	  ///////////////////////////////////////////this gets the list of stock for charlotte/////////////////////////////////////
		function getcompletedorders(){
		  strhand = function(str) {
			document.getElementById('completedorders').innerHTML=str;  
		  }
		  var strajax = new Ajax();
		  var PostData = "";
		  strajax.setMimeType('text/text');
		  strajax.doPost('phpscripts/getcompletedorderlist.php', PostData, strhand, 'text');
	}
		  ///////////////////////////////////////////this gets the list of stock for charlotte/////////////////////////////////////
		function getsavedorders(){
		  strhand = function(str) {
			document.getElementById('savedorders').innerHTML=str;  
		  }
		  var strajax = new Ajax();
		  var PostData = "";
		  strajax.setMimeType('text/text');
		  strajax.doPost('phpscripts/getsavedorderlist.php', PostData, strhand, 'text');
	}

  //////////////////////////this puts selected opportunities into the temp table and opens the order form/////////////////
	function stockoppTransfer(obj){
		var strPostData = '';
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		for (var i = 0; i < obj.length; i++){
				if (obj.elements[i].type == "checkbox"){
					if (obj.elements[i].checked == true){
						strPostData = strPostData+"ID"+i+"="+obj.elements[i].value+"&";
					}
				}
		 }
		 strhand = function(str) {
			 //document.getElementById('response').innerHTML=str; 
			window.location =str;  
		  }
		 if (strPostData != ''){
			 strajax.doPost('phpscripts/transferstockopps.php',strPostData, strhand, 'text');
			 //window.location = "orderform.php?ACTION=TRANS"
		 }
	}
	  //////////////////////////this puts selected opportunities into the temp table and opens the order form/////////////////
	function stockoppTransferexport(obj){
		var strPostData = '';
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		for (var i = 0; i < obj.length; i++){
				if (obj.elements[i].type == "checkbox"){
					if (obj.elements[i].checked == true){
						strPostData = strPostData+"ID"+i+"="+obj.elements[i].value+"&";
					}
				}
		 }
		 strhand = function(str) {
			 //document.getElementById('response').innerHTML=str; 
			window.location =str;  
		  }
		 if (strPostData != ''){
			 strajax.doPost('phpscripts/transferstockoppsexport.php',strPostData, strhand, 'text');
			 //window.location = "orderform.php?ACTION=TRANS"
		 }
	}

  //////////////////////////enables the right ones for the opps list/////////////////
	function updateOpportunities(obj){
		var strPostData = '';
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		for (var i = 0; i < obj.length; i++){
				if (obj.elements[i].type == "checkbox"){
					if (obj.elements[i].checked == true){
						strPostData = strPostData+"ID"+i+"="+obj.elements[i].value+"&";
					}
				}
		 }
		 strhand = function(str) {
			document.getElementById('response').innerHTML=str;  
		  }
		 if (strPostData == ''){
		 	//then everything is unchecked.
		 	strPostData = strPostData+"ID=NONE&";
		 	}
		 	
 	         strPostData = strPostData + "searched=" + document.getElementById('search').value;
			 strajax.doPost('phpscripts/updateOpportunities.php',strPostData, strhand, 'text');
			 //window.location = "staffarea.php"
		 //}
	}
  ///////////////////////////////////////////this enables the order form submit button//////////////////////////////////////////
	function enableordersubmit(obj){
		if (obj.elements['tandccheckbox'].checked == true){
			obj.elements['submitorderbutton'].disabled = false;
		} else {
			obj.elements['submitorderbutton'].disabled = true;
		}
	}
     //////////////this is called when a user chooses a product from the dropdown /////////////////////////////////////
	 ////////////////list of the order form. basically populates the boxes.///////////////////////////////////////////
	function echoproductdetails(record,row){
			var xmlajax = new Ajax();
		  	xmlajax.setMimeType('text/xml');
			//first we get the address details and populate these fields
		  	xmlAddresshandler = function(xml) 
			{
				//alert(document.getElementById('hiddenalpha'+row).value);
				res = XMLParse.xml2ObjArray(xml, 'record');
				if (document.getElementById('formtype').value == "sample"){
					document.getElementById('codecell'+row).innerHTML = res[0].prod_CODE.substr(0,4);
				} else {
					document.getElementById('codecell'+row).innerHTML = res[0].prod_CODE;
				}
				//document.getElementById('codecell'+row).innerHTML = res[0].prod_CODE;
				document.getElementById('hiddenalpha'+row).value = res[0].prod_CODE;
				document.getElementById('descriptioncell'+row).value = res[0].prod_NAME;
				document.getElementById('pppcell'+row).value = res[0].prod_PPP;
		  	}
			var strPostData = "ID="+record;
			xmlajax.doPost('phpscripts/getproductdetail.php', strPostData, xmlAddresshandler, 'xml');
	}
		function echosampleproductdetails(record,row){
			var xmlajax = new Ajax();
		  	xmlajax.setMimeType('text/xml');
			//first we get the address details and populate these fields
		  	xmlAddresshandler = function(xml) 
			{
				//alert(document.getElementById('hiddenalpha'+row).value);
				res = XMLParse.xml2ObjArray(xml, 'record');
				//alert(res[0].prod_CODE);
				document.getElementById('alpha'+row).value = res[0].prod_CODE.substr(0,4);
				document.getElementById('hiddenalpha'+row).value = res[0].prod_CODE.substr(0,4);
				document.getElementById('descriptioncell'+row).value = res[0].prod_NAME;
		  	}
			var strPostData = "ID="+record;
			xmlajax.doPost('phpscripts/getproductdetail.php', strPostData, xmlAddresshandler, 'xml');
	}

     //////////////this is used to clean fields /////////////////////////////////////
	 function undefinedclean(str){
		 if (str.substring(0,9)=="undefined"){
			 return " ";
		 } else {
			 return str;
		 }
	 }
	 ////////////////list of the order form. basically populates the address///////////////////////////////////////////
	function echodeliveryaddress(record){
			document.getElementById('addressid').value = record;
			var xmlajax = new Ajax();
		  	xmlajax.setMimeType('text/xml');
			//first we get the address details and populate these fields
		  	xmlAddresshandler = function(xml) 
			{
				res = XMLParse.xml2ObjArray(xml, 'record');
				document.getElementById('deliveryaddress').innerHTML = undefinedclean(res[0].shipto_COMPANY+"<BR>")+undefinedclean(res[0].shipto_ADD1+"<BR>")+undefinedclean(res[0].shipto_ADD2+"<BR>")+undefinedclean(res[0].shipto_ADD3+"<BR>")+undefinedclean(res[0].shipto_CITY+"<BR>")+undefinedclean(res[0].shipto_POSTCODE+"    ");
		  	}
			var strPostData = "ID="+record;
			xmlajax.doPost('phpscripts/getsingledeliveryaddress.php', strPostData, xmlAddresshandler, 'xml');
	}
	 ////////////////////////////////this function initialises the order form/////////////////////////////////////////
	function initorderform(){	
			//document.getElementById('alphaamount').value = parseInt(document.getElementById('alphaamount').value) + 1;
			var xmlajax = new Ajax();
		  	xmlajax.setMimeType('text/xml');
			//first we get the address details and populate these fields
		  	xmlAddresshandler = function(xml) 
			{
				res = XMLParse.xml2ObjArray(xml, 'record');
				document.getElementById('accountnumber').innerHTML = res[0].cust_ACCNUM;
				document.getElementById('ACCNUM').value = res[0].cust_ACCNUM;
				document.getElementById('address').innerHTML = undefinedclean(res[0].cust_NAME+"<BR>")+undefinedclean(res[0].cust_ADD1+"<BR>")+undefinedclean(res[0].cust_ADD2+"<BR>")+undefinedclean(res[0].cust_ADD3+"<BR>")+undefinedclean(res[0].cust_ADD4+"<BR>")+undefinedclean(res[0].cust_TOWN+"<BR>")+undefinedclean(res[0].cust_POSTCODE);
			}
			xmlajax.doGet('phpscripts/getcustomeraddress.php', xmlAddresshandler, 'xml');
			
			//add ten lines to the order form
			for (i=1;i<3;i++){addorderlines();}
			//finally we populate the order form if there is any stock in the temp table and clear the temp table out
	}
	 ////////////////////////////////this function initialises the exportorder form/////////////////////////////////////////
	function initexportorderform(){		  
			var xmlajax = new Ajax();
		  	xmlajax.setMimeType('text/xml');
			//first we get the address details and populate these fields
		  	xmlAddresshandler = function(xml) 
			{
				res = XMLParse.xml2ObjArray(xml, 'record');
				document.getElementById('accountnumber').innerHTML = res[0].cust_ACCNUM;
				document.getElementById('ACCNUM').value = res[0].cust_ACCNUM;
				document.getElementById('address').innerHTML = undefinedclean(res[0].cust_NAME+"<BR>")+undefinedclean(res[0].cust_ADD1+"<BR>")+undefinedclean(res[0].cust_ADD2+"<BR>")+undefinedclean(res[0].cust_ADD3+"<BR>")+undefinedclean(res[0].cust_ADD4+"<BR>")+undefinedclean(res[0].cust_TOWN+"<BR>")+undefinedclean(res[0].cust_POSTCODE);
			}
			xmlajax.doGet('phpscripts/getcustomeraddress.php', xmlAddresshandler, 'xml');
			
			//add ten lines to the order form
			for (i=1;i<3;i++){addexportorderlines();}
			//finally we populate the order form if there is any stock in the temp table and clear the temp table out
	}
	 ////////////////////////////////this function initialises the sample order form///////////////////////////////////////
		function initsampleorderform(){		  
			var xmlajax = new Ajax();
		  	xmlajax.setMimeType('text/xml');
			//first we get the address details and populate these fields
		  	xmlAddresshandler = function(xml) 
			{
				res = XMLParse.xml2ObjArray(xml, 'record');
				document.getElementById('accountnumber').innerHTML = res[0].cust_ACCNUM;
				document.getElementById('ACCNUM').value = res[0].cust_ACCNUM;
				document.getElementById('address').innerHTML = res[0].cust_NAME+"<BR>"+res[0].cust_ADD1+"<BR>"+res[0].cust_ADD2+"<BR>"+res[0].cust_ADD3+"<BR>"+res[0].cust_ADD4+"<BR>"+res[0].cust_TOWN+"<BR>"+res[0].cust_POSTCODE;
			}
			xmlajax.doGet('phpscripts/getcustomeraddress.php', xmlAddresshandler, 'xml');
			
			//add ten lines to the order form
			for (i=1;i<2;i++){addsampleorderlines();}
			//finally we populate the order form if there is any stock in the temp table and clear the temp table out
	}
	/////////////////////////////////////////////
	function clearsavedorder(id){
			var xmlajax = new Ajax();
		  	xmlajax.setMimeType('text/xml');
		  	strhand = function(str) 
			{
				return true;
			}
			xmlajax.doGet('phpscripts/clearorder.php?ID='+id, strhand, 'text');
		
	}
	////////////////////////////
		function submitexportorderfromsave(obj,saved){
		if (saved == '0'){//now we check that all the fields are filled in.
		if (obj.elements['POnumber'].value == ''){
			alert("Please enter a Purchase Order Number");
			return;
		}
		if (obj.elements['addressid'].value == ''){
			alert("Please Select a delivery address");
			return;
		}
		if (obj.elements['hiddenalpha1'].value == ''){
			alert("Please Order at least one product");
			return;
		} else {
			//check that they have filled in the other fields.
			if (obj.elements['quantitycell1'].value == ''){
				alert("Please enter a Price and quantity to complete the order");
				return;
			}
		}
		}
		var dte = new Date();
		clearsavedorder(obj.elements['ORDERID'].value);
		//if we get here then they have done the entire order correctly
		strPostData = "PO="+obj.elements['POnumber'].value+"&";
		strPostData = strPostData+"ADDRESS="+obj.elements['addressid'].value+"&";
		strPostData = strPostData+"SPECIAL="+obj.elements['specialinstructions'].value+"&";
		strPostData = strPostData+"ORDERDATE="+obj.elements['orderdate'].value+"&";
		strPostData = strPostData+"THISDATE="+dte.getFullYear()+"-"+(dte.getMonth()+1)+"-"+dte.getDate()+"&";
		strPostData = strPostData+"CLIENT="+document.getElementById('ACCNUM').value;
		
		  strhand = function(str) {
			//need to get str out of here and into a global variable
			//--------------------------------------------------------------------
			//saveorderline(obj,str)
				for (var i = 1; i <= obj.elements['alphaamount'].value; i++){
					//alert(obj.elements['alphaamount'].value - 1);
						if (obj.elements['hiddenalpha'+i].value != ""){
								//by this point it is selecting only the alphas with values.
								//now get the ID of that one and check they have filled in all the details in the quantity
								intRowposition = i;
								var strPostData2 = '';
								//if (obj.elements['quantitycell'+intRowposition].value != ''){
									//then add this to the second post string as a product to order.
									strPostData2 += "ID="+str+"&";
									strPostData2 += "CODE="+obj.elements['hiddenalpha'+intRowposition].value+"&";
									strPostData2 += "DESC="+obj.elements['descriptioncell'+intRowposition].value+"&";
									//strPostData2 += "PPK="+obj.elements['ppkcell'+intRowposition].value+"&";
									strPostData2 += "PPP="+obj.elements['pppcell'+intRowposition].value+"&";
									strPostData2 += "QUANTITY="+obj.elements['quantitycell'+intRowposition].value;
									//now stick this record in the database.
									  strhand2 = function(str) {							
										//alert("This is the one that has just been submitted "+i);
									  }
									  var strajax2 = new Ajax();
									  strajax2.setMimeType('text/text');
									  strajax2.doPost('phpscripts/saveexportorderline.php',strPostData2, strhand2, 'text');
								//}
							}	
				}
				//get the sales person for that area and email them the details
				//sendsamplerequestemail(strEmail);
				if (saved == '0'){
					strhand3 = function(str){
					document.getElementById('response').innerHTML="<BR><BR><BR><div align=\"center\"><span class=\"thankyou\">Thank you for placing your order with us, it has now been sent to our sales team for processing. If you require any further information regarding your order please contact us on 01709 835 388.</span></div>";
					}
					strajax.doGet('phpscripts/emailorder.php?ID='+str+"&ADDRESS="+obj.elements['addressid'].value, strhand3, 'text');
				} else {
					//alert("Finished");
					window.location="orders.php";	
				}
			//--------------------------------------------------------------------
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		   strPostData = strPostData+"&SAVED="+saved;
		  strajax.doPost('phpscripts/saveordermain.php',strPostData, strhand, 'text');
	}
	function resend_order(str){
		  strhand = function(str) {
			alert ("order sent");//do nothing cos we have finished here so we return back to the last function
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  strajax.doGet('phpscripts/emailorder.php?ID='+str, strhand, 'text');
	 
	}
	 ////////////////////////////////this function submits the order and checks the form///////////////////////////////////////
	function submitexportorder(obj,saved){
	  if (saved == '0'){//now we check that all the fields are filled in.
		if (obj.elements['POnumber'].value == ''){
			alert("Please enter a Purchase Order Number");
			return;
		}
		if (obj.elements['addressid'].value == ''){
			alert("Please Select a delivery address");
			return;
		}
		if (obj.elements['hiddenalpha1'].value == ''){
			alert("Please Order at least one product");
			return;
		} else {
			//check that they have filled in the other fields.
			if (obj.elements['quantitycell1'].value == ''){
				alert("Please enter a quantity to complete the order");
				return;
			}
		}
	  }
		var dte = new Date();
		//if we get here then they have done the entire order correctly
		strPostData = "PO="+obj.elements['POnumber'].value+"&";
		strPostData = strPostData+"ADDRESS="+obj.elements['addressid'].value+"&";
		strPostData = strPostData+"SPECIAL="+obj.elements['specialinstructions'].value+"&";
		strPostData = strPostData+"ORDERDATE="+obj.elements['orderdate'].value+"&";
		strPostData = strPostData+"THISDATE="+dte.getFullYear()+"-"+(dte.getMonth()+1)+"-"+dte.getDate()+"&";
		strPostData = strPostData+"CLIENT="+document.getElementById('ACCNUM').value;
		
		  strhand = function(str) {
			//need to get str out of here and into a global variable
			//--------------------------------------------------------------------
			//saveorderline(obj,str)
			//alert("We have this much"+obj.elements['alphaamount'].value);
				for (var i = 1; i <= obj.elements['alphaamount'].value; i++){
					//alert(obj.elements['alphaamount'].value - 1);
						//alert("saving order line"+i);
						if (obj.elements['hiddenalpha'+i].value != ""){
								//by this point it is selecting only the alphas with values.
								//now get the ID of that one and check they have filled in all the details in the quantity
								
								intRowposition = i;
								var strPostData2 = '';
								if (obj.elements['quantitycell'+intRowposition].value != ''){
									//then add this to the second post string as a product to order.
									strPostData2 += "ID="+str+"&";
									strPostData2 += "CODE="+obj.elements['hiddenalpha'+intRowposition].value+"&";
									strPostData2 += "DESC="+obj.elements['descriptioncell'+intRowposition].value+"&";
									//strPostData2 += "PPK="+obj.elements['ppkcell'+intRowposition].value+"&";
									strPostData2 += "PPP="+obj.elements['pppcell'+intRowposition].value+"&";
									strPostData2 += "QUANTITY="+obj.elements['quantitycell'+intRowposition].value;
									//now stick this record in the database.
									  strhand2 = function(str) {							
										//alert("This is the one that has just been submitted "+i);
									  }
									  var strajax2 = new Ajax();
									  strajax2.setMimeType('text/text');
									  strajax2.doPost('phpscripts/saveexportorderline.php',strPostData2, strhand2, 'text');
								}
							}	
				}
				//get the sales person for that area and email them the details
				//sendsamplerequestemail(strEmail);
				if (saved == '0'){
					strhand3 = function(str){
					document.getElementById('response').innerHTML="<BR><BR><BR><div align=\"center\"><span class=\"thankyou\">Thank you for placing your order with us, it has now been sent to our sales team for processing. If you require any further information regarding your order please contact us on 01709 835 388.</span></div>";
					}
					strajax.doGet('phpscripts/emailorder.php?ID='+str+"&ADDRESS="+obj.elements['addressid'].value, strhand3, 'text');
				} else {
					window.location="orders.php";	
				}

			//--------------------------------------------------------------------
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  strPostData = strPostData+"&SAVED="+saved;
		  strajax.doPost('phpscripts/saveordermain.php',strPostData, strhand, 'text');
	}
	 
	 ////////////////////////////////this function submits the order and checks the form///////////////////////////////////////
	function submitorder(obj,saved){
		if (saved == '0'){
			//now we check that all the fields are filled in.
			if (obj.elements['POnumber'].value == ''){
				alert("Please enter a Purchase Order Number");
				return;
			}
			if (obj.elements['addressid'].value == ''){
				alert("Please Select a delivery address");
				return;
			}
			if (obj.elements['hiddenalpha1'].value == ''){
				alert("Please Order at least one product");
				return;
			} else {
				//check that they have filled in the other fields.
				if (obj.elements['quantitycell1'].value == ''){
					alert("Please enter a Price and quantity to complete the order");
					return;
				}
			}			
		}
		var dte = new Date();
		//if we get here then they have done the entire order correctly
		strPostData = "PO="+obj.elements['POnumber'].value+"&";
		strPostData = strPostData+"ADDRESS="+obj.elements['addressid'].value+"&";
		strPostData = strPostData+"SPECIAL="+obj.elements['specialinstructions'].value+"&";
		strPostData = strPostData+"ORDERDATE="+obj.elements['orderdate'].value+"&";
		strPostData = strPostData+"THISDATE="+dte.getFullYear()+"-"+(dte.getMonth()+1)+"-"+dte.getDate()+"&";
		strPostData = strPostData+"CLIENT="+document.getElementById('ACCNUM').value;
		
		  strhand = function(str) {
			//need to get str out of here and into a global variable
			//--------------------------------------------------------------------
			//saveorderline(obj,str)
			
				for (var i = 1; i <= obj.elements['alphaamount'].value; i++){
					//alert(obj.elements['alphaamount'].value - 1);
						if (obj.elements['hiddenalpha'+i].value != ""){
								//by this point it is selecting only the alphas with values.
								//now get the ID of that one and check they have filled in all the details in the quantity
								intRowposition = i;
								var strPostData2 = '';
								if (obj.elements['quantitycell'+intRowposition].value != ''){
									//then add this to the second post string as a product to order.
									strPostData2 += "ID="+str+"&";
									strPostData2 += "CODE="+obj.elements['hiddenalpha'+intRowposition].value+"&";
									strPostData2 += "DESC="+obj.elements['descriptioncell'+intRowposition].value+"&";
									strPostData2 += "PPK="+obj.elements['ppkcell'+intRowposition].value+"&";
									strPostData2 += "PPP="+obj.elements['pppcell'+intRowposition].value+"&";
									strPostData2 += "QUANTITY="+obj.elements['quantitycell'+intRowposition].value;
									//now stick this record in the database.
									  strhand2 = function(str) {							
										//alert("This is the one that has just been submitted "+i);
									  }
									  var strajax2 = new Ajax();
									  strajax2.setMimeType('text/text');
									  strajax2.doPost('phpscripts/saveorderline.php',strPostData2, strhand2, 'text');
								}
							}	
				}
				//get the sales person for that area and email them the details
				//if this is a real order then send otherwise skip the email.
				if (saved == '0'){
					strhand3 = function(str){
					document.getElementById('response').innerHTML="<BR><BR><BR><div align=\"center\"><span class=\"thankyou\">Thank you for placing your order with us, it has now been sent to our sales team for processing. If you require any further information regarding your order please contact us on 01709 835 388.</span></div>";
					}
					strajax.doGet('phpscripts/emailorder.php?ID='+str+"&ADDRESS="+obj.elements['addressid'].value, strhand3, 'text');
				} else {
					//alert("Finished");
					window.location="orders.php";	
				}
			//--------------------------------------------------------------------
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  strPostData = strPostData+"&SAVED="+saved;
		  strajax.doPost('phpscripts/saveordermain.php',strPostData, strhand, 'text');
		//now loop around the products and see which ones are populated.
	}
		 ////////////////////////////////this function submits the order and checks the form///////////////////////////////////////
	function submitsavedorder(obj,saved){
		if (saved == '0'){
			//now we check that all the fields are filled in.
			if (obj.elements['POnumber'].value == ''){
				alert("Please enter a Purchase Order Number");
				return;
			}
			if (obj.elements['addressid'].value == ''){
				alert("Please Select a delivery address");
				return;
			}
			if (obj.elements['hiddenalpha1'].value == ''){
				alert("Please Order at least one product");
				return;
			} else {
				//check that they have filled in the other fields.
				if (obj.elements['quantitycell1'].value == ''){
					alert("Please enter a Price and quantity to complete the order");
					return;
				}
			}			
		}
		var dte = new Date();
		clearsavedorder(obj.elements['ORDERID'].value);
		//if we get here then they have done the entire order correctly
		alert("Clearing out "+obj.elements['ORDERID'].value);
		strPostData = "PO="+obj.elements['POnumber'].value+"&";
		strPostData = strPostData+"ADDRESS="+obj.elements['addressid'].value+"&";
		strPostData = strPostData+"SPECIAL="+obj.elements['specialinstructions'].value+"&";
		strPostData = strPostData+"ORDERDATE="+obj.elements['orderdate'].value+"&";
		strPostData = strPostData+"THISDATE="+dte.getFullYear()+"-"+(dte.getMonth()+1)+"-"+dte.getDate()+"&";
		strPostData = strPostData+"CLIENT="+document.getElementById('ACCNUM').value;
		
		  strhand = function(str) {
			//need to get str out of here and into a global variable
			//--------------------------------------------------------------------
			//saveorderline(obj,str)
				for (var i = 1; i <= obj.elements['alphaamount'].value; i++){
					//alert(obj.elements['alphaamount'].value - 1);
						if (obj.elements['hiddenalpha'+i].value != ""){
								//by this point it is selecting only the alphas with values.
								//now get the ID of that one and check they have filled in all the details in the quantity
								intRowposition = i;
								var strPostData2 = '';
								if (obj.elements['quantitycell'+intRowposition].value != ''){
									//then add this to the second post string as a product to order.
									strPostData2 += "ID="+str+"&";
									strPostData2 += "CODE="+obj.elements['hiddenalpha'+intRowposition].value+"&";
									strPostData2 += "DESC="+obj.elements['descriptioncell'+intRowposition].value+"&";
									strPostData2 += "PPK="+obj.elements['ppkcell'+intRowposition].value+"&";
									strPostData2 += "PPP="+obj.elements['pppcell'+intRowposition].value+"&";
									strPostData2 += "QUANTITY="+obj.elements['quantitycell'+intRowposition].value;
									//now stick this record in the database.
									  strhand2 = function(str) {							
										//alert("This is the one that has just been submitted "+i);
									  }
									  var strajax2 = new Ajax();
									  strajax2.setMimeType('text/text');
									  strajax2.doPost('phpscripts/saveorderline.php',strPostData2, strhand2, 'text');
								}
							}	
				}
				//get the sales person for that area and email them the details
				//sendsamplerequestemail(strEmail);
				if (saved == '0'){
					strhand3 = function(str){
					document.getElementById('response').innerHTML="<BR><BR><BR><div align=\"center\"><span class=\"thankyou\">Thank you for placing your order with us, it has now been sent to our sales team for processing. If you require any further information regarding your order please contact us on 01709 835 388.</span></div>";
					}
					strajax.doGet('phpscripts/emailorder.php?ID='+str+"&ADDRESS="+obj.elements['addressid'].value, strhand3, 'text');
				} else {
					//alert("Finished");
					window.location="orders.php";	
				}
			//--------------------------------------------------------------------
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  strPostData = strPostData+"&SAVED="+saved;
		  strajax.doPost('phpscripts/saveordermain.php',strPostData, strhand, 'text');
		//now loop around the products and see which ones are populated.
	}

	 ////////////////////////////////this function submits the order and checks the form///////////////////////////////////////
	function submitsampleorder(obj){
		//now we check that all the fields are filled in.
		if (obj.elements['hiddenalpha1'].value == ''){
			alert("Please Order at least one sample");
			return;
		} else {
			//check that they have filled in the other fields.
			if (obj.elements['quantitycell1'].value == ''){
				alert("Please enter a quantity to complete the request");
				return;
			}
		}
		//if we get here then they have done the entire order correctly
		var strPostData = '';

		strPostData += "ADDRESS="+obj.elements['deliveryaddress'].value.replace(/\r|\n|\r\n/g, "***")+"&";
		strPostData += "SPECIAL="+obj.elements['specialinstructions'].value+"&";
		strPostData += "ORDERDATE="+obj.elements['orderdate'].value+"&";
		strPostData += "THISDATE="+"date"+"&";
		strPostData += "CLIENT="+document.getElementById('ACCNUM').value;
		//alert(strPostData);
		  strhand = function(str) {
			//need to get str out of here and into a global variable
			//--------------------------------------------------------------------
			//savesampleorderlines(obj,str)
				for (var i = 1; i <= obj.elements['alphaamount'].value; i++){
					//alert(obj.elements['alphaamount'].value - 1);
						if (obj.elements['hiddenalpha'+i].value != ""){
								//by this point it is selecting only the alphas with values.
								//now get the ID of that one and check they have filled in all the details in the quantity
								intRowposition = i;
								var strPostData2 = '';
								if (obj.elements['quantitycell'+intRowposition].value != ''){
									//then add this to the second post string as a product to order.
									strPostData2 += "ID="+str+"&";
									strPostData2 += "CODE="+obj.elements['hiddenalpha'+intRowposition].value+"&";
									strPostData2 += "DESC="+obj.elements['descriptioncell'+intRowposition].value+"&";
									strPostData2 += "QUANTITY="+obj.elements['quantitycell'+intRowposition].value;
									//now stick this record in the database.
									  strhand2 = function(str) {							
										//alert("This is the one that has just been submitted "+i);
									  }
									  var strajax2 = new Ajax();
									  strajax2.setMimeType('text/text');
									  strajax2.doPost('phpscripts/savesampleorderline.php',strPostData2, strhand2, 'text');
								}
							}	
				}
				//get the sales person for that area and email them the details
				//sendsamplerequestemail(strEmail);
				strhand3 = function(str){
					document.getElementById('response').innerHTML="<BR><BR><BR><div align=\"center\"><span class=\"thankyou\">Thank you for placing your sample request with us, it has now been sent to our sales team for processing. If you require any further information regarding your sample request please contact us on 01709 835 388.</span></div>";
				}
				strajax.doGet('phpscripts/sendsamplerequestemail.php?ID='+str+"&ADDRESS="+obj.elements['deliveryaddress'].value.replace(/\r|\n|\r\n/g, "***"), strhand3, 'text');
				//document.getElementById('response').innerHTML=strEmail;  
				
			//--------------------------------------------------------------------
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  strajax.doPost('phpscripts/savesampleordermain.php',strPostData, strhand, 'text');
		//now loop around the products and see which ones are populated.
		
		
		
	}
	 /////////////////////////////this function sends the sample request email to the right area//////////////////////////////
	 function sendsamplerequestemail(strEmail){
		  strhand = function(str) {
			//do nothing cos we have finished here so we return back to the last function
		  }
		  var strPostData = "body="+strEmail;
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  strajax.doPost('phpscripts/sendsamplerequestemail.php',strPostData, strhand, 'text');
	 }
	 ////////////////////////////////this function adds rows to the order form////////////////////////////////////////
	function addorderlines(){
		  var tbl = document.getElementById('mainordertable');
		  var lastRow = tbl.rows.length;
		  // if theres no header row in the table, then iteration = lastRow + 1
		  var iteration = lastRow;
		  var row = tbl.insertRow(lastRow);
		  
		  // product code cell
		  var cellCode = row.insertCell(0);
		  document.getElementById('alphaamount').value = parseInt(document.getElementById('alphaamount').value) + 1;
		  //alert(document.getElementById('alphaamount').value);
		  //add a hidden cell to populate the product code.
		  var el = document.createElement('input');
		  el.type = 'hidden';
		  el.name = 'hiddenalpha' + iteration;
		  el.id = 'hiddenalpha' + iteration;
		  document.getElementById('orderform').appendChild(el);
		  
		  var el = document.createElement('div');
		  el.type = 'div';
		  //we set the position so we can reference the other elements from the call to opensearchwin
		  el.position = iteration;
		  el.id = 'codecell' + iteration;
		  el.innerHTML = "Search";
		  el.className = 'searchlinks';
		  if (el.attachEvent){
			  el.attachEvent('onclick',openSearchwin,false);
		  } else {
			  el.addEventListener('click',openSearchwin,false);
		  }
		  cellCode.appendChild(el);
		  //var productscombos = new dhtmlXCombo("codecell" + iteration,"alpha" + iteration,100);
		  //productscombos.enableFilteringMode(true,"phpscripts/fillproductcombo.php",true);
		  
		  // description cell
		  var cellDesc = row.insertCell(1);
		  var el = document.createElement('input');
		  el.disabled = true;
		  el.type = 'text';
		  el.className = 'descriptiontextboxes';//("class","textboxes");
		  el.name = 'descriptioncell' + iteration;
		  el.id = 'descriptioncell' + iteration;
		  cellDesc.appendChild(el);  
		  
		  // price per 1000 cell
		  var cellPPK = row.insertCell(2);
		  var el = document.createElement('input');
		  el.type = 'text';
		  el.value = '0';
		  el.className = 'ordertextboxes';
		  el.name = 'ppkcell' + iteration;
		  el.id = 'ppkcell' + iteration;
		  cellPPK.appendChild(el);
		  
		  //packs per pallet cell
		  var cellPPP = row.insertCell(3);
		  var el = document.createElement('input');
		  el.type = 'text';
	   	  el.className = 'ppptextboxes';
		  el.disabled = true;
		  el.name = 'pppcell' + iteration;
		  el.id = 'pppcell' + iteration;
		  cellPPP.appendChild(el);
		  
		  //pallet quantity cell
		  var cellQuantity = row.insertCell(4);
		  var el = document.createElement('input');
		  el.type = 'text';
		  el.value = '0';
	   	  el.className = 'quantitytextboxes';
		  el.name = 'quantitycell' + iteration;
		  el.id = 'quantitycell' + iteration;
		  cellQuantity.appendChild(el);
	}	////////////////////////////this function adds the order lines when they need to be populated
		function addsavedexportorderlines(productcode,description,ppk,ppp,pallets){
		  var tbl = document.getElementById('mainordertable');
		  var lastRow = tbl.rows.length;
		  // if theres no header row in the table, then iteration = lastRow + 1
		  var iteration = lastRow;
		  var row = tbl.insertRow(lastRow);
		  
		  // product code cell
		  var cellCode = row.insertCell(0);
		  document.getElementById('alphaamount').value = parseInt(document.getElementById('alphaamount').value) + 1;
		  //alert(document.getElementById('alphaamount').value);
		  //add a hidden cell to populate the product code.
		  var el = document.createElement('input');
		  el.type = 'hidden';
		  el.name = 'hiddenalpha' + iteration;
		  el.id = 'hiddenalpha' + iteration;
		  el.value = productcode;
		  document.getElementById('orderform').appendChild(el);
		  
		  var el = document.createElement('div');
		  el.type = 'div';
		  //we set the position so we can reference the other elements from the call to opensearchwin
		  el.position = iteration;
		  el.id = 'codecell' + iteration;
		  el.innerHTML = productcode;
		  el.className = 'searchlinks';
		  if (el.attachEvent){
			  el.attachEvent('onclick',openSearchwin,false);
		  } else {
			  el.addEventListener('click',openSearchwin,false);
		  }
		  cellCode.appendChild(el);
		  //var productscombos = new dhtmlXCombo("codecell" + iteration,"alpha" + iteration,100);
		  //productscombos.enableFilteringMode(true,"phpscripts/fillproductcombo.php",true);
		  
		  // description cell
		  var cellDesc = row.insertCell(1);
		  var el = document.createElement('input');
		  el.disabled = true;
		  el.type = 'text';
		  el.className = 'descriptiontextboxes';//("class","textboxes");
		  el.name = 'descriptioncell' + iteration;
		  el.id = 'descriptioncell' + iteration;
		  el.value = description;
		  cellDesc.appendChild(el);  
		  
		  /* price per 1000 cell
		  var cellPPK = row.insertCell(2);
		  var el = document.createElement('input');
		  el.type = 'text';
		  el.className = 'ordertextboxes';
		  el.name = 'ppkcell' + iteration;
		  el.id = 'ppkcell' + iteration;
		  el.value = ppk;
		  cellPPK.appendChild(el);*/
		  
		  //packs per pallet cell
		  var cellPPP = row.insertCell(2);
		  var el = document.createElement('input');
		  el.type = 'text';
	   	  el.className = 'ppptextboxes';
		  el.disabled = true;
		  el.name = 'pppcell' + iteration;
		  el.id = 'pppcell' + iteration;
		  el.value = ppp;
		  cellPPP.appendChild(el);
		  
		  //pallet quantity cell
		  var cellQuantity = row.insertCell(3);
		  var el = document.createElement('input');
		  el.type = 'text';
		  el.value = '0';
	   	  el.className = 'quantitytextboxes';
		  el.name = 'quantitycell' + iteration;
		  el.id = 'quantitycell' + iteration;
		  el.value = pallets;
		  cellQuantity.appendChild(el);
	}

	////////////////////////////this function adds the order lines when they need to be populated
		function addsavedorderlines(productcode,description,ppk,ppp,pallets){
		  //alert("successfully called the function");
		  var tbl = document.getElementById('mainordertable');
		  var lastRow = tbl.rows.length;
		  // if theres no header row in the table, then iteration = lastRow + 1
		  var iteration = lastRow;
		  var row = tbl.insertRow(lastRow);
		  
		  // product code cell
		  var cellCode = row.insertCell(0);
		  document.getElementById('alphaamount').value = parseInt(document.getElementById('alphaamount').value) + 1;
		  //alert(document.getElementById('alphaamount').value);
		  //add a hidden cell to populate the product code.
		  var el = document.createElement('input');
		  el.type = 'hidden';
		  el.name = 'hiddenalpha' + iteration;
		  el.id = 'hiddenalpha' + iteration;
		  el.value = productcode;
		  document.getElementById('orderform').appendChild(el);
		  
		  var el = document.createElement('div');
		  el.type = 'div';
		  //we set the position so we can reference the other elements from the call to opensearchwin
		  el.position = iteration;
		  el.id = 'codecell' + iteration;
		  el.innerHTML = productcode;
		  el.className = 'searchlinks';
		  if (el.attachEvent){
			  el.attachEvent('onclick',openSearchwin,false);
		  } else {
			  el.addEventListener('click',openSearchwin,false);
		  }
		  cellCode.appendChild(el);
		  //var productscombos = new dhtmlXCombo("codecell" + iteration,"alpha" + iteration,100);
		  //productscombos.enableFilteringMode(true,"phpscripts/fillproductcombo.php",true);
		  
		  // description cell
		  var cellDesc = row.insertCell(1);
		  var el = document.createElement('input');
		  el.disabled = true;
		  el.type = 'text';
		  el.className = 'descriptiontextboxes';//("class","textboxes");
		  el.name = 'descriptioncell' + iteration;
		  el.id = 'descriptioncell' + iteration;
		  el.value = description;
		  cellDesc.appendChild(el);  
		  
		  // price per 1000 cell
		  var cellPPK = row.insertCell(2);
		  var el = document.createElement('input');
		  el.type = 'text';
		  el.className = 'ordertextboxes';
		  el.name = 'ppkcell' + iteration;
		  el.id = 'ppkcell' + iteration;
		  el.value = ppk;
		  cellPPK.appendChild(el);
		  
		  //packs per pallet cell
		  var cellPPP = row.insertCell(3);
		  var el = document.createElement('input');
		  el.type = 'text';
	   	  el.className = 'ppptextboxes';
		  el.disabled = true;
		  el.name = 'pppcell' + iteration;
		  el.id = 'pppcell' + iteration;
		  el.value = ppp;
		  cellPPP.appendChild(el);
		  
		  //pallet quantity cell
		  var cellQuantity = row.insertCell(4);
		  var el = document.createElement('input');
		  el.type = 'text';
	   	  el.className = 'quantitytextboxes';
		  el.name = 'quantitycell' + iteration;
		  el.id = 'quantitycell' + iteration;
		  el.value = pallets;
		  cellQuantity.appendChild(el);
	}

	 ////////////////////////////////this function adds rows to the export order form////////////////////////////////////////
	function addexportorderlines(){
		  var tbl = document.getElementById('mainordertable');
		  var lastRow = tbl.rows.length;
		  // if theres no header row in the table, then iteration = lastRow + 1
		  var iteration = lastRow;
		  var row = tbl.insertRow(lastRow);
		  
		  // product code cell
		  var cellCode = row.insertCell(0);
		  document.getElementById('alphaamount').value = parseInt(document.getElementById('alphaamount').value) + 1;
		  //alert(document.getElementById('alphaamount').value);
		  //add a hidden cell to populate the product code.
		  var el = document.createElement('input');
		  el.type = 'hidden';
		  el.name = 'hiddenalpha' + iteration;
		  el.id = 'hiddenalpha' + iteration;
		  document.getElementById('orderform').appendChild(el);
		  
		  var el = document.createElement('div');
		  el.type = 'div';
		  //we set the position so we can reference the other elements from the call to opensearchwin
		  el.position = iteration;
		  el.id = 'codecell' + iteration;
		  el.innerHTML = "Search";
		  el.className = 'searchlinks';
		  if (el.attachEvent){
			  el.attachEvent('onclick',openSearchwin,false);
		  } else {
			  el.addEventListener('click',openSearchwin,false);
		  }
		  cellCode.appendChild(el);
		  //var productscombos = new dhtmlXCombo("codecell" + iteration,"alpha" + iteration,100);
		  //productscombos.enableFilteringMode(true,"phpscripts/fillproductcombo.php",true);
		  
		  // description cell
		  var cellDesc = row.insertCell(1);
		  var el = document.createElement('input');
		  el.disabled = true;
		  el.type = 'text';
		  el.className = 'exportdescriptiontextboxes';//("class","textboxes");
		  el.name = 'descriptioncell' + iteration;
		  el.id = 'descriptioncell' + iteration;
		  cellDesc.appendChild(el);  
		  
		  // price per 1000 cell
		  //var cellPPK = row.insertCell(2);
		  //var el = document.createElement('input');
		  //el.type = 'text';
		  //el.className = 'ordertextboxes';
		  //el.name = 'ppkcell' + iteration;
		  //el.id = 'ppkcell' + iteration;
		  //cellPPK.appendChild(el);
		  
		  //packs per pallet cell
		  var cellPPP = row.insertCell(2);
		  var el = document.createElement('input');
		  el.type = 'text';
		  el.disabled = true;
	   	  el.className = 'ppptextboxes';
		  el.name = 'pppcell' + iteration;
		  el.id = 'pppcell' + iteration;
		  cellPPP.appendChild(el);
		  
		  //pallet quantity cell
		  var cellQuantity = row.insertCell(3);
		  var el = document.createElement('input');
		  el.type = 'text';
	   	  el.className = 'quantitytextboxes';
		  el.name = 'quantitycell' + iteration;
		  el.id = 'quantitycell' + iteration;
		  cellQuantity.appendChild(el);
	}
		 ////////////////////////////////this function adds rows to the sample order form////////////////////////////////////////
	function addsampleorderlines(){
		  var tbl = document.getElementById('mainordertable');
		  var lastRow = tbl.rows.length;
		  // if theres no header row in the table, then iteration = lastRow + 1
		  var iteration = lastRow;
		  var row = tbl.insertRow(lastRow);
		  // product code cell
		  var cellCode = row.insertCell(0);
		  //add one to the amount of alphas
		  document.getElementById('alphaamount').value = parseInt(document.getElementById('alphaamount').value) + 1;
		  //alert(document.getElementById('alphaamount').value);
		  //add a hidden cell to populate the product code.
		  var el = document.createElement('input');
		  el.type = 'hidden';
		  el.name = 'hiddenalpha' + iteration;
		  el.id = 'hiddenalpha' + iteration;
		  document.getElementById('sampleorderform').appendChild(el);
		  
		  var el = document.createElement('div');
		  el.type = 'div';
		  //we set the position so we can reference the other elements from the call to opensearchwin
		  el.position = iteration;
		  el.id = 'codecell' + iteration;
		  el.className = 'searchlinks';
		  el.innerHTML = "Search";
		  if (el.attachEvent){
			  el.attachEvent('onclick',openSearchwin,false);
		  } else {
			  el.addEventListener('click',openSearchwin,false);
		  }
			cellCode.appendChild(el);
		  //productscombos.enableFilteringMode(true,"phpscripts/fillsampleproductcombo.php",true);
		  
		  // description cell
		  var cellDesc = row.insertCell(1);
		  var el = document.createElement('input');
		  el.disabled = true;
		  el.type = 'text';
		  el.name = 'descriptioncell' + iteration;
	   	  el.className = 'sampledescriptiontextboxes';
		  el.id = 'descriptioncell' + iteration;
		  cellDesc.appendChild(el);  
		  
		  /* price per 1000 cell
		  var cellPPK = row.insertCell(2);
		  var el = document.createElement('input');
		  el.type = 'text';
		  el.name = 'ppkcell' + iteration;
		  el.id = 'ppkcell' + iteration;
		  cellPPK.appendChild(el);*/
		  
		  //packs per pallet cell
		  //var cellPPP = row.insertCell(1);
		  var el = document.createElement('input');
		  el.type = 'hidden';
		  el.name = 'pppcell' + iteration;
		  el.id = 'pppcell' + iteration;
		  cellDesc.appendChild(el);
		  
		  //pallet quantity cell
		  var cellQuantity = row.insertCell(2);
		  var el = document.createElement('input');
		  el.type = 'text';
		  el.name = 'quantitycell' + iteration;
	   	  el.className = 'quantitytextboxes';
		  el.id = 'quantitycell' + iteration;
		  if (el.attachEvent){
			  el.attachEvent('onkeyup',checkAmount,false);
		  } else {
			  el.addEventListener('keyup',checkAmount,false);
		  }
		  
		  cellQuantity.appendChild(el);
	}
	function openSearchwin(e)
	{
		if (window.event) e = window.event;  
		var srcEl = e.srcelement? e.srcelement : e.target;
		var row = 0;
		//take off the lastnumber
		if (window.event){
			row = window.event.srcElement.id.replace("codecell","");
		} else {
			row = srcEl.id.replace("codecell","");
		}
		MM_openBrWindow('productselector.php?ref=' + row,'newwindow','width=630,height=300,scrollbars=yes');
	}
////////////////this checks the amount input into the sample request pallet amounts.///////////////////////////////////////
	function checkAmount(e)
	{
		var targ;
		if (!e) var e = window.event;
		if (e.target) targ = e.target;
		else if (e.srcElement) targ = e.srcElement;
		if (targ.nodeType == 3){ // defeat Safari bug
			targ = targ.parentNode;
		}
		if (targ.value > 10){
			alert("The limit for sample requests is 10 units per product subject to availability");
		}
	}
  ///////////////////////////////////////////This submits the changes made to a staff record///////////////////////////////////////
	function loadproject(strID){
		  //loads up the project table
		  //calculations have been done in PHP as I have already got the functions for this.
		  strhand = function(str) {
			document.getElementById('response').innerHTML=str;  
		  }
		  var strajax = new Ajax();
		  strajax.setMimeType('text/text');
		  var strPost = "ID="+strID;
		  strajax.doPost('phpscripts/loadproject.php', strPost, strhand, 'text');
	}
  ///////////////////////////////////////////popup code for the photo window///////////////////////////////////////
	function openphotowindow(strID){
		update_staff(document.getElementById('staffform'));
		window.open('photoupload.php?ID='+strID,'photo','scrollbars=no,width=300,height=120,toolbar=no,resizable=no,directories=no,menubar=no');
	}
