var rulesNum;
var isIE;

var marqueeheight  = "18px";
var xaamarqueeheight  = "18px";
var marqueespeed   = 1;
var xaamarqueespeed   = 2;
var marqueebgcolor = "#F1763B";
var xaamarqueebgcolor = "#4791c5";
var pauseit        = 1;
var xaapauseit        = 1;

marqueespeed = (document.all)? marqueespeed : Math.max(1, marqueespeed-1);
xaamarqueespeed = (document.all)? xaamarqueespeed : Math.max(1, xaamarqueespeed-1);
var copyspeed = marqueespeed;
var xaacopyspeed = xaamarqueespeed;
var pausespeed = (pauseit == 0) ? copyspeed : 0;
var xaapausespeed = (xaapauseit == 0) ? xaacopyspeed : 0;
var iedom = document.all || document.getElementById;

var actualwidth = '';
var xaaactualwidth = '';
var cross_marquee, ns_marquee;
var xaacross_marquee, xaans_marquee;

function sendForm(formName, controlName, newValue) {
   document.forms[formName].elements[controlName].value = newValue;
   document.forms[formName].submit();
}

function displayCaption(elID) {
   if(isIE)
      document.styleSheets[0].addRule("table.news_mosaic td.caption_pane#" + elID + " p", "display: block");
   else
      document.styleSheets[0].insertRule("table.news_mosaic td.caption_pane#" + elID + " p { display: block }", rulesNum);
}

function hideCaption() {
   if(isIE)
      document.styleSheets[0].removeRule(rulesNum);
   else
      document.styleSheets[0].deleteRule(rulesNum);
}

function detectUserAgent() {
   var agt = navigator.userAgent.toLowerCase();
   if(agt.indexOf("msie") != -1) {
      rulesNum = document.styleSheets[0].rules.length;
      isIE = true;
   }
   else {
      rulesNum = document.styleSheets[0].cssRules.length;
      isIE = false;
   }
}

/* Menus */
function showMenu(i, level) {
   menuId = "idMenu" + i
   if(level == '0') window.scrollTo(0,0);
   if(ns4) {
      if(document.layers[menuId] != null) {
         document.layers[menuId].visibility = "show"
         document.layers[menuId].display = "block"
      }
   }
   else {
      var theMenuObj = document.getElementById(menuId)
      if(theMenuObj) {
         theMenuObj.style.visibility = "visible"
         theMenuObj.style.display = "block"
      }
   }
   eval("Menu_" + level + "='" + i + "'");
}

function hidePreviousMenu(level) {
   if(eval("Menu_" + level) != "") {
      nextLevel = parseInt(level) + 1;
      menuId = "idMenu" + eval("Menu_" + level);

      if(ns4) {
         if(document.layers[menuId] != null) {
            document.layers[menuId].visibility = "hide"
            document.layers[menuId].display = "none"
         }
      }
      else {
         menouIdObj = document.getElementById(menuId)
         if (menouIdObj) {
           menouIdObj.style.visibility = "hidden"
           menouIdObj.style.display = "none"
         }
      }
      eval("Menu_" + level + "= ''");
      hidePreviousMenu(nextLevel);
   }
}

function doMenu(i, level) {
   hidePreviousMenu(level);
   showMenu(i, level);
}

function closeMenus() {
   hidePreviousMenu('0');
}






