﻿// to set the callback function just use objname.http.onreadystatechange = function() {}
function Ajax(url, method) {
  // the method defaults GET (fastest)
  this.method = method || "GET";
  // the xmlhttprequest 
  if (window.XMLHttpRequest) {
    this.http = new XMLHttpRequest(); // new browsers
  } else if (window.ActiveXObject) {
    this.http = new ActiveXObject("Microsoft.XMLHTTP"); // old ie
  }
  // the url and parameters send to it
  this.url = url;
  this.params;
  
  if (method == "POST")
    this.preparePost();
}


Ajax.prototype.preparePost = function() {
  var pos = this.url.indexOf('?');
  this.params = this.url.substr(pos + 1);
  this.url = this.url.substr(0, pos);
}

Ajax.prototype.send = function() {
  try {
	if(/MSIE (\d+\.\d+);/.test(navigator.userAgent))
		this.http.open(this.method, this.url, false);
	else
		this.http.open(this.method, this.url, true);
		if(this.method=="POST") {
		  this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded;charset=utf-8");
      this.http.setRequestHeader("Content-length", this.params.length);
      this.http.setRequestHeader("Connection", "close");
		}
    this.http.send(this.params);
  }
  catch (e) { }
}

/** function to evaluate json
*  @TODO hadv: check for some validation
*/
function json_eval(str) {
  var obj = eval("(" + str + ")");
  return obj;
}
/** replacing options by json */
function resetOptions(){
  this.options.length=1;
}
// usage:replaceOptions.call(select, json);
function replaceOptions(json) {
  try {
    this.options.length = 1; // the first element is a descriptive one and should be seen as null if required      
    var optionlist = json;
    if (typeof (optionlist) == "string")
      optionlist = json_eval(optionlist);

    for (var i = 0; i < optionlist.length; i++) {
      this.options[i + 1] = new Option(optionlist[i]["description"], optionlist[i]["value"]);
    }
  } catch (e) { }
}
/** function to add events listeners */
function addEvent(obj, evt, fn) {
  if (obj.addEventListener)
    obj.addEventListener(evt, fn, false);
  else if (obj.attachEvent)
    obj.attachEvent('on' + evt, fn);
}
/*
function FileUploader(url, infoElement)
{
if(typeof(infoElement)=="string")
infoElement=document.getElementById(infoElement);

var form = document.createElement("form"); // helperform
form.setAttribute("target", this.id+"_iframe");
form.setAttribute("action", url);
form.setAttribute("method", "post");
form.setAttribute("enctype", "multipart/form-data");
form.style.display="none";
form.style.width="0px";
form.style.height="0px";
var file = this.cloneNode(true);
var uid = document.getElementById("uid").cloneNode(true);
form.appendChild(file);
form.appendChild(uid);
var iframe;// 1 iframe par file
if( !(iframe = document.getElementById(this.id+"_iframe")) ) {
iframe = document.createElement("iframe");
iframe.setAttribute("id", this.id + "_iframe");
iframe.setAttribute("name", this.id + "_iframe");
}
  
addEvent(iframe, "load", function () {infoElement.innerHTML = "uploaded"; });

infoElement.innerHTML = "uploading...";
document.getElementsByTagName("body")[0].appendChild(iframe);
document.getElementsByTagName("body")[0].appendChild(form);
form.submit();
}
*/
function FileUploader(url, infoElement) {
  if (typeof (infoElement) == "string")
    infoElement = document.getElementById(infoElement);

  var iframe; // 1 iframe par file
  if (!(iframe = document.getElementById(this.id + "_iframe"))) {
    iframe = document.createElement("iframe");
    iframe.setAttribute("id", this.id + "_iframe");
    iframe.setAttribute("name", this.id + "_iframe");
  }

  addEvent(iframe, "load", function() { infoElement.innerHTML = "uploaded"; infoElement = null; });

  var form = document.createElement("form"); // helperform
  form.setAttribute("target", this.id + "_iframe");
  form.setAttribute("action", url);
  form.setAttribute("method", "post");
  form.setAttribute("enctype", "multipart/form-data");
  form.style.display = "none";
  form.style.width = "0px";
  form.style.height = "0px";
  var file = this.cloneNode(true);
  var uid = document.getElementById("uid").cloneNode(true);

  form.appendChild(this);
  form.appendChild(uid);
  iframe.document.appendChild(form);
  infoElement.innerHTML = "uploading...";
  document.getElementsByTagName("body")[0].appendChild(iframe);
  document.getElementsByTagName("body")[0].appendChild(form);
  //this.parentNode.replaceChild(file, this);
  form.submit();
}

// helperfunctions for dom
function firstChildElement(element) {
  if (typeof (element) == "string")
    element = document.getElementById(element);
  for (var i = 0; i < element.childNodes.length; i++)
    if (element.childNodes[i].nodeType == 1)
    return element.childNodes[i];
}
function nextSiblingElement() {
  var e = this.nextSibling;
  while (e.nodeType != 1)
    e = e.nextSibling;
  return e;
}
function previousSiblingElement() {
  var e = this.previousSibling;
  while (e.nodeType != 1)
    e = e.previousSibling;
  return e;
}
function removeEmptyNodes(element) {
  for (var i = 0; i < element.childNodes.length; i++)
    if (element.childNodes[i].nodeType == 3)
    element.childNodes[i].parentNode.removeChild(element.childNodes[i]);
}
