function validateEmpty(fld, fld_name) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field " + fld_name + " has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function validateDateSelect(fldDay, fldMonth, fldYear, fld_name) {
    var error = "";
 
    if (fldDay.value.length == 0) {
        fldDay.style.background = 'Yellow'; 
        error = "The required field " + fld_name + " has not been filled in correctly.\n"
    } else {
        fldDay.style.background = 'White';
    }
    if (fldMonth.value.length == 0) {
        fldMonth.style.background = 'Yellow'; 
        error = "The required field " + fld_name + " has not been filled in correctly.\n"
    } else {
        fldMonth.style.background = 'White';
    }
    if (fldYear.value.length == 0) {
        fldYear.style.background = 'Yellow'; 
        error = "The required field " + fld_name + " has not been filled in correctly.\n"
    } else {
        fldYear.style.background = 'White';
    }
    return error;  
}

function validateRadio(fld, fld_name) {
    var error = "";
	var myOption = -1;
 
	for (i=fld.length-1; i > -1; i--) {
		if (fld[i].checked) {
			myOption = i;
			i = -1;
		}
	}

    if (myOption == -1) {
        //fld.style.background = 'Yellow'; 
        error = "The required field " + fld_name + " has not been selected.\n"
    }
    return error;  
}

function validateCheckbox(fld, fld_name) {
    var error = "";
 
	if (fld.checked == false) {
        error = "The required field " + fld_name + " has not been selected.\n"
	}

    return error;  
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}

