
//2003-09-02 - kjurecka - JTS 8605  Added type I and G for Wrapper Display
//2003-10-03 - jcole - JTS 7900  Added isNumber function
//2004-11-05 - kjurecka - HBN-811:  modified function go to act as a normal redirect during the trial mktg testing of removing pop-ups.
//2006-10-03 - jstone - HBN-4049: added activateObjects for IE Flash activation
//2006-10-04 - jstone - HBN-4049: added writeObject method for IE activation.  activateObjects didn't work on WMP.
//2006-10-10 - jstone - HBN-4049: added writeWMP method for Windows Media Player object creation and activation.
//2006-12-12 - jcole - HBN-4590: Added checkMaxEleSize function
//2007-01-24 - ehawthorne - MKTG-1612: Added marketingPoll function
//2007-01-26 - kjurecka - MKTG-1612: Modified marketingPoll function since it isn't working in IE
//2007-02-05 - ehawthorne - MKTG-1699: Disabling the marketingPoll function.

    function QueryString(key) {
         var value = null;
         for (var i=0;i<QueryString.keys.length;i++) {
             if (QueryString.keys[i]==key) {
                   value = QueryString.values[i];
                   break;
              }
          }
          return value;
    }

    QueryString.keys = new Array();
    QueryString.values = new Array();

    function QueryString_Parse() {
        var query = window.location.search.substring(1);
        var pairs = query.split("&");
        for (var i=0;i<pairs.length;i++) {
            var pos = pairs[i].indexOf('=');
            if (pos >= 0) {
                var argname = pairs[i].substring(0,pos);
                var value = pairs[i].substring(pos+1);
                QueryString.keys[QueryString.keys.length] = argname;
                QueryString.values[QueryString.values.length] = value;
            }
        }
    }

    function sampleGrow(url) {
        if (window.outerWidth<=575) {
            window.resizeTo(675,window.outerHeight);
            location.href=url;
        } else if (window.document.body.clientWidth<=575 && window.document.documentElement.clientHeight) {
            window.resizeTo(675,window.document.documentElement.clientHeight+15);
              location.href=url;
        } else if (window.document.body.clientWidth<=575) {
            window.resizeTo(675,window.document.body.clientHeight);
            location.href=url;
        } else {
            location.href=url;
        }
    }

   function openNewWindow(url,height,width,left,top,type,/*optional*/options, /*optional*/name) {
    // Instead of specifying _blank, _parent, etc. use the name of "remote".
    // Note that as soon as some specs are listed, all of the defaults are turned OFF.
      self.name = "opener";
      if (!menubar)
         var menubar = 'no';
      var specs="resizable,scrollbars,status,width="+width+",height="+height+",left="+left+",top="+top;
      if (options) {
         specs = specs + ',' + options;
      }
      if (!name)
         var name = "remote";
      remote = window.open(url, name, specs);
      if (typeof(remote)!= "undefined") {
          if (type==1) {
              window.focus();
              remote.blur();
          } else {
              remote.focus();
          }
      }
    }

     // Function to display mktg popunder form
     function loadpopunder(url) {
          remote=window.open(url,"","width=800,height=510,scrollbars=1,resizable=1,toolbar=1,location=1,menubar=1,status=1,directories=0");
          if (typeof(remote)!= "undefined") {
            remote.blur();
          }
          window.focus();
     }

    //Window pop-up function
    function launch(url) {
        openNewWindow(url,560,640,20,20);
    }

    //Change the action of a form
    function changeFormAction(formName,action){
        document.forms[formName].action = action;
    }

    //Javascript validation stuff

    function IsChecked(input){
        for(var i=0;i<input.length;i++){
            if (input[i].checked){
                return true;
            }
        }
        alert("You must first select a radio button.");
        return false;
    }

    function SetCookie(CookieName,CookieValue,Expires) {
        if (Expires != 0) {
            document.cookie = CookieName + "=" + CookieValue + "; expires=" + Expires.toGMTString() + "; domain=hoovers.com; path=/";
        } else {
            document.cookie = CookieName+ "=" + CookieValue + "; domain=hoovers.com; path=/";
        }
    }

    function getUserName() {
        var UserName = GetSlice(GetCookie('auth_user_info'), '_', 4);
        if (UserName.length  == 0) {
            //registered hoov user does not exist so lets see if auth_user_info is populated from IP Authorization
            UserName = GetSlice(GetCookie('auth_user_info'),'_',1);
            switch (GetSlice(GetCookie('auth_user_info'),'_',3))
            {
                case 'Library':
                    UserName = "Library Researcher";
                    break;
                case 'Academic':
                    UserName = "Academic Researcher";
                    break;
            }

            /* The following information is the code to the label IPSURVEYEND
            is used to turn on ip survey popup which allows marketing and sales to gather
            feedback from selected users. The number next to each case is a userid defined in erights.
            if (GetCookie("ipSurveyTrack") != 1) {
                switch(GetSlice(GetCookie('ipAuthUserInfo'),'_',0))
                {
                 case '641793':
                        ipAuthCustomerSurvey();
                 case '122996':
                        ipAuthCustomerSurvey();
                 case '122013':
                        ipAuthCustomerSurvey();
                 case '228014':
                        ipAuthCustomerSurvey();
                 case '121501':
                        ipAuthCustomerSurvey();
                 case '473511':
                        ipAuthCustomerSurvey();
                 case '121443':
                        ipAuthCustomerSurvey();
                 case '120':
                        ipAuthCustomerSurvey();
                 case '615222':
                        ipAuthCustomerSurvey();
                 case '601319':
                        ipAuthCustomerSurvey();
                }
            }
            IPSURVEYEND
            */
        }
        if (UserName.length > 0) {
            return UserName;
        }
        return "";
    }

    function GetCookie(CookieName){
        Value = "";
        var Start = document.cookie.indexOf(CookieName + "=");
        if (Start != -1 ){
            Start = Start + 1 + CookieName.length;
            End = document.cookie.indexOf(";", Start);
            if (End == -1){
                End = document.cookie.length;
            }
            Value = document.cookie.substring(Start, End);
            Value = Value.replace(/\+/g, "%20");
            Value = unescape(Value);
        }
        return Value;
    }

    function GetSlice(String, Delimiter, SliceToGet){
        if (String == null || String == "") {
            return "";
        }
        var Slices = String.split(Delimiter);
        Value = "";
        if (Slices.length > SliceToGet){
            return Slices[SliceToGet];
        }
        return "";
    }

    // extracts hoovUser cookie data at position 'pos' (accounting for possible _'s in email address)
    function parseHoovUser(pos) {
        var elements = GetCookie('hoovUser').split('_');
        var exp = '@';
        while (!elements[1].match(exp) && GetCookie('hoovUser').match(exp)) {
            elements[1] += '_' + elements[2];
            for (i = 2; i < elements.length; i++) {
                elements[i] = elements[i+1];
            }
            elements.splice(elements.length-1, 1);
        }
        return elements[pos];
    }

    function GetUserType(UserTypeChar){
        switch (GetSlice(GetCookie('auth_user_info'),'_',3))
        {
            case 'Library':
                UserTypeChar = "XXX";
                break;
            case 'Academic':
                UserTypeChar = "XXX";
                break;
        }
        switch (UserTypeChar){
            case 'PP':
                return "Pro Premium Subscriber";
                break;
            case 'P':
            case 'PFT':
            case 'PSB':
                return "Pro Plus Subscriber";
                break;
            case 'E':
                return "Pro Subscriber";
                break;
            case 'T':
                return "Trial Subscriber";
                break;
            case 'FD':
                return "Trial Subscriber";
                break;
            case 'C':
                return "Comp Member";
                break;
            case 'I':
                return "Lite Subscriber";
                break;
            case 'G':
                return "Basic Subscriber";
                break;
            case 'EUK':
                return "Pro Subscriber";
                break;
            case 'PUK':
                return "Pro Plus Subscriber";
                break;
            case 'PPUK':
                return "Pro Premium Subscriber";
                break;
            case 'TUK':
                return "Trial Subscriber";
                break;
            case 'XXX':
                return "";
                break;
            default:
                return "Subscriber";
                break;
        }
    }

    function GetUnityUserType(UserTypeChar, numberOfSeats){
        switch (GetSlice(GetCookie('auth_user_info'),'_',3))
        {
            case 'Library':
            case 'Academic':
                UserTypeChar = "XXX";
                break;
        }
        switch (UserTypeChar){
            case 'PP':
            case 'PPUK':
                if (!isNaN(numberOfSeats)) {
                    if (numberOfSeats == 3) {
                        return "Select Premium SB Subscriber";
                    } else {
                        return "Premium Select Subscriber";
                    }
                } else {
                    return "Premium Select Subscriber";
                }
                break;
            case 'P':
            case 'PFT':
            case 'PSB':
            case 'PUK':
                if (!isNaN(numberOfSeats)) {
                    if (numberOfSeats == 3) {
                        return "Select Plus SB Subscriber";
                    } else {
                        return "Plus Subscriber";
                    }
                } else {
                    return "Plus Subscriber";
                }
                break;
            case 'E':
            case 'EUK':
                if (!isNaN(numberOfSeats)) {
                    if (numberOfSeats == 1) {
                        return "Hoover's Executive Subscriber";
                    } else if (numberOfSeats == 3) {
                        return "Select Pro SB Subscriber";
                    } else {
                        return "Pro Subscriber";
                    }
                } else {
                    return "Pro Subscriber";
                }
                break;
            case 'T':
            case 'FD':
            case 'TUK':
                return "Trial Subscriber";
                break;
            case 'C':
                return "Comp Member";
                break;
            case 'I':
                return "Lite Subscriber";
                break;
            case 'G':
                return "Basic Subscriber";
                break;
            case 'XXX':
                return "";
                break;
            default:
                return "Subscriber";
                break;
        }
    }

    function ClearForm(form){
        for(var i=0;i<form.elements.length;i++){
            form.elements[i].value="";
        }
    }

    function GetDateString(theDate){
        var Months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
        var Days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");

        var Year = theDate.getFullYear();
        var Date = theDate.getDate();
        var DayOfWeek = Days[theDate.getDay()];
        var Month = Months[theDate.getMonth()];

        return DayOfWeek + ", " + Month + " " + Date + ", " + Year;
    }


    function ipAuthCustomerSurvey() {
        openNewWindow("/global/hoov/index.xhtml?pageid=10781",450,500,0,0);
        tomorrow = new Date();
        tomorrow.setDate(tomorrow.getDate()+23)
        tomorrow.setHours(24);
        tomorrow.setMinutes(0);
        tomorrow.setSeconds(0);
        tomorrow.setMilliseconds(0);
        SetCookie("ipSurveyTrack","1",tomorrow);
    }

    function openOffsite(url) {
        window.open(url);
    }

     function goPopup(url)
     {
          opener.location.href=url;onClick=window.close();
     }

    function go(url) {window.location.href=url;}

    // This function will check to make sure that only numeric and other
    // specified chars are allowed.
    // Example usage: onBlur="isNumber(this)"
    function isNumber(field) {

        var valid = "-.0123456789";
        var ok = "yes";
        var temp;

        for (var i=0; i<field.value.length; i++) {
            temp = "" + field.value.substring(i, i+1);
            if (valid.indexOf(temp) == "-1") ok = "no";
        }

        if (ok == "no") {
            alert("Only numbers are accepted.");
            field.focus();
            field.select();
            return false;
        }

    }

    // Start exit popup section --------------------- //

    // Set the onUnload event inline
    //document.onUnload = exitPopup;

    // Set the onMouseDown event to check for Hoovers url
    document.onmousedown = isHooversURL;
    var suppress_popup = false;

    if(document.captureEvents) {
        document.captureEvents(Event.MOUSEDOWN);
    }

    function setPopupSuppress(e, re) {

        if (e.srcElement) {
            if (re.test(e.srcElement.href)) {
               suppress_popup = true;
            }
        } else if (e.target.parentNode) {
            if (re.test(e.target.parentNode)) {
               suppress_popup = true;
            }
        } else {
            if (re.test(e.target)) {
               suppress_popup = true;
            }
        }

    }

    // Function to check if the user is clicking on a Hoovers link or not
     function isHooversURL(e){

          var re = /hoovers.com/;
          if (!e) var e = window.event;
          setPopupSuppress(e, re);

     }

    // Function to display exit pop up ad
    function exitPopup() {

         var height   = 450;
         var width    = 425;
         var top      = 0;
         var left     = 0;

        // Only display ad if user is leaving Hoovers site, user is not logged in,
        // and cookie has not been set

        // JTS9989 SM Commented out the following code block to disable add
        // Check to see if cookie has been set indicating completion of form
        //if(!suppress_popup && !getUserName() && !GetCookie('exitAd')) {
        //    var URL = '/global/mktg/index.xhtml?pageid=10906';
            // Display exit pop up ad
        //    openNewWindow(URL, height, width, left, top);
            // Set cookie so that exit ad is only displayed once per session.
        //    SetCookie('exitAd',true,0);
        //}

    }

    // Extracts the reponse phone number from the visitor_from cookie, formats and diplays it.
    // Used on marketing pages
    function displayResponsePhone(defaultPhone) {
        var cookieValue = GetCookie('visitor_from');
        var responsePhone = '';
        var display = '';

        if (cookieValue && cookieValue.toString().length > 0) {
            responsePhone = GetSlice(cookieValue, '-', 2);
        }

        if (responsePhone == "") {
            responsePhone = defaultPhone;
        }
        display = "(" + responsePhone.substring(0, 3) + ") " + responsePhone.substring(3, 6);
        display += "-" + responsePhone.substring(6, 10);
        document.write(display);
    }

    function displayResponsePhoneA(defaultPhone)
    {
        var cookieValue = GetCookie('visitor_from');
        var responsePhone = '';
        var display = '';

        if (cookieValue && cookieValue.toString().length > 0) {
            responsePhone = GetSlice(cookieValue, '-', 2);
        }

        if (responsePhone == "")
            if (document.referrer.indexOf('global-mktg-join') > -1)
                responsePhone = '8664733932';
            else
                responsePhone = defaultPhone;
        display = "(" + responsePhone.substring(0, 3) + ") " + responsePhone.substring(3, 6);
        display += "-" + responsePhone.substring(6, 10);
        document.write(display);
    }
    // Declaring required variables for phone number validation
    var digits = "0123456789";
    // non-digit characters which are allowed in phone numbers
    var phoneNumberDelimiters = ".()- ";
    // characters which are allowed in international phone numbers
    // (a leading + is OK)
    var validWorldPhoneChars = phoneNumberDelimiters + "+";
    // Minimum no of digits in an international phone no.
    var minDigitsInIPhoneNumber = 10;

    // Used in phone number validation
    function isInteger(s) {
        var i;
        for (i = 0; i < s.length; i++)
        {
            // Check that current character is number.
            var c = s.charAt(i);
            if (((c < "0") || (c > "9"))) return false;
        }
        // All characters are numbers.
        return true;
    }

    // Used in phone number validation
    function stripCharsInBag(s, bag) {
        var i;
        var returnString = "";
        // Search through string's characters one by one.
        // If character is not in bag, append to returnString.
        for (i = 0; i < s.length; i++)
        {
            // Check that current character isn't whitespace.
            var c = s.charAt(i);
            if (bag.indexOf(c) == -1) returnString += c;
        }
        return returnString;
    }

    // Phone number validation
    function checkInternationalPhone(strPhone) {
        s=stripCharsInBag(strPhone,validWorldPhoneChars);
        return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
    }

    function checkEmail(emailAddress) {
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(emailAddress)) {
            return (true)
        }
        return (false)
    }

    function validateEmail(emailAddress) {
        if (!checkEmail(emailAddress)) {
            alert("Invalid e-mail address.");
            return (false);
        } else {
            return (true);
        }
    }

    function validateEmailAndPhone(emailAddress, phoneNumber) {
        var errMessage = "";

        if (!checkEmail(emailAddress)) {
            errMessage += "Invalid e-mail address.\n";
        }
        if (!checkInternationalPhone(phoneNumber)) {
            errMessage += "Invalid phone number.";
        }

        if (errMessage == "") {
            return (true);
        } else {
            alert(errMessage);
            return (false);
        }
    }

    function MyWindow() {
        this.width = 200;
        this.height = 200;
        this.top = 20;
        this.left = 20;
        this.resize = "yes";
        this.scroll = "yes";
        this.menu = "no";
        this.location = "no";
        this.status = "no";
        this.tool = "no";
        this.handle = null;
        this.focusOnOpen = true;

        this.MyWindow = function() {
            this.width = 200;
            this.height = 200;
            this.top = 20;
            this.left = 20;
            this.resize = "yes";
            this.scroll = "yes";
            this.menu = "no";
            this.location = "no";
            this.status = "no";
            this.tool = "no";
            this.handle = null;
        }

        this.setFocusOnOpen = function(f) {
            this.focusOnOpen = f;
        }

        this.toolBarOn = function() {
            this.tool = "yes";
        }

        this.toolBarOff = function() {
            this.tool = "no";
        }


        this.menuBarOn = function() {
            this.menu = "yes";
        }

        this.menuBarOff = function() {
            this.menu = "no";
        }

        this.scrollBarsOn = function() {
            this.scroll = "yes";
        }

        this.scrollBarsOff = function() {
            this.scroll = "no";
        }

        this.locationOn = function() {
            this.location = "yes";
        }

        this.locationOff = function() {
            this.location = "no";
        }

        this.statusBarOff = function() {
            this.status = "no";
        }

        this.statusBarOn = function() {
            this.status = "yes";
        }

        this.canResizeOn = function() {
            this.rezise = "yes";
        }

        this.canResizeOff = function() {
            this.rezise = "no";
        }

        this.setWidth = function(w) {
            this.width = w;
        }

        this.setHeight = function(h) {
            this.height = h;
        }

        this.setTop = function(t) {
            this.top = t;
        }

        this.setLeft = function(l) {
            this.left = l;
        }

        this.setName = function(n) {
            this.name = n;
        }

        this.setUrl = function(u) {
            this.url = u;
        }

        this.gethandle = function() {
            //returns the handle of the opened window
            return this.handle;
        }

        this.open = function() {
            paramString = "width=" + this.width + ",height=" + this.height + ",menubar="+this.menu;
            paramString += ",resizable=" + this.resize +",location="+this.location+",status="+this.status;
            paramString += ",toolbar="+this.tool+",scrollbars="+this.scroll;
            paramString += ",top="+this.top+",left="+this.left;
            this.handle = window.open(this.url,this.name,paramString);
            if (this.focusOnOpen) {
                this.handle.focus();
            }
        }

    }

  function explodeArray(item,delimiter) {
    tempArray=new Array(1);
    var Count=0;
    var tempString=new String(item);

    while (tempString.indexOf(delimiter)>0) {
        tempArray[Count]=tempString.substr(0,tempString.indexOf(delimiter));
        tempString=tempString.substr(tempString.indexOf(delimiter)+1,tempString.length-tempString.indexOf(delimiter)+1);
        Count=Count+1
    }

    tempArray[Count]=tempString;
    return tempArray;
}


