// stores the reference to the XMLHttpRequest object
var xmlHttp = createXmlHttpRequestObject(); 
// retrieves the XMLHttpRequest object
function createXmlHttpRequestObject() 
{	
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // if running Internet Explorer
  if(window.ActiveXObject)
  {
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // if running Mozilla or other browsers
  else
  {
    try 
    {
      xmlHttp = new XMLHttpRequest();
    }
    catch (e) 
    {
      xmlHttp = false;
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
 
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// make asynchronous HTTP request using the XMLHttpRequest object 
function process(URI)
{
  // proceed only if the xmlHttp object isn't busy
  if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
	// execute the respective page from the server with all the relevant getParams
    xmlHttp.open("GET", URI, true);  
    // define the method to handle server responses
    xmlHttp.onreadystatechange = handleServerResponse;
    // make the server request
    xmlHttp.send(null);
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('process()', 1000);
}

function updateTBLDate(caller,tbl,ID,OID)
{
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
	var retVal=getDateArray(caller)
	var fld = retVal.shift();
	var val=retVal.join("_");
	var URI = "simpleUpdate.php?caller="+fld+"&value="+val+"&ID="+ID+"&tbl="+tbl+"&OID="+OID+"&date=1";
	//alert (URI);
	xmlHttp.open("GET", URI , true); 
	xmlHttp.send(null);
	}
	else
	 setTimeout('updateTBLDate('+caller+','+tbl+','+ID+','+OID+')', 1000);
}
function markPics(caller,SID,BID)
{
	var action=0;
	var op=0;
	if(markPics.arguments.length==4) op = markPics.arguments[3];
	var vis="hidden";
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
	if (caller.checked) {action=1; vis="visible";}
	var URI = "markpics.php?action="+action+"&sid="+SID+"&bid="+BID;
	xmlHttp.open("GET", URI , true); 
	xmlHttp.onreadystatechange = refreshSortCriteria;
	xmlHttp.send(null);
	if(markPics.arguments.length==4)
	{
		//call from gallery window
		
		if (!op.closed && op.document.getElementById("CB_"+BID))
		{
			op.document.getElementById("CB_"+BID).checked=caller.checked;
			if (!action) 
			{
				for (var i=0;i<6;i++)
				{
					document.getElementById("RB_"+i).checked=false;
				}
				op.document.getElementById("BW_"+BID).style.visibility=vis;
			}
		}
		document.getElementById("bwbtns").style.visibility=vis;		
	}
	else //call from main window
	{
		if (NI && !NI.closed && NI.document.getElementById("CB_"+BID))
		{
			NI.document.getElementById("CB_"+BID).checked=caller.checked;
			NI.document.getElementById("bwbtns").style.visibility=vis;
			if (!action)
			{
				for (var i=0;i<6;i++)
				{
					NI.document.getElementById("RB_"+i).checked=false;
				}
			}
			}
			if (!action)
			{
				document.getElementById("BW_"+BID).style.visibility=vis;
			}
		}
	}
	else
	 setTimeout('markPics('+caller+','+SID+','+BID+')', 1000);
}

function notePics(SID,BID,val,op)
{
	var action=2;
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
	var URI = "markpics.php?action="+action+"&sid="+SID+"&bid="+BID+"&val="+val;
	if (!op.closed && op.document.getElementById("NOTE_"+BID))
	{
	op.document.getElementById("BW_"+BID).style.visibility="visible";
	op.document.getElementById("NOTE_"+BID).innerHTML=20*val+" %";
	}
	xmlHttp.open("GET", URI , true); 
	xmlHttp.onreadystatechange = refreshSortCriteria;
	xmlHttp.send(null);
	}
	else
	 setTimeout('notePics('+SID+','+BID+','+val+','+op+')', 1000);
}



function refreshSortCriteria()
{
if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
	
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
       xmlResponse = xmlHttp.responseXML;
      // obtain the document element (the root element) of the XML structure
      xmlDocumentElement = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      //  the document element
     res = xmlDocumentElement.firstChild.data;
	 updateSortPulldowns(Number(res));
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("refreshSortCriteria(): There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

function setSelects(res)
{
if (document.getElementById("showSelect") && document.getElementById("showSelect").options.length != res+1)
{
	var showSelect = document.getElementById("showSelect");
	var showOrder = document.getElementById("showOrder");
	var selIndex = showSelect.options.selectedIndex;
	if (selIndex > res) selIndex=0;
	var selIndex2 = showOrder.options.selectedIndex;
	showSelect.options.length=null;
	showSelect.options.length=res+1;
	showOrder.options.length=null;
	var Opt = new Option("Alle","0",false,false);
	var Opt2 = new Option("Bild","0",false,false);
	showSelect.options[0]=Opt;
}
else return;
if (!res)
{showOrder.options.length=1;showOrder.options[0]=Opt2;showOrder.options.selectedIndex=0;location.reload(); return;}
else if (res==1) //gemerkte Bilder, keine bewerteten
{
showOrder.options.length=2;
showOrder.options[0]=Opt2;
Opt = new Option("Gemerkte","1",false,false);
Opt2 = new Option("Markierung","1",false,false);
showSelect.options[1]=Opt;
showOrder.options[1]=Opt2;
showOrder.options.selectedIndex = selIndex2;
}
else if (res==2) //bewertete Bilder
{
showOrder.options.length=2;
showOrder.options[0]=Opt2;
Opt2 = new Option("Bewertung","1",false,false);
Opt = new Option("Gemerkte","2",false,false);
showSelect.options[1]=Opt;
Opt = new Option("Bewertete","1",false,false);
showSelect.options[2]=Opt;
showOrder.options[1]=Opt2;
showOrder.options.selectedIndex = selIndex2;
}
showSelect.options.selectedIndex = selIndex;
showSelect.disabled=false;
showOrder.disabled=false;
}

function updateSortPulldowns(res)
{
var target;
if (window.opener) {
target = window.opener;
if (target.closed) return;
target.setSelects(res);
}
else setSelects(res);
}

function updateTBL(caller,tbl,ID,OID)
{
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
  {
	var theVal = trim(caller.value);
	//alert(theVal + caller.type);
	if (caller.type=="checkbox" || caller.type=="radio")
		theVal=Number(caller.checked);
	else if (caller.type=="textarea") theVal=escape(theVal);
	if (caller.getAttribute('lp_id')) var callerID = caller.getAttribute('lp_id');
	else var callerID = caller.getAttribute('Id');
	var URI = "simpleUpdate.php?caller="+callerID+"&value="+theVal+"&ID="+ID+"&tbl="+tbl+"&OID="+OID;
	//var URI = "simpleUpdate.php";
	//alert(URI);
	// execute the respective page from the server with all the relevant getParams
    //xmlHttp.open("POST", URI , true); 
	xmlHttp.open("GET", URI , true); 
	xmlHttp.send("caller="+callerID+"&value="+theVal+"&ID="+ID+"&tbl="+tbl+"&OID="+OID);
	var pseudoDate = new Date();
	if (document.getElementById("changed_"+ID))
	{
	var Jahr = pseudoDate.getFullYear();
	var Monat = pseudoDate.getMonth();
	if (Monat <10) Monat="0"+Monat;
	var Tag = pseudoDate.getDate();
	if (Tag <10) Tag="0"+Tag;
	var Stunde = pseudoDate.getHours();
	if (Stunde <10) Stunde="0"+Stunde;
	var Minute = pseudoDate.getMinutes();
	if (Minute <10) Minute="0"+Minute;
	var Sec = pseudoDate.getSeconds();
	if (Sec <10) Sec="0"+Sec;
	document.getElementById("changed_"+ID).innerHTML = Jahr +"-"+Monat+"-"+Tag+" "+Stunde+":"+Minute+":"+Sec;
	} 
	
  }
  else
    // if the connection is busy, try again after one second  
    setTimeout('updateTBL('+caller+','+tbl+','+ID+','+OID+')', 1000);
}

function cascadingUpdate(callerval,tbl,uptbl)
{
	// callerval: the current id of the calling element's ENTRY
	// tbl: the table opened when the call was issued i.e. the DB context in which the calling element is embedded in
	// uptbl: the table where the the calling element takes its values from
	// *** IMPORTANT: there are (at least one) more arguments passed to this function = dependent elements to be updated by the call
	var URI = "cascadingUpdate.php?lng="+lng+"&tbl="+tbl+"&uptbl="+uptbl+"&id="+callerval+"&up=";
	var upEls = new Array();
	for(i=3;i<cascadingUpdate.arguments.length;i++)
	{
		upEls.push(cascadingUpdate.arguments[i]);
	}
	URI = URI + upEls.join(",");
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
    	//alert (URI);
		//document.write (URI);
		xmlHttp.open("GET", URI , true);
		xmlHttp.onreadystatechange = doCascadingUpdate;
		xmlHttp.send(null);
	}
    else 
    {
       setTimeout('cascadingUpdate('+cascadingUpdate.arguments+')', 1000);
    }
  
}

function updateFieldSelect(tbl,lng)
{
var URI = "updatefieldselect.php?tbl="+tbl+"&lng="+lng;
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
    	//alert (URI);
		xmlHttp.open("GET", URI , true);
		xmlHttp.onreadystatechange = doUpdateFieldSelect;
		xmlHttp.send(null);
	}
    else 
    {
       setTimeout('updateFieldSelect('+tbl+','+lng+')', 1000);
    }
}


function doCascadingUpdate()
{
 if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp.responseXML;
      // obtain the document element (the root element) of the XML structure
      xmlRoot = xmlResponse.documentElement;
      elementsArray = xmlRoot.getElementsByTagName("idname");
	  valueArray = xmlRoot.getElementsByTagName("value");
	  for (var i=0;i<elementsArray.length;i++)
	  {
	  	objID = elementsArray.item(i).firstChild.data;
		
		if (document.getElementById(objID))
		{
			optionsArray = valueArray.item(i).getElementsByTagName("option")
			document.getElementById(objID).options.length=null;
			document.getElementById(objID).options.length=optionsArray.length;
			for (var j=0;j<optionsArray.length;j++)
			{
			var Opt = new Option(unescape(optionsArray.item(j).firstChild.data),optionsArray.item(j).attributes.getNamedItem("value").value,false,false);
			document.getElementById(objID).options[j]=Opt;
			}
		}
	  }
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("doCascadingUpdate(): There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

function doUpdateFieldSelect()
{
 
 if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status==200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp.responseXML;
      // obtain the document element (the root element) of the XML structure
      xmlRoot = xmlResponse.documentElement;
	  optionsArray = xmlRoot.firstChild.data.split(";");
	 
	  if (document.getElementById('REF_FLD'))
	  {
	  document.getElementById('REF_FLD').options.length=null;
		document.getElementById('REF_FLD').options.length=optionsArray.length;

			for (var j=0;j<optionsArray.length;j++)
			{
			var theOpts = optionsArray[j].split(",");
			var Opt = new Option(unescape(theOpts[1]),theOpts[0],false,false);
			document.getElementById('REF_FLD').options[j]=Opt;
			}
			}
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("doUpdateFieldSelect(): There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }

}

// executed automatically when a message is received from the server
function handleServerResponse() 
{
  // move forward only if the transaction has completed
  if (xmlHttp.readyState == 4) 
  {
    // status of 200 indicates the transaction completed successfully
    if (xmlHttp.status == 200) 
    {
      // extract the XML retrieved from the server
      xmlResponse = xmlHttp.responseXML;
      // obtain the document element (the root element) of the XML structure
      xmlDocumentElement = xmlResponse.documentElement;
      // get the text message, which is in the first child of
      // the the document element
      helloMessage = xmlDocumentElement.firstChild.data;
      // update the client display using the data received from the server
      document.getElementById("divMessage").innerHTML = 
                                            '<i>' + helloMessage + '</i>';
      // restart sequence
      setTimeout('process()', 1000);
    } 
    // a HTTP status different than 200 signals an error
    else 
    {
      alert("handleServerResponse(): There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

function decode_utf8(utftext) {
    var plaintext = ""; var i=0; var c=c1=c2=0;
    // while-Schleife, weil einige Zeichen uebersprungen werden
    while(i<utftext.length)
        {
      c = utftext.charCodeAt(i);
        if (c<128) {
             plaintext += String.fromCharCode(c);
            i++;}
         else if((c>191) && (c<224)) {
            c2 = utftext.charCodeAt(i+1);
             plaintext += String.fromCharCode(((c&31)<<6) | (c2&63));
             i+=2;}
         else {
            c2 = utftext.charCodeAt(i+1); c3 = utftext.charCodeAt(i+2);
             plaintext += String.fromCharCode(((c&15)<<12) | ((c2&63)<<6) | (c3&63));
             i+=3;}
         }
     return plaintext;
 }
 
 function makeExclusive(el,listid,eid,oid,lng)
 {
 if (el.checked==0) return;
 var theFLD = el.name.replace(/LP_CB_/,"");
 //alert(theFLD);
 var URI = "makeexclusive.php?tblid="+listid+"&lng="+lng+"&ID="+eid+"&OID="+oid+"&caller="+theFLD;
 //alert (URI);
 if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
    	
		xmlHttp.open("GET", URI , true); 
		xmlHttp.send(null);
	}
    else 
    {
       setTimeout('makeExclusive('+el+','+listid+','+eid+','+oid+','+lng+')', 1000);
    }
 }
 
 function checkAllListentries(tbl,fld,oid,val)
 {
 //alert("XXXX");
 var URI="checkallentries.php?tbl="+tbl+"&FLD="+fld+"&OID="+oid+"&val="+val;
 //  alert (URI);
 if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
    	
		xmlHttp.open("GET", URI , true); 
		xmlHttp.send(null);
	}
    else 
    {
       setTimeout('checkAllListentries('+tbl+','+fld+','+oid+','+val+')', 1000);
    }
 }
 
function setFilterSession(elname,lid,lng,art,rel,hidden,shown,text)
{
//setFilterSession(el.name,LID,lng,'',suchrelation,Array(),Array(),'');
var thidden="";
var tshown="";
if (hidden!=0) thidden=hidden.join(",");
if (shown!=0) tshown=shown.join(",");

var URI = "setfilter.php?el="+elname+"&lid="+lid+"&lng="+lng+"&art="+art+"&rel="+rel+"&text="+text+"&hidden="+thidden+"&shown="+tshown;
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
    	//alert (URI);
		xmlHttp.open("GET", URI , true);
		xmlHttp.send(null);
	}
    else 
    {
       setTimeout('setFilterSession('+elname+','+lid+','+lng+','+art+','+rel+','+hidden+','+shown+','+text+')', 1000);
    }
}

function setFilterSess(lng,lid)
{
	var rel=document.getElementById('filterrelation').value;
	var elements=new Array();
	var thidden=new Array();
	var tshown=new Array();
	var ttext=new Array();
	var tart=new Array();
	for(var i=0;i<filter.length;i++)
	{
		el = document.getElementById(filter[i].field);
		if (el)
		{
			applyFilter(el,lid,lng,0);
			elements.push(filter[i].field);
			ttext.push(el.value);
			thidden.push(filter[i].hidden.join(","));
			tshown.push(filter[i].shown.join(","));
			artel=document.getElementById("sel_"+filter[i].field);
			if (artel) tart.push(artel.value);
			else tart.push("0");
		}
	}
	elements=elements.join(",");
	ttext=ttext.join(",");
	tart=tart.join(",");
	thidden=thidden.join(";");
	tshown=tshown.join(";");
	var URI = "setfiltersession.php?lid="+lid+"&lng="+lng+"&elements="+elements+"&art="+tart+"&rel="+rel+"&text="+ttext+"&hidden="+thidden+"&shown="+tshown;
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
	{
    	//alert (URI);
		xmlHttp.open("GET", URI , true);
		xmlHttp.send(null);
		activateFilterBTN(0);
	}
    else 
    {
       setTimeout('setFilterSess('+lng+','+lid+')', 1000);
    }
}