﻿function logIn(obj)
{
    var txtId=document.getElementById("txtLogin");
    var txtPwd=document.getElementById("txtpass");
    var objMain=document.getElementById("dvLogin");
    var objErrLabel=document.getElementById("lblError");
    var objLogout=document.getElementById("dvLogOut");
    if(txtId !=null && txtPwd !=null && objMain!=null && objErrLabel !=null && objLogout !=null)
    {
        if(txtId.value != "" && txtPwd.value != "")
        {
            document.getElementById("dvProcess").style.display="block";
            objMain.disabled=true;        
            obj.disabled=true;
            obj.value="wait...";
            objErrLabel.innerHTML="";
            objLogout.innerHTML="";
            if(validateEmail(txtId.value) && txtPwd.value.length>4) 
            {
                var xmlHttpReq = false;
                var self = this;
                if (window.XMLHttpRequest) 
                {
                    self.xmlHttpReq = new XMLHttpRequest();        
                }
                else if (window.ActiveXObject) 
                {
                    self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");         
                }
                self.xmlHttpReq.open('POST', "ajaxCollection.aspx", true);             
                self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                self.xmlHttpReq.onreadystatechange = function() 
                {
                    if (self.xmlHttpReq.readyState == 4) 
                    {          
                        document.getElementById("dvProcess").style.display="none";
                        objMain.disabled=false;
                        obj.disabled=false;
                        obj.value="";
                        
                        var GetValue=new Array();
                        if(self.xmlHttpReq.responseText.indexOf('~' != -1))
                        {
                            GetValue=self.xmlHttpReq.responseText.split("~");
                        }
                        if(GetValue[0].toString()!="")
                        {
                            //alert(GetValue[0]);
                            if(GetValue[0].toString()=="0")
                            {
                                objErrLabel.innerHTML=GetValue[1].toString();   
                                objErrLabel.style.display="block";
                            }
                            else
                            {
                                //alert(GetValue[1].toString());
                                objMain.style.display="none";
                                objErrLabel.style.display="none";
                                //objErrLabel.innerHTML="Welcome " + GetValue[1].toString(); 
                                //objLogout.innerHTML="<a href='#' title='Log Out' onclick='javascript:logout();'>Log Out</a>";
                                objLogout.style.display="block";
                                objLogout.innerHTML=GetValue[1].toString(); 
                                                                
                            }
                        }
                        else
                        {
                            alert("There is some error. Can not let you log in. Please try later.");
                            objLogout.style.display="none";
                        }
                    }
                    else
                    {
                        document.getElementById("dvProcess").style.display="block";
                        objMain.disabled=true;
                        objLogout.style.display="none";
                        objErrLabel.style.display="none";
                    }
                }                
                self.xmlHttpReq.send("mode=logIn&id="+ txtId.value + "&pwd="+txtPwd.value); 
            }
            else
            {
                alert("Please make sure you have entered a valid email address and password more than 4 letters.\n Then try again.");
                document.getElementById("dvProcess").style.display="none";
                objLogout.style.display="none";
                objErrLabel.style.display="none";
                objMain.disabled=false;
                obj.disabled=false;
                obj.value="";
            }
        }
        else
        {
            obj.disabled=false;
            objMain.disabled=false;
            objLogout.style.display="none";
            objErrLabel.style.display="none";
            obj.value="";
            alert("Please enter Login Id, password and then try again.");
            document.getElementById("txtLogin").focus();
        }  
    }
    else
    {
        alert("Cannot let you login due to congestion.");
    }
}
function logout(obj)
{
    var objMain=document.getElementById("UcHeader1_login");
    if(objMain!=null)
    {    
        obj.disabled=true;
        objMain.style.display="none";
        objMain.disabled=true;
        var xmlHttpReq = false;
        var self = this;
        if (window.XMLHttpRequest) 
        {
            self.xmlHttpReq = new XMLHttpRequest();        
        }
        else if (window.ActiveXObject) 
        {
            self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");         
        }
        self.xmlHttpReq.open('POST', "ajaxCollection.aspx", true);             
        self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
        self.xmlHttpReq.onreadystatechange = function() 
        {
            obj.disabled=false;
            objMain.disabled=false;
            objMain.style.display="block";
            document.getElementById("dvProcess").style.display="none";            
            if (self.xmlHttpReq.readyState == 4) 
            {          
                if(self.xmlHttpReq.responseText.toString()!="")
                {
                    objMain.innerHTML=self.xmlHttpReq.responseText.toString();
                    window.location.href="default.aspx";                                       
                }
                else
                {
                    alert("There is some error. Can not let you log out. Please try later.");
                }
            }
            else
            {                
                obj.disabled=true;
                objMain.disabled=true;
                objMain.style.display="none";
                document.getElementById("dvProcess").style.display="block";
            }
        }                
        self.xmlHttpReq.send("mode=logOut"); 
    }
    else
    {
        alert("Objects not found. Try again.");
        obj.disabled=false;
        objMain.disabled=false;
    }
}
function validateEmail(str)
{
    if(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		
	 }
	 else
	 {
	    return false;
	 }			
}

function allowedKey(e)
{
	var KeyID = (window.event) ? event.keyCode : e.which;
	if((KeyID >= 33 && KeyID <= 47) || (KeyID == 32) || (KeyID >= 58 && KeyID <= 64) ||
	   (KeyID >= 91 && KeyID <= 94) || (KeyID == 96) || (KeyID >= 123 && KeyID <= 126))
	{
		return false;
	}
	return true;
}