// Function to Check whether the field is blank or not
// ===================================================
function isBlank(s) {
    for (var i = 0; i < s.length; i++) {
        var c = s.charAt(i);
        if ((c != ' ') && (c != '\n') && (c != '\t'))
            return false;
    }
    return true;
}


// Function to Check for valid email address
// =========================================
function theFormEmail1(theForm, item) {

    var emailPat = /^(.+)@(.+)$/;
    var specialChars = "\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
    var validChars = "\[^\\s" + specialChars + "\]";
    var quotedUser = "(\"[^\"]*\")";
    var ipDomainPat = /^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
    var atom = validChars + '+';
    var word = "(" + atom + "|" + quotedUser + ")";
    var userPat = new RegExp("^" + word + "(\\." + word + ")*$");
    var domainPat = new RegExp("^" + atom + "(\\." + atom + ")*$");
    var matchArray = theForm.match(emailPat);
    if (matchArray == null) {
        alert("Email address seems incorrect (check @ and .'s)");
        item.focus();
        return false;
    }
    var user = matchArray[1];
    var domain = matchArray[2];

    if (user.match(userPat) == null) {
        alert("The username doesn't seem to be valid.");
        item.focus();
        return false;
    }

    var IPArray = domain.match(ipDomainPat);
    if (IPArray != null) {
        for (var i = 1; i <= 4; i++) {
            if (IPArray[i] > 255) {
                alert("Destination IP address is invalid!");
                item.focus();
                return false;
            }
        }
        return true;
    }
    var domainArray = domain.match(domainPat)
    if (domainArray == null) {
        alert("The domain name doesn't seem to be valid.");
        item.focus();
        return false;
    }

    var atomPat = new RegExp(atom, "g");
    var domArr = domain.match(atomPat);
    var len = domArr.length;
    if (domArr[domArr.length - 1].length < 2 ||
			    domArr[domArr.length - 1].length > 3) {
        alert("The address must end in a three-letter domain, or two letter country.");
        item.focus();
        return false;
    }

    if (len < 2) {
        var errStr = "This address is missing a hostname!";
        alert(errStr);
        item.focus();
        return false;
    }
    return true;
}

//=========================================
function theFormEmail(theForm, item) {
    if (isBlank(theForm)) {
        alert("Email required. Thank you.");
        item.focus();
        return false;
    }
    else {
        count = 0;
        flagdot = 0;
        pos = 0;
        str = theForm;
        l = str.length;
        for (i = 0; i < l; i++) {
            p = str.charAt(i)
            if (p == "@") {
                count++; pos = i;
            }
            if (p == ".") {
                flagdot = i;
            }
        }

        if (count == 1) {
            if (flagdot > 0) {
                return true;
            }
            else {
                alert("Please insert a correct email ID");
                item.focus();
                return false;
            }
        }
        else {
            alert("Please insert a correct email ID");
            item.focus();
            return false;
        }
    }
}

