// July 16, 2004
// added deletemsg function
// Dec. 14, 2004 - ryan
// added round function, ses_id variable
// Jan. 5, 2005 - ryan
// added toggle function for tip window
// Jun. 2, 2005 - ryan
// added SwitchDisplay function for help areas
// Feb 26, 2005 - Don
//Added PNG fix code
//April 16, 2007 - Don
//Added code for desktop config

var modaldiv;

idi = location.href.indexOf('id=');
idx = location.href.indexOf('&', idi+1);
var ses_id = "";
if(idi != -1) {
	if(idx == -1) {
		ses_id = location.href.substring(idi+3);
	} else {
		ses_id = location.href.substring(idi+3, idx);
	}
}

function isNumber(e){
	if(!isNS4){
//					document.getElementById('hphone3').value=e.keyCode;
		if (e.keyCode==8){ return true;}
		if (e.keyCode==9){ return true;}
		if ((e.keyCode > 45) && (e.keyCode < 58)){return true;}
		if ((e.keyCode > 95) && (e.keyCode < 106)){return true;}
		if ((e.keyCode ==190)){return true;}
		return false;
	}else{
		if (e.which==8){return true;}
		if (e.which==9){return true;}
		if ((e.which > 36) && (e.which < 58)){return true;}
		if ((e.which > 95) && (e.which < 106)){return true;}
		if ((e.which == 95)){return true;}
		return false;
	}
}

function fieldlimit(cfield, count, dfield){
	var lengther = cfield.value;
	if ((lengther.length)+0 == count){
		var temp=cfield.form.name;
		document.getElementById(dfield).focus();
	}
}

// needed?
function show(e)
{
var DivRef=document.getElementById(e);
var IfrRef = document.getElementById('mask');
DivRef.style.visibility="visible";
if (IfrRef){
	IfrRef.style.width = DivRef.offsetWidth;
IfrRef.style.height = DivRef.offsetHeight;
IfrRef.style.top = DivRef.style.top;
IfrRef.style.left = DivRef.style.left;
IfrRef.style.zIndex = DivRef.style.zIndex - 1;
IfrRef.style.display = "block";
}
}

// needed?
function hide(e)
{
var DivRef=document.getElementById(e);
var IfrRef = document.getElementById('mask');
DivRef.style.visibility="hidden";
if (IfrRef){
	IfrRef.style.display = "none";
}
}


function deletemsg(item){
	msg2='Are you sure you want to delete '+item;
	response=confirm(msg2);
	if (response){
		return true;
	}else{
		return false;
	}
}

function round(value, dp) {
	if(dp < 0) {
		return Math.round(value*Math.pow(10,-dp))/Math.pow(10,-dp);
	} else {
		return Math.round(value/Math.pow(10,dp))*Math.pow(10,dp);
	}
}


function toggle(){
	var DivRef=document.getElementById("tip");
		var IfrRef = document.getElementById('mask');
	if (DivRef.style.visibility=="visible") {
		DivRef.style.visibility="hidden";
		DivRef.style.display = "none";
		IfrRef.style.display = "none";

	} else {
		DivRef.style.visibility="visible";
		DivRef.style.display = "block";
		IfrRef.style.width = DivRef.offsetWidth;
		IfrRef.style.height = DivRef.offsetHeight;
		IfrRef.style.top = DivRef.style.top;
		IfrRef.style.left = DivRef.style.left;
		IfrRef.style.zIndex = DivRef.style.zIndex - 1;
		IfrRef.style.display = "block";
	}
}
function SwitchDisplay(obj){
	if(document.getElementById){
	var el = document.getElementById(obj);
	var ar = document.getElementById("masterdiv").getElementsByTagName("span"); //DynamicDrive.com change
		if(el.style.display != "block"){ //DynamicDrive.com change
			for (var i=0; i<ar.length; i++){
				if (ar[i].className=="highlightbox") //DynamicDrive.com change
				ar[i].style.display = "none";
			}
			el.style.display = "block";
		}else{
			el.style.display = "none";
		}
	}
}


function imgScale(elm, h, w) {
if((elm.width/elm.height)>(w/h)) {elm.width=w;} else {elm.height=h;}
}

function printPreview(page) {

	window.open("?page=print_"+page+"&id="+ses_id, "printpreview", "width=855,height=740");
}

