// 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 (compareInnerHTMLString(el, restriction, true))
			{
				element.push(el);
			}
			});
			break;
		default:
			element = $$(identifier);
			break;
	}
	return element;
}

// String functions
function compareInnerHTMLString(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 (null == checklocation || null == checkstring)
	{}
	else
	{
		if (!casesensitive) 
		{
			checkstring.toUpperCase();
			checklocation.toUpperCase();
		}
	}
	if (checklocation.innerHTML == checkstring)
	{
		ret = true;
	}
	return ret;
}

function compareString(checkstring1, checkstring2, 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 (null != checkstring1 && null != checkstring2)
	{
		if (!casesensitive) 
		{
			checkstring1 = checkstring1.toUpperCase();
			checkstring2 = checkstring2.toUpperCase();
		}
	}
	if (checkstring1 == checkstring2)
	{
		ret = true;
	}
	return ret;
}

function checkSubstring(checkstring, checksubstring, casesensitive)
{
	var ret = false;
	if (null != checkstring && null != checksubstring)
	{
		if (!casesensitive) 
		{
			checkstring = checkstring.toUpperCase();
			checksubstring = checksubstring.toUpperCase();
		}
	}
	if (checkstring.include(checksubstring))
	{
		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 (compareInnerHTMLString(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();
	});
}

function setElementIdByTitle(tagName, title, newid)
{
	getDOMHash(tagName, TAGNAME, "").each(function(el){
	if (compareString(el.readAttribute('title'), title, false))
	{
		el.writeAttribute('id',newid);
	}
	});
}

function setElementClassByTitle(tagName, title, newclass)
{
	getDOMHash(tagName, TAGNAME, "").each(function(el){
	if (compareString(el.readAttribute('title'), title, false))
	{
		el.writeAttribute('class',newclass);
	}
	});
}

function convertATagtoDivTag(tagClass)
{
	getDOMHash(tagClass, TAGCLASS, "").each(function(el){
		if (el != null && compareString(el.tagName, "a", false))
		{	
			el.replace('<div class=' + tagClass + '>' + el.innerHTML + '</div>');
		}
	});
}

function setElementClassByAttributeSubstring(tagName, attribute, substring, newClass)
{
	elements = getDOMHash(tagName, TAGNAME, "");
	elements.each(function(el){
	if (checkSubstring(el.readAttribute(attribute), substring, false))
	{
		el.writeAttribute('class',newClass);
	}
	});
}

function countTags(hashObject, tagName) // takes a prototype hash object and counts the number of tags (tagName) in it
{
	count = 0;
	hashObject.descendants().each(function(el)
	{
	if (compareString(el.tagName, tagName, false))
		{
			count++;
		}
	});
	return count;
}

// initialised by iGRaspHelper.asp

function offlineAppFix(appContainerId)
{
	rowTempTag = "removeRow";
	jobapplycontainer = getDOMHash(appContainerId,TAGID,"");
	if (true) //change
	{
		tagName = jobapplycontainer.tagName;
		needFix = false;
		if (jobapplycontainer.down('a').offsetHeight == 0)
		{
			needFix = true;
		}
		if (needFix)
		{
			switch(tagName)
			{
				case "TD":
					hideRow(appContainerId, TAGID, "");
					break;
				default:
					jobapplycontainer.hide();
			}
		}
	}
}
