function NewWindow(mypage, myname, w, h, scroll) 
	{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+'yes'+',resizable'
//	alert("winprops= " + winprops);
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) 
		{ 
		win.window.focus(); 
		}
	}

function OpenFullWindow(mypage) 
	{
	window.open(mypage, '', 'fullscreen=yes, scrollbars=auto');
	}

function PrintWindow(mypage, myname, w, h) 
	{
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops =
'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+'yes'+',menubar='+'yes'+',toolbar='+'yes'+',resizable' 
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) 
		{ 
		win.window.focus(); 
		}
	}

function closeAndRefresh() 
	{
    window.opener.location.reload();
    close();
	}

/* SetFrmVal will set the value for the FormName.FrmField to FrmFieldVal
*/
function SFrmVal(FormName,FrmField,FrmFieldVal)
	{
	FormName.UseList.value = 'No';
	}

function SubmitFrm(FormName)
	{
//var ThisFormName = eval("document.forms." + FormName);
	FormName.submit();
	}

/* Usage: ShowHideBlock(Blockname to show, state to enable)
ShowHideBlock(TOBlockOn,'none')
*/
function ShowHideBlock(BlockToShow,blockstate)
	{
	if (blockstate == "none")
		{
		BlockToShow.style.display = 'none';
		}
	else
		{
		BlockToShow.style.display = 'block';
		}
	}

/*
checkall will check all checkboxes, or uncheck them, depending on what is passed as thestate
javascript:checkall('Search_Form','SiteAreas',true) will check all
javascript:checkall('Search_Form','SiteAreas',false) will uncheck
*/
function checkall(formname,checkname,thestate)
	{
	var el_collection=eval("document.forms."+formname+"."+checkname)
	for (c=0;c<el_collection.length;c++)
		{
		el_collection[c].checked=thestate;
		}
	}

/*
uncheckbox will uncheck the specified form.checkbox
javascript:uncheckbox('Search_Form','CheckBoxName')
*/
function uncheckbox(formname,targ)
	{
	var targ_to_uncheck = eval("document.forms."+formname+"."+targ);
	targ_to_uncheck.checked = false;
	return false ;
	}

/* 
clearSelect is to deselect all items in a select box 
javascript:clearSelect('Search_Form','SelectBoxName')
*/
function clearSelect(formname,targ) 
	{
	var currentForm = eval("document.forms."+formname);
	var targ_selects = eval("document.forms."+formname+"."+targ);
	targ_selects.selectedIndex = -1;
	}

/* 
textCounter counts the number of characters in the passed form field, and shows in an alert popup the passed error message if the count exceeds the passed max. The field value is truncated at the maxlimit. Typically used in a textarea or text input in the onchange event.
onchange="textCounter(this.form.FieldName,150,'You entered too many characters for FieldName! The value will be truncated at 150 characters.')
*/
function textCounter(field, maxlimit,errmsg,fielddisp) 
	{
	if (typeof(fielddisp) != 'undefined')
		{
		WordCount=field.value.length;
		fielddisp.value=WordCount;
		}

	if (field.value.length > maxlimit) 
		{
		alert(errmsg);
		field.value = field.value.substring(0, maxlimit);
		if (typeof(fielddisp) != 'undefined')
			{
			fielddisp.value=maxlimit;
			}
		}
	}

/* 
WordCounter is similar to textCounter, except it counts the number of words in the passed form field and displays the current count, realtime, in the passed fielddisp which is typically an input box next to the textarea. It shows in an alert popup the passed error message if the count exceeds the passed max. The field value is truncated at the maxlimit. Typically used in a textarea input in the onkeyup event.
onkeyup="WordCounter(this.form.FieldName, 120, 'You entered too many words for FieldName. The maximum is 120. The value has been truncated at the 120th word.',this.form.FieldName_disp)
*/
function WordCounter(field, maxlimit,errmsg,fielddisp) 
	{
	Str=field.value;
	// alert('Str = ' + Str);
	WordArray=Str.split(" ");
	WordCount=WordArray.length;
	fielddisp.value=WordCount;
	if (WordCount > maxlimit) 
		{
		Wval = WordArray[maxlimit];
		Wvalslice = WordArray.slice(0,maxlimit);
		WvalsliceCount = Wvalslice.length;
		WvalsliceJoin = Wvalslice.join(" ");
		alert(errmsg);
		field.value = WvalsliceJoin;
		fielddisp.value= maxlimit;
		}
	}

