﻿var sessionID;var bType;var lang;
var ErroTxtArray = new Array();
function getElementsByClassName(oElm, strTagName, strClassName)
{	
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");var oElement;
	for(var i=0; i<arrElements.length; i++)
	{
		oElement = arrElements[i];      
		if(oRegExp.test(oElement.className))
		{arrReturnElements.push(oElement);}   
	}
	return (arrReturnElements);
}
function getElementPos(sElementID)
{
	if(!document.getElementById) return null;
	var e = document.getElementById(sElementID);
	if(e == null ) return null;	
	return getPosition(e);
}
function getPosition(Elemenent)
{
    var p = { x: 0, y: 0};
	while(Elemenent)
	{
	p.x += parseInt(Elemenent.offsetLeft,10);
	p.y += parseInt(Elemenent.offsetTop,10);
	Elemenent = Elemenent.offsetParent;
	}
	return p;
}

function IsEmailValid(EmailId)
{			
     var objRegex = new RegExp(/^.+@.+\..{2,4}$/);
     return objRegex.test(EmailId);
}
function LoginTimeout(ret){alert('Logged in');}
function DoLoginBoxClick(Type , LanguageId1 , sessionID1 , e)
{	
    try{
	    if(e.which || event.keyCode)
	    {
		    if(e.which == 13 || event.keyCode == 13)
		    {
			    switch(Type)
			    {
				    case '0':
				        ValidateLogin(LanguageId1 , sessionID1 );
				        return false;				    
					    break;
				    case '1':
					    SendPassToEmail(LanguageId1);
					    return false;
					    break;
			    }			
		    }
	    }
	}catch(e){}
}
function PrepareText(objSender, strText)
{
	if(objSender.value == strText)
	{
		objSender.value = "";
	}
	else{
		if(objSender.value == "")
		{
			objSender.value = strText;
		}
		else
		{
			objSender.value = objSender.value;
		}
	}
}
function readCookie(name)
{
   var ca = document.cookie.split(';');
   var nameEQ = name + "=";
   for(var i=0; i < ca.length; i++) 
   {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
		if (c.indexOf(nameEQ) == 0) 
		{
			return c.substring(nameEQ.length, c.length);    
		}
   }
   return null;
}  
function removeCookie(name)
{   createCookie(name, "", -1); }	
function createCookie(name, value , days)
{
   if (days)
   {    var date = new Date();
   		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = ";expires="+date.toGMTString();
   }
   else 
	var expires = "";     
	
   document.cookie = name+"="+value+expires+"; path=/";
}


function NewPasswordControlSwap(aForce, pClass) {
    var parent = getElementsByClassName(document, 'div', pClass)[0];
    var original = getElementsByClassName(parent, 'input', 'csPWBox')[0];
    var fake = getElementsByClassName(parent, 'input', 'csFakePWBox')[0];
    if (aForce) {
        fake.style.display = "none";
        original.style.display = "block";
        original.focus();
    }
    // Did the user already type the password
    if (original.value != "") {       
        return;
    }
    else {
        // Show/hide the real password field
        if (fake.style.display == "" || fake.style.display == "block") {
            fake.style.display = "none";         
            original.style.display = "block";
            original.focus();
        }
        else {            
            fake.style.display = "block";
            original.style.display = "none";
        }
    }
}

function LoginBackClik() {
    document.getElementById('divFPWStep').style.display = 'none';
    document.getElementById('divLoginBox').style.display = 'block';
}

