//**********************************************************************************
//form validation stuff (helper functions and vars)
//**********************************************************************************
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function sendToClipboard(str)
{
	if	(!!window.clipboardData)
	{
		window.clipboardData.setData("Text", str);
	}
	else
	{
		var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
		gClipboardHelper.copyString(str);
	}
}	

function hlpIsAnInt(s)
{
  var i;
  for (i=0; i<s.length; i++)
  {   
    var c = s.charAt(i);
    if (((c < "0") || (c > "9"))) return false;
  }
  return true;
}

function hlpstripCharsInBag(s, bag)
{
  var i;
  var returnString = "";
  for (i=0; i<s.length; i++)
  {   
    var c = s.charAt(i);
    if (bag.indexOf(c) == -1) returnString += c;
  }
  return returnString;
}

function hlpdaysInFebruary (year)
{
  return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function hlpDaysArray(n)
{
  for (var i=1; i<=n; i++)
  {
    this[i] = 31
	if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
	if (i==2) {this[i] = 29}
  } 
  return this
}

//**********************************************************************************
//form validation stuff
//**********************************************************************************
function isTime(frmField)
{
  return (frmField.value.search(/^[0-1]?[0-9]:[0-5]?[0-9]\s?[AaPp][Mm]?$/) != -1);
}

function isTimeOrBlank(frmField)
{
  if (frmField.value.length == 0) return true;
  return (frmField.value.search(/^[0-1]?[0-9]:[0-5]?[0-9]\s?[AaPp][Mm]?$/) != -1);
}
 
function isDate(frmField)
{
  var dtStr = frmField.value;
  var daysInMonth = hlpDaysArray(12)
  var pos1=dtStr.indexOf(dtCh)
  var pos2=dtStr.indexOf(dtCh,pos1+1)
  var strMonth=dtStr.substring(0,pos1)
  var strDay=dtStr.substring(pos1+1,pos2)
  var strYear=dtStr.substring(pos2+1)
  strYr=strYear
  if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
  if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
  for (var i=1; i<=3; i++)
  {
    if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
  }
  month=parseInt(strMonth)
  day=parseInt(strDay)
  year=parseInt(strYr)
  if (pos1==-1 || pos2==-1)
  {
    return false
  }
  if (strMonth.length<1 || month<1 || month>12)
  {
    return false
  }
  if (strDay.length<1 || day<1 || day>31 || (month==2 && day>hlpdaysInFebruary(year)) || day > daysInMonth[month])
  {
    return false
  }
  if (strYear.length != 4 || year==0 || year<minYear || year>maxYear)
  {
    return false
  }
  if (dtStr.indexOf(dtCh,pos2+1)!=-1 || hlpIsAnInt(hlpstripCharsInBag(dtStr, dtCh))==false)
  {
    return false
  }
  return true
}

function isNotBlank(frmField)
{
  return !((frmField.value.length==0) || (frmField.value==null));
}	

function isCurrency(frmField)
{
  tmpval = frmField.value.toString().replace(/\$|\,/g,'');
  return !(isNaN(tmpval));
}

function isInteger(frmField)
{
  return !(isNaN(parseInt(frmField.value))); 
}

function isIntegerOrBlank(frmField)
{
  if (frmField.value.length == 0) return true;
  return !(isNaN(frmField.value)); 
}

function isEmail(frmField)
{
  return (frmField.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}

function isEmailOrBlank(frmField)
{
  if (frmField.value.length == 0) return true;
  return (frmField.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
}

function isSelected(frmField)
{
  var flag = false;
  if (frmField.type == "select-one") //1st option is assumed to be a null selection by this code
  {
    return (frmField.selectedIndex != 0);
  }	
  else if (frmField.type == "select-multiple") //assumes none of the options are null selections	
  {
    for (i=0;((i<frmField.options.length) && (flag==false));i++)
	{
	  flag = frmField.options[i].selected;
	}
	return flag;
  }
  else //must be a radio group
  {
    for (i=0;i<frmField.length;i++)
	{
	  flag = frmField[i].checked;
	}
	return flag;
  }
}

function isSelectedAndNotBlank(frmField) //only for select boxes
{
  if (frmField.type == "select-one")
    return (frmField.options[frmField.selectedIndex].value != "")
  else
    return false;
}

function isPasswordMatched(frmField1,frmField2)
{
  return (frmField1.value == frmField2.value);
}

function doFormError(msg)
{
  if (msg.length != 0)
  {
    msg = ReplaceString(msg,"!","          * ");
	alert("The following problems were found in your submission:\n\n" + msg);
	return false;    
  }
  return true;  
}

//**********************************************************************************
//generic functions
//**********************************************************************************
function setStatus(txt)
{
  window.status = txt;
  return true;
}

function repeatString(str,cnt)
{
  var res = ""
  for (i=0;i<cnt;i++)
    res += str;
  return res;
}

function ReplaceString(txt,srch,rep)
{
  var p;
  do
  {
    p=txt.indexOf(srch);
    txt=txt.substr(0,p)+rep+txt.substr(p+srch.length,txt.length);
  }
  while(txt.indexOf(srch)!=-1);
  return(txt);
}


//**********************************************************************************
//cascading menus
//**********************************************************************************
var tID = null;
var mainMenu = null;

function executeTimer() {
	hideMenus();
}

function resetTimer() {
	clearTimeout(tID);
	tID = setTimeout("executeTimer()",1000);
}

function getPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
		if (!document.all) {
			curleft += -1;
			curtop += -1;
		}
	}
	return [curleft,curtop];
}

function showSubMenu(id) {
	var menuItem, subMenu, iframe = null;
  clearTimeout(tID);
	if (mainMenu) {
		hideMenus();
		if (id && id.length) {
			menuItem = document.getElementById(id);
			if (menuItem) {
				subMenu = document.getElementById("s" + id);
				if (subMenu) {
					subMenu.style.visibility = "visible";
					subMenu.style.top = getPos(menuItem)[1] + "px";
					subMenu.style.left = (getPos(menuItem)[0] + menuItem.offsetWidth + 4) + "px";
					iframe = document.getElementById(subMenu.id + "iframe");
					if (!iframe && !!document.createElement && !!document.appendChild) {
						iframe = document.createElement("iframe");
						iframe.id = subMenu.id + "iframe";
						if (!!document.all) {iframe.frameBorder = 0;}
						iframe.style.position = "absolute";
						iframe.style.zIndex = "-1";
						iframe.style.display = "block";
						iframe.style.border = "0px";
						iframe.style.top = subMenu.style.top;
						iframe.style.left = subMenu.style.left;
						iframe.style.width = subMenu.offsetWidth + "px";
						iframe.style.height = subMenu.offsetHeight + "px";
						iframe.style.visibility = "visible";
						document.getElementsByTagName("body")[0].appendChild(iframe);
					}
				}
			}
		}
	}
}
   
function hideMenus() {
	var i, menuItem, subMenu, iframe = null;
	if (mainMenu) {
		for (i=0; i<mainMenu.childNodes.length; i++) {
			menuItem = mainMenu.childNodes[i];
			if (menuItem.nodeType != 3) {
				if (menuItem.id && menuItem.id.length) {
					subMenu = document.getElementById("s" + menuItem.id);
					if (subMenu) {
						subMenu.style.visibility = "hidden";
						iframe = document.getElementById(subMenu.id + "iframe");
						if (iframe) {iframe.style.display = "none";}
					}	
				}	
			}
    }
  }
}

function initMenu() {
	var i, body, menuItem, subMenu, item, menuItem = null;
	if (!document.getElementById && !document.childNodes) {return;}
	body = document.getElementsByTagName("body")[0];
	body.onclick = hideMenus;
	mainMenu = document.getElementById("mainmenu");
	if (mainMenu) {
		for (i=0; i<mainMenu.childNodes.length; i++) {
			menuItem = mainMenu.childNodes[i];
			if (menuItem.nodeType != 3) {
				menuItem.onmouseover = hideMenus;
				if (menuItem.id && menuItem.id.length) {
					subMenu = document.getElementById("s" + menuItem.id);
					if (subMenu) {
						menuItem.onmouseout = resetTimer;
						menuItem.onmouseover = new Function("showSubMenu(\"" + menuItem.id + "\")");
						subMenu.onmouseout = resetTimer;
						subMenu.style.visibility = "hidden";
					}
				}	
			}		
    }
  }
}  	

window.onload = initMenu;