/*
 * listnav.js
 * interaktive Listenansicht
 *
 * (c) 2007 - 2009
 * All Rights reserved
 *
 * @package Web-Logbuch V4.00 SU
 *
 * @author Tobias Nowack (DG1ATN)
 *
 */
 
 /*
  * Initialer Aufruf der Listnav
  */
function init_blaettern(id)
{
 	var param = "id="+id+"&start=0";
	request(param, "xml/listnav.php",show_listnav);
}
/*
 * Blaeattern button 
 */
function blaettern(id, start)
{
 	var param = "id="+id+"&start="+start;
	request(param, "xml/listnav.php",show_listnav);
}
/*
 * Sortieren nach Spalte
 */
function order_listnav(id,orderBy,dir)
{
 	var param = "id="+id+"&orderBy="+orderBy+"&dir="+dir;
	request(param, "xml/listnav.php",show_listnav);
}
/*
 * Anzeige der Liste
 */
function show_listnav(req)
{
	var xmlDocument = req.responseXML;
    if (xmlDocument)
    {
// Auslesen der session_id
    	var elem = xmlDocument.getElementsByTagName('session_id')[0];
		if (elem && (elem.firstChild != null))
			var session_id = elem.firstChild.nodeValue;
    	
// Darstellungsbereich leoschen
		document.getElementById('blaettern').innerHTML = '';

// Versteckt die session_id hinterlegen
		var hidden = document.createElement('input');
		hidden.setAttribute('type','hidden');
		hidden.setAttribute('name','session_id');
		hidden.setAttribute('value',session_id);
		hidden.setAttribute('id','session_id');
		document.getElementById('blaettern').appendChild(hidden);
		
// Laden der style daten
		var style = xmlDocument.getElementsByTagName('style');
		if (style)
		{
			var elem = style[0].getElementsByTagName('table')[0];
 			if (elem && (elem.firstChild != null))
 				var styleTable = elem.firstChild.nodeValue;
			var elem = style[0].getElementsByTagName('tr')[0];
 			if (elem && (elem.firstChild != null))
 				var styleTr = elem.firstChild.nodeValue;
			var elem = style[0].getElementsByTagName('th')[0];
 			if (elem && (elem.firstChild != null))
 				var styleTh = elem.firstChild.nodeValue;
			var elem = style[0].getElementsByTagName('td')[0];
 			if (elem && (elem.firstChild != null))
 				var styleTd = elem.firstChild.nodeValue;
		}
		
// Darstellen der Button
		var buttons = xmlDocument.getElementsByTagName('button');
		if (buttons)
		{
	    	var table = document.createElement('table');
	    	table.setAttribute('align','center');
	    	table.setAttribute('width','100%');
	    	var line = document.createElement('tr');
	    	
			for (var i = 0; i < 11; i++)
			{
	 			var cell = document.createElement('td');
	 			cell.setAttribute('width','9%');
	 			if (i == 5)
	 			{
		 			var elem = xmlDocument.getElementsByTagName('start')[0];
		 			if (elem && (elem.firstChild != null))
		 				var start = elem.firstChild.nodeValue;
		 			var elem = xmlDocument.getElementsByTagName('end')[0];
		 			if (elem && (elem.firstChild != null))
		 				var end = elem.firstChild.nodeValue;
		 			var elem = xmlDocument.getElementsByTagName('count')[0];
		 			if (elem && (elem.firstChild != null))
		 				var count = elem.firstChild.nodeValue;		
		 			var data = document.createTextNode(start + ' - ' + end + ' / ' + count);
		 			cell.appendChild(data); 			
	 			}
	 			var tagName = 'button'+i;
	 			var elem = xmlDocument.getElementsByTagName(tagName)[0];
	 			if (elem && (elem.firstChild != null))
				{	 			
		 			var button = document.createElement('input');
		 			button.setAttribute('type','button');
					button.setAttribute('name',elem.firstChild.nodeValue);
					button.setAttribute('value',elem.firstChild.nodeValue);
					button.setAttribute('onClick',elem.getAttribute('onClick'));
					cell.appendChild(button);
				}
				line.appendChild(cell);
			}    	
			table.appendChild(line);
		}
		
    	document.getElementById('blaettern').appendChild(table);
    	var hr = document.createElement('hr');
    	hr.setAttribute('width','90%');
    	document.getElementById('blaettern').appendChild(hr);

// Ermitteln der Sortierung
		var order = xmlDocument.getElementsByTagName('order');
		if(order)
		{
			var elem = order[0].getElementsByTagName('orderBy')[0];
 			if (elem && (elem.firstChild != null))
 				var orderBy = elem.firstChild.nodeValue;
			var elem = order[0].getElementsByTagName('dir')[0];
 			if (elem && (elem.firstChild != null))
 				var dir = elem.firstChild.nodeValue;
		}
		
// Ermitteln der Funktion zum Verarbeiten von qso's
		var funcs = xmlDocument.getElementsByTagName('funcs');
		if(funcs)
		{
			var elem = funcs[0].getElementsByTagName('func')[0];
 			if (elem && (elem.firstChild != null))
 				var func = elem.firstChild.nodeValue;
			var elem = funcs[0].getElementsByTagName('logo')[0];
 			if (elem && (elem.firstChild != null))
 				var logo = elem.firstChild.nodeValue;
			var elem = funcs[0].getElementsByTagName('alt')[0];
 			if (elem && (elem.firstChild != null))
 				var alt = elem.firstChild.nodeValue;
			var elem = funcs[0].getElementsByTagName('hint')[0];
 			if (elem && (elem.firstChild != null))
 				var hint = elem.firstChild.nodeValue;
			var elem = funcs[0].getElementsByTagName('txt')[0];
 			if (elem && (elem.firstChild != null))
 				var txt = elem.firstChild.nodeValue;
 		}

// Ermitteln ob Massenverarbeitung vorgesehen ist, checkbox und anforderung
		var check = xmlDocument.getElementsByTagName('check');
		if (check)
		{
			var elem = check[0].getElementsByTagName('name')[0];
 			if (elem && (elem.firstChild != null))
 				var checkName = elem.firstChild.nodeValue;
			var elem = check[0].getElementsByTagName('value')[0];
 			if (elem && (elem.firstChild != null))
 				var checkvalue = elem.firstChild.nodeValue;
			var elem = check[0].getElementsByTagName('func')[0];
 			if (elem && (elem.firstChild != null))
 				var checkfunc = elem.firstChild.nodeValue;
		}
		
// Form anlegen (nur Notwendig beim Einsatz von Checkboxen)
		if (checkName)
		{
			var form = document.createElement('form');
			form.setAttribute('name','listForm');
		}
// Anlegen der Tabelle		
    	var table = document.createElement('table');
    	table.setAttribute('align','center');
    	table.setAttribute('style',styleTable);
    	table.setAttribute('id','listNav');
    	var line = table.insertRow(0);
    	line.setAttribute('style',styleTr);

// Darstellen der Kopfzeile    	
    	var headline = xmlDocument.getElementsByTagName('headline');
		for (var i = 0; i < headline[0].childNodes.length; i++)
		{
			if (headline[0].childNodes[i].nodeType != 1) continue;
 			var cell = document.createElement('th');
 	   		cell.setAttribute('style',styleTh);
 
 			var elem = headline[0].childNodes[i];
 			if ((elem.nodeName == 'qso_nr') && (checkName))
 			{
				var data = document.createTextNode(' ');
				cell.appendChild(data);
				line.appendChild(cell);
 			} 
 			if (elem.nodeName != 'qso_nr')
 			{
				var data = document.createTextNode(elem.firstChild.nodeValue);
				cell.appendChild(data);
				if (elem.nodeName == orderBy)
				{
					var img = document.createElement('img');
					if (dir == 'ASC')
					{
						img.setAttribute('src','images/asc.gif');
						cell.setAttribute('onClick','order_listnav(\"'+session_id+'\",\"'+elem.nodeName+'\",\"DESC\");');
					}
					if( dir == 'DESC')
					{
						img.setAttribute('src','images/desc.gif');
						cell.setAttribute('onClick','order_listnav(\"'+session_id+'\",\"'+elem.nodeName+'\",\"ASC\");');
					}
					cell.appendChild(img);
				}
				else
					cell.setAttribute('onClick','order_listnav(\"'+session_id+'\",\"'+elem.nodeName+'\",\"ASC\");');
				line.appendChild(cell);
			}
		}
		if (func)
		{
			var cell = document.createElement('th');
			cell.appendChild(document.createTextNode(' '));
 	   		cell.setAttribute('style',styleTh);
			line.appendChild(cell); 
		}   	
		
// Darstellen der Datenzeilen		
		var doc = xmlDocument.getElementsByTagName("qso");
	 	for (var i = 0; i < doc.length; i++)
 		{
			var line = table.insertRow(1+i);
		   	line.setAttribute('style',styleTr);
// QSO Nummer ermitteln
 			var elem = doc[i].getElementsByTagName("qso_nr")[0];
 			if (elem && (elem.firstChild != null))
				var qsoNr = elem.firstChild.nodeValue;

			line.setAttribute('id',qsoNr);
			
 			for (var j = 0; j < doc[i].childNodes.length; j++)
 			{
 				if (doc[i].childNodes[j].nodeType != 1) continue;
 	 			var cell = document.createElement('td');
	 	   		cell.setAttribute('style',styleTd);
 	 			var value = "";
	 			var elem = doc[i].childNodes[j];
	 			if (elem.firstChild != null)
	 			{
					value = elem.firstChild.nodeValue;
					var data = document.createTextNode(value); 
					cell.appendChild(data);
				}
	 	 		cell.setAttribute('id',elem.nodeName+'-'+qsoNr);
	 	 		cell.setAttribute('onClick','eqslSelectField(\"'+qsoNr+'\", \"'+elem.nodeName+'\", \"'+value+'\")');
	 	 		if (elem.nodeName == "DXCC")
				{
					cell.setAttribute("onmouseover","showTT(showDXCC('"+value+"'))");
					cell.setAttribute("onmouseout","hideWMTT()");
				}	 	 		
	 	 		if (elem.nodeName == "DOK")
				{
					cell.setAttribute("onmouseover","showTT(showDOK('"+value+"'))");
					cell.setAttribute("onmouseout","hideWMTT()");
				}
				if (elem.nodeName == "QSL_SENT")
				{
                                         cell.setAttribute("onmouseover","showTT(showQSLInfo())");
                                         cell.setAttribute("onmouseout","hideWMTT()");
				}	 	 		
				if (elem.nodeName != 'qso_nr')
					line.appendChild(cell);
				if ((elem.nodeName == 'qso_nr') && (checkName))
				{
	 	 			var cell = document.createElement('td');
		 	   		cell.setAttribute('style',styleTd);
					var input = document.createElement('input');
					input.setAttribute('type','checkbox');
					input.setAttribute('name',qsoNr);
					input.setAttribute('value',qsoNr);
					input.setAttribute('onChange','checkboxChanged();');
					cell.appendChild(input);
					line.appendChild(cell);				
				}
 			}
			if (func)
			{
 	 			var cell = document.createElement('td');
	 	   		cell.setAttribute('style',styleTd);
	 	 		cell.setAttribute('id',func+'-'+qsoNr);
				var link = document.createElement('a');
				if ( i == 0)
					var linkFocus = link;
				link.setAttribute('href','javascript:'+func+'('+qsoNr+')');
	 			link.setAttribute('title',hint);
				link.setAttribute('tabindex',i+2);
				link.setAttribute('onFocus','funcOnFocus('+qsoNr+');');
				link.setAttribute('onBlur','funcOnBlur('+qsoNr+');');
	 			if (logo)
	 			{
		 			var img = document.createElement('img');
	 				img.setAttribute('src',logo);
	 				img.setAttribute('alt',alt);
	 				link.appendChild(img);
	 			}
	 			if (txt)
	 			{
	 				var data = document.createTextNode(txt);
	 				link.appendChild(data);	 				
	 			}
	 			cell.appendChild(link);
	 			line.appendChild(cell);
			}
		}
		if (form)
    		form.appendChild(table);			
		else	
    		document.getElementById('blaettern').appendChild(table);
    	if (checkName)
    	{
    		var table = document.createElement('table');
    		table.setAttribute('style',styleTable);
    		var line = document.createElement('tr');
    		var cell = document.createElement('td');
    		cell.setAttribute('id','select');
    		cell.setAttribute('style','width:6%;');
    		var button = document.createElement('input');
    		button.setAttribute('type','button');
    		button.setAttribute('value', 'Select all');  
    		button.setAttribute('onClick','selectAll();'); 	
    		cell.appendChild(button);
 			line.appendChild(cell);
    		var cell = document.createElement('td');
    		cell.setAttribute('id','deselect');
    		cell.setAttribute('style','width:6%;');
 			line.appendChild(cell);
    		var cell = document.createElement('td');
    		cell.setAttribute('style','width:60%;');
 			line.appendChild(cell);
    		var cell = document.createElement('td');
    		var button = document.createElement('input');
    		button.setAttribute('type','button');
    		button.setAttribute('value', checkvalue);  
    		button.setAttribute('onClick', checkfunc+'("'+session_id+'");'); 	
    		cell.appendChild(button);
 			line.appendChild(cell);
 			table.appendChild(line);
    		form.appendChild(table);			
	    	document.getElementById('blaettern').appendChild(form);			
    	}
	}	
	if (linkFocus)
		linkFocus.focus(); 	
}
// Auswahl aller checkboxen sofern angezeigt
function selectAll()
{
 	for (var i = 0; i < document.listForm.elements.length; i++) 
 	{
    	var e = document.listForm.elements[i];
    	if ((e.type == 'checkbox')) 
    	{
			e.checked = true;
    	}
  	}
  	checkboxChanged();
}
// Abwahl aller checkboxen sofern angezeigt
function deSelectAll()
{
 	for (var i = 0; i < document.listForm.elements.length; i++) 
 	{
    	var e = document.listForm.elements[i];
    	if ((e.type == 'checkbox')) 
    	{
			e.checked = false;
    	}
  	}
  	checkboxChanged();
}
// Darstelung der Auswahlbutton
function checkboxChanged()
{
	var select = false;
	var deselect = false;
 	for (var i = 0; i < document.listForm.elements.length; i++) 
 	{
    	var e = document.listForm.elements[i];
    	if ((e.type == 'checkbox')) 
    	{
			if (e.checked == false)
				select = true;
			if (e.checked == true)
				deselect = true;
    	}
  	}
	var cell = document.getElementById('select');
	cell.innerHTML = "";
  	if (select)
  	{
  		var button = document.createElement('input');
  		button.setAttribute('type','button');
  		button.setAttribute('value', 'Select all');  
  		button.setAttribute('onClick','SelectAll();'); 	
  		cell.appendChild(button);
  	}
	var cell = document.getElementById('deselect');
	cell.innerHTML = "";
  	if (deselect)
  	{
  		var button = document.createElement('input');
  		button.setAttribute('type','button');
  		button.setAttribute('value', 'Deselect all');  
  		button.setAttribute('onClick','deSelectAll();'); 	
  		cell.appendChild(button);
  	}
}

function funcOnFocus(qso)
{
	var elem = document.getElementById('RUFZ-'+qso);
	if (elem)
	{
		var style = elem.getAttribute('style');
		elem.setAttribute('style','font-weight:bold;color:red;'+style);	
	}
}

function funcOnBlur(qso)
{
	var elem = document.getElementById('RUFZ-'+qso);
	if (elem)
	{
		var style = elem.getAttribute('style');
		var newStyle = style.replace(/font-weight:bold;color:red;/i,"");
		elem.setAttribute('style',newStyle);
	}
}

