/* Common Functions (common.js) */
function none() {
	return undefined;
};

function hideDiv(d) {
	d.className = 'dh';
};

function showDiv(d) {
	d.className = 'dv';
};

function fixYear(f) {
	var g = parseInt(f.value);
	if (g) {
		if (g > 0 && g < 1000) {
			f.value = g + 1900;
		}
	}
};

function sumAttr(cobj,attr) {
	var r = 0;
	while (cobj) {
		r += cobj[attr];
		cobj = cobj.offsetParent;
	}
	return r;
};

function objPosLeft(cobj) {
	return sumAttr(cobj,'offsetLeft');
};

function objPosTop(cobj) {
	return sumAttr(cobj,'offsetTop');
};

function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + '=';
	var begin = dc.indexOf('; ' + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) {
			return null;
		}
	}
	else {
		begin += 2;
	}
	var end = document.cookie.indexOf(';',begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
};

function quickLaunch(addr,name,width,height) {
	var w = window.open(addr,name,'scrollbars=yes,width=' + width + ',height=' + height);
	if (w.opener == null) w.opener = window;
	w.opener.name = 'opener';
};

function fillObject(fillobjid,htm) {
	if (document.getElementById(fillobjid)) {
		showDiv(document.getElementById(fillobjid));
		document.getElementById(fillobjid).innerHTML = htm;
	}
};

function fillObjectSimp(fillobjid,htm) {
	if (document.getElementById(fillobjid)) {
		document.getElementById(fillobjid).innerHTML = htm;
	}
};

function runImageCmd(url) {
	var d = new Date();
	url = url + '&t=' + d.getTime();
	if (document.getElementById('imgcmdarea')) {
		document.getElementById('imgcmdarea').innerHTML = '<img src="' + url + '" />';
	}
	else {
		var i = new Image;
		i.src = url;
	}
};

function trimString(s) {
	s = s.replace( /^\s+/g, '' );
	return s.replace( /\s+$/g, '' );
};


/* API (api.js) */
/* This is a set of simple functions for API references in page via JavaScript, allowing for dynamic
   data to be retrieved from the server without a full page reload. It supports both Mac and Windows
   computers, choosing to use XMLHTTP object on Windows browsers and IFRAMES on Macintosh browsers.
   To my knowledge, it has been tested and works well in the most common browsers including:

   - Firefox
   - Safari
   - Internet Explorer 5 and newer (maybe even 4, but hasn't been tried)
   - Netscape 6/7/8
*/

/* Browser Detection */
var NS6 = false;
var IE4 = (document.all);
if (!IE4) {
	NS6=(document.getElementById);
}
var NS4 = (document.layers);

var _hasInitiatedAPI = false;
var _hasXMLHTTP = false;
var _apiInstances = null;

/* Common Functions */
function getXMLHTTP(){
	var x = null;
	try {
		x = new ActiveXObject('Msxml2.XMLHTTP');
	}
	catch(e) {
		try {
			x = new ActiveXObject('Microsoft.XMLHTTP');
		}
		catch(oc) {
			x = null;
		}
	}
	if(!x && typeof XMLHttpRequest != 'undefined') {
		x = new XMLHttpRequest();
	}
	return x;
};

function escapeURI(sTerm){
	if (encodeURIComponent) return encodeURIComponent(sTerm);
	if (escape) return escape(sTerm);
	return sTerm;
};

function initiateAPI() {
	if (_hasInitiatedAPI) {
		return;
	}

	if (getXMLHTTP()) {
		_hasXMLHTTP = true;
	}
	else{
		_hasXMLHTTP = false;
	}

	// There is no instance 0
	_apiInstances = new Array(null);

	var iframcon = document.createElement('DIV');
	iframcon.style.visibility = 'hidden';
	iframcon.style.position = 'absolute';
	iframcon.style.left = '-10000px';
	iframcon.style.top = '-10000px';
	iframcon.style.width = '0';
	iframcon.style.height = '0';

	var ifram = document.createElement('IFRAME');
	ifram.name = 'ifloading';
	ifram.id = 'ifloading';

	iframcon.appendChild(ifram);
	document.body.appendChild(iframcon);

	_hasInitiatedAPI = true;
};

function createInstance(nam) {
	if (_apiInstances == null) {
		return false;
	}

	var nid = _apiInstances.length;
	_apiInstances[nid] = new Array(getXMLHTTP(),nam);
	return nid;
};

function getInstance(nam) {
	var i = 0;
	while (i < _apiInstances.length) {
		if (_apiInstances[i] && _apiInstances[i][0] && _apiInstances[i][1] == nam) {
			if (nam == null) {
				if (_apiInstances[i][0].readyState == 0) {
					_apiInstances[i][0] = getXMLHTTP();
					return i;
				}
			}
			else {
				if (_apiInstances[i][0] && _apiInstances[i][0].readyState != 0) {
					_apiInstances[i][0].abort();
				}

				_apiInstances[i][0] = getXMLHTTP();
			}
			return i;
		}
		i = i + 1;
	}
	return createInstance();
};

