/****************************

function fncEnable(chknum) - used for the projects application address tick box

function onlyNumbers(e) - used for the projects application to limit what keys can be entered for that field

function onlyChars(e) - similar to onlyNumbers, but for characters instead obviously

function select_marital() - used for projects application to enable or disable the spouse's name depending on the marital status choice

function validate_required(field,alerttxt) - used to validate text fields are not null or empty

function radio_check() - used to check that a radio button is selected

function phone_check(field) - used to check that at least one phone field has been entered - this needs more work

function echeck(str) - used to validate the email address (used validate_required for null/empty validation)

function validate_form(thisform) - used to validate the summer project application form

function no_comma(thisform) - not allowed to enter commas

****************************/


function no_comma(e){
  // declare variables
  var keynum
  var keychar
  var numcheck

  if(window.event){ //IE
    keynum = e.keyCode
	}
  else if(e.which){ // Netscape/Firefox/Opera
    keynum = e.which
	}
  keychar = String.fromCharCode(keynum)

if (keychar == ',' || keychar==';'){
return false;
}

}






function fncEnable(chknum){
// if it's not ticked - can enter details into the home addresses
  if(document.details["home=term"].checked == false){
    document.details["home_add1"].disabled = false;
    document.details["home_add2"].disabled = false;
    document.details["home_state"].disabled = false;
    document.details["home_ph"].disabled = false;
    document.details["home_ph_area"].disabled = false;
    document.details["home_pcode"].disabled = false;
    document.details["home_work_ph"].disabled = false;
    document.details["home_work_ph_area"].disabled = false;
  } else {
// if it is ticked - no editting and make the values match between the term address and non-term address
    document.details["home_add1"].disabled = true;
    document.details["home_add2"].disabled = true;
    document.details["home_state"].disabled = true;
    document.details["home_ph"].disabled = true;
    document.details["home_ph_area"].disabled = true;
    document.details["home_pcode"].disabled = true;
    document.details["home_work_ph"].disabled = true;
    document.details["home_work_ph_area"].disabled = true;
    document.details["home_add1"].value = document.details["term_add1"].value;
    document.details["home_add2"].value = document.details["term_add2"].value;
    document.details["home_state"].value = document.details["term_state"].value;
    document.details["home_ph"].value = document.details["term_ph"].value;
    document.details["home_ph_area"].value = document.details["term_ph_area"].value;
    document.details["home_work_ph_area"].value = document.details["work_ph_area"].value;
    document.details["home_work_ph"].value = document.details["work_ph"].value;
    document.details["home_pcode"].value = document.details["pcode"].value;
  }
}  



function onlyNumbers(e){
  // declare variables
  var keynum
  var keychar
  var numcheck

  if(window.event){ //IE
    keynum = e.keyCode
	}
  else if(e.which){ // Netscape/Firefox/Opera
    keynum = e.which
	}
  keychar = String.fromCharCode(keynum)
  // numcheck is the required regular expression for either a number OR   backspace OR tab
  numcheck = /(\d|[\b]|\u9)/


  if ((e.keyCode == 9)||(e.keyCode == 35)||(e.keyCode == 36)||(e.keyCode == 37)||(e.keyCode == 39)||(e.keyCode == 46)){
    return true
  }
  // return the character if it's true
  return numcheck.test(keychar)

}



