// Client-side methods associated with the tree control.
// Author: Brian Kerr-Jung
// Date: 3-29-2004
//When a checkbox is checked all the descendents need to be as well.
//If all siblings are checked, then the parent should be too.
function checkFamily(nodeId)
{
	// Check all children
	childPanelId = "child"+nodeId
	
	// If a child tree exists
	if(document.getElementById(childPanelId)!= null)
	{
		// Get all the checkboxes in the child tree panel (all descendent checkboxes)
		elms = document.getElementById(childPanelId).getElementsByTagName('input')
		//alert(elms.length)
		// Loop through the checkboxess and check them all
		for(i=0; i<elms.length; i++)
		{
			elms[i].checked = true
		}
	}
	
	// Check relavent ancestors
	
	// Split the id into an array
	levelsAr = nodeId.split('.')
	
	// Assume all are checked
	allChecked = true;
	
	// Loop through ascending up from last descendent
	while(levelsAr.length > 0)
	{
		// Remove last element to get parent id
		levelsAr.pop()
		
		// Put the array back together into a string
		currentNode = levelsAr.join('.')
		
		// If there's anything left
		if(currentNode.length>0){
			
			// Get children from the panel containing the children
			elm = document.getElementById("child"+currentNode).firstChild
			
			// loop through children
			while(elm != null)
			{
				// firstChild is tbody, first child is the table row, first child of row is cell, next sibling is cell containing checkbox, 
				// first child is the checkbox itself
				inputElms = elm.getElementsByTagName("input");
				if(!inputElms[inputElms.length-1].checked){
				//if(!elm.firstChild.firstChild.firstChild.nextSibling.firstChild.checked){
					
					// once an unchecked box is found the party is over
					allChecked = false
					break
				} 
				
				// move to the next sibling
				elm = elm.nextSibling
			}
			// jump out of the outer loop too
			if(!allChecked) break;
			
			// if we've made it this far, the children are all checked so the parent should be too
			parentInputs = document.getElementById(currentNode).firstChild.getElementsByTagName("input");
			parentInputs[parentInputs.length -1].checked = true;
		}
	}
}


// When a checkbox is unchecked, all the descendents and all the ancestors need to be too.
function uncheckFamily(nodeId)
{
	
	
	// Create the id for the child panel
	childPanelId = "child"+nodeId
	
	// If a child panel exists
	if(document.getElementById(childPanelId)!= null)
	{
		// Get all the checkboxes which are descendent
		elms = document.getElementById(childPanelId).getElementsByTagName('input')
		
		// Loop through the checkboxes and uncheck them all
		for(i=0; i<elms.length; i++)
		{
			elms[i].checked = false
		}
	}
	
	// Uncheck the ancestors
	idElms = nodeId.split('.')
	
	// Start with the root element
	rootElmId = idElms[0]
	
	// Uncheck the root element
	//document.getElementById(rootElmId).firstChild.nextSibling.checked = false
	//alert(document.getElementById(rootElmId).innerHTML)
	rootInputs = document.getElementById(rootElmId).firstChild.getElementsByTagName("input");
	rootInputs[rootInputs.length-1].checked = false;
	
	// Copy root element for loop
	nextElm = rootElmId
	
	// Loop through all the levels
	for(i=1; i< idElms.length; i++)
	{
		// Add the next level down to the id
		nextElm = nextElm +"." + idElms[i]
		
		// Get the div element. The first child is the link button, the nextsibling is the checkbox.
		tableInputs = document.getElementById(nextElm).firstChild.getElementsByTagName("input")
		tableInputs[tableInputs.length -1].checked = false;
	}
	
}// Added for SEL-940 by CSC India on 9/01/2009
function addDisableLink(){	colortab.style.display = "none";	colortabnoshow.style.display = "";}// End for SEL-940
// Capture the event, split into checked or unchecked events
//function nodeClicked(nodeId)
//{	
	// Get the div element. The first child is the link button, the nextsibling is the checkbox.
	// If the checkbox is checked, fire checked event, otherwise fire unchecked event.
	//if(document.getElementById(nodeId).firstChild.firstChild.firstChild.nextSibling.firstChild.checked)
	//{
