// Form Validation Functions  v1.0.1
// http://www.dithered.com/javascript/form_validation/index.html
// code by Chris Nott (chris@dithered.com)

// Remove characters that might cause security problems from a string 
function removeBadCharacters(string) 
		{
			if (string.replace) {
				string.replace(/[<>\"\'%;\)\(&\+]/, '');
			}
			return string;
		}


function checkbadchr()
		{
		if (document.layers||document.all||document.getElementById)
				{
				return removeBadCharacters();
				}
		else
				{
				return true;
				}
		}




function confirmDelete(myText) {
if (confirm ("Are you sure you want to permanently DELETE this " + myText + "?"))
{
	return true
}
	return false
}

function confirmSubmit(myText) {
if (confirm ("Are you sure you want to SUBMIT this " + myText + "?"))
{
	return true
}
	return false
}


// This script and many more are available free online at -->
// The JavaScript Source!! http://javascript.internet.com -->
// Original:  jgw (jgwang@csua.berkeley.edu ) -->
// Web Site:  http://www.csua.berkeley.edu/~jgwang/ -->
// Begin
function checkCapsLock( e ) {
	var myKeyCode=0;
	var myShiftKey=false;
	var myMsg='Caps Lock is On.\n\nTo prevent entering your password incorrectly,\nyou should press Caps Lock to turn it off.';

	// Internet Explorer 4+
	if ( document.all ) {
		myKeyCode=e.keyCode;
		myShiftKey=e.shiftKey;

	// Netscape 4
	} else if ( document.layers ) {
		myKeyCode=e.which;
		myShiftKey=( myKeyCode == 16 ) ? true : false;

	// Netscape 6
	} else if ( document.getElementById ) {
		myKeyCode=e.which;
		myShiftKey=( myKeyCode == 16 ) ? true : false;

	}

	// Upper case letters are seen without depressing the Shift key, therefore Caps Lock is on
	if ( ( myKeyCode >= 65 && myKeyCode <= 90 ) && !myShiftKey ) {
		alert( myMsg );

	// Lower case letters are seen while depressing the Shift key, therefore Caps Lock is on
	} else if ( ( myKeyCode >= 97 && myKeyCode <= 122 ) && myShiftKey ) {
		alert( myMsg );

	}
}
//  End -->



// This script and many more are available free online at
// The JavaScript Source!! http://javascript.internet.com

// Begin
function explain(name, output, msg) {
newwin = window.open('','','top=150,left=150,width=325,height=300');
if (!newwin.opener) newwin.opener = self;
with (newwin.document)
{
open();
write('<html>');
write('<body onLoad="document.form.box.focus()"><form name=form>' + msg + '<br>');
write('<p>You may enter your ' + name + ' here and it will be copied into the form for you.');
write('<p><center>' + name + ':  <input type=text name=box size=10 onKeyUp=' + output + '=this.value>');
write('<p><input type=button value="Click to close when finished" onClick=window.close()>');
write('</center></form></body></html>');
close();
   }
}


function CustomerWindow(expjobid){
	//Used to open the help window
	myCustomerWindow = window.open("roberts_addcustomer_popup.asp?jid="+expjobid,"myCustomerWindow","width=700,height=480,scrollbars=yes");
	//Focus on the window just opened
	myCustomerWindow.focus();
}
function HazardousWindow(expjobid){
	//Used to open the help window
	myHazardousWindow = window.open("roberts_addhazardous_popup.asp?jid="+expjobid,"myHazardousWindow","width=700,height=480,scrollbars=yes");
	//Focus on the window just opened
	myHazardousWindow.focus();
}

function InvoiceWindow(expjobid){
	//Used to open the help window
	myInvoiceWindow = window.open("roberts_addinvoice_popup.asp?jid="+expjobid,"myInvoiceWindow","width=700,height=480,scrollbars=yes");
	//Focus on the window just opened
	myInvoiceWindow.focus();
}

function ConsignmentWindow(expjobid){
	//Used to open the help window
	myConsignmentWindow = window.open("roberts_addConsignment_popup.asp?jid="+expjobid,"myConsignmentWindow","width=700,height=480,scrollbars=yes");
	//Focus on the window just opened
	myConsignmentWindow.focus();
}

function TypicalContainerWindow(expjobid){
	//Used to open the help window
	myTypicalContainerWindow = window.open("roberts_addtypicalcontainer_popup.asp?jid="+expjobid,"myTypicalContainerWindow","width=700,height=480,scrollbars=yes");
	//Focus on the window just opened
	myTypicalContainerWindow.focus();
}

function PackDepotWindow(expjobid){
	//Used to open the help window
	myPackDepotWindow = window.open("roberts_addPackDepot_popup.asp?jid="+expjobid,"myPackDepotWindow","width=700,height=480,scrollbars=yes");
	//Focus on the window just opened
	myPackDepotWindow.focus();
}

function TransportWindow(expjobid){
	//Used to open the help window
	myTransportWindow = window.open("roberts_addTransport_popup.asp?jid="+expjobid,"myTransportWindow","width=700,height=480,scrollbars=yes");
	//Focus on the window just opened
	myTransportWindow.focus();
}

function VesselWindow(expjobid){
	//Used to open the help window
	myVesselWindow = window.open("roberts_addVessel_popup.asp?jid="+expjobid,"myVesselWindow","width=700,height=480,scrollbars=yes");
	//Focus on the window just opened
	myVesselWindow.focus();
}

function CargoWindow(expjobid){
	//Used to open the help window
	myCargoWindow = window.open("roberts_addCargo_popup.asp?jid="+expjobid,"myCargoWindow","width=800,height=680,scrollbars=yes");
	//Focus on the window just opened
	myCargoWindow.focus();
}
function EmptiesWindow(expjobid){
	//Used to open the help window
	myEmptiesWindow = window.open("roberts_addempties_popup.asp?jid="+expjobid,"myEmptiesWindow","width=800,height=680,scrollbars=yes");
	//Focus on the window just opened
	myEmptiesWindow.focus();
}

function PrintWindow(expjobid){
	//Used to open the help window
	myPrintWindow = window.open("roberts_printjobs_popup.asp?jid="+expjobid,"myPrintWindow","width=800,height=680,scrollbars=yes");
	//Focus on the window just opened
	myPrintWindow.focus();
}

function PrintSummaryWindow(expjobid){
	//Used to open the help window
	myPrintWindow = window.open("roberts_printcargosummary_popup.asp?jid="+expjobid,"myPrintWindow","width=800,height=680,scrollbars=yes");
	//Focus on the window just opened
	myPrintWindow.focus();
}

function ContainerWindow(expjobid,container){
	//Used to open the help window
	myContainerWindow = window.open('roberts_container.asp?cid='+container+'&jid=' +expjobid,"myContainerWindow","width=1024,height=600,scrollbars=yes,resizable=yes");
	//Focus on the window just opened
	myContainerWindow.focus();
	//Add the creator property to the child window for referencing back to the parent
	myContainerWindow.creator=self;
}

function changepc(s,x){
	//Changes the postcode field based on the suburb selected
	//Search Array for a particular value and get its index position
	//s is the field variable 'this'
	//x is the name of the variable to be changed ie. the postcode
	var i=0
	var d
	var bolExists  = false
	
	while ((bolExists==false) && (i <= arrSuburb.length)){
		if(arrSuburb[i]==s.value)
		{
			d = arrPostcode[i];
		bolExists = true;
		}
		i++
	}
	
	// Assign object ref to the variable name supplied
	if(bolExists==true){
	x = document.getElementById(x)
	x.value=d;
	}
	else
	{
	x = document.getElementById(x)
	x.value="Unknown ";
	}

}
function testme(myValue){
	alert(myValue)
}

//Used to determine if a name change has occurred
var  myCustomerContact='';
function changedetails(s){
	//Changes the postcode field based on the suburb selected
	//Search Array for a particular value and get its index position
	var x=0
	var bolExists  = false
	var a,b,c,d,e,f,g,h,i,j

	//Set id for various fields to be filled out
	a=document.getElementById("ExpCustMob");
	b=document.getElementById("ExpCustFax");
	c=document.getElementById("ExpCustEmail");
	d=document.getElementById("ExpCustDirPhone");
	e=document.getElementById("ExpCustPOAdd1");
	f=document.getElementById("ExpCustPOAdd2");
	g=document.getElementById("ExpCustPOSubrb");
	h=document.getElementById("ExpCustPOState");
	i=document.getElementById("ExpCustPOCountry");
	j=document.getElementById("ExpCustPOPCode");
	
	//The name hasnt changed but fill out any empty fields
	while ((bolExists==false) && (x <= arrCustomerContacts.length)){
		
		if(arrCustomerContacts[x]==s.value){	
			//The contact name has changed so force an update of all values
			if (myCustomerContact!=s.value){
				a.value=arrCustomerMobile[x]
				b.value=arrCustomerFax[x]
				c.value=arrCustomerEmail[x]
				d.value=arrCustomerPhone[x]
				e.value=arrCustomerPOAdd1[x]
				f.value=arrCustomerPOAdd2[x]
				g.value=arrCustomerPOSubrb[x]
				h.value=arrCustomerPOState[x]
				i.value=arrCustomerPOCountry[x]
				j.value=arrCustomerPOPCode[x]
				bolExists = true;
			}
			else{
				//Change the var if nothing is in there
				if (a.value.length==0)
					a.value=arrCustomerMobile[x]
				if (b.value.length==0)
					b.value=arrCustomerFax[x]
				if (c.value.length==0)
					c.value=arrCustomerEmail[x]
				if (d.value.length==0)
					d.value=arrCustomerPhone[x]
				if (e.value.length==0)
					e.value=arrCustomerPOAdd1[x]
				if (f.value.length==0)
					f.value=arrCustomerPOAdd2[x]
				if (g.value.length==0)
					g.value=arrCustomerPOSubrb[x]
				if (h.value.length==0)
					h.value=arrCustomerPOState[x]
				if (i.value.length==0)
					i.value=arrCustomerPOCountry[x]
				if (j.value.length==0)
					j.value=arrCustomerPOPCode[x]
				bolExists = true;
			}
		}
		x++
	}
}

function changeportaldetails(s){
	//Changes the postcode field based on the suburb selected
	//Search Array for a particular value and get its index position
	var x=0
	var bolExists  = false
	var a,b,c,d,e,f,g,h,i,j

	//Set id for various fields to be filled out
	a=document.getElementById("ExpCustMob");
	b=document.getElementById("ExpCustFax");
	c=document.getElementById("ExpCustEmail");
	d=document.getElementById("ExpCustDirPhone");
	e=document.getElementById("ExpCustPOAdd1");
	f=document.getElementById("ExpCustPOAdd2");
	g=document.getElementById("ExpCustPOSubrb");
	h=document.getElementById("ExpCustPOState");
	i=document.getElementById("ExpCustPOCountry");
	j=document.getElementById("ExpCustPOPCode");
	
	//The name hasnt changed but fill out any empty fields
	while ((bolExists==false) && (x <= arrCustomerContacts.length)){
		//See if the value in the array we are looking at is the actual customers name
		if(arrCustomerContacts[x]==s.value){	
				//Change the var if nothing is in there
				if (a.value.length==0)
					a.value=arrCustomerMobile[x]
				if (b.value.length==0)
					b.value=arrCustomerFax[x]
				if (c.value.length==0)
					c.value=arrCustomerEmail[x]
				if (d.value.length==0)
					d.value=arrCustomerPhone[x]
				if (e.value.length==0)
					e.value=arrCustomerPOAdd1[x]
				if (f.value.length==0)
					f.value=arrCustomerPOAdd2[x]
				if (g.value.length==0)
					g.value=arrCustomerPOSubrb[x]
				if (h.value.length==0)
					h.value=arrCustomerPOState[x]
				if (i.value.length==0)
					i.value=arrCustomerPOCountry[x]
				if (j.value.length==0)
					j.value=arrCustomerPOPCode[x]
				bolExists = true;
		}
		x++
	}
}

		function ContactCode(FN,LN) {
			//Converts a Firstname, lastname combo into a contact code automatically
			var myCode
		
			// Format the name by grabbing first 2 chars of FN and first 4 chars of LN
			re1 = /(\S{2})/;
			re2 = /(\S{4})/;
				
			//format the name based on the RegExp			
			re2.exec(LN.value);
			myCode = RegExp.$1.toUpperCase()
			re1.exec(FN.value);
			myCode = myCode + RegExp.$1.toUpperCase()
			
			// Update the contact code on the form 
			document.getElementById("concode").value = myCode;

		}

		function EmployeeCode(FN,LN) {
			//Converts a Firstname, lastname combo into a contact code automatically
			var myCode
		
			// Format the name by grabbing first 2 chars of FN and first 4 chars of LN
			re1 = /(\S{2})/;
			re2 = /(\S{4})/;
				
			//format the name based on the RegExp			
			re2.exec(LN.value);
			myCode = RegExp.$1.toUpperCase()
			re1.exec(FN.value);
			myCode = myCode + RegExp.$1.toUpperCase()
			
			// Update the contact code on the form 
			document.getElementById("empcode").value = myCode;

		}
		
	function CompanyCode(FN) {
			//Converts a Firstname, lastname combo into a contact code automatically
			var myCode
		
			// Format the name 
			re1 = /^(\S{4})\s+(\S{4})/;
			
			//format the name based on the RegExp			
			re1.exec(FN.value);
			
			myCode = RegExp.$1.toUpperCase()
			myCode = myCode + RegExp.$2.toUpperCase()

			// If the regexp was empty
			if (myCode=="" )
				{myCode = FN.value}
			
			// Update the contact code on the form 
			document.getElementById("compcode").value = myCode.toUpperCase();

		}

// Div hide functions found at
//http://www.tek-tips.com/faqs.cfm?fid=5923
// NOTE: You cannot put a div inside a table properly, you need to split into 2 tables

//this function will show the content within the <div> tag
function ShowDiv(divName) 
{
  thisDiv = document.getElementById(divName);
  if (thisDiv) 
  {
    if (thisDiv.style.display == "none") {
      thisDiv.style.display = "block";
    }
  }
  else {
    alert("Error: Could not locate div with id: " + divName);
  }
}

//this function will hide the content within the <div> tag
function HideDiv(divName) 
{
  thisDiv = document.getElementById(divName);
  if (thisDiv) 
  {
    if (thisDiv.style.display == "block") {
      thisDiv.style.display = "none";
    }
  }
  else {
    alert("Error: Could not locate div with id: " + divName);
  }
}

function showchangedfields()
{
	//Updates based on an array of fields that need to be highlighted

	// Examine our array made by the server that has changed fields listed in it
	if (isArray(arrUpdatedFields))
	{
		for (i=0;i<arrUpdatedFields.length;i++)
		{
			//If the field we are looking for exists then make it red
			if (document.getElementById(arrUpdatedFields[i])!=null){
	   			styleObj=document.getElementById(arrUpdatedFields[i]).style;
				styleObj.color="red";
			// Take into account our special ajax search fields because they have hidden fields
			if (document.getElementById(arrUpdatedFields[i]).name== "ExpMode"){
	   			styleObj=document.getElementById('conmode').style;
				styleObj.color="red";}
			if (document.getElementById(arrUpdatedFields[i]).name== "ExpInputInits"){
	   			styleObj=document.getElementById('robcon').style;
				styleObj.color="red";}
			if (document.getElementById(arrUpdatedFields[i]).name== "ExpCustCon"){
	   			styleObj=document.getElementById('custcon').style;
				styleObj.color="red";}
			if (document.getElementById(arrUpdatedFields[i]).name== "ExpDocFinalPort"){
	   			styleObj=document.getElementById('final').style;
				styleObj.color="red";}
			if (document.getElementById(arrUpdatedFields[i]).name== "strLoadTerminal"){
	   			styleObj=document.getElementById('recterm').style;
				styleObj.color="red";}
			if (document.getElementById(arrUpdatedFields[i]).name== "ExpShipCo"){
	   			styleObj=document.getElementById('shipline').style;
				styleObj.color="red";}
			if (document.getElementById(arrUpdatedFields[i]).name== "ExpVessel"){
	   			styleObj=document.getElementById('vessel').style;
				styleObj.color="red";}
			if (document.getElementById(arrUpdatedFields[i]).name== "ExpDocLoadPort"){
	   			styleObj=document.getElementById('despatch').style;
				styleObj.color="red";}
			}
		}
	}
}

function updatesinglecolor(thistag)
{//Updates a single field specified to being red
   
   styleObj=document.getElementById(thistag).style;
   styleObj.color="red";
   
}


function displaycompanytype(){
	// Displays the company type information
	if (document.getElementById("supplier").checked)
		{ShowDiv('companytype');} 
	else 
		{HideDiv('companytype');}
}
//  End -->
function isArray(obj) {
   if (obj.constructor.toString().indexOf("Array") == -1)
      return false;
   else
      return true;
}

function addempties(){
// This runs through the form and grabs all the of the containers specs, ass them and put them
//into the total containers field
var formelements = document.addjobs.elements
var total;
total=0;
		for (i=0;i<formelements.length;i++)
		{

			if (formelements[i].name.indexOf("ExpEmpQty")>=0){
				total = total + parseInt(formelements[i].value);
				}
		}
		document.addjobs.ExpTotCont.value = total;		
}

function addElement()
{
//theValue field is used as a counter for tracking
//ni is the placeholder where the code is to be inserted
	
var ni = document.getElementById('optempty');
var numi = document.getElementById('numberofemptyentries');
var num = (document.getElementById("numberofemptyentries").value -1)+ 2;
numi.value = num;
var divIdName = "optempty"+num;
var newdiv = document.createElement('div');
newdiv.setAttribute("id",divIdName);

htmlcode = "<table><tr><td width='368'>Container Supplier:</td><td width='392'><input type='text' name='ExpEmpName" + num + "' id='ExpEmpName" + num + "' size='25'></td></tr>\n";
htmlcode += "<tr><td width='368'>Container Release No:</td><td width='392'><input type='text' name='ExpEmpRelNo" + num + "' id='ExpEmpRelNo" + num + "'  size='25'></td></tr>\n";
htmlcode += "<tr><td width='368'>Container Size (20 or 40)</td><td width='392'><input type='text' name='ExpEmpSize" + num + "' id='ExpEmpSize" + num + "' size='25'></td></tr>\n";
htmlcode += "<tr><td width='368'>Address 1</td><td width='392'><input type='text' name='ExpEmpAdd1" + num + "' id='ExpEmpAdd1" + num + "' size='25'></td></tr>\n";
htmlcode += "<tr><td width='368'>Address 2</td><td width='392'><input type='text' name='ExpEmpAdd2" + num + "' id='ExpEmpAdd2" + num + "' size='25'></td></tr>\n";
htmlcode += "<tr><td width='368'>Suburb</td><td width='392'><input type='text' name='ExpEmpSubrb" + num + "' id='ExpEmpSubrb" + num + "' size='25'></td></tr>\n";
htmlcode += "<tr><td width='368'>Trading Hours</td><td width='392'><input type='text' name='ExpEmpTrad" + num + "' id='ExpEmpTrad" + num + "' size='25'></td></tr>\n";
htmlcode += "<tr><td width='368'>Number Of Container Reqd</td><td width='392'><input type='text' name='ExpEmpQty" + num + "' id='ExpEmpQty" + num + "' size='25'>\n";
htmlcode += "<input type='radio' name='ExpEmpType" + num + "' value='std' checked>Standard\n";
htmlcode += "<input type='radio' name='ExpEmpType" + num + "' value='open'>Open Top\n";
htmlcode += "<input type='radio' name='ExpEmpType" + num + "' value='flat'>Flat Rack\n";
htmlcode += "<input type='radio' name='ExpEmpType" + num + "' value='hicube'>Hi Cube \n";
//This line allows you to selectively remove added items
//htmlcode += "<a href=\"javascript:;\" onclick=\"removeElement(\'"+divIdName+"\')\">Remove the empty listing</a>\n";
htmlcode += "</td></tr></table>";
newdiv.innerHTML = htmlcode;

ni.appendChild(newdiv);
}

function removeElement(divNum) {
  var d = document.getElementById('optempty');
  var olddiv = document.getElementById(divNum);
  d.removeChild(olddiv);
}
