/*
 * function allSpaces
 */
function allSpaces(szString){
	allSpc = true
	for(var i = 0;i <= szString.length-1;i++){
		if (szString.substring(i,i+1) != " "){
			allSpc = false
		}
	}
	return allSpc
}

/*
 * function isNotNumeric - checks for any non numberic input
 */
function isNotNumeric(instring)
{
	instring=instring.toLowerCase()
	RefString="1234567890";
	for (Count=0; Count < instring.length; Count++)
	{
		TempChar= instring.substring(Count, Count+1);
		if (RefString.indexOf(TempChar, 0)==-1)
		{			
			return true;
		} 
	}
	return false;
} 

/*
 * isEmail - checks for correct email format.
 */ 
function isEmail(strInput) {

	if( strInput == "" ) return "";

	strInput = strInput.toLowerCase();

	for( at=0,dot=0,i=0; i < strInput.length; i++ ) {
		x = strInput.charAt(i);

		if(  ('a' <= x && x <= 'z')  ||  ('0' <= x && x <= '9')  ||  x=='_'  ||  x=='-'  )
			continue;
		else if( x == "@" ) {
			if( ++at != 1 )
				return(-1);
		}
		else if( x == "." ) {
			if( at == 1 )
				dot++;
		}
		else {
			return(-1);
		}

	}

	if( at==0 || dot==0 )
		return(-2);
	else
		return(0);	// valid phone number
} 

function printWindow() {
bV = parseInt(navigator.appVersion);
if (bV >= 4) window.print();
}

var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
function calAge(aBday) {
	
	var chkslashpos = aBday.split('/')
	var nDate = ""
	var yr = chkslashpos[0]
	var m = chkslashpos[1]
	var d = chkslashpos[2]

	var today=new Date();
	var todayy=today.getYear();

	if ((navigator.appName == "Microsoft Internet Explorer") && (todayy < 2000))		
	todayy="19" + todayy;
	if (navigator.appName == "Netscape")
	todayy=1900 + todayy;

	var todaym=today.getMonth();
	var todayd=today.getDate();
	var todaystring=montharray[todaym]+" "+todayd+", "+todayy;
	var paststring=montharray[m-1]+" "+d+", "+yr;
	var difference=(Math.round((Date.parse(todaystring)-Date.parse(paststring))/(24*60*60*1000))*1);

	return difference;
}
	
function validate(form){

    // PAssword
	
   if (allSpaces(form.txtPassword.value)) {
      alert('You must enter a Password.')
      form.suburb.focus()
      return false
    }
	
    // account_name      
    	
	if (allSpaces(form.account_name.value)) {
      alert('You must enter a Name.')
      form.account_name.focus()
      return false
    }
    // address      
    if (allSpaces(form.address.value)) {
      alert('You must enter an address.')
      form.address.focus()
      return false
    }
    // suburb      
    if (allSpaces(form.suburb.value)) {
      alert('You must enter a Suburb.')
      form.suburb.focus()
      return false
    }
    
    // city    
    if (allSpaces(form.city.value)) {
      alert('You must enter a City.')
      form.city.focus()
      return false
    } 
    
    //State
    if (form.state.value == "") {
    	alert('You must enter a valid state.')
    	form.state.focus()
    	return false
    }	   
  
    // postcode      
    if (allSpaces(form.postcode.value)) {
      alert('You must enter a postcode.')
      form.postcode.focus()
      return false
    }
	
	if (allSpaces(form.dob.value)) {
      alert('You must enter your Date of Birth.')
      form.dob.focus()
      return false
    }
	
	if (form.dob.value.length != 10) {
      alert('Your Date of Birth must be in dd/mm/yyyy format (eg- 25/11/1970).')
      form.dob.focus()
      return false
    } else {
		var cmAge = 0;		
		cmAge = calAge(form.dob.value);
		if (cmAge < 6574) {
			alert('You must be at least 18 years old to apply for an application.')
		    form.dob.focus()	
			return false;
		}
	}
	
    // postcode numeric    
    if (isNotNumeric(form.postcode.value)) {
      alert('Please enter a valid post code number.')
      form.postcode.focus()
      return false
    }
	
	if (form.postcode.value.length != 4) {
      alert('The Postcode must be 4 digits.')
      form.postcode.focus()
      return false
    }
	
	if (isNotNumeric(form.phone.value)) {
      alert('Please enter only numbers for your phone number.')
      form.phone.focus()
      return false
    }
	
	if (form.phone.value.length != 10) {
      alert('Please enter your phone number including the area code.')
      form.phone.focus()
      return false
    }
		
	if (isNotNumeric(form.fax.value)) {
     	alert('Please enter only numbers for your fax number.')
    	form.fax.focus()
		if (form.fax.value.length != 10) {
      		alert('Please enter your fax number including the area code.')
      		form.fax.focus()
		}
      return false
    }	
   
   
	// telephone/email/fax
    if (allSpaces(form.phone.value)) {
      if (allSpaces(form.email.value)) {
        if (allSpaces(form.fax.value)) {
		  if (allSpaces(form.mobile.value)) {
          alert('You must enter either a phone, email, mobile or fax for contact information.')
          form.phone.focus()
          return false
		  }
        }
      }
    }
	
	//Check for 0 as first character in the phone number.
	if(form.phone.value.indexOf("0")==0) {
		
		}
	else
		{alert('Please enter your area code.')
      	form.phone.focus()
       return false
	}
	
	//Check for a mobile number in phone field.
	if(form.phone.value.indexOf("4")==1) {
		alert('Please enter a fixed line number. (eg. Your home number.)')
		form.phone.focus()
		return false
	}
	
	
	//Check for valid email address
	if (allSpaces(form.email.value)) {
	 
		}
	else
	{if(isEmail(form.email.value)) {
		alert('Please enter a valid email address.')
		form.email.focus()
		return false
		}
	}
	
	if (form.agreeTerms.checked != true){
		alert('Please ensure you have read and agree to all of the terms and conditions of our Privacy Policy.')
		form.agreeTerms.focus()
		return false
	}
	
    // Return form valid
    return true
  }
//-->

function isBilling() {
				
	// TYPE
	var rbBilling = document.all.ckbBilling;
				
	if (rbBilling != null ){
					
		// TYPE
					
		if (rbBilling.checked == true) {
			document.all.trBilling.style.display='inline';
		} 
		else {
			document.all.trBilling.style.display='none';
		}

	}
}

function isWOM() {
				
	// TYPE
	var rbBilling = document.all.chkWOM;
				
	if (rbBilling != null ){
					
		// TYPE
					
		if (rbBilling.checked == true) {
			document.all.trWOM22.style.display='inline';
		} 
		else {
			document.all.trWOM22.style.display='none';
		}

	}
}