
/*this scipt is responsible for initialising the environment for Login and Profile services*/


wolf.signoutHandler = function(wsfDocument)
{
    window.location = "default.htm";
}

wolf.inSession = false;
wolf.checkSession = function(inSession)
{
    if(inSession == true)
    {
        if(wolf.context.applicationId == null)
        {
            //get session priviliges
            result                      = top.helperLib.processSoapRequest(window, top.helperLib.wsfMapPath("/webservices/wf.session/session.asmx") ,"wf.Session","GetSessionXML")
            wolf.context.sessionXml     = top.XMLLib.getNodeText(result,"//GetSessionXMLResult");
            wolf.context.applicationId  = top.XMLLib.getNodeText(wolf.context.sessionXml, "//record/applicationID");
            wolf.context.userName       = top.XMLLib.getNodeText(wolf.context.sessionXml, "//record/UserName");
            wolf.context.login          = top.XMLLib.getNodeText(wolf.context.sessionXml, "//record/loginId");
            wolf.context.appPluginXmlObject   = top.XMLLib.objXMLDocTemplate.cloneNode(true)
            wolf.context.appPluginXmlObject.load(top.helperLib.wsfMapPath("$application/xml/plugin.xml", wolf.context.applicationId));

            wolf.context.solutionXmlObject = top.XMLLib.objXMLDocTemplate.cloneNode(true);
		    wolf.context.solutionXmlObject.load(top.helperLib.wsfMapPath("$application/xml/solution.xml", wolf.context.applicationId));
		    wolf.context.applicationName    = top.XMLLib.getNodeText(wolf.context.solutionXmlObject, "//record/name");

        }
        
        document.getElementById("username").innerHTML = wolf.context.userName;
        top.document.title = wolf.context.applicationName;
        
        document.getElementById("signinbox").style.display = "none";
        
        document.getElementById("signedinbox").style.display = "block"
        document.getElementById("contentframe").style.display = "block"
        //sessionReady();
    }
    else
    {
        document.getElementById("signinbox").style.display = "block";
        
        document.getElementById("signedinbox").style.display = "none"
        document.getElementById("contentframe").style.display = "none"
    }
    try{onLoad()}catch(e){}
}

wolf.context = new clsContext();
function clsContext()
{
	this.applicationId  = null;
	this.userName		= null;
	this.login			= null;
	this.sessionXml		= null;
	this.appPluginXml   = null;
}


wolf.pageReady = function()
{
    using("XMLLib")
    using("helperLib")


    top.helperLib.hostPrefix = "http://" + window.location.host;

    //check active session
    var result = top.helperLib.processSoapRequest(window, top.helperLib.wsfMapPath("/webservices/wf.session/session.asmx"),"wf.Session","IsActiveSession");
    var sFunctionResponse = top.XMLLib.getNodeText(result,"//IsActiveSessionResult");
    
    
    if(sFunctionResponse == "True")
    {
        wolf.inSession = true;
        wolf.checkSession(true)
    }
    else
    {
        wolf.checkSession(false)
    }
    
    
}
                    
function persist(property, value)
{
    db.transaction(function(tx) {
        tx.executeSql( "update properties set value = ? where property = ?", [value , property] );
    });
}

function login(evt)
{
	var result = top.helperLib.processSoapRequest(window, top.helperLib.wsfMapPath("/webservices/wf.session/session.asmx"), "wf.Session", "IsUserAuthenticated", "sLogin", document.getElementById("loginBox").value, "sPassword", document.getElementById("passwordBox").value, "sApplicationID", document.getElementById("appBox").value);
    if (result==null){return}
    if(document.getElementById("rememberBox").value == "on")
    {
        persist("login", document.getElementById("loginBox").value)
        persist("appid", document.getElementById("appBox").value)
        persist("pwd", document.getElementById("passwordBox").value)
        persist("autosignin", document.getElementById("rememberBox").value)
        
    }
    else
    {
        persist("login", null)
        persist("appid", null)
        persist("pwd", null)
        persist("autosignin", null)
    }
    
    var sFunctionResponse = top.XMLLib.getNodeText(result,"//IsUserAuthenticatedResult");
    var userIsAuthenticated = top.XMLLib.getNodeText(sFunctionResponse,"/document/record/authenticated") == "True";    
    
    if(userIsAuthenticated == true)
    {
        wolf.inSession = true;
        wolf.checkSession(true);
    }
}

if(window.addEventListener)
{
    window.addEventListener("load", wolf.pageReady);
}
else
{
    window.attachEvent("onload", wolf.pageReady)
}    
