var _strPageLogin = "login.php";
var funcLogin=null;
var intLoginType=0;
var funcLogout=null;
var intLogoutType=0;
var _isClickedOnPopup_login = false;
function clogout()
{
    if (isPopUp())
    {
        if (window.parent.opener)
        {
            // Call from parent window    
            invokeFuncAtParentWindow("clogout()");       
            // Focus to parent window
            focusWindow(window.parent.opener);
            // Close current window
            self.close();
        }
    }else{
        eraseCookieAll("CUSER_INFO");
        var strRequest = "act=" + ACT_USER_LOGOUT ;
        var ajax = new Ajax();
        ajax.SendRequestToServerWithCustomMsg(_strPageLogin, strRequest, clogout_OnCallBack, true, MSG_AJAX_SENDING_VN ,null);
    }
}
function clogout_OnCallBack(xmlHTTPRequest)
{
    if(xmlHTTPRequest.readyState == 4) 
	{
		if(xmlHTTPRequest.status == 200) 
		{			
		    var strRespond = xmlHTTPRequest.responseText.split(SEP_RESPONSE);		
		    core.getObject("divLogin").innerHTML = strRespond[4];
		    if (intLogoutType!=0)
		    {
		        if (funcLogout!=null)
	            {
	                funcLogout();
	            }
	        }
	        else
	        {
	            window.location.reload();
	        }
		}
	}
}
function login()
{
    if(core.trim(core.getObject("email").value)=="" || !core.isValidEmail(core.trim(core.getObject("email").value)))
    {
        showMessage(MSG_EMAIL_WRONG_FORMAT,"tblAlertLogin","lblAlertLogin");
        core.getObject("email").focus();
        return;
    }
    if(core.getObject("cpassword").value=="")
    {
        showMessage(MSG_PASSWORD_EMPTY,"tblAlertLogin","lblAlertLogin");
        core.getObject("cpassword").focus();
        return;
    }
    if (core.getObject("txtCode")!=null)
    {
        if (core.trim(core.getObject("txtCode").value)=="")
        {
            showMessage(MSG_SEC_CODE_NULL,"tblAlertLogin","lblAlertLogin");
            core.getObject("txtCode").focus();
            return;
        }
    }    
    //
    var strRequest = "act=" + ACT_USER_LOGIN + "&email="+core.getObject("email").value+"&pass="+core.getObject("cpassword").value+"&remember="+core.getObject("chkRemember").checked;
    if (core.getObject("txtCode")!=null)
    {
        strRequest += "&txtCode="+core.getObject("txtCode").value;
    }
    else
    {
        eraseCookieAll("CUSER_INFO");
    }
    var ajax = new Ajax();
    ajax.SendRequestToServerWithCustomMsg(_strPageLogin, strRequest, login_OnCallBack, true, MSG_AJAX_SENDING_VN ,null);
}

function login_OnCallBack(xmlHTTPRequest)
{
    if(xmlHTTPRequest.readyState == 4) 
	{
		if(xmlHTTPRequest.status == 200) 
		{			
		    var strRespond = xmlHTTPRequest.responseText.split(SEP_RESPONSE);		
		   if (!headerProcessing(strRespond[0],0,strRespond[2],true,true,false))
            {
                return;
            }
		    if(parseInt(strRespond[2]) == 1) //have sec code
		    {
		        // generate capcha form
		        core.getObject("divLogin").innerHTML = strRespond[4];
		        core.getObject("email").focus();	
                core.getObject("email").select();
		    }
		    if(parseInt(strRespond[3]) == 1) // is login succeed
		    {
		        if (core.getObject("chkRemember").checked)
                {
                    SetCookieAll("CUSER_INFO",strRespond[5],30);
                }
		       
		        if (intLoginType!=0)
		        {
		            core.getObject("divLogin").innerHTML = strRespond[4];
		            if (funcLogin!=null)
		            {
		                funcLogin();
		            }
		        }
		        else
		        {
		            window.location.reload();
		        }
		    }
		    else
		    {
		        core.getObject("divLogin").innerHTML = strRespond[4];
		        // Set focus
		        if (core.getObject("email")){
                    core.getObject("email").focus();	
                    core.getObject("email").select();	            
		        }
		        //showMessage(MSG_LOGIN_FAIL);
		    }
		}
	}
}

function viewProfile_login(userIDEncoded)
{
    if (isPopUp())
    {
        if (window.parent.opener)
        {
            // Call from parent window    
            invokeFuncAtParentWindow("viewProfile_login('" + userIDEncoded + "')");       
            // Focus to parent window
            focusWindow(window.parent.opener);
        }
    }else{
        hrefWindow("profile.php?uid=" + userIDEncoded);    
    }
}
function helpLogin_login()
{      
    if (isPopUp())
    {
        if (window.parent.opener)
        {
            // Call from parent window    
            invokeFuncAtParentWindow("helpLogin_login()");       
            // Focus to parent window
            focusWindow(window.parent.opener);
        }
    }else{
        hrefWindow("user.php?d=30");    
    }
}
function regUser_login()
{       
    if (isPopUp())
    {  
        if (window.parent.opener)
        {
            // Call from parent window    
            invokeFuncAtParentWindow("regUser_login()");       
            // Focus to parent window
            focusWindow(window.parent.opener);
            // Close current window
            self.close();         
        }
    }else{
        hrefWindow("user.php");    
    }
}
function viewMessage_login(userIDEncoded)
{    
    if (isPopUp())
    {   
        if (window.parent.opener)
        {
            // Call from parent window    
            invokeFuncAtParentWindow("viewMessage_login('" + userIDEncoded + "')");       
            // Focus to parent window
            focusWindow(window.parent.opener);
        }
    }else{
        hrefWindow("profile.php?d=6&uid=" + userIDEncoded);
    }
}
function viewAddedDoc_login(userIDEncoded)
{       
    if (isPopUp())
    {  
        if (window.parent.opener)
        {
            // Call from parent window    
            invokeFuncAtParentWindow("viewAddedDoc_login('" + userIDEncoded + "')");       
            // Focus to parent window
            focusWindow(window.parent.opener);
        }        
    }else{
        hrefWindow("profile.php?d=3&uid=" + userIDEncoded);
    }
}
function viewFavoriteDoc_login(userIDEncoded)
{    
    if (isPopUp())
    {    
        if (window.parent.opener)
        {
            // Call from parent window    
            invokeFuncAtParentWindow("viewFavoriteDoc_login('" + userIDEncoded + "')");       
            // Focus to parent window
            focusWindow(window.parent.opener);
        }   
    }else{
        hrefWindow("profile.php?d=2&uid=" + userIDEncoded);
    }
}