var divStatuses = new Array();
//var x = new Object();
//x.y = 1;


var categoryHasParent = true;
var categoryHasChildren = true;
var categoryNavigationOnly = true;

/* Core function to enable/disable a div.
 * Works by replacing the content of a div with
 * an inactive image and restoring the content
 * back.
 */
function toggleDiv(divid, disable){
	if(disable){
      document.getElementById(divid).style.display = 'none';
	}
	else{		
		if(document.getElementById(divid) != null){		
			document.getElementById(divid).style.display = 'block';
		}
	}
}

/* Initializes the search screen to its 
 * default inactive state.
 */
function initSearchScreen(){
	
	toggleDiv('textboxl', false);
	toggleDiv('criteriasection', false);
	toggleDiv('moreSpecificFlash', true);
	toggleDiv('jumpr', true);
	if(categorizerExists()){
		toggleDiv('navicl', false);
	}else{
		toggleDiv('navicl', true);
	}
	//toggleDiv('jumpl', false);	
	//toggleDiv('categorylist', false);
	toggleSeeekButton();

}

function handleMoreSpecificClick(){

	toggleDiv('navi2', false);
	toggleDiv('navi1', true);
	toggleDiv('navi3', true);
	restoreHelpLabels("left");
	restoreHelpLabels("right");
	resetCategoryFlags();
	toggleSeeekButton();
	toggleGuidesSection(true);
}

function handleTopClick(){

	toggleDiv('jumpl', true);
	toggleDiv('jumpr', true);
	toggleDiv('navi1', true);
	toggleDiv('navi3', true);
	restoreHelpLabels("left");
	restoreHelpLabels("right");
	resetCategoryFlags();
	toggleSeeekButton();
	toggleGuidesSection(true);	
}

function handleMoreGeneralClick(){	

	if(categoryHasParent){			
		toggleDiv('navi2', false);
	}
	else{
		toggleDiv('navi2', true);
	}

	toggleDiv('navi1', true);
	toggleDiv('navi3', true);
	restoreHelpLabels("left");
	restoreHelpLabels("right");
	resetCategoryFlags();
	toggleSeeekButton();
	toggleGuidesSection(true);
}

function toggleGuidesSection(disable){

	if(disable){
		document.getElementById('guides').innerHTML = "<h1 style='color:#A4A4A4;'>GUIDES</h1>";
		restoreHelpLabels("right");
	}
	else{
		document.getElementById('guides').innerHTML = "<h1 style='color:#ffa484;'>GUIDES</h1>";
	}
	toggleDiv('showall', disable);
	toggleDiv('textboxr', disable);	
	toggleDiv('jumpr', disable);
	toggleDiv('hideguidelist', !disable);
	toggleDiv('navicl', disable);

}

function toggleSeeekButton(){
	if(criteriaExists() || !categoryNavigationOnly || daysSinceExists() || categorizerExists()){
		toggleDiv('searchbutton',false);
	}
	else {
		toggleDiv('searchbutton',true);	
	}
}
function toggleSeeekButtonInact(navigationOnly){
if(!criteriaExists() && navigationOnly){
	toggleDiv('searchbutton1',false);
}
else {
	toggleDiv('searchbutton1',true);
}
}

function selectNavCategory(){
	alert("Categories in gray letters are just to help you get to more specific (and more useful) subcategories.\n\nPlease click the More Specific button and pick a subcategory.");
}

