﻿

    // ****************************************** Determine the checked radio Button of submitted element ******************************************
    // Expects input like 'document.form.elementname'
    function radioValue(rObj) {
        for (var i=0; i<rObj.length; i++) if (rObj[i].checked) return rObj[i].value;
        return false;
    }


    
    /*
        Class Utilities: utilities for manipulating the CSS class(es) of an HTML element
    */
    var CSSClass = {}; // Namespace Object
    
    CSSClass.is = function(e,c){
        if (typeof e == "string") e = document.getElementById(e);
        if (e==null) return;
        var classes = e.className;
        if (!classes) return false;                             // Not a member of any classes
        if (classes == c) return true;                          // Member of just this one class
        return e.className.search("\\b" + c + "\\b") != - 1;    // search; \b requires a match at a word boundary
    };
    
    CSSClass.add = function(e,c){
        if (typeof e == "string") e = document.getElementById(e);
        if (e==null) return;
        if (CSSClass.is(e,c)) return;                           // If already a Member do nothing
        if (e.className) c = " " + c;                           // Withespace sperator if needed
        e.className += c;                                       // Add to end
    };
    
    CSSClass.remove = function(e,c){
        if (typeof e == "string") e = document.getElementById(e);
        if (e==null) return;
        e.className = e.className.replace(new RegExp("\\b" + c + "\\b\\s*", "g"),"");
    };        
    /* End Class Utilities */

    
    /* Ajax */
	function GetXmlHttpObject()
    { 
        var objXMLHttp = null;
        if (window.XMLHttpRequest)
        {
            objXMLHttp = new XMLHttpRequest();
        }
        else if (window.ActiveXObject)
        {
            objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        return objXMLHttp;
    }
    
    
    
    /*
        Contact Form
    */
    
    function sendContactData()
    {
    
        var sErr = document.getElementById('sCFErrorMSGmail').value;
        var sEmail = document.getElementById('cEmail').value;
        
        // Validate here
        if (isInputDataComplete() == true)
        {
            if (isEmail_Correct(sEmail) == true)
            {
                document.getElementById('sAction').value = 'save';
                document.forms['fContent'].submit();
            }
            else
            {
                alert(sErr);
            }
        }
    }
    
    // Validate Email Addr
    function isEmail_Correct(sEmail)
    { 
                    
        var proto  = "(mailto:)?";
        var usr    = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
        var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
        var regex  = "^" + proto + "?" + usr + "\@" + domain + "$";

        var rgx    = new RegExp(regex);
        return rgx.exec(sEmail) ? true : false;
    }
    
    function isInputDataComplete()
    {
        var sErr = document.getElementById('sCFErrorMSG').value;        

        if (document.getElementById('cName').value =='' || document.getElementById('strCAPTCHA').value =='')
        {
            alert(sErr);
            return false;
        }
        else
        {
            if (document.getElementById('cEmail').value =='' && document.getElementById('cTelephone').value =='')
            {
                alert(sErr);
                return false;
            }
            else
            {
                return true;
            }
        }
    }
    
    
    /*
        Newsletter
    */
    function sendNLOptInData()
    {
        var sErr = document.getElementById('sNLOptInErrorMSG').value;    
        var sEmail = document.getElementById('NLOptInEmail').value;
        
        if (document.getElementById('NLOptInFirstName').value=='' || document.getElementById('NLOptInLastName').value=='' || document.getElementById('strCAPTCHA').value=='')
        {
            alert(sErr);
        }
        else
        {
            if (isEmail_Correct(sEmail) == true)
            {
                document.getElementById('sAction').value = 'save';
                document.forms['fContent'].submit();
            }
            else
            {
                alert(sErr);
            }
        }
    }
    
    
    /*
        Capture Enter key
    */
    function fireButton(event)
    {
        if(event.keyCode==13)
        {
            performSearch();
        }
    }
    
    function performSearch()
    {
        if (document.getElementById('searchText').value == '')
        {
        }
        else
        {
            document.getElementById('sAction').value = 'search';
            document.getElementById('PageType').value = '4';
            document.forms['fContent'].submit();
        }
    }
    
    function addTransparency()
    {
        var arVersion = navigator.appVersion.split("MSIE")
        var version = parseFloat(arVersion[1])

        if ((version >= 5.5) && (document.body.filters) && (version <= 7)) 
        {
           for(var i=0; i<document.images.length; i++)
           {
              var img = document.images[i]
              var imgName = img.src.toUpperCase()
              if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
              {
                 var imgID = (img.id) ? "id='" + img.id + "' " : ""
                 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
                 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
                 var imgStyle = "display:inline-block;" + img.style.cssText 
                 if (img.align == "left") imgStyle = "float:left;" + imgStyle
                 if (img.align == "right") imgStyle = "float:right;" + imgStyle
                 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
                 var strNewHTML = "<span " + imgID + imgClass + imgTitle
                 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
                 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
                 img.outerHTML = strNewHTML
                 i = i-1
              }
           }
        }
    }
    
    function setImageSize()
    {
        var obj = document.getElementById('sidebar');
        if (document.all)
        {
            var minSize = 1154;
        }
        else
        {
            var minSize = 1024;
        }
        if (Geometry.getViewportWidth() <= minSize) {	        
	        try {
                var aLiIMG = obj.getElementsByTagName("img");
                for (var i=0;i<aLiIMG.length;i++)
                {
                    aLiIMG[i].style.width = parseFloat(aLiIMG[i].width * 0.8) + 'px';
                    aLiIMG[i].style.height = parseFloat(aLiIMG[i].height * 0.8) + 'px';
                }
            }
            catch (e){}
	    }
    }
    
    /*
        FAQ Server
    */
    function setQuestionID(qID)
    {
        document.getElementById("FAQ_Question").value=qID;
    }
    
    
    function ReLoad()
    {
        document.forms["fContent"].submit();
    }
    
    // ****************************************************************** Tooltip ******************************************************************

    function Tooltip()
    {
        this.tooltip = document.createElement("div");//tooltip shadow
        this.tooltip.style.position = 'absolute';
        this.tooltip.style.visibility = 'hidden';
        this.tooltip.className = 'tooltipShadow';
        
        this.content = document.createElement('div');
        this.content.style.position = 'relative';
        this.content.className ='tooltipContent';
        
        this.tooltip.appendChild(this.content);
     }
     
    Tooltip.prototype.show = function(text,x,y){
        this.content.innerHTML = text;
        this.tooltip.style.left = x +'px'
        this.tooltip.style.top = y + 'px'
        this.tooltip.style.visibility = 'visible';
        
        if (this.tooltip.parentNode != document.body)
            document.body.appendChild(this.tooltip);
     }   
        
    Tooltip.prototype.hide = function() {
        this.tooltip.style.visibility = "hidden";
    }

    Tooltip.X_OFFSET = 25;
    Tooltip.Y_OFFSET = 25;
    Tooltip.DELAY = 500;

    Tooltip.prototype.schedule = function(target, e) {
        var text = target.getAttribute("tooltip");
        if(!text) return;
        
        var x = e.clientX + Geometry.getHorizontalScroll();
        var y = e.clientY + Geometry.getVerticalScroll();
        x += Tooltip.X_OFFSET;
        y += Tooltip.Y_OFFSET;
        
        var self = this;
        var timer = window.setTimeout(function() {self.show(text, x, y);}, Tooltip.DELAY);
        
        if (target.addEventListener) target.addEventListener('mouseout', mouseout, false);
        else if (target.attachEvent) target.attachEvent('onmouseout', mouseout);
        else target.onmouseout = mouseout;
        
        function mouseout() {
            self.hide();
            window.clearTimeout(timer);
            if (target.removeEventListener)
                target.removeEventListener('mouseout',mouseout, false);
            else if (target.detachEvent) target.detachEvent('onmouseout', mouseout);
            else target.onmouseout = null;
        }
        
    }
    
    /* Dirty hack
        This dirty hack is used for all tooltips which are at right hand side of screen - they normally are shown
        outside of viewport; by moving them to the left (200px), we will be able to display them within viewport.
        This hack is used for tooltips within Stock Balance, for example.
    */
    Tooltip.prototype.schedule2Left = function(target, e) {
        var text = target.getAttribute("tooltip");
        if(!text) return;
        
        var x = e.clientX + Geometry.getHorizontalScroll();
        var y = e.clientY + Geometry.getVerticalScroll();
        x += Tooltip.X_OFFSET-200;
        y += Tooltip.Y_OFFSET;
        
        var self = this;
        var timer = window.setTimeout(function() {self.show(text, x, y);}, Tooltip.DELAY);
        
        if (target.addEventListener) target.addEventListener('mouseout', mouseout, false);
        else if (target.attachEvent) target.attachEvent('onmouseout', mouseout);
        else target.onmouseout = mouseout;
        
        function mouseout() {
            self.hide();
            window.clearTimeout(timer);
            if (target.removeEventListener)
                target.removeEventListener('mouseout',mouseout, false);
            else if (target.detachEvent) target.detachEvent('onmouseout', mouseout);
            else target.onmouseout = null;
        }
        
    }
    
    Tooltip.tooltip = new Tooltip();
    Tooltip.show = function(text,x,y) {Tooltip.tooltip.show(text,x,y);}
    Tooltip.hide = function() {Tooltip.tooltip.hide();}
    Tooltip.schedule = function(target, e){Tooltip.tooltip.schedule(target, e);}
    Tooltip.schedule2Left = function(target, e){Tooltip.tooltip.schedule2Left(target, e);}
     
    // **************************************************************** END Tooltip **************************************************************** 
    
    /*
        End FAQ Server
    */
    
    
    /*
        Menu
    */
    function getPosX(obj)
    {
        var x=0;
        while(obj){
            x += obj.offsetLeft;
            obj=obj.offsetParent;
        }          
        return x;
    }
    
    function getPosY(obj)
    {
        var y=0;
        while(obj){
            y += obj.offsetTop;
            obj=obj.offsetParent;
        }          
        return y;
    }
    
    function isShowLeft(posX, objWidth)
    {
        wX = Geometry.getViewportWidth();
        if (posX + objWidth > wX)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    function showNavSubMenu(obj, objMenuID, sLevel)
    {
        var parentX = getPosX(obj);
        var parentY = getPosY(obj);
        var objMenu = document.getElementById(objMenuID);
        if (sLevel=='2')
        {
            if (isShowLeft(parentX, 220))
            {
                var maxX = parentX + obj.offsetWidth -221;
                var posX = maxX; 
            }
            else
            {
                var posX = parentX;
            }
            var posY = parentY + 22;        
        }
        
        objMenu.style.top = posY + 'px';
        objMenu.style.left = posX + 'px';
        objMenu.style.display = 'block';
    }
    
    function hideNavSubMenu(objMenuID, objParentMenu)
    {
        var objMenu = document.getElementById(objMenuID);
        objMenu.style.display = 'none';       
    }
    
    function showMe(obj)
    {
        obj.style.display='block';
    }
        
    function hideMe(obj)
    {
        obj.style.display='none';
    }

    // ********************************************************* Teach JS ASP ********************************************************
    function InStr(strSearch, charSearchFor)
    /*
    InStr(strSearch, charSearchFor) : Returns the first location a substring (SearchForStr)
                               was found in the string str.  (If the character is not
                               found, -1 is returned.)
                               
    Requires use of:
	    Mid function
	    Len function
    */
    {
	    for (i=0; i < Len(strSearch); i++)
	    {
	        if (charSearchFor == Mid(strSearch, i, 1))
	        {
			    return i;
	        }
	    }
	    return -1;
    }
    
    function Len(str)
    /***
            IN: str - the string whose length we are interested in

            RETVAL: The number of characters in the string
    ***/
    {  return String(str).length;  }

    
    function Mid(str, start, len)
    /***
            IN: str - the string we are LEFTing
                start - our string's starting position (0 based!!)
                len - how many characters from start we want to get

            RETVAL: The substring from start to start+len
    ***/
    {
            // Make sure start and len are within proper bounds
            if (start < 0 || len < 0) return "";

            var iEnd, iLen = String(str).length;
            if (start + len > iLen)
                    iEnd = iLen;
            else
                    iEnd = start + len;

            return String(str).substring(start,iEnd);
    }

   function Left(str, n)
    /***
            IN: str - the string we are LEFTing
                n - the number of characters we want to return

            RETVAL: n characters from the left side of the string
    ***/
    {
            if (n <= 0)     // Invalid bound, return blank string
                    return "";
            else if (n > String(str).length)   // Invalid bound, return
                    return str;                // entire string
            else // Valid bound, return appropriate substring
                    return String(str).substring(0,n);
    }

    function Right(str, n){
        if (n <= 0)
           return "";
        else if (n > String(str).length)
           return str;
        else {
           var iLen = String(str).length;
           return String(str).substring(iLen, iLen - n);
        }
    }


    // Default //
    
    // higlight menu when mouseover
    // Ver.: 2.0.0
    function hoverMenu(sNodeID) {
        try {
            if (document.getElementById('sLI_ID').value == sNodeID) // If we hover over curently selected menu item, do not highlight
            {
                return;
            }
            else {
                document.getElementById('sOldColor_Node').value = document.getElementById(sNodeID).style.color;
                document.getElementById(sNodeID).style.backgroundColor = '#8080FF';
                document.getElementById(sNodeID).style.color = 'White';
            }
        }
        catch (e) { }
    }

    // release menu on mouseout
    // Ver.: 2.0.0
    function leaveMenu(sNodeID) {
        if (document.getElementById('sLI_ID').value == sNodeID) {
            return;
        }
        else {
            document.getElementById(sNodeID).style.backgroundColor = 'transparent';
            document.getElementById(sNodeID).style.color = document.getElementById('sOldColor_Node').value; /*'Black';*/
        }
    }