﻿

function setMasterHeaderHeigh(masterHeadId) {
    var _logoLeft = document.getElementById("logoleft");
    var _logoRight = document.getElementById("logoright");
    var _master = document.getElementById(masterHeadId);
    if( _logoLeft && _logoLeft.offsetHeight && _logoRight && _logoRight.offsetHeight && _master) {            
        var logoLeftHeight = _logoLeft.offsetHeight;
        var logoRightHeight = _logoRight.offsetHeight;
        var defHeight = 70;
        if (logoLeftHeight + 10 > defHeight || logoRightHeight + 10 > defHeight) {        
            var size = logoLeftHeight > logoRightHeight ? logoLeftHeight+10 : logoRightHeight+10;
            if (size<1000) {
                size += "px";
                document.getElementById(masterHeadId).style.height = size; 
            } 
        }
    }
}
function toggleBox(szDivID, iState) { // 1 visible, 0 hidden
    if(document.getElementById) {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = iState ? "visible" : "hidden";
        var err = document.getElementById('errormessage');
        err.innerHTML = "&nbsp;";
    }
}

function checkEnterPressed(evt)
{
    if (evt)
    {
        if (evt.keyCode == 13)
            return true;
        else
            return false;
    }
}
function checkCredentials() {
    var uid = document.getElementById('txtusr');
    var pwd = document.getElementById('txtpwd');
    var result = GuestAjaxLogin.isValidUser(uid.value, pwd.value).value;
    var err = document.getElementById('errormessage');
    uid.value = ""; pwd.value = "";
    if (!result) {
        //err.innerHTML = "Login failed!";      
        err.innerHTML = mlLoginFailed;    
    } else {
        err.innerHTML = "&nbsp;"; 
        window.location.reload();
    } 
}
var pressed = "";
var oldnormimage = "";
function on_over(theimage, thename) {
  if (document.images) {
    if (thename != pressed) {
      oldnormimage = document.images[thename].src;
      document.images[thename].src=theimage;
    }
  }
}
function on_out(thename) {
  if (document.images) {
    if (thename != pressed) {
      document.images[thename].src=oldnormimage;
    }
  }
}
function getDivAllHeaderLinks() {
    var obj = null;
    var idObj = document.getElementById("DivAllHeaderLinksId");
    if (idObj) {
        var strId = idObj.value;
        obj = document.getElementById(strId);
    }
    return obj;
}
function showDivAllHeaderLinksId() {
    var obj = getDivAllHeaderLinks();
    if (obj) {
        obj.style.display = "";
        obj.style.visibility = "visible";
    }
}
function hideDivAllHeaderLinksId() {
    var obj = getDivAllHeaderLinks();
    if (obj) {
        obj.style.visibility = "hidden";
    }
}
function getTdSearchControl() {
    var obj = null;
    var idObj = document.getElementById("tdSearchControlId");
    if (idObj){
        var strId = idObj.value;
        obj = document.getElementById(strId);
    }
    return obj;
}
function showTdSearchControl() {
    var obj = getTdSearchControl();
    if (obj) {
        obj.style.display = "";
        obj.style.visibility = "visible";
    }
}
function hideTdSearchControl() {
    var obj = getTdSearchControl();
    if (obj) {
        obj.style.visibility = "hidden";
    }
}
// Search - PART NUMBER i DESCRIPTION ---------------------------------------
function txtSearchKeyPress(e,showPleaseWait) {
    var charCode=(e.which) ? e.which : event.keyCode;
    if (""+charCode == "13") {
        btnSearchClick(showPleaseWait);
        return false;
    }
    return true;
}

