﻿var eecUtils = new EecUtils();
var wndFormDesigner = null;
function EecUtils()
{
    this.getQueryStringValue = function(sValueName)
    {
        var oParams = this.parseQueryString();
        if(typeof(oParams[sValueName]) == 'undefined')
            return null;
        return oParams[sValueName];
    }
    
    this.parseQueryString = function(sQueryString)
    {
        if(typeof(sQueryString) == 'undefined')
        {
            sQueryString = window.location.href;
        }
        var oParams = new Object();
        var aParams = sQueryString.split("?");
        if(aParams.length != 2)
        {
            return oParams;
        }
        aParams = aParams[1].split("#");
        aParams = aParams[0].split("&");
        for (var iParam = 0; iParam < aParams.length; iParam++) 
        {
            var aParam = aParams[iParam].split("=");
            if(aParam.length == 2)
            {
                oParams[aParam[0]] = aParam[1];
            }
        }
        return oParams;
    }
    
    this.replaceQueryStringValue = function(sValueName, sNewValue)
    {
        var sNewQueryString = window.location.href.split('?')[0];        
        var sParams = window.location.search.substring(1);
        var bFlag = false;
        aParams = sParams.split("&");
        for (var i = 0; i < aParams.length; i++) 
        {
            var aParam = aParams[i].split("=");
            if (aParam[0] == sValueName) 
            {
                aParam[1] = sNewValue;
                bFlag = true;
            }
            if(i == 0)
                sNewQueryString += "?";
            else
                sNewQueryString += "&";
            sNewQueryString += aParam[0] + "=" + aParam[1];
        }
        if(!bFlag)
        {
            sNewQueryString += "&" + sValueName + "=" + sNewValue;
        }
        return sNewQueryString;
    }
    
    this.stringWidthInPixels = function(sValue, sFontFamily, iFontSize, sFontWeight)
    {    
        if(!Prototype)
        {
            alert('include prototype.js required');
            return 0;
        }
            
        var cSpan = $('ctl_EecUtils_WidthCalculator');
        if(cSpan == null)
        {            
            cSpan = document.createElement('span');
            cSpan.id = 'ctl_EecUtils_WidthCalculator';
            document.body.appendChild(cSpan);
            cSpan = $(cSpan);
        }
        cSpan.show();
        if(typeof(sFontFamily) != "undefined" && sFontFamily != null)
        {
            cSpan.setStyle({'font-family' : sFontFamily});
        }
        if(typeof(iFontSize) != "undefined" && iFontSize != null)
        {
            cSpan.setStyle({'font-size' : iFontSize});
        }
        if(typeof(sFontWeight) != "undefined" && sFontWeight != null)
        {
            cSpan.setStyle({'font-weight' : sFontWeight});
        }
        cSpan.innerText = sValue;
        var iWidth = cSpan.getWidth();
        cSpan.hide();
        return iWidth;
    }
    
    this.clearStyles = function(sHtml)
    { 
        sHtml = removeStyleAttribute(sHtml);
        sHtml = this.removeTags(sHtml, 'span');
        sHtml = this.removeTags(sHtml, 'font');
        sHtml = this.removeTags(sHtml, 'strong');
        sHtml = this.removeTags(sHtml, 'em');
        sHtml = this.removeTags(sHtml, 'u');
        sHtml = this.removeTags(sHtml, 'strike');
        sHtml = this.removeTags(sHtml, 'h1');
        sHtml = this.removeTags(sHtml, 'h2');
        sHtml = this.removeTags(sHtml, 'h3');
        sHtml = this.removeTags(sHtml, 'h4');
        return sHtml;
    }
    
    this.replaceClasses = function(sHtml, sClass)
    {    
        if(typeof(sClass) == 'undefined' || sClass == null)
            sClass = '';
        if(sClass)
            sClass = "class=" + sClass;
            
        var rxClass;
        rxClass = RegExp("class=\\w*\\s", "gi");
        sHtml = sHtml.replace(rxClass, sClass + " ");
        rxClass = RegExp("class=\\w*>", "gi");
        return sHtml.replace(rxClass, sClass + ">");
    }
    
    function removeStyleAttribute(sHtml)
    {
        var rxStyle;
        rxStyle = RegExp('style="[^"]*"', "gi");
        sHtml = sHtml.replace(rxStyle, "");
        rxStyle = RegExp("style='[^']*'", "gi");
        return sHtml.replace(rxStyle, "");
    }
    
    this.removeStyle = function(sHtml, sAttribute)
    {
        var rxStyle;
        var sStyle = sAttribute + ":" + "[^;^'^\"]+"
        rxStyle = RegExp(sStyle, "gi");
        return sHtml.replace(rxStyle, "");
    }
    
    this.removeTags = function(sFromHtml, sTagName)
    {        
        var rxTag = RegExp("</?" + sTagName + "[^>]*>", "gi");
        return sFromHtml.replace(rxTag, "");
    }
    
    // uses if you want to wrap text to some tag, 
    // because begining space will not be vilible
    // so we replace it by &nbsp;
    this.ckeckBeginingSpace = function(sHtml)
    {        
        if(sHtml.substr(0,1) == ' ')                
            sHtml = '&nbsp;' + sHtml.substr(1,sHtml.length-1);
        return sHtml;
    }
    
    //use this if you want make all page content shadowed and nonaccesible
    this.getPelenaSeven = function()
    {        
        var dPelena = document.getElementById('dPelena');
        if(dPelena == null)
        {
            dPelena = document.createElement('div');        
            dPelena.id = "dPelena";
            dPelena.style.display = "none";
            dPelena.style.position = "absolute";    
            dPelena.style.top = "0px";
            dPelena.style.left = "0px";
            
            dPelena.style.width = document.body.offsetWidth + document.body.scrollLeft + "px";
            dPelena.style.height = document.body.offsetHeight + document.body.scrollTop + "px";
            
            dPelena.style.backgroundColor = "#000000";
            dPelena.className = "transparent30";                      
            document.body.appendChild(dPelena);
        }
        
        dPelena.show = function(){this.style.display = "block";};
        dPelena.hide = function(){this.style.display = "none";};  
        
        Event.observe(window, 'resize', function()
            {
                var oPelena = eecUtils.getPelenaSeven();
                oPelena.style.width = document.body.offsetWidth + document.body.scrollLeft + "px";
                oPelena.style.height = document.body.offsetHeight + document.body.scrollLeft + "px";
            });
        
        return dPelena;
    }

    this.getPopup = function() {
        var dPopup = document.getElementById('dEECUtilsPopup');
        if (dPopup == null) {
            dPopup = document.createElement('div');
            dPopup.id = "dEECUtilsPopup";
            dPopup.style.display = "none";
            dPopup.style.position = "absolute";
            dPopup.style.top = "0px";
            dPopup.style.left = "0px";

            dPopup.style.width = "10px";
            dPopup.style.height = "10px";

            dPopup.style.backgroundColor = "#FFFFFF";
            document.body.appendChild(dPopup);
        }

        dPopup.show = function() { this.style.display = "block"; };
        dPopup.hide = function() { this.style.display = "none"; };
        dPopup.isDisplayed = function() { return this.style.display == "block"; }

        return dPopup;
    }    
}

