/**
  * *********************************************************************************************** *
  * *  Written by Keith Vernon                                                                    * *
  * *  Date : 23 October 2007                                                                     * *
  * *                                                                                             * *
  * *  Décalog                                                                                    * *
  * *                                                                                             * *
  * *  DK.dom                                                                                     * *
  * *  DK.http                                                                                    * *
  * *  DK.ajax                                                                                    * *
  * *  DK.form                                                                                    * *
  * *  DK.Utf8                                                                                    * *
  * *       encode                                                                                * *
  * *       decode                                                                                * *
  * *                                                                                             * *
  * *********************************************************************************************** *
*/
	// The $ function
	//$ = function(p) {return document.getElementById(p)}

	DK={version:"1.0-beta1"};
	//window["undefined"]=window["undefined"];
	
	/* Create a DOM object */
	DK.dom = function(xmlstring) {
									// xmlstring to load
									var dom
									if (window.DOMParser) {
										var objDOMParser = new DOMParser();
										dom = objDOMParser.parseFromString(xmlstring, "text/xml")
									} else if (window.ActiveXObject) { // IE
										try {dom = new ActiveXObject("Msxml2.DomDocument"); } catch (e) {
										try {dom = new ActiveXObject("Msxml.DomDocument");} catch (e) {}
									 }
									 	dom.loadXML(xmlstring)
									}
									if (!dom) {
										alert('Cannot create XMLDOM instance');
										return false;
									} else {
										return dom;
									}
								}
								
	/* Create an HttpRequest Object */								
	DK.http = function() {
								// no parameters
								var http_request
								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('Cannot create XMLHTTP instance');
									return false;
								} else {
									return http_request;
								}
						 }
	/* Synchronous/Asynchronous with either POST or GET */						 
	DK.ajax = function(a) {
								// a.post
								// a.url
								// a.sync
								// a.readyfunction
								// a.errorfunction
								// a.xmldom
								var http_request
								try {
									http_request = DK.http()

									if (a.async) {
										http_request.onreadystatechange = function() {
															if (http_request.readyState == 4) {
																http_request.onreadystatechange = function() {}
																a.readyfunction(http_request.responseText, a.xmldom, a.id, a)
															}
										}
									}
									this.method = (a.post) ? "POST" : "GET"		
									http_request.open(this.method,a.url,a.async)	// Synchronous request
									
 									//if (a.post) http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');
									//else 
 										http_request.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=UTF-8');
										//http_request.setRequestHeader("Content-Type", "text/plain;charset=UTF-8");

									http_request.send(a.xmldom)
									if (!a.async) this.text = http_request.responseText
								} catch (e) {
									if (a.errorfunction) a.errorfunction(e, a.url, a.id)
								}
								
							}
	/* POST form fields */							
	DK.form = function(a) {
								// url 					(POST url)
								// input[] 				(array of name/value pairs)
								// encodeUtf8 			(true/false)
								// submit 				(true/false)
								// addinput()			(name,value)
								
								// ... also : target, name, method
								var f = document.createElement("form");
								
								f.style.display = 'none'
								document.body.appendChild(f);
								
								//f.acceptCharset = (a.acceptCharset) ? a.acceptCharset : null;
								//f.enctype 		= (a.enctype) ? a.enctype : null;
								//f.encoding 		= (a.encoding) ? a.encoding : null;
								f.target 		= (a.target) ? a.target : '_self';
								f.name 			= (a.name) ? a.name : 'frm';
								f.action 		= a.url;
								f.method 		= "POST";
								
								for (var inp in a.input) {
									var field = document.createElement('input');
									field.name = inp;
									field.id = inp;
									field.value = (a.encodeUtf8) ? DK.Utf8.encode(a.input[inp]) : a.input[inp]
									f.appendChild(field);
								}		
								this.addinput = function(name,value) {
									var field = document.createElement('input');
									field.name = name;
									field.id = name;
									field.value = (a.encodeUtf8) ? DK.Utf8.encode(value) : value;
									f.appendChild(field);
								}
								this.submit = function() {f.submit()}
								if (a.submit) f.submit();	// submit straight away
						}
	/* Encode or decode a string with UTF8 */
	DK.Utf8 = {
		
			// public method for url encoding
			encode : function (string) {
				string = string.replace(/\r\n/g,"\n");
				var utftext = "";
		
				for (var n = 0; n < string.length; n++) {
		
					var c = string.charCodeAt(n);
		
					if (c < 128) {
						utftext += String.fromCharCode(c);
					}
					else if((c > 127) && (c < 2048)) {
						utftext += String.fromCharCode((c >> 6) | 192);
						utftext += String.fromCharCode((c & 63) | 128);
					}
					else {
						utftext += String.fromCharCode((c >> 12) | 224);
						utftext += String.fromCharCode(((c >> 6) & 63) | 128);
						utftext += String.fromCharCode((c & 63) | 128);
					}
		
				}
		
				return utftext;
			},
		
			// public method for url decoding
			decode : function (utftext) {
				var string = "";
				var i = 0;
				var c = c1 = c2 = 0;
		
				while ( i < utftext.length ) {
		
					c = utftext.charCodeAt(i);
		
					if (c < 128) {
						string += String.fromCharCode(c);
						i++;
					}
					else if((c > 191) && (c < 224)) {
						c2 = utftext.charCodeAt(i+1);
						string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
						i += 2;
					}
					else {
						c2 = utftext.charCodeAt(i+1);
						c3 = utftext.charCodeAt(i+2);
						string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
						i += 3;
					}
		
				}
		
				return string;
			}
		
		}
	

