var IsAccepted = false;
var companyNameArray = new Array(); 
var companyAddressArray = new Array(); 
var companySuburbArray = new Array(); 
var companyPostcodeArray = new Array(); 
var companyAbnArray = new Array(); 
var companyAdminArray = new Array(); 
var companyFaxArray = new Array(); 
var companyStateArray = new Array(); 
var companyWebaddressArray = new Array(); 

function findCompany(theObject, mycompany, nexttarget) {
   var mycityid = "(51,0,2)";
	 if (mycompany == 18) {
	    mycityid = "(51,0,3)";
	 }
   if (document.getElementById("CityID")) {
	    if (document.getElementById("CityID").value == "") {
			   alert("You need to specify the city")
				 document.getElementById("CityID").focus()
				 return
			}	 
 		  mycityid = document.getElementById("CityID").value;
   } else if (document.getElementById("CompanyID")) {
	    if (document.getElementById("CompanyID").value == "") {
			   alert("You need to specify the city")
				 document.getElementById("CompanyID").focus()
				 return
			}	 
	    if (document.getElementById("CompanyID").value == "(78,0,1)")
			    mycityid = "(51,0,2)";
	    else if (document.getElementById("CompanyID").value == "(78,0,19)")
			    mycityid = "(51,0,2)";
	    else if (document.getElementById("CompanyID").value == "(78,0,11)")
			    mycityid = "(51,0,3)";
	    else if (document.getElementById("CompanyID").value == "(78,0,16)")
			    mycityid = "(51,0,4)";
	    else if (document.getElementById("CompanyID").value == "(78,0,14)")
			    mycityid = "(51,0,7)";
			
	 }

   useAddressLookup(theObject,"locality", mycityid, nexttarget)
}

