//ajax.js

//Function to create an XMLHttp Object.
function getxmlhttp (){
	//Create a boolean variable to check for a valid Microsoft active x instance.
	var xmlhttp = false;
	//Check if we are using internet explorer.
	try {
		//If the javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using internet explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			//Else we must be using a non-internet explorer browser.
			xmlhttp = false;
		}
	}
	
	// If not using IE, create a
	// JavaScript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	return xmlhttp;
}

//Function to process an XMLHttpRequest.
function processajax (serverPage, obj, getOrPost, str){
	
	//Get an XMLHttpRequest object for use.
	var xmlhttp = getxmlhttp ();
	var i = 0;
	if (getOrPost == "get"){
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				document.getElementById(obj).innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
	} else {
		xmlhttp.open("POST", serverPage, true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				document.getElementById(obj).innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(str);
	}
}

//Function to populate form data
function ajaxForm(oForm){
	var result="";
	for (var i = 0; i < oForm.elements.length; i++) {
		if (i!=0){
			result += "&";
		}
		if (oForm.elements[i].type == "checkbox"){
			if (oForm.elements[i].checked){
				result += oForm.elements[i].name+"="+oForm.elements[i].value;
			}
		} else if(oForm.elements[i].type == "radio") {
			if (oForm.elements[i].checked){
				result += oForm.elements[i].name+"="+oForm.elements[i].value;
			}
			var tmpradio = oForm.elements[i];
			for (var ir = 0; ir < tmpradio.length; ir++) {
				if (tmpradio[ir].checked){
					result += tmpradio.name+"="+tmpradio[ir].value;
				}
			}
		} else {
			result += oForm.elements[i].name+"="+oForm.elements[i].value;
		}
	}
	return result;
}


function center_box(box, width, height){ 
	has_inner = typeof(window.innerWidth) == 'number';
	has_element = document.documentElement && document.documentElement.clientWidth; 
	cleft = has_inner
		? pageXOffset + 
			(window.innerWidth - width)/2
		: has_element
			? document.documentElement.scrollLeft +
				(document.documentElement.clientWidth - width)/2
			: document.body.scrollLeft +
				(document.body.clientWidth - width)/2;
	ctop = has_inner
		? pageYOffset + (window.innerHeight - height)/2
		: has_element
			? document.documentElement.scrollTop +
				(document.documentElement.clientHeight - height)/2
			: document.body.scrollTop +
				(document.body.clientHeight - height)/2;
	box.style.left = cleft > 0 ? cleft + 'px' : '0px';
	box.style.top = ctop > 0 ? ctop + 'px' : '0px';
}

function timemachine(){
	var currentTime = new Date();
	var day = currentTime.getDate();
	var month = currentTime.getMonth()+1;
	var year = currentTime.getFullYear();
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	var seconds = currentTime.getSeconds();
	var result = "";
	if (day < 10){
		day = "0" + day;
	}
	if (month < 10){
		month = "0" + month;
	}
	if (hours < 10){
		hours = "0" + hours;
	}
	if (minutes < 10){
		minutes = "0" + minutes;
	}
	if (seconds < 10){
		seconds = "0" + seconds;
	}
	document.getElementById('namedate').innerHTML = day+"/"+month+"/"+year+" "+hours+":"+minutes+":"+seconds;
}
