
/********************************
LEFT NAVIGATION HIDE/SHOW SCRIPTS
*********************************/

function appear(id, boolvisible, rollImageUrl)
{		
	var elementID = "imgHome" + id;
	if(boolvisible==1)
	{  
		document.getElementById(elementID).setAttribute("src", rollImageUrl);
	    //document.getElementById(id).style.display ='block';	 
	    document.getElementById(id).style.visibility = 'visible';
	}
	else
	{	
	  document.getElementById(elementID).setAttribute("src", rollImageUrl);
	  //document.getElementById(id).style.display ='none';	 
	  document.getElementById(id).style.visibility = 'hidden';
	}
}

function RemoveContItemTitleFromCD(id,factor)
{
	if(factor==1)
	{	
		document.getElementById(id).style.display ="none";
	}
}

//----- Function to allow both Alphabets and Numbers ---------
function AllowOnlyAlphabetsAndNumbers(e) {				
	var keycode;
	var retVal = true;
	keycode = e.charCode ? e.charCode : e.keyCode;		
	
	//If not an alphabet then do not allow	
	if ((keycode <= 122 && keycode >= 97) || (keycode == 8) || (keycode == 39) || (keycode == 44)) {					
		retVal = true;
	}else{
		if (keycode <= 90 && keycode >= 65){						
			retVal = true;
		}else{
			if (keycode <= 57 && keycode >= 48){
				retVal = true;
			}else{
				if (keycode == 32){
					retVal = true;
				}else{
					retVal = false;
				}
			}	
		}
	}
	if(!retVal)
	{			
		if(window.event) event.returnValue = false;	//IE
		else e.preventDefault();	//FireFox & NS
	}
}

/*************************
GRANTS SEARCH PAGE SCRIPTS
**************************/

//Function to show hover effect in IE
function hoverEffect()
{
	//Check if there is a DIV named "subprogram" in the current page
	if(document.getElementById("subprogram"))
		recurseList(document.getElementById("subprogram"));
}

//Function to show hover effect in IE for a passed element ID
function hoverEffect(elementID)
{
	//Check if there is a DIV named "subprogram" in the current page
	if(document.getElementById(elementID))
		recurseList(document.getElementById(elementID));
}

function recurseList(navRoot)
{
      var i = 0;
      for (i=0; i<navRoot.childNodes.length; i++) 
      {
		node = navRoot.childNodes[i];
	    
		if (node.childNodes.length == 0)
			return;      

		if (node.nodeName=="LI") 
		{
			assignEvents(node);
		}

		for (j=0; j<node.childNodes.length; j++) 
		{
			if (node.childNodes[j].nodeName=="UL") 
			{
					recurseList(node.childNodes[j]);
			}
		}      
      }
}

function assignEvents(navNode)
{
      navNode.onmouseover=function()
      {
            this.className +=" over"; 
      }

      navNode.onmouseout=function() 
      {           
            this.className=this.className.replace(" over", "");
      }         
      
      navNode.onclick=function() 
      {			           
            if(this.childNodes[0].nodeName == "INPUT")	//Check if the control is a textbox or checkbox
			{
				//If checkbox is checked then make it unchecked
				if(this.childNodes[0].checked)
				{
					//this.childNodes[0].checked = false;
				}
				else	//if checkbox is unchecked then make it checked
				{
					//this.childNodes[0].checked = true;
				}
			}
      }           
}

//On page load call the hoverEffect function
//window.onload = hoverEffect;

/*********************************************
Script for CHECK/UNCHECK in Grants Search Page
**********************************************/

//This method shows the appropriate subprogram div for a selected program
//selected program category ID is passed as parameter to this function
function showSubPrograms(progCategoryID, reInitialize)
{
	var programIDs;		//stores delimited all program category IDs
	var programIdArray;	//stores the program ID array

	progCategoryID = progCategoryID - 1;

	//Reading the program category IDs from the hidden field "programIDs"
	programIDs = document.getElementById("programIDs").value;	
	//split the programIDs with "|" delimiter and store in an array	
	programIdArray = programIDs.split("|");
	
	//Reset the scroll container "scroll position" to zero
	document.getElementById("scrollBox").scrollTop = 0;
	//Check if a category id was passed	i.e., a program is selected
	if(progCategoryID == -1)
	{
		//first show the subprogram holder
		document.getElementById("SubProgHolder").style.display = 'none';
	}
	else
	{
		//first show the subprogram holder
		document.getElementById("SubProgHolder").style.display = 'block';
	}
	//Loop through all the program category IDs
	for(i=0; i<programIdArray.length; i++)
	{
		//if the passed category id matches with category id in the array then show it else hide it
		if(progCategoryID == programIdArray[i])
		{
			//show the sub program block of the selected program category id
			document.getElementById("SubProgBlock"+programIdArray[i]).style.display = 'block';	
		}
		else
		{
			//hide the sub program block other than the selected program category id
			document.getElementById("SubProgBlock"+programIdArray[i]).style.display = 'none';	
		}		
	}		 	
	
	//Re-Initialize all the checkboxes to unselected state
	if(reInitialize) selectAllCheckboxes(false);
}