// Function to Check for all the mandatory fields
// ==============================================
function validateRegistration(theForm) {

    // Check for First Name	
    if (isBlank(theForm.fname.value)) {
        alert("First Name should not be Empty");
        theForm.fname.focus();
        return false;
    }

    // Check for Last Name	
    if (isBlank(theForm.lname.value)) {
        alert("Last Name should not be Empty");
        theForm.lname.focus();
        return false;
    }

    // Check for gender
    /*
    if (theForm.gender.selectedIndex == 0) {
        alert("Please select your gender");
        theForm.gender.focus();
        return false;
    }

    // Check for birthday
    if (theForm.birthday.selectedIndex == 0) {
        alert("Please select the day of birth");
        theForm.birthday.focus();
        return false;
    }

    // Check for birthmonth
    if (theForm.birthmonth.selectedIndex == 0) {
        alert("Please select the month of birth");
        theForm.birthmonth.focus();
        return false;
    }

    // Check for birthyear
    if (theForm.birthyear.selectedIndex == 0) {
        alert("Please select the year of birth");
        theForm.birthyear.focus();
        return false;
    }
    */
    // Check for UserID
    if (isBlank(theForm.user_id.value)) {
        alert("UserName should not be Empty");
        theForm.user_id.focus();
        return false;
    }
    else {
        for (i = 0; i < theForm.user_id.value.length; i++) {
            if (((theForm.user_id.value.charCodeAt(i) < 48) || (theForm.user_id.value.charCodeAt(i) > 57)) && ((theForm.user_id.value.charCodeAt(i) < 65) || (theForm.user_id.value.charCodeAt(i) > 90)) && ((theForm.user_id.value.charCodeAt(i) < 97) || (theForm.user_id.value.charCodeAt(i) > 122)) && (theForm.user_id.value.charCodeAt(i) != 45) && (theForm.user_id.value.charCodeAt(i) != 46) && (theForm.user_id.value.charCodeAt(i) != 95)) {
                alert("Invalid characters not allowed in User ID");
                theForm.user_id.focus();
                return false;
            }
        }
    }

    // Check for Password
    if (isBlank(theForm.passwd.value)) {
        alert("Password should not be Empty");
        theForm.passwd.focus();
        return false;
    }
    else {
        for (i = 0; i < theForm.passwd.value.length; i++) {
            if (((theForm.passwd.value.charCodeAt(i) < 48) || (theForm.passwd.value.charCodeAt(i) > 57)) && ((theForm.passwd.value.charCodeAt(i) < 65) || (theForm.passwd.value.charCodeAt(i) > 90)) && ((theForm.passwd.value.charCodeAt(i) < 97) || (theForm.passwd.value.charCodeAt(i) > 122)) && (theForm.passwd.value.charCodeAt(i) != 45) && (theForm.passwd.value.charCodeAt(i) != 46) && (theForm.passwd.value.charCodeAt(i) != 95)) {
                alert("Invalid characters not allowed in Password");
                theForm.passwd.focus();
                return false;
            }
        }
    }

    // Check for Confirm Password
    if (isBlank(theForm.cpasswd.value)) {
        alert("Confirm Password should not be Empty");
        theForm.cpasswd.focus();
        return false;
    }
    else {
        for (i = 0; i < theForm.cpasswd.value.length; i++) {
            if (((theForm.cpasswd.value.charCodeAt(i) < 48) || (theForm.cpasswd.value.charCodeAt(i) > 57)) && ((theForm.cpasswd.value.charCodeAt(i) < 65) || (theForm.cpasswd.value.charCodeAt(i) > 90)) && ((theForm.cpasswd.value.charCodeAt(i) < 97) || (theForm.cpasswd.value.charCodeAt(i) > 122)) && (theForm.cpasswd.value.charCodeAt(i) != 45) && (theForm.cpasswd.value.charCodeAt(i) != 46) && (theForm.cpasswd.value.charCodeAt(i) != 95)) {
                alert("Invalid characters not allowed in Confirm Password");
                theForm.cpasswd.focus();
                return false;
            }
        }
    }

    // Check for maching password

    var a = theForm.passwd.value
    var b = theForm.cpasswd.value;
    if (a != b) {
        alert("Your password doesn't match with the confirm password");
        theForm.cpasswd.focus();
        return false;
    }

    // code to convert passsword to HEx before sending over internet
    /*
    // Check for Primary Email
    if (!theFormEmail1(theForm.pemail.value, theForm.pemail)) {
    return false;
    }
    
    // Check for Primary email of IITBombay
    var peml = theForm.pemail.value
    if (peml.search("iitbombay.org") != -1) {
    alert("You cannot use iitbombay.org/iitmumbai.org  in your primary email");
    theForm.pemail.focus();
    return false;
    }

    // Check for Secondary email of IITBombay
    var seml = theForm.semail.value
    if (seml.length > 0) {
    if (seml.search("iitbombay.org") != -1) {
    alert("You cannot use iitbombay.org/iitmumbai.org  in your Secondary email");
    theForm.semail.focus();
    return false;
    }
    }

    // Check for Degree
    if (theForm.degree.selectedIndex == 0) {
    alert("Degree should not be Empty");
    theForm.degree.focus();
    return false;
    }

    // Check for Year
    if (theForm.grad_year.selectedIndex == 0) {
    alert("Year should not be Empty");
    theForm.grad_year.focus();
    return false;
    }

    // Check for Department
    if (theForm.department.selectedIndex == 0) {
    alert("Department should not be Empty");
    theForm.department.focus();
    return false;
    }
    // Check for Department
    if (theForm.Deg1.selectedIndex != 0) {
    if (theForm.DegYear1.selectedIndex == 0) {
    alert("Please Fill the Year of Degree");
    theForm.DegYear1.focus();
    return false;
    }
    if (theForm.DegDept1.selectedIndex == 0) {
    alert("Please select the Department");
    theForm.DegDept1.focus();
    return false;
    }

    }
    if (theForm.Deg2.selectedIndex != 0) {

        if (theForm.DegYear2.selectedIndex == 0) {
    alert("Please Fill the Year of Degree");
    theForm.DegYear2.focus();
    return false;
    }
    if (theForm.DegDept2.selectedIndex == 0) {
    alert("Please select the department");
    theForm.DegDept2.focus();
    return false;
    }

    }
    if (theForm.Deg3.selectedIndex != 0) {
    if (theForm.DegYear3.selectedIndex == 0) {
    alert("Please Fill the Year of Degree");
    theForm.DegYear3.focus();
    return false;
    }
    if (theForm.DegDept3.selectedIndex == 0) {
    alert("Please select the department");
    theForm.DegDept3.focus();
    return false;
    }
    }

    // Check for Address1
    if (isBlank(theForm.haddress1.value)) {
    alert("Address should not be Empty");
    theForm.haddress1.focus();
    return false;
    }

    // Check for City
    if (isBlank(theForm.hcity.value)) {
    alert("City should not be Empty");
    theForm.hcity.focus();
    return false;
    }

    // Check for State
    if (isBlank(theForm.hstate.value)) {
    alert("State should not be Empty");
    theForm.hstate.focus();
    return false;
    }

    // Check for Postal Code
    if (isBlank(theForm.hpostal_code.value)) {
    alert("Postal Code should not be Empty");
    theForm.hpostal_code.focus();
    return false;
    }

    // Check for Country
    if (isBlank(theForm.hcountry.value)) {
    alert("Country should not be Empty");
    theForm.hcountry.focus();
    return false;
    }

    // Check for Area Of Residence
    if (theForm.resi_area.selectedIndex == 0) {
    alert("Please Select Residential Area");
    theForm.resi_area.focus();
    return false;
    }

    // Check for chapter
    if (theForm.chapter.selectedIndex == 0) {
    alert("Please Select Chapter");
    theForm.chapter.focus();
    return false;
    }

    // Check for Phone
    if (isBlank(theForm.hphone.value)) {
    alert("Phone should not be Empty");
    theForm.hphone.focus();
    return false;
    }

    // Check for Company
    if (isBlank(theForm.wcompany.value)) {
    alert("Company should not be Empty");
    theForm.wcompany.focus();
    return false;
    }
    */
    return true;
}

