window.onerror = function () { return true; }
var textBox = "";
var txtID="";
var divID="";
var hID;
var tID;
var isAdmin = false;
var isPro = false;
function loadxml(category_id,hiddenId,txtId,varName,admin,ispro)
{
	isAdmin = admin;
	isPro = ispro;
	hID = hiddenId;
	tID = txtId;
	if(!varName)
	{
		varName = "writeroot";
		
	}
	divID = varName;
	
	if (document.getElementById){
			document.getElementById(divID).style.backgroundColor='#FFFFFF';		
	}else{
		if (document.layers){ //NS4
			document.writeroot.backgroundColor='#FFFFFF';
		}
		else { //IE4
			document.all.writeroot.style.backgroundColor='#FFFFFF';
		}
	}
	textBox = txtId;
	txtID = hiddenId;
	var canItWork = importXML( root+'admin/modules/categories/xml_categories.php?category_id='+category_id, 'showCategories' );
}

//clear the category dic
function closeit()
{

	var toremove=document.getElementById('mytable');
	var ref=toremove.parentNode;
	try{
		ref.removeChild(toremove);
	}
	catch(e)
	{}
	track=0;
	
	//added by Fahad Pervaiz on 09 may,2006
	browswerNumber=getBrowserType();
	if(browserNumber==2)
	{
		ref.removeAttribute('style');
	} else
	{
		ref.removeAttribute('style');
	}
	document.getElementById(divID).style.display = "none";
}

var track=0;//track if the table is already appended
var parentcat=0;

