﻿function returnObjById(id) { 
    if (document.getElementById) 
        var returnVar = document.getElementById(id); 
    else if (document.all) 
        var returnVar = document.all[id]; 
    else if (document.layers) 
        var returnVar = document.layers[id]; 
    return returnVar; 
}
function CheckEmailExists(eMail)
{
    frm=document.forms[0];
    eMail = eMail.trim();   
    if(eMail == "")
    {
        // Empty email ID
        var RsltElem = returnObjById('ResultId');
        RsltElem.innerHTML = "Please enter an email id."
        returnObjById('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 = returnObjById('ResultId');
            RsltElem.innerHTML = "Please enter a valid email id."
            returnObjById('ctl00_ContentPlaceHolder1_btnRegister').disabled=true;
            returnObjById('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 = returnObjById('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=" + returnObjById('ctl00_ContentPlaceHolder1_txtEmail').value + "'>Click Here</a> to reset or assign a new password.<br />This will be emailed to you.";
        returnObjById('ctl00_ContentPlaceHolder1_btnRegister').disabled=true;
        returnObjById('ctl00_ContentPlaceHolder1_txtPassword').focus();
    }
    else if(result == "FALSE")
    {
        // Success
        RsltElem.innerHTML = "";
        returnObjById('ctl00_ContentPlaceHolder1_btnRegister').disabled=false;
        returnObjById('ctl00_ContentPlaceHolder1_txtPassword').focus();
    }
    else if(result == "NULL")
    {
        RsltElem.innerHTML = "Please enter an email ID.";
        returnObjById('ctl00_ContentPlaceHolder1_btnRegister').disabled=true;
    }
    else if(result == "ERROR")
    {
        RsltElem.innerHTML = "Error in connection. Please try again.";
        returnObjById('ctl00_ContentPlaceHolder1_btnRegister').disabled=true;
        returnObjById('ctl00_ContentPlaceHolder1_txtEmail').focus();
    }
    else
    {
        RsltElem.innerHTML = "Unknown error. Please try again after some time.";
        returnObjById('ctl00_ContentPlaceHolder1_btnRegister').disabled=true;
        returnObjById('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 = returnObjById('ResultId');
    RsltElem.innerHTML = "Service Error. Please try again after some time." 
    returnObjById('ctl00_ContentPlaceHolder1_btnRegister').disabled=true;
    returnObjById('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();


