// 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("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}

function updateSortPulldowns(res)
{
var target;
if (window.opener) {target = window.opener; if (target.closed) return;}
else target = window;
//alert (res);
if (target.document.getElementById("showSelect") && target.document.getElementById("showSelect").options.length != res+1)
{
	var showSelect = target.document.getElementById("showSelect");
	var showOrder = target.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;target.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 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);
	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;
	//alert(URI);
	// execute the respective page from the server with all the relevant getParams
    xmlHttp.open("GET", URI , true); 
	xmlHttp.send(null);
	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?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);
		xmlHttp.open("GET", URI , true);
		xmlHttp.onreadystatechange = doCascadingUpdate;
		xmlHttp.send(null);
	}
    else 
    {
       setTimeout('cascadingUpdate('+cascadingUpdate.arguments+')', 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(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("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("There was a problem accessing the server: " + xmlHttp.statusText);
    }
  }
}
