﻿var xmlHttp;
 var origHTML="";
 var dvCont = null;
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}


function stateChanged() 
{ 
    if (xmlHttp.readyState==4)
    { 
        if (xmlHttp.status==200)
        {
          origHTML = xmlHttp.responseText;
		
		 if(dvCont != null)
		 {
			dvCont.innerHTML =origHTML;
		 }
			
        }
        else
        {
            alert("Problem retrieving XML data");
        }
     }
}

function testUsername(username)
{
   xmlHttp=GetXmlHttpObject(); 
  
  if (xmlHttp==null)
   {
     alert ("Your browser does not support AJAX!");
      return;
   } 
   xmlHttp.onreadystatechange=stateChanged;
   xmlHttp.open("GET","util_aj.php?ue="+username,true);
   xmlHttp.send(null);
 
}

function adminUtil(url,divContent)
{
   xmlHttp=GetXmlHttpObject(); 
  dvCont = document.getElementById(divContent);
  if (xmlHttp==null)
   {
     alert ("Your browser does not support AJAX!");
      return;
   } 
   xmlHttp.onreadystatechange=stateChanged;
   xmlHttp.open("GET",url,true);
   xmlHttp.send(null);
 
}
function changeQuestion(selectOption)
{
	
	adminUtil('util_aj.php?idQuestion='+selectOption.options[selectOption.selectedIndex].value,'divAttributes');
	document.getElementById("sAnswers").selectedIndex = 0;
	var dvParent = document.getElementById("div_parent");
	var dvChild = document.getElementById("div_child");	
	dvParent.innerHTML = "";
	dvChild.innerHTML = ""; 
}
function changeOptions(selectOption)
{
	
	adminUtil('util_aj.php?idOpt='+selectOption.options[selectOption.selectedIndex].value,'divAttributes');
	document.getElementById("optAtr").selectedIndex = 0;
	var dvParent = document.getElementById("div_parent");
	var dvChild = document.getElementById("div_child");	
	dvParent.innerHTML = "";
	dvChild.innerHTML = ""; 
}

function changeLevel(selectLevel,level)
{	
	for(i=level*1;;i++)
	{
		var dvChild = document.getElementById("dvLevel"+i);
		if(dvChild !=null)
			removeCategory(dvChild);
		else
			break;			
	}	
	
	if(selectLevel.options[selectLevel.selectedIndex].value != "-1")
	{
		addCategory(level);
		adminUtil('util_aj.php?level='+level+'&id='+selectLevel.options[selectLevel.selectedIndex].value,'dvLevel'+level);
	}
}

