﻿/* Init Search Box Blur/Focus Behaviour
############################################## 
*/
function initSearch() {
    storednameval = $("#SearchForaQuestion").val();

    $("#SearchForaQuestion").focus(function() {
        if ($(this).val() == storednameval) {
            $(this).val("")
        }
    });
    $("#SearchForaQuestion").blur(function() {
        if ($(this).val() == "") {
            $(this).val(storednameval)
        }
    });
}

function initMyTmoPhone()
{
    storednameval = $("#myTMobile-phone").val();

    $("#myTMobile-phone").focus(function()
    {
        if ($(this).val() == storednameval)
        {
            $(this).val("")
        }
    });
    $("#myTMobile-phone").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).val(storednameval)
        }
    });
}

function initMyTmoPassword()
{
    storednameval = $("#myTMobile-password").val();

    $("#myTMobile-password").focus(function()
    {
        if ($(this).val() == storednameval)
        {
            $(this).val("")
        }
    });
    $("#myTMobile-password").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).val(storednameval)
        }
    });
}

function updateWebTrendTag(url)
{
    window.location = url;
}


// This function swaps the passed TextBox to a Password Box.
function changeTextToPass(textId, passId) {
    // Get Reference to the existing TextBox
    var theTextBox = document.getElementById(textId);
    var thePassBox = document.getElementById(passId);

    thePassBox.onkeypress = function(e) {
        // Call Validation Function on Enter KeyPress
        if (!e) { e = window.event; }
        var charCode = (e.keyCode || e.charCode);

        if (charCode == 13 || charCode == 3) {
            e.cancelBubble = true;
            e.returnValue = false;
            handleLogin(mytmoUrl);
            // Return False prevents the search box event handler from picking up the keypress.
            return false;
        }

    }

    theTextBox.style.display = 'none';
    thePassBox.style.display = 'block';
    thePassBox.focus();
}

// This function restores the password box back to a text box

function restorePassToText(passId, textId) {

    // Get Reference to the existing PassBox
    var thePassBox = document.getElementById(passId);
    var theTextBox = document.getElementById(textId);

    // If the passbox has no content, then change it back to a TextBox.
    if (thePassBox.value == "") {
        theTextBox.style.display = 'block';
        thePassBox.style.display = 'none';
    }
}

// This function only returns true if the character passed is valid for the MSISDN field
function isValidMSISDNCharacter(keyEvent) {
    var charCode = (keyEvent.which) ? keyEvent.which : keyEvent.keyCode

    // Check to see if the character code is the representation for one of the numbers, 0-9.
    if (charCode == 13 || charCode == 3) {
        handleLogin(mytmoUrl);
        return false;
    }

    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        if (charCode == 189 || charCode == 45) {
            return true;
        }
        else {
            return false;
        }
    }
    else if (charCode == 8 || charCode == 46) {
        return true;
    }
    else {
        return true;
    }

}
// This function Moves the focus to the password field after there are ten characters in the MSIDISN field.			
function AutoFocus() {
    var Msisdn = document.getElementById('myTMobile-phone').value;
    var thePassBox = document.getElementById('password-clear');
    if (Msisdn.length == 10) {
        thePassBox.focus();

    }
}

// This function returns true if the MSIDISN field and password fields both pass validation.
function ValidateMsisdnPassword() {

    var msisdn;
    var password;
    var errMessage;
    var agt = navigator.userAgent.toLowerCase();
    var logintable;

    msisdn = document.getElementById('myTMobile-phone');
    password = document.getElementById('myTMobile-password');
    errMessage = document.getElementById('errMessage');

    if (msisdn.value == "" || msisdn.value == " Phone #" || msisdn.value == "Phone #") {
        // Msisdn is not entered, check password value to determine error message to display
        if (password.value == "" || password.value == " Password" || password.value == "Password") {
            errMessage.innerHTML = errNoInfo;
            errMessage.style.display = 'block';
            return false;
        }
        else {
            errMessage.innerHTML = errNoPhone;
            errMessage.style.display = 'block';
            return false;
        }

    }
    else if (msisdn.value.length < 10) {
        errMessage.innerHTML = errInvalidPhone;
        errMessage.style.display = 'block';
        return false;
    }
    else {
        // Valid Msisdn has been entered, check for password.
        if (password.value == "" || password.value == " Password") {
            errMessage.innerHTML = errNoPass;
            errMessage.style.display = 'block';
        }
        else {
            // Valid Msisdn and a Password has been entered. Validation passed.
            errMessage.style.display = 'none';
            return true;
        }
    }
}


// This function is called by either the enter keypress in the password field, or by clicking the login button.
function handleLogin() {

    if (ValidateMsisdnPassword()) {

        var submitForm = document.createElement('FORM');
        submitForm.name = 'form1';
        submitForm.method = 'POST';
        submitForm.action = mytmoUrl;

        var msisdnBox = document.createElement('INPUT');
        msisdnBox.type = 'HIDDEN';
        msisdnBox.value = document.getElementById('myTMobile-phone').value;
        msisdnBox.name = "txtMSISDN";
        submitForm.appendChild(msisdnBox);

        var passBox = document.createElement('INPUT');
        passBox.type = 'HIDDEN'
        passBox.value = document.getElementById('myTMobile-password').value;
        passBox.name = "txtPassword";
        submitForm.appendChild(passBox);

        document.body.appendChild(submitForm);
        submitForm.submit();

        return true;
    }
    else {
        return false;
    }
}

function getClientHeight() {
    return f_filterResults(
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
    var n_result = n_win ? n_win : 0;
    if (n_docel && (!n_result || (n_result > n_docel)))
        n_result = n_docel;
    return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

// Error Message text, these can be overridden if needed by the client page.
var errNoInfo = "Please enter phone number & password.";
var errNoPass = "Please enter password.";
var errNoPhone = "Please enter phone number.";
var errInvalidPhone = "Your phone number should be 10 digits.<br/> For example: 4445551212";