/* Notifications Marquee */
function populate(){
    xaamarqueewidth  = document.body.clientWidth-360;
      if (iedom)
      {
          xaacross_marquee = document.getElementById ? document.getElementById("xaaiemarquee") : document.all.xaaiemarquee;
          if (xaacross_marquee)
          {
            xaacross_marquee.style.left = parseInt(xaamarqueewidth) + 8 + "px";
            xaacross_marquee.innerHTML = xaamarqueecontent;
            xaaactualwidth = document.all ? xaatemp.offsetWidth : document.getElementById("xaatemp").offsetWidth;
            xaalefttime = setInterval("xaascrollmarquee()", 20);
          }
      }
      else if( document.layers )
      {
          xaans_marquee = document.xaans_marquee.document.xaans_marquee2;
          if (xaans_marquee)
          {
            xaans_marquee.left = parseInt(xaamarqueewidth) + 8;
            xaans_marquee.document.write(xaamarqueecontent);
            xaans_marquee.document.close();
            xaaactualwidth = xaans_marquee.document.width;
            xaalefttime = setInterval("xaascrollmarquee()", 20);
          }
      }



    marqueewidth  = document.body.clientWidth-360;
      if (iedom)
      {
          cross_marquee = document.getElementById ? document.getElementById("iemarquee") : document.all.iemarquee;
          if (cross_marquee)
          {
            cross_marquee.style.left = parseInt(marqueewidth) + 8 + "px";
            cross_marquee.innerHTML = marqueecontent;
            actualwidth = document.all ? temp.offsetWidth : document.getElementById("temp").offsetWidth;
            lefttime = setInterval("scrollmarquee()", 20);
          }
      }
      else if( document.layers )
      {
          ns_marquee = document.ns_marquee.document.ns_marquee2;
          if (ns_marquee)
          {
            ns_marquee.left = parseInt(marqueewidth) + 8;
            ns_marquee.document.write(marqueecontent);
            ns_marquee.document.close();
            actualwidth = ns_marquee.document.width;
            lefttime = setInterval("scrollmarquee()", 20);
          }
      }


}



function scrollmarquee(){
    marqueewidth  = document.body.clientWidth-360;
    if( iedom )
    {
        if (cross_marquee)
        {
          if( parseInt( cross_marquee.style.left ) > ( actualwidth * (-1) + 8) )
          {
              cross_marquee.style.left = parseInt( cross_marquee.style.left ) - copyspeed + "px";
          }
          else
          {
              cross_marquee.style.left = parseInt( marqueewidth ) + 8 + "px";
          }
        }

    } else if( document.layers )
    {
      if (ns_marquee)
      {
        if( ns_marquee.left > ( actualwidth * (-1) + 8) ) {
            ns_marquee.left -= copyspeed;
        } else {
            ns_marquee.left = parseInt( marqueewidth ) + 8;
        }
      }
    }
}

function xaascrollmarquee(){
    xaamarqueewidth  = document.body.clientWidth-360;
    if( iedom )
    {
      if (xaacross_marquee)
      {
        if( parseInt( xaacross_marquee.style.left ) > ( xaaactualwidth * (-1) + 8) )
        {
            xaacross_marquee.style.left = parseInt( xaacross_marquee.style.left ) - xaacopyspeed + "px";
        }
        else
        {
            xaacross_marquee.style.left = parseInt( xaamarqueewidth ) + 8 + "px";
        }
      }

    } else if( document.layers )
    {
      if (xaans_marquee)
      {
        if( xaans_marquee.left > ( xaaactualwidth * (-1) + 8) ) {
            xaans_marquee.left -= xaacopyspeed;
        } else {
            xaans_marquee.left = parseInt( xaamarqueewidth ) + 8;
        }
      }
    }
}

/* Subscription Templates */
function selectColumn() {
   var availSelect = document.forms["templatesForm"].elements["availableCols"];
   var selSelect   = document.forms["templatesForm"].elements["selectedCols"];
   var selIndex = availSelect.selectedIndex;
   if(selIndex != -1) {
      var selOption = availSelect.options[selIndex];
      availSelect.options.remove(selIndex);
      selSelect.options.add(selOption);
   }
}

function deselectColumn() {
   var availSelect = document.forms["templatesForm"].elements["availableCols"];
   var selSelect   = document.forms["templatesForm"].elements["selectedCols"];
   var selIndex = selSelect.selectedIndex;
   if(selIndex != -1) {
      var selOption = selSelect.options[selIndex];
      selSelect.options.remove(selIndex);
      availSelect.options.add(selOption);
   }
}

function deselectAll() {
   var availSelect = document.forms["templatesForm"].elements["availableCols"];
   var selSelect   = document.forms["templatesForm"].elements["selectedCols"];
   var selItems = selSelect.options.length;
   if(selItems > 0) {
      var selOption;
      for(var i = selItems - 1; i >= 0; i--) {
         selOption = selSelect.options[i];
         selSelect.options.remove(i);
         availSelect.options.add(selOption);
      }
   }
}

