﻿function CheckEmailExists(eMail)
{
    frm=document.forms[0];
    eMail = eMail.trim();   
    if(eMail == "")
    {
        // Empty email ID
        var RsltElem = document.getElementById("ResultId");
        RsltElem.innerHTML = "Please enter an email id."
        frm.ctl00_ContentPlaceHolder1_btnRegister.disabled=true;
    }
    else
    {
        if(echeck(eMail) == true)
        {
            // Valid email ID
            Palgrave.Register.CheckEmailService.CheckEmailExists(eMail,SucceededCallback);
        }
        else
        {
            // Invalid email ID
            var RsltElem = document.getElementById("ResultId");
            RsltElem.innerHTML = "Please enter a valid email id."
            frm.ctl00_ContentPlaceHolder1_btnRegister.disabled=true;
            frm.ctl00_ContentPlaceHolder1_txtEmail.focus();
        }
    }
}

// This is the callback function invoked if the Web service
// succeeded.
// It accepts the result object as a parameter.
function SucceededCallback(result, eventArgs)
{
    frm=document.forms[0]
    
    // Page element to display feedback.
    var RsltElem = document.getElementById("ResultId");
    
    result = result.trim();
    
    if(result == "TRUE")
    {
        RsltElem.innerHTML = "We already have a record for this email address.<br />Please <a href='ResetPassword.aspx?Email=" + frm.ctl00_ContentPlaceHolder1_txtEmail.value + "'>Click Here</a> to reset or assign a new password.<br />This will be emailed to you.";
        frm.ctl00_ContentPlaceHolder1_btnRegister.disabled=true;
        frm.ctl00_ContentPlaceHolder1_txtPassword.focus();
    }
    else if(result == "FALSE")
    {
        // Success
        RsltElem.innerHTML = "";
        frm.ctl00_ContentPlaceHolder1_btnRegister.disabled=false;
        frm.ctl00_ContentPlaceHolder1_txtPassword.focus();
    }
    else if(result == "NULL")
    {
        RsltElem.innerHTML = "Please enter an email ID.";
        frm.ctl00_ContentPlaceHolder1_btnRegister.disabled=true;
    }
    else if(result == "ERROR")
    {
        RsltElem.innerHTML = "Error in connection. Please try again.";
        frm.ctl00_ContentPlaceHolder1_btnRegister.disabled=true;
        frm.ctl00_ContentPlaceHolder1_txtEmail.focus();
    }
    else
    {
        RsltElem.innerHTML = "Unknown error. Please try again after some time.";
        frm.ctl00_ContentPlaceHolder1_btnRegister.disabled=true;
        frm.ctl00_ContentPlaceHolder1_txtEmail.focus();
    }
}


// This is the callback function invoked if the Web service
// failed.
// It accepts the error object as a parameter.
function FailedCallback(error)
{
    frm=document.forms[0]
    // Display the error.    
    var RsltElem = 
        document.getElementById("ResultId");
    RsltElem.innerHTML = "Service Error. Please try again after some time." 
    frm.ctl00_ContentPlaceHolder1_btnRegister.disabled=true;
    frm.ctl00_ContentPlaceHolder1_txtEmail.focus();
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		  // alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
 

if (typeof(Sys) !== "undefined") Sys.Application.notifyScriptLoaded();

