var ie=document.all;
var ns6=document.getElementById&&!document.all;

function openReminderWindow() {
	rwin = window.open('email_reminder.php','eWindow','toolbar=0,status=0,scrollbars=1,resizable=1,width=480,height=300');
	rwin.focus();
}

function openFTPWindow() {
	ftpwin = window.open('ftp/open.php?lib=open','ftpo','toolbar=0,status=0,scrollbars=1,resizable=1,width=650,height=600');
	ftpwin.focus();
}

function restrFTPWindow() {
	ftpwin = window.open('ftp/open.php?lib=restr','ftpr','toolbar=0,status=0,scrollbars=1,resizable=1,width=650,height=600');
	ftpwin.focus();
}

function checkDeleteForm(frm) {
	if(checkGenericForm(frm)) {
		return confirm("Are you sure you want to delete this entry?");
	}
	return false;
}

function checkForm(frm) {
	for (var i=0;i<frm.length;i++) {
		current = frm.elements[i];
		if(current.name.indexOf("_req") > -1) {
			if(current.value.replace(/^\s+|\s+$/, '') == "" && current.type=="text") {
					alert("You need to enter text in all required fields.");
					return false;
				}
			if(current.value.replace(/^\s+|\s+$/, '') == "" && current.type=="password") {
				alert("You need to enter text in all password fields.");
				return false;
			}
			if(current.value == "#" && current.type != "text") {
				alert("You have to make a selection in all drop down menus.");
				return false;
			}
			if(current.value.replace(/^\s+|\s+$/, '') == "" && current.type=="textarea") {
				alert("You need to enter text in all required text areas.");
				return false;
			}
			if(current.name == "spam_req") {
				if(current.checked != true) {
					alert("You have not checked the verification box. Please do so and try again.");
					return false;
				}
			}
		}
	}
	return true;
}

function checkProgramForm1(frm) {
	for (var i=0;i<frm.length;i++) {
		current = frm.elements[i];

		if(current.value.replace(/^\s+|\s+$/, '') == "" && current.type=="text") {
				alert("You need to enter text in all required fields.");
				return false;
			}
		if(current.value.replace(/^\s+|\s+$/, '') == "" && current.type=="password") {
			alert("You need to enter text in all password fields.");
			return false;
		}
		if(current.value == "#" && current.type != "text") {
			alert("You have to make a selection in all drop down menus.");
			return false;
		}
		if(current.value.replace(/^\s+|\s+$/, '') == "" && current.type=="textarea") {
			alert("You need to enter text in all required text areas.");
			return false;
		}
		
	}
	return true;
}

function checkProgramName(frm) {
	if(frm.pgm_name && frm.pgm_name.value.replace(/^\s+|\s+$/, '') != "") {
		return true;
	}
	alert("You need to enter at least the name for this program.");
	return false;
}

function checkProgramForm2(frm) {
	for(i = 0; i < frm.directors.length; i++) { 
		frm.directors.options[i].selected = true; 
	}
	return checkProgramForm1(frm);
}

function checkMeetingsForm(frm) {
	if(frm.mtitle.value.replace(/^\s+|\s+$/, '') == "" || frm.cal2.value.replace(/^\s+|\s+$/, '') == "") {
			alert("You need to enter text in all required fields");
			return false;
	}
	return true;
}

function checkTipsForm(frm) {
	if(frm.nbody.value.replace(/^\s+|\s+$/, '') == "") {
			alert("You need to enter text in all required fields");
			return false;
	}
	return true;
}

function checkNewsForm(frm) {
	if(frm.ntitle.value.replace(/^\s+|\s+$/, '') == "" || frm.cal2.value.replace(/^\s+|\s+$/, '') == "") {
			alert("You need to enter text in all required fields");
			return false;
	}
	return true;
}

function checkUserForm(frm) {
	if(frm.user.value.replace(/^\s+|\s+$/, '') == "" || frm.email.value.replace(/^\s+|\s+$/, '') == "" || frm.fname.value.replace(/^\s+|\s+$/, '') == "") {
		alert("You need to enter text in all required fields");
		return false;
	}
	if(!frm.admin[0].checked && !frm.admin[1].checked) {
		alert("You need to select admin rights for this user.");
		return false;
	}
	return true;
}

