
function GetProtocol() {
    var transferProtocol = 'http://';

    if (typeof (protocol) != 'undefined' && protocol == 'https://') {
        transferProtocol = protocol;
    }

    return transferProtocol;
}

LoginControl = function() {
    if (
	   $('username_i') == null ||
	   $('password_i') == null ||
	   $('ok_b') == null

	) {
        alert("In order to function properly every page that wants to show our login functionality\n"
		+ "must contains elements with following id's:\n"
		+ "\tusername_i\n"
		+ "\tpassword_i\n"
		+ "\tok_b\n")
        return;
    }

    function DoLogin(secondLogin) {
        //Clear errors
        $('inv_pass').style.display = "none";
        $('inv_user').style.display = "none";

        if ($('error_area') != null)
            $('error_area').style.display = "none";
        if ($('error_t') != null)
            $('error_t').innerHTML = "";

        $('ok_b').onclick = null;
        var test = new CrossDomainChanel(
		GetProtocol() + serverName + '/Secure/pageremotelogin.aspx?ast=oy&Request=login' +
		'&username=' + $V('username_i') +
		'&password=' + $V('password_i')
		, RenderData, HasResults);

        function RenderData() {
            if (!HasResults()) {
                if ($("loginbox_area") != null)
                    $("loginbox_area").style.display = "none"
                return;
            }
            var ex = GetError();
            if (!ex.Error) {
                var ls = GetLoginStatus();
                if (ls.LoginStatus == "OK") {
                    $('ok_b').onclick = null;
                    if ((typeof alkazin) != "undefined") {
                        var test = new CrossDomainChanel(alkazin + '?data=' + $V('username_i') + ':' + $V('password_i'), function() { }, function() { });
                    }

                    //                    if ((typeof estours) != "undefined") {
                    //                        var xml = GetXmlHttp();
                    //                        xml.open('POST', estours, false);
                    //                        xml.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                    //                        xml.send("data=" + $V('username_i') + ':' + $V('password_i'));
                    //                    }


                    window.location = ls.RedirectURL;
                }
            }
            else {
                var inputValidationError = false;

                if (ex.Message.indexOf("Invalid Password") > -1) {
                    inputValidationError = true;
                    $('inv_pass').style.display = "inline";
                }

                if (ex.Message.indexOf("Invalid Username") > -1) {
                    inputValidationError = true;
                    $('inv_user').style.display = "inline";

                }

                if (!inputValidationError) {
                    if ($('error_area') != null)
                        $('error_area').style.display = "inline";
                    if ($('error_t') != null)
                        $('error_t').innerHTML = "<nobr>" + ex.Message + "</nobr>";
                }
                //Add Client Error Handaling
            }
            $('ok_b').onclick = DoLogin;
        }
    }

    function RenderLanguageButtons() {
        var langButtonsDiv = $('languagesButtons_area');
        if (langButtonsDiv != null) {
            this.langs = GetLanguages().Languages;
            var ButtonsInRow = GetLanguages().ButtonsInRow * 1;
            var buttonsString = "<table><tr>"

            var j = 0;
            for (var i = 0; i < langs.length; i++) {
                if (langs[i].Url != null) {
                    buttonsString += '<td><img id="lang' + i + '" src="' + langs[i].Image + '" border="0" onclick=Change(' + i + ') style="cursor: pointer; cursor: hand"></td>';
                    j++;

                    if (j == ButtonsInRow) {
                        buttonsString += '</tr><tr>';
                        j = 0;
                    }
                }
            }
            buttonsString += '</tr></table>'
            langButtonsDiv.innerHTML = buttonsString;

        }
    }

    function RenderResources() {
        this.resources = GetResources().Resources;
        if ($('username_t') != null)
            $('username_t').innerHTML = resources.loginLblUserName + ':';

        if ($('password_t') != null)
            $('password_t').innerHTML = resources.AgentLog_Password;
        if ($('newAgent_a') != null) {
            $('newAgent_a').href = resources.PageNewRegistration;
            var imges = $('newAgent_a').getElementsByTagName('img');
            if (imges.length == 0)
                $('newAgent_a').innerHTML = resources.AgentLog_NewAgent;
        }

        var newUserLinks = document.getElementsByName('newUser_a');
        if (newUserLinks.length > 0) {
            for (i = 0; i < newUserLinks.length; i++) {
                if (typeof (protocol) != 'undefined' && protocol == 'https://')
                    newUserLinks[i].href = 'https://' + resources.PageNewRegistration.substr(7, resources.PageNewRegistration.length - 7);
                else
                    newUserLinks[i].href = resources.PageNewRegistration;

                var imges = newUserLinks[i].getElementsByTagName('img');
                if (imges.length == 0)
                    newUserLinks[i].innerHTML = resources.AgentLog_NewAgent;
            }
        }

        if ($('forgotPassword_a') != null) {
            $('forgotPassword_a').innerHTML = resources.CusLog_ForgotPassword;
            $('forgotPassword_a').href = resources.PageUserForgotPassword;
        }
        if ($('error_area') != null)
            $('error_area').style.display = "none";
        if ($('error_t') != null)
            $('error_t').innerHTML = "";

        var img = document.createElement('img');
        img.id = "inv_pass"
        img.src = GetProtocol() + serverName + '/Modules/customizable/images/requiredIcon_ani.gif';
        img.style.display = "none";
        $('password_i').parentNode.appendChild(img)

        img = document.createElement('img');
        img.id = "inv_user"
        img.src = GetProtocol() + serverName + '/Modules/customizable/images/requiredIcon_ani.gif';
        img.style.display = "none";
        $('username_i').parentNode.appendChild(img)

        document.onkeypress = function(e) {
            if (codeTouche(e) == 13) {
                DoLogin();
                return false;
            }
            else
                return true;
        }
    };

    function PopulateUserNameFromCookie() {
        if ($('username_i') != null) {
            var reg = /TGS\.Customer=.+username=(.+?)&.+/;
            var ar = reg.exec(document.cookie);
            if (ar != null && ar[1] != null)
                $('username_i').value = ar[1];
        }
    }

    function Show() {

        PopulateUserNameFromCookie();

        var test = new CrossDomainChanel(
		GetProtocol() + serverName + '/Secure/pageremotelogin.aspx?ast=oy&Request=init'
		, Render, HasResults);

        function Render() {
            if (!HasResults()) {
                if ($("loginbox_area") != null)
                    $("loginbox_area").style.display = "none"
                return;
            }
            RenderLanguageButtons();
            RenderResources();
            $('ok_b').onclick = DoLogin;
        }
    };

    function HasResults() {
        if (typeof (GetError) == "undefined")
            return false;

        return true;
    };


    this.Show = Show;
}

var lc;

function Change(i)
{
    var Url = GetLanguages().Languages[i].Url;
    var index = Url.indexOf('http://');
    if (index > -1)
        serverName = Url.substr(index + 7, Url.length - (index + 7))
    else
        serverName = Url;
    /*Solution for Escorted Tour Log In*/
    var culture = GetLanguages().Languages[i].Culture;

    //    estours = escortedTourURLs[culture];
    //        
    //    /*In case of dedicated website*/
    //    if ((typeof estours) == "undefined" && estDefault == 'true') 
    //    {
    //        estours = escortedTourURLs["default"];
    //    }        

    lc.Show();
}

//Login facade
function ShowLogin() {
    try {
        lc = new LoginControl();
        lc.Show();
    }
    catch (e) {
        if (document.getElementById("loginbox_area") != null)
            document.getElementById("loginbox_area").style.display = "none"
    }
}
