//
// Copyright (c) Just2Easy Limited 2007,2010
//
// All rights reserved.
//
// see - www.j2e.com
//
// For use of ajax.js see www.j2e.com/Geoff/AJAX+and+j2e/
//

var j2eData = "";

function ajaxFunction( script, button, parms ){
	var xmlHttp;
	document.body.style.cursor = "wait";

	try{  // Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e){  // Internet Explorer
		try{
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
        	catch (e){
			try{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}

	xmlHttp.onreadystatechange=function(){
		if(xmlHttp.readyState==4){
			var response = xmlHttp.responseText;
			var a = response.split("@@@");

//alert( response );
			j2eData = a[0];

			for (var i=1; i<a.length; i+=2){
				if (a[i]=="url"){
					window.open(a[2],"_self");
				}
				else if (a[i]=="_alert"){
					alert(a[2]);
				}
				else{
					var o = document.getElementById(a[i]);

					if ( o != undefined ){
						var data = a[i+1];

						if (data=="enable")
							o.disabled=false;
						else if (data=="disable")
							o.disabled=true;
						else if (data=="select")
							o.checked=true;
						else if (data=="deselect")
							o.checked=false;	
						else if (data=="hide")
							hide(a[i]);
						else if (data=="show")	
							show(a[i]);				
						else if (data=="pointer")	
							o.style.cursor = 'pointer';				
						else if (data=="default")	
							o.style.cursor = 'default';				
						else{
							if (o.type=='text' || o.type=='textarea')
								o.value = data;
							else
								o.innerHTML = data;
						}
					}
				}
			}

			document.body.style.cursor = "default";
		}
	}

	var url = "http://www.j2e.com/MySql/"+script+".php";
	
	/* If button is type string we did not have the button to do the action
	 * Therefore construct a special url with the params passed to this function
	 */
	if (button && typeof button === 'string'){
		url += "?button="+button + "&sid="+Math.random();
		
		//Instead of using the button.value use the params if they are defined below
	}
	else {
		if ( button != undefined ){
			url += "?button="+button.id + "&sid="+Math.random();
	
			if (button.type == "checkbox" || button.type == "radio")
				url += "&value="+button.checked;
			else
				url += "&value="+button.value;
		}
		else
			url += "?button=0&value=0&sid="+Math.random();
	}

	if (parms!=undefined)
		url += parms;
	
	url += "&data=" +j2eData;

	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	return false;
}
