/**
 * <p>
 * <b>GlsAjax </b>
 * </p>
 * <p>
 * This a class to handle ajax-calls on the client-side
 * It's closely integrated into the Struts-framework
 * </p>
 * 
 * 
 * 
 * @author Jan Hoffman 14.02.2006
 * 
 *  
 */
//add formelement dynamicly
function addFormElement(id,target,action)
{
   var body=document.getElementsByTagName("body").item(0);
   var form = document.createElement('form');
   form.setAttribute("id",id);
   form.setAttribute("target",target);
   form.setAttribute("action",action);
   body.appendChild(form);
   return form;
}
//add hidden tag dynamicly
function addDataElement(name,value)
{
    var element = document.createElement('input');
    element.setAttribute("type","hidden");
    element.setAttribute("name",name);
    element.setAttribute("value",value);
    return element;
}
var debugOn=false;

var debug = new console();

function console()
{
   if (debugOn)
   {
    	this.win=window.open();
   		this.println =  function(strLine)
   		{
      		this.win.document.writeln(strLine+"<br>");
   		};
   		this.print = function(strValue)
   		{
      		this.win.document.write(strValue);
   		}; 
   		this.clear =function()
   		{
   			this.win.document.open();
   		};
    }
	else
	{
		this.println =  function(strLine)
   		{
      		
   		};
   		this.print = function(strValue)
   		{
      		
   		}; 
   		this.clear =function() {};
	}   
}

var tmpAjax; 
 //define attribute to send back to GlsAjaxAction
function DEFINES(){}
DEFINES.checkbox = new Array("value","checked","disabled","name","id");
DEFINES.text = new Array("value","disabled","name");
DEFINES.hidden = new Array("value","name");
//QueryCommit test if submit is ok or should be cancelled
function QueryCommit(event){ if (tmpAjax!=null && tmpAjax.hasErrors)  { return true;  }}
function QueryGeckoCommit(event) { if (tmpAjax!=null && tmpAjax.hasErrors) { event.preventDefault();}}


function getAttribute(node,attrName)	{   if (node[attrName]!=null)   return node[attrName];   return "";}
function attributeExist(node,attrName)	{   return node[attrName]!=null;}

function getElementByAttrValue(node,attrName,attrValue)
{
	if (getAttribute(node,attrName)==attrValue) return node;      
		for (var i=node.childNodes.length-1; i>=0; i--)  
		{
			var child = node.childNodes.item(i);
			if ( child.nodeType == 1 )  
			{
				var tmpNode=this.getElementByAttrValue(child, attrName,attrValue);
				if (tmpNode!=null)   return tmpNode;
			}
		}
	return null;
}

function strip(value)
{
   if (value.indexOf("#")>-1)    return value.substr(value.indexOf("#")+1);
   else						     return value;
}


function isGecko()
{
   return navigator.userAgent.indexOf("Gecko")>0;
}

function isMSIE()
	{
	  bMSIE=navigator.userAgent.indexOf("MSIE")>0;
	  if (navigator.userAgent.indexOf("Opera")>0)
	     bMSIE=false;
	  return bMSIE;
	}
	
function isOpera()
{
	return navigator.userAgent.indexOf("Opera")>0;
}

function getText(xmlNode)
   {
      if (isGecko())
         return xmlNode.getText();
      else    
         return xmlNode.text;
   }

//*********************** XML stuff ************************************************

// used to find the Automation server name
function getDomDocumentPrefix() {
	if (getDomDocumentPrefix.prefix)
		return getDomDocumentPrefix.prefix;
	
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			o = new ActiveXObject(prefixes[i] + ".DomDocument");
			return getDomDocumentPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	throw new Error("Could not find an installed XML parser");
}