function PositionLoggedInDiv() {
		var ctrl = document.getElementById('divLoginForm');
		ctrl.style.display = 'block';
        ctrl.style.left = '759px';
        ctrl.style.top = '68px';
    
}
function ValidateLogin(LanguageId,sessionID1)
{	
	sessionID = sessionID1;
	ClearLoginErrors();
	var TxtEmail = getElementsByClassName(document,"input","csCLoginTxtBox")[0];
	var TxtPass = getElementsByClassName(document,"input","csPWBox")[0];
	var TxtBoxDiv = document.getElementById("divLoginTxtBox");
	var TxtPwdBoxDiv = document.getElementById("divLoginPwdTxtBox");
	if(TxtEmail.value != "" && TxtPass.value != "")
	{
		if(IsEmailValid(TxtEmail.value))
		{
			document.getElementById('divWait').style.display = "block";			
			AjaxPro.timeoutPeriod = 60*1000;
			AjaxWrapper.DoLogin(TxtEmail.value, TxtPass.value, LanguageId,GetMemberDetailsCallBack,null,null, null, LoginTimeout, null);
		}
		else
		{
		    TxtBoxDiv.className = "csTxtBoxDivErr";
		     if(container != null)
			{
			    var txtBox = getElementsByClassName(container,"input","csCLoginTxtBox");
			    if(txtBox != null)
			    {
			        txtBox[0].value = ErroTxtArray[0];
			    }
			}
		}
	}
	else
	{
	    var container = document.getElementById("divLoginBox");
		if(TxtEmail.value == "")
		{
			if(container != null)
			{
			    var txtBox = getElementsByClassName(container,"input","csCLoginTxtBox");
			    if(txtBox != null)
			    {
			        txtBox[0].value = ErroTxtArray[1];
			    }
			}
			TxtBoxDiv.className = "csTxtBoxDivErr";
		}
		else if(!IsEmailValid(TxtEmail.value))
		{
		    TxtBoxDiv.className = "csTxtBoxDivErr";
		    if(container != null)
			{
			    var txtBox = getElementsByClassName(container,"input","csCLoginTxtBox");
			    if(txtBox != null)
			    {
			        txtBox[0].value = ErroTxtArray[0];
			    }
			}		
		}
		if(TxtPass.value == "")
		{
		    TxtPwdBoxDiv.className = "csTxtBoxDivErr";
		    if(container != null)
			{
			    var txtBox = getElementsByClassName(container,"input","csFakePWBox");
			    if(txtBox != null)
			    {
			        txtBox[0].value = ErroTxtArray[2];
			    }
			}			
		}
	}
}
function GetMemberDetailsCallBack(ret)
{
	document.getElementById('divWait').style.display = "none";	  
	var container = document.getElementById("divLoginBox");
	var txtFakePwd = getElementsByClassName(container,"input","csFakePWBox")[0];
	var txtOriginalPwd = getElementsByClassName(container,"input","csPWBox")[0];	
	var TxtPwdBoxDiv = document.getElementById("divLoginPwdTxtBox");
	if(ret.value[0] == 1 )
	{
		//Remember PassWord 
		var RememberPassWord  = document.getElementById('imgRemPwd');
		if(RememberPassWord.getAttribute('value') == "1")
		{   
			var strEmail = getElementsByClassName(document,"input","csCLoginTxtBox")[0].value;
			var strPassword = getElementsByClassName(document,"input","csPWBox")[0].value;
			createCookie("email", strEmail , 7);  
			createCookie("password", strPassword , 7);      
		}
		else
		{
			removeCookie("email");
			removeCookie("password");
		}
		window.location.href = 	ret.value[3];
	}
	//Wrong Password
	else if( ret.value[0] == -1 )
	{
        TxtPwdBoxDiv.className = "csTxtBoxDivErr";
        txtOriginalPwd.setAttribute("value","");
        txtOriginalPwd.style.display = "none";
        txtFakePwd.style.display = "block";
        if(container != null)
        {
            var txtBox = getElementsByClassName(container,"input","csFakePWBox");
            if(txtBox != null)
            {
                txtBox[0].value = ErroTxtArray[3];
            }
        }        
	}
	//If Member Inactive
	else if( ret.value[0] == 2 )
	{
	    txtOriginalPwd.style.display = "none";
	    txtOriginalPwd.setAttribute("value","");
        txtFakePwd.style.display = "block";
	    TxtPwdBoxDiv.className = "csTxtBoxDivErr";
        if(container != null)
        {
            var txtBox = getElementsByClassName(container,"input","csFakePWBox");
            if(txtBox != null)
            {
                txtBox[0].value = ErroTxtArray[4];
            }
        }		
	}
	//If Email is not found in DB
	else
	{
	    txtOriginalPwd.style.display = "none";
	    txtOriginalPwd.setAttribute("value","");
        txtFakePwd.style.display = "block";
	    TxtPwdBoxDiv.className = "csTxtBoxDivErr";
        if(container != null)
        {
            var txtBox = getElementsByClassName(container,"input","csFakePWBox");
            if(txtBox != null)
            {
                txtBox[0].value = ErroTxtArray[5];
            }
        }		
	}
}

function TimeOut()
{}

/** Club Login **/
function ShowFPWForm() {
	ClearLoginErrors();
    document.getElementById('divLoginBox').style.display = 'none';
    document.getElementById('divFPWStep').style.display = 'block';
    document.getElementById('divPwdSend').style.display = 'none';
    document.getElementById('divFPWForm').style.display = 'block';
    var txtCtrls = getElementsByClassName(document,"input","csCLoginTxtBox");
    if(txtCtrls != null && txtCtrls.length >= 2)
    {
        txtCtrls[1].value = txtCtrls[0].value;
    }
}

