// Load a new page in the current window
function redirect(url) {
	window.location.href = url;
}

function confirmRedirect(dispMessage,url) {
	input_box=confirm(dispMessage);
	if (input_box) {
		redirect(url);
	}
}

function checkboxAlert(checkboxField,bStatus,dispMessage) {
	if (checkboxField.checked == bStatus) {
		alert(dispMessage);
	}
}

function checkboxToggle(checkboxField) {
	if (checkboxField.checked == true)
		checkboxField.checked = false;
	else
		checkboxField.checked = true;
}

function toggleLayer(whichLayer) {
	var elem, vis;
	if (document.getElementById) // this is the way the standards work    
		elem = document.getElementById(whichLayer);
	else if (document.all) // this is the way old msie versions work      
		elem = document.all[whichLayer];
	else if (document.layers) // this is the way nn4 works    
		elem = document.layers[whichLayer];
		vis = elem.style;
	// if the style.display value is blank we try to figure it out here  
	if (vis.display==''&&elem.offsetWidth!=undefined&&elem.offsetHeight!=undefined)
		vis.display = (elem.offsetWidth!=0&&elem.offsetHeight!=0)?'block':'none';
	vis.display = (vis.display==''||vis.display=='block')?'none':'block';
}

//make text in form field Title Case
function titleCase(formField) { 
	var tmpStr, tmpChar, preString, postString, strlen; 
	tmpStr = formField.value.toLowerCase(); 
	stringLen = tmpStr.length; 
	if (stringLen > 0) { 
	  for (i = 0; i < stringLen; i++) { 
	    if (i == 0) { 
	      tmpChar = tmpStr.substring(0,1).toUpperCase(); 
	      postString = tmpStr.substring(1,stringLen); 
	      tmpStr = tmpChar + postString; 
	    } else { 
	      tmpChar = tmpStr.substring(i,i+1); 
	      if (tmpChar == " " && i < (stringLen-1)) { 
		      tmpChar = tmpStr.substring(i+1,i+2).toUpperCase(); 
		      preString = tmpStr.substring(0,i+1); 
		      postString = tmpStr.substring(i+2,stringLen); 
		      tmpStr = preString + tmpChar + postString; 
	      } 
	    } 
	  } 
	} 
	formField.value = tmpStr; 
} 
		
// Check the length of a textarea field and warn user if they've entered greater than a given number of characters.
function checkTextareaLength(field,maxLength,fieldDesc) {
	if (field.value.length > maxLength) {
		alert('Warning. "' + fieldDesc + '" can accept a maximum of ' + maxLength + ' characters. It currently contains ' + field.value.length + ' characters and must be revised before proceding.');
		field.focus();
		field.select();
	}
}

// Format today's date ("Sunday, January 1, 2000")
function formatToday() {
	var today = new Date();
	var day = today.getDate();
	var dayofweek = today.getDay();
	var month = today.getMonth();
	var year = formatYear(today.getYear());
	var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
	var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
	var formatToday = days[dayofweek] + ', ' + months[month] + ' ' + day + ', ' + year;
	
	return formatToday;
}

// Standardize year given browser differences
function formatYear(year) {
	return (year < 1000) ? year + 1900 : year;
}

// Validate Date
function validateDate(pDate) {	// assumes date is in format mdy
	var month,day,year,leap=false;
	if (pDate.length == 0) return true;
	month = getDatePart(pDate,'m');
	day = getDatePart(pDate,'d');
	year = getDatePart(pDate,'y');
	if (isNaN(month) || isNaN(day) || isNaN(year)) return false; // date parts include non-numerics
	if (year.length == 2) year = '20'+year;	// assume 2-digit years are 20xx
	if ((month < 1 || month > 12) || (day < 1 || day > 31) || (year.length != 4 || year < 1900)) return false;
	if ((day == 31) && ((month == 4) || (month == 6) || (month == 9) || (month == 11))) return false;
	// February validation
	if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) leap = true;
	if ((month == 2) && ((leap && day > 29) || (!leap && day > 28))) return false;
	return true;
}

