//////////////////////////////////////////////////////////////
// THESE ARE VALID AND USED

function GetXmlHttpObject(){
    var xmlHttp=null;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlHttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
    return xmlHttp;
}


function myAjaxCall(url,moodi){
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null){
		alert ("Browser does not support HTTP Request");
		return false;
	}

	xmlHttp.open("GET",url,moodi);

	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4 && xmlHttp.status==200){
			//alert (xmlHttp.responseText);
		}
	}

	xmlHttp.send(null);

}
//////////////////////////////////////////////////////////////

function hide(id,eltype){
	var base=document.getElementById(id).className;
	//var base=document.getElementById(id).getAttribute("class");

	if (eltype=="r")
		base = str_replace("visible","hidden",base);
	else
		base = str_replace("shown","hidden",base);

	document.getElementById(id).className=base;
	document.getElementById(id).className=base;
}

function unhide(id,eltype){
	var base=document.getElementById(id).className;

	if (eltype=="r"){
		base = str_replace("hidden","visible",base)	
	}
	else{
		base = str_replace("hidden","shown",base)	
	}

	document.getElementById(id).className=base;
}

function toggleVis(id,eltype){
	var x=document.getElementById(id).className;

	if ((x.indexOf("visible")>-1) || (x.indexOf("shown")>-1)){
		hide (id,eltype);
	}else{
		unhide(id,eltype);
	}
}

function logout(){
   myAjaxCall('logout.php',false);
}

function hideListItem(list_id,item_id){
	var url = "includes/ajax/update_list.php?lid="+list_id+"&pid="+item_id+"&act=h";
	var x=myAjaxCall(url,true);
	
	//str_replace (search, replace, subject);
	var visItems=document.getElementById("visibleItems").value;
	var alt1=","+item_id+",";
	var alt2=","+item_id;
	var alt3=item_id+",";
	var alt4=item_id;

	if (visItems.indexOf(alt1)>-1)
		document.getElementById("visibleItems").value = str_replace(alt1,",",visItems);
	else 
	if (visItems.indexOf(alt2)>-1)
		document.getElementById("visibleItems").value = str_replace(alt2,"",visItems);
	else
	if (visItems.indexOf(alt3)>-1)
		document.getElementById("visibleItems").value= str_replace(alt3,"",visItems);
	else
	if (visItems.indexOf(alt4)>-1)
		document.getElementById("visibleItems").value="";

	var hidItems=document.getElementById("hiddenItems").value;

	if (hidItems.length > 0)
		document.getElementById("hiddenItems").value=document.getElementById("hiddenItems").value+","+item_id;
	else
		document.getElementById("hiddenItems").value=item_id;

	document.getElementById('li_'+item_id).src='themes/default/btnAdd.png';
	
	toggleVis('pr_'+item_id,'b');
}

function unhideListItem(list_id,item_id){
	var url = "includes/ajax/update_list.php?lid="+list_id+"&pid="+item_id+"&act=u";
	var x=myAjaxCall(url,true);
	//str_replace (search, replace, subject);
	var hidItems=document.getElementById("hiddenItems").value;
	var alt1=","+item_id+",";
	var alt2=","+item_id;
	var alt3=item_id+",";
	var alt4=item_id;

	if (hidItems.indexOf(alt1)>-1)
		document.getElementById("hiddenItems").value = str_replace(alt1,",",hidItems);
	else 
	if (hidItems.indexOf(alt2)>-1)
		document.getElementById("hiddenItems").value = str_replace(alt2,"",hidItems);
	else
	if (hidItems.indexOf(alt3)>-1)
		document.getElementById("hiddenItems").value= str_replace(alt3,"",hidItems);
	else
	if (hidItems.indexOf(alt4)>-1)
		document.getElementById("hiddenItems").value="";



	var visItems=document.getElementById("visibleItems").value;

	if (visItems.length > 0)
		document.getElementById("visibleItems").value=document.getElementById("visibleItems").value+","+item_id;
	else
		document.getElementById("visibleItems").value=item_id;

	document.getElementById('li_'+item_id).src='themes/default/btnBasket.png';

	//toggleVis('pr_'+item_id,'b');


}

function delListItem(list_id,item_id){
	var url = "includes/ajax/update_list.php?lid="+list_id+"&pid="+item_id+"&act=d";
	var x=myAjaxCall(url,true);
	
	//str_replace (search, replace, subject);
	var visItems=document.getElementById("visibleItems").value;
	var alt1=","+item_id+",";
	var alt2=","+item_id;
	var alt3=item_id+",";
	var alt4=item_id;

	if (visItems.indexOf(alt1)>-1)
		document.getElementById("visibleItems").value = str_replace(alt1,",",visItems);
	else 
	if (visItems.indexOf(alt2)>-1)
		document.getElementById("visibleItems").value = str_replace(alt2,"",visItems);
	else
	if (visItems.indexOf(alt3)>-1)
		document.getElementById("visibleItems").value= str_replace(alt3,"",visItems);
	else
	if (visItems.indexOf(alt4)>-1)
		document.getElementById("visibleItems").value="";

	toggleVis('pr_'+item_id,'b');

}

