/***********************************************************************************/
/**	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();
}
function MenuPageSortSubmit(objTableForm,objFilterForm,strObjName) {
    if (objTableForm.SortBy.value != objTableForm.SortBy2.value) {
        var sortDirection = true;
        var SortColumns = '';
        var strSortDirection = 'ASC';

        SortColumns = objTableForm.SortBy.value;

        /*Check if this is called by changing drop down or by clicking radio buttons( Asc or Desc)
        if its drop down then check the defaultSort attribute for the each option in the drop down
        and apply it. If its empty then use the exisitng selected radio button.
        
        if its not drop down change, then use exisitng selected option of drop down to apply sort direction 
        */
        if (strObjName.toUpperCase() == 'SORT1') {

            var tempSort = tempSort = objTableForm.SortBy.options[objTableForm.SortBy.selectedIndex].getAttribute('defaultSort');
            if(tempSort != '')
            {
                strSortDirection = tempSort;
                if (strSortDirection.toUpperCase() == 'ASC')
                    objTableForm.SortDirection[0].checked = true;
                else
                    objTableForm.SortDirection[1].checked = true;

            }
            if (objTableForm.SortDirection[0].checked)
                strSortDirection = 'ASC';
            else if (objTableForm.SortDirection[1].checked)
                strSortDirection = 'DESC';
            
        }
        else {
            /*
            If function is called by changing the radio buttons
            */
            if (objTableForm.SortDirection[0].checked)
                strSortDirection = 'ASC';
            else if (objTableForm.SortDirection[1].checked)
                strSortDirection = 'DESC';
        }
        /*Check if this is called by changing drop down or by clicking radio buttons( Asc or Desc)
        if its drop down then check the defaultSort attribute for the each option in the drop down
        and apply it. If its empty then use the exisitng selected radio button.
        
        if its not drop down change, then use exisitng selected option of drop down to apply sort direction 
        */
        if (objTableForm.SortBy2.value != '') {
            SortColumns += ',' + objTableForm.SortBy2.value;
            if (strObjName.toUpperCase() == 'SORT2') {

                var tempSort = tempSort = objTableForm.SortBy2.options[objTableForm.SortBy2.selectedIndex].getAttribute('defaultSort');
                if (tempSort != '') {
                    strSortDirection += ',' + tempSort;
                    if (tempSort.toUpperCase() == 'ASC')
                        objTableForm.SortDirection2[0].checked = true;
                    else
                        objTableForm.SortDirection2[1].checked = true;

                }
                if (objTableForm.SortDirection2[0].checked)
                    strSortDirection += ',ASC';
                else if (objTableForm.SortDirection2[1].checked)
                    strSortDirection += ',DESC';

            }
            else {
                /*
                if function is called by changing radio buttons
                */
                if (objTableForm.SortDirection2[0].checked)
                    strSortDirection += ',ASC';
                else if (objTableForm.SortDirection2[1].checked)
                    strSortDirection += ',DESC';
            }
            
        }
        objFilterForm.OrderString.value = SortColumns;
        objFilterForm.Ascending.value = strSortDirection;
        if (objFilterForm.QueryString.value.length > 0) {
            //Handle multiple presses - e.g. from order by button (otherwise query string will be added many times)
            if (objFilterForm.action.indexOf("?") == -1)
                objFilterForm.action = objFilterForm.action + "?" + objFilterForm.QueryString.value;
        }  
        objFilterForm.submit();
    }
    else {
        alert('You cannot sort records with same fields');
        if (strObjName.toUpperCase() == 'SORT1')
            objTableForm.SortBy.value = document.getElementById('TempSort1').value;
        else
            objTableForm.SortBy2.value = document.getElementById('TempSort2').value;
    }
}
//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;
}
function getScrollXY() {
    var x = 0, y = 0;
    if (typeof (window.pageYOffset) == 'number') {
        // Netscape
        x = window.pageXOffset;
        y = window.pageYOffset;
    } else if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        // DOM
        x = document.body.scrollLeft;
        y = document.body.scrollTop;
    } else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
        // IE6 standards compliant mode
        x = document.documentElement.scrollLeft;
        y = document.documentElement.scrollTop;
    }
    return [x, y];
}

function setScrollXY(x, y) {
    window.scrollTo(x, y);
}