function onlyChars(e){
  // declare variables
  var keynum
  var keychar
  var numcheck

  if(window.event){ // IE
    keynum = e.keyCode
	}
  else if(e.which){ // Netscape/Firefox/Opera
    keynum = e.which
	}
  keychar = String.fromCharCode(keynum)
  // numcheck is the required regular expression for either a letter OR backspace OR tab or space
  numcheck = /(\s|[a-zA-Z-']|[\b]|\u9)/

  if ((e.keyCode == 9)||(e.keyCode == 35)||(e.keyCode == 36)||(e.keyCode == 37)||(e.keyCode == 39)||(e.keyCode == 46)){
    return true
  }
  // return the character if it's true
  return numcheck.test(keychar)

}






function select_marital(){
  if (document.details["marital"].value != "single"){
    document.details["spouse"].disabled = false
  } else {
    document.details["spouse"].disabled = true
  }
}





function strlen(field,len,alerttxt){
if (field.value.length != len){
alert(alerttxt);field.focus();return false
}
}






function validate_required(field,alerttxt)
{
with (field)
{
if (value==null||value=="")
  {alert(alerttxt);return false}
else {return true}
}
}

function radio_check(){
var radios = document.forms.details.elements.sex;
for (var i=0; i<radios.length; i++){
if(radios[i].checked) {return true}
}
alert('You must select a gender!')
return false
}





function phone_check(field){
  with(document.details){
    if (field=="term"){
      var term
      term=((term_ph_area.value-0)+(term_ph.value-0)+(work_ph_area.value-0)+( work_ph.value-0)+(mobile.value-0));
      if (term=="0"||term==""||term==null){
        alert("You must input at least one phone number for term.");term_ph_area.focus();return false
      } else {
        if ((term_ph_area.value=="") && (term_ph.value=="")){
          if ((work_ph_area.value=="") && (work_ph.value=="")){
            if (strlen(mobile,10,"Invalid Mobile Number")==false){
              return false
            }
          } else {
            if ((work_ph_area.value=="") && (work_ph.value!="")){
              alert("Please enter area code for your work phone");work_ph_area.focus();return false
            }
            if ((work_ph_area.value!="") && (work_ph.value=="")){
              alert("Please enter more than just an area code for your work phone");work_ph.focus();return false
            }
            if (strlen(work_ph_area,2,"Invalid Work Phone Area")==false){
              return false
            }
            if (strlen(work_ph,8,"Invalid Work Phone Number")==false){
              return false
            }
          }
        } else {
          if ((term_ph_area.value=="") && (term_ph.value!="")){
            alert("Please enter area code for your term phone");term_ph_area.focus();return false
          }
          if ((term_ph_area.value!="") && (term_ph.value=="")){
            alert("Please enter more than just an area code for your term phone");term_ph.focus();return false
          }
          if (strlen(term_ph_area,2,"Invalid Term Phone Area")==false){
            return false
          }
          if (strlen(term_ph,8,"Invalid Term Phone Number")==false){
            return false
          }
        }
      }
    }

    if (field=="home" && document.details["home=term"].checked == false){
      var home
      home=((home_ph_area.value-0)+(home_ph.value-0)+(home_work_ph_area.value-0)+(home_work_ph.value-0));
      if (home=="0"||home==""||home==null){
        alert("You must input at least one phone number for home.");return false
      }else{
        if ((home_ph_area.value=="") && (home_ph.value=="")){
          if ((home_work_ph_area.value=="") && (home_work_ph.value!="")){
            alert("Please enter area code for your work phone");home_work_ph_area.focus();return false
          }
          if ((home_work_ph_area.value!="") && (home_work_ph.value=="")){
            alert("Please enter more than just an area code for your work phone");home_work_ph.focus();return false
          }
          if (strlen(home_work_ph_area,2,"Invalid Work Phone Area")==false){
            return false
          }
          if (strlen(home_work_ph,8,"Invalid Work Phone Number")==false){
            return false
          }
        } else {
          if ((home_ph_area.value=="") && (home_ph.value!="")){
            alert("Please enter area code for your home phone");home_ph_area.focus();return false
          }
          if ((home_ph_area.value!="") && (home_ph.value=="")){
            alert("Please enter more than just an area code for your home phone");home_ph.focus();return false
          }
          if (strlen(home_ph_area,2,"Invalid Home Phone Area")==false){
            return false
          }
          if (strlen(home_ph,8,"Invalid Home Phone Number")==false){
            return false
          }
        }
      }
    }
    
    if (field=="em1"){
      var em1
      em1=((em1_home_area.value-0)+(em1_home.value-0)+(em1_work_area.value-0)+(em1_work.value-0)+(em1_mobile.value-0));
      if (em1=="0"||em1==""||em1==null){
        alert("You must input at least one phone number for Emergency Contact 1.");return false
      }else {
        if ((em1_home_area.value=="") && (em1_home.value=="")){
          if ((em1_work_area.value=="") && (em1_work.value=="")){
            if (strlen(em1_mobile,10,"Invalid Mobile Number")==false){
              return false
            }
          } else {
            if ((em1_work_area.value=="") && (em1_work.value!="")){
              alert("Please enter area code for Emergency Contact 1's work phone");em1_work_area.focus();return false
            }
            if ((em1_work_area.value!="") && (em1_work.value=="")){
              alert("Please enter more than just an area code for Emergency Contact 1's work phone");em1_work.focus();return false
            }
            if (strlen(em1_work_area,2,"Invalid Emergency Contact 1 Work Phone Area")==false){
              return false
            }
            if (strlen(em1_work,8,"Invalid Emergency Contact 1 Work Phone Number")==false){
              return false
            }
          }
        } else {
          if ((em1_home_area.value=="") && (em1_home.value!="")){
            alert("Please enter area code for Emergency Contact 1's home phone");em1_home_area.focus();return false
          }
          if ((em1_home_area.value!="") && (em1_home.value=="")){
            alert("Please enter more than just an area code for Emergency Contact 1's home phone");em1_home.focus();return false
          }
          if (strlen(em1_home_area,2,"Invalid Emergency Contact 1 Home Phone Area")==false){
            return false
          }
          if (strlen(em1_home,8,"Invalid Emergency Contact 1 Home Phone Number")==false){
            return false
          }
        }
      }
    }
    
    if (field=="em2"){
      var em2
      em2=((em2_home_area.value-0)+(em2_home.value-0)+(em2_work_area.value-0)+(em2_work.value-0)+(em2_mobile.value-0));
      if (em2=="0"||em2==""||em2==null){
        alert("You must input at least one phone number for Emergency Contact 2.");return false
      }else {
        if ((em2_home_area.value=="") && (em2_home.value=="")){
          if ((em2_work_area.value=="") && (em2_work.value=="")){
            if (strlen(em2_mobile,10,"Invalid Mobile Number")==false){
              return false
            }
          } else {
            if ((em2_work_area.value=="") && (em2_work.value!="")){
              alert("Please enter area code for Emergency Contact 2's work phone");em2_work_area.focus();return false
            }
            if ((em2_work_area.value!="") && (em2_work.value=="")){
              alert("Please enter more than just an area code for Emergency Contact 2's work phone");em2_work.focus();return false
            }
            if (strlen(em2_work_area,2,"Invalid Emergency Contact 2 Work Phone Area")==false){
              return false
            }
            if (strlen(em2_work,8,"Invalid Emergency Contact 2 Work Phone Number")==false){
              return false
            }
          }
        } else {
          if ((em2_home_area.value=="") && (em2_home.value!="")){
            alert("Please enter area code for Emergency Contact 2's home phone");em2_home_area.focus();return false
          }
          if ((em2_home_area.value!="") && (em2_home.value=="")){
            alert("Please enter more than just an area code for Emergency Contact 2's home phone");em2_home.focus();return false
          }
          if (strlen(em2_home_area,2,"Invalid Emergency Contact 2 Home Phone Area")==false){
            return false
          }
          if (strlen(em2_home,8,"Invalid Emergency Contact 2 Home Phone Number")==false){
            return false
          }
        }
      }
    }
    return true
  }
}






function echeck(str) {

var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)

if (str.indexOf(at)==-1)
{alert("Invalid E-mail");return false}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
{alert("Invalid E-mail");return false}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
{alert("Invalid E-mail");return false}

if (str.indexOf(at,(lat+1))!=-1)
{alert("Invalid E-mail");return false}

if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
{alert("Invalid E-mail");return false}

if (str.indexOf(dot,(lat+2))==-1)
{alert("Invalid E-mail");return false}
		
if (str.indexOf(" ")!=-1)
{alert("Invalid E-mail");return false}

if (ldot==(lstr-1))
{alert("Invalid Email");return false}

return true					

}





function validate_form(thisform)
{
with (thisform)
{
if (validate_required(surname,"Surname must be filled out")==false)
  {surname.focus();return false} 
if (validate_required(first_name,"All given name/s must be filled out")==false)
  {first_name.focus();return false} 
if (radio_check()==false)
  {name.focus();return false} 

if (uni.value == "Please select uni"){
alert("Please select a university campus.");uni.focus();return false
}


if (validate_required(term_add1,"Term street Address must be filled out")==false)
  {term_add1.focus();return false}  
if (validate_required(term_add2,"Term suburb must be filled out")==false)
  {term_add2.focus();return false} 
if (validate_required(pcode,"Term postcode must be filled out")==false)
  {pcode.focus();return false} 
if (strlen(pcode,4,"Invalid Postcode")==false)
  {return false} 
if (phone_check("term")==false)
  {return false} 
if (validate_required(email,"Email must be filled out")==false)
  {email.focus();return false}
if (echeck(email.value)==false)
  {email.focus();return false}
if (validate_required(home_add1,"Home street Address must be filled out")==false)
  {home_add1.focus();return false}  
if (validate_required(home_add2,"Home suburb must be filled out")==false)
  {home_add2.focus();return false} 
if (validate_required(home_pcode,"Home postcode must be filled out")==false)
  {home_pcode.focus();return false} 
if (strlen(home_pcode,4,"Invalid Home postcode")==false)
  {return false} 
if (phone_check("home")==false)
  {return false} 
if (validate_required(em1_name,"Emergency Contact 1's name must be filled out")==false)
  {em1_name.focus();return false}
if (validate_required(em1_rel,"Emergency Contact 1's relationship to you must be filled out")==false)
  {em1_rel.focus();return false}
if (phone_check("em1")==false)
  {return false} 
if (validate_required(em2_name,"Emergency Contact 2's name must be filled out")==false)
  {em2_name.focus();return false}
if (validate_required(em2_rel,"Emergency Contact 2's relationship to you must be filled out")==false)
  {em2_rel.focus();return false}
if (phone_check("em2")==false)
  {return false} 
}
}

