var http_request = false;

function makeRequest(url, parameters) {
  if (parameters==null) parameters = false;
  http_request = false;
  if (window.XMLHttpRequest) {
	 http_request = new XMLHttpRequest();
	 //if (http_request.overrideMimeType) http_request.overrideMimeType('text/html');
  } 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('Cannot create XMLHTTP instance');
	 return false;
  }
  http_request.onreadystatechange = response;

  http_request.open("POST", url);
  //alert(get(document.getElementById(parameters)));
  if (document.getElementById('textarea_url')) document.getElementById('textarea_url').value = url + "\n\n" + ((parameters!=false)?get(document.getElementById(parameters)):'');
  http_request.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); 
  http_request.send(
	'xml=<root>' + ((parameters!=false)?get(document.getElementById(parameters)):'') +  '</root>'
  );
}

function response() {
	if (http_request.readyState == 4 ) {
		if (http_request.status==200) {
			//debug
			if (document.getElementById('textarea_code')) document.getElementById('textarea_code').value = http_request.responseText;
			//output ophalen in XML		
			var output = http_request.responseXML.documentElement;
			var err, msg, parent, target, node, childNode;
			//loopen door de output
			if (output!=null && output.hasChildNodes && output.childNodes!=null) {
				for(var i = 0; i < output.childNodes.length; i++) {
					node = output.childNodes[i];
					//nodeType 3 is #text, heb je niks aan
					if (node.nodeType!=3 && node.hasChildNodes()) {
						switch(node.nodeName) {
							case "add":
								var obj = null;
								for(var e = 0; e < node.childNodes.length; e++) {
									childNode = node.childNodes[e];
									if (childNode.nodeType!=3) {
										if (childNode.nodeName=="target") {
											if (document.getElementById(childNode.firstChild.nodeValue)!=null && obj!=null) {
												BrowserDetect.init();
												if (obj=="attr") set_attribute(document.getElementById(childNode.firstChild.nodeValue),attrNode.nodeName,(attrNode.hasChildNodes())?attrNode.firstChild.nodeValue:'',BrowserDetect.browser);//document.getElementById(childNode.firstChild.nodeValue).setAttribute(attrNode.nodeName,attrNode.firstChild.data);
												else if  (obj=="event") {
													if (BrowserDetect.browser=="Explorer") {
														eval('document.getElementById(childNode.firstChild.nodeValue).' + attrNode.nodeName + ' = function() { ' + attrNode.firstChild.nodeValue + ' } ');
													} else {
														set_attribute(document.getElementById(childNode.firstChild.nodeValue),attrNode.nodeName,(attrNode.hasChildNodes())?attrNode.firstChild.nodeValue:'',BrowserDetect.browser);
													}
												} else document.getElementById(childNode.firstChild.nodeValue).appendChild(obj);
												//if (child.firstChild) newE.
											} else
												alert("kan " + childNode.nodeValue + " niet vinden." + childNode.firstChild.nodeValue);
										} else {
											obj = add_element(childNode);
											if (obj=="attr" || obj=="event") var attrNode = childNode;
										}
									}
								}
							break;
							case "fill":
								var formNode = null;
								var form = null;
								for(var e = 0; e < node.childNodes.length; e++) {
									formNode = node.childNodes[e];
									if (formNode.nodeType!=3) {
										eval("form = document." + formNode.nodeName);
										if (form!=null) {
											for(var a = 0; a < formNode.childNodes.length; a++) {
												childNode = formNode.childNodes[a];
												if (childNode.nodeType!=3) {
													if (childNode.nodeName=="action") {
														form.action = childNode.firstChild.nodeValue;
													} else {
														var tmp;
														eval("tmp = form." + childNode.nodeName);
														if (tmp!=null) eval("form." + childNode.nodeName + ".value = (childNode.hasChildNodes()) ? childNode.firstChild.nodeValue : ''");
													}
												}
											}
										}
									}
								}
							break;
							case "remove_all":
								if (document.getElementById(node.firstChild.nodeValue)!=null) {
									var element = document.getElementById(node.firstChild.nodeValue)
									var aantal = element.childNodes.length-1;
									while(aantal>=0) element.removeChild(element.childNodes[aantal--]);
								} else
									alert("kan " + node.firstChild.nodeValue + " niet vinden.");								
							break;
							case "remove":
								if (document.getElementById(node.firstChild.nodeValue)!=null) {
									document.getElementById(node.firstChild.nodeValue).parentNode.removeChild(document.getElementById(node.firstChild.nodeValue));
								} else
									alert("kan " + node.firstChild.nodeValue + " niet vinden.");								
							break;
							case "showhide":
								for(var e = 0; e < node.childNodes.length; e++) {
									childNode = node.childNodes[e];
									if (childNode.nodeType!=3) showhide(childNode.nodeName,childNode.firstChild.nodeValue);
								}
							break;
							case "event":
								eval(node.firstChild.nodeValue);
							break;
						}
					}
				}
			}
			set_load(null);
			
		}
	}	
}