//This method will achieve the check/uncheck logic for the check boxes in the Subprogram control
function checkUncheckLogic(thisChkBox)
{
	var numOfCtrlsInPage;	//Number of form controls in the page
	var childControl;		//Form controls
	var chkStatus;			//Checkbox checked/unchecked
	var categoryID;			//Category ID of the passed checkbox
	var parentCategoryID;	//Parent Category ID of the passed checkbox
	var rootChkBox;			//Root checkbox (All Subprograms)
	var selProgramID;		//Selected Program Category ID
		
	//Get the number of form controls present
	numOfCtrlsInPage = document.Form1.length;
	//Get the selected program category ID
	selProgramID = document.Form1.ddlProgramArea.selectedIndex -1;
	//Get the category id from the passed checkbox id
	categoryID = getCategoryID(thisChkBox.id);	
	//Get the parent category id from the passed checkbox id
	parentCategoryID = getParentCategoryID(thisChkBox.id);
	//Get the "All Sub programs" checkbox control
	rootChkBox = getRootCheckBox(numOfCtrlsInPage)	
	
	//Get all the child checkboxes checked if this (parent) checkbox was checked
	chkStatus = thisChkBox.checked;	
	
	//Check if the "All Subprograms" checkbox was clicked
	if(thisChkBox.id == "subprogid--#")
	{	
		//Loop through all the controls in the page
		for(var i=0; i<numOfCtrlsInPage; i++)
		{
			//Assign the current control to a variable
			childControl = document.Form1.elements[i];
			//Check if the control id contains the name "subprogid<<sel prog cat id>>" (to verify that it is child of allsubprograms)
			if(isPresent(childControl.id, "subprogid"+selProgramID)) childControl.checked = chkStatus;			
		}	
	}
	else	//Use the Select/UnSelect logic
	{	
		//Check if this subprogram has childs	
		if(!hasChilds(thisChkBox.id, categoryID, numOfCtrlsInPage, selProgramID))
		{
			//Logic to check the parent checkbox if all its childs are checked			
			checkParentCheckbox(thisChkBox, parentCategoryID, numOfCtrlsInPage, selProgramID)			
		}
		else
		{						
			//Loop through all the controls in the page
			for(var i=0; i<numOfCtrlsInPage; i++)
			{
				//Assign the current control to a variable
				childControl = document.Form1.elements[i];
				//Check if the control id contains the name "subprogid-"+ categoryID+"-" (it is a child of this subprogram)
				if(isPresent(childControl.id, "subprogid"+selProgramID+"-"+ categoryID+"-"))
				{
					childControl.checked = chkStatus;
					//Perform the check/uncheck logic so that the grant children are also selected
					//First check if this control has children
					if(hasChilds(childControl.id, getCategoryID(childControl.id), numOfCtrlsInPage, selProgramID)) 
					{ checkUncheckLogic(childControl); }
				}
			}			
			
			//Logic to check the parent checkbox if all its childs are checked			
			checkParentCheckbox(thisChkBox, parentCategoryID, numOfCtrlsInPage, selProgramID)	
		}
		
		//Check if all child of "All Subprograms" are checked
		if(allChildsChecked(rootChkBox.id, "", numOfCtrlsInPage, selProgramID))
			rootChkBox.checked = true;
	}	
	
	//Get all the selected Subprograms
	getSelectedSubPrograms(numOfCtrlsInPage);
}

//This method returns the CategoryID present in the checkbox id Ex: 85 is returned from "subprogid-85"
function getCategoryID(strChkBoxID)
{
	return strChkBoxID.substring(strChkBoxID.lastIndexOf("-")+1, (strChkBoxID.length-1));
}

//This method returns the ParentCategoryID present in the checkbox id Ex: 85 is returned from "subprogid-85-110"
function getParentCategoryID(strChkBoxID)
{
	return strChkBoxID.substring(strChkBoxID.indexOf("-")+1, strChkBoxID.lastIndexOf("-"));
}

//This method returns true if string str2 is present in string str1
function isPresent(str1, str2)
{
	return (str1.indexOf(str2) >= 0);
}

//This method returns true, if a subprogram has child subprograms
function hasChilds(strChkBoxID, categoryID, numOfCtrlsInPage, selProgramID)
{
	//create a string checkbox id with which its child checkbox id would start with
	var strPrntChkBoxID = "subprogid"+selProgramID+"-"+ categoryID+"-";		
	//Boolean variable which will be set to true if any of the control id matches with the "strPrntChkBoxID"
	var childPresent = false;	
	
	//Loop through all the controls in the page
	for(var i=0; i<numOfCtrlsInPage; i++)
	{		
		//Assign the current control to a variable
			childControl = document.Form1.elements[i];
			//Check if the control id contains the name "subprogid" (to verify that it is child of allsubprograms)
			if(isPresent(childControl.id, strPrntChkBoxID)) childPresent = true;			
	}	
	//return the value stored in childPresent (true or false)
	return childPresent;
}

