// This file requires prototype to be included in the template using this file.

var ALL = -1;
var TAGNAME = 0;
var TAGID = 1;
var TAGCLASS = 2;
var TAGCSS= 3;
var TAGINNERHTML = 4;

// These are enabled by iGRaspHelper.asp and are used to make sure dependencies are properly enabled.
var dropdown = false;

// element locators

function checkDOMObjectExists(identifier, identifier_type, restriction) // wrapper for getDOMHash gives boolean true if the DOM object exists
{
	var ret = false;
	element = getDOMHash(identifier, identifier_type, restriction);
	if (element.size() > 0)
	{
		ret = true;
	}
	return ret;
}

function getDOMHash(identifier, identifier_type, restriction) // returns a DOM object or objects wit/h the prototype hash.
{
	var element = null;
	var tagname = null;
	switch(identifier_type)
	{
		case TAGID:
			element = $(identifier);
			break;
		case TAGCLASS:
			element = $$("."+identifier);
			break;
		case TAGINNERHTML:
			element = new Array();
			elements = $$(identifier).each(function(el){
			if (compareString(el, restriction, true))
			{
				element.push(el);
			}
			});
			break;
		default:
			element = $$(identifier);
			break;
	}
	return element;
}

// String functions
function compareString(checklocation, checkstring, casesensitive) // checks if checkstring is the same as in the object checklocation, pass a prototype hash object or regular dom object, casesensitive set to false will mean case differences will not fail check
{
	var ret = false;
	if (!casesensitive) 
	{
		checkstring.toUpperCase();
		checklocation.toUpperCase();
	}
	if (checklocation.innerHTML == checkstring)
	{
		ret = true;
	}
	return ret;
}

function replaceHTML(htmlcontent, containertype, containeroptionsarray, replaceelement) // allows adding html (htmlcontent) in a container of html type (containertype) with the options of the container (containeroptionsarray) replacing the content of replaceelement.
{
	var containeroptions = "";
	var newcontent = new Element(containertype, containeroptionsarray);
	newcontent.update(htmlcontent);
	$$(replaceelement).each(function(el){
	el.update(newcontent);
	});
}

function appendHTML(htmlcontent, containertype, containeroptionsarray, replaceelement) // allows adding html (htmlcontent) in a container of html type (containertype) with the options of the container (containeroptionsarray) replacing the content of replaceelement.
{
	var containeroptions = "";
	var newcontent = new Element(containertype, containeroptionsarray);
	newcontent.update(htmlcontent);
	$$(replaceelement).each(function(el){
	el.insert(newcontent);
	});
}

//general functions

//redirects the page
function redirect(location)
{
	 window.location = location;
}

// adds the IE dropdown fix
function addDropdownExtender(elementid, pageSection)
{
	if (dropdown)
	{
		YAHOO.Hack.FixIESelectWidth(elementid, pageSection);
	}
}

// updates the agency jobapplylink href link so the referral agency can be changed
function updateAgency(applyURLSuffix) {
	if (applyURLSuffix != "INVALID" && applyURLSuffix != "") 
	{
		var qStr = applyURLSuffix;
		var anchors = document.getElementById("jobapplylink").getElementsByTagName("a");
		if (anchors && qStr != "0") {
			for (i=0, j=anchors.length; i < j; i++) {
				var url = anchors[i].href.split("?")[0];
				anchors[i].href = url+qStr;
			}
		}
	}
}

// functions that are performed on rows

function addRowId(uniqueElementId, newIdString) //takes an elements id inside the row and works its way up to the row and then sets the id of the row to newIdString.
{
	$(uniqueElementId).up('tr').writeAttribute('id',newIdString);
}

function swapTableRows(fromRow, toRow) { // fromRow, toRow are ids of the rows to be swapped
	var fromRowHolder = $(fromRow).replace($(toRow).innerHTML);
	$(toRow).replace(fromRowHolder);
}

function moveTableRow(moveRow, beforeRow) { // moveRow (id) is moved to before beforeRow (id)
	var moveRowTransport = $(moveRow).remove();
	Element.insert($(beforeRow),{'before':moveRowTransport});
}

function hideRow(identifier, identifier_type, restriction)
{
	var element = null;
	var tagname = null;
	var processElement = true;
	switch(identifier_type)
	{
		case TAGID:
			element = $(identifier);
			break;
		case TAGCLASS:
			element = $$("."+identifier);
			break;
		case TAGINNERHTML:
			elements = $$(identifier).each(function(el){
				if (compareString(el, restriction, true))
				{
					hideElementAncestor(el, "tr");
				}
			});
			processElement = false;
			break;
		default:
			element = $$(identifier);
			break;	
	}
	if (processElement == true)
	{
		if (Object.isArray(element))
		{
			var nodes = $A(element);
			nodes.each(function(node){
				hideElementAncestor(node, "tr");	
			});
		}
		else 
		{
			hideElementAncestor(element, "tr");	
		}
	}
}

// any element functions

function hideElementAncestor(element, ancestor)
{
	if (element != null && ancestor != null)
	{
		element.up(ancestor).hide();
	}
}

function setElementIdByInnerText(tagName, innerText, newid) //used with iGRaspHelper trans method, can be used to find element based on trans id.
{
	getDOMHash(tagName, TAGINNERHTML, innerText).each(function(el){
	el.writeAttribute('id',newid);
	});
}

function hideEmptyJobDescriptionTags()
{
	getDOMHash("p", TAGINNERHTML, "").each(function(el){
	el.hide();
	});
	getDOMHash("div.header span.general_bold", TAGINNERHTML, "").each(function(el){
	el.ancestors()[0].hide();
	});
}