﻿// JScript File
var xmlHttpEmail;
var parentPageTemp;
var userPreviousURL;

function Authenticate(parentPage)
{ 
    _userName = document.getElementById("txtLoginUserName").value.replace("'","''");
    _password = document.getElementById("txtLoginPassword").value;
    parentPageTemp = parentPage;
    
    document.getElementById("divLogin").innerHTML="&nbsp;";
    
    if(_userName != "" && _password != "")
    {
        _userName = trim(_userName);
       // _password = trim(_password);
        if(_password.indexOf("&")!=-1)
        {
            //_password=_password.replace('&',"#100");
             _password= escape(_password);
        }
        if(parentPage.search(/home page/i) >= 0)
        {
            var loadstatustext="<img src='Images/home page/loading.gif' /><span style='color:Black'>&nbsp;Login in progress...</span>";
        }
        else
        {
            var loadstatustext="<img src='../../Images/home page/loading.gif' /><span style='color:Black'>&nbsp;Login in progress...</span>";
        }
        document.getElementById("spnRequestContents").innerHTML = loadstatustext;
        xmlHttpEmail=null;
        xmlHttpEmail=GetXmlHttpObject();
        if (xmlHttpEmail==null)
        {
            alert("Browser does not support HTTP Request.");
            return;
        } 
        if(parentPage.search(/home page/i) >= 0)
        {
            var url="WebPages/EmailTemp.aspx";
        }
        else
        {
            var url="../EmailTemp.aspx";    
        } 
        //url=url;
        //url=url+"&sid="+Math.random()
        
        url=url + "?varUserName="+_userName+"&varPassword="+_password;
        
        var parameters="varUserName="+_userName+"&varPassword="+_password;
        
        xmlHttpEmail.onreadystatechange=stateChanged;
        xmlHttpEmail.open("GET",url,true);
        //xmlHttpEmail.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        //xmlHttpEmail.setRequestHeader("Connection", "Keep-Alive");       
        xmlHttpEmail.send(null);
        return false;
    }
    else if(_userName == "")
    {
        var loadstatustext="<span style='color:red'>Please enter Email ID.</span>"
        document.getElementById("spnRequestContents").innerHTML = loadstatustext;
        return false;
    }
    else if(_password == "")
    {
        var loadstatustext="<span style='color:red'>Please enter Password.</span>"
        document.getElementById("spnRequestContents").innerHTML = loadstatustext;
        return false;
    }
}
//---------------------------------------------------------------------------------------------
function trim(stringToTrim) 
{
    if(stringToTrim != null)
    {
	    return stringToTrim.replace(/^\s+|\s+$/g,"");
	}
	else
	{
	    return null;
	}
}
//---------------------------------------------------------------------------------------------
function stateChanged() 
{ 
 
    if (xmlHttpEmail.readyState==4 )
    { 
       
       if((xmlHttpEmail.responseText).search(/Successfull/i) >= 0)
       {
            if (parentPageTemp.search(/home page/i) >= 0 )
            {
               self.location.href = "MyHomePage.aspx";
            }
            else
            {
               self.location.href = "../../MyHomePage.aspx";
            }
            /*
            document.getElementById("spnRequestContents").innerHTML = "";
            //var xmlDoc=xmlHttpEmail.responseXML;
            var str;
            str=xmlHttpEmail.responseText;        
            str=str.replace("<form","<div");
            document.getElementById("divLogin").innerHTML = str;
            */
       }
       else
       {
            document.getElementById("spnRequestContents").innerHTML = "";
            //var xmlDoc=xmlHttpEmail.responseXML;
            var str;
            str=xmlHttpEmail.responseText;
            str=str.replace("<form","<div");
            document.getElementById("divLogin").innerHTML = str;
       }
 
    } 
} 
     