function editOption(typeOption)
{
	switch(typeOption)
	{
		case "1" :  var selVal = document.getElementById("productOpt");
					var val =  selVal.options[selVal.selectedIndex].value;
					document.getElementById("div_parent").innerHMTL = "";
					document.getElementById("div_child").innerHMTL = "";
					alert(document.getElementById("div_child").innerHMTL);
					document.getElementById("optAtr").selectedIndex = 0;
					if(val != "-1")
						adminUtil("util_aj.php?eOption="+val,"div_parent"); 
					else
						document.getElementById("div_parent").innerHTML = "select a valid option";
					break;
		case "2":	var selVal = document.getElementById("optAtr");
					var val =  selVal.options[selVal.selectedIndex].value;
				    var selParent = document.getElementById("productOpt");
					var valP =  selParent.options[selParent.selectedIndex].value;
					if(val != "-1")
						adminUtil("util_aj.php?eAttribute="+val+"&idParent="+valP,"div_child"); 
					else
						document.getElementById("div_child").innerHTML = "select a valid option";
					break;		
	}
}
function addOption(typeOption)
{
	switch(typeOption)
	{
		case "1" : 
					document.getElementById("productOpt").selectedIndex = 0;
					document.getElementById("optAtr").selectedIndex = 0;
					document.getElementById("div_parent").innerHMTL = "";
					document.getElementById("div_child").innerHMTL = "";
					adminUtil("util_aj.php?eOption=-1","div_parent"); 					
					break;
		case "2" : 	var selVal = document.getElementById("productOpt");
					var val =  selVal.options[selVal.selectedIndex].value;
					if(val != "-1")
					{
						document.getElementById("optAtr").selectedIndex = 0;
						document.getElementById("div_child").innerHTML = "";
						adminUtil("util_aj.php?eAttribute=-1&idParent="+val,"div_child"); 
					}
					else
					{
						document.getElementById("div_child").innerHTML = "*Select a valid option";
					}
						
					break;		
	}
	
}
function deleteOption(typeOption)
{
	switch(typeOption)
	{
		case "1" :  var selVal = document.getElementById("productOpt");
					var val =  selVal.options[selVal.selectedIndex].value;
					if(val != "-1")
					{
						adminUtil("util_aj.php?dOption="+val,"div_parent"); 							
						selVal.remove(selVal.selectedIndex);
						selVal.selectedIndex = 0;
						var selOpt = document.getElementById("optAtr");
						 removeOption(selOpt);
						selOpt.selectedIndex = 0;
					}
					else
						document.getElementById("div_parent").innerHTML = "*Select a valid option";									
					break;
		case "2":	var selVal = document.getElementById("optAtr");
					var val =  selVal.options[selVal.selectedIndex].value;				 
					if(val != "-1")
					{
						adminUtil("util_aj.php?dAtr="+val,"div_child"); 
						selVal.remove(selVal.selectedIndex);
						selVal.selectedIndex = 0;
					}
					else
						document.getElementById("div_child").innerHTML = "*Select a valid attribute";
					break;				
	}
}
function removeOption(selectObject)
{  
  var i;
  for (i = selectObject.length - 1; i>0; i--) {
   
     selectObject.remove(i);
   
  }
}

