// JavaScript Document
function ge(str) {
	return document.getElementById(str);
}

function rememberEmail() {
	if(ge('remember_me').checked) {
		setCookie('email',ge('email').value,1000);
		} else {
		eraseCookie('email');
	}
}


function isChild(s, d) {
	while (s) {
		if (s == d) {
			return true;
		}
		s = s.parentNode;
	}
	return false;
}
function Left(obj) {
	var curleft = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	} else {
		if (obj.x) {
			curleft += obj.x;
		}
	}
	return curleft;
}
function Top(obj) {
	var curtop = 0;
	if (obj.offsetParent) {
		while (obj.offsetParent) {
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	} else {
		if (obj.y) {
			curtop += obj.y;
		}
	}
	return curtop;
}


function alignTo(objParent,objChild) {
	objChild.style.left = Left(objParent) + "px";
	var offSet = 0;
	if(this.offsetHeight != undefined) {
		offSet = this.offsetHeight;
	}
	objChild.style.top = Top(objParent) + offSet + "px";
	if(arguments[2] != undefined) {
		objChild.style.width = arguments[2] + "px";
	}
	objChild.style.display='';
}

var object_to_hide;

// hides an object
function checkClick(e) {
	e ? evt = e : evt = event;
	CSE = evt.target ? evt.target : evt.srcElement;
	if (object_to_hide) {
		if (!isChild(CSE, object_to_hide)) {
			object_to_hide.style.display = "none";
		}
	}
}
document.all ? document.attachEvent("onclick", checkClick) : document.addEventListener("click", checkClick, false);


function getNextMonth(intMonth) {
	var d1 = ge('start_dt');		// start date
	var d2 = ge('end_dt');			// end date	
	
	var date1 = new Date(d1.value);
	//var date2 = new Date(d2.value);
	var date2 = new Date();
	
	date1.setMonth(parseInt(date1.getMonth(),10) + parseInt(intMonth));
	//date2.setMonth(parseInt(date2.getMonth()) + parseInt(intMonth));
	date1.setDate(1);
	
	date2.setMonth(parseInt(date1.getMonth(),10) + 1);
	date2.setFullYear(date1.getFullYear());
	date2.setDate(1);	// sets it to first of month
	date2.setDate(date2.getDate()-1);	// go back one day to get last day of month
	
	d1.value = formatDate(date1,'MM/dd/yyyy');	// from CalendarPopup.js
	d2.value = formatDate(date2,'MM/dd/yyyy');	// from CalendarPopup.js
	
	ge('frmFilter').submit();
}

function changeInt(obj,e) {
	if(isNaN(obj.value)) {
		return;
	}
	// pressing the up arrow will increase the number
	if(e.keyCode == 38) {
		// pressed up arrow in FF
		obj.value = parseInt(obj.value)+1;
	}
	// pressing the down arrow will decrease the number
	if(e.keyCode == 40) {
		// pressed up arrow in FF
		obj.value = parseInt(obj.value)-1;
	}
}

function changeDate(obj,e) {
	var myDate = new Date();
	var month, day, year;
	var arrDt = new Array(1);
	
	if(isDate(obj.value)) {
		myDate = new Date(obj.value);
	} else {
		return;
	}
	
	obj.setAttribute('autocomplete','off');
	
	// pressing the up arrow will decrement the date value by one day
	if(e.keyCode == 38) {
		// pressed up arrow in FF
		myDate.setDate(parseInt(myDate.getDate(),10)-1);
		obj.value = formatDate(myDate,'MM/dd/yyyy');	// from CalendarPopup.js
	}
	// pressing the down arrow will increment the date value by one day
	if(e.keyCode == 40) {
		// pressed down arrow in FF
		myDate.setDate(parseInt(myDate.getDate(),10)+1);
		obj.value = formatDate(myDate,'MM/dd/yyyy');	// from CalendarPopup.js
	}
}

/*
reformats dates entered as m/d/yyyy to mm/dd/yyyy
*/
function reformatDate(inp) {
	var d = inp.value;
	if(d.length==0) {
		return;
	}
	d = d.replace(/\/\//gi,'/');	// replace double slashes
	var parsedDate = new Date(d);
	var month = (parsedDate.getMonth() + 1).toString();
	var date = parsedDate.getDate().toString(); //date is the day of the month
	var year = parsedDate.getFullYear().toString();
	if(month.length == 1)
		month = '0' + month;
	if(date.length == 1)
		date = '0' + date.toString();
	parsedDate = month + '/' + date + '/' + year;
	inp.value = parsedDate;
}

function mapLink(address,city,state,zip,title) {
	// creates this URL:
	// http://maps.google.com/maps?q=4862+Gallia+St,+New+Boston,+OH+45662
	
	var myURL = 'http://maps.google.com/maps?q=';
	myURL = myURL + address + ', ' + city + ', ' + state + ' ' + zip + ' (' + title + ')';
	maps = window.open(myURL,'maps');
}
function mapLatLong(lat,long,title) {
	// creates this URL:
	// http://maps.google.com/maps?q=38.75,-82.93
	
	var myURL = 'http://maps.google.com/maps?q=';
	myURL = myURL + lat + ',' + long + ' (' + title + ')';
	maps = window.open(myURL,'maps');
}

function checkThisBox(obj) {
	if(obj) {
		if(obj.checked){
			obj.parentNode.className = 'selected';
			} else {
			obj.parentNode.className = '';
		}
	}
}

// formats phone numbers as user types input
// usage: <input onkeypress="formatPhoneNum(this)"
function formatPhoneNum(e) { 
	var PhoneLength;
	PhoneLength = e.value.length;
	// puts in the first dash 
	if (PhoneLength==4 && e.value.substring(2,3) != '-'){
		e.value = e.value.substring(0,3) + '-' + e.value.substring(3,PhoneLength);
	}
	// stepping over where the dash usually goes
	if (PhoneLength==9 && e.value.substring(7,8) != '-'){
		e.value = e.value.substring(0,7) + '-' + e.value.substring(7,PhoneLength);
	}
	// remove double dash
	e.value = e.value.replace('--','-');
}

function cancelModify() {
	location.href = 'index.cfm';
}

// revised 10/9/2006 -jp -for faster performance with longer lists
// revised 5/3/2007 -jp - looking for a 3rd argument in the value to make the selection (for sublists)
function checkAll(objCB,arrFlds) {
	// locate all checkboxes except for the indicator
	// example: <input type="checkbox" id="selectAll_1" checked onclick="checkAll(this,this.form.contact);" />
	// the inputs should all have the same name i.e. "contact" in this example	
	var chk = objCB.checked;
	var fld = 0;
	// 10/17/2006 - jp - bug found when only one checkbox is in the array
	if (arrFlds.type == 'checkbox') {
		arrFlds.checked = chk;
		return true;
	}
	// if multiple are found, check them all
	if(arrFlds.length) {
		fld = arrFlds.length;
		for(var j=0;j<fld;j++) {
			if(!arrFlds[j].disabled) {
				if(arguments[2]) {	// if there is a third value, check for the value in the cb
					if(arrFlds[j].id.indexOf(arguments[2]) >= 0) {
						arrFlds[j].checked = chk;
					}
				} else {
					arrFlds[j].checked = chk;
				}
				checkThisBox(arrFlds[j]);
			}
		}
		return true;
	} 
}

var allowedKeys = new Array();
allowedKeys = [8,9,16,17,18,19,27,33,34,35,36,37,38,39,40,44,45,46,91,116,144,145];
/*
	Allowed IE/Firefox keycodes
	8	BACKSPACE
	9	TAB
	16	SHIFT
	17	CTRL
	18	ALT
	19	BREAK
	27	ESC
	
	33	PAGE UP
	34	PAGE DOWN
	35	END
	36	HOME
	37	ARROW LEFT
	38	ARROW UP
	39	ARROW RIGHT
	40	ARROW DOWN
	44	PRINT SCREEN
	45	INSERT
	46	DELETE
	
	91	WINDOWS
	116	F5
	144	NUM LOCK
	145	SCROLL LOCK
	
*/

/*
	Usage:
	<textarea name="comment_tx" id="comment_tx" cols="50" rows="5" maxlength="2000"
		onkeypress="return isMaxLength(this,event);"
		onkeyup="return isMaxLength(this,event);"
		style="overflow:visible;height:100px;"></textarea>
*/
function isMaxLength(obj,e) {
	var mlength = obj.getAttribute ? parseInt(obj.getAttribute('maxlength')) : '';
	
	var msgID = obj.id + '_msg';
	var msg = document.getElementById(msgID);
	// check for message text
	if (!msg) {
		msg = document.createElement('div');
		msg.id = msgID;
		msg.className = 'small';
		obj.parentNode.appendChild(msg);
	}
	msg.innerHTML = obj.value.length + ' of ' + mlength + ' characters used';
	
	var isAllowedKey = false;
	//alert(e.keyCode);
	for (i=0;i<allowedKeys.length;i++) {
		if(parseInt(e.keyCode) == parseInt(allowedKeys[i])) {
			//alert(e.keyCode);
			isAllowedKey = true;
			break;
		}
	}
	if(isAllowedKey) {
		return true;
	}
	//alert('continuing');
	
	if (obj.getAttribute && obj.value.length==mlength) {
		return false;
	}
	if (obj.getAttribute && obj.value.length>mlength) {
		obj.value=obj.value.substring(0,mlength);
		return false;
	}
	return true;
}

var allCaps = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var numChars = '0123456789';
var realNumChars = numChars + '-.';
var phoneChars = numChars + '-';
var dateChars = numChars + '/';

// USAGE: <input type="text" onKeyPress="return allowChar(this,phoneChars,event);">
function allowChar(obj,allowedChars,e) {
	var key = '';
	var KEY_SPECIAL = '';
	var isAllowedKey = false;

	for (i=0;i<allowedKeys.length;i++) {
		if(parseInt(e.keyCode) == parseInt(allowedKeys[i])) {
			//alert(e.keyCode);
			isAllowedKey = true;
			break;
		}
	}
	// increment/decrement dates
	changeDate(obj,e);
	// increment/decrement numbers
	changeInt(obj,e);
	
	if(isAllowedKey) {
		return true;
	}

	if(document.all) {
		key = String.fromCharCode(e.keyCode).toLowerCase();		// IE
		if (allowedChars.indexOf(key)<0) {
			return false;
		}
	} else {
		key = String.fromCharCode(e.which).toLowerCase();		// Firefox/Netscape
		if (allowedChars.indexOf(key)<0) {
			return false;
		}
	}
}

function jump(thisNum,e,max,action) {
    if (document.layers) {
        if (e.target.value.length >= max)
            eval(action);
    }
    else if (document.all){
        if (thisNum.value.length > (max-1))
            eval(action);
    }
}
function insertDash() { 
	var TaxIDLength;
  	TaxIDLength = document.forms[0].TaxID.value.length;
  	if (TaxIDLength==2){
  		document.forms[0].TaxID.value += "-";
  	}
  	var SSNLength;
  	SSNLength = document.forms[0].SSN.value.length;
  	if (SSNLength==3 || SSNLength==6){
  		document.forms[0].SSN.value += "-";
  	}
}
	
function loadAndStripe() {	
	// automatically stripe the table, if any are found
	var docTable = document.getElementsByTagName('table');
	for(var i=0;i<docTable.length;i++) {
		if(docTable[i].className.indexOf('report') >= 0) {
			stripeTable(docTable[i]);
			//docTable[i].style.border = '1px solid red';
		}
	}
	//stripeTable(document.getElementById('summaryTable'));
}

function formatCurrency(num) {
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num + '.' + cents);
}

function checkShape(s) {
	if(s=='Other') document.getElementById('specify').style.display = ''
	else document.getElementById('specify').style.display = 'none'
}

function confirmDelete() {
	return confirm("Please confirm you would like to remove this item.\n[ WARNING: This action is irreversible. ]");
}

// automatically sets focus to the first non-empty field
function setFocus() {
	if(document.location.href.indexOf('#') > 0) {
		return;	
	}
	for(z=0;z<document.forms.length;z++) {
		for (i=0;i<document.forms[z].elements.length;i++) {
			if(document.forms[z].elements[i].value == '' 
				&& document.forms[z].elements[i].getAttribute("type") != 'hidden') {
				try {
					document.forms[z].elements[i].focus();
				} catch(e) {
					window.status = document.forms[z].elements[i].name + ' couldn\'t accept focus';
					var timer = window.setTimeout("window.status = 'Done.'",3*1000);
				} 
				break;
			}
		}
	}
}

function toggleLayer(o) {
	if(o.style.display == 'none') o.style.display = ''
	else o.style.display = 'none'
}

// form formatting functions
	// http://www.novell.com/documentation/extendas35/docs/help/books/TechFormValidation.html
	// USAGE:  <input name="phone" type="text" onBlur="formatPhone(this);">
	function formatPhone(obj) {
		obj.className = '';
		if (obj.value != '') {
			var bool = formatString(obj.value,/\d{3}-\d{3}-\d{4}/);
			if (!bool) {	// error found
				obj.className = 'required';	
				alert('Expecting phone format 000-000-0000');
				
				obj.focus();
			}
			return bool;
		}
	}
	
	// USAGE:  <input name="DSN" type="text" onBlur="formatDSN(this);">
	function formatDSN(obj) {
		obj.className = '';
		if (obj.value != '') {
			var bool = formatString(obj.value,/\d{3}-\d{4}/);
			if (!bool) {	// error found
				obj.className = 'required';	
				alert('Expecting DSN format 000-0000');
				obj.focus();
			}
			return bool;
		}
	}
	
	// USAGE:  <input name="dateInput" type="text" onBlur="formatDate(this);">
	function formatDate(obj) {	// assuming mm/dd/yyyy
		obj.className = '';
		if (obj.value != '') {
			var bool = formatString(obj.value,/\d{2}\/\d{2}\/\d{4}/);
			if (!bool) {	// error found
				obj.className = 'required';	
				alert('Expecting date format mm/dd/yyyy');
				obj.focus();
			}
			return bool;
		}
	}
	
	function formatString(str,pattern) {
		// format a string based on what is passed in 
		// 999-999-9999
		var retVal = true;
		//pattern = /\d{3}-\d{3}-\d{4}/;
		if (!pattern.test(str)) {
			//document.forms[0].elements[2].focus();
			retVal = false;
		}
		return retVal;
	}
	

function clearSearch(obj) {
	if(obj.value == '- Search -') {
		obj.value = '';
	}
}

var fckDone = false;

function doFCK(objTxtBx) {
	if(fckDone) {
		return;
	}
	fckDone = true;
	
	//FCKeditor( instanceName, width, height, toolbarSet, value )
	//var oFCKeditor = new FCKeditor( 'prob_desc', '100%', '200' ) ;		//, 'Basic'
	var oFCKeditor = new FCKeditor( objTxtBx.name, '100%', '300px' ) ;		//, 'Basic'
	
	if (appHome == undefined) {
		appHome = '';
	}
	
	oFCKeditor.BasePath = appHome + 'FCKeditor/';	// apphome defined dynamically in header
	if(objTxtBx.className.indexOf('basic')>=0) {
		oFCKeditor.ToolbarSet = 'Basic';	// 'Full';
	}
	//oFCKeditor.Value = '' ;
	//oFCKeditor.Create() ;
	oFCKeditor.ReplaceTextarea() ;
}

function initFCK() {
	var arrTA = document.getElementsByTagName('textarea');
	for(var j=0;j<arrTA.length;j++) {
		if(arrTA[j].className.indexOf('fck')>=0) {
			doFCK(arrTA[j]);
		}
	}
}

// function used to check form values on submission.
// USAGE: <form onSubmit="return checkFormPlusBlur(this);" ...
function checkFormPlusBlur(objForm) {
	if(checkForm(objForm)) {
		// loop through form elements
		var arrEl = objForm.elements;
		for(var i = 0; i<arrEl.length; i++) {
			if((document.all && arrEl[i].typeOf == 'submit'  || (arrEl[i].type == 'submit'))) {
				arrEl[i].setAttribute('readOnly','true');
				arrEl[i].setAttribute('disabled','true');
				arrEl[i].style.color = '#999';
				arrEl[i].style.background = '#EEE';
				arrEl[i].value = 'Please wait...';
				arrEl[i].style.cursor = 'wait';
				document.body.style.cursor = 'wait';
				return true;
				break;
			}
		}
		//objForm.submit();
	}
	return false;
}

function selectPage() {
	var myLink = ge('nav').getElementsByTagName('a');
	var thisPage = location.href;
	// main navigation
	for(var i=myLink.length-1;i>=0;i--) {
		var thisHref = myLink[i].href;
		if(thisPage.indexOf(thisHref) >= 0) {
			myLink[i].className = 'selected';
			myLink[0].className = '';
			break;
		}
	}
	// sub-level navigation
	if(ge('nav2')) {
		var myLink = ge('nav2').getElementsByTagName('a');
		for(var i=myLink.length-1;i>=0;i--) {
			var thisHref = myLink[i].href;
			if(thisPage.indexOf(thisHref) >= 0) {
				myLink[i].className = 'selected';
				break;
			}
		}
	}
}

var inited = false;
// initial functions to run onload (called from slideshow.js)
function init() {
	try {
		if(inited==false) {
			setFocus();
		}
	} catch(e) {}
	try {
		loadAndStripe();
	} catch(e) {}
	try {
		initFCK();
	} catch(e) {}
	try {
		selectPage();
	} catch(e) {}
	inited = true;
}
//var sessionTimer = window.setTimeout("location.href = appHome + 'message.cfm?session_expired=1'",61*60*1000);
window.onload = init;