function getDocumentObject(objectId)
{
	// checkW3C DOM, then MSIE 4
	if (document.getElementById && document.getElementById(objectId)) {
		return document.getElementById(objectId);
	} else if (document.all && document.all(objectId)) {
		return document.all(objectId);
	} else {
		return false;
	}
}

function getStyleObject(objectId)
{
	// checkW3C DOM, then MSIE 4, then NN 4.
	var object = getDocumentObject(objectId);
	if (object) {
		return object.style;
	} else if (document.layers && document.layers[objectId]) {
		return document.layers[objectId];
	} else {
		return false;
	}
}

function changeObjectVisibility(objectId, newVisibility)
{
	// first get the object's stylesheet
	var styleObject = getStyleObject(objectId);

	// then if we find a stylesheet, set its visibility
	// as requested
	if (styleObject) {
		styleObject.display = newVisibility;
		return true;
	} else {
		return false;
	}
}

function switchDiv(div_id)
{
	var style_sheet = getStyleObject(div_id);
	if (style_sheet) {
		hideAll();
		changeObjectVisibility(div_id, "");
	} else {
		alert("sorry, this only works in browsers that do Dynamic HTML");
	}
}

function replaceNodeText(node, findtext, newtext)
{
	var attr = node.attributes;
	if (attr != null)
	{
		var att = getNamedItem(attr, "name");
		if (attrHasValue(att)) {
			att.nodeValue = att.nodeValue.replace(findtext, newtext);
		}

		att = getNamedItem(attr, "id");
		if (attrHasValue(att)) {
			att.nodeValue = att.nodeValue.replace(findtext, newtext);
		}

		att = getNamedItem(attr, "href");
		if (attrHasValue(att)) {
			att.nodeValue = att.nodeValue.replace(findtext, newtext);
		}
    }
	if (node.nodeName != 'select' && node.childNodes != null)
	{
		var childs = node.childNodes;
		for (var i=0; i<childs.length; i++)
		{
			try
			{
				replaceNodeText(childs.item(i), findtext, newtext);
			}
			catch (e)
			{}
		}
	}
}

function getNamedItem(attr, name)
{
	if (attr.getNamedItem)
		return attr.getNamedItem(name);
	else
		return attr[name];
}

function limit(element, maxChars)
{
	if (element.value.length > maxChars)
		element.value = element.value.substring(0,maxChars);
	return (element.value.length < maxChars);
}

function confirmLimitInChars(element, maxChars, fieldName)
{
	var node = getObj(element);
	var length = node.value.length;

	if (length > maxChars)
	{
		alert (fieldName + " is too long.  Please limit to " + maxChars + " characters.  It is currently " + length + " characters.");
		return false;
	}
}

function confirmLimitInWords(element, maxWords, fieldName)
{
	var node = getObj(element);
	var nohtml = node.value.replace(/&nbsp;/g, " ");
	nohtml.replace("\s+", " ");
	var length = nohtml.split(/\s/).length;

	if (length > maxWords)
	{
		alert (fieldName + " is too long.  Please limit to " + maxWords + " words.  It is currently " + length + " words");
		return false;
	}
}


function attrHasValue(att)
{
	return (att != null && att.nodeValue!=null && att.nodeValue.length > 0);
}

function deleteNode(node)
{
	var obj = getObj(node);
	obj.parentNode.removeChild(obj);
}

function deleteNodeById(id) 			{ deleteNode(id); }				// depracated - use deleteNode()
function hideNodeById(id) 				{ hideNode(id); } 				// depracated - use hideNode()
function showNodeById(id, confirm) 		{ showNode(id, confirm); } 		// depracated - use showNode()
function disableNodeById(id) 			{ disableNode(id); } 			// depracated - use disableNode()
function enableNodeById(id, confirm) 	{ enableNode(id, confirm); } 	// depracated - use enableNode()
function toggleNodeById(id) 			{ toggleNode(id); }  			// depracated - use toggleNode()