function btnSearchClick(showPleaseWait) {

showPleaseWait = String(showPleaseWait);

//alert("Type of: " + typeof(showPleaseWait) + "\r\nValue: " + showPleaseWait);

if (showPleaseWait == "YES")
        pleaseWaitShow();
 
    var schType = "";
    var schText = "";
    var obj = document.getElementById("ddlSearch");
    if (obj) {
        schType = obj.options[obj.selectedIndex].value;
    }
    obj = document.getElementById("txtSearch");
    if (obj)
     schText = obj.value;
   // alert("Search length: " + schText.length + "\n\rSearch input: " + schText);
    if (schText.length > 0) {
        if (AjaxPro) {
            AjaxPro.onTimeout = function() 
            {
                //alert("Server didn't respond in a timely maner.");
                alert(mlNoResponse);
            }
            AjaxPro.timeoutPeriod = 1000 * 60 * 5;
        }
        Webcom.Configurator.Util.Dojo.AjaxMessenger.SendMessage(schText, schType, [], "MasterProductCCSearch", btnSearchClick_callback);
    }
    else {
   
 if (showPleaseWait == "YES")
        pleaseWaitHide();
       //alert("Please enter data.");
    }
}
function btnSearchClick_callback(res) {

    if (res.error) {
       alert(res.error.Message);
    } else {
        if (!res.value.ok)
        {
            alert(res.value.message);
            if(res.value.evalCode)
            {
                eval(res.value.evalCode);
            }
        } else {
            eval(res.value.evalCode);
        }
    }
}

function disableLinks() {
    var ie8 = isIE8();

    var link1 = document.getElementById("aSetup");
    if(link1) {
        if(!ie8) link1.disabled = "disabled";
        link1.onclick = function() { return false; }
    }
    var link2 = document.getElementById("aHome");
    if(link2) {
        if(!ie8) link2.disabled = "disabled";
        link2.onclick = function() { return false; }
    }
    var link3 = document.getElementById("aUserPage");
    if(link3) {
        if(!ie8) link3.disabled = "disabled";
        link3.onclick = function() { return false; }
    }
    var link4 = document.getElementById("aNewQuote");
    if(link4) {
        if(!ie8) link4.disabled = "disabled";
        link4.onclick = function() { return false; }
    }
    var link5 = document.getElementById("aExistingQuotes");
    if(link5) {
        if(!ie8) link5.disabled = "disabled";
        link5.onclick = function() { return false; }
    }
    var link6 = document.getElementById("aCurrentQuote");
    if(link6) {
        if(!ie8) link6.disabled = "disabled";
        link6.onclick = function() { return false; }
    }
    var link7 = document.getElementById("aSyncrhronize");
    if(link7) {
        if(!ie8) link7.disabled = "disabled";
        link7.onclick = function() { return false; }
    }
    var link8 = document.getElementById("aLogoff");
    if(link8) {
        if(!ie8) link8.disabled = "disabled";
        link8.onclick = function() { return false; }
    }
    var link9 = document.getElementById("aOffline");
    if(link9) {
        if(!ie8) link9.disabled = "disabled";
        link9.onclick = function() { return false; }
    }
    var link10 = document.getElementById("aBulkValidation");
    if(link10) {
        if(!ie8) link10.disabled = "disabled";
        link10.onclick = function() { return false; }
    }
}

function isIE8()
{
    if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
        var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
        if (ieversion>=8) return true;        
    }
    return false;
}

function pleaseWaitShow() {

  makeShadowWhileSearch();
  var pleaseWait = document.getElementById("pleaseWaitDiv");
  if (pleaseWait)
    pleaseWait.style.display = "";
}

function pleaseWaitHide() {

  hideShadowAfterSearch();
  var pleaseWait = document.getElementById("pleaseWaitDiv");
  if (pleaseWait)
    pleaseWait.style.display = "none";
    
  var pleaseWaitParent = parent.document.getElementById("pleaseWaitDiv");
  if (pleaseWaitParent)
    pleaseWaitParent.style.display = "none";
}

var shadow = null;

function makeShadowWhileSearch() {
    if(shadow == null)
    {  
        shadow = document.createElement("div");
        document.body.appendChild(shadow);
    }
    with(shadow.style)
    {
        if (navigator.appVersion.indexOf("MSIE 6") > 0)
            position = "absolute";
        else
            position = "fixed";
        left = 0 + "px";
        top = 0 + "px";
        width = document.body.clientWidth + "px";
        height = document.body.clientHeight + "px";
        zIndex = 1000;
        backgroundColor = "#000000";
        display = "block";
        border = "solid 0px black";
        //ie
        filter = 'Alpha(Opacity=20)';
        //firefox
        opacity = 0.2;
    }
}

function hideShadowAfterSearch() {
    if (shadow) 
        shadow.style.display = 'none';
}

var processDiv = null;
var processIcon = null;

