var saved1, saved2;

String.prototype.trim = function (tabs) {
	if (tabs)
		return this.replace(/^(\s+|\t+)+|(\s+|\t+|)+$/g, '');
	else
		return this.replace(/^\s+|\s+$/g, '');
};

function doHot(item) {
//	saved1 = document.getElementById(item).style.backgroundColor;
//	saved2 = document.getElementById(item).style.color;
//	document.getElementById(item).style.backgroundColor = '#664433';
//	document.getElementById(item).style.color = '#fff';
}

function doNot(item) {
//	document.getElementById(item).style.backgroundColor = saved1;
//	document.getElementById(item).style.color = saved2;
//	document.getElementById(item).style.backgroundColor = '#FFFFFF';
//	document.getElementById(item).style.color = '#664433';
}

function openValidationSelector(sess) {
	window.open("ValidationOptions.tem?SessionID=" + sess, "intrview","width=700 height=400 top=100 left=100,toolbar=0,menubar=0,personalbar=0,status=0,scrollbars=1,resizable=1");
}

function specialAnswersVisible(control) {
	o = document.getElementById(control).style;
	if (eval('document.datainput.Has' + control + '.checked'))
		o.visibility = "visible"
	else
		o.visibility = "hidden";
}

function checkPosting() {
	if (document.datainput.posting.value=='1') {
		alert('busy...');
		return false;
	}
	document.datainput.posting.value='1';
	return true;
}

function go() {
	if (checkDataLabel())
		document.datainput.submit();
}

function editFilters(s) {
	window.open("editFilters.tem?sessionid=" + s, "intrview","width=600 height=500 top=200 left=200,toolbar=0,menubar=0,personalbar=0,status=0,scrollbars=1,resizable=1");
}

function baseFrame() {
	var n = 0;
	var o = window;
	while (o.parent && o.name != 'base' && (o != o.parent)) {
		o = o.parent;
	}
	if (!o.framelinks) {
		o.framelinks = {};
	}
	if (!o.framelinks.lwdbott) {
		// document not loaded yet. Return dummy object to avoid javacript errors...
		o.framelinks.lwdbott = {document: {F: {Q: {value: null}}}};
	}
	return o;
}

function setPane(nr) {
	for (cnt = 1; cnt <= 4; cnt++) {
		o = document.getElementById('pane' + cnt);
		if (o)
			o.style.display = (nr == cnt)?'block':'none';
	}
	o = document.getElementById('empty');
	o?o.focus():'';
}

function setPanePostback(pane, sessionID) {
	document.location.replace('?pane=' + pane + '&sessionid=' + sessionID);
}

function setSubPane(nr) {
	for (cnt = 1; cnt <= 4; cnt++) {
		o = document.getElementById('subpane' + cnt);
		if (o)
			o.style.display = (nr == cnt)?'block':'none';
	}
	o = document.getElementById('empty');
	o?o.focus():'';
}

function setSubPanePostback(subpane, sessionID) {
	document.location.replace('?subpane=' + subpane + '&sessionid=' + sessionID);
}

function bold() {
	if (!document.selection)
		return;
	str = document.selection.createRange().text;
	if (str=="")
		return;
	document.selection.createRange().text = "<b>" + str + "</b>";
}

function underline() {
	if (!document.selection)
		return;
	str = document.selection.createRange().text;
	if (str=="")
		return;
	document.selection.createRange().text = "<u>" + str + "</u>";
}

function italic() {
	if (!document.selection)
		return;
	str = document.selection.createRange().text;
	if (str=="")
		return;
	document.selection.createRange().text = "<i>" + str + "</i>";
}

function cleanupBullets(field, makebreaks) {
	if (!makebreaks)
		var aStr = document.getElementById(field).value.split("\n")
	else
		var aStr = [document.getElementById(field).value];
	var result = '';
	var lines = 0;
	for (cnt = 0; cnt < aStr.length; cnt++) {
		if (aStr[cnt].replace(/\r/g, '').replace(/\t/g, '').replace(/ /g, '') == '')
			continue;
		lines += 1;
		var pos = aStr[cnt].indexOf('\t');
		if (pos >= 0 && pos < 10) {
			str = aStr[cnt].substring(pos + 1)
			var pos = str.indexOf('\t');
			if (str.length >= 5 && (pos > str.length-5)) { // format bullet	brandname	1, strip trailing 1
				str = str.substring(0, pos);
			}
		} else {
			strtmp = aStr[cnt].replace(/^\S{1,5}(\s){2,}(\S*(\s+\S+)*)\s*$/, '$2');
			str = strtmp?strtmp:aStr[cnt];
		}
		str = str.trim();
		if ((str != '') && (str  != '\r') && str != ('\n'))
			result += str + '\n';
	}
	result = result.replace(/[\s\r\n]+$/, '');
	if ((makebreaks) && (str.indexOf('<br />') == -1)) {
		result = result.replace(/\r?\n/g, '<br />\n');
	} else {
		if (lines == 1) { // headers ended op on 1 line, tab delimited?
			result = aStr[0].replace(/\t/g, '\n');
		}
	}
	//result = result.replace(/?|?/g, "'");
	str.replace(/(<br \/>)+$/, '');
	document.getElementById(field).value = result;
}

function runOnloadHooks() {
	if (!window.onloadHooks)
		return;
	for (var cnt = 0; cnt < window.onloadHooks.length; cnt++) {
		window.onloadHooks[cnt]();
	}
}

function registerOnloadHook(func) {
	if (!window.onloadHooks) {
		window.onloadHooks = new Array();
		if (window.onload) {
			registerOnloadHook(window.onload);
		}
	}
	window.onloadHooks[window.onloadHooks.length] = (typeof func == 'function')?func:new Function(func);
	window.onload = runOnloadHooks;
}

function getStyleVal(source_id, IEStyleName, CSSStyleName) {
	if (typeof source_id == 'object')
		var elem = source_id
	else
		var elem = document.getElementById(source_id);
	if (elem.currentStyle) {
		result = elem.currentStyle[IEStyleName];
	} else if (window.getComputedStyle) {
		var compStyle = window.getComputedStyle(elem, "");
		result = compStyle.getPropertyValue(CSSStyleName);
	} else {
		result = "";
	}
	return result;
}

function getPageXY (e) {
	x = y = 0; //global helper vars
	w = e.offsetWidth;
	h = e.offsetHeight;
	while (e) {
		if (getStyleVal(e, 'position') == 'relative')
			break;
		x += parseInt(e.offsetLeft);
		y += parseInt(e.offsetTop);
		e = e.offsetParent || null;
	}
	return {x: x, y: y, w: w, h: h};
}
