//a variable for referencing our file manager window
var fmwin;

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 cycleObject(obj) {

	visib = document.getElementById(obj).style.visibility;

	if (visib=="visible") hideObject(obj);
	else showObject(obj);

}

function cycleImageObject(obj,img,open,close) {

	visib = document.getElementById(obj).style.visibility;

	if (visib=="visible") {

		hideObject(obj);
		document.getElementById(img).src = close;

	}	
	else {
	
		showObject(obj);
		document.getElementById(img).src = open;

	}
}

function showMenu(obj) {

	document.getElementById(obj).style.visibility="visible";

}

function hideMenu(obj) {

	document.getElementById(obj).style.visibility="hidden";

}


function hideObject(obj) {

	document.getElementById(obj).style.position="absolute";
	document.getElementById(obj).style.visibility="hidden";
	document.getElementById(obj).style.top="0px";
	document.getElementById(obj).style.left="0px";
	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 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 rightParms(width,height) {

	var fullWidth = screen.width;

	var left = fullWidth - width - 10;

	string = "left=" + left +",top=0,width=" + width + ",height=" + height;

	return string;
}


function centerParms(width,height) {

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

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

	return string;
}

function centerWidth(width) {

	var newval = (screen.width - width) / 2;
	return newval

}

function centerHeight(height) {

	var newval = (screen.height - height) / 2;
	return newval

}

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";

	var checkValue = document.getElementById(dayField).value;
	var dayValue = checkValue - 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);

	}

	if (checkValue>total) newValue = total;
	else newValue = checkValue;
	
	var newSelect = newValue - 1;

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

function phoneJump(prevField,curField,nextField,posField,property)  {

	curValue = document.getElementById(curField).value;
	curPos = curValue.length;

	if (prevField) {
		prevValue = document.getElementById(prevField).value;
		prevPos = prevValue.length;
	}
	
	if (property=="store") {

		document.getElementById(posField).value = curPos;

	}
	else {

		//basically, if our new cursor value is less than or equal to
		//our stored value, we need to jump back one field

		storePos = document.getElementById(posField).value;
		storePos = parseInt(storePos);

		//if our storePos is 2, we'll probably be jumping up, so check
		if (storePos>=2) {
			if (storePos<=curPos && nextField!=null) {
				document.getElementById(nextField).focus();
			}

		}
		else if (storePos=="0") {

			if (storePos>=curPos) {
				document.getElementById(prevField).focus();
				//window.event.keycode = "39";
				document.getElementById(prevField).value=prevValue;
			}

		}

	}
}


if (navigator.appName!="Microsoft Internet Explorer") {

        xPosition = 0;
        yPosition = 0;

        function captureMousePosition(e) {

                xPosition = e.clientX;
                yPosition = e.clientY;

        }

        //document.captureEvents(Event.MOUSEMOVE);
        document.onmousemove = captureMousePosition;

}

function showFunctionMenu(id,vert,horiz) {

        newVert = parseInt(vert);
        newHoriz = parseInt(horiz);

	if (navigator.appName!="Microsoft Internet Explorer") {

                xPos = xPosition + document.body.scrollLeft + newHoriz;
                yPos = yPosition + document.body.scrollTop + newVert;

	} else {

                //get the mouse position
                xPos = window.event.x + document.body.scrollLeft + newHoriz;
                yPos = window.event.clientY + document.body.scrollTop + newVert;

	}

        document.getElementById(id).style.left = xPos;
        document.getElementById(id).style.top = yPos;
        showAbsObject(id);

}

function hideFunctionMenu(id) {

	document.getElementById(id).style.zIndex="-10";
	document.getElementById(id).style.visibility="hidden";

}

function switchImg(id,img) {

        document.getElementById(id).src = img;

}

function manageFiles(page) {

	parms = centerParms('950','630') + ",width=950,height=630,status=no,resizeable=yes,scrollbars=yes";
	url = "index.php?module=filemanage&page=" + page;

	fmwin = window.open(url,"_filemanage",parms);

}

function simpleManageFiles(filePath,formName,previewName) {

	parms = centerParms('900','630') + ",width=900,height=630,status=no,resizeable=yes,scrollbars=yes";
	url = "index.php?module=simplefilemanage&filePath=" + filePath + "&returnFormName=" + formName + "&returnPreviewName=" + previewName;

	window.open(url,"_simplefilemanage",parms);

}

//calculates the left offset of an object
function calculateOffsetLeft(r){
        return Ya(r,"offsetLeft");
}

//calcuates teh top offset of an object
function calculateOffsetTop(r){
        return Ya(r,"offsetTop");
}

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

function nameWindow(newname) {

	window.name = newname;

}

function escapeBackslash(str) {

	var arr = str.split("\\");
	var newstr = arr.join("\\\\");

	return newstr;
}

function isChecked(ref) {

	var i;

	if (!ref) return false;
	var len = ref.length;
	if (!len) return false;

	for (i=0;i<len;i++) {
		if (ref[i].checked==true) return true;
	}	

	//if we get to here, nothing was checked
	return false;

}

//trim function
function Trim(str){
    return str.replace(/^s+|\s+$/g,"");
}
