// JavaScript Document

////////////////Check fields for text//////////////
var g_alerttxt = "";
var isValid = true;
function validate_required(field,alerttxt)
{
   with (field)
   {
      if (value==null||value=="")
      {
         g_alerttxt = g_alerttxt + alerttxt;
         return false
      }
      else
      {
         return true;
      }

   }
   
}

//////////////ZIP code validation////////////////////
function IsZip(sText)

{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   var i;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }

   return IsNumber;   
}

//////////////////Study Score validation/////////////////////
function validate_SC(field)
{
   with (field)
   {
      if (value==null||value=="")
      {
         isValid = false;
         style.backgroundColor='orange';
      }
      
      else if (!IsZip(value))
      {
         isValid = false;
         style.backgroundColor='orange';
      }
         
      else if((value < 0))
      {
         isValid = false;
         style.backgroundColor='orange';
      }
      
      else
      {
         style.backgroundColor='white';
      }
                  
   }
   
}

//////////////ENTER validation///////////////////////
function IsENTER(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   var i;
 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
   }

   return IsNumber;   
}

/**
 * DHTML date validation script for dd/mm/yyyy. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
   var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
   // February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
   for (var i = 1; i <= n; i++) {
      this[i] = 31
      if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
      if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
   var daysInMonth = DaysArray(12)
   var pos1=dtStr.indexOf(dtCh)
   var pos2=dtStr.indexOf(dtCh,pos1+1)
   var strDay=dtStr.substring(0,pos1)
   var strMonth=dtStr.substring(pos1+1,pos2)
   var strYear=dtStr.substring(pos2+1)
   strYr=strYear
   if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
   if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
   for (var i = 1; i <= 3; i++) {
      if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
   }
   month=parseInt(strMonth)
   day=parseInt(strDay)
   year=parseInt(strYr)
   if (pos1==-1 || pos2==-1){
      //alert("The date format should be : dd/mm/yyyy")
      return false
   }
   if (strMonth.length<1 || month<1 || month>12){
      //alert("Please enter a valid month")
      return false
   }
   if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
      //alert("Please enter a valid day")
      return false
   }
   if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
      //alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
      return false
   }
   if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
      //alert("Please enter a valid date")
      return false
   }
return true
}

function ValidateForm(){
   var dt=document.frmSample.txtDate
   if (isDate(dt.value)==false){
      dt.focus()
      return false
   }
    return true
 }

////////////////////////SubjectStudyScore validation//////////////////////////
//check which boxes are checked and if corresponding study score box is filled correctly

function validateSubjectStudyScore(thisform)
{
   var elCB = document.getElementsByName('myCB[]');
   var elSC = document.getElementsByName('mySC[]');

   //alert(elCB.length);
   
   for(var j = 0; j < elCB.length; ++j)
   {
         
      if(elCB[j].checked)
      {
         //alert(j + "checked");
         validate_SC(elSC[j]);
         
      }
      else
      {
         //alert(j + " not checked");
         elSC[j].style.backgroundColor='white';
         elSC[j].value = null;
      }
   }
   
}

////////////////Call function and check tutor_enrol for student form////////////////////////////
function validate_form_tutorstudent(thisform)
{
   isValid = true;

   with (thisform)
   {
      
      if (validate_required(FirstName,"- First name must be filled out.\n")==false)
      {
         FirstName.focus();
         FirstName.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         FirstName.style.backgroundColor='white';
      }
      
      if (validate_required(LastName,"- Last name must be filled out.\n")==false)
      {
         LastName.focus();
         LastName.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         LastName.style.backgroundColor='white';
      }
      
      if (validate_required(DOB,"- Date of Birth must be filled out.\n")==false)
      {
         DOB.focus();
         DOB.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         DOB.style.backgroundColor='white';
      }
      
      if (isDate(DOB.value)==false)
      {
         DOB.focus()
         DOB.style.backgroundColor='orange';
         isValid = false
      }
      else
      {
         DOB.style.backgroundColor='white';
      }     
      
      if (validate_required(Telephone, "- Telephone must be filled out.\n")==false)
      {
         Telephone.focus();
         Telephone.style.backgroundColor='orange';
         isValid = false;
      }
      else

      {
         Telephone.style.backgroundColor='white';
      }
      if (validate_required(Address,"- Address must be filled out.\n")==false)
      {
         Address.focus();
         Address.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Address.style.backgroundColor='white';
      }
      if (validate_required(Suburb,"- Suburb must be filled out.\n")==false)
      {
         Suburb.focus();
         Suburb.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Suburb.style.backgroundColor='white';
      }
      if (validate_required(ENTER,"- ENTER score must be filled out.\n")==false)
      {
         ENTER.focus();
         ENTER.style.backgroundColor='orange';        
         isValid = false;
      }
      else if(IsENTER(ENTER.value) == false)
      {
         ENTER.focus();
         g_alerttxt = g_alerttxt + "- ENTER must be numeric.\n";
         ENTER.style.backgroundColor='orange';
         isValid = false;
      }
      else if(ENTER.value > 99.95)
      {
         ENTER.focus();
         g_alerttxt = g_alerttxt + "- ENTER cannot be higher than 99.95.\n";
         ENTER.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         ENTER.style.backgroundColor='white';
      }
      
      if (validate_required(VCEComp,"- Year VCE Completed must be filled out.\n")==false)
      {
         VCEComp.focus();
         VCEComp.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         VCEComp.style.backgroundColor='white';
      }
      
      if (validate_required(StartUNI,"- Year Started University must be filled out.\n")==false)
      {
         StartUNI.focus();
         StartUNI.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         StartUNI.style.backgroundColor='white';
      }
      

      
      /*
      if(Subjects.selectedIndex < 0)
      {
         Subjects.focus();
         Subjects.style.backgroundColor='orange';
         g_alerttxt = g_alerttxt + "- Select subject(s)\n";
         isValid = false;
      }
      else
      {
         Subjects.style.backgroundColor='white';
      }
      */
      
      // Validate subject and study score
      validateSubjectStudyScore(thisform);
      
      if (validate_required(Email,"- Email must be filled out.\n")==false)
      {
         Email.focus();
         Email.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Email.style.backgroundColor='white';
      }
      if (validate_required(ZipCode,"- Zip Code must be filled out.\n")==false)
      {
         ZipCode.focus();
         ZipCode.style.backgroundColor='orange';
         isValid = false;
      }     
      else if(IsZip(ZipCode.value) == false)
      {
         ZipCode.focus();
         ZipCode.style.backgroundColor='orange';
         g_alerttxt = g_alerttxt + "- Zip Code must be numeric.\n";
         isValid = false;
      }
      else if(ZipCode.value.length != 4)
      {
         ZipCode.focus();
         ZipCode.style.backgroundColor='orange';
         g_alerttxt = g_alerttxt + "- Zip Code must be 4 digits.\n";
      }
      else
      {
         ZipCode.style.backgroundColor='white';
      }
      if (validate_required(Password,"- Password must be filled out.\n")==false)
      {
         Password.focus();
         Password.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Password.style.backgroundColor='white';
      }
      
      if (validate_required(Confirm,"- You must confirm your password.\n")==false)
      {
         Confirm.focus();
         Confirm.style.backgroundColor='orange';
         isValid = false;
      }
      else if (Password.value != Confirm.value)
      {  
         Password.focus();
         Confirm.style.backgroundColor='orange';
         Password.style.backgroundColor='orange';
         Password.value = ""
         Confirm.value = ""
         alert("Password and confirmation do not match.");
         isValid = false;
      }
      else
      {
         Confirm.style.backgroundColor='white';
      }

      if(!isValid)
      {
      // alert(g_alerttxt);
      // g_alerttxt = "";
         return false;        
      }
      else
      {
      // alert("all is good");
         return true;
      }
   }
   
}



