﻿// JScript File
var uri = "/ProcessLogin.aspx";

function InitializePage()
{
    start();
    
    document.getElementById('userEmail').focus();
}
    
function showloginarea()
{
    document.getElementById('userEmail').value = "";
    document.getElementById('invitationCode').value = "";
	document.getElementById('MemLoginDiv').style.display   = "block";
	
	document.getElementById('ForgotPWDDiv').style.display = "none";
}

function showforgotpwdarea()
{
	document.getElementById('MemLoginDiv').style.display   = "none";
	
	document.getElementById('tbUserEmailFP').value = "";
	document.getElementById('ForgotPWDDiv').style.display = "block";
}

function SendPassCode()
{
    var EmailRegExp = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,3}$/;
    var userEmail = document.getElementById('tbUserEmailFP').value;
    
    //Validate Email.
    if(userEmail == '')
    {
        alert('Enter Email');
        return;
    }
    else
    {
        if(!EmailRegExp.test(userEmail))
        {
            alert("Enter valid Email"); 
            return;
        }
    }
    
    var data = "action=forgotPassCode&userEmail=" + userEmail;
    
    SendData(data);
}		

function CatchLoginEnter(e) 
{
	if (!e) 
	    e = window.event;
	    
	if (e.keyCode) 
		code = e.keyCode;		
	else if (e.which) 
	    code = e.which;
	    
	if(code == 13) 
    {
        ValidateUser();
    }
}

function CatchRegisterEnter(e)
{
	if (!e) 
	    e = window.event;
	    
	if (e.keyCode) 
		code = e.keyCode;		
	else if (e.which) 
	    code = e.which;
	    
	if(code == 13) 
    {
        SendInvitation();
    }
}

function SendInvitation()
{
    var EmailRegExp = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,3}$/;
    var invtEmail = document.getElementById('invitationEmail').value;
    
    //Validate Email.
    if(invtEmail == '')
    {
        alert('Enter Email');
        return;
    }
    else
    {
        if(!EmailRegExp.test(invtEmail))
        {
            alert("Enter valid Email"); 
            return;
        }
    }
    
    d('invitationAck').innerHTML = "Registering ... ";
    
    var data = "action=sendInvitation&userEmail=" + invtEmail;
    
    SendData(data);
}    

function ValidateUser()
{
    var EmailRegExp = /^[\w-\.]+@([\w-]+\.)+[\w-]{2,3}$/;
    
    var userEmail = document.getElementById('userEmail').value;
    
    if(userEmail == '')
    {
        alert("Enter Email"); 
        return;
    }
    else
    {
        if(!EmailRegExp.test(userEmail))
        {
            alert("Enter valid Email"); 
            return;
        }
    }
    
    var invitationCode  = document.getElementById('invitationCode').value;
    
    if(invitationCode == '')
    {
        alert('Enter Invitation Code');
        return;
    }
    
    var data = "action=login&userEmail=" + userEmail + "&invitationCode=" + invitationCode;
    //alert(data);
    SendData(data);
}

function HandleResponse(xmlDoc)
{
	var strErrorCode = xmlDoc.getElementsByTagName("ERROR")[0].childNodes[0].nodeValue;
	
	if(strErrorCode == '0')
	{
	    var strAction = xmlDoc.getElementsByTagName("ACTION")[0].childNodes[0].nodeValue;
	    
	    if(strAction == 'sendInvitation')   //Send Invitation.
	    {
	        document.getElementById('invitationEmail').value = "";
	        var strExists = xmlDoc.getElementsByTagName("EXISTS")[0].childNodes[0].nodeValue;
	        
	        if(strExists == "N")
                d('invitationAck').innerHTML = 'Thank you, we will mail you the invitation code.';	        
            else
                d('invitationAck').innerHTML = 'This email has already been registered, we will resend the invitation code.';	        
	    }
	    else if(strAction == 'login')       //User Login.
	    {
	        var isValid = xmlDoc.getElementsByTagName("VALID")[0].childNodes[0].nodeValue;
            
	        if(isValid == "Y")
	        {
	            var currentURL = window.location.href;
                var i =	currentURL.lastIndexOf('/');        	        
                window.location.href = currentURL.substr(0, i+1) + 'TweeboMain.aspx';
	        }
	        else
	        {
	            alert('Invalid Email or Code!');
	        }
	    }
	    else if(strAction == 'forgotPassCode')
	    {
            var strExists = xmlDoc.getElementsByTagName("EXISTS")[0].childNodes[0].nodeValue;
	        
	        if(strExists == "Y")
	        {
	            document.getElementById('tbUserEmailFP').value = "";
                alert("The invitation code has been mailed to you.");	        
            }
            else
                alert("Invalid Email!");	        
	    }
	}
	else
	    alert("Tweebo: An error has occurred while processing your request");    
}