//This method returns true, if all child subprograms of a subprogram are already checked
function allChildsChecked(strChkBoxID, categoryID, numOfCtrlsInPage, selProgramID)
{
	//create a string checkbox id with which its child checkbox id would start with
	var strPrntChkBoxID;		
	//Number of child sub programs
	var numOfChild = 0;	
	//Number of child sub programs checked
	var numOfChildChecked = 0;
	
	if(categoryID != "") strPrntChkBoxID = "subprogid"+selProgramID+"-"+ categoryID+"-";
	else strPrntChkBoxID = "subprogid"+selProgramID+"-";
	
	//Loop through all the controls in the page
	for(var i=0; i<numOfCtrlsInPage; i++)
	{
		//Assign the current control to a variable
			childControl = document.Form1.elements[i];
			//Check if the control id contains the name "subprogid" (to verify that it is child of allsubprograms)
			if(isPresent(childControl.id, strPrntChkBoxID)) 
			{				
				if(childControl.id != "subprogid--#")
				{
					//increment the number of childs by 1
					numOfChild++;
					//if this control is checked then increment the number of childs checked by 1
					if(childControl.checked) numOfChildChecked++;
				}
			}
	}	
		
	//return the value stored in childPresent (true or false)
	return (numOfChild == numOfChildChecked);
}

//This method finds the parent control of a child control
function getParentCheckbox(childControl, parentCategoryID, numOfCtrlsInPage)
{
	var parentControl;		//Parent control of the child control
	var strParentCtrlIdEnds;	//string that holds how a parent control id would end Ex: "-85#"
	
	parentControl = childControl;
	strParentCtrlIdEnds = "-"+ parentCategoryID +"#";
	
	//Loop through all the controls in the page
	for(var i=0; i<numOfCtrlsInPage; i++)
	{
		//Assign the current control to a variable
			childControl = document.Form1.elements[i];
			//Check if the control id contains the string like strParentCtrlIdEnds
			if(isPresent(childControl.id, strParentCtrlIdEnds)) 
			{
				parentControl = childControl;
			}
	}	
	
	//Return the found Parent Control checkbox
	return parentControl;
}

//This method will check a parent checkbox if all its child are checked
function checkParentCheckbox(thisChkBox, parentCategoryID, numOfCtrlsInPage, selProgramID)
{
	var parentChkBox;		//Parent Checkbox of the curent child checkbox
	
	//First check if this is not a second level category
	if(parentCategoryID == "-") parentCategoryID = "";	
	
	//Get the parent checkbox id
	parentChkBox = getParentCheckbox(thisChkBox, parentCategoryID, numOfCtrlsInPage)
	
	//Just check if all the childs of the parent checkbox is already checked
	if(allChildsChecked(parentChkBox.id, parentCategoryID, numOfCtrlsInPage, selProgramID))
	{
		//Automatically check the parent checkbox as all its childs are checked
		parentChkBox.checked = true;					
	}	
	else
	{	
		//Automatically Uncheck the parent checkbox as one of its child is Unchecked		
		if(thisChkBox.id != parentChkBox.id)
		{
			parentChkBox.checked = false;
		}
		//Since atleast one child is unchecked, automatically Uncheck the root checkbox
		getRootCheckBox(numOfCtrlsInPage).checked = false;
	}
}

//This will check/uncheck the "All Subprograms" checkbox based on status passed
function getRootCheckBox(numOfCtrlsInPage)
{
	var rootChkBox;
	
	//Loop through all the controls in the page
	for(var i=0; i<numOfCtrlsInPage; i++)
	{
		//Assign the current control to a variable
			childControl = document.Form1.elements[i];
			//Check if the control id is equal to "All Subprograms" checkbox id
			if(childControl.id == "subprogid--#") 
			{
				rootChkBox = childControl;
			}
	}	
	//Return the "All Subprograms" checkbox control
	return rootChkBox;
}

function getSelectedSubPrograms(numOfCtrlsInPage)
{
	document.Form1.cbChecked.value = "";
	//Loop through all the controls in the page
	for(var i=0; i<numOfCtrlsInPage; i++)
	{
		//Assign the current control to a variable
		childControl = document.Form1.elements[i];
		//Check if the control id contains the name "subprogid" (it is a subprogram checkbox)
		if(isPresent(childControl.id, "subprogid")) 
		{
			//Check if this checkbox is checked
			if(childControl.checked)
			{
				if(document.Form1.cbChecked.value == "") document.Form1.cbChecked.value = childControl.value;					
				else document.Form1.cbChecked.value = document.Form1.cbChecked.value + "|"+ childControl.value;				
			}
		}
	}	
}

function selectAllCheckboxes(chkStatus)
{
	var numOfCtrlsInPage;	//Number of form controls in the page
	
	//Get the number of form controls present
	numOfCtrlsInPage = document.Form1.length;	
	//first clear the selected subprograms hidden field
	document.Form1.cbChecked.value = "";
	
	//Loop through all the controls in the page
	for(var i=0; i<numOfCtrlsInPage; i++)
	{
		//Assign the current control to a variable
		childControl = document.Form1.elements[i];
		//Check if the control id contains the name "subprogid" (it is a subprogram checkbox)
		if(isPresent(childControl.id, "subprogid")) 
		{
			childControl.checked = chkStatus;
		}
	}
}