function resetBoxOption()
{
	document.getElementById("div_child").innerHMTL = "";
}
function editQuestion(typeOption)
{
	switch(typeOption)
	{
		case "1" :  var selVal = document.getElementById("sQuestions");
					var val =  selVal.options[selVal.selectedIndex].value;
					document.getElementById("sAnswers").selectedIndex = 0;
					document.getElementById("div_parent").innerHMTL = "";
					document.getElementById("div_child").innerHMTL = "";
					if(val != "-1")
						adminUtil("util_aj.php?eQuestion="+val,"div_parent"); 
					else
						document.getElementById("div_parent").innerHTML = "*Select a valid question";
					break;
		case "2":	var selVal = document.getElementById("sAnswers");
					var val =  selVal.options[selVal.selectedIndex].value;
				    var selParent = document.getElementById("sQuestions");
					var valP =  selParent.options[selParent.selectedIndex].value;
					if(val != "-1")
						adminUtil("util_aj.php?eAnswer="+val+"&idPQuestion="+valP,"div_child"); 
					else
						document.getElementById("div_child").innerHTML = "*Select a valid answer";
					break;		
	}
}
function addQuestion(typeOption)
{
	switch(typeOption)
	{
		case "1" : 
					document.getElementById("sQuestions").selectedIndex = 0;
					document.getElementById("sAnswers").selectedIndex = 0;
					document.getElementById("div_parent").innerHMTL = "";
					document.getElementById("div_child").innerHMTL = "";
					adminUtil("util_aj.php?eQuestion=-1","div_parent"); 					
					break;
		case "2" : 	var selVal = document.getElementById("sQuestions");
					var val =  selVal.options[selVal.selectedIndex].value;
					if(val != "-1")
					{
						document.getElementById("sAnswers").selectedIndex = 0;
						document.getElementById("div_child").innerHTML = "";
						adminUtil("util_aj.php?eAnswer=-1&idPQuestion="+val,"div_child"); 
					}
					else
					{
						document.getElementById("div_child").innerHTML = "*Select a valid question";
					}
						
					break;		
	}
	
}
function deleteQuestion(typeOption)
{
	switch(typeOption)
	{
		case "1" :  var selVal = document.getElementById("sQuestions");
					var val =  selVal.options[selVal.selectedIndex].value;
					if(val != "-1")
					{
						adminUtil("util_aj.php?dQuestion="+val,"div_parent"); 							
						selVal.remove(selVal.selectedIndex);
						selVal.selectedIndex = 0;
						var selOpt = document.getElementById("sAnswers");
						removeOption(selOpt);
						selOpt.selectedIndex = 0;
					}
					else
						document.getElementById("div_parent").innerHTML = "*Select a valid question";
									
					break;
		case "2":	var selVal = document.getElementById("sAnswers");
					var val =  selVal.options[selVal.selectedIndex].value;				 
					if(val != "-1")
					{
						adminUtil("util_aj.php?dAnswer="+val,"div_child"); 
						selVal.remove(selVal.selectedIndex);
						selVal.selectedIndex = 0;
					}
					else
						document.getElementById("div_child").innerHTML = "*Select a valid answer";
					break;				
	}
}
function uniqueUsername(username)
{
   origHTML ="";
   testUsername(username);

   if(origHTML != "0")
  { 	
  	return false;
  }
  return true;

   
}
function getCategoriesAdmin(idCategory,dvContent,level)
{
	 xmlHttp=GetXmlHttpObject(); 
   dvCont = document.getElementById(dvContent); 
  
   if(dvCont != null)
	{
		if(dvCont.style.display == "none")
		{
			dvCont.style.display="";
			dvCont.style.position="relative";			
		}
		else
		{
			dvCont.style.display="none";
			dvCont.style.position="absolute";
		}

	}
   if (xmlHttp==null)
   {
     alert ("Your browser does not support AJAX!");
      return;
   } 
   xmlHttp.onreadystatechange=stateChanged;
   xmlHttp.open("GET","util_aj.php?idCategory="+idCategory+"&level="+level,true);
   xmlHttp.send(null);
  
  
}
function getProducts(idCategory,dvContent)
{   
   xmlHttp=GetXmlHttpObject(); 
  dvCont = document.getElementById(dvContent);
   dvProd = document.getElementById("dvProduct");
   dvProd.innerHTML ="";
  if (xmlHttp==null)
   {
     alert ("Your browser does not support AJAX!");
      return;
   } 
   xmlHttp.onreadystatechange=stateChanged;
   xmlHttp.open("GET","getProd.php?id="+idCategory,true);
   xmlHttp.send(null);
 
}
function getProductsAdmin(dvProducts,start)
{
	var idCategory = -1;
	for(i=1; ;i++)
	{
		var sel = document.getElementById("sLevel"+i);
		if(sel== null || sel.selectedIndex == 0)
		{
			break;
		}
		else
			idCategory = sel.options[sel.selectedIndex].value;			
	}
	
	var product = document.getElementById("tName").value;
	var code = document.getElementById("tCode").value;
	adminUtil("util_aj.php?searchCategory="+idCategory+"&product="+product+"&code="+code+"&start="+start,dvProducts); 
	return false;
	
}
function updateStatusProduct(sel,idProduct)
{
	adminUtil("util_aj.php?updateStatus="+sel.options[sel.selectedIndex].value+"&productUpdate="+idProduct,""); 
	alert("Status has been changed");
}
function getProductProperties(idProduct,dvContent)
{
   
   xmlHttp=GetXmlHttpObject(); 
  dvCont = document.getElementById(dvContent);
  if (xmlHttp==null)
   {
     alert ("Your browser does not support AJAX!");
      return;
   } 
   xmlHttp.onreadystatechange=stateChanged;
   xmlHttp.open("GET","getProdPropertie.php?id="+idProduct,true);
   xmlHttp.send(null);
 
}
function addCategory(idLevel)
{
	var divParent =document.getElementById("dvCatParent");
	var newDivId="dvLevel"+idLevel;
	var newDiv=document.createElement(newDivId);
	newDiv.setAttribute("id",newDivId);
	newDiv.style.cssText = "float:left; width:150px;margin-top:5px";
	divParent.appendChild(newDiv);	
	
}
function removeCategory(subLevel)
{
	var divParent = document.getElementById("dvCatParent");	
	divParent.removeChild(subLevel);
}