/* 
checkEmailAddress will validate the passed form field value against the regular expression below. If it doesn't pass, it alerts with the passed msg if it is defined or the default error, focuses back on the calling document with the form field selected.
*/
function checkEmailAddress(field,msg) 
	{
// Note: The next expression must be all on one line...
//       allow no spaces, linefeeds, or carriage returns!
	var goodEmail = field.value.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);

	if (!goodEmail)
		{
		if (typeof(msg) != 'undefined')
			{
		   alert('' + msg);
	   		}
		else
			{
			alert('The email address you entered is not valid. Please enter a valid email address.');
			}

		self.focus();
		field.select();
		field.focus();
	   }
	}

// -------------------------------------------------------------------
// autoComplete (text_input, select_input, ["text"|"value"], [true|false])
//   Use this function when you have a SELECT box of values and a text
//   input box with a fill-in value. Often, onChange of the SELECT box
//   will fill in the selected value into the text input (working like
//   a Windows combo box). Using this function, typing into the text
//   box will auto-select the best match in the SELECT box and do
//   auto-complete in supported browsers.
//   Arguments:
//      field = text input field object
//      select = select list object containing valid values
//      property = either "text" or "value". This chooses which of the
//                 SELECT properties gets filled into the text box -
//                 the 'value' or 'text' of the selected option
//      forcematch = true or false. Set to 'true' to not allow any text
//                 in the text box that does not match an option. Only
//                 supported in IE (possible future Netscape).
// -------------------------------------------------------------------
function autoComplete (field, select, property, forcematch) 
	{
	var found = false;
	for (var i = 0; i < select.options.length; i++) 
		{
		if (select.options[i][property].toUpperCase().indexOf(field.value.toUpperCase()) == 0) 
			{
			found=true; break;
			}
		}

		if (found)
			{ 
			select.selectedIndex = i; 
			}
		else 
			{ 
			select.selectedIndex = -1; 
			}

		if (field.createTextRange) 
			{
			if (forcematch && !found) 
				{
				field.value=field.value.substring(0,field.value.length-1); 
				return;
				}

		var cursorKeys ="8;46;37;38;39;40;33;34;35;36;45;";
		if (cursorKeys.indexOf(event.keyCode+";") == -1) 
			{
			var r1 = field.createTextRange();
			var oldValue = r1.text;
			var newValue = found ? select.options[i][property] : oldValue;
			if (newValue != field.value) 
				{
				field.value = newValue;
				var rNew = field.createTextRange();
				rNew.moveStart('character', oldValue.length) ;
				rNew.select();
				}
			}
		}
	}
/* 
writeSelected will copy the text of the selected option(s) from the passed selectbox obj to the form element passed as targ. If additional 'yes' arg is passed, the script will also write the selectbox value to a form element with the same base name as targ with _val appended.
onchange="writeSelected('MainFrm','Selectbox','inputbox')
onchange="writeSelected('MainFrm','Selectbox','inputbox','yes')
*/
function writeSelected(frm,obj,targ,writeval)
	{
	var ChosenText = "";
	var ChosenTextVal = "";
	var selectObj = eval("document.forms."+frm+"."+obj);
	var descObj = eval("document.forms."+frm+"."+targ);
	if (writeval == 'yes')
		{
		var descObjval = eval("document.forms."+frm+"."+targ + "_val");
		}	
		if (selectObj.type == "select-multiple")
		{
		for (var i=0; i < selectObj.options.length; i++ )
			{
			if (selectObj.options[i].selected) 
				{
				ChosenText = ChosenText + selectObj.options[i].text+",";
				ChosenTextVal = ChosenTextVal + selectObj.options[i].value+",";
				}
			}
		descObj.value = ChosenText;
		if (writeval == 'yes')
			{
			descObjval.value = ChosenTextVal;
			}
		}
	else
		{
		if (selectObj.type == "select-one")
			{
	for (var i=0; i < selectObj.options.length; i++ )
				{
		if (selectObj.options[i].selected) 
					{
					ChosenText  = ChosenText + selectObj.options[i].text;
					ChosenTextVal = ChosenTextVal + selectObj.options[i].value;
					}
				}
			descObj.value = ChosenText;
		if (writeval == 'yes')
				{
				descObjval.value = ChosenTextVal;
				}
			}
		}
	}