function correctPNG() { // correctly handle PNG transparency in Win IE 5.5 or higher.
	for(var i=0; i<document.images.length ; i++) {
		var img = document.images[i]
		var imgName = img.src.toUpperCase()
		if (imgName.substring(imgName.length-3, imgName.length) == "PNG") {
			var imgID = ( img.id) ? "id='" + img.id + "' " : ""
			var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			var imgStyle = "display:inline-block;" + img.style.cssText
			var imgAttribs = img.attributes;
			for (var j=0; j<imgAttribs.length; j++) {
				var imgAttrib = imgAttribs[j];
				if (imgAttrib.nodeName == "align") {
					if (imgAttrib.nodeValue == "left") imgStyle = "float:left;" + imgStyle
					if (imgAttrib.nodeValue == "right") imgStyle = "float:right;" + imgStyle
					break
				}
			}
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			strNewHTML += " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			strNewHTML += "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			strNewHTML += "(src='" + img.src + "', sizingMethod='scale');\""
			strNewHTML += " onmouseover=\"PNGswap('" + img.id + "');\" onmouseout=\"PNGswap('" + img.id +"');\""
			strNewHTML += "><\/span>"
			img.outerHTML = strNewHTML
			i = i-1
		}
	}
}


function PNGswap(myID) {
	var strOver  = "_on"
	var strOff = "_off"
	var oSpan = document.getElementById(myID)
	if(oSpan != null) {
		var currentAlphaImg = oSpan.filters(0).src
		if (currentAlphaImg.indexOf (strOver) != -1)
			oSpan.filters(0).src = currentAlphaImg.replace(strOver,strOff)
		else
			oSpan.filters(0).src = currentAlphaImg.replace(strOff,strOver)
	}
}
//this section handles the column moving for desktop config
function MoveOption(Source, Target) {
	objSourceElement=document.getElementById(Source);
	objTargetElement=document.getElementById(Target);
	var aryTempSourceOptions = new Array();
	var x = 0;

	//looping through source element to find selected options
	for (var i = 0; i < objSourceElement.length; i++) {
		if (objSourceElement.options[i].selected) {
			//need to move this option to target element
			var intTargetLen = objTargetElement.length++;
			objTargetElement.options[intTargetLen].text = objSourceElement.options[i].text;
			objTargetElement.options[intTargetLen].value = objSourceElement.options[i].value;
		}
		else {
			//storing options that stay to recreate select element
			var objTempValues = new Object();
			objTempValues.text = objSourceElement.options[i].text;
			objTempValues.value = objSourceElement.options[i].value;
			aryTempSourceOptions[x] = objTempValues;
			x++;
		}
	}

	//resetting length of source
	objSourceElement.length = aryTempSourceOptions.length;
	//looping through temp array to recreate source select element
	for (var i = 0; i < aryTempSourceOptions.length; i++) {
		objSourceElement.options[i].text = aryTempSourceOptions[i].text;
		objSourceElement.options[i].value = aryTempSourceOptions[i].value;
		objSourceElement.options[i].selected = false;
	}
}
function moveUpList(e) {
	target=document.getElementById(e);
	if ( target.length != -1) { // If the list is empty
		var selected = target.selectedIndex;
		if ((selected >0)&&(target.length != 0 ) ) {


// Get the text/value of the one directly above the hightlighted entry as well as the highlighted entry; then flip them

			var moveText1 = target[selected-1].text;
			var moveText2 = target[selected].text;
			var moveValue1 = target[selected-1].value;
			var moveValue2 = target[selected].value;
			target[selected].text = moveText1;
			target[selected].value = moveValue1;
			target[selected-1].text = moveText2;
			target[selected-1].value = moveValue2;
			target.selectedIndex = selected-1; // Select the one that was selected before
		} // Ends the check for there being something selected
	} // Ends the check for there being none in the list
}
function moveDownList(e) {
	target=document.getElementById(e);
	if ( target.length != -1) { // If the list is empty
		var selected = target.selectedIndex;
		if ((selected <target.length-1)&&(selected >-1)&&(target.length != 0 ) ) {


// Get the text/value of the one directly above the hightlighted entry as well as the highlighted entry; then flip them

			var moveText1 = target[selected+1].text;
			var moveText2 = target[selected].text;
			var moveValue1 = target[selected+1].value;
			var moveValue2 = target[selected].value;
			target[selected].text = moveText1;
			target[selected].value = moveValue1;
			target[selected+1].text = moveText2;
			target[selected+1].value = moveValue2;
			target.selectedIndex = selected+1; // Select the one that was selected before
		} // Ends the check for there being something selected
	} // Ends the check for there being none in the list
}