function runAPI(loc,nam) {
	initiateAPI();

	loc = loc + '&sessid=' + getCookie('rev_sessid');

	if (_hasXMLHTTP) {
		var instanceId = getInstance(nam);

		if (instanceId === false) {
			return false;
		}

		try {
			_apiInstances[instanceId][0].open('GET',loc + '&js=true',true);
			_apiInstances[instanceId][0].onreadystatechange = function() {
				if (_apiInstances[instanceId][0].readyState == 4 && _apiInstances[instanceId][0].responseText) {
					if (_apiInstances[instanceId][0].responseText.charAt(0) != '<') {
						eval(_apiInstances[instanceId][0].responseText);
					}
				}
			};
			_apiInstances[instanceId][0].send(null);
		}
		catch (e) {
			alert("An error occured while trying to interact with the server. Please note that whatever action you were trying to carry out DID NOT go through. Please confirm that you are online, then try again. If you continue to get the error, please refresh the current page.\n\nDetails:\n" + e.message);
		}

		return true;
	}
	else {
		frames['ifloading'].document.location.replace(loc);

		return true;
	}
};

/* HTML Functions */
function addToList(lstid, htm) {
	if (document.getElementById) {
		var itm = document.createElement('LI');
		itm.innerHTML = htm;
		document.getElementById(lstid).appendChild(itm);
	}
}

