var req="false";

function InitializeAjax()
{
	
	if(window.XMLHttpRequest) 
	{	
		// branch for native XMLHttpRequest object
		try 
		{
			req = new XMLHttpRequest();
		} 
		catch(e) 
		{
			req = false;
		}
	}
	else if(window.ActiveXObject) 
	{	
		// branch for IE/Windows ActiveX version
		try 
		{
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch(e) 
		{
			try 
			{
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(e) 
			{
				req = false;
			}
		}
	}
	
}

function getCatList(catId, divName, imgId)
{


// Initializing the Global Object	
//var req=false;
//var page=page

DivName=divName;

// Check for browser compatibility
InitializeAjax();

	// check the object for the rest of the operations
	if(req)
	{
	
		 // Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
		 var sURL = "inc/list_categories.php?catId="+catId+"&mess=";
	
		 req.open( "Get", sURL, false);
		 
		 req.send("");
			 
		 var result=req.responseText;
		 
		// alert (catId);
		// alert (divName);
		// alert (imgId);
		 if(document.getElementById(DivName).style.display=="block"){
		 	
		 	document.getElementById(DivName).style.display='none';
		 	document.getElementById(imgId).src="images/icon_folder.gif";
			
		 
		 }else{
		 	document.getElementById(DivName).style.display='block';
			document.getElementById(DivName).innerHTML=result;
			document.getElementById(imgId).src="images/icon_folder1.gif";
			
		 }


		 
	}else{
		document.getElementById(imgId).src="images/icon_folder1.gif";
				 	
	}

}


function getProdList(catId, divName, imgId)
{
		
	
DivName=divName;

// Check for browser compatibility
InitializeAjax();

	// check the object for the rest of the operations
	if(req)
	{
	
		 
		 
		 // Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
		 var sURL = "inc/list_product.php?catId="+catId+"&mess=";
	
		 req.open( "Get", sURL, false);
		 
		 req.send("");
			 
		 var result=req.responseText;
		 
		 if(document.getElementById(DivName).style.display=="block"){
		 	document.getElementById(DivName).style.display='none';
		 	document.getElementById(imgId).src="images/icon_folder.gif";
			
	
		 }else{
	
		 	document.getElementById(DivName).style.display='block';
			document.getElementById(DivName).innerHTML=result;
			document.getElementById(imgId).src="images/icon_folder1.gif";
	
		 }
	
	}else{
		document.getElementById(imgId).src="images/icon_folder1.gif";
		
	
	}

}







function showMenuLayer3(tdID,catID,catName) {
	
document.getElementById("onClickCatId").value=catID;
document.getElementById("onClickCatName").value=catName;
document.getElementById("MenuLayer3").style.height="95px";
document.getElementById("MenuLayer3").style.clip="rect(0,150,200,0)";

document.getElementById("MenuLayer3").style.left = findPosX(tdID);
document.getElementById("MenuLayer3").style.top = findPosY(tdID)+19;
document.getElementById('messege').value="";
}

function hideMenuLayer3() {
document.getElementById("MenuLayer3").style.height="0px";
document.getElementById("MenuLayer3").style.clip="rect(0,0,0,0)";
document.getElementById('messege').value="";
}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}


/*
	This function display the products  form
*/

function showProductForm(){
	var parentCatID = document.getElementById("onClickCatId").value;
	var parentCatName = document.getElementById("onClickCatName").value;
	
	// Check for browser compatibility
	InitializeAjax();
	if(req){
		 // Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
		 var sURL = "ADproductsForm.php?catName="+parentCatName;
	 
		 req.open( "Get", sURL, false);
		 
		 req.send("");
			 
		 var result=req.responseText;
		 
		 document.getElementById('formDisplay').style.display='block';
		 
		 document.getElementById('formDisplay').innerHTML=result;
		 
		 //set the hidden field value to parent category id
		 document.getElementById('parentCatIdHidden').value=parentCatID;
		 document.getElementById('Edithidden').value="Save";
		document.getElementById('submit').value="Save";
	}
	
	
}

/*
	This function display  the product List
*/

function ViewProduct(catid){
	var parentCatID = document.getElementById("onClickCatId").value;
	var parentCatName = document.getElementById("onClickCatName").value;
	// Check for browser compatibility

	InitializeAjax();
	if(req){
		 // Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
		 var sURL = "productslist.php?catName="+parentCatName+"&parentCatID="+catid;
	 
		 req.open( "Get", sURL, false);
		 
		 req.send("");
			 
		 var result=req.responseText;
	
		 document.getElementById('formDisplay').style.display='block';
		 
		 document.getElementById('formDisplay').innerHTML=result;
		 	
		 //set the hidden field value to parent category id
		// document.getElementById('parentCatIdHidden').value=parentCatID;
		 
		 //document.getElementById('Edithidden').value="Edit"; 
		//alert("neerraj");
		// document.getElementById('submit').value="Edit";
		 
	}
	
}

/*
	This function display the products  form
*/

function EditProductForm(fld_id){
	var prodID = document.getElementById("onClickCatId").value;
	var parentCatName = document.getElementById("onClickCatName").value;
	
	// Check for browser compatibility
	InitializeAjax();
	if(req){
		 // Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
		 var sURL = "ADproductsForm.php?prodid="+fld_id;
	 
		 req.open( "Get", sURL, false);
		 
		 req.send("");
			 
		 var result=req.responseText;
		 
		 document.getElementById('formDisplay').style.display='block';
		 
		 document.getElementById('formDisplay').innerHTML=result;
		 
		 //set the hidden field value to parent category id
		 //document.getElementById('parentCatIdHidden').value=parentCatID;
		 document.getElementById('Edithidden').value='Edit';
		document.getElementById('submit').value='Edit';
	}
	
	
}


/*
	This function delete  the products 
*/


function askforremoval(thetype1,str1,parentCatID,fld_id)
{
	msg="";
	
	
	if(confirm("Would You like to delete this "+thetype1+" ?\n"+msg)) 
	{
	 	if (str1 !="")
		   {
		   document.getElementById('Location_ID_DIV').innerHTML = "<p align='center'><strong><font color='#FF0000'>Loading...</font></strong></p>";
		  
		   var url="ADproductslistForm.php?page=" + str1 + "&parentCatID=" + parentCatID +"&delete=1&fld_id="+fld_id
			
		   xmlHttp=GetXmlHttpObject(stateChangedL)
		   xmlHttp.open("GET", url , true)
		   xmlHttp.send(null)

		   }
		   else
		   {
		   document.getElementById("Location_ID_DIV").innerHTML=""
		   }
			

		return true;
		
	}
	else 
	{
		return false;
	}
}


function stateChangedL()
{
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
   {
   document.getElementById("Location_ID_DIV").innerHTML=xmlHttp.responseText
   }
}

function GetXmlHttpObject(handler)
{
var objXmlHttp=null

if (navigator.userAgent.indexOf("Opera")>=0)
   {
    alert("This example doesn't work in Opera")
    return
   }
if (navigator.userAgent.indexOf("MSIE")>=0)
   {
   var strName="Msxml2.XMLHTTP"
   if (navigator.appVersion.indexOf("MSIE 5.5")>=0)
      {
      strName="Microsoft.XMLHTTP"
      }
   try
      {
      objXmlHttp=new ActiveXObject(strName)
      objXmlHttp.onreadystatechange=handler
      return objXmlHttp
      }
   catch(e)
      {
      alert("Error. Scripting for ActiveX might be disabled")
      return
      }
    }
if (navigator.userAgent.indexOf("Mozilla")>=0)
   {
   objXmlHttp=new XMLHttpRequest()
   objXmlHttp.onload=handler
   objXmlHttp.onerror=handler
   return objXmlHttp
   }
}





/*
	This function display the products category form
*/

function showProductCatForm(){
	var parentCatID = document.getElementById("onClickCatId").value;
	var parentCatName = document.getElementById("onClickCatName").value;
	
	// Check for browser compatibility
	InitializeAjax();
	if(req){
		 // Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
		 var sURL = "ADproductsCategoryForm.php?catName="+parentCatName;
	 
		 req.open( "Get", sURL, false);
		 
		 req.send("");
			 
		 var result=req.responseText;
		 
		 document.getElementById('formDisplay').style.display='block';
		 
		 document.getElementById('formDisplay').innerHTML=result;
		 
		 //set the hidden field value to parent category id
		 document.getElementById('parentCatIdHidden').value=parentCatID;
		 document.getElementById('Edithidden').value="Save";
		document.getElementById('submit').value="Save";
	}
	
	
}


/*
	Edit Category  
*/

function EditProductCatForm(){
	var parentCatID = document.getElementById("onClickCatId").value;
	var parentCatName = document.getElementById("onClickCatName").value;
	// Check for browser compatibility
	InitializeAjax();
	if(req){
		 // Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
		 var sURL = "ADproductsCategoryForm.php?catName="+parentCatName+"&parentCatID="+parentCatID;
	 
		 req.open( "Get", sURL, false);
		 
		 req.send("");
			 
		 var result=req.responseText;
		 
		 document.getElementById('formDisplay').style.display='block';
		 
		 document.getElementById('formDisplay').innerHTML=result;
		 
		 //set the hidden field value to parent category id
		 document.getElementById('parentCatIdHidden').value=parentCatID;
		 document.getElementById('Edithidden').value="Edit";
		 document.getElementById('submit').value="Edit";
		 
	}
	
}

/*
	Delete Category  
*/

	function DeleteCategory(){
	var parentCatID = document.getElementById("onClickCatId").value;
	var parentCatName = document.getElementById("onClickCatName").value;
	// Check for browser compatibility
	InitializeAjax();
	if(req){
		 // Prepare the XMLHTTP object for a HTTP POST to our validation ASP page
		 var sURL = "DelproductsCategoryForm.php?catName="+parentCatName+"&parentCatID="+parentCatID;
	 
		 req.open( "Get", sURL, false);
		 
		 req.send("");
			 
		 var result=req.responseText;
		 
		 document.getElementById('formDisplay').style.display='block';
		 
		 document.getElementById('formDisplay').innerHTML=result;
		 
		 //set the hidden field value to parent category id
		 document.getElementById('parentCatIdHidden').value=parentCatID;
		  document.getElementById('Edithidden').value="Delete";
		   document.getElementById('submit').value="Delete";
		 
	}
	
	}

/*
Save Category
*/

function saveCategoryAndValidate(){

//	Get posted Category name
	  var catName = document.getElementById("prodCat").value;
	  	  
	  if(trimString(catName)==""){
	  	alert("Please enter category name");
	  	return false;
	  }else{
  		
  		if ( ! (Res = catName.match(/^[0-9_a-zA-Z]*$/)) )
  		{ 
			alert("Please enter valid category.\n Special characters are not allowed..");
			return (false);
		}
	  }
//	  get hidden parentcat id
	  var parentCatId = document.getElementById("parentCatIdHidden").value;
	  
//	 Check for browser compatibility
	  InitializeAjax();
	if(req){
		 //Prepare the req object for a HTTP POST to our validation PHP page
		var sURL = "ADinsertProductsCategory.php";
		
		var Flds = 'categoryName=' + catName + '&parentCategoryId=' + parentCatId;
		
		req.onreadystatechange = updatePage;
		req.open("POST", sURL, true);
		req.setRequestHeader("Content-Type","application/x-www-form-urlencoded; charset=UTF-8");
		req.send(Flds);
	
	}
		 
}




