﻿	// masking the phone # field with "-"
	function mask(str, textbox, loc, delim)
	{
		var locs = loc.split(',');
		for (var i = 0; i <= locs.length; i++){
			for (var k = 0; k <= str.length; k++){
				if (k == locs[i]){
					if (str.substring(k, k+1) != delim){
							str = str.substring(0,k) + delim + str.substring(k,str.length)
					}
				}
			}
		}
		textbox.value = str
		return false;
	}
	
	// enforce only numberic character entered
	function numbersonly(myfield, e, dec)
    {
        var key;
        var keychar;

        if (window.event)
           key = window.event.keyCode;
        else if (e)
           key = e.which;
        else
           return true;
        keychar = String.fromCharCode(key);

        // control keys
        if ((key==null) || (key==0) || (key==8) || 
            (key==9) || (key==27) )
           return true;

        if (key==13)
            return false;
            
        // numbers
        else if ((("0123456789").indexOf(keychar) > -1))
           return true;

        // decimal point jump
        else if (dec && (keychar == "."))
           {
           myfield.form.elements[dec].focus();
           return false;
           }
        else
           return false;
    }

    // validate mandatory field
    function CheckEmpty(textbox)
    {
        if (textbox.value == "")
        {
            textbox.className = "fieldinput_invalid";
            return 1;
        }
        textbox.className = "fieldinput_validate";
        return 0;
    }
    
    // validate min length
    function CheckMinLength(textbox, minlength,alerttxt)
    {

        if(textbox.value.length == 0)
            return true;
            
        if(textbox.value.length < minlength)
        {
            alert(alerttxt);
            textbox.focus();
            return false;
        }
        return true;
    }
    
    // validate mandatory dropdown
    function CheckNoSelection(dropdown)
    {
        if(dropdown.selectedIndex == 0)
        {
            dropdown.className = "fieldinput_invalid";
            return 1
        }   
        dropdown.className = "fieldinput_validate";
        return 0;
    }
    
    // validate email format
    function ValidateEmail(field,alerttxt)
    {
        with (field)
        {
            apos=value.indexOf("@");
            dotpos=value.lastIndexOf(".");
            if (apos<1||dotpos-apos<2)
            {
                alert(alerttxt);
                field.focus();
                return false;
            }
            else 
            {
                return true;
            }
        }
    }    
    
    // validate postal code
    function ValidatePostalCode(val)
    {    
        var regEx = /[a-zA-Z][0-9][a-zA-Z]( )[0-9][a-zA-Z][0-9]/;    
        if(regEx.test(val)){
            return true;    
        }    
        else {
            alert("Invalid Postal Code!");
            return false;    
        }
    }
    
        // validate numeric value only
    function IsNumeric(str, maxDotCount, alerttxt)
    {
        if (str.length == 0)
            return true;
        
        numdecs = 0;
        for (i = 0; i < str.length; i++)
        {
            mychar = str.charAt(i);
            if ((mychar >= "0" && mychar <= "9") || mychar == "." )
            {
                if (mychar == ".")
                    numdecs++;
            }
            else
            {
                alert(alerttxt);
                return false;
            }
        }
        if (numdecs > maxDotCount)
        {
            alert(alerttxt);
            return false;
        }
        return true;
    }
    
    // validate numeric field
    function IsNumber(field,maxDotCount,alerttxt)
    {
        str = field.value;
        if (IsNumeric(str, maxDotCount, alerttxt)){
            return true;
        }
        else{
            field.focus();
            return false;
        }
    }    
    
    // validate phone num
    function ValidatePhoneNum(txtPhoneNum)
    {
        str = txtPhoneNum.value;
        if (str != "")
        {
            str = str.replace("-", "").replace("-", "");
            if (!IsNumeric(str, 0, "Invalid Phone #")){
                return false;
            }
            else{
                if (!CheckMinLength(txtPhoneNum, 12, "Invalid Phone #"))
                    return false;
                else
                    return true;
            }
        }
        else
            return true;
    }
    
    function VerifyFormFields(ClientID)
    {
        errCount = 0;
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtFirstName")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtLastName")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtParentFirstName")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtParentLastName")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtGrade")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtHomePhone")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtOfficePhone")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtEMail")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtAltEmail")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtEmergencyContactName")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtEContactPhone")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtStreetAdd")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtCity")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtPostalCode")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtMajorIntersection")[0]);
        errCount += CheckEmpty(document.getElementsByName(ClientID + "txtNearestIntersection")[0]);
        chkAllegies = document.getElementsByName(ClientID + "RadioButton1")[0];

        if (chkAllegies.checked)
        {
            ddlEpipen = document.getElementsByName(ClientID + "ddEpiPen")[0];
            if (ddlEpipen.options[ddlEpipen.selectedIndex].value == "Yes")
                errCount += CheckEmpty(document.getElementsByName(ClientID + "txtNearestIntersection")[0]);
        }
                //alert(errCount);
        if (errCount > 0)
        {
            alert("Please complete all mandatory fields");
            return false;
        }
        //Validate fields value
        if (!ValidatePhoneNum(document.getElementsByName(ClientID + "txtHomePhone")[0]))
            return false;
        if (!ValidatePhoneNum(document.getElementsByName(ClientID + "txtOfficePhone")[0]))
            return false;
        if (!ValidatePhoneNum(document.getElementsByName(ClientID + "txtCellPhone")[0]))
            return false;
        if (!ValidatePhoneNum(document.getElementsByName(ClientID + "txtEContactPhone")[0]))
            return false;
        if (!ValidateEmail(document.getElementsByName(ClientID + "txtEMail")[0], "Please enter a valid email address."))
            return false;
        if (!ValidateEmail(document.getElementsByName(ClientID + "txtAltEmail")[0], "Please enter a valid email address."))
            return false;
        if (!IsNumber(document.getElementsByName(ClientID + "txtGrade")[0], 0, "Please enter a valid Grade."))
            return false;
        if (!ValidatePostalCode(document.getElementsByName(ClientID + "txtPostalCode")[0]))
            return false;
        if (!document.getElementsByName(ClientID + "chkGeneralAgreement")[0].checked){
            alert("Must accept all the above agreements!");
            return false;
        }
        if (!document.getElementsByName(ClientID + "chkStudentAgreement")[0].checked){
            alert("Must accept all the above agreements!");
            return false;
        }
        if (!document.getElementsByName(ClientID + "chkParentAgreement")[0].checked){
            alert("Must accept all the above agreements!");
            return false;
        }
        return true;
    }