/* Calendar (calendar.js) */
var _calDiv;
var _calMonthNames = new Array("","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var _calDays = new Array("S","M","T","W","T","F","S");

function hideAllSelect() {
	var sa = document.getElementsByTagName('SELECT');
	if (sa) {
		var i = 0;
		var maxi = sa.length;
		while (i < maxi) {
			if (sa[i].className != 'calshow') {
				sa[i].style.visibility = 'hidden';
			}
			i = i + 1;
		}
	}
};

function showAllSelect() {
	var sa = document.getElementsByTagName('SELECT');
	if (sa) {
		var i = 0;
		var maxi = sa.length;
		while (i < maxi) {
			if (sa[i].className != 'calshow') {
				sa[i].style.visibility = 'visible';
			}
			i = i + 1;
		}
	}
};

function daysinMonth(month,year) {
	if (month == 2) {
		if (((year/4, 10) - parseInt(year/4, 10)) == 0) {
			return 29;
		}
		return 28;
	}

	if (month == 4 || month == 6 || month == 9 || month == 11) {
		return 30;
	}

	return 31;
};

function setCalDate(fldID,month,day,year) {
	var fld = $(fldID);
	fld.value = month + '/' + day + '/' + year;
	if ( fld.onchange ) fld.onchange();
	cancelCal();
};

function cancelCal() {
	showAllSelect();
	_calDiv.style.display = 'none';
	_calDiv.style.visibility = 'hidden';
	_calDiv.style.left = '-500px';
	_calDiv.style.top = '-500px';
};

function drawCalendar(fldID,month,year) {
	var s, i, tempMonth, tempYear;

	month = parseInt(month, 10);
	year = parseInt(year, 10);

	tempMonth = month - 1;
	tempYear = year;

	if (tempMonth == 0) {
		tempMonth = 12;
		tempYear = tempYear - 1;
	}


	s = '<table border="0" cellpadding="2" cellspacing="1" width="100%" class="pucal"><tr> <td width="15%" align="center" bgcolor="#cccccc"><a href="#" onClick="drawCalendar(\'' + fldID + '\',' + tempMonth + ',' + tempYear + '); return false;">&lt;&lt;</a></td> ';

	s = s + '<td width="70%" colspan="5" align="center" bgcolor="#cccccc"><select name="calmon" id="calmon" onChange="drawCalendar(\'' + fldID + '\',this.options[this.selectedIndex].value,' + year + ');" class="calshow">';

	i = 1;
	while (i <= 12) {
		s = s + '<option value="' + i + '"';
		if (i == month) {
			s = s + ' selected';
		}
		s = s + '>' + _calMonthNames[i] + '</option>';
		i = i + 1;
	}

	s = s + '</select> <select name="calyear" id="calyear" onChange="drawCalendar(\'' + fldID + '\',' + month + ',this.options[this.selectedIndex].value);" class="calshow">';

	i = year - 8;
	while (i <= (year + 8)) {
		s = s + '<option value="' + i + '"';
		if (i == year) {
			s = s + ' selected';
		}
		s = s + '>' + i + '</option>';
		i = i + 1;
	}


	s = s + '</select></td> ';

	tempMonth = month + 1;
	tempYear = year;

	if (tempMonth == 13) {
		tempMonth = 1;
		tempYear = tempYear + 1;
	}

	s = s + '<td width="15%" align="center" bgcolor="#cccccc"><a href="#" onClick="drawCalendar(\'' + fldID + '\',' + tempMonth + ',' + tempYear + '); return false;">&gt;&gt;</a></td> </tr><tr> ';
	i = 0;
	while (i < 7) {
		s = s + '<td width="1';
		if (i == 0 || i == 6) {
			s = s + '5';
		}
		else {
			s = s + '4';
		}
		s = s + '%" bgcolor="#000099" align="center"><font color="#ffffff">' + _calDays[i] + '</font></td> ';
		i = i + 1;
	}
	s = s + '</tr>';

	var dt = new Date(year,month-1,1);

	var dow = dt.getDay();
	i = 0;
	while (i < dow) {
		if (i == 0) {
			s = s + '<tr> ';
		}
		s = s + '<td bgcolor="#ffffff">&nbsp;</td> ';
		i = i + 1;
	}

	var today = new Date();
	var todayMonth = today.getMonth() + 1;
	var todayYear = today.getYear() + 1900;
	var lt = -1;
	if (todayMonth == month && todayYear == year) {
		lt = today.getDate();
	}


	var days = daysinMonth(month,year);
	i = 1;
	while (i <= days) {
		if (dow == 0) {
			s = s + '<tr> ';
		}
		s = s + '<td bgcolor="#';
		if (i == lt) {
			s = s + 'ff9900';
		}
		else {
			s = s + 'ffff99';
		}
		s = s + '" align="center"><a href="#" onClick="setCalDate(\'' + fldID + '\',' + month + ',' + i + ',' + year + '); return false;">' + i + '</a></td> ';
		dow = dow + 1;
		if (dow == 7) {
			s = s + '</tr>';
			dow = 0;
		}
		i = i + 1;
	}
	if (dow > 0) {
		while (dow < 7) {
			s = s + '<td bgcolor="#ffffff">&nbsp;</td> ';
			dow = dow + 1;
		}
		s = s + '</tr>';
	}
	s = s + '</table><table cellspacing="1" cellpadding="2" width="100%"><tr> <td width="50%"><a href="#" onClick="drawCalendar(\'' + fldID + '\',' + todayMonth + ',' + todayYear + '); return false;"><img src="/images/calendar/today.gif" border="0" /> Today</a></td> <td width="50%" align="right"><a href="#" onClick="cancelCal(); return false"><img src="/images/calendar/cancel.gif" border="0" /> Cancel</a></td> </tr></table>';



	_calDiv.innerHTML = s;
};

function showCalendar(fldID) {
	var fld;
	fld = $(fldID);
	if (!fld) {
		return false;
	}

	if (!(_calDiv)) {
		_calDiv = document.createElement('DIV');
		_calDiv.id = 'cal';
		_calDiv.name = 'cal';
		_calDiv.style.position = 'absolute';
		_calDiv.style.overflow = 'hidden';
		_calDiv.style.border = '1px solid black';
		_calDiv.style.padding = '1px;';
		_calDiv.style.visibility = 'hidden';
		_calDiv.style.display = 'none';
		_calDiv.style.background = '#fff';

		document.body.appendChild(_calDiv);
	}

	_calDiv.style.top = (objPosTop(fld) + (1.2 * fld.offsetHeight)) + 'px';
	_calDiv.style.left = objPosLeft(fld) + 'px';

	if (fld.offsetWidth < 200) {
		_calDiv.style.width = '200px';
	}
	else {
		_calDiv.style.width = fld.offsetWidth + 'px';
	}

	var curdate = new Date();

	var curMonth = curdate.getMonth() + 1;
	var curYear = curdate.getYear();

	/* Parse the initial date */
	if (fld.value != '') {
		var val;
		val = fld.value;
		if (val.match(/^[01]?\d(\/|-)[0123]?\d(\/|-)(\d{2}|\d{4})$/)) {
			var prts = val.split(/\/|-/);
			curMonth = prts[0];
			curYear = prts[2];
		}
	}

	curMonth = parseInt(curMonth, 10);
	curYear = parseInt(curYear, 10);

	if (curYear < 100) {
		if (curYear < 50) {
			curYear = curYear + 2000;
		}
		else {
			curYear = curYear + 1900;
		}
	}
	if (curYear >= 100 && curYear < 200) {
		curYear = curYear + 1900;
	}

	drawCalendar(fldID,curMonth,curYear);

	hideAllSelect();

	_calDiv.style.display = '';
	_calDiv.style.visibility = 'visible';
};

/* Macromedia Functions (mm.js) */
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
};
MM_reloadPage(true);

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_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_findObj(n, d) { //v4.0
  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 && document.getElementById) x=document.getElementById(n); return x;
};

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];}
};