function showNode(node, confirm)
{
	var obj = getObj(node);
	if (obj != null) {
		if (confirm == false) {
			hideNode(obj);
		} else {
			if (obj.nodeName == 'SPAN' || obj.nodeName == 'A') obj.style.display = 'inline';
			else if (obj.nodeName == 'TR' || obj.nodeName == 'THEAD') obj.style.display = '';
			else obj.style.display = 'block';
		}
	}
}

function hideNode(node)
{
	var obj = getObj(node);
	if (obj != null) {
		obj.style.display = 'none';
	}
}

function toggleNode(node, disable)
{
	var obj = getObj(node);
	if (obj != null) {
		if (obj.style.display == 'none') {
			showNode(obj);
			if (disable) {
				enableNode(obj);
			}
		} else {
			hideNode(obj);
			if (disable) {
				disableNode(obj);
			}
		}
	}
}

function getObj(obj)
{
	if (typeof obj == "object") {
		return obj;
	} else if (typeof obj == "string") {
		return document.getElementById(obj);
	}
}

function toggleNodes(node1, node2, disable)
{
	var obj1 = getObj(node1);
	var obj2 = getObj(node2);

	if (obj1 != null) {
		if (obj1.style.display == 'none') {
			showNode(obj1);
			hideNode(obj2);
			if (disable) {
				enableNode(obj1);
				disableNode(obj2);
			}
		} else {
			showNode(obj2);
			hideNode(obj1);
			if (disable) {
				enableNode(obj2);
				disableNode(obj1);
			}
		}
	}
}

function disableNode(id)
{
	var obj = getObj(id);
	if (obj != null) {
		obj.disabled = true;
	}
}
function enableNode(id, confirm)
{
	var obj = getObj(id);
	if (obj != null) {
		if (confirm == false) {
			disableNode(obj);
		} else {
			obj.disabled = false;
		}
	}
}

function countSiblingNodes(id)
{
	var node = document.getElementById(id);
	return node.parentNode.childNodes.length;
}

var gSubmited = false;
var submitIntervalId;
function jobsterSubmitHandler(form) {
	if (gSubmited) {
		//alert('canceling submit');
		return false;
	}
	gSubmited = true;
	submitIntervalId = window.setInterval("enableSubmit()", 5000);
	return true;
}

function enableSubmit()
{
	window.clearInterval(submitIntervalId);
	gSubmited = false;
}

function showHoverTip(id, left)
{
	var divRef = document.getElementById('ht_' + id);
	var iframeRef = document.getElementById('htIframe_' + id);

	divRef.style.display = "block";
	if (left != null)
	{
		divRef.style.left = "-" + (divRef.offsetWidth + 14) + "px";
		if (iframeRef != null)
			iframeRef.style.left =  "-" + (divRef.offsetWidth + 14) + "px";
    }

    if (iframeRef != null)
    {
		iframeRef.style.width = divRef.offsetWidth;
		iframeRef.style.height = divRef.offsetHeight;
		iframeRef.style.zIndex = divRef.style.zIndex - 1;
		iframeRef.style.display = "block";
	}
}

function hideHoverTip(id)
{
	var divRef = document.getElementById('ht_' + id);
	var iframeRef = document.getElementById('htIframe_' + id);

	divRef.style.display = "none";
	if (iframeRef != null)
		iframeRef.style.display = "none";
}

function splitLongWords(string, strLength)
{
	if (strLength == null)
	{
		return string;
	}

	var newString = '';
	var wordsArray = string.split(" ");
	for (i = 0; i < wordsArray.length; i++)
	{
		if (wordsArray[i].length > strLength)
		{
			newString += wordsArray[i].substring(0, strLength) + " " +
					( ( wordsArray[i].substring(strLength).length > strLength ) ?
						( splitLongWords(wordsArray[i].substring(strLength), strLength) ) :
						( wordsArray[i].substring(strLength) + " " ) );
		}
		else
		{
			newString += wordsArray[i] + " ";
		}
	}
	return newString;
}