function checkMembersForm(frm) {
	if(frm.email.value.replace(/^\s+|\s+$/, '') == "" || frm.fname.value.replace(/^\s+|\s+$/, '') == "") {
		alert("You need to enter text in all required fields");
		return false;
	}
	if(!frm.pub[0].checked && !frm.pub[1].checked) {
		alert("You need to select whether this member should be visible to the wide public or not.");
		return false;
	}
	return true;
}


function checkLinksForm(frm) {
	if(frm.lcat.value == "#") {
		alert("Please select a category from the drop down list");
		return false;
	}	
	if(frm.lcat.value == "newcat" && frm.newcatname.value.replace(/^\s+|\s+$/, '') == "") {
		alert("You must provide a category name for the new category.");
		return false;
	}
	if(frm.ltext.value.replace(/^\s+|\s+$/, '') == "" || frm.llink.value.replace(/^\s+|\s+$/, '') == "") {
		alert("You need to enter text in all required fields.");
		return false;
	}
	return true;
}

function checkFinderForm(frm) {
	leftvalues = false;
	rightvalues = false;
	if(frm.ziponly.value.replace(/^\s+|\s+$/, '') != "" && frm.radiusonly.value.replace(/^\s+|\s+$/, '') != "") {
		leftvalues = true;
	}
	if(frm.city.value.replace(/^\s+|\s+$/, '') != "" && frm.state.value != "#" && frm.radius.value.replace(/^\s+|\s+$/, '') != "") {
		rightvalues = true;
	}
	if(leftvalues || rightvalues) {
		return true;
	}
	alert("Please enter zip and distance on the left OR city, state and distance on the right");
	return false;
}

function checkDistance(fld) {
	if(!isNumeric(fld.value)) {
		alert("Distance can only be an integer value.");
		fld.value = "";
		fld.focus();
	}
}

function checkZipCode(fld) {
	if(fld.value.length < 5) {
		alert("Zip codes are 5 digit numbers for US and 7 letter codes for Canada.");
		fld.value = "";
		fld.focus();	
	}
}

function isNumeric(sText) {
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}


function checkGenericForm(frm) {
	message = "";
	for (var i=0;i<frm.length;i++)
	{
		current = frm.elements[i];
		if(current.name=="rightemployee" || current.name.indexOf("sel") > -1) {}
		else if(current.name == "hwebedit" || current.name == "newhweb") {}
		else if(frm.payperiod != null) {
			if(frm.payperiod.value == "#" && frm.payperiod_m.value == "#") {
				alert("You have to make a selection in all drop down menus.");
				return false;
			}
			if(frm.payperiod.value != "#" || frm.payperiod_m.value != "#") 
				return true;
		}
		else if(current.value.replace(/^\s+|\s+$/, '') == "" && current.type=="text") {
			alert("You need to enter text in all required fields.");
			return false;
		}
		else if(current.value.replace(/^\s+|\s+$/, '') == "" && current.type=="password") {
			alert("You need to enter text in all password fields.");
			return false;
		}
		else if(current.value == "#" && current.type!="text") {
			alert("You have to make a selection in all drop down menus.");
			return false;
		}
	}
	return true;
}

function showCategory(item) {
	myform = item.form;
	if(item.value == "newcat") {
		myform.newcatname.style.visibility = "visible";
		myform.addnewcat.value = "true";
	}
	else {
		myform.newcatname.style.visibility = "hidden";
		myform.addnewcat.value = "false";
	}
}

function checkInfoForm(frm) {
	mes = "";
	if(frm.rname.value.replace(/^\s+|\s+$/, '') == "") mes += "Please enter your name.\n";
	if(frm.remail.value.replace(/^\s+|\s+$/, '') == "") mes += "Please enter your email address.\n";
	if(mes != "") { alert(mes); return false; }
	return true;
}

