var request;
var queryString; //will hold the POSTed data
var ProcMethod;
var ProcessCount;
var theuA;
var StoreID;
var rootUrl;
var ua = navigator.userAgent.toLowerCase();	
var ie = (ua.indexOf('msie')!=-1);
ProcessCount = 1;

function sendData(ProcessMethod){
	//alert(StoreID);
	// var url="https://www.bdlogic.com/" + StoreID + "/cat_ajaxP.asp";
	 ProcMethod = ProcessMethod;
	 httpRequest("POST",postURL,true);
}


/* Initialize a Request object that is already constructed
 reqType: The HTTP request type such as "GET" or "POST." 
 url: The URL of the server program.
 isAsynch: Whether to send the request asynchronously or not. */
 function initReq(reqType,url,isAsynch){
 /* Specify the function that will handle the HTTP response */
 request.onreadystatechange=handleResponse;
 request.open(reqType,url,isAsynch);
 /* set the Content-Type header for a POST request */
 request.setRequestHeader("Content-Type",
 "application/x-www-form-urlencoded; charset=UTF-8");
 //alert(queryString);
 request.send(queryString);
}

/* Wrapper function for constructing a Request object.
 Parameters:
 reqType: The HTTP request type such as GET or POST.
 url: The URL of the server program.
 asynch: Whether to send the request asynchronously or not. */

function httpRequest(reqType,url,asynch){
 //Mozilla-based browsers
 if(window.XMLHttpRequest){
 request = new XMLHttpRequest();
 initReq(reqType,url,asynch);
 theuA = "n";
 } else if (window.ActiveXObject){
 theuA = "i";
 request=new ActiveXObject("Msxml2.XMLHTTP");
 if (! request){
 request=new ActiveXObject("Microsoft.XMLHTTP");
 }
 if(request){
 initReq(reqType,url,asynch);
 /* Unlikely to branch here, as IE uses will be able to use either 
 one of the constructors*/
 } else {
 alert("Your browser does not permit the use of all "+
 "of this application's features!");}
 } else {
 alert("Your browser does not permit the use of all "+
 "of this application's features!");}
}



//event handler for XMLHttpRequest
function handleResponse(){
	var btnHTM;
	btnHTM = '<img border="0" src="../i/update_dim.gif" hspace="2" id="updBtn" '+
				'alt="Inventory Updated" title="Inventory Updated">';
	if(request.readyState == 4){
		if(request.status == 200){
			if (ProcMethod=='LoadTopLevelCats'){				
				SetCat(request.responseText);				
			} else if (ProcMethod=='LoadChildCats'){				
				SetChildCat(request.responseText);				
			} else if (ProcMethod=='LoadOpenCats'){				
				SetOpenCat(request.responseText);				
			}
			
		 } else {
		 alert("A problem occurred with communicating between "+
		 "the XMLHttpRequest object and the server program. \n\n" +
		 "request.status: " +request.status);
		}
 	}//end outer if
}


function postServerMassge(msg,SpanID){
	var theHTM = '';
	var MsgSpanID;
	if (SpanID==''){
		MsgSpanID = 'ProcMsg';
	} else {
		MsgSpanID = SpanID;
	}
	theHTM += "<p><table bgcolor='#FFFF99' style='border: 1px solid #7EBB00' " +
			"border='0' cellpadding='5' cellspacing='0'>";
	theHTM += "<tr>";
	theHTM += "<td align='left' class='BlackM'>" + msg + "</td>";
	theHTM += "<td width='100' align='right' valign='middle' class='BlackSB'>" +
			"<div align='center' class='divButtonGreen' onClick=clearmsg('"+MsgSpanID+"');>OK</div></td>";	
	theHTM += "</tr>";	
	theHTM += "</table></p>";
	
	if (document.getElementById){		
		document.getElementById(MsgSpanID).innerHTML = theHTM;			
	} else if (document.layers){
		document.MsgSpanID.innerHTML = theHTM;
	}
}


function clearmsg(SpanID){
	if (document.getElementById){		
		document.getElementById(SpanID).innerHTML = '';			
	} else if (document.layers){
		document.SpanID.innerHTML = '';
	}
}

	
function setQueryStringCustom(append,FrmObj){
 	//initialize the top-level variable; also reset the variable to cover when
 	//the user clicks multiple times
 	queryString="";

	
	// append any qry tring data passed on
	if (queryString==""){
		queryString += append;	
	} else {
		queryString += append + "&";
	}
	//alert(queryString);
}





function hideactionRows(show,rowid){
	if (show){
		if (document.getElementById){		
			document.getElementById(rowid).style.display = "";
		} else if (document.layers){			
			document.rowid.style.display = "";
		}
	} else {
		if (document.getElementById){		
			document.getElementById(rowid).style.display = "none";
		} else if (document.layers){			
			document.rowid.style.display = "none";
		}
	}
}


