var xmlHttp = false;
initAjax = function() {

	if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				xmlHttp = false;
			}
		}
	}

	if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
		xmlHttp = new XMLHttpRequest();
	}

};

initAjax();

function sendServer(method, serverPage, variables, ID) {
initAjax();
	serverPage = "sayfaYukle.php?p=" + serverPage + variables ;
	
	xmlHttp.open(method, noCache(serverPage), true);
	//xmlHttp.overrideMimeType('text/text; charset=utf-8');
	//xmlHttp.setRequestHeader("If-Modified-Since", "Sat, 1 Jan 2000 00:00:00 GMT"); 
	xmlHttp.onreadystatechange = function() {
	
		if (xmlHttp.readyState == 4) {
				//alert(xmlHttp.getAllResponseHeaders())
			if (xmlHttp.status == 200) {

				container = document.getElementById(ID);
				y = document.getElementById(ID).lastChild;
				if(y){
					document.getElementById(ID).removeChild(y);
				}
					var newdiv = document.createElement("div");
					//alert(xmlHttp.responseText);
					newdiv.innerHTML = parseScript(xmlHttp.responseText);
					container.appendChild(newdiv);
					variables = false;
					xmlHttp = false;
				

			}
		}else{
				container = document.getElementById(ID);
				y = document.getElementById(ID).lastChild;
				if(y){
					document.getElementById(ID).removeChild(y);
				}
					var newdiv = document.createElement("div");
					newdiv.innerHTML = "<p align='center' style='padding-top:400px;padding-left:200px;'><img align='center' src='images/loader2.gif' ></p>";
					container.appendChild(newdiv);
				
		}
	};

	if (method == "POST") {
		if (serverPage == "sayfaYukle?p=") {
			xmlHttp.setRequestHeader("Content-type",
					"multipart/form-data; charset=utf-8");
			xmlHttp.setRequestHeader("Content-length", veriables.length);
			xmlHttp.setRequestHeader("Connection", "close");
		} else
			xmlHttp.setRequestHeader("Content-type",
					"application/x-www-form-urlencoded; charset=utf-8");
					xmlHttp.setRequestHeader("Content-length", veriables.length);
			xmlHttp.setRequestHeader("Connection", "close");
	}
	xmlHttp.send(variables);
};

function parseScript(_source) {
	var source = _source;
	var scripts = new Array();

	// Strip out tags
	while (source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
		var s = source.indexOf("<script");
		var s_e = source.indexOf(">", s);
		var e = source.indexOf("</script", s);
		var e_e = source.indexOf(">", e);

		// Add to scripts array
		scripts.push(source.substring(s_e + 1, e));
		// Strip from source
		source = source.substring(0, s) + source.substring(e_e + 1);
	}

	// Loop through every script collected and eval it
	for ( var i = 0; i < scripts.length; i++) {
		try {
			eval(scripts[i]);
		} catch (ex) {
			// alert(ex);
		}
	}
	// alert(scripts);
	// Return the cleaned source
	return source;
}

function noCache(uri){
	return uri.concat(/\?/.test(uri) ? "&" : "?","noCache=",(new Date).getTime(),".",Math.random()*1234567);
};

