// AJAX framework. (c) 2007 Vaidas Balsys

// Function to create AJAX object
function ajaxObject() {
	var myObject;
	try {
		// Firefox, Opera 8.0+, Safari
		myObject = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			myObject = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				myObject = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert("Your browser does not support AJAX!");
				return false;
			}
		}
	}
	return myObject;

}

// Function for retrieving data from remote AJAX responder. Multiple text lines
// (each line ends with \n) are placed to select box - one line per option
// Required parameters:
// 		target - select box object, e.g. document.getElementById('mySelect')
// 		source - url string to AJAX responder, e.g. 'getdata.php?min=1&max=7'
//		prompt - first zero value option in select box, e.g. 'please select'
//				 leave it empty if you do not want this option
function getAjaxSelectData(target, source, prompt) {
	xmlHttp = new ajaxObject();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			sourceData = xmlHttp.responseText;
			sourceData = sourceData.split("\n");
			while (target.length > 0) target.remove(0);
			if (prompt != '') {
				option = document.createElement('option');
				option.text = prompt;
				option.value = 0;
				try {
					target.add(option, null);
				} catch(ex) {
					target.add(option);
				}
			}
			for (i = 0; i < sourceData.length - 1; i++) {
				option = document.createElement('option');
				option.text = sourceData[i];
				option.value = sourceData[i];
				try {
					target.add(option, null);
				} catch(ex) {
					target.add(option);
				}
			}
		}
	}
	xmlHttp.open("POST", source, true);
	xmlHttp.send(null);
}

function getAjaxHtmlData(target, source) {
	xmlHttp = new ajaxObject();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			sourceData = xmlHttp.responseText;
			target.innerHTML = sourceData;
		}
	}
	xmlHttp.open("GET", source,true);
	xmlHttp.send(null);
}

// -------- Carz.ie selector functions ----------

function createRequest(req, selModel){
	req += '&make=' + document.getElementById('csmake').value;
	req += '&model=' + selModel;
	req += '&price1=' + document.getElementById('csprice1').value;
	req += '&price2=' + document.getElementById('csprice2').value;
	req += '&age=' + document.getElementById('csage').value;
	req += '&body=' + document.getElementById('csbody').value;
	req += '&fuel=' + document.getElementById('csfuel').value;
	req += '&seller=' + document.getElementById('csseller').value;
	req += '&area=' + document.getElementById('csarea').value;
	req += (document.getElementById('csisleather').checked) ? '&isleather=1' : '&isleather=0';
	req += (document.getElementById('csisalloys').checked) ? '&isalloys=1' : '&isalloys=0';
	req += (document.getElementById('csiscd').checked) ? '&iscd=1' : '&iscd=0';
	req += (document.getElementById('csisaircon').checked) ? '&isaircon=1' : '&isaircon=0';
	req += (document.getElementById('csisauto').checked) ? '&isauto=1' : '&isauto=0';

//	alert (req);
	return req;
}

function updateSelector(target) {
/*	xmlHttp = new ajaxObject();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState < 4) {
			target.innerHTML = 'Searching';
		}
		if (xmlHttp.readyState == 4) {
			sourceData = xmlHttp.responseText;
			target.innerHTML = sourceData;
		}
	}
	request = createRequest('carz.ie/_ajax.php?task=getqtyonly', document.getElementById('csmodel').value);
	xmlHttp.open("POST", request, true);
	xmlHttp.send(request);
*/

	target.innerHTML = 'Searching';
	xmlHttp = new ajaxObject();
	request = createRequest('carz.ie/_ajax.php?task=getqtyonly', document.getElementById('csmodel').value);
	xmlHttp.open("POST", request, false);
	xmlHttp.send(request);
	sourceData = xmlHttp.responseText;
	target.innerHTML = sourceData;

}

function updateSelectorByMark(models, target, selModel) {
	xmlHttp = new ajaxObject();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState < 4) {
			target.innerHTML = 'Searching';
		}
		if (xmlHttp.readyState == 4) {
			sourceData = xmlHttp.responseText;
			sourceData = sourceData.split("\n");
			while (models.length > 0) models.remove(0);	// removing old model list

			option = document.createElement('option');
			option.text = 'All models';
			option.value = 0;
			try {	// for Firefox, Opera
				models.add(option, null);
			} catch(ex) {	// for IE
				models.add(option);
			}

			for (i = 1; i < sourceData.length - 1; i++) {
				option = document.createElement('option');
				option.text = sourceData[i];
				option.value = sourceData[i];
				option.style.background = '#f1fbdd';
				option.defaultSelected = (sourceData[i] == selModel);
				option.selected = (sourceData[i] == selModel);
				try {	// for Firefox, Opera
					models.add(option, null);
				} catch(ex) {	// for IE
					models.add(option);
				}
			}
			target.innerHTML = sourceData[0];
		}
	}
	request = createRequest('carz.ie/_ajax.php?task=getmodels', selModel);
//	alert(request);
	xmlHttp.open("POST", request, true);
	xmlHttp.send(request);
}

function updateAreas(country, areas, selArea) {
	xmlHttp = new ajaxObject();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			sourceData = xmlHttp.responseText;
			sourceData = sourceData.split("\n");
			while (areas.length > 0) areas.remove(0);	// removing old model list

			for (i = 0; i < sourceData.length - 1; i++) {
				option = document.createElement('option');
				sourceDataElements = sourceData[i].split("|");
				option.text = sourceDataElements[1];
				option.value = sourceDataElements[0];
				option.defaultSelected = (sourceDataElements[0] == selArea);
				option.selected = (sourceDataElements[0] == selArea);
				try {	// for Firefox, Opera
					areas.add(option, null);
				} catch(ex) {	// for IE
					areas.add(option);
				}
			}
		}
	}
	request = 'carz.ie/_ajax.php?task=getareas&ctry=' + country;
//	alert(request);
	xmlHttp.open("POST", request, true);
	xmlHttp.send(request);
}

function ajxGetDealerList(objId, country, area) {
	xmlHttp = new ajaxObject();
	request = 'carz.ie/_ajax.php?task=getdealerlist&ctry=' + country + '&area=' + area;
	xmlHttp.open("POST", request, false);
	xmlHttp.send(request);
	document.getElementById(objId).innerHTML = xmlHttp.responseText;
}

function ajxGetDealerDetails(objId, dealerId) {
	xmlHttp = new ajaxObject();
	request = 'carz.ie/_ajax.php?task=getdealerdetails&id=' + dealerId;
	xmlHttp.open("POST", request, false);
	xmlHttp.send(request);
	document.getElementById(objId).innerHTML = xmlHttp.responseText;
}
