

$(document).ready(setUpDocument);

function setUpDocument(jqRef) {
   $("#meta_navigation > ul > li:not(:first)").css({"border-left":"solid #5a5a5a 1px", "margin":"0 0 0 3px", "padding":"0 0 0 3px"});
   $("#SubNavigation > ul > li:not(:has(ul))").css({"padding":"3px 0 3px 20px"});

   $("#top_navigation").mouseleave(hideAllTopNavigation);
   $("#top_navigation ul li").mouseenter(handleListItem);
   $("#top_navigation ul li").mouseleave(handleListItem);
   $("#top_navigation ul li a").mouseenter(handleLink);

   $(".children_summary ul li").mouseenter(handleChildrenSummaryListItem);
   $(".children_summary ul li").mouseleave(handleChildrenSummaryListItem);
}

function handleListItem(event) {
   var target = $(event.target);
   if(event.type == "mouseenter") {
      target.siblings("li").each(function(index, value) {
         if($(this).children("ul").css("display") != "none") $(this).children("ul").hide();
      });
      target.children("ul").show();
   }
   if(event.type == "mouseleave") {
      target.children("ul").hide();
   }
}

function handleLink(event) {
   var target = $(event.target);
   if(event.type == "mouseenter") {
      target.parent("li").siblings("li").each(function(index, value) {
         if($(this).children("ul").css("display") != "none") $(this).children("ul").hide();
      });
      target.parent("li").children("ul").show();
   }
}

function hideAllTopNavigation() {
   $("#top_navigation > ul > li > ul > li > ul > li > ul").hide();
   $("#top_navigation > ul > li > ul > li > ul").hide();
   $("#top_navigation > ul > li > ul").hide();
   //$("#top_navigation > ul > li > ul").hide();
}

function handleChildrenSummaryListItem(event) {
   var target = $(event.target);
   if(target.is("a")) target = target.parent("li");

   if(event.type == "mouseenter") {
      target.css({"background-color":"#DFF6CC", "border-bottom":"solid white 1px"});
      target.children("a").css({"color":"#339933"});
   }
   else {
      target.css({"background-color":"#339933", "border-bottom":"solid #DFF6CC 1px"});
      target.children("a").css({"color":"#DFF6CC"});
   }
}

// Old Methods

function disp_text() {
   var w = document.getElementById('mylist').selectedIndex;
   var url = document.getElementById('mylist').options[w].value;
   window.location = url;
}

function SetCookie(name,value,days,path,domain,secure){
var now = new Date();
var out = new Date(now.getTime() + days * 24 * 60 * 60 * 1000);
	var curCookie=name+"="+escape(value)+
	((days)?"; expires="+out.toGMTString():"")+
	((path)?"; path="+path:"")+
	((domain)?"; domain="+domain:"")+
	((secure)?"; secure":"");
	document.cookie=curCookie;
}




function GetCookie(sName)
{
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  // a cookie with the requested name does not exist
  return null;
}




function urlQueryString(variable) 
{ 
    var query = window.location.search.substring(1); 
    var vars = query.split("&"); 

    for (var i=0;i<vars.length;i++) 
   { 
       var pair = vars[i].split("="); 
       if (pair[0] == variable) 
       { 
        return pair[1]; 
       } 
    } 
return null;
}





function redirect (list)
{
	if (list.options[list.selectedIndex].value != "#")
	{
		window.location.href = list.options[list.selectedIndex].value;
	}
}





function noenter() 
{
    return !(window.event && window.event.keyCode == 13); 
}




function IsNumeric(strString)
   //  check for valid numeric strings		 
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }




function doZipCode(id)
{

var zip = document.getElementById(id);

if (IsNumeric(zip.value)==false)
    {
       alert("Zip Codes must only contain numbers.");
        zip.focus();
        zip.select()
       return false;
     }

else if (zip.value.length != 5)
    {
        alert("Zip codes must be 5 digits.");
        zip.focus();
        zip.select();
        return false;
      }
else
  {
      SetCookie('BBB_com_Zip',zip.value,1,'/','','');
      SetCookie('BBB_com_PestRepID','-1',1,'/','','');
      SetCookie('BBB_com_GolfRepID','-1',1,'/','','');
      SetCookie('BBB_com_LawnRepID','-1',1,'/','','');
      SetCookie('BBB_com_VectorRepID','-1',1,'/','','');
      SetCookie('BBB_com_ForestRepID','-1',1,'/','','');
      SetCookie('BBB_com_NoFireAntsRepID','-1',1,'/','','');
   }

window.location.reload();
}