function liToggler(list_id,item_id){
	var x=document.getElementById('li_'+item_id).src;
	if (x.indexOf("themes/default/btnAdd.png")>0){
		unhideListItem(list_id,item_id);
	}
	else if (x.indexOf("themes/default/btnBasket.png")>0){
		hideListItem(list_id,item_id);
	}
}


function toggleHiddenItemVisibility(list_id){
	var x=document.getElementById("hiddenItems").value;

	var table= x.split(",");
	for ( var i in table )
		toggleVis('pr_'+table[i],'b');
}

function listNameEdit(list_id,nval){
	//var ss = document.getElementById(element_name);
	var url = "includes/ajax/update_list_name.php?lid="+list_id+"&nval="+nval;
	var x=myAjaxCall(url,true);
}

function catRename(tid,new_name){
	var url = "includes/ajax/cat_rename.php?tid="+tid+"&name="+new_name;
	var x=myAjaxCall(url,true);
	
	return 0;
}


////////// LIST HANDLING
function listDel(list_id){
	var url = "includes/ajax/list_del.php?lid="+list_id;
	var x=myAjaxCall(url,true);
	hide('lst_'+list_id,'r');
}

function listHide(list_id){
	var url = "includes/ajax/list_hide.php?lid="+list_id;
	var x=myAjaxCall(url,true);
	hide('lst_'+list_id,'r');
}

function listUnhide(list_id){
	var url = "includes/ajax/list_unhide.php?lid="+list_id;
	var x=myAjaxCall(url,true);
	document['uh_'+list_id].src='themes/default/btnOrg.png';
}


function toggleVisibility(element){
	if (element!=null){
/*
		if (element.className=="hidden")
			element.setAttribute("class","shown");
		else if (element.className=="shown")
			element.setAttribute("class","hidden");
*/

		if (element.style.display != 'none')
			element.style.display= 'none';
		else if (element.style.display =='none')
			element.style.display ='block';
	}
	return 0;
}

function toggleHidden(listing,mode){
	if (listing!=""){
		var larray = listing.split(",");
		for ( var i in larray ){
			var el='pr_'+larray[i];
			element=document.getElementById(el);
			if (mode=="hide"){
				if (element.style.display !='none')
					x=toggleVisibility(element);
			}
			else if (mode=="show"){
				if (element.style.display =='none')
					x=toggleVisibility(element);
	
			}
		}
	}
	return 0;
}


function toggleGroupStyle(groupid,nstyle){
	var classid='grpttl_'+groupid;
	var ss = document.getElementById(classid).className;

	if (!!nstyle!=""){
		fullstyle=nstyle;
		//document.getElementById(classid).setAttribute("class",fullstyle);
		document.getElementById(classid).className=fullstyle;
	}
	else {
		if (ss=="grpTitle")
			document.getElementById(classid).className="grpTitleCollapsed";
		else
			document.getElementById(classid).className="grpTitle";
	}
}


function groupsCollapse(parameter){

	var table=parameter.split(",");
	for ( var i in table )
	{
		var grpid="grp_"+table[i];
		document.getElementById(grpid).style.display="none";
		x=toggleGroupStyle(table[i],'grpTitleCollapsed');
	}
	return 0;
}
function groupsExpand(parameter){

	var table=parameter.split(",");
	for ( var i in table )
	{
		var grpid="grp_"+table[i];
		x=toggleGroupStyle(table[i]);
		document.getElementById(grpid).style.display="block";
		x=toggleGroupStyle(table[i],'grpTitle');
	} 

	return 0;
}

function groupCollapser(listofgroups,mode){
	if (mode=="collapse")
		x=groupsCollapse(listofgroups);
	else if (mode=="expand")
		x=groupsExpand(listofgroups);

	x=toggleVisibility(document.getElementById('grpCollapse'));
	x=toggleVisibility(document.getElementById('grpExpand'));

}

function singleGroupToggler(prid){
	var prgroup="grp_"+prid;
	x=toggleVisibility(document.getElementById(prgroup));

	x=toggleGroupStyle(prid);
}


/*
function addtoList(list,value){
	var nlist = list.split(",");
	if (!(in_array(value,nlist))){
		if (list=="")
			list = value;
		else
			list = list + "," + value;

	}
	return list;
}
function aMod(action,list_id,product_id){
	var url = "includes/ajax/update_list.php?lid="+list_id+"&pid="+product_id+"&act="+action;
	var x=myAjaxCall(url,true);
	if (!is_numeric(action) && action!="u")
		x=toggleVisibility(document.getElementById('pr_' + product_id));
}

*/

