/***********************************************************************************/
/**	This file is included with all navigational asp files, (those with Tabs).  It  */
/** Verify's the form and replaces the quotes (so FormVerification.js must already */
/** be included in the asp file) and then redirect to the relevent page (depending */
/** on the value of "Link" which is passed by the actual TabClick Function		   */
/**	and the value of CompanyID - user may have clicked next etc...				   */
/***********************************************************************************/

function TabClick(FormName, strAction, strError)
{
	if (FormName != "")
	{
		if (Verify(FormName, strError) == true && AllowSubmit == true)
		{
			FormName.action = strAction;	//.replace (/&/g, "%26")
			FormName.submit();
			AllowSubmit = false;
		}
		else
			AllowSubmit = true;
	}
	
	else
	{
		window.location=strAction;
	}
}

function ASPNetTabClick(FormName, strAction, strError)
{
    if(FormName != "")
    {
        if (Verify(FormName, strError) == true && PageValid == true)
		{
		    PageValid = true;
		}
		else
		{
		    PageValid = false;
		}
	}
	
	else
	{
		window.location=strAction;
	}
	
	
}

/***********************************************************************************/
/**	This function simply changes the location property of a window (objWindow) and */
/** was first used to allow window.opener navigation from the href property of	   */
/** items in a report. e.g. candidate summary info navigates the opening window to */
/** the candidate details when a user clicks on the candidate's name			   */
/***********************************************************************************/
function NavigateWindow(objWindow,strURL)
{
	objWindow.location = strURL;
}


/***********************************************************************************/
/**	This function checks if the website url has the http:// in it. If not, it	   */
/*	preceeds it with it before opening the window								   */
/***********************************************************************************/
function OpenWebsiteClick(strWebsiteURL)
{
	if (strWebsiteURL.toUpperCase().indexOf("HTTP://") == -1)
		window.open("http://" + strWebsiteURL);
	else
		window.open(strWebsiteURL);
}

function ApplyFormatting(strToFormat, strFormatType)
{
	var ReturnString = "";
	
	switch (strFormatType.toUpperCase())
	{
		case 'DATE(US)':
		
			var strDateArray = strToFormat.split("/");
			ReturnString = strDateArray[1] + "/" + strDateArray[0] + "/" + strDateArray[2];
			return(ReturnString);														
			break;
		
		//This works with a  style Date
		case 'DATE(IETF)':
			
			if (strToFormat != "")
			{
				var strDateArray = strToFormat.split("/");
				ReturnString = strDateArray[0] + " ";
				
				switch (parseFloat (strDateArray[1]))
				{
					case 1:
						ReturnString += "Jan";
						break;
					case 2:
						ReturnString += "Feb";
						break;
					case 3:
						ReturnString += "Mar";
						break;
					case 4:
						ReturnString += "Apr";
						break;
					case 5:
						ReturnString += "May";
						break;
					case 6:
						ReturnString += "Jun";
						break;
					case 7:
						ReturnString += "Jul";
						break;
					case 8:
						ReturnString += "Aug";
						break;
					case 9:
						ReturnString += "Sep";
						break;
					case 10:
						ReturnString += "Oct";
						break;
					case 11:
						ReturnString += "Nov";
						break;
					case 12:
						ReturnString += "Dec";
						break;
				}
				
				ReturnString += " " + strDateArray[2];
			}

			return(ReturnString);
			break;
		
		case 'URL':
			ReturnString = escape (strToFormat);
			ReturnString = ReturnString.
							replace (/\+/g, "%2B").
							replace (/&/g, "%26").
							replace(/\"/g,'%22').
							replace(/\'/g, '%27').
							replace(/\//g,'%2F');

			return(ReturnString);						
			break;			
	}
}

//To use this function, the page calling it must contain a form (FormName) which
//has hidden fields "OrderString" & "Ascending", it handles ordering of table columns on user clicks

function FilterClick (FormName,field,Ascending)
{
	FormName.OrderString.value=field;
		
	if (Ascending == true)
		FormName.Ascending.value="ASC";
	else
		FormName.Ascending.value="DESC";
			
	if (FormName.QueryString.value.length > 0)
	{
		//Handle multiple presses - e.g. from order by button (otherwise query string will be added many times)
		if (FormName.action.indexOf ("?") == -1)
			FormName.action = FormName.action + "?" + FormName.QueryString.value;
	}
	FormName.submit();
}

//Used by navbar etc.. to validate the value as a number between the values MinValue & MaxValue
function ValidateRecordPosition(FieldName,MinValue,MaxValue)
{
	FieldName.value = FieldName.value.replace (/,/g, '');
	
	if (isNumeric(FieldName.value))
	{
		if (MinValue != "")
		{
			if (FieldName.value < MinValue)
			{
				FieldName.value = MinValue;
			}
		}
			
		if (MaxValue != "")
		{
			if (FieldName.value > MaxValue)
			{
				FieldName.value = MaxValue;
			}
		}
		return(true);
	}
	else
	{
		alert ("The record position you have entered is not a number");
		return (false);
	}
}

//These return the X & Y position of any object
function getY(obj){
	return (obj.offsetParent ? obj.offsetTop + getY(obj.offsetParent) : obj.y ? obj.y : 0);
}
function getX(obj){
	return obj.offsetLeft + (obj.offsetParent ? getX(obj.offsetParent) : obj.x ? obj.x : 0);
}

//Returns the height of a document
function GetDocumentHeight(doc) {
  var docHt = 0, sh, oh;
  if (doc.height) docHt = doc.height;
  else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
  }
  return docHt;
}

//Sets the height of an IFrame (to the height of the document inside it)

function SetIFrameHeight(iframeName) {
  var iframeWin = window.frames[iframeName];
  var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
  if ( iframeEl && iframeWin ) {
    iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous  
    var docHt = GetDocumentHeight(iframeWin.document);
    // need to add to height to be sure it will all show
    if (docHt) iframeEl.style.height = docHt + "px";
  }
}

function LoadIFrame(iframeName, url) {
  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;   
    return false;
  }
  else return true;
}