﻿
var loginValid = false;
var scardCalls = 0;//used for ignoring the first call made to LoginSCard(code,path)


$(document).ready(function() {

//    if ($("div#panel")) {
//        $("div#panel").animate({
//            height: "450px",
//            width: "100%"
//        })
//		.animate({
//		    height: "400px", width: "98%"
//		}, "medium")
//		.animate({
//		    height: "450px", width: "100%"
//		}, "medium")
//		.animate({
//		    height: "420px", width: "99%"
//		}, "medium")
//		.animate({
//		    height: "450px", width: "100%"
//		}, "medium");
//    }

    if ($("div#header"))
        $("div#header").fadeIn(1000);

    if ($("div#login"))
        $("div#login").fadeIn(1000);

    if ($("input#txtUsername"))    
        $("input#txtUsername").focus();

    if ($("h2#Powered"))
        $("h2#Powered").fadeIn(4000);


    $("input#txtPassword").keypress(function(e) {
        if (e.keyCode == 13) {
            if (loginValid == true)
                location.href = "StudyListHome.aspx";
            else
                DoLogin();
        }
        if (e.keyCode == 27) {
            CloseModal();
        }
    });

});



//function DoLogin()
//{
//    //document.getElementById('LoginMsg').innerHTML = '<CENTER><IMG SRC="images/loading2.gif"></CENTER>';
//    user = document.getElementById("txtUsername").value;
//    pass = document.getElementById("txtPassword").value;
//                 
//    if(user=='' || pass=='')
//    {
//        alert("Username and Password fields cannot be empty");
//        //$("div#divUserName").show();
//	    //$("input#txtUsername").focus();	 
//        return;
//    }
//        
//    var reqBody ="";
//    
//    reqBody = "USERNAME="+user;
//    reqBody += "&PASSWORD="+pass;
//    
//    url = "LoginHandler.ashx";
//    
//    var currRes = makeAPOST(url,reqBody);
//	//alert(currRes);	
//    LoginResult(currRes);
//    
//}

function LoginResult(currRes)
{
    if(currRes=='User Validated')
    {
        //$('#basicModalContent').modal({onOpen: modalOpen});
        if (readCookie("hippa_" + $("input#txtUsername")[0].value)=="1")
            location.href = 'StudyListHome.aspx';
        else {
            $('#basicModalContent').modal();
            loginValid = true;
        }
    }
    else if(currRes=='Change Password')
    {
        location.href='ChangePassword.aspx';
    }
    else if(currRes=='refresh')
    {
        location.href = 'Login.aspx';
    }
    else
    {  
	    var LogMsg = document.getElementById('LoginMsg');
	    LogMsg.innerHTML = currRes;
	    //$("div#divUserName").show();
        //	$("div#login").show();
	    
	    $("input#txtPassword")[0].value = "";
	    $("input#txtUsername")[0].value = "";
	    $("div#LoginMsg").fadeIn();
	    $("input#txtUsername").focus();
	    return;
    }
}


function CloseModal()
{
    $.modal.close();
    loginValid = false;
    $("input#txtUsername")[0].value = "";
    $("input#txtPassword")[0].value = "";
    $("div#LoginMsg")[0].innerHTML = "";
}

function RedirectToHome(uid) {
    if ($('#chkHIPPA').is(':checked'))
        createCookie("hippa_" + $("input#txtUsername")[0].value, "1", 3650);
    location.href = 'StudyListHome.aspx';
}

//Smart Card Functions
function initializecard()
{
    try
    {kicker.InitializeSmartCard();}
    catch (e) {}    
}

function LoginSCard(code)
{
    //alert(code);
    if(scardCalls!=0)
    {
        var currRes = makeAPOST("LoginHandler.ashx?SC=true","CODE="+code);
	    //alert(currRes);	
        LoginResult(currRes);
    }
    scardCalls++;
}
//End of Smart Card Functions
