/*Javascript used by top level and second level menus.
Assumption that top level menu will always exist on 
a screen. Second level menu cannot exist on its own. JS 
file loaded only in top level menu.
*/
var Header1Hide;
var Header2Hide;

/*Change this when deploying the project on a diff machine
Use the actual machine name and not 'localhost'.
*/
var machineName;

/*
Change uRequest to uRequestManager.
Hardcoding mc name and web url is not desirable.
Find a way to migrate this to relative paths if possible.
*/
 function setHeader1(nolink) {
	var urlPath = 'http://'+machineName+'/';

    document.write('<div id="top">');
    document.write(makeLink('MyPortal', nolink, '#', '(logo)'), ' | ');
    document.write(makeLink('Requests', nolink, urlPath+'uRequestManager/ui/RequestHome.aspx', 'Requests'), ' | ');
    document.write(makeLink('Grants', nolink, urlPath+'uGrantManager/ui/GrantHome.aspx', 'Grants'), ' | ');
    document.write(makeLink('Contracts', nolink, urlPath+'uContractManager/ui/ContractHome.aspx', 'Contracts'), ' | ');
    document.write(makeLink('Worksets', nolink, urlPath+'uRequestManager/ui/WorksetsHome.aspx', 'Worksets'), ' | ');
    document.write(makeLink('Contacts', nolink, urlPath+'uRequestManager/ui/ContactsHome.aspx', 'Contacts'), ' ');
    document.write('<span style="position:absolute;right:0;margin-right: 10px;">', makeLink('Help', nolink, '#', 'Help'), ' | ');
    document.write('<a href="https://knet.wkkf.org/">KNet</a>');
    document.write('</span></div>');
}

function setRequestHeader2(nolink) {
    document.write('<div id="top2">');
    document.write(makeLink('Overview', nolink, 'RequestOverview.aspx', 'Overview'), ' | ');
    document.write(makeLink('Details', nolink, 'RequestDetailReadOnly.aspx', 'Details'), ' | ');
    document.write(makeLink('Contacts', nolink, 'OrgContact.aspx', 'Contacts'), ' | ');
    document.write(makeLink('Documents', nolink, 'DocumentsHome.aspx', 'Documents'), ' | ');
    document.write(makeLink('Tasks', nolink, 'RequestTasks.aspx', 'Tasks'), ' | ');
    document.write(makeLink('Financials', nolink, 'Financial.aspx', 'Financials'), ' ');
    document.write('</div>');
}

function setContractHeader2(nolink) {
    document.write('<div id="top2">');
    document.write(makeLink('Overview', nolink, 'ContractOverview.aspx', 'Overview'), ' | ');
    document.write(makeLink('Details', nolink, 'ContractDetailReadOnly.aspx', 'Details'), ' | ');
    document.write(makeLink('Contacts', nolink, 'ContractOrgContact.aspx', 'Contacts'), ' | ');
    document.write(makeLink('Documents', nolink, 'ContractDocuments.aspx', 'Documents'), ' | ');
    document.write(makeLink('Tasks', nolink, 'ContractTasks.aspx', 'Tasks'), ' | ');
    document.write(makeLink('Financials', nolink, 'ContractFinancial.aspx', 'Financials'), ' ');
    document.write('</div>');
}

function setGrantHeader2(nolink) {
    document.write('<div id="top2">');
    document.write(makeLink('Overview', nolink, 'GrantOverview.aspx', 'Overview'), ' | ');
    document.write(makeLink('Details', nolink, 'GrantDetailReadOnly.aspx', 'Details'), ' | ');
    document.write(makeLink('Contacts', nolink, 'GrantOrgContact.aspx', 'Contacts'), ' | ');
    document.write(makeLink('Documents', nolink, 'GrantDocuments.aspx', 'Documents'), ' | ');
    document.write(makeLink('Tasks', nolink, 'GrantTasks.aspx', 'Tasks'), ' | ');
    document.write(makeLink('Financials', nolink, 'GrantFinancials.aspx', 'Financials'), ' ');
    document.write('</div>');
}