// Compare Two Dates:	return -1 if first date is before second, 0 if same, 1 if first is after second;
// 										precision can be "y", "m", or "d" (default).
function dateCompare(date1,date2,precision) {	// assumes dates are in format mdy
	var month1,day1,year1,month2,day2,year2;
	month1 = parseInt(getDatePart(date1,'m'));
	day1 = parseInt(getDatePart(date1,'d'));
	year1 = parseInt(getDatePart(date1,'y'));
	month2 = parseInt(getDatePart(date2,'m'));
	day2 = parseInt(getDatePart(date2,'d'));
	year2 = parseInt(getDatePart(date2,'y'));
	if (precision == 'y') {
		if (year1 < year2) return -1;
		else if (year == year2) return 0;
		else return 1;
	} else if (precision == 'm') {
		if (year1 < year2) return -1;
		else if (year1 > year2) return 1;
		else if (month1 < month2) return -1;
		else if (month1 == month2) return 0;
		else return 1;
	} else {
		if (year1 < year2) return -1;
		else if (year1 > year2) return 1;
		else if (month1 < month2) return -1;
		else if (month1 > month2) return 1;
		else if (day1 < day2) return -1;
		else if (day1 == day2) return 0;
		else return 1;
	}
}

// Return Date Part
function getDatePart(pDate,pPart) {	// assumes date is in format mdy
	var p,q,separator;
	if (pDate.indexOf('/')>0) separator = '/';
	else if (pDate.indexOf('-')>0) separator = '-';
	else if (pDate.indexOf('.')>0) separator = '.';
	else return false;	// don't recognize date part separators
	p = pDate.indexOf(separator);
	q = pDate.substring(p+1).indexOf(separator);
	if (pPart == 'm') return pDate.substring(0,p);
	else if (pPart == 'd') return pDate.substring(p+1,p+q+1);
	else if (pPart == 'y') return pDate.substring(p+q+2);
	else return false;	// invalid date part requested
}

// Popup Calendar screen.availHeight screen.availWidth
function popCal(field,evt) {
	var month,year,width,height,left,top,prp='',qStr='';
	// Set popup window size
	width = 175;
	height = 175;
	// Make sure if they already have a date, it's valid
	if (!validateDate(field.value)) {
		alert(field.value+' is an invalid date (mm\/dd\/yyyy).');
		return false;
	}
	// Set popup window month/year to existing, or now() by default
	month = getDatePart(field.value,'m');
	year = getDatePart(field.value,'y');
	if (month) qStr += 'month='+month+'&';
	if (year) qStr += 'year='+year+'&';
	qStr += 'field='+field.name;
	// Set window properties
	if (evt != null) {
		if ((evt.screenX+10+width) < screen.availWidth) prp += 'left='+(evt.screenX+10)+',';
		else prp += 'left='+(evt.screenX-10-width)+',';
		if ((evt.screenY+height) < screen.availHeight) prp += 'top='+(evt.screenY)+',';
		else prp += 'top='+(evt.screenY-10-height)+',';
	}
	prp += 'scrollbars=no,resizable=no,width='+width+',height='+height;
	popWin = window.open('\/calendar.asp?'+qStr,'popCal',prp);
	popWin.focus();
}

// Popup Notes Edit Window
function popNotes(field,id,len,evt) {
	var width,height,left,top,prp='';
	// Set popup window size
	width = 400;
	height = 310;
	// Set window properties
	if (evt != null) {
		if ((evt.screenX+10+width) < screen.availWidth) prp += 'left='+(evt.screenX+10)+',';
		else prp += 'left='+(evt.screenX-10-width)+',';
		if ((evt.screenY+height) < screen.availHeight) prp += 'top='+(evt.screenY)+',';
		else prp += 'top='+(evt.screenY-10-height)+',';
	}
	prp += 'scrollbars=no,resizable=no,width='+width+',height='+height;
  popWin = window.open('','popNotes'+id,prp);
  if (popWin.document.forms[0] == null) {
    var page='';
    page += '<html><head><title>Editor</title><script language="JavaScript">\n';
		page += '<!-- \n';
		page += 'function checkForm() {';
		page += 'if ((' + len + ' != -1) && (document.edit.notes.value.length > ' + len + ')) alert(\'The maximum number of characters allowed is ' + len + '. The string currently contains \' + document.edit.notes.value.length + \' characters.\');';
		page += 'else {';
		page += 'window.opener.replaceNotes(\''+field.name+'\',document.edit.notes.value);';
		page += 'window.close();';
		page += '}} \n\\ -->\n';
		page += '</script></head><body>';
		page += '</head><body>';
    page += '<form name="edit" onsubmit="return false;">';
    page += '<textarea name="notes" rows="15" cols="40">';
    page += field.value;
    page += '</textarea><br>';
    page += '<input name="Submit" type="Submit" value="Save" onclick="checkForm();">&nbsp;&nbsp;';
    page += '<input name="Cancel" type="Submit" value="Cancel" onclick="window.close();">';
    page += '</form></body></html>';
    popWin.document.write(page);
  }
  popWin.document.edit.notes.focus();
}