function padzero(frm,obj)
	{
//function used for padding zeros to the project id,to make it 7 characters
	var usr;
	var len;
	var zeros = new Array("0","00","000","0000","00000","000000");
	var fobj=eval("document.forms."+frm+"."+obj)
	alert("fobj = " + fobj);
	usr = fobj.value;
	len = usr.length;

if (len < 7)
	{
	len = 7-len-1;
	fobj.value = zeros[len] + fobj.value;
	}
}

function trim(str)
	{
	return( (""+str).replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') );
	}

function checkObj()
	{
	//alert(typeof(obj));
	var obj = eval("document.forms.frm1");
	alert(typeof(obj));
	if(typeof(obj)!='undefined')
		{
		alert('Dude exists');
		}
	else{
		alert('Dude DOES NOT exist');
		}
	}

function checkMandatoryFields(fieldsArr, labelsArr)
	{
	var ErrorStr="";
	for(var i=0;i<fieldsArr.length;i++)
		{
		var type = eval("document.all."+fieldsArr[i]+".type")
		if(type=="text" ||type=="file" ||type=="textarea")
			{
			var len = eval("trim(document.all."+fieldsArr[i]+".value).length")
			if(!len)
				{
				ErrorStr=ErrorStr+"Please Enter "+labelsArr[i] +"\n";
				}
			}
		else if (type=="select-one")
			{
			var len = eval("document.all."+fieldsArr[i]+".value")
			if(len==0)
				{
				ErrorStr=ErrorStr+"Please Select "+labelsArr[i] +"\n";
				}
			}
		else
			{
//				ErrorStr=ErrorStr+"Please choose "+labelsArr[i] +"\n";
			}
		}
	return 	ErrorStr;		
	}

/* ConfirmDelete is to prompt the user to click OK or Cancel, about deleting a record.
need to improve validating that frm is legit
usage: <input type="button" name="Operation" value="Delete" onclick="ConfirmDelete('MainFrm','Are you etc.')"
MLK 10/19/05
*/
function ConfirmDelete(frm, msg)
	{
	var ThisFormName = eval("document.forms." + frm);

	alert('ThisFormName = ' + ThisFormName);
	var Zmsg = "Are you certain you want to delete this record? Click the Cancel button to return to the form, or the OK button to proceed with the delete.";
	if (msg.length)
		{
		Zmsg = msg;
		}
	if (confirm(Zmsg))
		{
		ThisFormName.submit();
		return true;
		}
	else
		{
		return false;
		}
	}
function selectFinal(formname, targ) {
 var finalObj = eval("document.forms."+formname+"."+targ);
	for (i=finalObj.length-1; i>=0; i--) {
finalObj.options[i].selected = true;
	}
}
function moveVals(n, formname, obj, targ) {
 var fromObj = eval("document.forms."+formname+"."+obj);
 var to = eval("document.forms."+formname+"."+targ);
	if (n == 1 || n == 2) {
		var indTo = to.length-1;
		for (i=fromObj.length-1; i>=0; i--) {
			if (n==1 || fromObj.options[i].selected) {
				indTo++;
				to.options[indTo] = new Option(fromObj.options[i].text, fromObj.options[i].value);
				fromObj.options[i] = null;
			}
		}
	} else if (n == 3 || n == 4) {
		var indFrom = fromObj.length-1;
		for (i=to.length-1; i>=0; i--) {
			if (n==4 || to.options[i].selected) {
				indFrom++;
				fromObj.options[indFrom] = new Option(to.options[i].text, to.options[i].value);
				to.options[i] = null;
			}
		}
	}
}
function ResetInput1()
{
input1.value = '';
}