function showProcessingIcon() 
{

if(show_processing_icon)
    {
        if(processIcon==null)
            {
                processIcon = document.createElement("img");
                processIcon.src = _mt_path+"images/processing.gif";
                document.body.appendChild(processIcon);
            }
                 
        if(processDiv == null)
            {  
                processDiv = document.createElement("div");       
                document.body.appendChild(processDiv);        
            }

            with(processDiv.style)
            {

                if (navigator.appVersion.indexOf("MSIE 6") > 0)
                    position = "absolute";
                else
                    position = "fixed";
                left = 0 + "px";
                top = 0 + "px";
                width = document.body.clientWidth + "px";
                height = document.body.clientHeight + "px";
                zIndex = 2000;
                backgroundColor = "#000000";
                display = "block";
                border = "solid 0px black";
                //ie
                filter = 'Alpha(Opacity=20)';
                //firefox
                opacity = 0.2;
            }   

            with(processIcon.style)
            {
                if (navigator.appVersion.indexOf("MSIE 6") > 0)
                    position = "absolute";
                else
                    position = "fixed";
                
                left = (document.body.clientWidth-processIcon.width)/2 + "px";
                top = (document.body.clientHeight-processIcon.height)/2 + "px";            
                zIndex= 2001;
                display = 'block';
                //ie
                filter = 'Alpha(Opacity=100)';
                //firefox
                opacity = 1;            
            }  
       }   
 }

function hideProcessingIcon() {
    if (processDiv) 
        processDiv.style.display = 'none';
    if(processIcon)
        processIcon.style.display = 'none';
}

// Captcha support
var captchaShadow = null;

function makeCaptchaShadow(zInd, isVisible) 
{
    var showDiv = isVisible;
    if(isVisible == null)
        showDiv = true;
        
    if(showDiv)
    {
        if(captchaShadow == null)
        {  
            captchaShadow = document.createElement("div");
            document.body.appendChild(captchaShadow);
        }
        
        with(captchaShadow.style)
        {
            if (navigator.appVersion.indexOf("MSIE 6") > 0)
            {
                position = "absolute";
            }
            else
            {
                position = "fixed";
            }
                
	        left = 0 + "px";
	        top = 0 + "px";
	        width = document.body.clientWidth + "px";
	        height = document.body.clientHeight + 300 + "px";
	        
	        zIndex = zInd;
	        backgroundColor = "#000000";
	        display = "block";
	        border = "solid 0px black";
	        //ie
	        filter = 'Alpha(Opacity=20)';
	        //firefox
	        opacity = 0.2;
        }
    }
}

function hideCaptchaShadow() {
    if (captchaShadow) 
        captchaShadow.style.display = 'none';
}

function openCaptchaIframe(width, height, src)
{
    var mainDiv = document.getElementById("captchaIframeDiv");
    if(mainDiv != null)
    {
        mainDiv.innerHTML = "";
        var iframe = document.createElement('iframe'); 
        iframe.frameBorder = 0; 
        iframe.width = (width - 6) + "px"; 
        iframe.height = (height - 25) + "px"; 
        mainDiv.appendChild(iframe); 
        
        var randomNumber = Math.floor(Math.random()*11);
        iframe.src = src.replace("//", "/") + '?rrr=' + randomNumber;
    }
}

function closeCaptchaIframe()
{
    var mainDiv = document.getElementById("captchaIframeDiv");
    if(mainDiv != null)
    {
        mainDiv.innerHTML = "";
    }
}

function captchaValidated(pageUrl, siteUrl)
{
    var ua = navigator.userAgent.toLowerCase(); 
    
    if(window.event) window.event.returnValue = false;
    
    if ( ua.indexOf( "msie" ) != -1 ) 
    {
        var t = document.getElementById("captchaHref2");
        if(t != null)
            t.click();
    }
    else
    {
        if(siteUrl.toLowerCase().indexOf('/localhost/') != -1)
            siteUrl = siteUrl.replace("https", "http");
            
        window.location.href = siteUrl + "/" + pageUrl;
    }
    
    toggleBoxCaptcha('captchaDiv', 0);
    //hideCaptchaShadow();
    closeCaptchaIframe();
}

function toggleBoxCaptcha(szDivID, iState) { // 1 visible, 0 hidden
    if(document.getElementById) {
        var obj = document.getElementById(szDivID);
        obj.style.visibility = iState ? "visible" : "hidden";
    }
}