function showCategories(xmlDoc)
{ 
	//remove element if already there
	if(track>=1)
	{
		var toremove=document.getElementById('mytable'); 
	//	var ref=document.getElementById(divID); 
	//	ref.removeChild(toremove);
//		#modified by Tofeeq
		var prnt = toremove.parentNode;
		prnt.removeChild(toremove);
	}
	
	track++;

	var x = xmlDoc.getElementsByTagName('tablerow');
	
	
	//browser detection
	browserNumber=getBrowserType();
	
	if(browserNumber==2)
	{
		var divnum= document.createElement('<DIV id="mytable" >');
	} else 
	{ 
		var divnum= document.createElement('DIV');
		divnum.setAttribute('id','mytable');
		
	}
		
	
	if(browserNumber==2)
	{
		//for I/E
		var newEl = document.createElement('<TABLE class="xmlTbl" align="center" width="100%" cellpadding="3" cellspacing="1" border="0">');
	} else 
	{ 
	//For mozilla
		var newEl = document.createElement('TABLE');		
		newEl.setAttribute('align','center');
		newEl.setAttribute('width','100%');
		newEl.setAttribute('cellpadding','3');
		newEl.setAttribute('cellspacing','1');
		newEl.setAttribute('class','xmlTbl');
//		newEl.setAttribute('border','1');
	}	
	
	//newEl.setAttribute('cellPadding',2);
	var tmp = document.createElement('TBODY');
	newEl.appendChild(tmp);	
	divnum.appendChild(newEl);
	
	var row = document.createElement('TR');
	//create element for parent
	var prow = document.createElement('TR');
	var pcontainer = document.createElement('th');
	var pContDiv = document.createElement('div');
	pContDiv.className = "mainHeadDiv";
	//Home
	var paDiv = document.createElement('div');
	paDiv.className = "leftHeadDiv";
	var pahref = document.createElement('A');
	pahref.setAttribute("href",'javascript: loadxml('+parentcat+',"'+hID+'","'+tID+'","'+divID+'",'+isAdmin+');');
	var ptheData =document.createTextNode('<< HOME >> ');
	pahref.appendChild(ptheData);
	paDiv.appendChild(pahref);
	//Close
	var p2Div = document.createElement('div');
	p2Div.className = "rightHeadDiv";
	var p2ahref = document.createElement('A');
	p2ahref.setAttribute('class','white');
	p2ahref.setAttribute('href','javascript: closeit();');
	var p2theData =document.createTextNode('<< CLOSE >>');
	p2ahref.appendChild(p2theData);
	p2Div.appendChild(p2ahref);
	
	
	pContDiv.appendChild(paDiv);	
	pContDiv.appendChild(p2Div);
	//pContDiv.setAttribute("color","#ffffff");
	pcontainer.appendChild(pContDiv);
	prow.appendChild(pcontainer);
	tmp.appendChild(prow);
	
	/*for (j=0;j<x[0].childNodes.length;j++)
	{
		if (x[0].childNodes[j].nodeType != 1) continue;
		var container = document.createElement('TH');
		var theData = document.createTextNode(x[0].childNodes[j].nodeName+"-");
		container.appendChild(theData);
		row.appendChild(container);
	}
	tmp.appendChild(row);*/
	var haschild = 0;
	var secondFunction = "";
	for (i=0;i<x.length;i++)
	{
		var row = document.createElement('TR');
			row.setAttribute("bgcolor","#ffffff");		
		//alert(x[i].childNodes.length);
		haschild = x[i].getElementsByTagName("has_child")[0].firstChild.data;
		for (j=0;j<x[i].childNodes.length;j++)
		{ 
			
			//avoid extra nodes
			if(j%3==0)
			{
				var id=x[i].childNodes[j].firstChild.nodeValue;
				continue;//avoid ids
			} else if(j%3==1)
			{
				//var parent_id=x[i].childNodes[j].firstChild.nodeValue;
				continue;//avoid printing parent id
			}
			
			if (x[i].childNodes[j].nodeType != 1) continue;
			var container = document.createElement('TD');
			
			if((haschild == 0)||isAdmin) secondFunction = 'updatevals(\''+id+'\',\''+x[i].childNodes[j].firstChild.nodeValue+'\','+'\''+hID+'\','+'\''+tID+'\',\''+isPro+'\'); ';
			else
					secondFunction = "";
			//create an A tag element
			if(browserNumber==2)
			{
				var ahref = document.createElement('<A class="blacktextbold" href="javascript: loadxml('+id+',\''+hID+'\',\''+tID+'\',\'\','+isAdmin+',\''+isPro+'\''+');'+secondFunction+'">');
			} else
			{
				var ahref = document.createElement('A');
				ahref.setAttribute('class','blacktextbold');
				ahref.setAttribute('href','javascript: loadxml('+id+',"'+hID+'","'+tID+'",\'\','+isAdmin+');'+secondFunction);
			}
				
			//get category name
			var theData = document.createTextNode(unescape(x[i].childNodes[j].firstChild.nodeValue));
			
			//creating select link
//			updatevals(\''+id+'\',\''+x[i].childNodes[j].firstChild.nodeValue+'\','+'\''+hID+'\','+'\''+tID+'\');
			/*if(browserNumber==2)
			{
				//alert(textBox);
				var selecthref= document.createElement('<A class="a1" href="javascript: updatevals(\''+id+'\',\''+x[i].childNodes[j].firstChild.nodeValue+'\','+'\''+hID+'\','+'\''+tID+'\');">');
			} else 
			{
				var selecthref= document.createElement('A');
				selecthref.setAttribute('class','a1');
				selecthref.setAttribute('href','javascript: updatevals(\''+id+'\',\''+x[i].childNodes[j].firstChild.nodeValue+'\','+'\''+hID+'\','+'\''+tID+'\');');
			}
			var selectdata = document.createTextNode(' select');*/
			//append 
			ahref.appendChild(theData);
			//selecthref.appendChild(selectdata);
			container.appendChild(ahref);
			//container.appendChild(selecthref);	
			row.appendChild(container);
		}
		tmp.appendChild(row);
	}

	var dv = document.getElementById(divID);
	dv.appendChild(divnum);
	dv.style.display = "block";
	//alert(window.screen.height);
	//alert(dv.offsetTop+" >> "+divnum.offsetHeight+" = "+(dv.offsetTop+divnum.offsetHeight));
	var winHeight = (window.innerHeight?window.innerHeight:(document.body.clientHeight?document.body.clientHeight:document.documentElement.clientHeight));
	var offset = ((dv.offsetTop+divnum.offsetHeight)-winHeight);
	if( offset > 0){
		//dv.style.top = (dv.style.top-offset)+"px";
	}
}