function toggleChkBox(ctrl,rootPath) {
    if (ctrl != null) {
        if (ctrl.getAttribute('value') != null && ctrl.getAttribute('value') == '1') {
            ctrl.src = rootPath+'/Images/ClubLogin/chkbox.png';
            ctrl.setAttribute('value', '0');
        }
        else {
            ctrl.src = rootPath+'/Images/ClubLogin/chkbox_checked.png';
            ctrl.setAttribute('value', '1');
        }
    }   
}
function SendPassToEmail(languageid) {
    ClearLoginErrors();
    var pPWD = document.getElementById('divFPWStep');
    if (pPWD != null) {
        var txtEmail = getElementsByClassName(pPWD, "input", "csCLoginTxtBox")[0];
        var TxtFPwdBoxDiv = document.getElementById("divFPWTxtBoxDiv");
        var ValidEmailAddress = false;
         
        if (txtEmail.value != null && txtEmail.value != "") {
            ValidEmailAddress = IsEmailValid(txtEmail.value);
            if (ValidEmailAddress) {
                AjaxPro.timeoutPeriod = 60 * 1000;
                var ret = AjaxWrapper.SendPassword(txtEmail.value,languageid, null, null, null, null, TimeOut, null);
                if (ret.value == 1) // password send
                {
                    document.getElementById('divPwdSend').style.display = 'block';
                    document.getElementById('spnEmailId').innerHTML = txtEmail.value;
                    document.getElementById('divFPWForm').style.display = 'none';
                }
                else if (ret.value == 2) // Error - Member withdrawn
                {
                    TxtFPwdBoxDiv.className = "csTxtBoxDivErr";
                    if(pPWD != null)
                    {
                        var txtBox = getElementsByClassName(pPWD,"input","csCLoginTxtBox");
                        if(txtBox != null)
                        {
                            txtBox[0].value = ErroTxtArray[5];
                        }
                    }                    
                }
                else if (ret.value == 3) // Error - Member Inactive
                {
                    TxtFPwdBoxDiv.className = "csTxtBoxDivErr";
                    if(pPWD != null)
                    {
                        var txtBox = getElementsByClassName(pPWD,"input","csCLoginTxtBox");
                        if(txtBox != null)
                        {
                            txtBox[0].value = ErroTxtArray[6];
                        }
                    }                    
                }
                else if (ret.value == -1)// Error - Wrong Param
                {
                    TxtFPwdBoxDiv.className = "csTxtBoxDivErr";
                    if(pPWD != null)
                    {
                        var txtBox = getElementsByClassName(pPWD,"input","csCLoginTxtBox");
                        if(txtBox != null)
                        {
                            txtBox[0].value = ErroTxtArray[7];
                        }
                    }
                    document.getElementById('divForgetErr5').style.display = 'block';
                }
                else if (ret.value == -2)// Error - Member not found
                {
                    TxtFPwdBoxDiv.className = "csTxtBoxDivErr";
                    if(pPWD != null)
                    {
                        var txtBox = getElementsByClassName(pPWD,"input","csCLoginTxtBox");
                        if(txtBox != null)
                        {
                            txtBox[0].value = ErroTxtArray[5];
                        }
                    }
                    
                }
                else // Error - Wrong param
                {
                    TxtFPwdBoxDiv.className = "csTxtBoxDivErr";
                    if(pPWD != null)
                    {
                        var txtBox = getElementsByClassName(pPWD,"input","csCLoginTxtBox");
                        if(txtBox != null)
                        {
                            txtBox[0].value = ErroTxtArray[6];
                        }
                    }                                        
                }
            }
            else
            {
                TxtFPwdBoxDiv.className = "csTxtBoxDivErr";
                if(pPWD != null)
                {
                    var txtBox = getElementsByClassName(pPWD,"input","csCLoginTxtBox");
                    if(txtBox != null)
                    {
                        txtBox[0].value = ErroTxtArray[0];
                    }
                }                         
            }
        }
        else
        {
            TxtFPwdBoxDiv.className = "csTxtBoxDivErr";
            if(pPWD != null)
            {
                var txtBox = getElementsByClassName(pPWD,"input","csCLoginTxtBox");
                if(txtBox != null)
                {
                    txtBox[0].value = ErroTxtArray[8];
                }
            }             
        }
    }
}
function LoadNewLogin(rootPath) 
{
    var pElement = document.getElementById('divLoginBox');
    if (pElement != null) 
    {
        if (readCookie("email") != null && readCookie("email") != "" && readCookie("password") != null && readCookie("password") != "") 
        {
            getElementsByClassName(pElement, "input", "csFakePWBox")[0].style.display = 'none';
            getElementsByClassName(pElement, "input", "csPWBox")[0].style.display = '';
            getElementsByClassName(pElement, "input", "csCLoginTxtBox")[0].value = readCookie("email");
            getElementsByClassName(pElement, "input", "csPWBox")[0].value = readCookie("password");
            document.getElementById("imgRemPwd").src = rootPath+'/Images/ClubLogin/chkbox_checked.png';
            document.getElementById("imgRemPwd").setAttribute('value', '1');
        }
    }
}
function ClearLoginErrors() {  
    var TxtBoxDiv = document.getElementById("divLoginTxtBox");
	var TxtPwdBoxDiv = document.getElementById("divLoginPwdTxtBox");
	var TxtFPwdBoxDiv = document.getElementById("divFPWTxtBoxDiv");
	TxtBoxDiv.className = "csTxtBoxDiv";
	TxtPwdBoxDiv.className = "csTxtBoxDiv";
	TxtFPwdBoxDiv.className = "csTxtBoxDiv";
}

function IsErrorTxt(txt)
{
   return contains(ErroTxtArray,txt);
}
function contains(arr, txt) {
    var i = arr.length;
    while (i--) {
       if (arr[i].toString() == txt.toString()) {
           return true;
       }
    }
    return false;
}
function PrepareTextExtention(objSender, strText)
{
	if(objSender.value == strText || IsErrorTxt(objSender.value))
	{
		objSender.value = "";
	}
	else{
		if(objSender.value == "")
		{
			objSender.value = strText;
		}
		else
		{
			objSender.value = objSender.value;
		}
	}
}
