﻿/* 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 == 118 || charCode == 45 || charCode == 40 || charCode == 41 || charCode == 46 || charCode == 32) {
            return true;
        }
        else {
            return false;
        }
    }
    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('myTMobile-password');
    if (Msisdn.length == 10) {
        thePassBox.focus();

    }
}

// This function Moves the focus to the password field after there are ten characters in the MSIDISN field in the Login Tile.			
var tileLoginClicked = false;
function TileLoginAutoFocus() {
    var Msisdn = document.getElementById('TileLogin_myTMobilePhone').value;
    var thePassBox = document.getElementById('TileLogin_myTMobilePwd');
    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 (tileLoginClicked) {
        msisdn = document.getElementById('TileLogin_myTMobilePhone');
        password = document.getElementById('TileLogin_myTMobilePwd');
        errMessage = document.getElementById('TileLogin_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;
        }
    }
}

function handleTileLogin() {
    tileLoginClicked = true;
    handleLogin();
}

// 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';
        if (tileLoginClicked) {
            msisdnBox.value = document.getElementById('TileLogin_myTMobilePhone').value;
        }
        else {
            msisdnBox.value = document.getElementById('myTMobile-phone').value;
        }
        msisdnBox.name = "txtMSISDN";
        submitForm.appendChild(msisdnBox);

        var passBox = document.createElement('INPUT');
        passBox.type = 'HIDDEN'
        if (tileLoginClicked) {
            passBox.value = document.getElementById('TileLogin_myTMobilePwd').value;
        }
        else {
            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";

//PngFix code
(function($) {

    jQuery.fn.pngFix = function(settings) {

        // Settings
        settings = jQuery.extend({
            blankgif: 'blank.gif'
        }, settings);

        var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
        var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);

        if (jQuery.browser.msie && (ie55 || ie6)) {

            //fix images with png-source
            jQuery(this).find("img[src$=.png]").each(function() {

                jQuery(this).attr('width', jQuery(this).width());
                jQuery(this).attr('height', jQuery(this).height());

                var prevStyle = '';
                var strNewHTML = '';
                var imgId = (jQuery(this).attr('id')) ? 'id="' + jQuery(this).attr('id') + '" ' : '';
                var imgClass = (jQuery(this).attr('class')) ? 'class="' + jQuery(this).attr('class') + '" ' : '';
                var imgTitle = (jQuery(this).attr('title')) ? 'title="' + jQuery(this).attr('title') + '" ' : '';
                var imgAlt = (jQuery(this).attr('alt')) ? 'alt="' + jQuery(this).attr('alt') + '" ' : '';
                var imgAlign = (jQuery(this).attr('align')) ? 'float:' + jQuery(this).attr('align') + ';' : '';
                var imgHand = (jQuery(this).parent().attr('href')) ? 'cursor:hand;' : '';
                if (this.style.border) {
                    prevStyle += 'border:' + this.style.border + ';';
                    this.style.border = '';
                }
                if (this.style.padding) {
                    prevStyle += 'padding:' + this.style.padding + ';';
                    this.style.padding = '';
                }
                if (this.style.margin) {
                    prevStyle += 'margin:' + this.style.margin + ';';
                    this.style.margin = '';
                }
                var imgStyle = (this.style.cssText);

                strNewHTML += '<span ' + imgId + imgClass + imgTitle + imgAlt;
                strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;' + imgAlign + imgHand;
                strNewHTML += 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;';
                strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + jQuery(this).attr('src') + '\', sizingMethod=\'scale\');';
                strNewHTML += imgStyle + '"></span>';
                if (prevStyle != '') {
                    strNewHTML = '<span style="position:relative;display:inline-block;' + prevStyle + imgHand + 'width:' + jQuery(this).width() + 'px;' + 'height:' + jQuery(this).height() + 'px;' + '">' + strNewHTML + '</span>';
                }

                jQuery(this).hide();
                jQuery(this).after(strNewHTML);

            });

            // fix css background pngs
            jQuery(this).find("*").each(function() {
                var bgIMG = jQuery(this).css('background-image');
                if (bgIMG.indexOf(".png") != -1) {
                    var iebg = bgIMG.split('url("')[1].split('")')[0];
                    jQuery(this).css('background-image', 'none');
                    jQuery(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
                }
            });

            //fix input with png-source
            jQuery(this).find("input[src$=.png]").each(function() {
                var bgIMG = jQuery(this).attr('src');
                jQuery(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
                jQuery(this).attr('src', settings.blankgif)
            });

        }

        return jQuery;

    };

})(jQuery);

$(function() { $(document).pngFix(); });


$(document).ready(function() {
    if (window.location.href.indexOf("shop/cart/default.aspx?savecart=true") > 0) {
        theForm.action = "default.aspx";
        __doPostBack('btnSaveCart', '');
    }
});

// common fucntions
// check if value is empty or null
function IsObjEmptyOrNull(chkValue) {
    if (typeof (chkValue) == "undefined") {
        return true;
    }
    switch (chkValue) {
        case null:
            return true;
        case "":
            return true;
        default:
            return false;
    }
}

// get Current page name from url.
//return page name in lowercase .
function GetCurrentPageName() {
    var sPath = window.location.pathname;
    var sPage = "";
    if (!IsObjEmptyOrNull(sPath)) {
        sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
        sPage = sPage.toLowerCase();
    }
    return sPage;

}

/*CR 57117 Starts*/
var zipCodeValue = "";
var city = "";
var state = "";
var errMsg = "Please enter valid zip code or city, state";
function fnStoreLocator(URL, zipCode, defaultValue, cityState, sourcePage) {
    zipCodeValue = zipCode;
    if (ValidateZipCode(zipCodeValue, defaultValue, cityState)) {
        if (((zipCodeValue.length == 5) && (city.length == 0 && state.length == 0)) || ((zipCodeValue.length == 5) && (cityState == (city + ", " + state))))
            window.location = URL + "&zipcode=" + zipCodeValue + "&referer={" + sourcePage.replace("&","1~1") + "}";
        else {
            window.location = URL + "&city=" + city + "&state=" + state + "&referer={" + sourcePage.replace("&", "1~1") + "}";
        }
    }
}

