
////////////// Layers functions ///////
// Browsercheck:
var browserIE = document.all ? 1 : 0
var browserNetscape = document.layers ? 1 : 0


//// Layers
function isShown(id)
{
	return document.getElementById(id).style.display != "none" ;
}

function showLayer(id)
{
	element = document.getElementById(id);
	
	if(element == null) {
		alert('Element not found!');
		return;
	}

	// for rows set 'table-row' if IE else 'block'
	if((element.tagName == "tr" || element.tagName == "TR")) {
		if(browserIE)
			element.style.display = "block";
		else
			element.style.display = "table-row";
	} 
	else
		element.style.display = "block";

}
function showIt(id)
{
	showLayer(id);
}

function switchIt(id) {
	if (isShown(id))
		hideIt(id);
	else
		showIt(id);
}

function hideLayer(id)
{
	return document.getElementById(id).style.display = "none";
}
function hideIt(id)
{
	hideLayer(id);
}

function switchLayer(objCheckbox, layer_id) {
	if (objCheckbox.checked)
		showLayer(layer_id);
	else
		hideLayer(layer_id);
}


function switchAll(idPrefix, id) {
	//alert(idPrefix);
	for(i=0; i<document.all.length; i++) {
		obj = document.all[i];
		if(obj.id.substring(0, idPrefix.length) == idPrefix && obj.id != id){
			//alert(obj.id);
			//id.style.display = "none";
			//if(obj.style.display == "none")
			//	obj.style.display = "block";
			//else
				obj.style.display = "none";
				

		}
		if(obj.id == id) {
			if(obj.style.display == "none")
				obj.style.display = "block";
			else
				obj.style.display = "none";
		}
			//switchIt(id);
		
	}
	//switchIt(id);
}

function switch12(id) {
	switchIt(id + "1");
	switchIt(id + "2");
}

// Example: obj = findObj("image1");

function findObj(theObj, theDoc)
{

  var p, i, foundObj;

  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
	theDoc = parent.frames[theObj.substring(p+1)].document;
	theObj = theObj.substring(0,p);
  }

  if(!(foundObj = theDoc[theObj]) && theDoc.all) 
  	foundObj = theDoc.all[theObj];

  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
	foundObj = theDoc.forms[i][theObj];

  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
	foundObj = findObj(theObj, theDoc.layers[i].document);

  if(!foundObj && document.getElementById) 
  	foundObj = document.getElementById(theObj);

  

  return foundObj;

}
function setIfr(id, location) {
	document.getElementById(id).src = location;
}

/////// form functions /////////////////////
function ask(message, URLStr)
{
	if(URLStr == null) {
		if(confirm("Are you sure?"))
		{
			if(message != null)
				location = message;
			return true;
		}
		return false;
	}
	
	if(confirm(message))
	{
		location = URLStr;
		return true;
	}
	return false;
}



/////////////// list actions //////////////////

function cmdEdit(id){
	document.mf.cmd.value='edit'; 
	document.mf.id.value=id; 
	document.mf.submit();
	document.mf.cmd.value='showList'; 
}


function cmdDelete(id, message){
	if(message == null)
	   message = "Are you sure?";
	if(confirm(message)) {
		document.mf.cmd.value='delete'; 
		document.mf.id.value=id;
		document.mf.submit();
		document.mf.cmd.value='showList'; 
	}
}

function cmdAddNew(){
	document.mf.cmd.value='addNew'; 
	document.mf.submit();
	document.mf.cmd.value='showList';
}

function listGo(where) {
	document.mf.go.value=where; 
	document.mf.cmd.value='showList'; 
	document.mf.submit();
	document.mf.cmd.value='showList'; 
}

function listOrderBy(what) {
	document.mf.orderBy.value=what; 
	document.mf.cmd.value='showList'; 
	document.mf.submit();
	document.mf.cmd.value='';
}



//////////////// AJAX //////////////////////////