function validate1012FormOnSubmit() {
var reason = "";
  
  reason += validateEmpty(document.reg.student_email, "Student Email");  
  reason += validateEmpty(document.reg.legal_surname, "Legal Surname");
  reason += validateEmpty(document.reg.previous_surname, "Previous Surname(s)");
  reason += validateEmpty(document.reg.legal_middle_name, "Legal Middle Name");
  reason += validateEmpty(document.reg.legal_first_name, "Legal First Name");
  reason += validateRadio(document.reg.gender, "Gender");
  reason += validateDateSelect(document.reg.dob_day, document.reg.dob_month, document.reg.dob_year, "Date of Birth");
  reason += validatePhone(document.reg.phone_home);
  reason += validateEmpty(document.reg.student_email, "Student's Email");
  reason += validateEmpty(document.reg.parent_email, "Parent's Email");
  reason += validateRadio(document.reg.bc_resident, "Resident of BC");
  reason += validateRadio(document.reg.citizenship, "Citizenship");
  reason += validateEmpty(document.reg.home_address, "Home Address");
  reason += validateEmpty(document.reg.home_city, "City");
  reason += validateEmpty(document.reg.home_province, "Province");
  reason += validateEmpty(document.reg.home_country, "Country");
  reason += validateEmpty(document.reg.home_postal_code, "Postal Code");
  reason += validateEmpty(document.reg.first_last_parent_1, "First & Last name of Parent/Guardian");
  reason += validateEmpty(document.reg.parent_1_relationship, "Parent/Guardian Relationship");
  reason += validateRadio(document.reg.aboriginal, "First Nations, Metis, Inuit Aboriginal Ancestory");
  reason += validateRadio(document.reg.received_la, "Has student received Learning Assistance?");
  reason += validateRadio(document.reg.had_iep, "Has student had an Individual Education Plan?");
  reason += validateRadio(document.reg.grade_last_completed, "Grade you last completed");
  reason += validateRadio(document.reg.existing_nides_student, "Are you currently enrolled at NIDES?");
  reason += validateRadio(document.reg.remaining_enrolled, "Are you remaining enrolled at another school?");
  reason += validateCheckbox(document.reg.agree_1, "First 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_2, "Second 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_3, "Third 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_8, "Fourth 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_5, "Fifth 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_6, "Sixth 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_travel, "Travel 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_7, "Seventh 'I Agree' checkbox");
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateK9FormOnSubmit() {
var reason = "";

  reason += validateRadio(document.reg.start_date, "Start Date");  
  reason += validateEmpty(document.reg.student_email, "Student Email");  
  reason += validateEmpty(document.reg.legal_surname, "Legal Surname");
  reason += validateEmpty(document.reg.legal_middle_name, "Legal Middle Name");
  reason += validateEmpty(document.reg.legal_first_name, "Legal First Name");
  reason += validateRadio(document.reg.gender, "Gender");
  reason += validateDateSelect(document.reg.dob_day, document.reg.dob_month, document.reg.dob_year, "Date of Birth");
  reason += validatePhone(document.reg.phone_home);
  reason += validateEmpty(document.reg.student_email, "Student's Email");
  reason += validateEmpty(document.reg.parent_email, "Parent's Email");
  reason += validateRadio(document.reg.bc_resident, "Resident of BC");
  reason += validateRadio(document.reg.citizenship, "Citizenship");
  reason += validateEmpty(document.reg.home_address, "Home Address");
  reason += validateEmpty(document.reg.home_city, "City");
  reason += validateEmpty(document.reg.home_province, "Province");
  reason += validateEmpty(document.reg.home_country, "Country");
  reason += validateEmpty(document.reg.home_postal_code, "Postal Code");
  reason += validateEmpty(document.reg.first_last_parent_1, "First & Last name of Parent/Guardian");
  reason += validateEmpty(document.reg.parent_1_relationship, "Parent/Guardian Relationship");
  reason += validateEmpty(document.reg.home_facilitator_name, "Home Facilitator's Name");
  reason += validateRadio(document.reg.aboriginal, "First Nations, Metis, Inuit Aboriginal Ancestory");
  reason += validateEmpty(document.reg.care_card, "Care Card #");
  reason += validateEmpty(document.reg.doctor_name, "Doctor's Name");
  reason += validateEmpty(document.reg.doctor_phone, "Doctor's Phone");
  reason += validateEmpty(document.reg.dentist_name, "Dentist's Name");
  reason += validateEmpty(document.reg.dentist_phone, "Dentist's Phone");
  reason += validateEmpty(document.reg.allergies_health_conditions, "Allergies and Health Conditions");
  reason += validateRadio(document.reg.life_threatening, "Life Threatening?");
  reason += validateRadio(document.reg.eclass, "Interaction Day Request");
  reason += validateRadio(document.reg.received_la, "Has student received Learning Assistance?");
  reason += validateRadio(document.reg.had_iep, "Has student had an Individual Education Plan?");
  reason += validateRadio(document.reg.psychoed_assessment, "Has there been a Psycho-educational Assessment?");
  reason += validateRadio(document.reg.computer, "Do you have a computer?");
  reason += validateRadio(document.reg.internet_connection, "Do you have an internet connection?");
  reason += validateRadio(document.reg.existing_nides_student, "Are you currently enrolled at NIDES?");
  reason += validateRadio(document.reg.grade_applying_for, "Grade you are applying for");
  reason += validateRadio(document.reg.grade_last_completed, "Grade you last completed");
  reason += validateDateSelect(document.reg.date_last_grade_completed_day, document.reg.date_last_grade_completed_month, document.reg.date_last_grade_completed_year, "Date last grade completed");
  reason += validateEmpty(document.reg.name_last_school, "Name of last school");
  reason += validateEmpty(document.reg.reasons_for_withdrawal, "Reasons for withdrawal");
  reason += validateCheckbox(document.reg.agree_1, "First 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_2, "Second 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_3, "Third 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_5, "Fifth 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_6, "Sixth 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_7, "Seventh 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_8, "Eighth 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_travel, "Travel 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_9, "Ninth 'I Agree' checkbox");
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

function validateK9ReRegFormOnSubmit() {
var reason = "";

  reason += validateRadio(document.reg.start_date, "Start Date");  
  reason += validateEmpty(document.reg.student_email, "Student Email");  
  reason += validateEmpty(document.reg.legal_surname, "Legal Surname");
  reason += validateEmpty(document.reg.legal_middle_name, "Legal Middle Name");
  reason += validateEmpty(document.reg.legal_first_name, "Legal First Name");
  reason += validatePhone(document.reg.phone_home);
  reason += validateEmpty(document.reg.student_email, "Student's Email");
  reason += validateEmpty(document.reg.parent_email, "Parent's Email");
  reason += validateRadio(document.reg.bc_resident, "Resident of BC");
  reason += validateRadio(document.reg.citizenship, "Citizenship");
  reason += validateEmpty(document.reg.first_last_parent_1, "First & Last name of Parent/Guardian");
  reason += validateEmpty(document.reg.parent_1_relationship, "Parent/Guardian Relationship");
  reason += validateEmpty(document.reg.home_facilitator_name, "Home Facilitator's Name");
  reason += validateRadio(document.reg.eclass, "Interaction Day Request");
  reason += validateRadio(document.reg.grade_applying_for, "Grade you are applying for");
  reason += validateRadio(document.reg.grade_last_completed, "Grade you last completed");
  reason += validateCheckbox(document.reg.agree_1, "First 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_2, "Second 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_3, "Third 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_5, "Fifth 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_6, "Sixth 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_7, "Seventh 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_8, "Eighth 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_travel, "Travel 'I Agree' checkbox");
  reason += validateEmpty(document.reg.questions_comments, "Contact Info Changes");
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}


function validateGiftedFormOnSubmit() {
var reason = "";

  reason += validateRadio(document.reg.start_date, "Start Date");  
  reason += validateEmpty(document.reg.student_email, "Student Email");  
  reason += validateEmpty(document.reg.legal_surname, "Legal Surname");
  reason += validateEmpty(document.reg.previous_surname, "Previous Surname(s)");
  reason += validateEmpty(document.reg.legal_middle_name, "Legal Middle Name");
  reason += validateEmpty(document.reg.legal_first_name, "Legal First Name");
  reason += validateRadio(document.reg.gender, "Gender");
  reason += validateDateSelect(document.reg.dob_day, document.reg.dob_month, document.reg.dob_year, "Date of Birth");
  reason += validatePhone(document.reg.phone_home);
  reason += validateEmpty(document.reg.student_email, "Student's Email");
  reason += validateEmpty(document.reg.parent_email, "Parent's Email");
  reason += validateRadio(document.reg.bc_resident, "Resident of BC");
  reason += validateEmpty(document.reg.home_address, "Home Address");
  reason += validateEmpty(document.reg.home_city, "City");
  reason += validateEmpty(document.reg.home_province, "Province");
  reason += validateEmpty(document.reg.home_country, "Country");
  reason += validateEmpty(document.reg.home_postal_code, "Postal Code");
  reason += validateEmpty(document.reg.first_last_parent_1, "First & Last name of Parent/Guardian");
  reason += validateEmpty(document.reg.parent_1_relationship, "Parent/Guardian Relationship");
  reason += validateRadio(document.reg.aboriginal, "First Nations, Metis, Inuit Aboriginal Ancestory");
  reason += validateRadio(document.reg.existing_nides_student, "Are you currently enrolled at NIDES?");
//  reason += validateRadio(document.reg.remaining_enrolled, "Are you remaining enrolled at another school?");
  reason += validateEmpty(document.reg.name_last_school, "Name of school");
  reason += validateRadio(document.reg.current_grade, "Your current grade");
  reason += validateCheckbox(document.reg.agree_1, "First 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_2, "Second 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_3, "Third 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_5, "Fifth 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_6, "Sixth 'I Agree' checkbox");
  reason += validateCheckbox(document.reg.agree_7, "Seventh 'I Agree' checkbox");
      
  if (reason != "") {
    alert("Some fields need correction:\n" + reason);
    return false;
  }

  return true;
}

/**
 * DHTML date validation script. 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 validateDate(fld, fld_text){
	var error = "";
	var dtStr = fld.value;
	var daysInMonth = DaysArray(12);
	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strYear=dtStr.substring(0,pos1);
	var strMonth=dtStr.substring(pos1+1,pos2);
	var strDay=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){
        fld.style.background = 'Yellow'; 
		error = fld_text + ": The date format should be : yyyy-mm-dd\n";
	}
	if (strMonth.length<1 || month<1 || month>12){
        fld.style.background = 'Yellow'; 
		error = fld_text + ": Please enter a valid month\n";
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
        fld.style.background = 'Yellow'; 
		error = fld_text + ": Please enter a valid day\n";
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
        fld.style.background = 'Yellow'; 
		error = fld_text + ": Please enter a valid 4 digit year between "+minYear+" and "+maxYear+"\n";
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
        fld.style.background = 'Yellow'; 
		error = fld_text + ": Please enter a valid date\n";
	}
	
	return error;
}