////////////////Call function and check tutor_enrol for teacher form///////////////////////////
function validate_form_tutorteacher(thisform)
{
   isValid = true;
   
   with (thisform)
   {
      
      if (validate_required(FirstName,"- First name must be filled out.\n")==false)
      {
         FirstName.focus();
         FirstName.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         FirstName.style.backgroundColor='white';
      }
      
      if (validate_required(LastName,"- Last name must be filled out.\n")==false)
      {
         LastName.focus();
         LastName.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         LastName.style.backgroundColor='white';
      }
      
      
      if (validate_required(DOB,"- Date of Birth must be filled out.\n")==false)
      {
         DOB.focus();
         DOB.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         DOB.style.backgroundColor='white';
      }
      
      if (isDate(DOB.value)==false)
      {
         DOB.focus()
         DOB.style.backgroundColor='orange';
         isValid = false
      }
      else
      {
         DOB.style.backgroundColor='white';
      }     
      
      
      if (validate_required(Telephone, "- Telephone must be filled out.\n")==false)
      {
         Telephone.focus();
         Telephone.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Telephone.style.backgroundColor='white';
      }
      
      if (validate_required(Address,"- Address must be filled out.\n")==false)
      {
         Address.focus();
         Address.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Address.style.backgroundColor='white';
      }
      
      
      if (validate_required(Suburb,"- Suburb must be filled out.\n")==false)
      {
         Suburb.focus();
         Suburb.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Suburb.style.backgroundColor='white';
      }
      
      if (validate_required(Email,"- Email must be filled out.\n")==false)
      {
         Email.focus();
         Email.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Email.style.backgroundColor='white';
      }
            
      
      if (validate_required(ZipCode,"- Zip Code must be filled out.\n")==false)
      {
         ZipCode.focus();
         ZipCode.style.backgroundColor='orange';
         isValid = false;
      }     
      else if(IsZip(ZipCode.value) == false)
      {
         ZipCode.focus();
         ZipCode.style.backgroundColor='orange';
         g_alerttxt = g_alerttxt + "- Zip Code must be numeric.\n";
         isValid = false;
      }
      else if(ZipCode.value.length != 4)
      {
         ZipCode.focus();
         ZipCode.style.backgroundColor='orange';
         g_alerttxt = g_alerttxt + "- Zip Code must be 4 digits.\n";
      }
      else
      {
         ZipCode.style.backgroundColor='white';
      }
      
      if (validate_required(Password,"- Password must be filled out.\n")==false)
      {
         Password.focus();
         Password.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Password.style.backgroundColor='white';
      }
      
      if (validate_required(Confirm,"- You must confirm your password.\n")==false)
      {
         Confirm.focus();
         Confirm.style.backgroundColor='orange';
         isValid = false;
      }
      else if (Password.value != Confirm.value)
      {  
         Password.focus();
         Confirm.style.backgroundColor='orange';
         Password.style.backgroundColor='orange';
         Password.value = ""
         Confirm.value = ""
         alert("Password and confirmation do not match.");
         isValid = false;
      }
      else
      {
         Confirm.style.backgroundColor='white';
      }
      
      
      
      if (validate_required(VITnumber,"- VITNumber must be filled out.\n")==false)
      {
         VITnumber.focus();
         VITnumber.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         VITnumber.style.backgroundColor='white';
      }


      if(!isValid)
      {
      // alert(g_alerttxt);
      // g_alerttxt = "";
         return false;        
      }
      else
      {
      // alert("all is good");
         return true;
      }
   }
   
}