// Takes output from Popup Note Edit Window and inserts in appropriate field
function replaceNotes(fieldName,text) {
  field = MM_findObj(fieldName);
  field.value = text;
  field.onchange();
}

// Set hidden updated-record flag to true
function updRec(fieldName) {
  field = MM_findObj(fieldName);
  field.value = 'true';
}

// Enable/disable add-record fields
// Arguments:  list of fields that are affected followed by boolean indicating current state of checkbox
function enableAddRec() {
  var args=enableAddRec.arguments;
  for (i=0; i<(args.length-1); i++) {
    obj=MM_findObj(args[i]);
    if (obj) {
      obj.disabled = !(args[args.length-1]);
    }
  }
}

function addToFavorites(url,name) {
	if (window.external) window.external.AddFavorite(url,name);
	else alert("Sorry! Your browser doesn't support this function.");
}

function capWords(formField) { 
	var tmpStr, tmpChar, preString, postString, strlen; 
	tmpStr = formField.value.toLowerCase(); 
	stringLen = tmpStr.length; 
	if (stringLen > 0) { 
	  for (i = 0; i < stringLen; i++) { 
	    if (i == 0) { 
	      tmpChar = tmpStr.substring(0,1).toUpperCase(); 
	      postString = tmpStr.substring(1,stringLen); 
	      tmpStr = tmpChar + postString; 
	    } else { 
	      tmpChar = tmpStr.substring(i,i+1); 
	      if (tmpChar == " " && i < (stringLen-1)) { 
		      tmpChar = tmpStr.substring(i+1,i+2).toUpperCase(); 
		      preString = tmpStr.substring(0,i+1); 
		      postString = tmpStr.substring(i+2,stringLen); 
		      tmpStr = preString + tmpChar + postString; 
	      } 
	    } 
	  } 
	} 
	formField.value = tmpStr; 
} 


// Macromedia functions
function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_validateForm() { //v4.0
	  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
		  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
		    if (val) {if(val.id=='')nm=val.name;else nm=val.id; if ((val=val.value)!="") {
		      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
		        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
		      } else if (test!='R') {
		        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
		        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
		          min=test.substring(8,p); max=test.substring(p+1);
		          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
		    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
		  } if (errors) alert('The following error(s) occurred:\n'+errors);
	  document.MM_returnValue = (errors == '');
	}

function MM_openBrWindow(theURL,winName,features) { //v2.0 (BCA - added focus)
  popWin = window.open(theURL,winName,features);
	popWin.focus();
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_reloadPage(init) {  //reloads the window if Nav resized  -- edits by BCA
  if (init==true) with (navigator) {if ((appName=="Netscape")) {//&&(parseInt(appVersion)==4)
    document.MM_pgW=innerWidth; onresize=MM_reloadPage; }}// document.MM_pgH=innerHeight;
  else if ((innerWidth!=document.MM_pgW) && ((innerWidth > docMinWidth) || (document.MM_pgW > docMinWidth))) location.reload();// || innerHeight!=document.MM_pgH
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 
	return window.undefined; 
}

function getViewportWidth() {
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
	return window.undefined; 
}

function CloseIframe(){//if the parent contains this function, then we are using modal iframe
if (window.parent.hidePopWin) window.parent.hidePopWin(null); else self.close();
}

/**
 * X-browser event handler attachment and detachment
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, true);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}

function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

function getPopupCaller() {
if (window.parent.hidePopWin) return window.parent; else return window.opener;
}
// End of IFrame Popup Routines

function IE7Up()
{ 											
	var agt = navigator.userAgent.toLowerCase();
	var major = parseInt(navigator.appVersion);
	var ie4 = ((major == 4) && (agt.indexOf("msie 4")!=-1));
		var ie5 = ((major == 4) && (agt.indexOf("msie 5")!=-1));
	var ie6 = ((major == 4) && (agt.indexOf("msie 6")!=-1));
		return IE6Up = (!ie4 && !ie5 && !ie6 && major >= 4);		       
}