function showSpanMsg(el,msg){
	if (document.getElementById){
		document.getElementById(el).innerHTML = msg;
	} else if (document.layers){
		document.el.innerHTML =msg;
	}
}

//-------------------------------------------------- Local Functions -----------------------


function hidePdtSelRow(elmid){
	if (document.getElementById){		
		document.getElementById(elmid).style.display = "none";
	} else if (document.layers){			
		document.elmid.style.display = "none";
	}
}
function showPdtSelRow(elmid){
	if (document.getElementById){		
		document.getElementById(elmid).style.display = "";
	} else if (document.layers){			
		document.elmid.style.display = "";
	}
	if(elmid=='PdtSelectForm'){
		if(document.mainpdtlayout.Suppliers.length==1){
			//alert('load suplier');
			setQueryString('ajaxReqCode=suppliers',''); 
			sendData('suppliers');
		}
	}
}

function LoadOpenCats(acatid){
	
	//prep query string form form post
	setQueryStringCustom('ajaxReqCode=LoadOpenCats&parentID='+acatid,''); 
	
	// send ajax request to server
	sendData('LoadOpenCats');
}

function LoadTopLevelCats(){

	//prep query string form form post
	setQueryStringCustom('ajaxReqCode=LoadTopLevelCats',''); 
	
	// send ajax request to server
	sendData('LoadTopLevelCats');
}

function LoadChildCats(parentID){
	ActiveCatID = parentID;
	
	ToggleLoading();
	//prep query string form form post
	setQueryStringCustom('ajaxReqCode=LoadChildCats&parentID='+parentID,''); 
	
	// send ajax request to server
	sendData('LoadChildCats');
}
function setMenuColor (obj,color) {
	if (document.getElementById)
		document.getElementById(obj).bgColor = color;
	else if (document.layers)
		document.obj.bgColor = color;
}

function setLink(url){
	location.href = url;
}

function getCurrentMenuStatus(id){
	var iName = '';
	if (document.getElementById)
		iName = document.getElementById(id).src;
	else if (document.layers)
		iName = document.obj.bgColor.src;
	if (iName.indexOf("menu_closed")>0){
		return true;
	}
	return false
}
function LoadMenuChildren(parentName, parentID){
	var menuClosed;
	var imgID = 'ivmenu'+parentID;
	var bgID = 'vmenu'+parentID;
	menuClosed = getCurrentMenuStatus(imgID);	
	
	ActiveCatName = unescape(parentName);
	
	if (menuClosed){
		if (document.getElementById)
			document.getElementById(imgID).src = 'i/menu_open.gif';
			if(document.getElementById(bgID)){
				document.getElementById(bgID).bgColor = VARTICAL_COLOR_SELECTED;
			}
		else if (document.layers)
			document.imgID.src =  'i/menu_open.gif';
			if(document.bgID){
				document.imgID.bgColor = VARTICAL_COLOR_SELECTED;
			}
		LoadChildCats(parentID);
	} else {
		if (document.getElementById)
			document.getElementById(imgID).src = 'i/menu_closed.gif';
			if(document.getElementById(bgID)){
				document.getElementById(bgID).bgColor = VARTICAL_COLOR_UNSELECTED;
			}
		else if (document.layers)
			document.imgID.src =  'i/menu_closed.gif';
			if(document.bgID){
				document.imgID.bgColor = VARTICAL_COLOR_UNSELECTED;
			}
		CloseChildCats(unescape(parentName), parentID);
	}	
	
}