function openEditorWindow(sUrl, sWindowName, sOptions)
{
    wndFormDesigner = window.showModalDialog(sUrl, null, "dialogHeight:" + screen.height + "px;dialogWidth:" + screen.width + "px;");
    setTimeout(checkFormDesigner, 500);
}

function checkFormDesigner()
{
    if(wndFormDesigner == null || typeof(wndFormDesigner.closed) == "undefined" || wndFormDesigner.closed)
    {
        refreshPage();
    }
    else
    {
        setTimeout(checkFormDesigner, 500);
    }
}

function refreshPage()
{
    window.location = window.location;
}

 function formatCurrency (obj) {
	   anynum=obj;
	   divider =100;
	   workNum=Math.abs((Math.round(anynum*divider)/divider));
	   workStr=""+workNum
       if (workStr.indexOf(".")==-1){workStr+="."}
	   dStr=workStr.substr(0,workStr.indexOf("."));dNum=dStr-0
	   pStr=workStr.substr(workStr.indexOf("."))
	   while (pStr.length-1< 2){pStr+="0"}
	   if(pStr =='.') pStr ='';
	   //--- Adds a comma in the thousands place.    
	   if (dNum>=1000) {
		  dLen=dStr.length
		  dStr=parseInt(""+(dNum/1000))+","+dStr.substring(dLen-3,dLen)
	   }
	   //-- Adds a comma in the millions place.
	   if (dNum>=1000000) {
		  dLen=dStr.length
		  dStr=parseInt(""+(dNum/1000000))+","+dStr.substring(dLen-7,dLen)
	   }
	   retval = dStr + pStr
 	   return retval;
 }

function parseCurrency(sCurrency)
 {
    sCurrency = sCurrency + "";
    while (sCurrency.indexOf(",") != -1)
        sCurrency = sCurrency.replace(",", "");
    return parseFloat(sCurrency);
 }

