var login,passwd;
var xmlHttp;

icon1=new Image();
icon2=new Image();
icon1.src="images/loading-trans.gif";
icon2="images/warning.png";

function openLightBox()
        {
        document.getElementById('light').style.display='block';
        document.getElementById('fade').style.display='block';
        }
    function closeLightBox()
        {
        document.getElementById('light').style.display='none';
        document.getElementById('fade').style.display='none';
        }
    function changePasswordCaption()
	{
	var attribute_type=document.getElementById('passwd').getAttribute("type");
	if(attribute_type=='text')
		{
		//document.getElementById('password').value='';
		//document.getElementById('password').setAttribute('type','password');
		var html='<input type="password" class="text-input" style="width:200px;" id="passwd"  name="password" value="" onfocus="javascript:changePasswordCaption();" onblur="javascript:addPasswordCaption();"><a href="#" class="inline-link">Forgot?</a>';
		document.getElementById('password_row').innerHTML=html;
		//alert('ok');
		document.getElementById('passwd').focus();
		}
	}
    function addPasswordCaption()
            {
            var password=document.getElementById('passwd').value;
            if(password=='')
                    {
                    //document.getElementById('password').setAttribute('type','text');
                    //document.getElementById('password').value='Password';
                    var html='<input type="text" class="text-input" style="width:200px;" id="password"  name="password" value="Password" onfocus="javascript:changePasswordCaption();" onblur="javascript:addPasswordCaption();"><a href="#" class="inline-link">Forgot?</a>';
                    document.getElementById('password_row').innerHTML=html;
                    }
            }
            
    function changeUsernameCaption()
            {
            var username=document.getElementById('email').value;
            if(username=='Username/Email Address')
                    {
                    document.getElementById('email').value="";
                    }
            }
    function addUsernameCaption()
            {
                    var username=document.getElementById('email').value;
                    if(username=='')
                    {
                    document.getElementById('email').value="Username/Email Address";
                    }
            }
function validate()
{
    var email=document.getElementById('email').value;
    var passwd=document.getElementById('passwd').value;
    if(email=="")
    {
        document.getElementById('osx-modal-title').style.color="#FBAF5D";
        document.getElementById('osx-modal-title').innerHTML="<img src='images/warning.png' alt='warning' />Email cannot be empty";
        return false;
    }
    if(!/^\w(\.?[\w-])*@\w(\.?[\w-])*\.[a-z]{2,5}$/i.test(email))
    {
        document.getElementById('osx-modal-title').style.color="#FBAF5D";
        document.getElementById('osx-modal-title').innerHTML="<img src='images/warning.png' alt='warning' />Invalid email format";
        return false;
    }
    if(passwd=="")
    {
        document.getElementById('osx-modal-title').style.color="#FBAF5D";
        document.getElementById('osx-modal-title').innerHTML="<img src='images/warning.png' alt='warning' />Password cannot be empty";
        return false;
    }
    if(passwd.length<6 || passwd.length>46)
    {
        document.getElementById('osx-modal-title').style.color="#FBAF5D";
        document.getElementById('osx-modal-title').innerHTML="<img src='images/warning.png' alt='warning' />Password should be more than 6 characters";
        return false;
    }
    startRequest(email,passwd);
}
function checkEnter(e)
{
    var characterCode;
    if(e && e.which)
    {
        e=e;
        characterCode=e.which;
    }
    else
        e=event;
        characterCode=e.keyCode
    if(characterCode==13)
    {
        validate();
    }
}

function createXMLHttpRequest()
{
    if(window.ActiveXObject)
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    if(window.XMLHttpRequest)
            xmlHttp=new XMLHttpRequest();
}
function startRequest(e,p)
{
    createXMLHttpRequest();
    xmlHttp.open("POST","instant-login.php",true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.onreadystatechange=handleStateChange;
    xmlHttp.send("login="+e+"&passwd="+p);
}
function handleStateChange()
{
    if(xmlHttp.readyState==1)
    {
        document.getElementById('osx-modal-title').innerHTML="<img src='images/loading-trans.gif' alt='Loading' />Authentication in progress";
    }
    if(xmlHttp.readyState==4)
    {
        if(xmlHttp.status==200)
        {
            var reply=xmlHttp.responseText;
            if(reply=='Success')
            {
                document.getElementById('osx-modal-title').innerHTML="Authenticated. Now Redirecting...";
                window.location="http://www.enterhelix.com/";
            }
            else if(reply=='Inactive')
            {
                document.getElementById('osx-modal-title').style.color="#F2583E";
                document.getElementById('osx-modal-title').innerHTML="<img src='images/warning.png' alt='warning' />Your account is not yet active";
            }
            else
            {
                document.getElementById('osx-modal-title').innerHTML="<img src='images/warning.png' alt='warning' />";
                document.getElementById('osx-modal-title').innerHTML=reply;
            }
                
        }
    }
}
