// holds an instance of XMLHttpRequest
var xmlHttp = createXmlHttpRequestObject();
var xmlOutput = "";

// creates an XMLHttpRequest instance
function createXmlHttpRequestObject() {
  // will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // this should work for all browsers except IE6 and older
  try {
    // try to create XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e) {
    // assume IE6 or older
    var XmlHttpVersions = new Array("MSXML2.XMLHTTP.6.0",
                                    "MSXML2.XMLHTTP.5.0",
                                    "MSXML2.XMLHTTP.4.0",
                                    "MSXML2.XMLHTTP.3.0",
                                    "MSXML2.XMLHTTP",
                                    "Microsoft.XMLHTTP");
    // try every prog id until one works
 
    for (var i=0; i<XmlHttpVersions.length && !xmlHttp; i++) {
      try { 
        // try to create XMLHttpRequest object
        xmlHttp = new ActiveXObject(XmlHttpVersions[i]);
      } 
      catch (e) {}
    }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");
  else 
    return xmlHttp;
}

// Process
function ajax_go(phpscript, output, phpscript2, output2) {
	xmlOutput = output;
	if(xmlHttp) {
		try {
			xmlHttp.open("GET", phpscript, true);
		    xmlHttp.onreadystatechange = handleRequestStateChange;
		    xmlHttp.send(null);
		}
		catch(e) {
			alert("Can't connect to server:\n" + e.toString());
		} 
	}

	if(phpscript2 && output2) {
		link ="ajax_go('"+phpscript2+"', '"+output2+"')";
		setTimeout(link,100);
}

}

// function that handles the HTTP response
function handleRequestStateChange() {
  // obtain a reference to the <div> element on the page
  myDiv = document.getElementById(xmlOutput);
  // display the status of the request 
  if (xmlHttp.readyState == 4) {
    // continue only if HTTP status is "OK"
    if (xmlHttp.status == 200) {
      try {
        // read the message from the server
        response = xmlHttp.responseText;
        // display the message 
       	myDiv.innerHTML = response;
      }
      catch(e) {
        // display error message
        alert("Error reading the response: " + e.toString());
      }
    } 
    else {
      // display status message
      alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
    }
  }
}

// Save resize window
function setresize(win) {
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
    	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
    	myWidth = document.documentElement.clientWidth;
		myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    	//IE 4 compatible
		myWidth = document.body.clientWidth;
    	myHeight = document.body.clientHeight;
  	}
  	myWidth+=8;
  	myHeight+=0;
	str = "konfig/setresize.php?window="+win+"&w="+myWidth+"&h="+myHeight;
	ajax_go(str, "ajaxout");
}

// Move window to new position
function move() {
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=='Netscape') {
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf('Microsoft')!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
		moveTo((screen.width/2)-(winW/2), (screen.height/3)-(winH/2));
	}
}

// Add option to select
function addoption(select, value, text) {
	var opt = window.opener.document.createElement('option');
	opt.text = text;
	opt.value = value;
	opt.selected=true;
	select = select + '_id';
	var sel = window.opener.document.getElementById(select);
	try {
		sel.add(opt, null);
	}
	catch(ex) {
		sel.add(opt);
	}
}

