// JavaScript Document


function validate(theform,HideItems) {

var s = HideItems;


	//Need to check if more than 1 item passed otherwise we get a JS error
	if(s.length >1) {
	var arrItems = new Array();
	arrItems = s;//.split(",");
	}
	else {
	var arrItems=s;
	}
		


 
// The whitespace has been trimmed from the beginning and end of myString
// Let's use the string's built-in length function to test that


	//Every form has first, last name, email and phone 	
		if (Trim(theform.FirstName.value)=="") {
		alert("Please enter your first name");
		theform.FirstName.focus()
		return false; }

		if (Trim(theform.LastName.value)=="") {
		alert("Please enter your last name");
		theform.LastName.focus()
		return false; }
	
		if (Trim(theform.EmailAddress.value)=="") {
		alert("Please enter your email address");
		theform.EmailAddress.focus()
		return false; }
		
		if (Trim(theform.PhoneNumber.value)== "") {
		alert("Please enter a contact number");
		theform.PhoneNumber.focus()
		return false; }
	
		if (!IsNumeric(theform.PhoneNumber.value)) {
		alert('Please enter numeric characters only for your contact number') 
		theform.PhoneNumber.focus(); 
		return false; 
		} 
					
	//Loop through all available fields but only display alert for ones that want
	for($i=4;$i<=10;$i++) {
	
		if(inArray($i,arrItems) == false) { //we want false as array contains items we don't want to check
		
			switch($i) {
			
				case 4:
				
					if (Trim(theform.Address.value)=="") {
					alert("Please enter your postal address");
					theform.Address.focus()
					return false; }
					
				break;
				
				case 5:
					
					if (Trim(theform.Suburb.value)=="") {
					alert("Please enter your suburb");
					theform.Suburb.focus()
					return false; }
					
				break;
				
				case 6:
				
					if (theform.State.value=="-Select-") {
					alert("Please select your state");
					theform.State.focus()
					return false; }
					
				break;
			
				case 7:
				
					if (Trim(theform.Postcode.value)=="") {
					alert("Please enter your postcode");
					theform.Postcode.focus()
					return false; }
					
					if (!IsNumeric(theform.Postcode.value)) {
					alert('Please enter numeric characters only for your postcode') 
				    theform.Postcode.focus(); 
				    return false; 
				    } 
						
				break;
				
				case 8:
				
					if (theform.found.value=="-Select-") {
					alert("Please indicate how you found out about Share Wealth Systems");
					theform.found.focus()		
					return false; }

					if (theform.found.value=="Other" && Trim(theform.other.value)=="") {
					alert("Please indicate how you found out about Share Wealth Systems");
					theform.other.focus()
					return false; }
					
				break;
				
				case 9:
					
					if (theform.Session.value=="-Select-") {
					alert("Please indicate which session you will be attending");
					theform.Session.focus()
					return false; }
					
				break;
				
				
				case 10:
				
				if (theform.TC.checked != true) {
				alert("You must accept the terms and conditions");
				theform.TC.focus()
				return false; }
				
				break;
				
			}
		
		}

	}

}



function CheckForOther(obj1,obj2) {
var el1 = document.getElementById(obj1);
var el2 = document.getElementById(obj2);

//details is the name of the form
var w = document.details.found.selectedIndex;
var selected_text = document.details.found.options[w].text;

	if (selected_text=="Other") {
	el1.style.visibility="visible";
	el2.style.visibility="visible";
	el2.style.display = 'block';
	el1.style.display = 'block';
	}
	else {
	el1.style.visibility="hidden";
	el2.style.visibility="hidden";
	}
	
}


function inArray(needle, haystack) {



	for (h in haystack) {
		
		if (haystack[h] == needle) {
		return true;

		}

	}

return false;

} 


//Checks to ensure only numeric values are entered for postcode and phone number
function IsNumeric(sText)
{
   var ValidChars = "0123456789 +()";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }
   

// I renamed the function TrimUsingWhileLoop to just Trim for short
function Trim(str)
{  while(str.charAt(0) == (" ") )
  {  str = str.substring(1);
  }
  while(str.charAt(str.length-1) == " " )
  {  str = str.substring(0,str.length-1);
  }
  return str;
}


function validateContactUs(theform) {

	
		if (theform.department.value== "-- Select Department --") {
		alert("Please select a department for your query");
		theform.department.focus()
		return false; }
	
		if (theform.subject.value == "") {
		alert('Please enter a subject title for your query') 
		theform.subject.focus(); 
		return false; 
		} 
		
		if (theform.name.value == "") {
		alert('Please enter your name') 
		theform.name.focus(); 
		return false; 
		} 


		if (theform.email.value == "") {
		alert('Please enter your email address') 
		theform.email.focus(); 
		return false; 
		} 
	
		
		if (theform.phone.value == "") {
		alert('Please enter a contact number') 
		theform.phone.focus(); 
		return false; 
		} 
		
		
		if (theform.message.value == "") {
		alert('Please enter your query') 
		theform.message.focus(); 
		return false; 
		} 

}