//FIRST, TELL THE BROWSERS TO REACT TO THE EVENT

var curKeyCode = null;

function ck(e) {
 if (window.event) curKeyCode = window.event.keyCode;
 else if (e) curKeyCode=e.which;
}

document.onkeydown = ck;
window.onkeydown = ck;

if (document.captureEvents) document.captureEvents(Event.KEYDOWN);

 
function changeClass(id,section) {

	document.getElementById(id).className = section;

}

function fileFormSubmit(formName,action,module) {

	document.getElementById("module").value = module;
	document.getElementById("formAction").value = action;

	document[formName].submit();

}

function hideObject(obj) {

	document.getElementById(obj).style.position="absolute";
	document.getElementById(obj).style.visibility="hidden";
	document.getElementById(obj).style.zIndex="-10";

}

function showObject(obj) {

	document.getElementById(obj).style.position="static";
	document.getElementById(obj).style.visibility="visible";
	document.getElementById(obj).style.zIndex="1";

}

function showRelObject(obj) {

	document.getElementById(obj).style.position="relative";
	document.getElementById(obj).style.visibility="visible";
	document.getElementById(obj).style.zIndex="1";

}

function showAbsObject(obj) {

	document.getElementById(obj).style.position="absolute";
	document.getElementById(obj).style.visibility="visible";
	document.getElementById(obj).style.zIndex="1";

}

function cycleObject(obj) {

	var visib = document.getElementById(obj).style.visibility;
	
	if (visib=="visible") hideObject(obj);
	else showObject(obj);

}

function blinkText(id) {

	setTimeout("blinkText('" + id + "')",500);

	var x = document.getElementById(id).style.color;

	if (x == "red") {
		document.getElementById(id).style.color="#DCDCDC";
	}
	else {
		document.getElementById(id).style.color="red";
	}
}

function changeStyle(id,styleValue) {

	document.getElementById(id).style.cursor="default";

	if (styleValue == "highlight") {

		document.getElementById(id).style.backgroundColor = "#DCDCDC";
		document.getElementById(id).style.color = "black";

	} else {

		document.getElementById(id).style.backgroundColor = "gray";
		document.getElementById(id).style.color = "white";

	}
}



function centerParms(width,height) {

	xPos = (screen.width - width) / 2;
	yPos = (screen.height - height) / 2;

	string = "left=" + xPos + ",top=" + yPos;

	return string;
}

function centerStyleParms(width,height) {

	xPos = (screen.width - width) / 2;
	yPos = (screen.height - height) / 2;

	string = "left:" + xPos + ";top:" + yPos + ";";

	return string;
}

function openPicWindow(path) {

	path = "../" + path;
	parms = "scrollbars=yes";
	window.open('header/popup.html?'+path,'_new',parms);

}


function makeArray() {
    this[0] = makeArray.arguments.length;
    for (i = 0; i<makeArray.arguments.length; i++)
   this[i+1] = makeArray.arguments[i];
}

function LeapYear(year) {
    if ((year/4)   != Math.floor(year/4))   return false;
    if ((year/100) != Math.floor(year/100)) return true;
    if ((year/400) != Math.floor(year/400)) return false;
    return true;
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }

function daysPerMonth(year) {

	var months = new makeArray('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	var days_per_month   = new makeArray(31,28,31,30,31,30,31,31,30,31,30,31);
	

	var myDate = new Date(); // 1st January 1999

	if (LeapYear(y2k(year)))
   		 days_per_month[2] = 29;
	else
    	days_per_month[2] = 28;


	return days_per_month;

}

function fixDay(prefix) {

	//reset the days
	dayField = prefix + "_day";
	monthField = prefix + "_month";
	yearField = prefix + "_year";

	dayValue = document.getElementById(dayField).value - 1;
	//alert(dayValue);
	//how many days are in our current month
	num = parseInt(document.getElementById(monthField).value);
	yearValue = document.getElementById(yearField).value;

	dayArray = daysPerMonth(yearValue);
	total = dayArray[num];

	//reset the day dropdown
	document.getElementById(dayField).options.length = "0";

	//rebuild it with the correct number of days for the cur month
	for (row=0;row<total;row++) {

		if (row<9) cur = "0" + parseInt(row + 1);
		else cur = row + 1;	

		document.getElementById(dayField)[row] = new Option(cur,cur);

	}

	document.getElementById(dayField)[dayValue].selected = true;
	
}

function saveCaret(curField) {

	elem = document.getElementById(curField);

	if (elem.isTextEdit) elem.caretPos = document.selection.createRange();	
	else return true;
	
}

function getCaretPos(curField) {

	elem = document.getElementById(curField);
	
	if (elem.isTextEdit && elem.caretPos) {

		origMax = document.getElementById(curField).maxLength;
		newMax = origMax + 1;
		
		document.getElementById(curField).maxLength = newMax;
		var bookmark = "~";
		var caretPos = elem.caretPos;
		var orig = elem.value;
		caretPos.text = bookmark;
		var i = elem.value.search( bookmark );
		elem.value = orig;
		document.getElementById(curField).maxLength = origMax;
		
		return i;
		
	}

}

function fieldJump(curField,prevField,nextField) {

	if (document.getElementById(curField).selectionEnd) {
		curPos = document.getElementById(curField).selectionEnd;
	}
	else if (window.event) {
	
		saveCaret(curField);
		curPos = getCaretPos(curField);
		if (window.event) curKeyCode = window.event.keyCode;

	}
	maxLen = document.getElementById(curField).maxLength;

	window.status = curPos;

	if (!curPos && (curKeyCode=="8" || curKeyCode=="37")) {
		document.getElementById(prevField).focus();
	}
	else if (curPos==maxLen) {
		document.getElementById(nextField).focus();
	}
}