function GetXmlHttpObject()
{ 
    var objXMLHttp=null;
    if (window.XMLHttpRequest)
    {
        objXMLHttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {
        objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    return objXMLHttp;
}
//function UnloadRequstingImage()
//{  
//    document.getElementById("spnRequestContents").innerHTML = "HI";
//}

//function SetFocus_LoginButton()
//{
//    var btnLoginObj = document.getElementById("btnLogin");
//    if(btnLoginObj!= null && btnLoginObj !="")
//    {
//        btnLoginObj.focus();
//        return true; 
//    }
//    else
//    {
//        return false;
//    }
//}

function checkEnterKeyHomePage(event)
{

    if(event.keyCode == 13)
    {
        Authenticate("home page");
    }
}

function checkEnterKeyOnlineRegistration(event)
{

    if(event.keyCode == 13)
    {
        Authenticate("online registration");
    }
    if(document.getElementById("hidLoginByEnterKey")!=null)
    { document.getElementById("hidLoginByEnterKey").value="EnterKey";
    }
    return false;
}

//function checkEnterKeyIndividual(event)
//{

//    if(event.keyCode == 13)
//    {
//         //Authenticate("online registration");
//        var btnLoginObj = document.getElementById("TopFrame1_btnLogin");
//        if (document.all)
//	    {
//		    // IE
//		     //btnLoginObj.focus();		   
//             btnLoginObj.click();
//		    //var tempLogId = document.getElementById("lblWorkoutType");    
//	    }
//	    else
//	    {
//		    // FireFox 
//		    //btnLoginObj.focus();		   
//		    var e = document.createEvent("MouseEvents");		    
//		    e.initEvent("click", true, true);
//		    btnLoginObj.dispatchEvent(e);
//	    }
//        
//    }
//}
//To avoid post back
function IgnoreEnterKey(e)
{   
    var controlTagName;
    var inputType;
    if(e.srcElement)
    {
        controlTagName = e.srcElement.tagName;
        inputType = e.srcElement.type;
    }
    else
    {
        controlTagName = e.target.tagName;
        inputType = e.target.getAttribute("type");          
    }
    
    var textBox = "";
    if(controlTagName == "INPUT" && inputType == "text" || inputType == "password")
    {
        textBox = "true";
    }
    else if(controlTagName == "INPUT")
    {
        textBox = "false";
    }
    
    if(controlTagName == "TEXTAREA" || controlTagName == "IMG" || controlTagName == "A" )
    {
        // Do nothing allow enter key 
    }
    else if(textBox == "true")
    {
        if (e.keyCode == 13)
        {
            if (e.stopPropagation) 
            {
                e.stopPropagation();
                e.preventDefault();  
            }
            else
            {
                e.returnValue=false;
                e.cancel = true;
            }
        }
    }
    else
    {
        if (e.keyCode == 13)
        {
            if (e.stopPropagation) 
            {
                e.stopPropagation();
                e.preventDefault();
            }
            else
            {
                e.returnValue=false;
                e.cancel = true;
            }
        }
    }
}



/*Code for Login Popup */
function AuthenticateLoginCredentials(parentPage)
{ 
    _userName = document.getElementById("TopFrame1_txtEmailId").value.replace("'","''");
    _password = document.getElementById("TopFrame1_txtPassword").value;
    parentPageTemp = parentPage;
    
    document.getElementById("divLoginPopup").innerHTML="&nbsp;";
    
    if(_userName != "" && _password != "")
    {
        _userName = trim(_userName);
       // _password = trim(_password);
        if(_password.indexOf("&")!=-1)
        {
            //_password=_password.replace('&',"#100");
             _password= escape(_password);
        }
        
        if(parentPage.search(/home page/i) >= 0)
        {
            var loadstatustext="<img src='Images/home page/loading.gif' /><span style='color:Black'>&nbsp;Login in progress...</span>";
        }
        else
        {
            var loadstatustext="<img src='../../Images/home page/loading.gif' /><span style='color:Black'>&nbsp;Login in progress...</span>";
        }
       
        document.getElementById("spnRequestContentsLoginPopup").innerHTML = loadstatustext;
        xmlHttpEmail=null;
        xmlHttpEmail=GetXmlHttpObject();
        if (xmlHttpEmail==null)
        {
            alert("Browser does not support HTTP Request.");
            return;
        } 
         
        if(parentPage.search(/home page/i) >= 0)
        {
           var url="WebPages/EmailTemp.aspx";
        }
        else
        {
            var url="../EmailTemp.aspx";    
        } 
        //url=url;
        //url=url+"&sid="+Math.random()
        
        url=url + "?varUserName="+_userName+"&varPassword="+_password;
        
        var parameters="varUserName="+_userName+"&varPassword="+_password;
        
        xmlHttpEmail.onreadystatechange=stateChangedLoginPopup;
        xmlHttpEmail.open("GET",url,true);
        //xmlHttpEmail.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        //xmlHttpEmail.setRequestHeader("Connection", "Keep-Alive");       
        xmlHttpEmail.send(null);
        return false;
    }
    else if(_userName == "")
    {
        var loadstatustext="<span style='color:red'>Please enter Email ID.</span>"
        document.getElementById("spnRequestContentsLoginPopup").innerHTML = loadstatustext;
        return false;
    }
    else if(_password == "")
    {
        var loadstatustext="<span style='color:red'>Please enter Password.</span>"
        document.getElementById("spnRequestContentsLoginPopup").innerHTML = loadstatustext;
        return false;
    }
}

function stateChangedLoginPopup() 
{ 
 
    if (xmlHttpEmail.readyState==4 )
    { 
       
       if((xmlHttpEmail.responseText).search(/Successfull/i) >= 0)
       {
         
            if (parentPageTemp.search(/home page/i) >= 0 )
            {
               if (( window.location.href.search(/sessionExpired.aspx/i) >= 0)||( window.location.href.search(/onlineRegistration.aspx/i) >= 0))
               {
                self.location.href = "MyHomePage.aspx";
               }
               else if (( window.location.href.search(/EventSalesPage.aspx/i) >= 0)||( window.location.href.search(/EventSalesPageCustomization.aspx/i) >= 0) || ( window.location.href.search(/EventSalesPageTools.aspx/i) >= 0))
               {
                self.location.href = "WebPages/EventCalendar/AddEvent.aspx";
               }
               else
               {
                 window.location.href=window.location.href;
               }
            }
            else
            {   
                 if (( window.location.href.search(/sessionExpired.aspx/i) >= 0)||( window.location.href.search(/onlineRegistration.aspx/i) >= 0))
                {
                    self.location.href = "../../MyHomePage.aspx";
                 }
                else if (( window.location.href.search(/EventSalesPage.aspx/i) >= 0)||( window.location.href.search(/EventSalesPageCustomization.aspx/i) >= 0) || ( window.location.href.search(/EventSalesPageTools.aspx/i) >= 0))
               {
                self.location.href = "../../WebPages/EventCalendar/AddEvent.aspx";
               }
               else
               {
                 window.location.href=window.location.href;
               }
            }
       }
       if((xmlHttpEmail.responseText).search(/Inactive/i) >= 0)
       {
           var loadstatustext="<span style='color:red'>Inactive user account.</span>"
            document.getElementById("spnRequestContentsLoginPopup").innerHTML = loadstatustext;
            return false;
       }
       if((xmlHttpEmail.responseText).search(/Invalid/i) >= 0)
       {
           var loadstatustext="<span style='color:red'>Invalid Email Id or Password.</span>"
            document.getElementById("spnRequestContentsLoginPopup").innerHTML = loadstatustext;
            return false;
       }
    } 
} 
     


/*End of Code for Login Popup */