function URLDecode(encoded)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef";
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
       if (ch == "+") {
           plaintext += " ";
           i++;
       } else if (ch == "%") {
            if (i < (encoded.length-2)
                    && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
                    && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
                plaintext += unescape( encoded.substr(i,3) );
                i += 3;
            } else {
                plaintext += "%[ERROR]";
                i++;
            }
        } else {
           plaintext += ch;
           i++;
        }
    } // while
   return plaintext;
};

function BTS() {
     //$search_cookie_value = GetSlice(GetCookie('HoovWhatWasSearchedSession'), '_', 0);
     openNewWindow('/global/mktg/index.xhtml?pageid=13782',405,775,20,20,0);

}
//For explination on usage and need for the PropertyStyleClass and the DomStyle Class please see Wiki Entry
// http://wiki.hoovers.com/bin/edit/HOL/JavaScriptChangingStyle

function PropertyStyle() {
    this.changeStyle = function(childElement,attribute,value) {
        childElement.style[attribute] = value;
    }
}

function DomStyle() {
    this.changeStyle = function(childElement,attribute,value) {
        childElement.setAttribute("style",attribute + ": " + value);
    }
}

function OpenSiteSelectorPopup() {
     if (GetCookie('HoovCountry') == "") {
          launch("/global/selectsiteform.xhtml");
     }
     return true;
}

