FavCatMenu = new Array;

url			 = document.location.href;
xend		 = url.lastIndexOf("/") + 1;
var base_url = site_url+"Post/";

// FavCatMenu is called by the select boxes, and by init
// if this is a new category
// if we are re-populating, we need to follow anonther process
 function trimAll(strValue) {
/************************************************
DESCRIPTION: Removes leading and trailing spaces.

PARAMETERS: Source string from which spaces will
  be removed;

RETURNS: Source string with whitespaces removed.
*************************************************/ 
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }
    
   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  //alert(strValue);
  return strValue;
}
var arSelected = new Array(); 

function getMultiple(ob) { 
	//while (ob.selectedIndex != -1){ 
				//if (ob.selectedIndex != 0) 
					arSelected.push(ob.options[ob.selectedIndex].value); 
					//ob.options[ob.selectedIndex].selected = false; 
	//} // You can use the arSelected array for further processing. 

	//alert(arSelected);
}
function FavcatMenu(loc) {
	var Id;
	var hasChild;
	var ParentID;
 // make sure user didn't click on an empty box
  if ((loc != -1) && (FavCatMenu[loc].length <= 0) )
   {
   	return;
   }
  // get the chosen category from this FavCatMenu
  if (loc == -1)
   {
   	Id = 0;
    hasChild = 1;
   }
  else
   {
    if (FavCatMenu[loc].selectedIndex == -1)
     {
		return;
     }else{
	
	  if(loc == 0){
		  
		   if(in_array(FavCatMenu[loc].options[FavCatMenu[loc].selectedIndex].value,arSelected) == false){
			    arSelected.push(FavCatMenu[loc].options[FavCatMenu[loc].selectedIndex].value); 
				//alert(FavCatMenu[loc].selectedIndex);
		   }
		   
	  }

  	   var IdB		= FavCatMenu[loc].options[FavCatMenu[loc].selectedIndex].value;
	   var IdSplit  = IdB.split("=");
	   //alert(loc);
  	   Id			= IdSplit[0];
	   hasChild		= IdSplit[1];
	 }
   } 

    if (hasChild == 1)
  	{ 
		for (i = loc + 1; i < FavCatMenu.length; i++)
		 { 
			FavCatMenu[i].length = 0;
			FavCatMenu[i].options[0] = new Option("-----Please Select-----");
		 }   
		// clear out expanded menus, if there are some
	   if (xmlhttp) 
		 {
			var pos = loc + 1;
			loadfavIt(Id,pos,0);
		 }
		else 
		 {
		  alert("Your browser is outdated, and no longer supported.");
		 }
		if(parseInt(Id) > 0){
		       document.PostListing.Category1.value = Id;  
		}else if(loc >= 0){
			   
			   var IdBP		 = FavCatMenu[loc-1].options[FavCatMenu[loc-1].selectedIndex].value;
			   var IdBSplit  = IdBP.split("=");
			   ParentID		 = IdBSplit[0];

			   document.PostListing.Category1.value = ParentID;  
		}
   }else{
	  
		for (i = loc + 1; i < FavCatMenu.length; i++)
		 { 
			FavCatMenu[i].length = 0;
		    FavCatMenu[i].options[0] = new Option("-----No Sub-Category-----");
		 }   
	    var catlevel = document.PostListing.CATLEVEL.value;
		var loclen = loc + 1;
		if(loclen < 3){
		loclen = 3;
		}
		for (kk = loclen; kk <= catlevel; kk++)
		{
			document.getElementById('FavCatMenu_'+kk).style.display = "none";
			
		}
		if(parseInt(Id) > 0){
				document.PostListing.Category1.value = Id;
		}else{
			
			   var IdBP		 = FavCatMenu[loc-1].options[FavCatMenu[loc-1].selectedIndex].value;
			   var IdPSplit  = IdBP.split("=");
			   ParentID		 = IdPSplit[0];
			   
			   if(parseInt(ParentID) > 0){
			   document.PostListing.Category1.value = ParentID;  
				}
		}
	}
}

function loadfavIt(Id,loc,sel) {

	var getUrl = base_url + "Categories.php?mode=forward&id="+Id;
	
	xmlhttp.open("GET", getUrl,false);
	xmlhttp.send(null) 
	var xResponse = xmlhttp.responseText;
	var sResponse = xResponse.split("\n");
	
	 // loop through the sResponse array
	 for (var i=0; i<sResponse.length; i++)
	  {
		
	   //split the row into its values
	   var sValues = sResponse[i].split("||");

	   CatId = sValues[0] + "=" + sValues[2];
	   var CatName;

	   if (sValues[2] > 0 )
		{
			CatName = sValues[1] + " ->";
		}
	   else
		{
			 CatName = sValues[1];
		}
		
	   if (CatName)
		{	
				
				FavCatMenu[loc].options[i] = new Option(CatName,CatId);
				if(sel){
					
					if ((trimAll(CatId) == trimAll(sel)))
					  {	 
						FavCatMenu[loc].options[i].selected = true;
					  }else{
						FavCatMenu[loc].options[i].selected = false;
					  }
				}else{
					FavCatMenu[loc].options[i].selected = false;
				}
		}
	  }
	
	var SelCategory = document.PostListing.Category1.value;
	var catlevel    = document.PostListing.CATLEVEL.value;

	var loopstart;
	var loclen = loc;

	if(loclen > catlevel){
		loclen = catlevel;
	}
	if(sel != "" || sResponse.length > 1){
		document.getElementById('FavCatMenu_'+loclen).style.display = "";
	}
	
	if(loclen < 3){
		loopstart = 3;
	}else if(sResponse.length > 0){
		loopstart = loclen + 1; 
	}else{
	    loopstart = loclen + 1;
	}
	
	for (kk = loopstart; kk <= catlevel; kk++)
	{
		document.getElementById('FavCatMenu_'+kk).style.display = "none";
	}
	if(parseInt(Id) > 0){
	 document.PostListing.Category1.value = Id; 
	}
}  


function initFavoriteMenu() {
	
 j = 0;
 
 for (i = 0; i < document.PostListing.elements.length; i++)
  {
	 
	  if (document.PostListing.elements[i].name.indexOf("FavCatMenu") != -1){
		 FavCatMenu[j++] = document.PostListing.elements[i];
	  }
  }
 var initialId = document.PostListing.Category1.value;


 if (parseInt(initialId) >= 0)
  {
   if (xmlhttp) 
    {
	    

    	var getUrl = base_url + "Categories.php?mode=reverse&id=" + initialId;
		 
		 xmlhttp.open("GET", getUrl,false);
		 xmlhttp.send(null) 
		 var xResponse = xmlhttp.responseText;
		 var sResponse = xResponse.split("\n");
		 var parentId = 0;
		 var load = 0;

		 for (var i=0; i<sResponse.length; i++)
		  {
			 if(trimAll(sResponse[i]) != ""){
				   //split the row into its values
				   var sValues = sResponse[i].split("||")
				   if (trimAll(sValues[0]) != "")
					{
					   CatId = sValues[0] + "=" + sValues[1];
					 
					}else{
						CatId = 0;
					}
					
				   loadfavIt(parentId,load,CatId);
				   parentId = sValues[0];
				   load++;
			 }
		  }     
		
		

  	 }
  	else
	{
		alert("Your browser is outdated, and no longer supported.");
	}
  }
 else
 	{ 
		 FavcatMenu(-1);
  } 
}

