// JavaScript Document

function gAjax() {

	this.createAjaxObj = function() {
		var httprequest=false;
		if (window.XMLHttpRequest){ 
			httprequest=new XMLHttpRequest();
		if (httprequest.overrideMimeType)
			httprequest.overrideMimeType('text/xml');
		}
		else if (window.ActiveXObject){ 
		try {
			httprequest=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e){
			try{
				httprequest=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){}
			}
		}
		return httprequest;
	}

	this.ajaxpack=new Object();
	this.ajaxpack.basedomain="http://"+window.location.hostname;
	this.ajaxpack.ajaxobj=this.createAjaxObj();
	this.ajaxpack.filetype="txt";
	this.ajaxpack.addrandomnumber=0;

	this.getAjaxRequest = function(url, parameters, callbackfunc, filetype){
		this.ajaxpack.ajaxobj=this.createAjaxObj(); 
		var parameters=parameters+"&ajaxcachebust="+new Date().getTime();
		if (this.ajaxpack.ajaxobj){
			this.ajaxpack.filetype=filetype;
			this.ajaxpack.ajaxobj.onreadystatechange=callbackfunc;
			this.ajaxpack.ajaxobj.open('GET', url+"?"+parameters, true);
			this.ajaxpack.ajaxobj.send(null);
		}
	}

	this.postAjaxRequest=function(url, parameters, callbackfunc, filetype){
		this.ajaxpack.ajaxobj=this.createAjaxObj();
		if (this.ajaxpack.ajaxobj){
			this.ajaxpack.filetype=filetype;
			this.ajaxpack.ajaxobj.onreadystatechange = callbackfunc;
			this.ajaxpack.ajaxobj.open('POST', url, true);
			this.ajaxpack.ajaxobj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			this.ajaxpack.ajaxobj.setRequestHeader("Content-length", parameters.length);
			this.ajaxpack.ajaxobj.setRequestHeader("Connection", "close");
			this.ajaxpack.ajaxobj.send(parameters);
		}
	}	
}

function gConvertCR2(input) {
	var output = "";
	for (var i = 0; i < input.length; i++) {
		if ((input.charCodeAt(i) == 10)/* && (input.charCodeAt(i + 1) == 10)*/) {
			i++;
			output += ";";
		} else {
			output += input.charAt(i);
		}
	}
	return output;
}

function gConvertCR(input){
	replaceWith = "";
	input = escape(input)
	for(i=0; i<input.length; i++){
	if(input.indexOf("%0D%0A") > -1){
	input=input.replace("%0D%0A",replaceWith)
	}
	else if(input.indexOf("%0A") > -1){	
	input=input.replace("%0A",replaceWith)
	}
	else if(input.indexOf("%0D") > -1){	
	input=input.replace("%0D",replaceWith)
	}
	}
	input=unescape(input)
	return input;
}

function gSavHTML(comm,ghtml) {
	var url = "gsavhtml.php"
	var method = "post";
	var ObjAjax =  new gAjax();
	var gcmd1 = document.getElementById("gcmd1");
	var gcmd2 = document.getElementById("gcmd2");
	var parameters="comm="+comm+"&ghtml="+encodeURIComponent(gConvertCR(ghtml));

	gcmd1.value = "Saving...";
	gcmd1.disabled = true;
	gcmd2.value = "Saving...";
	gcmd2.disabled = true;

	//alert(parameters);
	
	this.processGetPost = function(){
		var myajax=ObjAjax.ajaxpack.ajaxobj;
		var myfiletype=ObjAjax.ajaxpack.filetype;
		if (myajax.readyState == 4){ 
			if (myajax.status==200 || window.location.href.indexOf("http")==-1){ 
				//document.getElementById("gstats").innerHTML=myajax.responseText;
				alert("Page successfully save!" + myajax.responseText);
				gcmd1.disabled = false;
				gcmd1.value = "Save";
				gcmd2.disabled = false;
				gcmd2.value = "Save";
			}
		}
	}
	
	ObjAjax.postAjaxRequest(url, parameters, this.processGetPost, 'txt');
	return;	
}

/*--------------------------------------*/

$d = document;
$e = function(id) {
	return document.getElementById(id);
}