//////////////Call functions and check student enrol form////////////////
function validate_student_form(thisform)
{
   isValid = true;


   with (thisform)
   {
/*

      if (validate_required(PFirstName,"- First name must be filled out.\n")==false)
      {
         PFirstName.focus();
         PFirstName.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         PFirstName.style.backgroundColor='white';
      }
      if (validate_required(PLastName,"- First name must be filled out.\n")==false)
      {
         PLastName.focus();
         PLastName.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         PLastName.style.backgroundColor='white';
      }
      if (validate_required(SFirstName,"- First name must be filled out.\n")==false)
      {
         SFirstName.focus();
         SFirstName.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         SFirstName.style.backgroundColor='white';
      }
      
      if (validate_required(SLastName,"- First name must be filled out.\n")==false)
      {
         SLastName.focus();
         SLastName.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         SLastName.style.backgroundColor='white';
      }
      if (validate_required(Telephone,"- First name must be filled out.\n")==false)
      {
         Telephone.focus();
         Telephone.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Telephone.style.backgroundColor='white';
      }
      if (validate_required(Address,"- First name must be filled out.\n")==false)
      {
         Address.focus();
         Address.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Address.style.backgroundColor='white';
      }
      
      if (validate_required(Suburb,"- First name must be filled out.\n")==false)
      {
         Suburb.focus();
         Suburb.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Suburb.style.backgroundColor='white';
      }
      
      
      if (validate_required(ZipCode,"- First name must be filled out.\n")==false)
      {
         ZipCode.focus();
         ZipCode.style.backgroundColor='orange';
         isValid = false;
      }
      else if(IsZip(ZipCode.value) == false)
      {
         ZipCode.focus();
         ZipCode.style.backgroundColor='orange';
         isValid = false;
      }
      else if(ZipCode.value.length != 4)
      {
         ZipCode.focus();
         ZipCode.style.backgroundColor='orange';
      }
      else
      {
         ZipCode.style.backgroundColor='white';
      }
      
      var listValue = Subjects.defaultValue;
      if(listValue == 0)
         {
         Subjects.focus;
         Subjects.style.backgroundColor='orange';
         alert("enter");
         return false;
         }
      
      
      
      if (validate_required(Email,"- First name must be filled out.\n")==false)
      {
         Email.focus();
         Email.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Email.style.backgroundColor='white';
      }
      
      if (validate_required(Password,"- First name must be filled out.\n")==false)
      {
         Password.focus();
         Password.style.backgroundColor='orange';
         isValid = false;
      }
      else
      {
         Password.style.backgroundColor='white';
      }
      
      if (validate_required(Confirm,"- First name must be filled out.\n")==false)
      {
         Confirm.focus();
         Confirm.style.backgroundColor='orange';
         isValid = false;
      }
      else if(Confirm.value != Password.value)
      {
         Password.focus();
         Confirm.style.backgroundColor='orange';
         Password.style.backgroundColor='orange';
         Password.value = "";
         Confirm.value = "";
         alert("Password and confirmation do not match.");
         isValid = false;
      }
      else
      {
         Confirm.style.backgroundColor='white';
      }
      */
      
      if(!isValid)
      {
         return false;        
      }
      else
      {
         return true;
      }
      //return false;
   }

}
