﻿var errorEmailAddr = "<p>Please enter a valid email address.</p>";
var errorMembershipNo = "<p>Please enter your membership number.</p>";
var errorCredentialsNotFound = "<p>Sorry, your credentials could not be found.</p>";

   
function Login()
{      
    var emailValid = ValidateEmail();
    var membershipnoValid = ValidateMembershipNo();
    
    if( emailValid == false || membershipnoValid == false )
    {
        return false;
    }   

    Login_xmlhttpPost("/PageTemplates/FormProcessing.aspx?" + 
                "FormType=TeacherLogin" +
                
                "&Email=" + escape(document.getElementById('teachersemail').value) +
                "&MembershipNo=" + escape(document.getElementById('teachersmembershipno').value));
      
    return false;
}

function Login_xmlhttpPost(strURL) 
{
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('GET', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            Login_updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(null);
}


function Login_updatepage(str)
{
    if (str.substring(0,6) != "failed")
    {
        window.location = str;    
    }
    else
    {
        document.getElementById('ErrTeachersEmail').innerHTML = errorCredentialsNotFound;
        document.getElementById('ErrTeachersEmail').style.display = "inline";
    }
}

function ValidateEmail()
{
    var email = document.getElementById('teachersemail').value;
    if ( (email.indexOf("@") > 1) && //  must contain @, and it must not be the first character
          (email.lastIndexOf(".") > email.indexOf("@")) &&  // last dot must be after the @
          (email.indexOf("@") != email.length) &&  // @ must not be the last character
          (email.indexOf("..") < 0) && // two periods in a row is not valid
          (email.indexOf(".") != email.length) &&  // . must not be the last character
          (AllValidEmailChars(email)) ) // all characters must be valid
    {
        document.getElementById('ErrTeachersEmail').style.display = "none";
        document.getElementById('ErrTeachersEmail').innerHTML = "";
	    return true;
    }
    else
    {
        document.getElementById('ErrTeachersEmail').style.display = "inline";
        document.getElementById('ErrTeachersEmail').innerHTML = errorEmailAddr;
        return false;
    }
}

function ValidateMembershipNo()
{
    if (document.getElementById('teachersmembershipno').value != "")
    {
        document.getElementById('ErrTeachersMembershipNo').style.display = "none";
        document.getElementById('ErrTeachersMembershipNo').innerHTML = "";
	    return true;
    }
    else
    {
        document.getElementById('ErrTeachersMembershipNo').style.display = "inline";
        document.getElementById('ErrTeachersMembershipNo').innerHTML = errorMembershipNo;
        return false;
    }
}

function AllValidEmailChars(email)
{
    var isValid = true;
    var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
    for (var i=0; i < email.length; i++) 
    {
        var letter = email.charAt(i).toLowerCase();
        if (validchars.indexOf(letter) != -1)
        {
          continue;
        }
        else
        {
            isValid = false;
            break;
        }
    }
    return isValid;
}