function toggleAllCheckboxes(masterCheckbox, descriptor ) {

	if (typeof(descriptor) == 'string' )          // name of checkboxes - just toggle those with this name
	{
		for (var i = 0; i < document.getElementsByName(descriptor).length; i++) {
			if (!document.getElementsByName(descriptor)[i].disabled) { // don't select disabled checkboxes
				document.getElementsByName(descriptor)[i].checked = masterCheckbox.checked;
			}
		}
	}
	else if (typeof(descriptor) == 'object')    // form object - just toggle those in the form
	{
		var form = descriptor;
		var inputs = form.elements;
		for(i=0; i<inputs.length; i++)
		{
			if(inputs[i].type=="checkbox" && !inputs[i].disabled)
			{
				 inputs[i].checked = masterCheckbox.checked;
			}
		}
	}
	else // null, toggle all checkboxes
	{
		var inputs = document.getElementsByTagName("input");

		for(i=0;i<inputs.length;i++)
		{
			if(inputs[i].type=="checkbox" && !inputs[i].disabled)
			{
				 inputs[i].checked = masterCheckbox.checked;
			}
		}
	}
}

function countCheckedBoxes(boxNames) {
	var totalSet = 0;

	for (var i = 0; i < document.getElementsByName(boxNames).length; i++) {
		if (document.getElementsByName(boxNames)[i].checked == true) {
			totalSet++;
		}
	}
	return totalSet;
}

// Replace smart quotes and other special characters
String.prototype.replaceSmartCharacters = function()
{
	var result = this;

	var regexApos = eval('/' + unescape("%145") + '|' +
							unescape("%u2018") + '|' +
							unescape("%u2019") + '/g');

	var regexQuot = eval('/' + unescape("%u201c") + '|' +
							unescape("%146") + '|' +
							unescape("%u201d") + '|' +
							unescape("%147") + '|' +
							unescape("%148") + '/g');

	var regexDash = eval('/' + unescape("%u2014") + '|' +
							unescape("%u2013") + '|' +
							unescape("%u2022") + '|' +
							unescape("%151") + '/g');

	var regexElip = eval('/' + unescape("%u2026") + '/g');


	result = result.replace(regexApos, "'"); // replace single quotes
	result = result.replace(regexQuot, "'"); // replace double quotes
	result = result.replace(regexDash, "-"); // replace dashes
	result = result.replace(regexElip, "..."); // replace elipse
	result = result.replace(/[ ]+/g, " "); // replace no break spaces
	result = result.replace(/ +/g, " "); // replace no break spaces

	return result;
}

function goTo(url)
{
	window.location = url;
}

function copyToClipboard(idName) {
	var id = document.getElementById(idName);
	if (id.nodeName.toLowerCase() != 'textarea') return;
	id.focus();
	id.select();
	if (document.selection) {
		var copiedTxt = document.selection.createRange();
		copiedTxt.execCommand("Copy");
	}
}


function getBrowserWidth()
{
	if (navigator.userAgent.indexOf("MSIE") > 0)
		return(document.body.offsetWidth);
	else
		return window.outerWidth;
}

function getBrowserHeight()
{
	if (navigator.userAgent.indexOf("MSIE") > 0)
		return(document.documentElement.clientHeight);
	else
		return(window.outerHeight);
}

function launch(file, name, width, height, showMenus, showScrollbars)
{
	var X = getBrowserWidth() / 2;
	var Y = getBrowserHeight() / 2;
	if (showMenus == null)
		showMenus="no";
	if (showScrollbars == null)
		showScrollbars="yes";
	if (width == null)
		width = getBrowserWidth() - 100;
	if (height == null)
		height = getBrowserHeight() - 100;

	var string= "top=" + (window.screenTop + (Y - (height/2))) + ",left=" + (window.screenLeft + (X - (width/2)))  + ",width=" + width + ",height=" + height + ",toolbar=no,scrollbars=" + showScrollbars + ",directories=no,menubar=" + showMenus + ",resizable=yes,dependent=yes";
	if (name != null)
		name = name.replace(" ", "_");
	else
		name = "newWindow";

	var hwnd = window.open(file, name, string);
	if (window.focus)
		hwnd.focus();

}