function ValidateZipCode(zipCode, defaultValue, cityState) {
    var errMessage = document.getElementById('zipErrMessage');
    var zip = "";
    errMessage.innerHTML = "";

    if (document.getElementById('txtLocation') != null) 
    {
        zip = trimAll(document.getElementById('txtLocation').value);
        if (defaultValue.length > 0 && zip.indexOf(defaultValue) >= 0) {
            errMessage.innerHTML = errMsg;
            return false;
        }
        if ((zip == cityState) && (zipCode.length == 5)) 
        {
            return true;
        }
        if ((!isNaN(zip)) && (zip.length == 5) && (!IsSpecialChar(zip)) && (zip != "00000")) {
            zipCodeValue = zip;
            return true;
        }
        else 
        {
            if (zip.indexOf(',') >= 0) 
            {
                var arr = zip.split(',');
                if (arr.length == 2) 
                {
                    var tempCity = trimAll(arr[0]);
                    var tempState = trimAll(arr[1]);

                    if (tempCity.length == 0 || tempState.length == 0) {
                        errMessage.innerHTML = errMsg;
                        return false;
                    }

                    if ((!IsSpecialChar(tempCity)) && (!IsNumber(tempCity)))  
                    {
                        city = tempCity;
                    }
                    else 
                    {
                        errMessage.innerHTML = errMsg;
                        return false;
                    }
                    if ((!IsSpecialChar(tempState)) && (!IsNumber(tempState))) 
                    {
                        state = tempState.toUpperCase();
                        if (state.length >= 2) 
                        {
                            return true;
                        }
                        else 
                        {
                            errMessage.innerHTML = "Oops, your entry wasn't formatted correctly. Please try again.";
                            return false;
                        }
                    }
                    else 
                    {
                        errMessage.innerHTML = errMsg;
                        return false;
                    }
                }
                else 
                {
                    errMessage.innerHTML = errMsg;
                    return false;
                }
            }
            else 
            {
                errMessage.innerHTML = errMsg;
                return false;
            }
        }
    }
    errMessage.innerHTML = errMsg;
    return false;
}

function trimAll(sString) {
    while (sString.substring(0, 1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length - 1, sString.length) == ' ') {
        sString = sString.substring(0, sString.length - 1);
    }
    return sString;
}

function IsSpecialChar(strString)
//  check for valid SpecialChar strings
{
    if (strString.length == 0) return false;
    var strValidChars = "<>@!#$%^&*()_+[]{}?:;|'\"\\./~`-=";
    var strChar;
    blnResult = false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length; i++) 
    {
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) >= 0) {
            blnResult = true;
            break;
        }
        else {
            blnResult = false;
        }
    }
    return blnResult;
}

function IsNumber(strString)
//  check for valid number strings
{
    if (strString.length == 0) return false;
    var strValidNumbers = "0123456789";
    var strNum;
    blnResult = false;

    //  test strString consists of valid characters listed above
    for (i = 0; i < strString.length; i++) {
        strNum = strString.charAt(i);
        if (strValidNumbers.indexOf(strNum) >= 0) {
            blnResult = true;
            break;
        }
        else {
            blnResult = false;
        }
    }
    return blnResult;
} 


/*CR 57117 Ends*/