function redisplay(pageUrl) {
   var select = document.forms["templatesForm"].elements["sbn"];
   var sbnID = select.options[select.selectedIndex].value;
   window.location = pageUrl + sbnID;
}

function submitTemplatesForm() {
   var templForm = document.forms["templatesForm"];
   var selSelect = templForm.elements["selectedCols"];
   var itemCount = selSelect.options.length;
   if(itemCount > 0) {
      var columns = "";
      for(var i = 0; i < itemCount; i++) {
         columns = columns + selSelect.options[i].value + ";";
      }
      templForm.elements["columns"].value = columns;
      templForm.submit();
   }
}

/* Search page (Subjects) */
function showSubjectsDialog(context, controlNum) {
   var width  = 500;
   var height = 250;
   var top  = (screen.height - height) / 2;
   var left = (screen.width - width) / 2;
   window.open(context + "/subjects?control=" + controlNum, "sbjbrowser",
               "height=" + height + ", width=" + width + ", top=" + top + ", left=" + left + ", location=no, menubar=no, resizable=no, scrollbars=no, status=no, toolbar=no");
}

function setSubject(selectControl, controlNum) {
   var select = document.forms[0].elements[selectControl];
   var selIndex = select.selectedIndex;
   if(selIndex > 0) {
      var selOption = select.options[selIndex];
      var nameControl  = window.opener.document.forms["searchform"].elements["sbjname" + controlNum];
      var valueControl = window.opener.document.forms["searchform"].elements["SBJCT" + controlNum];
      nameControl.value = selOption.text;
      valueControl.value = selOption.value;
      window.close();
   }
}

/* Selector */
function openSelector(context, type, formName, keyField, nameField) {
   window.open(context + "/selector?seltype=" + type + "&form=" + formName + "&keyfield=" + keyField + "&namefield=" + nameField,
               "selector",
               "height=230, width=330, top=200, left=300, location=no, menubar=no, resizable=no, scrollbars=no, status=no, toolbar=no");
}

function performSelect(formName, keyField, nameField) {
   var form = window.opener.document.forms[formName];
   var select = document.getElementById("Selector");
   if(select.selectedIndex != -1) {
      var selOption = select.options[select.selectedIndex];
      form.elements[keyField].value = selOption.value;
      form.elements[nameField].value = selOption.innerHTML;
      window.close();
   }
}

function clearSelect(formName, keyField, nameField) {
   var form = document.forms[formName];
   form.elements[keyField].value = "";
   form.elements[nameField].value = "";
}

function showHelpWindow(helpPage) {
   var width  = 648;
   var height = 500;
   var top  = (screen.height - height) / 2;
   var left = (screen.width - width) / 2;
   window.open("/help/" + helpPage, "helpwindow",
               "height=" + height + ", width=" + width + ", top=" + top + ", left=" + left + ", location=no, menubar=no, resizable=no, scrollbars=yes, status=no, toolbar=no");
}

function showFTSHelpWindow(helpPage) {
   var width  = 600;
   var height = 300;
   var top  = (screen.height - height) / 2;
   var left = (screen.width - width) / 2;
   window.open(helpPage, "help",
               "height=" + height + ", width=" + width + ", top=" + top + ", left=" + left + ", location=no, menubar=no, resizable=no, scrollbars=yes, status=no, toolbar=no");
}

function countAddToBasketItems() {
	var count = 0;
	var items = document.getElementsByName('basketitem');
	for(var i=0; i<items.length; i++) {
		if(items[i].checked) {
			count++;
		}
	}
	return count;
}

function getWinConfig(height, width) {
	var left = parseInt((screen.availWidth/2) - (width/2));
	var top = parseInt((screen.availHeight/2) - (height/2));
	return 'height=' + height + ',width=' + width +
		',status=yes,toolbar=no,menubar=no,location=no' +
		',left=' + left + ',top=' + top +
		',screenX=' + left + ',screenY=' + top +
		',resizable=yes,scrollbars=yes';
}

var dnldwin;

function checkWin() {
	if(dnldwin != null) { dnldwin.close(); }
}

function openDnldWin(url) {
	checkWin();
	dnldwin = window.open(url, 'dnld', getWinConfig(200, 350));
	dnldwin.focus();
}