function handleCategoryClickAjax(runAjax,navigationOnly){
	
	if(runAjax){
	$.ajax({type: 'GET', 
		url: '/ajax/HandleCategoryClickAjax.jsp', 
		cache: false,
		async:false,
		success: function(data){
			$( "tr.categoryTr1" ).each(function() {
				var filter = "f:ctzrRight:centerRows";
				var match = $(this).attr('id').search(new RegExp(filter, 'i')); 
				if (match >= 0) {             
					$(this).remove();          
					}          
			}); 
			
			if(navigationOnly){
				toggleGuidesSection(true);
			}
			else{
				document.getElementById('guides').innerHTML = "<h1 style='color:#ffa484;'>GUIDES</h1>";
				toggleDiv('showall', false);
				toggleDiv('textboxr', false);
				toggleDiv('hideguidelist', false);
				toggleDiv('navicl', true);
				toggleDiv('jumpr', true);
			}
			
			toggleDiv('f:ctzrRight:scrollAreaHor', true);
			toggleDiv('f:ctzrRight:scrollAreaVert', true);
		
	}
	});
	}
}
var xTimer;
var timeoutID;
function handleCategoryClick(hasParent, hasChildren, navigationOnly,runAjax){
	
	categoryHasParent = hasParent;
	categoryHasChildren = hasChildren;
	categoryNavigationOnly = navigationOnly;
	
	if(hasParent){
		toggleDiv('navi1',false);
		//toggleDiv('navi2', false);
	}
	else{
		toggleDiv('navi1',true);
		//toggleDiv('navi2', true);
	}
	
	if(hasChildren){
		toggleDiv('navi3', false);
	}
	else{
		toggleDiv('navi3', true);
	}
		
	if(navigationOnly){
		toggleSeeekButton();
		toggleSeeekButtonInact(navigationOnly);
		toggleGuidesSection(true);
		stopImg();
		timeoutID = window.setTimeout(startImg, 3000);
		}
	else{
		toggleSeeekButton();
		toggleSeeekButtonInact(navigationOnly);
		restoreHelpLabels("right");
		document.getElementById('guides').innerHTML = "<h1 style='color:#ffa484;'>GUIDES</h1>";
		toggleDiv('showall', false);
		toggleDiv('textboxr', false);
		//toggleDiv('hideguidelist', true);
		stopImg();
	}
	
	toggleDiv('naviad', false);
	handleCategoryClickAjax(runAjax,navigationOnly);
	//window.status='Status '+x.y;
	//x.y=5;

}

function startImg(){
	toggleDiv('moreSpecificFlash', false);
}

function stopImg() 
{  
	toggleDiv('moreSpecificFlash', true);
	window.clearTimeout(timeoutID);
}
function handleCategoryClickCategorize(hasParent, hasChildren, navigationOnly){

	categoryHasParent = hasParent;
	categoryHasChildren = hasChildren;
	categoryNavigationOnly = navigationOnly;
	
	if(hasParent){
		toggleDiv('navi1',false);
		//toggleDiv('navi2', false);
	}
	else{
		toggleDiv('navi1',true);
		//toggleDiv('navi2', true);
	}
	
	if(hasChildren){
		toggleDiv('navi3', false);
	}
	else{
		toggleDiv('navi3', true);
	}
		
}

function handleGuideClick(){

	toggleDiv('navicl', false);
	toggleDiv('searchbutton1',true);
	toggleDiv('searchbutton',false);
	toggleDiv('naviad', false);
}

function handleClearClick(){

	toggleDiv('navicl', true);
	restoreHelpLabels("right");
}

function handleCategoryJumpClick(){

	toggleDiv('jumpl', true);	
	toggleDiv('navi2', false);
	resetCategoryFlags();
	toggleSeeekButton();
	toggleGuidesSection(true);
}

function handleShowallClick(){

	toggleDiv('hideguidelist', true);
	restoreHelpLabels("right");
}

function handleGuidesJumpClick(){

	toggleDiv('jumpr', true);
	toggleDiv('hideguidelist', true);
}

/* Enables/disables Jump buttons when text
 * boxes on their left are typed in.
 * 
 */
function handleTextInput(divid, text){

	if(text.length>=3){
		toggleDiv(divid, false);
	}
	else{
		toggleDiv(divid, true);
	}	
}

function criteriaExists(){
	var zip = document.getElementById('zipCode');
	var miles = document.getElementById('zipRadius');
	var text = document.getElementById('searchText');
	var anyallofthe = document.getElementById('anyallofthe');
	
	var result = (zip!=null && zip.value.length>=5 && isNumeric(zip.value) 
			     && miles!=null && miles.value.length>0 && isNumeric(miles.value))
			     ||(text!=null && text.value.length>0) || anyallofthe!=null;
	if(zip!=null && zip.value.length>=5 && isNumeric(zip.value)){
		zip.value = zip.value.substring(0,5);
	}
	//alert(result);
	return result;
}