function showUserDetails(sel) {
	document.useradd.selname.value=sel.options[sel.selectedIndex].text;
	document.useradd.selemail.value=sel.options[sel.selectedIndex].value;
	document.useradd.seluser.value=sel.options[sel.selectedIndex].id;
}

function showHospitalDetails(sel) {
	document.hospitaladd.selname.value=sel.options[sel.selectedIndex].text;
	document.hospitaladd.selweb.value=sel.options[sel.selectedIndex].value;
}

function showRemoveUserDetails(sel) {
	_name=sel.options[sel.selectedIndex].text;
	_email=sel.options[sel.selectedIndex].title;
	_user=sel.options[sel.selectedIndex].id;
	if(_name == "Select user") {
		_name = ""; _email = ""; _user = "";
	}
	document.userremove.selname.value=_name;
	document.userremove.selemail.value=_email;
	document.userremove.seluser.value=_user;
}

function showRemoveHospitalDetails(sel) {
	_name=sel.options[sel.selectedIndex].text;
	_web=sel.options[sel.selectedIndex].id;
	if(_name == "Select user") {
		_name = ""; _web = "";
	}
	document.hospitalremove.selname.value=_name;
	document.hospitalremove.selemail.value=_web;
}

function clearBut() {
	document.useradd.selname.value="";
	document.useradd.selemail.value="";
	document.useradd.seluser.value="";
}

function movetolist(from, to, tempfrom, tempto) {
  fromList = eval('document.assigndirsform.' + from);
  toList = eval('document.assigndirsform.' + to);
  if (toList.options.length > 0 && toList.options[0].value == 'temp') {
    toList.options.length = 0;
  }
  var sel = false;
  for (i=0; i < fromList.options.length; i++) {
    var current = fromList.options[i];
    if (current.selected) {
      sel = true;
      if (current.value == 'temp') {
        alert ('You cannot move this text!');
        return;
      }
      txt = current.text;
      val = current.value;
      populateTemp(tempfrom, tempto, txt, val);
      toList.options[toList.length] = new Option(txt,val);
      fromList.options[i] = null;
      i--;
    }
  }
  if (!sel) alert ('You haven\'t selected any directors!');
}

function moveSelToText(from, to) {
  fromList = eval('document.emailform.' + from);
  toText = eval('document.emailform.' + to);
  var sel = false;
  for (i=0; i < fromList.options.length; i++) {
    var current = fromList.options[i];
    if (current.selected) {
      sel = true;
      txt = current.text;
      val = current.value;
	  currenttext = toText.value;
	  if(currenttext == "") delim = "";
	  else delim = "\n";
      currenttext += delim + "'" + txt + "' " + "<" + val + ">";
      toText.value = currenttext;
      fromList.options[i] = null;
      i--;
    }
  }
  if (!sel) alert ('You haven\'t selected any employees!');
}

function moveAll(from, to) {
	_a = eval('document.emailform.' + from);
	for (a=0; a < _a.options.length; a++) {
    	_a.options[a].selected = "true";
    }
    moveSelToText(from, to);
}

function populateTemp(tmfr, tmto, intext, invalue) {
	if(tmfr == "_inact[]") {
		_from = document.getElementById('_inact');
		_to = document.getElementById('_act');
	}
	if(tmfr == "_act[]") {
		_from = document.getElementById('_act');
		_to = document.getElementById('_inact');
	}
	
	found = false;
    	for (a=0; a < _to.options.length; a++) {
    		if(_to.options[a].text == intext && _to.options[a].value == invalue) {
    			_to.options[a]=null;
    			found = true;
    		}
    	}

	if(!found) {
		_from.options[_from.length] = new Option(intext, invalue);	
	}
}

function passAll(frm) {
	if(checkGenericForm(frm)) {
		_a = document.getElementById('_act');
		for (a=0; a < _a.options.length; a++) {
    		_a.options[a].selected = "true";
    	}
    	_i = document.getElementById('_inact');
		for(i=0; i< _i.options.length; i++){ 		
    		_i.options[i].selected = "true";
    	}
    	return true;
	}
	else {
		return false;
	}
}

