// ** begin front door promo functions
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
// ** end front door promo functions

// utility function to check if form field contains only whitespace
function isBlank(s) {
  for(var i = 0; i < s.length; i++) {
    var c = s.charAt(i);
    if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
  }
  return true;
}

// function to check if kw search keyword input is empty, if not submit appropriate form
function validateKeywordForm () {
    if ((document.kwsearchbar.qt.value == null) || (document.kwsearchbar.qt.value == "") || isBlank(document.kwsearchbar.qt.value)) {
        alert("You must provide a query term.");
    } else {
        document.kwsearchbar.submit();
    }
}

// checks a form to see if we have any COCO Ids to use
// if not, alert with a message, and return false
function verifyCocoForm(f,m) {
   // f is the form element, or form element Id, or form element name
   //    (if it's the name, it will only do the first ellement found)
   // m is the message to alert, or undefined if you don't want one
   //    (still returns false, of course)

   var n = 'id'; // the form element to look for

   // figure out what out input is
   if ( (typeof(f)).match(/^(STRING|NUMBER)$/i) ) {
      if ( undefined != document.getElementById(f) ) {
         f = document.getElementById(f);
      } else {
         if ( document.getElementsByName &&
              undefined != document.getElementsByName(f) ) {
            f = document.getElementsByName(f)[0];
         }
      }
   }
   // if we're still here, then f is a node we can operate on

   // if the browser can't handle getElementsByTagName, just let them
   // go and hope for the best
   if (! f.getElementsByTagName) {
     return true;
   }

   var elle = f.getElementsByTagName('input');
   for (var i=0; elle != undefined && i < elle.length; i++) {

      // if our form has any hidden elements with the name n, we're done
      if ( elle[i].name == n &&
           elle[i].type.match(/^HIDDEN$/i) ) { alert('hidden n');
         return true;
      }

      // if our form has any checkboxes that are checked, we're done
      if ( elle[i].name == n &&
           elle[i].checked &&
           elle[i].type.match(/^CHECKBOX$/i) ) {
         return true;
      }
   }

   // if we made it this far, we didn't find anything
   if (undefined != m) {
       alert(m);
   }
   return false;
}

function addHoverClass(nav) {
    if(document.all && document.getElementById(nav).currentStyle){
        // only MSIE supports document.all
        var navroot = document.getElementById(nav);
        if (navroot != null) {
            /* Get all the list items within the menu */
            var lis=navroot.getElementsByTagName("LI");
            for(i=0;i<lis.length;i++){
                lis[i].onmouseover=function(){
                    this.className+=" over";
                    menuIFrameDisplay(this, "block");
                }
                lis[i].onmouseout=function(){
                    this.className=this.className.replace(" over", "");
                    menuIFrameDisplay(this, "none");
                }
            }
        }
    }
}
function menuIFrameDisplay(e, value) {
    for (j = 0; j < e.childNodes.length; j++) {
        if (e.childNodes[j].tagName == "IFRAME") {
            e.childNodes[j].style.display = value;
        }
    }
}
window.onload=function() {
    addHoverClass('mainMenu');
}

function isValid(textStr) {
    return ((textStr != null) && !isBlank(textStr));
}

/* Javascript functions for search */

/* clean up search text param */
function encodeText(keyword) {
    keyword = decodeURIComponent(keyword);

    /// strip quotes
    keyword = keyword.replace(/\"/g, "");

    /// strip leading whitespaces
    keyword = keyword.replace(/^\s+/, "");
    /// strip trailing quotes
    keyword = keyword.replace(/\s+$/, "");
    /// trim consecutive spaces to one
    keyword = keyword.replace(/\s+/, " ");

    /// for purpose of pretty url rewriting: tranform hyphens to underscores and spaces to hyphens
    keyword = keyword.replace(/-/g, "_");
    keyword = keyword.replace(/\s+/g, "-");

    keyword = escape(keyword);
    return encodeURIComponent(keyword);
}

/* Javascript functions for search : pretty rewrite search url and submit */
function validateAndForward(aForm) {
    var textStr = aForm.qt.value;
    if (!isValid(textStr)) {
        alert("You must provide a query term.");
    } else {
        var keyword = encodeText(textStr);
        window.location = "/" + aForm.nodeid.value + "/" + keyword + ".html";
    }
}