function CloseChildCats(parentName, parentID){
	var parentMenuObj = "vMenuSpan" + parentID
	var theHTM;
	theHTM = '<a class="dlBlack" href="'+rootUrl+'dept.asp?dept='+escape(parentName)+'&activeCat='+parentID+'">' + 
			unescape(parentName) + '</a>'
	if (document.getElementById){		
		document.getElementById(parentMenuObj).innerHTML = theHTM;			
	} else if (document.layers){
		document.parentMenuObj.innerHTML = theHTM;
	}	
}
function formatMenuData(catData,ChildMenu){
	var CatsArray;
	var theHTM = '';
	var Columns;
	var menuBgColor;
	var menuStyle;
	if (catData==''){
		theHTM = '';
	} else {
		CatsArray = catData.split('@@@');
		theHTM += '<table border="0" width="100%" cellspacing="0" cellpadding="1">'
		for (var i = 0; i < CatsArray.length; i++) {
			Columns = CatsArray[i].split(';');
			if (Columns[0]==ActiveCatID){
				menuBgColor = VARTICAL_COLOR_SELECTED;
			} else {
				menuBgColor = VARTICAL_COLOR_UNSELECTED;
			}
			//if (ChildMenu){
				//menuStyle = "mainMenuChild";
			//} else {
				menuStyle = "mainMenuParent";
			//}
			if(Columns[2]=='False'){
				theHTM += '<tr>'
				theHTM += '<td valign="top" align="center" width="13" class="'+menuStyle+'" ' +
						'bgcolor="' + menuBgColor + '">' + 
					    '<img src="i/menu_null.gif" border="0" id="ivmenu'+Columns[0]+'"></td>';
				theHTM += '<td valign="top" class="'+menuStyle+'Txt" ' +
						'bgcolor="' + menuBgColor + '" nowrap id="vmenu'+Columns[0]+'" ' +
						'OnMouseOver=setMenuColor("vmenu'+Columns[0]+'","' + VARTICAL_COLOR_ROLLOVER + '") ' +
						'OnMouseOut=setMenuColor("vmenu'+Columns[0]+'","' + menuBgColor + '") ' +
						'onClick=""><span id="vMenuSpan' +Columns[0]+'">'+
						'<a class="dlBlack" href="'+rootUrl+'dept.asp?dept=' + escape(Columns[1]) +
						'&activeCat='+Columns[0]+'">' + Columns[1] + '</a></span></td>';
				theHTM += '</tr>'
												  
			
			} else {
			
				theHTM += '<tr>'
				theHTM += '<td valign="top"  align="center"  width="13" class="'+menuStyle+'" ' +
						'bgcolor="' + menuBgColor + '" >' + 
					    '<img style="cursor:pointer; cursor:hand;" src="i/menu_closed.gif" ' +
						'id="ivmenu'+Columns[0]+'" onClick=LoadMenuChildren("' + escape(Columns[1]) +'",'+Columns[0]+') '+
						'title="Click To Open Sub-Menu" border="0"></td>';
				theHTM += '<td valign="top" class="'+menuStyle+'Txt" ' +
						'bgcolor="' + menuBgColor + '" nowrap id="vmenu'+Columns[0]+'" ' +
						'OnMouseOver=setMenuColor("vmenu'+Columns[0]+'","' + VARTICAL_COLOR_ROLLOVER + '") ' +
						'OnMouseOut=setMenuColor("vmenu'+Columns[0]+'","' + menuBgColor + '") ' +
						'onClick=""><span id="vMenuSpan' +Columns[0]+'">'+
						'<a class="dlBlack" href="'+rootUrl+'dept.asp?dept=' + escape(Columns[1]) +
						'&activeCat='+Columns[0]+'">' + Columns[1] + '</a></span></td>';
				theHTM += '</tr>'
			}
		}
		theHTM += '</table>'
	}
	return theHTM;
}

function SetCat(catData){
	
	var parentMenu = formatMenuData(catData,false);
	if (document.getElementById){		
		document.getElementById('vMenuSpan').innerHTML = parentMenu;			
	} else if (document.layers){
		document.cats.innerHTML = parentMenu;
	}
	//document.debug.debugcode.value=parentMenu;
}

function SetOpenCat(catData){
	//alert(catData);
	if (document.getElementById){		
		document.getElementById('vMenuSpan').innerHTML = catData;			
	} else if (document.layers){
		document.cats.innerHTML = catData;
	}
	//document.debug.debugcode.value=catData;
}

function SetChildCat(catData){
	
	var childMenu;
	var parentMenuObj = "vMenuSpan" + ActiveCatID;
	childMenu = '<font class="BlackSB"><a class="dlBlack" href="'+rootUrl+'dept.asp?dept='+
		escape(ActiveCatName)+'&activeCat='+ActiveCatID+'">'+ unescape(ActiveCatName) + '</a></font>';
	
	childMenu +=  formatMenuData(catData, true);
	
	if (document.getElementById){		
		if (document.getElementById(parentMenuObj)){
			document.getElementById(parentMenuObj).innerHTML = childMenu;		
		}
	} else if (document.layers){
		if(document.parentMenuObj){
			document.parentMenuObj.innerHTML = childMenu;
		}
	}		
}

function ToggleLoading(){
	var parentMenuObj = "vMenuSpan" + ActiveCatID;
	if (document.getElementById){		
		if (document.getElementById(parentMenuObj)){
			document.getElementById(parentMenuObj).innerHTML = '<font class="GrayDrkS">Loading...</font>';		
		}
	} else if (document.layers){
		if(document.parentMenuObj){
			document.parentMenuObj.innerHTML = '<font class="GrayDrkS">Loading...</font>';
		}
	}		
}