function GetXmlHttpObject(handler)
{
	var objXmlHttp=null
	if (navigator.userAgent.indexOf("Opera")>=0){
		alert("This example doesn't work in Opera")
		return
	}
	if (navigator.userAgent.indexOf("MSIE")>=0){
		var strName="Msxml2.XMLHTTP"
		if (navigator.appVersion.indexOf("MSIE 5.5")>=0){
			strName="Microsoft.XMLHTTP"
		}
		try{
			objXmlHttp=new ActiveXObject(strName)
			objXmlHttp.onreadystatechange=handler
			return objXmlHttp
		}
		catch(e){
			alert("Error. Scripting for ActiveX might be disabled")
			return
		}
	}
	if (navigator.userAgent.indexOf("Mozilla")>=0){
		objXmlHttp=new XMLHttpRequest()
		objXmlHttp.onload=handler
		objXmlHttp.onerror=handler
		return objXmlHttp
	}
}

function DataGrid() {
	this.url = "";
	var showIn = "dataGridContainer";
	var until = "<img src='img/loading.gif' /> Requesting content...";
	var usualColor = "'#e6eef6'";
	var wrongColor = "'#ffffff'";
	function handleHttpResponse () {
		document.getElementById(showIn).innerHTML= until;
		if (xmlHttp.readyState == 4){
			if (xmlHttp.status == 200){
				try{
					document.getElementById(showIn).innerHTML=xmlHttp.responseText;
				} catch(e) {
					alert("Error reading the response: " + e.toString());
				}
			} else {
				alert("There was a problem retrieving the data:\n" + xmlHttp.statusText);
			}
		}
	}
	this.sendData = function  (data,dir) {
		var myRandom=parseInt(Math.random()*99999999);  // cache buster

		xmlHttp=GetXmlHttpObject(handleHttpResponse);
		xmlHttp.open("GET",this.url + data + "&dir=" + dir + "&rand=" + myRandom, true);
		// xmlHttp.setRequestHeader("Content-Type","text/html;charset=");
		xmlHttp.send(null);
	}
	this.urlPramters = function  (str) {
		var urlString = '';
		var fieldsArray = str.split("|");
		for (i in fieldsArray){
			urlString += fieldsArray[i] + "=" + eval("document.getElementById('txt" + fieldsArray[i] + "').value") + "&";
		}
		return urlString;
	}
	this.requiredFilds = function  (str) {
		var status = true;
		if(str.length > 0){
			var fieldsArray = str.split("|");
			for (i in fieldsArray){
				eval("var " + fieldsArray[i] + " = document.getElementById('txt" + fieldsArray[i] + "').value");
				if ( eval("document.getElementById('txt" + fieldsArray[i] + "').getAttribute('type') == 'text'")){
					eval("document.getElementById('txt" + fieldsArray[i] + "').style.backgroundColor=" + wrongColor);
					if ( eval(fieldsArray[i] +".length == 0")){
						eval("document.getElementById('txt" + fieldsArray[i] + "').style.backgroundColor=" + usualColor);
						status = false;
					}
				}
			}
		}
		return status;
	}
}

DataGrid.prototype = {
	set: function (url,column) {
		this.url = url;
		this.listRecords (column,'');
	},
	listRecords: function (column,dir) {
		this.sendData("mode=list&param=" + escape(column) , dir);
	},
	saveRecord: function (param,dir,fields,mandatorys) {
		if(this.requiredFilds(mandatorys)){
			this.sendData(this.urlPramters(fields)+"mode=save&param=" + escape(param) , dir);
		}
	},
	saveNewRecord: function (param,dir,fields,mandatorys) {
		if(this.requiredFilds(mandatorys)){
			this.sendData(this.urlPramters(fields)+"mode=newsave&param=" + escape(param) , dir);
		}
	},
	newRecord: function (param,dir) {
		this.sendData("mode=new&param=" + escape(param) , dir);
	},
	manipulateRecord: function (mode,id,param,dir) {
		if ( confirm("Are you sure you want to "+mode+" record ?") != 1 ){
			return false;
		}
		this.sendData("id="+id+"&mode="+mode+"&param=" + escape(param) , dir);
	}
};

var dg = new DataGrid();