//function to show loading
function showLoading(elem)
{
	var theLoad=document.getElementById(elem);
	
	//get browser type	
	browserNumber=getBrowserType();
	
	//loading text
	//select a type of browser 
	if(browserNumber==2)
	{
		var option= document.createElement('<OPTION>');
	} else
	{
		var option= document.createElement('OPTION');
	}
	
	var loadText = document.createTextNode("Loading...");
	option.appendChild(loadText);
	theLoad.appendChild(option);
	
	theLoad.setAttribute('disabled',true);
	
}

function removeLoading(elem)
{
	var theLoad=document.getElementById(elem);
	
	//get browser type	
	browserNumber=getBrowserType();
	
	var kids = theLoad.childNodes;
	var numkids = kids.length;		
	theLoad.removeChild(kids[0]);//remove loading here
	
	//enable the select field
	if(browserNumber==2)
	{
		theLoad.removeAttribute('disabled');
		//theLoad.setAttribute('disabled',false);
	} else
	{
		theLoad.removeAttribute('disabled');
	}
	
}

//get browser type
function getBrowserType()
{
	var browserName=navigator.appName;
	browserNumber=0;
	if (browserName=="Netscape")
	{
		browserNumber=1;
	}
	else if (browserName=="Microsoft Internet Explorer")
	{
		//IE
		browserNumber=2;
	}
	else if (browserName=="Opera")
	{
		//opera
		browserNumber=3;
	}
	else 
	{
		//someother browser
		browserNumber=4;
	}
	
	return browserNumber;
}

//clear parts dropdown
function clearProducts()
{
	var toremove=document.getElementById('product_id');
	var tor=toremove.length;

	var kids = toremove.childNodes;
	var numkids = kids.length;
	for(var r = numkids-1; r >= 0; r--) {
		
		toremove.removeChild(kids[r]);
	}
}

//for sub part#s
function loadxml2(category_id)
{ 
	//show loading 		
	showLoading('product_id');	
	var canItWork = importXML( root+'admin/modules/products/xml_products.php?category_id='+category_id, 'showCategories2' );
}

var track2=0;//track if the table is already appended

function showCategories2(xmlDoc)
{ 
	//remove element if already there
	if(track2>=1)
	{
		var toremove=document.getElementById('product_id');
		var tor=toremove.length;

		var kids = toremove.childNodes;
        var numkids = kids.length;
        for(var r = numkids-1; r >= 0; r--) {
			
			toremove.removeChild(kids[r]);
		}
		
		//show loading again as every thing is removed
		//show loading 
		showLoading('product_id');
	}
	
	track2++;

	var x = xmlDoc.getElementsByTagName('tablerow');
	
	var selectobj=document.getElementById('product_id');
	//get browser type	
	browserNumber=getBrowserType();
		
	//select a product 
	if(browserNumber==2)
	{
		var objsel= document.createElement('<OPTION value="xxx">');
	} else
	{
		var objsel= document.createElement('OPTION');
		objsel.setAttribute('value',"xxx");
	}
	
	var objdatasel = document.createTextNode("Select a Product");
	objsel.appendChild(objdatasel);
	selectobj.appendChild(objsel);	
	
	for (i=0;i<x.length;i++)
	{
		for (j=0;j<x[i].childNodes.length;j++)
		{ 			
			//avoid extra nodes
			if(j%2==0)
			{
				var id=x[i].childNodes[j].firstChild.nodeValue;				
				continue;//avoid ids
			}
			
			if (x[i].childNodes[j].nodeType != 1) continue;
				
			//get category name
			var theData = document.createTextNode(unescape(x[i].childNodes[j].firstChild.nodeValue));
			if(browserNumber==2)
			{
				var optionobj= document.createElement('<OPTION value="'+id+'">');
			} else
			{
				var optionobj= document.createElement('OPTION');
				optionobj.setAttribute('value',id);
			}			
			optionobj.appendChild(theData);
			selectobj.appendChild(optionobj);		
		}
	}	
	
	//remove loading here
	removeLoading('product_id');
}