function daysSinceExists(){
	var daysSince = document.getElementById('daysSince');
	var result = (daysSince!=null && daysSince.value.length>0);
	return result;
}

function categorizerExists(){
	var ctzrid =$('a.selected1').parent('td').parent('tr.categoryTr1').attr('id');
    if(ctzrid) {
    	return true;
    }else {
    	return false;
    }
 }

function handleOtherCriteria(){
	toggleSeeekButton();
}

function isNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
   { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
      {
         IsNumber = false;
      }
    }
   return IsNumber;
   
}

function restoreHelpLabels(box){
	if(box == "left"){
		var lbox = document.getElementById('searchCategory');
		lbox.value = "Partial Category Name";
		lbox.style.fontStyle = "italic";
	}
	else if(box == "right"){
		var rbox = document.getElementById('searchCategorizer');
		rbox.value = "Partial Guide Name";
		rbox.style.fontStyle = "italic";
	}
}

function resetCategoryFlags(){
	categoryHasChildren = true;
	categoryHasParent = true;
	categoryNavigationOnly = true;
}

String.prototype.startsWith = function(str) 
{return (this.match("^"+str)==str)}

String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}

function startPageDarkenEffect() {
  var searchbutton = document.getElementById("searchbutton");
  var searchbuttonflash = document.getElementById("searchbuttonflash");
  var darkpage = document.getElementById("darkpage");

  var p = findPos(searchbutton);
  searchbuttonflash.style.left = (p[0] + 23) + "px";
  searchbuttonflash.style.top = (p[1] + 8) + "px";

  darkpage.style.visibility = "visible";
  searchbuttonflash.style.visibility = "visible";
  makePageDarkenEffectImpl(8);
}

function makePageDarkenEffectImpl(val) {
  var darkpageImg = document.getElementById("darkpageImg");
  darkpageImg.style.opacity = val / 100;
  darkpageImg.style.filter = "alpha(opacity=" + val + ")";
  if (val < 72) {
    if (browserName == "msie") {
      setTimeout("makePageDarkenEffectImpl(" + (val + 16) + ")", 30);
    } else {
      setTimeout("makePageDarkenEffectImpl(" + (val + 8) + ")", 15);
    }
  } else {
    var searchbuttonflash = document.getElementById("searchbuttonflash");
    searchbuttonflash.style.visibility = "visible";
  }
}

function setModal(modal, img, root) {
  if (!root) {
    root='';
  }
  var darkpageImg = document.getElementById("darkpageImg");
  if (darkpageImg) {
    if (img) {
      darkpageImg.src=img;
    } else if (darkpageImg.src != root+'images/backgr1.png'){
      darkpageImg.src=root+'images/backgr1.png';
    }

    var darkpage = document.getElementById("darkpage");
    var opa;
    if (modal) {
      opa = 50;
      darkpage.style.visibility = "visible";
    } else {
      opa = 0;
      darkpage.style.visibility = "hidden";
    }

    darkpageImg.style.opacity = opa / 100;
    darkpageImg.style.filter = "alpha(opacity=" + opa + ")";
  }
}


var dialogShown = {};

function showHideDialog(dialogDivId, root) {
  if (!dialogShown[dialogDivId]) {
    dialogShown[dialogDivId] = true;
    setModal(true, null, root);
  } else {
    dialogShown[dialogDivId] = false;
    setModal(false, null, root);
  }

  var obj = document.getElementById(dialogDivId);
  if (dialogShown[dialogDivId])
    obj.style.visibility = 'visible';
  else
    obj.style.visibility = 'hidden';
}


function startAjax(btn, event, execute, render, listener) {
  startWaitCursor();
  jsf.ajax.request(btn, event, {execute:execute,render:render,listener:listener}); 
  return false;
}