function makeLink(nolink1, nolink2, href, text) {
    var str = '';
    if (nolink1 != nolink2) {
  	str = '<a href="' + href + '">';
    }
    str += text;
    if (nolink1 != nolink2) {
  	str += '</a>';
    }
    return str;
}

//The message body
var ShowMess;

//Function to display the message.
function ShowAlert()
{
	if(ShowMess != undefined)
	{
		alert(ShowMess);	
		ShowMess = undefined;
	}					
}	

//Variable to suppress messages when form is being posted.
var suppressMessage;

suppressMessage = false;
//Function called when page is submitted			
function callOnSubmit(){
	suppressMessage = true;
}
//Function called when a page is closed or user navigates away from the screen.
function onScreenClosed()
{
	if (suppressMessage == false){
		if(window.confirm("Do you want to save your changes?\nClick OK to save changes, Cancel to abandon them.")) {
			if (typeof(Page_ClientValidate) != 'function' ||  Page_ClientValidate()) __doPostBack('btnSave','')
			}
		}
}

		//*************************************************
		//Start -- Get Object Functions
		//Function to get object instance 
		//by the element name.
		function getObject(objectName)
		{
			return document.getElementById(objectName);
		}
		//End -- Get Object Functions
		//*************************************************

		//*************************************************
		//Start -- Redirect Page
		//Function to redirect to a page 
		//parameter --  strFilePath=relative path of the page  
		function RedirectPage(strFilePath)
		{
			//redirecting 
			document.location.href(strFilePath);
		}
		//End -- Redirect Page
		//*************************************************

		/*====================================================================
		This method check whether a control's value proprty is empty string
		=====================================================================*/
		function checkIsEmpty(ctrl)
		{
			if(!(document.getElementById(ctrl) ==null))
			{
				//alert(ctrl);
				var obj = document.getElementById(ctrl);
			
				if(obj.value == '')
				{		   
					return true;
				}
				else
				{
					return false;
				}	
			}
			else
			{
				//alert(ctrl);
			return true;
			}
		}
		
		/*==================================================
		Method to set the Default button in a screen
		===================================================*/
		function setDefaultButton(btnName){
			if (document.all){
				if (event.keyCode == 13){ 
					event.returnValue=false;
					event.cancel = true;
					var obj = document.getElementById(btnName);
					obj.click();
				}
			}
		}
		
		//==================================================================
		// LTrim(string) : Returns a copy of a string without leading spaces.
		//==================================================================
		function LTrim(str)
		/*
		PURPOSE: Remove leading blanks from the string.
		IN: str - the string we want to LTrim
		*/
		{
		var whitespace = new String(" \t\n\r");

		var s = new String(str);

		if (whitespace.indexOf(s.charAt(0)) != -1)
			{
			// We have a string with leading blank(s)...

			var j=0, i = s.length;

			// Iterate from the far left of string until we
			// don't have any more whitespace...
			while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
				j++;

			// Get the substring from the first non-whitespace
			// character to the end of the string...
			s = s.substring(j, i);
		}
		return s;
		}

		/*
		==================================================================
		RTrim(string) : Returns a copy of a string without trailing spaces.
		==================================================================
		*/
		function RTrim(str)
		/*
		PURPOSE: Remove trailing blanks from our string.
		IN: str - the string we want to RTrim

		*/
		{
		// We don't want to trip JUST spaces, but also tabs,
		// line feeds, etc.  Add anything else you want to
		// "trim" here in Whitespace
		var whitespace = new String(" \t\n\r");

		var s = new String(str);

		if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
			// We have a string with trailing blank(s)...

			var i = s.length - 1;       // Get length of string

			// Iterate from the far right of string until we
			// don't have any more whitespace...
			while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
				i--;


			// Get the substring from the front of the string to
			// where the last non-whitespace character is...
			s = s.substring(0, i+1);
		}

		return s;
		}

		/*
		=============================================================
		Trim(string) : Returns a copy of a string without leading or trailing spaces
		=============================================================
		*/
		function Trim(str)
		/*
		PURPOSE: Remove trailing and leading blanks from our string.
		IN: str - the string we want to Trim

		RETVAL: A Trimmed string!
		*/
		{
			return RTrim(LTrim(str));  
		}

		/*
		=============================================================
		Check_Float(string) : Function for validating float values.
		=============================================================
		*/

		function Check_Float(str)
					{
						
				    
				var field = document.forms[0].elements[str];
				var value =  Escape_Commas(field.value);
				var floatPattern = /^(-?\d+)|(-?\d*\.\d+)|(-?\d+(e|E)-?\d+)|(-?\d*\.\d+(e|E)-?\d+)$/;
					
				txtObject = getObject(str);
				
				if(!( txtObject.value == ""))
				{			
					if(value.match(floatPattern) == null)
						{
						flg = 1;
						strCurrValue = 0;
						strCurrIncDecValue = 0;
						alert(field.value + " is not a valid decimal number.");
						field.select();
						field.focus();						
						return ;						
						}	
					else if (value > 0 && value < 1)
						{
							var str = value;
							if (str.charAt(0) == '.')
							{
								value = 	"0" + value;
							}
							field.value = Round(value,5);
							return ;	
						}
					else
						{
							if(!(isNaN(value)))							
							{
								field.value = Format_Currency(Round(value,5));
								return ;
								}
							else
								{
									strCurrValue = 0;
									strCurrIncDecValue = 0;
									alert(field.value + " is not a valid number.");
									field.select();
									field.focus();
									return ;
								}
						}				
				}	
				else
				{
					return ;
				}		
					
			}
			
		//*********************************************************************************
		/*
		=============================================================
		parse_Float(string) : Function for parsing float values.
		=============================================================
		*/
		function parse_Float(str)
		{
		  if( str != '' )
		  {
		    var fixStr = GetAmountNum(str);
		  }
		  // Remove dollar signs and commas.
			var value = parseFloat(fixStr);
			if((isNaN(fixStr)) || (fixStr == ''))
			{
				return 0;
				
			}
			else
			{
				return value;
			}
			
		}
		
		function GetAmountNum(amtStr)
		{
		  var fixStr = new String(amtStr);
		  var myRegExp = /\$/g;
		  fixStr = fixStr.replace(myRegExp,'');
		  myRegExp = /\,/g;
		  fixStr = fixStr.replace(myRegExp,'');
		  return fixStr;
		}
				
		//*********************************************************************************
		/*
		=============================================================
		hideShowTable(tableName,button Name) : Function to Hide / show the table
											on click of the button
		=============================================================
		*/
		
		function hideShowTable(tblName,btnName)
		{		
			
		tableObject = getObject(tblName);
		ButtonObject = getObject( btnName);
			
		if (ButtonObject.value == "+")
		{
			tableObject.style.display="block";
			ButtonObject.value="-"; 
			
		}
		else 
		{
		   	ButtonObject.value="+"; 
		   	tableObject.style.display="none";
		}	
		
		}
		//*********************************************************************************
		/*
		=============================================================
		Escape_Commas(string) : Function to escape commas in a string
		=============================================================
		*/
		function Escape_Commas(str) 
		{
			if( str != "")
			{
			var strArr = new Array();
			var intCount = 0;
			str = str.toString();
			strArr = str.split(",")
			str = ""
			for(intCount = 0; intCount < strArr.length; intCount ++)
			{
				str = str + strArr[intCount];
		        
			}
		    
		}
		return str;
		}
		
		//*********************************************************************************
		/*
		=============================================================
		Format_Currency(string) : Function to format the currency values 
								by inserting commas in appropriate places
		=============================================================
		*/
		/*
		function Format_Currency(str)
		{
				var strTemp;
				var Diff;
				var blnSignFlag  = false;
				strTemp = Escape_Commas(str);
				
				if((!(isNaN(parseFloat(strTemp)))) && (strTemp != ""))
				{
				
						if((parseFloat(strTemp) > -1) && (parseFloat(strTemp) < 1))
							return strTemp;
						strTemp = parseFloat(strTemp) + 0;
						Diff = Round(parseFloat(strTemp)- parseInt(strTemp),5);
						strTemp = parseInt(strTemp);
						if (strTemp < 0)
						{
							blnSignFlag = true;
							strTemp = strTemp * -1;
						}
						strTemp = strTemp.toString();
						
						if(strTemp.length > 3)
						{		
							
							switch(strTemp.toString().length)
								{
									case 4 : 
										strTemp = strTemp.substring(0, strTemp.length - 3) + "," + strTemp.substring(strTemp.length - 3,strTemp.length);
										break;
									case 5 : 
										strTemp = strTemp.substring(0, strTemp.length - 3) + "," + strTemp.substring(strTemp.length - 3,strTemp.length);
										break;
									case 6 : 
										strTemp = strTemp.substring(0, strTemp.length - 3) + "," + strTemp.substring(strTemp.length - 3,strTemp.length);
										break;
									case 7 : 
										strTemp = strTemp.substring(0, strTemp.length - 6) + "," + strTemp.substring(strTemp.length - 6, strTemp.length - 3) + "," + strTemp.substring(strTemp.length - 3,strTemp.length);
										break;
									case 8 : 
										strTemp = strTemp.substring(0, strTemp.length - 6) + "," + strTemp.substring(strTemp.length - 6, strTemp.length - 3) + "," + strTemp.substring(strTemp.length - 3,strTemp.length);
										break;
									case 9 : 
										strTemp = strTemp.substring(0, strTemp.length - 6) + "," + strTemp.substring(strTemp.length - 6, strTemp.length - 3) + "," + strTemp.substring(strTemp.length - 3,strTemp.length);
										break;
									default :
										strTemp = strTemp.substring(0, strTemp.length - 9) + "," + strTemp.substring(strTemp.length - 9, strTemp.length - 6) + "," + strTemp.substring(strTemp.length - 6, strTemp.length - 3) + "," + strTemp.substring(strTemp.length - 3,strTemp.length);
										break;
								}
							}
						}
						if(Diff != 0)
						{
							if(Diff < 0)
								Diff = Diff * -1;
							Diff = Diff.toString();
							strTemp =  strTemp + Diff.substring(1, Diff.length)
						}
						if( blnSignFlag == true)
						{
							blnSignFlag = false;
							strTemp = "-" + strTemp;
						}
						return strTemp;

				}
			
			return strTemp;
			}
	    */
		//*********************************************************************************
		/*
		=============================================================
		Format_Currency(string) : Function to round the currency values 
								to 2 decimal places
		=============================================================
		*/
		function Round(str)
			{	
				if ((str != '0') && (!(isNaN(str))) && (str != null))
					{
						str = Math.round(parse_Float(str) * 100)/100;
					}
				if(str == '0')
					{
						return 0;
					}
				return space;	
			}
			

			//Functionality not impemented message
			function underConstructionMessage(){
				alert('Action not implemented for Iteration B.');
			}

// DisableAllLinks - function used to prevent double clicking buttons
function DisableAllLinks() {
	
	//
	var link;
	var links = document.body.getElementsByTagName("a");
	var key;
	for (key in links) {
		link = links[key];
		//link.disabled = true;
		if (link.runtimeStyle) {
			link.runtimeStyle.color = "#aaaaaa";
			link.onclick = "";
		}
		link.href = "#";
	}

	var links = document.body.getElementsByTagName("input");
	for (key in links) {
		link = links[key];
		if(link.type) {
			if(link.type == "submit" || link.type == "image" || link.type == "button") {
				if (link.runtimeStyle) {
					link.runtimeStyle.color = "#aaaaaa";
					link.onclick = "";
				}
				link.disabled = true;
			}
		}
	}

	var links = document.body.getElementsByTagName("button");
	for (key in links) {
		link = links[key];
		if (link.runtimeStyle) {
			link.runtimeStyle.color = "#aaaaaa";
			link.onclick = "";
		}
		link.disabled = true;
	}
}

function openHelpWin(title, helptext, language) {
  window.open('Help.aspx?t=' + title + '&h=' + helptext + '&LanguageID=' + language, 'HelpWin', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=380,height=200,left=312,top=184');
}
