// JavaScript
var fontsize_adjust = true;
var nos_class = "nos_collapsed";

function procNosLang() {
	document.frmlang.submit();
}

function chk_idx_search() {
	var q = document.searchform.query.value;
	var default_q = document.searchform.query.defaultValue;
	var valid = true;
	
	if (q == default_q) {
		valid = false;
	}
	return valid;
}

function toggle_vis(obj) {
	var el = document.getElementById(obj);
    if (el.style.display != 'none' ) {
    	el.style.display = 'none';
    }
    else {
    	el.style.display = '';
    }
}

function hide_jrdesc(no) {
	for(i=1; i<=no; i++) {
		var obj = "d"+i;
		el = document.getElementById(obj);
		el.style.display = 'none';
		//alert("id: "+obj+" Display: "+el.style.display);
	}
}

function getElementsByClassName(oElm, strTagName, strClassName) {
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for (i=0; i<arrElements.length; i++) {
		oElement = arrElements[i];
		if (oRegExp.test(oElement.className)) {
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function toggle_sums() {
	change_nos_class();
}

function change_nos_class() {
	
	if (nos_class == "nos_doc") {
		new_class = "nos_collapsed";
		str = "with";
	}
	else {
		new_class = "nos_doc";
		str = "without";
	}
	
	class_array = new Array();
	class_array = getElementsByClassName(document, "*", nos_class);
	nos_class = new_class;
	
	for (i=0; i<class_array.length; i++) {
		if (class_array[i].className != new_class ) {
			class_array[i].className = new_class;
		}
	}
	
	el = document.getElementById('sums');
	
	if (el) {
		el.innerHTML = str;
	}
}

function selectall() {
  // Select all nos 
  form = document.getElementById('form_nos');
  if (form) {
    input = form.getElementsByTagName('input');
    if (input.length > 0) {
      for (i=0; i <input.length; i++) {
        if ((input[i].type == 'checkbox') && input[i].checked == false) {
          input[i].checked = true;
        }
      }
    }
  }
}

function deselectall() {
  // Deselect all / any 'checked' nos selections 
  form = document.getElementById('form_nos');
  if (form) {
    input = form.getElementsByTagName('input');
    if (input.length > 0) {
      for (i=0; i <input.length; i++) {
        if ((input[i].type == 'checkbox') && input[i].checked) {
          input[i].checked = false;
        }
      }
    }
  }
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

// FONT SIZE

function rememberFontSize(divname,fontsize) {
  var cookie = readCookie("fontsize");
  var cookieout = '';
  if (cookie) {
    var divs = cookie.split('~');
    for (var i=0; i<divs.length;i++) {
      var pairs = divs[i].split('|');
      if (pairs[0] == divname) {
        cookieout += divname+'|'+fontsize+'~';
      } else if (pairs[0] && pairs[0] != 'undefined') {
        cookieout += pairs[0]+'|'+pairs[1]+'~';
      }
    }
  } else {
    cookieout = divname+'|'+fontsize+'~';
  }
  createCookie('fontsize',cookieout,365);
}

function setFontSize(cookieval) {
  var divs = cookieval.split('~');
  for (var i=0; i<divs.length;i++) {
    var pairs = divs[i].split('|');
    var div = document.getElementById(pairs[0]);
    if (div) {
      div.style.fontSize = pairs[1];
    }
  }
}

function increasefont(divname,step) {
  if (!step) { step = 10; }
  var div = document.getElementById(divname);
  if (!div) return;
  var fontsize = div.style.fontSize;
  if (!fontsize) {
    fontsize = "100%";
  }
  fontsize = parseInt(fontsize);
  fontsize += step;
  if (fontsize > 300) {
    alert('This is the maximium size.');
    fontsize = 300;
  }
  fontsize += '%';
  rememberFontSize(divname,fontsize);
  div.style.fontSize = fontsize;
}

function defaultfont(divname) {
  var fontsize = "";
  var div = document.getElementById(divname);
  if (!div) return;

  rememberFontSize(divname,fontsize);
  div.style.fontSize = fontsize;
}

function decreasefont(divname,step) {
  var cookie = readCookie("fontsize");
  if (!step) { step = 20; }
  var div = document.getElementById(divname);
  if (!div) return;
  var fontsize = div.style.fontSize;
  if (!fontsize) {
    fontsize = "100%";
  }
  fontsize = parseInt(fontsize);
  fontsize -= step;
  if (fontsize < 50) {
    alert('This is the minimum size.');
    fontsize = 50;
  }
  fontsize += '%';
  rememberFontSize(divname,fontsize);
  div.style.fontSize = fontsize;
}

/*
window.onload = function(e) {
  var cookie = readCookie("style");
  var fontsize = readCookie("fontsize");
  if (fontsize) {
    setFontSize(fontsize);
  }
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}


window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);
*/

function fix_menuIE() {
	// IE 6 and below: Amend styles for <ul><li> elements for 'a:hover' styles to 'a.over'
	// For drop down menu
	if (document.all&&document.getElementById)
	{
		navRoot = document.getElementById("nav");
		
		for (i=0; i<navRoot.childNodes.length; i++)
		{
			node = navRoot.childNodes[i];
			
			if (node.nodeName=="UL")
			{
				for (j=0; j<node.childNodes.length; j++)
				{
					node_child = node.childNodes[j];
					if (node_child.nodeName=="LI")
					{
						node_child.onmouseover=function() {
						this.className+=" over";
						}
						node_child.onmouseout=function() {
						this.className=this.className.replace(" over", "");
						}
					}
				}
			}
		}
	}
}


function openHelpWin(url) {
	var width = 600;
	var height = 400;
	var swidth = screen.width;
	var sheight = screen.height;
	var day = new Date();
	var id = day.getTime();
	var lf = (screen.width - width)/2;
	var tf = (screen.height - height)/2;
	var newWin = eval("page" + id + " = window.open(url,'" + id + "','toolbar=1,scrollbars=1,location=0,status=1,menubar=0,resizable=1,width=" + width +",height=" + height +",top=10,left=10');");
}

function IsNumeric(strString) {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for(i = 0; i < strString.length && blnResult == true; i++) {
      strChar = strString.charAt(i);
      if(strValidChars.indexOf(strChar) == -1) {
         blnResult = false;
      }
   }
   return blnResult;
}

function validateCStool() {
	var form = document.frmcstool;
	var total = 0;
	
	for(var i=9; i>=0; i--) {
		var el = eval("document.frmcstool.cf"+i);
		var val = el.value;
		if(!IsNumeric(val)) {
			alert("Please enter numeric values only.");
			el.focus();
			return false;
			break;
		}
		else {
			total = total+parseInt(val,10);
		}
	}
	
	if(total < 1) {
		alert("You have not entered any staff numbers for any of the levels.");
		return false;
	}
}