function addLoadEvent ( func )
{
	var oldonload = window.onload;

	if (typeof window.onload != 'function')
	{
		window.onload = func;
	}
	else
	{
		window.onload =
			function()
			{
				oldonload();
				func();
			}
	}
}

String.prototype.trim = function() {
	// Strip leading and trailing white-space
	return this.replace(/^\s*|\s*$/g, "");
}

function blind(id, down, duration)
{
	if (duration == null)
		duration = .5;
	if ( getObj(id) )
	{
		down ? Effect.BlindDown(id, {duration: duration}) : Effect.BlindUp(id, {duration: duration});
	}
}

function getURLParam(paramName){
	var value = null;
	var href = window.location.href;
	if ( href.indexOf("?") > -1 ){
		var queryString = href.substr(href.indexOf("?")).toLowerCase();
		var queryStringArray = queryString.split("&");
		for ( var i = 0; i < queryStringArray.length; i++ ){
			if (queryStringArray[i].indexOf(paramName + "=") > -1 ){
				var nameValuePair = queryStringArray[i].split("=");
				value = nameValuePair[1];
				break;
			}
		}
	}
	return value;
}


/**
 * Sets a Cookie with the given name and value.
 *
 * name       Name of the cookie
 * value      Value of the cookie
 */
function setCookie(name, value)
{
	var expires = new Date();
	expires.setTime(expires.getTime() + (1000 * 60 * 60 * 24 * 365));  // 1 year later
	document.cookie = name + "=" + escape(value) + "; expires=" + expires.toGMTString() +  "; path=/";
}

function setSessionCookie(name, value)
{
	document.cookie = name + "=" + escape(value) + "; path=/";
}

/**
 * Gets the value of the specified cookie.
 *
 * name        Name of the cookie.
 *
 * Returns a string containing the value of specified cookie, null if it does not exist.
 */
function getCookie(name)
{
	cookie_name = name + "=";
	cookie_length = document.cookie.length;
	cookie_begin = 0;
	while (cookie_begin < cookie_length)
	{
		value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring(cookie_begin, value_begin) == cookie_name)
		{
			var value_end = document.cookie.indexOf (";", value_begin);
			if (value_end == -1)
			{
				value_end = cookie_length;
			}
			return unescape(document.cookie.substring(value_begin, value_end));
		}
		cookie_begin = document.cookie.indexOf(" ", cookie_begin) + 1;
		if (cookie_begin == 0)
		{
			break;
		}
	}
	return null;
}

/**
 * Deletes the specified cookie.
 *
 * name         Name of the cookie
 */
function deleteCookie(name)
{
	if (getCookie(name))
	{
		document.cookie = name + "=" + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

/**
 * Throw error when user inputs two consecutive logical operators in Keywords field.
 *
 * name         keywords
 */
function checkForConsecutiveLogicalOperators(title)
{
	var x=0;
	var newSplitString = new Array();
	var splitString=title.split(" ");
	var flag=0;
	for(var i=0;i<splitString.length;i++)
	{		
		if(splitString[i]!="")
		{		
			newSplitString[x]=splitString[i];
			x++;
		}		
	}
	
	for(var y=0;y<newSplitString.length;y++)
	{	
		if((newSplitString[y]=="AND"|| newSplitString[y]=="OR"
		|| newSplitString[y]=="NOT" || newSplitString[y]=="and" || newSplitString[y]=="or" || newSplitString[y]=="not") && ( newSplitString[y+1]=="AND"||
		newSplitString[y+1]=="OR"||
		newSplitString[y+1]=="NOT" || newSplitString[y+1]=="and"  || newSplitString[y+1]=="or"  || newSplitString[y+1]=="not"))
		{			
			flag++;
			break;
		}		
	}
	if(flag > 0)
	{
		alert('Specifying consecutive logical operators in Keywords field is not valid. Please correct the Keywords entered.');
		return false;
	}
}