///////////////////////////////////////////////////////////////////////////////
//****************for listing products in form with checkbox*****************//
//function to show loading
function showListLoading(elem)
{
	var theLoad=document.getElementById(elem);
	
	//get browser type	
	browserNumber=getBrowserType();
	
	//define table body
	var tmp = document.createElement('TBODY');
	theLoad.appendChild(tmp);	
	
	//loading text
	//select a type of browser 
	if(browserNumber==2)
	{
		var option= document.createElement('TR');
		var suboption= document.createElement('<TD class="blacktextnormal">');
	} else
	{
		var option= document.createElement('TR');
		var suboption= document.createElement('TD');
		suboption.setAttribute('class','blacktextnormal');
	}
	
	var loadText = document.createTextNode("Loading...");
	suboption.appendChild(loadText);
	option.appendChild(suboption);
	tmp.appendChild(option);
	
	//theLoad.setAttribute('disabled',true);
	
}

function removeListLoading(elem)
{
	var theLoad=document.getElementById(elem);
	
	//get browser type	
	browserNumber=getBrowserType();
	
	var kids = theLoad.childNodes;
	var numkids = kids.length;		
	theLoad.removeChild(kids[0]);//remove loading here
	
	//enable the select field
	/*if(browserNumber==2)
	{
		theLoad.setAttribute('disabled',false);
	} else
	{
		theLoad.removeAttribute('disabled');
	}*/
	
}

//clear parts dropdown
function clearProductList()
{
	var toremove=document.getElementById('listproducts');
	var tor=toremove.length;

	var kids = toremove.childNodes;
	var numkids = kids.length;
	for(var r = numkids-1; r >= 0; r--) {
		
		toremove.removeChild(kids[r]);
	}
}

//for importing product list
function loadProductList(category_id)
{ 
	//show loading 
	showListLoading('listproducts');
	var canItWork = importXML( 'xml_products.php?category_id='+category_id, 'showProductList' );
}

var track3=0;//track if the table is already appended

function showProductList(xmlDoc)
{
	//remove element if already there
	if(track3>=1)
	{
		var toremove=document.getElementById('listproducts');
		var tor=toremove.length;

		var kids = toremove.childNodes;
        var numkids = kids.length;
        for(var r = numkids-1; r >= 0; r--) {
			
			toremove.removeChild(kids[r]);
		}
		
		//show loading again as every thing is removed
		//show loading 
		showListLoading('listproducts');
	}
	
	track3++;

	var x = xmlDoc.getElementsByTagName('tablerow');
	
	var selectobj=document.getElementById('listproducts');
	
	//define table body
	var tmp = document.createElement('TBODY');
	selectobj.appendChild(tmp);	
	
	//get browser type	
	browserNumber=getBrowserType();
	
	for (i=0;i<x.length;i++)
	{
		var optionm= document.createElement('TR');
		for (j=0;j<x[i].childNodes.length;j++)
		{ 			
			//avoid extra nodes
			if(j%2==0)
			{
				var id=x[i].childNodes[j].firstChild.nodeValue;				
				continue;//avoid ids
			}
			
			if(browserNumber==2)
			{				
				var suboptionm= document.createElement('<TD class="blacktextbold">');				
			} else
			{
				var suboptionm= document.createElement('TD');
				suboptionm.setAttribute('class','blacktextnormal');
			}
			
			if (x[i].childNodes[j].nodeType != 1) continue;
				
			//get category name
			var theData = document.createTextNode(unescape(x[i].childNodes[j].firstChild.nodeValue));
			
			if(browserNumber==2)
			{
				var optionobj= document.createElement('<input id="related_product_id" name="related_product_id" type="checkbox" value="'+id+'">');
			} else
			{
				var optionobj= document.createElement('INPUT');
				optionobj.setAttribute('type','checkbox');
				optionobj.setAttribute('value',id);
				optionobj.setAttribute('name','related_product_id');
			}
			suboptionm.appendChild(optionobj);
			suboptionm.appendChild(theData);
			optionm.appendChild(suboptionm);
					
		}
		
		tmp.appendChild(optionm);
	}	
	
	//remove loading here
	removeListLoading('listproducts');
}