function selall(list) {
	for (var j=0; j<arguments.length; j++){
		target=document.getElementById(arguments[j]);
		for(var i = 0;i < target.length;i++){
			target.options[i].selected = true;
		}
	}
}

function openNote(e,nid){
	if (!e) var e = window.event;
	document.getElementById(nid).style.display='inline';
	w=100;
	x=e.clientX;
	sX= document.body.scrollLeft;
	y=e.clientY + document.body.scrollTop;
	winX=document.body.offsetWidth;
	if ((x+w)>winX){
	document.getElementById(nid).style.top=(y-10);
	document.getElementById(nid).style.left=(x+sX-w-10);

	}else{
	document.getElementById(nid).style.top=(y-10);
	document.getElementById(nid).style.left=(x+sX+10);
	}
}

function cipher(str, dir) {
	var newStr = "";
	for(i = 0; i < str.length; i++) {
		newStr += String.fromCharCode(str.charCodeAt(i) + (dir ? 1 : -1));
	}
	return newStr;
}

function clearDateField(e, o) {
	var keynum;

	keynum = e.keyCode;
	if(e.which) { // Netscape/Firefox/Opera
		keynum = e.which;
	}

	if(keynum == 46) {
		o.value = "";
	}
}

function edSetEdit(num) {
	for(i = 1; i <= num; i++) {
		e = document.getElementById('e' + i);
		d = document.getElementById('d' + i);
		if(e) {
			if(e.style.display == 'none') {
				e.style.display = 'block';
			} else {
				e.style.visibility = 'visible';
			}
		}
		if(d) { d.style.display = 'none'; }
	}
}


// Popdiv functions

function getScrollHeight() {
	var h = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
	return h ? h : 0;
}
function getWindowHeight() {
	var h = window.innerHeight || document.body.clientHeight || document.documentElement.clientHeight;
	return h ? h : 0;
}
function getWindowWidth() {
	var w = window.innerWidth || document.body.clientWidth || document.documentElement.clientWidth;
	return w ? w : 0;
}

/*******************************
* show_popdiv
*
* Displays a popdiv
*
* string id - the "id" of the popdiv
* int x - the x-offset of the popdiv, -1 for centered, null for don't change
* int y - the y-offset of the popdiv, -1 for centered, null for don't change
* boolean modal - Whether or not to show a modal div (grey out background)
*/
function show_popdiv(id, x, y) {
	e = document.getElementById(id);

	if(e) {
		e.style.display = "block";
		if(y != null) { e.style.top = getScrollHeight() + (y == -1 ? ((getWindowHeight() - e.clientHeight) / 2) : y) + "px"; }
		if(x != null) { e.style.left = (x == -1 ? ((getWindowWidth() - e.clientWidth) / 2) : x) + "px"; }
	}
}

/*******************************
* show_popdiv_atcursor
*
* Displays a popdiv based on the cursor position
*
* object ev - the event object of the click
* string id - the "id" of the popdiv
* int x - the x-offset of the popdiv relative to the click
* int y - the y-offset of the popdiv relative to the click
*/
function show_popdiv_atcursor(ev, id, x, y) {
	ev = ev || window.event; // IE hack
	d = document.getElementById(id);

	if(ev.pageX || ev.pageY) {
		x += ev.pageX;
		y += ev.pageY;
	} else {
		x += ev.clientX + document.body.scrollLeft - document.body.clientLeft;
		y += ev.clientY + document.body.scrollTop  - document.body.clientTop;
	}

	if(d) {
		d.style.display = "block";
		d.style.top = y + "px";
		d.style.left = x + "px";

		// Make sure the popup doesn't go off the screen
		if(x + d.clientWidth > getWindowWidth()) {
			d.style.left = (getWindowWidth() - d.clientWidth - 5) + "px";
		}
		if(y + d.clientHeight > (getWindowHeight() + getScrollHeight())) {
			d.style.top = (getWindowHeight() + getScrollHeight() - d.clientHeight - 5) + "px";
		}
	}
}

function hide_popdiv(id) {
	e = document.getElementById(id);
	if(e) {
		e.style.display = 'none';
	}
	if(modaldiv) {
		modaldiv.style.display = 'none';
	}
}