function checkPass(fld) {
	if(fld.value.length < 6 && fld.value.length > 0) {
		fld.value="";
		fld.focus();
		alert("Your password cannot be shorter than 6 characters.\nPlease choose another password.");
	}
}	

function checkIfOther(el) {
	if(el.value=="other") {
		el.form.addtlprog.style.visibility = "visible";
	}
	else {
		el.form.addtlprog.style.visibility = "hidden";
	}
}

function checkIfOthers(el, nm) {
	otherfld = eval('el.form.' + nm);
	if(el.value=="other") {	
		otherfld.style.visibility = "visible";
	}
	else {
		otherfld.style.visibility = "hidden";
	}
}


function ietruebody(){
	return (document.compatMode && document.compatMode!="BackCompat" && !window.opera)? document.documentElement : document.body
}

function enlarge(which, e, position, imgwidth, imgheight){
	if (ie||ns6){
		crossobj=document.getElementById? document.getElementById("showimage") : document.all.showimage
			if (position=="center"){
				pgyoffset=ns6? parseInt(pageYOffset) : parseInt(ietruebody().scrollTop)
				horzpos=ns6? pageXOffset+window.innerWidth/2-imgwidth/2 : ietruebody().scrollLeft+ietruebody().clientWidth/2-imgwidth/2
				vertpos=ns6? pgyoffset+window.innerHeight/2-imgheight/2 : pgyoffset+ietruebody().clientHeight/2-imgheight/2
				if (window.opera && window.innerHeight) //compensate for Opera toolbar
					vertpos=pgyoffset+window.innerHeight/2-imgheight/2
				vertpos=Math.max(pgyoffset, vertpos)
			}
			else{
				var horzpos=ns6? pageXOffset+e.clientX : ietruebody().scrollLeft+event.clientX
				var vertpos=ns6? pageYOffset+e.clientY : ietruebody().scrollTop+event.clientY
			}

		crossobj.style.left=horzpos+"px"
		crossobj.style.top=vertpos+"px"
		
		crossobj.innerHTML='<img src="'+which+'">'
		crossobj.style.visibility="visible"
		return false
	}
	else //if NOT IE 4+ or NS 6+, simply display image in full browser window
		return true
}

function closepreview(){
	crossobj.style.visibility="hidden"
}

function drag_drop(e){
	if (ie&&dragapproved){
		crossobj.style.left=tempx+event.clientX-offsetx+"px"
		crossobj.style.top=tempy+event.clientY-offsety+"px"
	}
	else if (ns6&&dragapproved){
		crossobj.style.left=tempx+e.clientX-offsetx+"px"
		crossobj.style.top=tempy+e.clientY-offsety+"px"
	}
	return false
}

function initializedrag(e){
	if (ie&&event.srcElement.id=="dragbar"||ns6&&e.target.id=="dragbar"){
		offsetx=ie? event.clientX : e.clientX
		offsety=ie? event.clientY : e.clientY
		
		tempx=parseInt(crossobj.style.left)
		tempy=parseInt(crossobj.style.top)
		
		dragapproved=true
		document.onmousemove=drag_drop
	}
	


}

function unselectDirectors() {
	document.forms["modifyprogform"].director.selectedIndex  = -1;
}

function removeSelected() {
	var x = document.forms["directorsendmailform"].directors;
	if(x != null && x.selectedIndex > -1) x.remove(x.selectedIndex);
}

function removeDirector() {
	var x = document.forms["modifyprogform"].directors;
	if(x != null && x.selectedIndex > -1) x.remove(x.selectedIndex);
}

function checkAll(fld) {
	sel = fld.checked;
	for (var i=0;i<fld.form.length;i++) {
		current = fld.form.elements[i];
		if(current.type=="checkbox") {
			current.checked = sel;
		}
	}
}

function imposeMaxLength(Object, MaxLen)
{
  return (Object.value.length <= MaxLen);
}