function add_element(obj) {
	var newE = null;
	var attE = null;
	
	switch(obj.nodeName.toLowerCase()) {
		case "html":
		case "head":
		case "body":
		case "title":
		case "script":
		case "style":
		case "object":
		case "em":
		case "strong":
		case "q":
		case "cite":
		case "dfn":
		case "abbr":
		case "acronym":
		case "code":
		case "var":
		case "kbd":
		case "samp":
		case "sub":
		case "sup":
		case "del":
		case "ins":
		case "isindex":
		case "a":
		case "br":
		case "map":
		case "area":
		case "param":
		case "embed":
		case "noembed":
		case "applet":
		case "span":
		case "p":
		case "blockquote":
		case "select":
		case "option":
		case "h1":
		case "h2":
		case "h3":
		case "h4":
		case "h5":
		case "h6":
		case "dl":
		case "dt":
		case "dd":
		case "ol":
		case "ul":
		case "li":
		case "dir":
		case "menu":
		case "table":
		case "tr":
		case "th":
		case "td":
		case "colgroup":
		case "caption":
		case "thead":
		case "tbody":
		case "tfoot":
		case "button":
		case "div":
		case "pre":
		case "address":
		case "iframe":
		case "frameset":
		case "frame":
		case "noframes":
		case "big":
		case "small":
		case "tt":
			newE =document.createElement(obj.nodeName);
			if (obj.hasChildNodes()) {
				for(var e = 0; e < obj.childNodes.length; e++) {
					var child = obj.childNodes[e];
					attE = add_element(child);
					BrowserDetect.init();
				
					if (attE=="attr") {
						if (child.firstChild) set_attribute(newE,child.nodeName,(child.hasChildNodes())?child.firstChild.nodeValue:'',BrowserDetect.browser);
					} else if (attE=="event") { 
						//alert(child.nodeName + " " + child.firstChild.nodeValue);
						//newE.attachEvent(child.nodeName,child.firstChild.nodeValue,false);
						if (BrowserDetect.browser=="Explorer") {
							eval('newE.' + child.nodeName + ' = function() { ' + child.firstChild.nodeValue + ' } ');
						} else {
							set_attribute(newE,child.nodeName,(child.hasChildNodes())?child.firstChild.nodeValue:'',BrowserDetect.browser);
						}
					} else if (attE!=null) newE.appendChild(attE);
				}
			}
		break;
		
		case "onblur":
		case "onchange":
		case "onclick":
		case "ondblclick": 
		case "onfocus":
		case "onkeydown":
		case "onkeypress": 
		case "onkeyup":
		case "onmousedown":
		case "onmousemove":
		case "onmouseout":
		case "onmouseover": 
		case "onmouseup":
		case "onresize":
		case "onscroll":
			newE = "event";
		break;
		
		case "txt":
			//ce = obj.first;
			newE = document.createTextNode('');
			newE.data= (obj.firstChild) ? obj.firstChild.nodeValue : '';
		break;
		default:
			newE = "attr";

		break;
		
	}
	return newE;
}

function get(obj) {
	var getstr = "";
	var i = 0;
	
	if (obj!=false && obj!=null) {
		if (obj.tagName.toUpperCase()!="INPUT" && obj.tagName.toUpperCase()!="TEXTAREA" && obj.tagName.toUpperCase()!="SELECT") {
			if (obj.tagName.toUpperCase()!="FORM") {
				for (i; i<obj.childNodes.length; i++) {
					var oo = obj.childNodes[i];
					if (oo.hasChildNodes() && oo.tagName.toUpperCase()!="TEXTAREA" && oo.tagName.toUpperCase()!="SELECT") {
						getstr += get(oo);
					}
					getstr += get_element(oo);
				}
			} else {
				for (i; i<obj.elements.length; i++) {
					var oo = obj.elements[i];
					getstr += get_element(oo);
				}
			}
		} else {
			getstr += get_element(obj);
		}
		return getstr;
	}
}