function getXmlHttpPrefix() {
	if (getXmlHttpPrefix.prefix)
		return getXmlHttpPrefix.prefix;
	var prefixes = ["MSXML2", "Microsoft", "MSXML", "MSXML3"];
	var o;
	for (var i = 0; i < prefixes.length; i++) {
		try {
			// try to create the objects
			o = new ActiveXObject(prefixes[i] + ".XmlHttp");
			return getXmlHttpPrefix.prefix = prefixes[i];
		}
		catch (ex) {};
	}
	throw new Error("Could not find an installed XML parser");
}

// XmlHttp factory
function XmlHttp() {}

XmlHttp.create = function () {
	try {
		if (window.XMLHttpRequest) {
			var req = new XMLHttpRequest();
			// some versions of Moz do not support the readyState property
			// and the onreadystate event so we patch it!
			if (req.readyState == null) {
				req.readyState = 1;
				req.addEventListener("load", function () {
					req.readyState = 4;
					if (typeof req.onreadystatechange == "function")
						req.onreadystatechange();
				}, false);
			}
			return req;
		}
		if (window.ActiveXObject) {
			return new ActiveXObject(getXmlHttpPrefix() + ".XmlHttp");
		}
	}
	catch (ex) {}
	// fell through
	throw new Error("Your browser does not support XmlHttp objects");
};

// XmlDocument factory
function XmlDocument() {}

if( isGecko() )
{
	// prototying the Document
   Document.prototype.selectNodes = function(strNodeName)
   {
      var aResult = [];
      aResult = this.getElementsByTagName(strNodeName);
      return aResult;
   }

   // prototyping the Element
   	Element.prototype.selectNodes = function(cXPathString)
   	{
      	if(this.ownerDocument.selectNodes){ return this.ownerDocument.selectNodes(cXPathString, this);   }
    	else{throw "selectNodes not implemented";}
   	}
   	// prototyping the Document
   	Document.prototype.selectSingleNode = function(strNodeName)
   	{
      	var xItems = this.selectNodes(strNodeName);
      	if( xItems.length > 0 )      {    return xItems[0];      }
      	else      {         return null;      }
   	}	
   
   	// prototyping the Element
   	Element.prototype.selectSingleNode = function(strNodeName)
   	{	
      	if(this.ownerDocument.selectSingleNode)   {    return this.ownerDocument.selectSingleNode(strNodeName);      }
      	else{throw "selectSingleNode not implemented";}
   	}
	Element.prototype.setText = function(strText) { this.appendChild(document.createTextNode(strText)); }
	Element.prototype.getText = function() 
	{ 
	   if (this.firstChild!=null)
	      return this.firstChild.nodeValue;
	   else
	      return "";
	}
}

    	
XmlDocument.create = function () {
	try {
		// DOM2
		if (document.implementation && document.implementation.createDocument) {
			var doc = document.implementation.createDocument("", "", null);
			
			// some versions of Moz do not support the readyState property
			// and the onreadystate event so we patch it!
			if (doc.readyState == null) {
				doc.readyState = 1;
				doc.addEventListener("load", function () {
					doc.readyState = 4;
					if (typeof doc.onreadystatechange == "function")
						doc.onreadystatechange();
				}, false);
			}
			
			return doc;
		}
		if (window.ActiveXObject)
			return new ActiveXObject(getDomDocumentPrefix() + ".DomDocument");
	}
	catch (ex) {}
	throw new Error("Your browser does not support XmlDocument objects");
};



if (window.DOMParser &&
	window.XMLSerializer &&
	window.Node && Node.prototype && Node.prototype.__defineGetter__) {
	
	//XMLDocument.prototype.loadXML = 
	Document.prototype.loadXML = function (s) {
		// parse the string to a new doc	
		var doc2 = (new DOMParser()).parseFromString(s, "text/xml");
		// remove all initial children
		while (this.hasChildNodes())
			this.removeChild(this.lastChild);
		// insert and import nodes
		for (var i = 0; i < doc2.childNodes.length; i++) {
			this.appendChild(this.importNode(doc2.childNodes[i], true));
		}
	};
	
	Document.prototype.__defineGetter__("xml", function () {
		return (new XMLSerializer()).serializeToString(this);
	});
}


 
 