function doEscape(s) {
   s=s.replace(/\&/,"&amp;")
   s=s.replace(/\"/,"&quot;")
   s=s.replace(/\'/,"&#039;")
   s=s.replace(/>/,"&gt;")
   s=s.replace(/</,"&lt;")
   return s;
}
function HighLight(theObject) {
   theObject.select();
}
function PrintSubmission() {
/*
   document.getElementById("CIRForm").rows["fee"].style.display=""
   document.getElementById("CIRForm").rows["conditions"].style.display=""
   document.getElementById("CIRForm").rows["authorise"].style.display=""
   document.getElementById("CIRForm").rows["submitoption"].style.display="none"
   document.getElementById("CIRForm").rows["term"].style.display="none"
*/	 

   if (document.getElementById("CompanyID")) {
	    if (document.getElementById("CompanyID").selectedIndex == 0) {
	      alert("You must select a city");
        return
		  }		
   }   

   document.getElementById("fee").style.display=""
   document.getElementById("conditions").style.display=""
   document.getElementById("authorise").style.display=""
   document.getElementById("submitoption").style.display="none"
   document.getElementById("term").style.display="none"
//	 window.open("CreateCode.php", "clientFrame");
   window.focus();
	 window.print();  
}


function ReadConditions () {
//   document.getElementById("CIRForm").rows["fee"].style.display=""
//   document.getElementById("CIRForm").rows["conditions"].style.display=""

   if (document.getElementById("CompanyID")) {
	    if (document.getElementById("CompanyID").selectedIndex == 0) {
	      alert("You must select a city");
        return
		  }		
   }   

   document.getElementById("fee").style.display=""
   document.getElementById("conditions").style.display=""

}
function SubmitForm (myCompany) {
   if (IsAccepted) {
//     window.open("CreateCode.php", "clientFrame");
     // make sure user selects a city
			if (document.getElementById("CityID")) {
   			if (document.getElementById("CityID").selectedIndex == 0) {
		  	   alert("You must select your city");
			        return
				}   
			}   
			// check company
//			myCompany = '<?echo $_SESSION["DCompanyID"]; ?>'
			mycityvalue = ""
			if (document.getElementById("CityID")) {
			    if (document.getElementById("BusinessSuburb").value == "" || XMLfield(document.getElementById("BusinessSuburb").item, "locality.suburbid") == null) {
		  	      alert("You must enter a business suburb");
	            return
    			}   

			    if (document.getElementById("CityID").value == "(51,0,3)") 	   // sydney
			       myCompany = "(78,0,11)"
			    else if (document.getElementById("CityID").value == "(51,0,4)") 	   // brisbane
			       myCompany = "(78,0,16)"
			    else if (document.getElementById("CityID").value == "(51,0,7)") 	   // perth
			       myCompany = "(78,0,14)"
				  
			    mycityvalue = "<cityid value='" + document.getElementById("CityID").value + "' />\n"
			} else if (document.getElementById("CompanyID")) {
			    // check all astericks
			    if (document.getElementById("CompanyID").selectedIndex == 0) {
		  	      alert("You must select a city");
//							document.getElementById("CompanyID").focus();
	            return
    		  }   
			    if (document.getElementById("CompanyName").value == "") {
		  	      alert("You must enter your company's name");
							document.getElementById("CompanyName").focus();
	            return
    		  }   
			    if (document.getElementById("Telephone").value == "") {
		  	      alert("You must enter your company's telephone number");
							document.getElementById("Telephone").focus();
	            return
    		  }   
			    if (document.getElementById("Abn").value == "") {
		  	      alert("You must enter your company's ABN number");
							document.getElementById("CompanyName").focus();
	            return
    		  }   
			    if (document.getElementById("AccountsContact").value == "") {
		  	      alert("You must enter your company's accounts contact");
							document.getElementById("AccountsContact").focus();
	            return
    		  }   
			    if (document.getElementById("AccountsContact").value == "") {
		  	      alert("You must enter your company's accounts contact person");
							document.getElementById("AccountsContact").focus();
	            return
    		  }   
			    if (document.getElementById("AccountsPosition").value == "") {
		  	      alert("You must enter your company's accounts contact person's position in your company");
							document.getElementById("AccountsPosition").focus();
	            return
    		  }   
			    if (document.getElementById("AuthorisedBy").value == "") {
		  	      alert("You must enter your company's authorise person");
							document.getElementById("AuthorisedBy").focus();
	            return
    		  }   
			    if (document.getElementById("AuthorisedPosition").value == "") {
		  	      alert("You must enter your company's authorise person's position in your company");
							document.getElementById("AuthorisedPosition").focus();
	            return
    		  }   
			    if (document.getElementById("Bank").value == "") {
		  	      alert("You must enter your company's bank's name");
							document.getElementById("Bank").focus();
	            return
    		  }   
			    if (document.getElementById("Branch").value == "") {
		  	      alert("You must enter your company's bank's branch");
							document.getElementById("Branch").focus();
	            return
    		  }   
			    if (document.getElementById("BusinessAddr").value == "") {
		  	      alert("You must enter your company's address");
							document.getElementById("BusinessAddr").focus();
	            return
    		  }   
			    if (document.getElementById("BusinessSuburb").value == "" || XMLfield(document.getElementById("BusinessSuburb").item, "locality.suburbid") == null) {
		  	      alert("You must enter your company's suburb");
	            return
    			}   
			    if (document.getElementById("PostalAddr").value == "") {
		  	      alert("You must enter your company's postal address");
							document.getElementById("PostalAddr").focus();
	            return
    		  }   
			    if (document.getElementById("PostalSuburb").value == "" || XMLfield(document.getElementById("PostalSuburb").item, "locality.suburbid") == null) {
		  	      alert("You must enter your company's postal suburb");
	            return
    			}   
			    if (document.getElementById("Reference1").value == "") {
		  	      alert("You must enter your company's first reference");
							document.getElementById("Reference1").focus();
	            return
    		  }   
			    if (document.getElementById("Reference1Tel").value == "") {
		  	      alert("You must enter your company's first reference telephone number");
							document.getElementById("Reference1Tel").focus();
	            return
    		  }   
			    if (document.getElementById("Reference2").value == "") {
		  	      alert("You must enter your company's second reference");
							document.getElementById("Reference2").focus();
	            return
    		  }   
			    if (document.getElementById("Reference2Tel").value == "") {
		  	      alert("You must enter your company's second reference telephone number");
							document.getElementById("Reference2Tel").focus();
	            return
    		  }   
				 
			    myCompany = document.getElementById("CompanyID").value	
				  if (document.getElementById("CompanyID").value == "(78,0,1)" || document.getElementById("CompanyID").value == "(78,0,19)")
			       mycityvalue = "<cityid value='(51,0,2)' />\n"
				  else if (document.getElementById("CompanyID").value == "(78,0,11)")
			       mycityvalue = "<cityid value='(51,0,3)' />\n"
				  else if (document.getElementById("CompanyID").value == "(78,0,16)")
			       mycityvalue = "<cityid value='(51,0,4)' />\n"
				  else if (document.getElementById("CompanyID").value == "(78,0,14)")
			       mycityvalue = "<cityid value='(51,0,7)' />\n"								
			}
			
			// build XML
   	 XML = "\n<signup>\n"+ 
			      mycityvalue+
		     "<companyid value='" + myCompany + "' />\n" +
				 "<companyname value='"+doEscape(document.getElementById("CompanyName").value)+"' />\n"+
				 "<abn value='"+doEscape(document.getElementById("Abn").value)+"' />\n"+
				 "<telephone value='"+doEscape(document.getElementById("Telephone").value)+"' />\n"+
				 "<fax value='"+doEscape(document.getElementById("Fax").value)+"' />\n"+
				 "<email value='"+doEscape(document.getElementById("Email").value)+"' />\n"+
				 "<bussinessaddr value='"+doEscape(document.getElementById("BusinessAddr").value)+"' />\n"+
				 "<postaladdr value='"+doEscape(document.getElementById("PostalAddr").value)+"' />\n"+
				 "<bussinesssuburb value='"+doEscape(document.getElementById("BusinessSuburb").value)+"' />\n"+
				 "<possuburb value='"+doEscape(document.getElementById("PostalSuburb").value)+"' />\n"+
				 "<businesstype value='"+doEscape(document.getElementById("BusinessType").value)+"' />\n"+
				 "<melways value='"+doEscape(document.getElementById("Melways").value)+"' />\n"+
				 "<accountscontact value='"+doEscape(document.getElementById("AccountsContact").value)+"' />\n"+
				 "<accountsposition value='"+doEscape(document.getElementById("AccountsPosition").value)+"' />\n"+
				 "<authorisedby value='"+doEscape(document.getElementById("AuthorisedBy").value)+"' />\n"+
				 "<authorisedposition value='"+doEscape(document.getElementById("AuthorisedPosition").value)+"' />\n"+
				 "<bank value='"+doEscape(document.getElementById("Bank").value)+"' />\n"+
				 "<branch value='"+doEscape(document.getElementById("Branch").value)+"' />\n"+
				 "<reference1 value='"+doEscape(document.getElementById("Reference1").value)+"' />\n"+
				 "<reference1tel value='"+doEscape(document.getElementById("Reference1Tel").value)+"' />\n"+
				 "<reference2 value='"+doEscape(document.getElementById("Reference2").value)+"' />\n"+
				 "<reference2tel value='"+doEscape(document.getElementById("Reference2Tel").value)+"' />\n"+
//				 "<frequentaddress value='"+doEscape(document.getElementById("FrequentAddress").value)+"' />\n"+
				 "</signup>\n\n"

			document.getElementById("XMLSignUp").value += XML
			document.forms.Application.submit();
     
			// finish building XML
		 alert("Your application has been submitted. Thank you.")
	 } else {
	   alert("This application cannot be processed until the conditions are read and accepted \n"+
		 			 "Please click on the 'Accept' icon after you have acknowledged the conditions before submitting")			 	   
	 } 	 
}
function Accepted () {
   if (IsAccepted) {
//      document.getElementById("Accept").src="../../images/application/accept.gif"
      document.getElementById("Accept").src="gui/accept.gif"
			IsAccepted = false;
	 }		
	 else	{
//      document.getElementById("Accept").src="../../images/application/accepted.gif"
      document.getElementById("Accept").src="gui/accepted.gif"
			IsAccepted = true;
   }			
}

function ChangeCursor(theObject) {
  theObject.style.cursor = 'point' 
}

function SetFormFocus() {
		document.getElementById("CompanyName").focus();
}

function ChangeDetails(theObject) {
     if (theObject.selectedIndex == 0) {
			   document.getElementById("info").style.visibility = "hidden";
			   return
		 }
			
     var myCompany = ""
		 if (document.getElementById("CityID")) {
		   document.getElementById("info").style.visibility = "visible";
       if (document.getElementById("CityID").value == "(51,0,2)") { 	   // melbourne
          myCompany = "1"
		  	  myState = "Victoria";
       } else if (document.getElementById("CityID").value == "(51,0,3)") { 	   // sydney
          myCompany = "11"
			    myState = "New South Wales";
       } else if (document.getElementById("CityID").value == "(51,0,4)") { 	   // brisbane
          myCompany = "16"
			    myState = "Queensland";
       } else if (document.getElementById("CityID").value == "(51,0,7)") { 	   // perth
          myCompany = "14"
			    myState = "Western Australia";
       }
		 } else if (document.getElementById("CompanyID")) {
//          myCompany = document.getElementById("CompanyID").value
//		  	  myState = "Victoria";
			 document.getElementById("info").style.display = ""
       if (document.getElementById("CompanyID").value == "(78,0,1)") {		 
           myCompany = "1";
  		 	   myState = "Victoria";
       } else if (document.getElementById("CompanyID").value == "(78,0,11)") {		 
           myCompany = "11";
			     myState = "New South Wales";
       } else if (document.getElementById("CompanyID").value == "(78,0,16)") {		 
           myCompany = "16";
			     myState = "Queensland";
       } else if (document.getElementById("CompanyID").value == "(78,0,14)") {		 
           myCompany = "14";
			     myState = "Western Australia";
       } else if (document.getElementById("CompanyID").value == "(78,0,19)") {		 
           myCompany = "19";
			     myState = "Victoria";
			 }		
		 }	 
     document.getElementById("phoneLabel").innerHTML = "("+companyStateArray[myCompany]+") "+companyAdminArray[myCompany] 
     document.getElementById("faxLabel").innerHTML = "("+companyStateArray[myCompany]+") "+companyFaxArray[myCompany]
     document.getElementById("addressLabel").innerHTML = companyAddressArray[myCompany]+", "+companySuburbArray[myCompany]+", "+companyPostcodeArray[myCompany]+", "+myState+", Australia"
     document.getElementById("abnLabel").innerHTML = companyAbnArray[myCompany]
     document.getElementById("emailLabel").innerHTML = companyWebaddressArray[myCompany]
     document.getElementById("conditionABN").innerHTML = companyAbnArray[myCompany]
     document.getElementById("conditionName").innerHTML = companyNameArray[myCompany]
     document.getElementById("stateLabel").innerHTML = myState
}
