function json() {
	//first check if the user has added a certain form value
	if(document.getElementById('json').value=='') {
		alert('I dont seem to get any parameters from the json field!');
		return false;
	}else if(document.getElementById('json').value!='') {
		//if everything seems okay, let's add the json data to the object: data
		//which we can later use to send the needed data.
		//alert(document.getElementById('json').value);
		
		data = eval('(' + document.getElementById('json').value + ')');
	}
	return data;
}

function getxmlhttp() {
	//This part of my script has been taken from 'Beginning with Ajax with PHP written by Lee Babin.'
	//---
	//create a boolean to check for a valid Microsoft Active X Instance.
	var xmlhttp = false;
	//check if we are using IE
	try{
		//if the js version is > 5
		xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//if we are using IE
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		} catch (E) {
			//Else we must be using a non-IE Browser (i.e.: FireFox)
			xmlhttp = false;
		}
	}
	
	// If not using IE, create a JS instance for the object:
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
	return xmlhttp;
}
function ajax_send(method, action, object) {
	var output = '';
	xml = getxmlhttp();
	if (method == '') {
		getOrpost = 'post';
	}else{
		getOrpost = method;
	}
	if (getOrpost == "get") {
		
		xml.open("GET", action);
		xml.onreadystatechange = function () {
			if (xml.readyState == 4 && xml.status == 200) {
				if (object != '') {
					if (document.getElementById(object)) {
						document.getElementById(object).innerHTML = xml.responseText;
					}
				}
				//controller(object, xml.responseText);
			}else{
				document.getElementById(object).innerHTML = '<img src="http://www.bannersketch.com/site/files/media/loader.gif">';
			}
			
		}
		xml.send(null);
	} else {
		data = json();
		xml.open("POST", data.action, true);
		xml.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xml.onreadystatechange = function() {
			if (xml.readyState == 4 && xml.status == 200) {
				//return xml.responseText;
			}
		}
		xml.send(form2queryString(data.form));
	}
}

function ajax_proto(method, action, object) {
	new Ajax.Request(action,
	  {
	    method:'get',
	    onSuccess: function(transport){
	      var response = transport.responseText || "no response text";
	      $(object).innerHTML = response;
	    },
	    onFailure: function(){ alert('Something went wrong...') }
	  });
	  
	new Ajax.Updater(object, action, { method: 'get' });
}

function controller(object, data) {
	//alert(data);
}

function form2queryString(form){
	var obj = new Array();
	var ar = new Array();
	form = eval(form);
	for(var i=0;i<form.elements.length;i++){
		try {
			elm = form.elements[i];
			nm = elm.name;
			if(nm != ''){
				switch(elm.type.split('-')[0]){
					case "select":
						for(var s=0;s<elm.options.length;s++){
							if(elm.options[s].selected){
								if(typeof(obj[nm]) == 'undefined') obj[nm] = new Array();
								obj[nm][obj[nm].length] = escape(elm.options[s].value);
							}	
						}
						break;
					
					case "radio":
						if(elm.checked){
							if(typeof(obj[nm]) == 'undefined') obj[nm] = new Array();
							obj[nm][obj[nm].length] = escape(elm.value);
						}	
						break;
					
					case "checkbox":
						if(elm.checked){
							if(typeof(obj[nm]) == 'undefined') obj[nm] = new Array();
							obj[nm][obj[nm].length] = escape(elm.value);
						}	
						break;
					
					default:
						if(typeof(obj[nm]) == 'undefined') obj[nm] = new Array();
						obj[nm][obj[nm].length] = escape(elm.value);
						break;
				}
			}
		}catch(e){}
	}
	for(x in obj) ar[ar.length] = x+'='+obj[x].join(',');
	return ar.join('&');
}