function enableDisableField(checkboxFieldId, fieldId) {
  //alert("clicked"+$('#'+checkboxFieldId).attr('checked'));
  var checkbox = document.getElementById(checkboxFieldId);
  var field = document.getElementById(fieldId);
  if($('#'+checkboxFieldId).attr('checked')==true)
  {
	  $('#'+fieldId).removeAttr('disabled'); 
	  $('#'+fieldId).val('0');
  }
  else {
	  $('#'+fieldId).val('');
	  $('#'+fieldId).attr('disabled','disabled');
  }
  //field.enabled = checkbox.checked;
}

/*
Show/Hide Individual or Organization information
*/
function showHideById(id){
	
	var nameTable = document.getElementById('nameTable');
	var orgTable = document.getElementById('orgTable');
	
	if( id=='f:individualLink1' || id=='f:individualLink2'){
		if(orgTable!=null || orgTable !=undefined){
			orgTable.style.display = 'none';	
		}

		if(nameTable!=null || nameTable !=undefined){
			nameTable.style.display = 'block';		
		}
	} 

	if(id=='f:organizationLink1' || id=='f:organizationLink2'){
					
		if(nameTable!=null || nameTable !=undefined){
			nameTable.style.display = 'none';
		}

		if(orgTable!=null || orgTable !=undefined){
			orgTable.style.display = 'block';					
		}
	}
	

}

/*
 * validate email address field
 */
function validateEmailAddress() {
	var email = document.getElementById('f:emailaddress');
	if(email){
		//var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
		var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
		if (!filter.test(email.value)) {
			//alert('Invalid E-mail ID');
			alert("The e-mail address you entered can't be interpreted.\n Please check the value you entered for typos.  If you don't find any errors, please use the Contact function (on the blue menu bar) to let us know what happened, and include the e-mail address you were trying to enter.");
			email.focus
			return false;
		}
	}
}	



function displayThumbnail(actionUrl){
	   var url = $('#url').val();
	   var prefix = "http://";
	   if (url!=null && url.length>0) {
		   
		      var searchResult = checkForHttpProtocol(url);
		      if (searchResult == -1) {
		    	  url = prefix + url;
		      } else if (searchResult > 0) {
		    	  url = url.substr(searchResult);
		      }

	      document.forms[0].url.value = url;
	      document.forms[0].action = actionUrl;
	      scrollHelper(document.forms[0]);
	      document.forms[0].submit();
	}
}

function showBgColor(pureNav){

	$('a').parent('td').parent('tr.categoryTr').attr('bgcolor','#ffffe8');

	$("tr.categoryTr td a").each(function() {
		if($(this).hasClass('categoryLight'))
			{
			$(this).css('color','#9e8a82');
			}
		if($(this).hasClass('category')){
			$(this).css('color','#3E3530');
		}
		});
	
	if(pureNav==true){
		
		$('a.selected').parent('td').parent('tr.categoryTr').attr('bgcolor','#9e8a82');
		$('a.selected').css('color','#ffffe8');
		
		}		
	else {

		$('a.selected').parent('td').parent('tr.categoryTr').attr('bgcolor','#dfaf49');
		$('a.selected').css('color','#ffffe8');
	}
		}

function processEnterJumpCategory(e) {
	   var keynum = 0;
	   if (window.event) {
	      keynum = e.keyCode
	   } else if (e.which) {
	      keynum = e.which
	   }
	   if (keynum == 13) {
		   checkAndSubmitCategory();
	   }
	}

function processEnterJumpCategory2(e,actionURL) {
	   var keynum = 0;
	   if (window.event) {
	      keynum = e.keyCode
	   } else if (e.which) {
	      keynum = e.which
	   }
	   if (keynum == 13) {
		   jumpToCategory2(actionURL);
	   }
	}

function processEnterJumpCategorizer(e) {
	   var keynum = 0;
	   if (window.event) {
	      keynum = e.keyCode
	   } else if (e.which) {
	      keynum = e.which
	   }
	   if (keynum == 13) {
		   checkAndSubmitCategorizer();
	   }
	}