//		checkFamily(nodeId)
//	} else 
//	{
//		uncheckFamily(nodeId)
//	}
//}
function nodeClicked(clickedElm,nodeId)
{	
	// Get the div element. The first child is the link button, the nextsibling is the checkbox.
	// If the checkbox is checked, fire checked event, otherwise fire unchecked event.
	//if(document.getElementById(nodeId).firstChild.firstChild.firstChild.nextSibling.firstChild.checked)
	if(clickedElm.checked)
	{
		checkFamily(nodeId)
	} else 
	{
		uncheckFamily(nodeId)
	}
}

// Show or hide Child panels.  The caller is the element clicked, the node id will be
// used to find the panel to show or hide.
function collapseChild(caller, nodeId)
{
	//toggle it
	if(document.getElementById("child"+nodeId).style.display=="none")
	{		
		// Make panel visible
		document.getElementById("child"+nodeId).style.display="block"
		// change the link button
		caller.innerHTML = "~"
	} else 
	{	
		// Make panel invisible
		document.getElementById("child"+nodeId).style.display="none"
		// Change the link button
		caller.innerHTML = "+"
	}
	
	// Return false to keep the button from posting back.
	return false;
}

function openStandardPopup(url)
{

	var myLeft = (screen.width-500)/2;
    var myTop = (screen.height-300)/2;

	newWin=window.open(url,"","width=500,height=300,toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=0,scrollbars=yes,left="+myLeft+",top="+myTop)
	
}