function ajaxSetInnerHTML(url, object, innerHTML) {

	var http_request = false;

	if (window.XMLHttpRequest) { // Mozilla, Safari,...
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		
		}
	} else if (window.ActiveXObject) { // IE
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}

	if (!http_request) {
		alert('Giving up. Cannot create an XMLHTTP instance');
		return false;
	}
	
	http_request.onreadystatechange = function() { 
		if (http_request.readyState == 4) {
            if (http_request.status == 200) {
				//alert("Result: " + http_request.responseText);
				if(innerHTML == null || innerHTML == "")
					innerHTML = http_request.responseText;	
				else
					innerHTML = innerHTML.replace("$(result)", http_request.responseText);
			
					
				if(object == "JS-EXECUTE")
					eval(innerHTML);
				else if(typeof object == "string")
					document.getElementById(object).innerHTML=innerHTML;
				else if(object != null)
					object.innerHTML=innerHTML;
            } else {
                alert('AJAX: There was a problem with the server.');
				return false;
            }
        }
		// eval(callBackExecute)alertContents(http_request); 
	};
	http_request.open('GET', url, true);
	http_request.send(null);
	return true;

}
function ajaxExecute(url, jsToExecute) {
	return ajaxSetInnerHTML(url, "JS-EXECUTE", jsToExecute);
}
function ajax(url) {
	return ajaxSetInnerHTML(url);
}
function askAjax(url) 
{
	if(ask())
		return ajax(url);
}

function ajaxSet(url, itemToSet, value) {
	
	jsToExecute=itemToSet + "=" + "'" + value + "'";
	return ajaxSetInnerHTML(url, "JS-EXECUTE", jsToExecute);
	
}

function ajaxGet(url) {
	return ajaxSetInnerHTML(url, "GET");
}

function setInnerHTML(object, innerHTML) {
	document.getElementById(object).innerHTML=innerHTML;
}




////// TABLE DOM /////////
/**
Clone row with 'rowName', add it after. Original row will be renamed to 'rowName' + '_old'
If idTagName is defined:
	set id tag to '0'
*/
function cloneRow(rowName, idTagName) {
	row = document.getElementById(rowName); 
	
	newRow = null;
	if(row == null) {
	 	alert("Row not found!");
		return null;
	}
	try {
		newRow = row.cloneNode(true);
		row.parentNode.insertBefore(newRow, row);
		if(browserIE)
			newRow.style.display = "block";
		else
			newRow.style.display = "table-row";
			
		newRow.id = "";
	} catch (err) {
		alert(err.description)
	}
	/*
	if(idTagName != null) {
		var inputTags = newrow.getElementsByTagName("input");
		for (var i = 0; i < inputTags.length ; i++)
		   if(inputTags[i].name == idTagName)
		   		inputTags[i].value = "0";
		

	}
	*/
	
	return newRow;
}
function deleteRow(element) {
	if(typeof element == "string")
		element = document.getElementById(object);
	
	// find tr (table row)
	var i;
	for(i = 0; i<10; i++) {
		if(element.tagName == "TR" || element.tagName == "tr")
			break;
		element = element.parentNode;
	}
	if(i < 10) // found
		element.parentNode.removeChild(element);
	else
		alert("Row not found");
		
}
/**
Find first child with defined name
DOESN'T WORK
*/
function findChild(element, childNodeName)
{
	if(typeof element == "string")
		element = document.getElementById(object);
	
	 for (i = 0; i < element.childNodes.length; i++) {
     	childNode = element.childNodes[i];
		if(childNode.name == childNodeName)
			return childNode;
		
		// search inside child
		foundObject = findChild(childNode, childNodeName);
		if(foundObject != null)
			return foundObject;
	}

	return null;
}

function testPrevious(element)
{
	prev = element.previousSibling;
	if(prev == null)
		alert("no previous");
	
	alert(prev.name);
}