function Logout()
{
SetCookie('BBB_com_FirstName','-1',1,'/','','');
SetCookie('BBB_com_Zip','-1',1,'/','','');
SetCookie('BBB_com_PestRepID','-1',1,'/','','');
SetCookie('BBB_com_GolfRepID','-1',1,'/','','');
SetCookie('BBB_com_LawnRepID','-1',1,'/','','');
SetCookie('BBB_com_VectorRepID','-1',1,'/','','');
SetCookie('BBB_com_ForestRepID','-1',1,'/','','');
SetCookie('BBB_com_NoFireAntsRepID','-1',1,'/','','');
SetCookie('BBB_com_Profession','-1',1,'/','','');
SetCookie('BBB_com_promoCode','-1',1,'/','','');
}



/* Function needed for (1) PageDiscussion Module and (2) the validation of the formulargenerator. */
function getFieldValue ( theField, vType){
	//this function will return the field value (or value list) based on the element type
	theValue=""; 
	sep=";"; 
	hits=0;
	vType=vType.toLowerCase();
	//text is the user-entered value as a string
	if(vType=="text" ) return(theField.value); 
	//textarea is the user-entered value as a string array of one element
	if(vType=="textarea" ) return(theField.value);
	//select is an array of selection pointers to an array of strings representing the choices
	if(vType=="select"){
		for ( i=0; i<theField.options.length; i++){
			if(theField.options[i].selected){
				hits++;
				if(theField.options[i].value==""){
					e=theField.options[i].text;
				}else{ 
					e=theField.options[i].value;
				}
				if(hits==1){
					theValue=e;
				}else{
					theValue+= sep+e;
				}
			}
		}
		return(theValue);
	}
	if(vType=="dropdown"){
		if(theField.options[0].selected){
			return("");
		}
		return("True");
	}
	// check Emailaddress
	if(vType=="mail"){
		var mail=theField.value;
		if(mail==""){
			return ("");
		}else{
			var erg =mail.search(/.+@..+\...+/);
			if(erg==-1){
				return ("");
			}
			var erg=mail.search(/\s/);
			if(erg!=-1){
				return ("");
			}
		}
		return (mail);
	}
	//checkboxes & radio buttons
	if(vType=="checkbox"||vType=="radiobutton"){
		if(theField.value==null){
			//if we're here, we are validating a radio button or a nn multi-element checkbox
			for ( i=0; i<theField.length; i++){
				if(theField[i].checked){
					hits++;
					if(hits==1){
						theValue=theField[i].value;
					}else{
						theValue+= sep+theField[i].value;
					}
				} 
			}
		} 
		return(theValue);
	}else{
		return(theField.value);
	}
}






function addToFavoritesEN(pagename){
	var errmsg="Your browser does not support automated adding to favourites, probably you are not using Internet Explorer. However, please yuse your browsers menu command for bookmarking instead. Thank you!";
	return addToFavorites(pagename,errmsg);
}

function addToFavorites(pagename,errmsg){
var adress=window.location.href;
if(document.all){
window.external.addFavorite(adress,pagename);
}
else{
alert(errmsg);
}
}


function checkContent(field, content)
{
var f = document.getElementById(field);

if (content == "HTML") 
{
   var htmlContent = new Array();
   htmlContent[0] = "href"
   htmlContent[1] = "<a"
   htmlContent[2] = "<br>"
   htmlContent[3] = "<ul"
   htmlContent[4] = "<ol"
   htmlContent[5] = "function"
   htmlContent[6] = "{"
   
   for (i=0;i<htmlContent.length;i++)
   {
       if (f.value.indexOf(htmlContent[i])>-1)
       {
           alert("HTML code is not allowed.");
           return false;
       }
   }

  }
  else
  {
    
    if (f.value.indexOf(content)>-1)
    {
       alert("HTML code is not allowed.");
       return false;
    }
  }
}