function openCustomPopup(url, winWidth, winHeight, showScrollbars)
{

	var myLeft = (screen.width-winWidth)/2;
    var myTop = (screen.height-winHeight)/2;

	newWin=window.open(url,"","width="+winWidth+",height="+winHeight+",toolbar=0,location=0,directories=0,status=0,menubar=0,resizable=0,scrollbars="+showScrollbars+",left="+myLeft+",top="+myTop)
	
}
function openErrorPopup(url, winWidth, winHeight){
	openCustomPopup(url, winWidth, winHeight, false)
	// returning false will prevent further action
	return false
}
/*
==================================================================
LTrim(string) : Returns a copy of a string without leading spaces.
==================================================================
*/
function LTrim(str)
/*
   PURPOSE: Remove leading blanks from our 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));
}


//Client Side script for managing select lists (Executive Ranking and sorting
//User Defined Column lists
//Author: Brian Kerr-Jung
//Created : 5/14/2004

// Move selected options from one select list to another
	function moveOptions(senderSelect, receiverSelect)
	{
	
	 for(i=0;i<senderSelect.options.length;  i++){
		var senderSelectedIndex = i
		var receiverLength = receiverSelect.options.length
		var senderOption = senderSelect.options[i]

		if(senderOption.selected){

			var optionObj = new Option(senderOption.text, senderOption.value, false, false);
 
			receiverSelect.options[receiverLength] = optionObj
	 		
	 		
			}
		}
		//loop through backwards to removed items as the length will shrink when items are removed
		for(i=senderSelect.options.length-1; i>-1 ;i--){
			if(senderSelect.options[i].selected){
				senderSelect.options[i] = null;
			}
		}	
	}
	
	function moveUp(selectList)
	{
		for(i=0; i< selectList.options.length; i++){
			if(selectList.options[i].selected){
		 		var selectedItemIndex = i
		 		if(selectedItemIndex > 0){
				var bottomOption = selectList.options[selectedItemIndex]
		 		var topOption	=	selectList.options[selectedItemIndex-1]
		 		var upOptionObj = new Option(bottomOption.text, bottomOption.value, false, true);
		 		var downOptionObj = new Option(topOption.text, topOption.value, false, false);
		 		selectList.options[selectedItemIndex-1] = upOptionObj
		 		selectList.options[selectedItemIndex] = downOptionObj;
				} else{
					break;
				}
			}
	 	}
	}
	function moveDown(selectList)
	{
		//for(i=document.forms[0].ExecRight.options.length-1; i>-1; i--){
		for(i=selectList.options.length-1; i>-1; i--){
			if(selectList.options[i].selected){
		 		var selectedItemIndex = i
		 		if(selectedItemIndex < selectList.options.length-1){
				var bottomOption = selectList.options[selectedItemIndex+1]
		 		var topOption	=	selectList.options[selectedItemIndex]
		 		var upOptionObj = new Option(bottomOption.text, bottomOption.value, false, false);
		 		var downOptionObj = new Option(topOption.text, topOption.value, false, true);
		 		selectList.options[selectedItemIndex] = upOptionObj
		 		selectList.options[selectedItemIndex+1] = downOptionObj;
				} else{
					break;
				}
			}
	 	}
	}
	function selectAll(listBox)
	{
		for(i=0; i<listBox.options.length; i++)
		{
			listBox.options[i].selected=true
		}
	return true;
	}
// END Select List Script	
	
		// Used by registration pages to validate length of input
		function ClientValidateUsername(source, arguments)
		{
				if (arguments.Value.length < 3)
					arguments.IsValid = false;
				else
					arguments.IsValid = true;
					
		}
		function ClientValidatePassword(source, arguments)
		{
				if (arguments.Value.length < 5)
					arguments.IsValid = false;
				else
					arguments.IsValid = true;
					
		}

function checkForCompanyAbbrev(textBox)
{

  noiseWords = new Array("Limited Liability Partnership","LLP","Limited Liability Corporation",
"LLC","Company", "Co","Corporation","Corp","Incorporated","Inc", "Ltd","del", "delaware","usa","pllc",
"ii","pc","Limited Partnership","LP","Prtnr","Companies")
//noiseWords = new Array("LLP","LLC","Co","Corp","Inc","LLC","Inc","Ltd","LLP","lp","pllc","pc","Co''s")

inputWords = textBox.value.split(" ")


 //alert(noiseWords.length) 
 for(i=0; i< noiseWords.length; i++)
 {
    
    for(j=0;j < inputWords.length; j++){
    if(inputWords[j].toLowerCase()==noiseWords[i].toLowerCase() ||
       inputWords[j].toLowerCase()=="(" + noiseWords[i].toLowerCase()+")" ||
        inputWords[j].toLowerCase()==","+noiseWords[i].toLowerCase())
      {
        message = "Tip: You may unintentionally be restricting your search results by entering one or more \"noise\" words. To improve your search results, do not include these words in your search.\n\n"
        message += "Noise words include the following: "
        //LLP, LLC, Co, Corp, Inc, LLC, Inc, Ltd, LLP, lp, pllc, pc, Co's\n\n"
        message += "Limited Liability Partnership, LLP, Limited Liability Corporation, LLC, Company, Co, Corporation, Corp, Incorporated, Inc, Ltd, del, delaware, usa, pllc, ii, pc, Limited Partnership, LP, Prtnr, Companies\n\n"
        message += "To continue with your search as you have entered it, click 'OK.'\n"
        message += "To edit your entry click 'Cancel.'" 
        return confirm(message)
      }
    }
  }
  return true
  
}

function launchProgress() 
{
    
    newURL  = "/Selectory/SearchPages/progress.htm"
    newName = "progress"
    orgName = "parentWin"
    //alert(window.screenLeft)
    if(window.screenLeft!=null){
      winLeft = (document.body.offsetWidth - 300) / 2 + window.screenLeft;
      winTop  = (document.body.offsetHeight - 300) / 2 + window.screenTop;
    } else {
      winLeft = (document.body.offsetWidth - 300) / 2 + window.screenX;
      winTop  = (document.body.offsetHeight - 300) / 2 + window.screenY;

    }
    //alert(winLeft+","+winTop)

    var newFeatures="height=300,width=300,scrollbars=no,resizeable=no,menubar=no,statusbar=no,locationbar=no,left="+winLeft+",top="+winTop
    //alert(newFeatures)
    var remote = window.open(newURL, newName, newFeatures);
    
    if (remote.opener == null) // if something went wrong
        remote.opener = window;
    remote.opener.name = orgName;
    return remote;
    
}

function addSubmitEvent(func) 
{    
        var oldsubmit = document.forms[0].onsubmit;
       if (typeof document.forms[0].onsubmit != 'function') {
            document.forms[0].onsubmit = func;
        } else {
          document.forms[0].onsubmit = function() {
            oldsubmit();
            func();
           }
        }
}
	


