/*
function showRow(rowId)
{
	document.getElementById(rowId).style.display = 'block';
}

function hideRow(rowId)
{
	document.getElementById(rowId).style.display = 'none';
}
*/
/* for menus */
//window.onload=show("");
function show(id) {
	if (tout != 0)
		window.clearTimeout(tout);
	var d = document.getElementById(id);
	for (var i = 1; i<=10; i++) {
		if (document.getElementById('smenu'+i)) {document.getElementById('smenu'+i).style.display='none';}
	}
if (d) {d.style.display='block';}
	if (id != ''){
//		alert("id = {" + id + "}");
	 hideSelects();
	 }
}

var tout = 0;

function timedClear()
{
	if (tout != 0)
		window.clearTimeout(tout);
		
	tout = window.setTimeout("show('');showSelects();", 900);
}


function hideSelects()
{
	if (document.all)
	{
		var aSelects = document.body.getElementsByTagName("SELECT");
		for (i = 0; i < aSelects.length; i++)
		{
			aSelects[i].style.visibility = "hidden";
		}
	}
}

function showSelects()
{
	if (document.all)
	{
		var aSelects = document.body.getElementsByTagName("SELECT");
		for (i = 0; i < aSelects.length; i++)
		{
			aSelects[i].style.visibility = "visible";
		}
	}
}

function showRow(rowId)
{
	document.getElementById(rowId).className = 'nothidden';
}

function hideRow(rowId)
{
	document.getElementById(rowId).className = 'hidden';
}


function checkEmail(str){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		testresults=true
	else
	{
		testresults=false
	}
	return (testresults)
}

function getCheckValue(cbxHandle)
{
	var c_value = "";
	for (var i=0; i < cbxHandle.length; i++)
	{
		if (cbxHandle[i].checked)
		{
			c_value = c_value + cbxHandle[i].value + ",";
		}
	}
	return c_value;
}

function getRadioValue(handle)
{
	for (var i=0; i < handle.length; i++)
	{
		if (handle[i].checked)
		{
			//c_value = c_value + cbxHandle[i].value + ",";
			return handle[i].value;
		}
	}

}

function goTo(url)
{
	window.location = url;
}

function isDate(dateVal)
{
	var re = /^(()|(\d{1,2}(\/|-)\d{1,2}(\/|-)\d{2,4}))$/
	if (re.test(dateVal))
		return true;
	else return false;
}

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(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
}

function checkDateBetter(obj){
	var dtStr = obj.value;
	var daysInMonth = DaysArray(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){
		alert("Please enter the date as: mm/dd/yyyy");
		obj.value = '';
		obj.focus();
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid date")
		obj.value = '';
		obj.focus();
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid date")
		obj.value = '';
		obj.focus();
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		obj.value = '';
		obj.focus()
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		obj.value = '';
		obj.focus()
		return false
	}
return true
}

function checkDate(obj)
{
	var result = isDate(obj.value);
	if (result)
		return true;
	else
	{
		alert("Please enter a valid date");
		obj.value = '';
		obj.focus();
		return false;
	}
}


function updateCB(input_name)
{
	// obviously this is crippled in that it depends on the form being named theForm
	cbElement = eval("document.theForm." + input_name + "CB");
	realElement = eval("document.theForm." + input_name);
	if (cbElement.checked)
		realElement.value = 1;
	else
		realElement.value = 0;
		
}

function isTime(inputVal)
{
  return /^([01]?[0-9]|[2][0-3])(:[0-5][0-9])?$/.test(inputVal)
}

function checkTime(obj)
{
	var result = isTime(obj.value);
	if (result)
		return true;
	else
	{
		alert("Please enter a valid time in 24 hour format with no a.m. or p.m., for example 4:30 P.M. would be entered 16:30");
		obj.focus();
		return false;
	}
}


function popPoster(eposter_id)
{
	var w = window.open('eposter_view.cfm?eposter_id=' + eposter_id, 'eposterwin','width=750,height=1000,resizable=no,toolbar=no');
	w.focus();
}

function printMe()
{
	// appropriated from another web site
	if (window.print) {
		setTimeout('window.print();', 333);
	}
	else if (agt.indexOf("mac") != -1) {
		alert("Press 'Cmd+p' on your keyboard to print page.");
	}
	else {
		alert("Press 'Ctrl+p' on your keyboard to print page.")
	}
}

function setRadio(handle, val)
{
	for (var i=0; i < handle.length; i++)
	{
		if (handle[i].value == val)
		{
			handle[i].checked = true;
		}
	}

}

function pop(url, width, height)
{
	var w = window.open(url, 'popWin','width=' + width + ',height=' + height + ',resizable=no,toolbar=no,status=yes');
	w.focus();

}

function charsLeft(inputId, displayId, maxChars)
{
	var t = document.getElementById(inputId);
	var n = t.value.length;
	var r = parseInt(maxChars) - n; // -1 because it's on keypress not onkeyup
	var displayId = document.getElementById(displayId);
	
	if (r <= 0)
	{
		t.value = t.value.substr(0, maxChars - 1);
		//alert("You have entered as much text (" + maxChars + " characters) as the field will allow.");
		return false;
	}
	else
	{
		if (r == 1) r = 0; // pretty silly.
		displayId.innerHTML = r  + " characters remaining";
		return true;
	}
}

function areYouSureGeneric(url)
{
	if (window.confirm("Are you sure you wish to perform the selected action?\n(Once completed, the action cannot be undone.)"))
		window.location = url;
}

function popJobMessage(job_id)
{
	var url = "Pop_Message.cfm?job_id=" + job_id;
	pop(url, 450, 425);
}

function popJobMessageGeneric(linktext)
{
	var url = "Pop_Message.cfm?message_text=" + linktext;
	pop(url, 450, 425);
}

function checkFloat(obj)
{
	var n = obj.value;
	
	if (isNumeric(n))
		return true;
	else
	{
		alert("Please enter a valid number");
		obj.value = '';
		obj.focus();
	}
}

function isNumeric(x) {
     var fr = new RegExp("^[0-9]+\.[0-9]+$");
     var ir = new RegExp("^[0-9]+$");

     if (fr.exec(x) != null) {
//          alert("Float!");
		  return true;
     } else if (ir.exec(x) != null) {
//          alert("Integer!");
		  return true;
     } else {
//          alert("Invalid number!");
		  return false;
     }
}

function checkInt(obj)
{
	var ir = new RegExp("^[0-9]+$");
	if (ir.exec(obj.value) != null)
	{
		return true;
	}
	else
	{
		alert("Please enter a valid integer (no decimals)");
		obj.value = '';
		obj.focus();
	}
		 
}

function checkBox(id) 
    { 
	inputObj = document.getElementById(id);
	imageObj = document.getElementById('imgCheck' + id);
	
//	alert(inputObj.value);
    if(inputObj.value == 0) //if a value is not true, use this rather than == false, 'cos the first time no value will be set and it will be undefined, not true or false 
        { 
        imageObj.src = "http://commprojects.jhsph.edu/communications/images/true.png"; //change the image 
        inputObj.value = 1; //change the field value 
        } 
    else 
        { 
        imageObj.src = "http://commprojects.jhsph.edu/communications/images/false.png"; 
        inputObj.value = 0; 
        } 
    }

