CatMenu = new Array;

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

// catMenu 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;
}

function catMenu(loc) {
	var Id;
	var hasChild;
	
 // make sure user didn't click on an empty box
  if ((loc != -1) && (CatMenu[loc].length <= 0) )
   {
   	return;
   }
  // get the chosen category from this CatMenu
  if (loc == -1)
   {
   	Id = 0;
    hasChild = 1;
   }
  else
   {
    if (CatMenu[loc].selectedIndex == -1)
     {
		return;
     }else{
		 
  	   var IdB		= CatMenu[loc].options[CatMenu[loc].selectedIndex].value;
	   var IdSplit  = IdB.split("=");
  	   Id			= IdSplit[0];
	   hasChild		= IdSplit[1];
	 }
   } 

    

  
  if (hasChild == 1)
  	{ 
		for (i = loc + 1; i < CatMenu.length; i++)
		 { 
		  CatMenu[i].length = 0;
		 
		  CatMenu[i].options[0] = new Option("-----Please Select-----","");
		 }   
		// clear out expanded menus, if there are some
	   if (xmlhttp) 
		 {
			var pos = loc + 1;
			loadIt(Id,pos,0);
		 }
		else 
		 {
		  alert("Your browser is outdated, and no longer supported.");
		 }
	  document.PostListing.Category1.value = "";
	  document.getElementById('CategoryNew').value = Id;
   }else{
		for (i = loc + 1; i < CatMenu.length; i++)
		 { 
			CatMenu[i].length = 0;
		    CatMenu[i].options[0] = new Option("-----No Sub-Category-----","");
			CatMenu[i].options[0].disabled='disabled';
		 }   
	   var catlevel = document.PostListing.CATLEVEL.value;
		var loclen = loc + 1;
		if(loclen < 3){
		loclen = 3;
		}
		for (kk = loclen; kk <= catlevel; kk++)
		{
			document.getElementById('CatMenu_'+kk).style.display = "none";
		}
		document.PostListing.Category1.value = Id;  
		document.getElementById('CategoryNew').value = "";
	}
}

function loadIt(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)
		{	
				var locdec = loc - 1;
				if(locdec >= 1){
						   var IdBp		= CatMenu[locdec].options[CatMenu[locdec].selectedIndex].value;
		  
						   var IdpSplit  = IdBp.split("=");
						   var Idp			= IdpSplit[0];
						   var hasChildp	= IdpSplit[1];

						   if(hasChildp == 1){
								//CatMenu[loc].options[0] = new Option("-----Please Select-----");
							}else{
								CatMenu[loc].options[0] = new Option("-----No Sub-Category-----","");
							}
				}else{

					//CatMenu[loc].options[0] = new Option("-----Please Select-----");
				}

				CatMenu[loc].options[i] = new Option(CatName,CatId);
				if(sel){
					
					if ((trimAll(CatId) == trimAll(sel))){	 
						CatMenu[loc].options[i].selected = true;
					  }else{
						CatMenu[loc].options[i].selected = false;
					 }
					  
				}else{
					CatMenu[loc].options[i].selected = false;
				}
		}
	  }
	 
	if(CatMenu[loc].selectedIndex > 0){
	   var IdB		= CatMenu[loc].options[CatMenu[loc].selectedIndex].value;
	  
	   var IdSplit  = IdB.split("=");
  	   Id			= IdSplit[0];
	   hasChild		= IdSplit[1];

	   var locinc			   = loc+1;
	   CatMenu[locinc].length  = 0;

		if(hasChild == 1){
			CatMenu[locinc].options[0] = new Option("-----Please Select-----","");
		}else{
			CatMenu[locinc].options[0] = new Option("-----No Sub-Category-----","");
		}
	}
	
	

	var SelCategory = document.PostListing.Category1.value;
	var catlevel    = document.PostListing.CATLEVEL.value;
	
	var catdis;
	for (catdis = locinc+1; catdis <= catlevel; catdis++)
	{
		CatMenu[catdis].length = 0;
	}

	var loopstart;
	var loclen = loc;

	if(loclen > catlevel){
		loclen = catlevel;
	}
	if(sel != "" || sResponse.length > 1){
		document.getElementById('CatMenu_'+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('CatMenu_'+kk).style.display = "none";
	}


}  


function initMenu() {
	
 j = 0;
 
 for (i = 0; i < document.PostListing.elements.length; i++)
  {
	 
	  if (document.PostListing.elements[i].name.indexOf("CatMenu") != -1){
		 CatMenu[j++] = document.PostListing.elements[i];
	  }
  }
 var initialId = document.PostListing.Category1.value;
	
	if(initialId == ""){
		initialId = document.getElementById('CategoryNew').value;
		catMenu(-1);
	}

 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]) != "" || (parentId > 0 && document.getElementById('CategoryNew').value != "")){
				  //split the row into its values
				   var sValues = sResponse[i].split("||")
				   if (trimAll(sValues[0]) != "")
					{
					   CatId = sValues[0] + "=" + sValues[1];
					 
					}else{
						CatId = 0;
					}
				   loadIt(parentId,load,CatId);
				   parentId = sValues[0];
				   load++;
			 }
		  }     
		
		

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

