function getHTTPObject(){
	var xmlhttp;
	// Attempt to initialize xmlhttp object
	try {
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}
    catch (e) {
		// Try to use different activex object
		try {
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch (E) {
			xmlhttp = false;
		}
	}
	// If not initialized, create XMLHttpRequest object
	if (!xmlhttp) {
		if (typeof XMLHttpRequest != 'undefined') {
			xmlhttp = new XMLHttpRequest();
		}
		else {
			alert("This browser does not support AJAX.");
			return null;
		}
	}
	return xmlhttp;
}
 

// Change the value of the outputText field
function setOutput(){
	var combo = document.getElementById('oras');
	combo.options.length = 0;
	if(httpObject.readyState == 4){
		var response = httpObject.responseText;
		var items = response.split(";");
		var count = items.length;
		for (var i=0;i<count;i++) {
			if (items[i] == '') continue;
			var options = items[i].split("-");
			if (options[1] == 'false')
				combo.options[i] =	new Option(options[0], options[0]);
			else
				combo.options[i] =	new Option(options[0], options[0], true, true);
		}
	}
	else 
		combo.options[0] = new Option('asteptati...');
}

// Implement business logic
function doWork(tip){
	httpObject = getHTTPObject();
	if (httpObject != null) {
		if (tip == 'admincont') loc = '/orase/arata_localitati/'+document.getElementById('judet').value;
		else loc = '/orase/arata_localitati/'+document.getElementById('judet').value;
		httpObject.open("GET", loc, true);
		httpObject.onreadystatechange = setOutput;
		httpObject.send(null);
	}
}

function setOutput_msg() {
	if (httpObject.readyState == 4) {
		if (httpObject.responseText == "") {
			document.getElementById('erori').className = 'success';
			document.getElementById('erori').innerHTML = 'Datele sunt valide. Salvez...';
			document.formInregistrare.submit();
		}
		else {
			document.getElementById('erori').className = 'error';
			document.getElementById('erori').innerHTML = httpObject.responseText;
			document.getElementById('erori').style.display = "block";
		}
	}
	else {
		document.getElementById('erori').className = 'success';
		document.getElementById('erori').innerHTML = 'Verific datele introduse...';
	}
			
}
function submit_Inregistrare(tip_loc) {
	httpObject = getHTTPObject();
	if (httpObject != null) {
		if (tip_loc == 'modif') loc = "/index.php/ajax/verificare_modif_furnizor/";
		else loc = "/index.php/inregistrare/verificare_inregistrare/";
		httpObject.open("POST", loc, true);
		httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpObject.setRequestHeader("Connection", "close");
		httpObject.onreadystatechange = setOutput_msg;
		httpObject.send("email=" + encodeURIComponent(document.formInregistrare.email.value) + "&nume=" + encodeURIComponent(document.formInregistrare.nume.value));
	}
}


function show_Login_msg() {
	if (httpObject.readyState == 4) {
		if (httpObject.responseText == "") {
			document.getElementById('erori').className = 'notice';
			document.getElementById('erori').innerHTML = 'Verific datele...';
			document.formLogin.submit();
		}
		else {
			document.getElementById('erori').className = 'error';
			document.getElementById('erori').innerHTML = httpObject.responseText;
		}
	}
	else {
		document.getElementById('erori').className = 'notice';
		document.getElementById('erori').innerHTML = 'Verific datele introduse...';
		document.getElementById('erori').style.display = "block";
	}
			
}

function Login() {
	httpObject = getHTTPObject();
	if (httpObject != null) {
		httpObject.open("POST", "/index.php/login/verificare_login/", true);
		httpObject.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		httpObject.setRequestHeader("Connection", "close");
		httpObject.onreadystatechange = show_Login_msg;
		httpObject.send("email=" + encodeURIComponent(document.formLogin.email.value) + "&pass=" + encodeURIComponent(document.formLogin.password.value));
	}
}
