/*
 * For pop-ups
 */
/*
//MOVED TO modal.js
function getTop() {
  if (window.screenY) return window.screenY;
  return window.screenTop;
}

function getLeft() {
  if (window.screenX) return window.screenX;
  return window.screenLeft;
}

function openWindow(link, height, width) {
 openWindowAt(link, height, width, 25, 25);
}

function openWindowAt(link, height, width, x, y) {
 var t = getTop() + x, l = getLeft() + y;
 var winname = 'uniquePopup' + (new Date()).getTime();
 var options = 'menubar=no,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',top=' + t + ',left=' + l;
 w = window.open(link, winname, options);
 w.focus();
}

//UNUSED
function openPrintWindowAt(link, height, width, x, y) {
 var t = getTop() + x, l = getLeft() + y;
 var winname = 'uniquePopup' + (new Date()).getTime();
 var options = 'menubar=yes,scrollbars=yes,resizable=yes,width=' + width + ',height=' + height + ',top=' + t + ',left=' + l;
 w = window.open(link, winname, options);
 w.focus();
}
*/

/*
 * Generic form submission
 */
function formSubmit(form, perform) {
	if (form && form.action) {
		form.action.value = perform;
		form.submit();
	}
}
function validFormSubmit(form, perform) {
	if (form && form.action && isValid(form)) {
		formSubmit(form, perform);
	}
}

/*
 * For the MListing
 */
function deactivateSelected(form) {
	validFormSubmit(form, 'deactivated');
}
function checkAllDeactivated(cb) {
	if (cb && cb.form) {
		var o = eval('cb.form.deactivate_1_1');
		for (var i=2; o; i++) {
			if (!o.disabled) o.checked = cb.checked;
			o = eval('cb.form.deactivate_' + i + '_1');
		}
	}
}
function isValid(form) {
	var o = eval('form.deactivate_1_1');
	for (var i=2; o; i++) {
		if (o.checked) {
			return 1;
		}
		o = eval('form.deactivate_' + i + '_1');
	}
	return 0;
}


/*
 * For custom dynamic navs
 */
var onSelfAttr="kOnSelfAttr";
function showDynamicNav(nav, self, parent) {
	var n = document.getElementById(nav);
	if (n) {
		if(self) {
			n.setAttribute(onSelfAttr, true);
		}
		else {
			n.setAttribute(onSelfAttr, false);
		}
		
		if (parent) {
			var pos = getPosition(parent);
			//mozilla
			n.setAttribute("style", "left:"+pos.x+"px"+";top:"+(pos.x+parent.offsetHeight)+"px;");
			//ie
			n.style.left = pos.x + "px";
			n.style.top = (pos.y + parent.offsetHeight + 2) + 'px';
		}
		
		for(var i = 0; i < n.childNodes.length; i++) {
			if(!(n.childNodes[i].nodeName == "#text".toString())) {
				if(!n.childNodes[i].type || n.childNodes[i].type.toString() != "hidden") {
					n.style.display = 'block';
					return;
				}
			}
		}

		n.style.display = 'none';
	}
}
function tryHide(nav) {
	var id = window.setTimeout("hideDynamicNav('"+nav+"', false);", 250);
}
function forceHideDynamicNav(nav) {
	var n = document.getElementById(nav);
	if(n) {
		n.style.display = 'none';
	}
}
function hideDynamicNav(nav, self) {
	var n = document.getElementById(nav);
	var onSelf = n.getAttribute(onSelfAttr);
	if(n && (self || (onSelf != null && "false" == onSelf.toString()))) {
		n.style.display = 'none';
		n.setAttribute(onSelfAttr, false);
	}
}
function getPosition(e){
	var left = 0;
	var top  = 0;
	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}
	left += e.offsetLeft;
	top  += e.offsetTop;
	return {x:left, y:top};
}

/* TextArea maximize */
function maximize(textarea) {
	var ta = document.getElementById(textarea);
	if (ta) {
		var height = window.innerHeight, width = window.innerWidth;
		if (!height && !width && document.body) {
			height = document.body.offsetHeight, width = document.body.offsetWidth;
		}
		ta.style.position = 'absolute';
		ta.style.width = width + 'px';
		ta.style.height = height + 'px';
		ta.style.top = 0;
		ta.style.left = 0;
		var m = document.getElementById('maximizeTextArea');
		m.style.display = 'none';
		m = document.getElementById('minimizeTextArea');
		m.style.display = 'block';
		m.style.position = 'absolute';
		m.style.top = '8px';
		m.style.left = (width - 84) + 'px';
	}
}
function minimize(textarea) {
	var ta = document.getElementById(textarea);
	if (ta) {
		ta.style.position = 'relative';
		ta.style.width = '';
		ta.style.height = '';
		var m = document.getElementById('maximizeTextArea');
		m.style.display = 'block';
		m = document.getElementById('minimizeTextArea');
		m.style.display = 'none';
		m.style.position = 'absolute';
	}
}
function showMaximize(ta) {
	var m = document.getElementById('maximizeTextArea');
	if (ta && m) {
		var pos = getPosition(ta), width = pos.x + ta.offsetWidth - 84, height = pos.y + 8;
		if (!document.all) height = height + 20, width = width + 16;
		m.style.top = height + 'px';
		m.style.left = width + 'px';
		m.style.display = 'block';
	}
}
function hideMaximize() {
	var m = document.getElementById('maximizeTextArea');
	if (m) m.style.display = 'none';
}

/* Script for setting proper height on an iFrame */
function setFrameHeight(frameName, frameIndex) {
	var frame = document.getElementById(frameName);
	if (frame.document && frame.document.body) frame.style.height = frame.document.body.offsetHeight;
	else frame.style.height = document.body.clientHeight + 'px';
}
