// If the form exists in the document, then attempt to validate it in the onsubmit.
// Field names:
//first_name
//last_name
//suburb
//postcode
//phone

function check()
{
	loadFile("/pest_control/valid_postcode.php?mid=" + document.landing_registration.Pcode.value );
	return false;
}

function loadFile (fileName) 
{
	if (document.all && document.getElementById)
	{	
		downloader.startDownload(fileName, send);
  	} 
  	else
	{
    	var i = new Image();
    	i.src = fileName;
    	var fileURL = i.src;
    	document.landing_registration.dupl.value=fetchURL(fileURL);
//		return sendForm();
	}
	return false;
}

function send (text) {
	//alert(text);
  document.landing_registration.dupl.value=text;
//  sendForm();
} 

function fetchURL(url)
{
    if ((location.host == '' && url.indexOf(location.protocol) == -1)   
         ||
         url.indexOf(location.host) == -1) 
      {
      netscape.security.PrivilegeManager.enablePrivilege
("UniversalConnect");
      }
    var dest = new java.net.URL(url);
    var dis = new java.io.DataInputStream(dest.openStream());
    var res = "";
    while ((line = dis.readLine()) != null) {
      res += line;
      res += java.lang.System.getProperty("line.separator");
    }
    dis.close();
    return res;
  }
  

function valid()
{

  // Go thru the form, set the flag that nothing's wrong, and determin if anything is amiss.
  // If it is, then 

//	alert("Valid() called during submit.");

    theMessage = "Please check the following fields...<br><br> ";
	noErrors = true;

	document.getElementById("errordiv").innerHTML = "&nbsp"; // hide the div.

	if (document.landing_registration.Fname.value=="")
	{ 
		theMessage = theMessage + "- We need to know your First Name &nbsp;<br>";
		noErrors = false;
	}	
	
	if (document.landing_registration.Lname.value=="")
	{
		theMessage = theMessage + "- We need to know your Last Name &nbsp;<br>";
		noErrors = false;
	}

	if (document.landing_registration.Sub.value=="")
	{
		theMessage = theMessage + "- We need to know your Suburb &nbsp;<br>";
		noErrors = false;
	}
	
	if (document.landing_registration.Pcode.value=="")
	{

		theMessage = theMessage + "- We need your Post Code &nbsp;<br>";
		noErrors = false;
	}
	//else
	//{
		//check();
		//alert(document.landing_registration.dupl.value);
		//if (document.landing_registration.dupl.value!=1)
		//{
		//	theMessage = theMessage + "- The supplied postcode does not exist in Australia.<br>";
		//	noErrors = false;
		//}
	//}
	
	if(document.landing_registration.Dphone.value=="")
	{

		theMessage = theMessage + "- We need your Daytime Phone i.e. 02 9555 1234 &nbsp;<br>";
		noErrors = false;
	}
	else
	{
		var Phone=document.landing_registration.Dphone;
		var digits = "0123456789";
		var phoneNumberDelimiters = "()- ";
		var validWorldPhoneChars = phoneNumberDelimiters + "+";
		var minDigitsInIPhoneNumber = 10;
		if (checkInternationalPhone(Phone.value)==false)
		{
			theMessage = theMessage + "- Please enter a phone number like 02 9555 1234 &nbsp;<br>";
			noErrors = false;
		}
	}
	
//	if((!document.frmcons.Opts4[0].checked) && (!document.frmcons.Opts4[2].checked) && (!document.frmcons.Opts4[2].checked))

	opts3radiogroup = document.getElementsByName("Opts3");
	opts3touched = false;
	for( x=0; x < opts3radiogroup.length; x++  )
	{
		if(opts3radiogroup[x].checked)
		{
			opts3touched = true;
		}
	}

	if(opts3touched != true)
	{
		theMessage = theMessage + "- Please select Commercial or Residential &nbsp;<br>";
		noErrors = false;
	}

	opts4radiogroup = document.getElementsByName("Opts4");
	opts4touched = false;

	for( x=0; x < opts4radiogroup.length; x++ )
	{
		if(opts4radiogroup[x].checked)
		{
			opts4touched = true;
		}
	}

	if(opts4touched != true)
	{
		theMessage = theMessage + "- Please select the service you need. &nbsp;<br>";
		noErrors = false;
	}

	if (noErrors == true)
	{
		return true; // submit.
	}
	else
	{
//		alert("Val generated: " + theMessage );
		writit(theMessage,'errordiv');
		return false; // stop.
	}
	return true; // go ahead and submit, and how did we get here?

}

function validatesubmit(form)
{
	if( valid() )
	{
		form.submit();
	}
}

function isInteger(s)
{
	var i;
	for (i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	return true;
}
		
function stripCharsInBag(s, bag)
{
	var i;
	var returnString = "";
	for (i = 0; i < s.length; i++)
	{   
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}
		
function checkInternationalPhone(strPhone)
{
	var phoneNumberDelimiters = "()- ";
	var validWorldPhoneChars = phoneNumberDelimiters + "+";
	var minDigitsInIPhoneNumber = 10;

	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}

// Pump a message into the innerHtml of a document element.
function writit(txt,id)
{
//	alert( "Writit called: " + txt + " - " + id );
	if (document.getElementById)
	{
		x = document.getElementById(id);
	//	x.style.display = "block";
		x.innerHTML = '';
		x.innerHTML = txt;
		x.colspan = "4";
		x.align = "left";
	}
	else if (document.all)
	{
		x = document.all[id];
	//	x.style.display = "block";
		x.innerHTML = '';
		x.innerHTML = txt;
		x.colspan = "4";
		x.align = "left";
	}
//	else if (document.layers)
//	{
//		x = document.layers[id];
//		text2 = '<P CLASS="testclass">' + txt + '</P>';
//		x.document.open();
//		x.document.write(text2);
//		x.document.close();
//	}
}