function get_element (obj) {
	var getstr = '';
	
	if (obj!=null) {
		switch(obj.tagName) {
			case "TEXTAREA":
			case "INPUT":
				switch(obj.type.toLowerCase()) {
					case "checkbox":
					   getstr +=  (obj.checked) ? "<"+obj.name + ">" + obj.value + "</" + obj.name + ">" : "<" + obj.name + "></" + obj.name + ">";
					break;
					case "radio":
					   if (obj.checked) getstr += "<" + obj.name + ">" + obj.value + "</" + obj.name + ">";
					break;
					case "text":
					case "password":
					default:
					   getstr += "<"+obj.name + ">" + ((obj.value!="undefined")?obj.value:'') + "</" + obj.name + ">";
					break;
					
				}
			break;
			
			case "SELECT":
				if (obj.multiple) {
					for(i = 0;i<obj.options.length;i++) {
						getstr += "<" + obj.name + ">" + obj.options[i].value + "</" + obj.name + ">";	
					}
				} else {
					getstr += "<" + obj.name + ">" + obj.options[obj.selectedIndex].value + "</" + obj.name + ">";	
				}
			break;
		 }
	}
	return getstr;
}

function showhide(obj,hide) {
	if (el = document.getElementById(obj)) {
		el.style.display = (hide=="hide" || hide=="0" || hide=="false") ? "none" : "block";
	}
}

function set_attribute(obj,attrName,attrValue,browser) {
	//IE / FF verschillen... zucht
	switch(attrName.toLowerCase()) {
		case "classname":
		case "class":
			attrName = (browser=="Explorer") ? "className" : "class";
		break;
		case "id":
			attrName = (browser=="Explorer") ? "id" : "id";
		break;
	}
	
	obj.setAttribute(attrName,attrValue);
}

var old_load = null;
function set_load(obj) {
	//interrogate(obj);
	if (obj==null && old_load!=null) {
		var tmp = document.getElementById('load_' + old_load.name);
		if (tmp!=null) tmp.parentNode.removeChild(tmp);
		old_load.style.display = 'block';
		old_load = null;
	} else if (obj!=null && obj!='undefined' && obj.parentNode!=null) {
		old_load = obj;
		var el =document.createElement("img");
		el.setAttribute("src","/import/image/load.gif");
		set_attribute(el,"ID","load_" + obj.name);
		obj.parentNode.appendChild(el);
		obj.style.display = 'none';
	}
}



function interrogate(what) {
    var output = '';
	var val;
    for (var i in what) {
		eval('val = what.' + i)
        output += i+ ' - ' + val + '\n';
	}
    document.getElementById('textarea_prop').value += "\n\n\n\n\n\n\n\n\n\n-------------" + what + "------------------------------------\n\n\n\n\n\n\n\n" + output;
}



var hexChars = "0123456789ABCDEF";
function Dec2Hex (Dec) {
	var a = Dec % 16;
	var b = (Dec - a)/16;
	hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
	return hex;
}

function changbg (begin, end, steps) {   
	steps = steps -1 ;
	redA     = begin.charAt(0) + begin.charAt(1);
	red_valA = parseInt(redA,'16');
	redB     = end.charAt(0) + end.charAt(1);
	red_valB = parseInt(redB,'16');
	red_int  = ((red_valB - red_valA) / steps) * -1;
	grnA     = begin.charAt(2) + begin.charAt(3);
	grn_valA = parseInt(grnA,'16');
	grnB     = end.charAt(2) + end.charAt(3);
	grn_valB = parseInt(grnB,'16');
	grn_int  = ((grn_valB - grn_valA) / steps) * -1;
	bluA     = begin.charAt(4) + begin.charAt(5);
	blu_valA = parseInt(bluA,'16');
	bluB     = end.charAt(4) + end.charAt(5);
	blu_valB = parseInt(bluB,'16');
	blu_int  = ((blu_valB - blu_valA) / steps) * -1;
	red = red_valA;
	grn = grn_valA;
	blu = blu_valA;
	document.bgColor = begin;
	red -= red_int;
	red_round = Math.round(red);
	red_hex = Dec2Hex(red);
	grn -= grn_int;
	grn_round = Math.round(grn);
	grn_hex = Dec2Hex(grn);
	blu -= blu_int;
	blu_round = Math.round(blu);
	blu_hex = Dec2Hex(blu);
	setTimeout("changbg('" + red_hex + grn_hex + blu_hex + "','"+end+"'," + steps + ")",1);
}


var old_class = null;
function mouse(e,obj,cl) {
	if (!cl) cl = '';
	if (e.type=="mouseout") {
		obj.className = (old_class!=null) ? old_class : "mouseout" + cl;
		old_class = null;
	} else {
		old_class = obj.className;
		obj.className = "mouseover" + cl;
	}
}


























//BrowserDetect.init();
var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};
