﻿
function CreateLoginProcess(appPath,
targetUrl,
additionalItems,
validate)
{
    var returnValue = false;
    var xmlhttp = new ajaxRequest();
    if(xmlhttp != null)
    {
       
        var loginProcessUrl = appPath + 
        "/Login/GenerateProcess.ashx?TargetUrl=" + 
        targetUrl + 
        "&AdditionalItems=" + 
        additionalItems +
        "&Validate=" +
        validate
       
        xmlhttp.open("GET", loginProcessUrl, false); 
        xmlhttp.send(null);
        if((xmlhttp.responseText != null) && (xmlhttp.responseText.length > 0))
        {
            var txtSTKN = document.getElementById("txtSTKN");
            if(txtSTKN != null)
            {
                txtSTKN.value = xmlhttp.responseText;
                returnValue = true;
                if(xmlhttp.responseText.indexOf(",") > -1)
                {
                    txtSTKN.value = txtSTKN.value.substring(0,txtSTKN.value.indexOf(","));
                    var vId = xmlhttp.responseText.substring(xmlhttp.responseText.indexOf(",") + 1);
                    var txtTKN = document.getElementById("txtTKN");
                    if(txtTKN != null)
                    {
                        txtTKN.value = vId;
                        var txtType = document.getElementById("txtType");
                        if(txtType != null)
                        {
                            if(txtType.value == "2")
                            {
                                txtType.value = "21";
                            }
                            else if(txtType.value == "3")
                            {
                                txtType.value = "22";
                            }
                            else if(txtType.value == "4")
                            {
                                txtType.value = "24";
                            }
                            else if(txtType.value == "5")
                            {
                                txtType.value = "25";
                            }
                            else if(txtType.value == "6")
                            {
                                txtType.value = "23";
                            }
                        }
                    }
                }
            }
        }
    }
    return returnValue;
}

function ajaxRequest()
{
    if (window.XMLHttpRequest)
    {
        // code for IE7+, Firefox, Chrome, Opera, Safari
        return new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    { 
        // code for IE6, IE5
        return  new ActiveXObject("Microsoft.XMLHTTP");
    }
    else
    {
        return null;
    }
}

function UsernameBehaviour(eventId, 
usernameId, 
defaultText)
{
    var postField = document.getElementById("txtUserName");
    var username = document.getElementById(usernameId);
    
    if((postField != null) && 
    (username != null))
    {
        postField.value = username.value;
       
        if(((eventId == "click") || (eventId == "focus")) && 
        (username.value == defaultText))
        {
            username.value = "";
        }
        else if((eventId == "blur") && (username.value == ""))
        {
            username.value = defaultText;
        }
    }
}

function PasswordBehaviour(passwordId, 
plainPasswordId,
direction)
{
    var postField = document.getElementById("txtPassword");
    var password = document.getElementById(passwordId);
    var plainPassword = document.getElementById(plainPasswordId);
       
    if((postField != null) && 
    (password != null) && 
    (plainPassword != null))
    {
        postField.value = password.value;
        
        if(direction == 1)
        {
            if(password.value.length == 0)
            {
                password.style.display = "none";
                plainPassword.style.display = "block";
            }
        }
        else if(direction == 2)
        {
            password.style.display = "block";
            plainPassword.style.display = "none";
            password.focus();
        }
        
    }
}

function SetHiddenPassword(passwordId)
{
    var postField = document.getElementById("txtPassword");
    var password = document.getElementById(passwordId);
    
    if((postField != null) && (password != null))
    {
        postField.value = password.value;   
    }
}

function AddLinkButtonClickFunction(id) 
{
    var linkButton = document.getElementById(id);

    if ((linkButton) && (typeof(linkButton.click) == 'undefined')) 
    {
        linkButton.click = function() 
        {
            var result = true;

            if (linkButton.onclick) 
            {
                result = linkButton.onclick();
            }     
            if ((typeof(result) == 'undefined') || (result))
            {
                eval(linkButton.getAttribute('href'));
            }
        }
    }
}

