
// 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);
}

// -------- Dedicated functions ----------

function listSubCategories(target, parent, selectedValue) {
	xmlHttp = new ajaxObject();
	request = baseURL + '/_ajax.php?task=getsubcatlist&parent=' + parent;
	xmlHttp.open("GET", request, false);
	xmlHttp.send(request);

	sourceData = xmlHttp.responseText;
	sourceData = sourceData.split("\n");
	while (target.length > 0) target.remove(0);	// removing old list

	option = document.createElement('option');
	option.text = '* Top *';
	option.value = parent;
	option.style.background = '#ccffcc';
	try {	// for Firefox, Opera
		target.add(option, null);
	} catch(ex) {	// for IE
		target.add(option);
	}

	for (i = 0; i < sourceData.length - 1; i++) {
		option = document.createElement('option');
		sourceDataRow = sourceData[i].split("|");
		option.text = sourceDataRow[1];
		option.value = sourceDataRow[0];
		if (sourceDataRow[0] == selectedValue) {
			option.defaultSelected = true;
			option.selected = true;
		}
		try {	// for Firefox, Opera
			target.add(option, null);
		} catch(ex) {	// for IE
			target.add(option);
		}
	}
}


function ajxLogin(email, password) {
	// Using POST method
	xmlHttp = new ajaxObject();
	request = baseURL + '/_ajax.php?task=login';
	params = 'email=' + email + '&pass=' + password;	// POST params
	xmlHttp.open("POST", request, false);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	switch(xmlHttp.responseText){
		case '0': location.reload(true); break;
		case '1': alert ('Incorrect email and/or password. Try again.'); break;
		case '2': alert ('Account expired. Contact Aviarama for account reactivation.'); break;
		default: alert ('Unexpected error. Contact Aviarama.\n\n' + xmlHttp.responseText); break;
	}
}

function ajxLogout() {
	xmlHttp = new ajaxObject();
	request = baseURL + '/_ajax.php?task=logout';
	xmlHttp.open("GET", request, false);
	xmlHttp.send(request);
	location.reload(true);
}

function ajxChangeSV(name, value, reload) {
	// Using POST method
	xmlHttp = new ajaxObject();
	request = baseURL + '/_ajax.php?task=chsv';	// GET params
	params = 'name=' + name + '&value=' + value;				// POST params
	xmlHttp.open("POST", request, false);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	if (reload) location.reload(true);
}
/*
function ajxDeleteMail(token) {
	// Using POST method
	xmlHttp = new ajaxObject();
	request = baseURL + '/_ajax.php?task=deletemail';	// GET params
	params = 'token=' + token;				// POST params
	xmlHttp.open("POST", request, false);
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	location.reload(true);
}

function ajxGetDialCode(id, container) {
	xmlHttp = new ajaxObject();
	request = baseURL + '/_ajax.php?task=getdialcode&id=' + id;
	xmlHttp.open("GET", request, false);
	xmlHttp.send(request);
	container.innerHTML = xmlHttp.responseText;
}

function ajxSuggestCompany(textObject, idObject) {
	var cursorPos = 0;
	xmlHttp = new ajaxObject();
	xmlHttp.onreadystatechange = function() {
		if (xmlHttp.readyState == 4) {
			response = xmlHttp.responseText.split(':');
			idObject.value = response[0];
			textObject.value = response[1];

			// Selecting text after cursor position
			if (textObject.setSelectionRange) {
		        textObject.setSelectionRange(cursorPos, textObject.value.length);
		    } else if (textObject.createTextRange) {
		        var oRange = textObject.createTextRange();
			//  alert (cursorPos);
				oRange.moveStart("character", cursorPos);
		        oRange.moveEnd("character", textObject.value.length);
		        oRange.select();
		    }

		}
	}
	if (textObject.value != '') {
		cursorPos = textObject.value.length;
		request = baseURL + '/_ajax.php?task=suggestcompany&str=' + textObject.value;
		xmlHttp.open("GET", request, true);
		xmlHttp.send(null);
	}
}
*/