function FormatCookieDate() {
    var d = new Date();
    var day= '' + d.getDate();
    if (day.length == 1) {
        day = '0' + day;
    }
    var month= '' + (d.getMonth() + 1);
    if (month.length == 1) {
        month = '0' + month;
    }
    var year=d.getFullYear();
    return currDate = '' + year + '' + month + '' + day;
}

function activateObjects() {
    objects = document.getElementsByTagName("object");
    for (var i = 0; i < objects.length; i++)
    {
        objects[i].outerHTML = objects[i].outerHTML;
    }
}

function writeObject(str, mdiv) {
    var d = document.getElementById(mdiv);
    d.innerHTML = str;
}

function writeWMP(url, mdiv) {
    str = '<OBJECT ID="MediaPlayer1" width="320" height="240" classid="CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902" standby="Loading Microsoft Windows Media Player components..." type="application/x-oleobject" style="margin:10px 0 0 10px">';
        str = str + '<PARAM NAME="FileName" VALUE="'+url+'" />';
        str = str + '<PARAM NAME="animationatStart" VALUE="true" />';
        str = str + '<PARAM NAME="transparentatStart" VALUE="true" />';
        str = str + '<PARAM NAME="autoStart" VALUE="true" />';
        str = str + '<PARAM NAME="showControls" VALUE="true" />';
        str = str + '<EMBED type="application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" name="MediaPlayer1" width="320" height="240" AutoStart="true" style="margin:10px 0 0 10px" src="'+url+'"></EMBED>';
    str = str + '</OBJECT>';
    writeObject(str, mdiv);
}

function checkMaxEleSize(element, max) {
    if (element.value.length > max) {
        var msg = "You have exceeded the " + max + " character limit for this field. Please shorten your search terms. Your current character count = " + element.value.length + ".";
        alert(msg);
        return false;
    }
    return true;
}

function marketingPoll() {
    //cookie set in feedcontrol.cls   
    /*
    if (GetCookie('mktgPop') == '1') {
        //setting cookie to 5 so popup doesn't try to reopen another popup
        SetCookie('mktgPop', 5, 0);
        window.open('/free/mktg/join.xhtml?pageid=15811','Survey','width=475,height=475');        
    }
    */
}


