// Declarando o AJAX
var Ajax = false;

function AjaxRequest() {
	Ajax = false;
	if (window.XMLHttpRequest) { // Se Mozilla, Safari etc
		Ajax = new XMLHttpRequest();
		if (Ajax.overrideMimeType) {
			Ajax.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) { // Se Internet Explorer
		try {
			Ajax = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				Ajax = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
			}
		}
	}
}

function getState(state) {
	//alert(state);
	
	AjaxRequest();
	if (!Ajax) {
		alert('Não foi possível iniciar o Ajax!');
		return;
	}
	Ajax.onreadystatechange = function() {
		var info = document.getElementById('info');
		
		if (Ajax.readyState == 4) {
			if (Ajax.status == 404) {
				return false;
			}
			if (Ajax.status == 200) {
				var html = Ajax.responseText;
				info.innerHTML = html;
			}
		}
	};
	Ajax.open("GET", "estados/" + state + ".php", true);
	Ajax.send(null);
}

function mostraCidade(){

	var sltSC = document.getElementById('sltCidade');
	if (Ajax.readyState != 4){
		var opt = new Option('Carregando...', '');
		if (document.all){
			sltSC.add(opt);
		} else {
			sltSC.add(opt, null);
		}
	} else if (Ajax.readyState == 4){
		if (Ajax.status == 200){
			var xmldoc = Ajax.responseXML;
			if (xmldoc.hasChildNodes()){
				var nos   = xmldoc.getElementsByTagName('cidade');
				sltSC.options.length = 0;
				var opt = new Option('Selecione a Cidade', '');
					if (document.all){
						sltSC.add(opt);
					} else {
						sltSC.add(opt, null);
					}
				for (var i=0; i < nos.length; i++){
					var n   = nos[i]; 
					var c   = n.childNodes[0].firstChild.nodeValue;
					var d   = n.childNodes[1].firstChild.nodeValue;
					var opt = new Option(d, c);
					if (document.all){
						sltSC.add(opt);
					} else {
						sltSC.add(opt, null);
					}
				}
			}
		} else {
			alert ("Erro no retorno do servidor. Erro: " + ajax.statusText);
		}
	}
}

function buscaCidade(){
	var cat   = document.getElementById('sltEstado').value;
	if (cat > 0){
		AjaxRequest();
		if (!Ajax){
			alert("Não foi possível inicias o AJAX");
			return;
		}
		Ajax.onreadystatechange = mostraCidade;
		Ajax.open("GET", "php/xml/cidades.php?estado=" + cat, true);
		Ajax.send(null);
	} else {
		cat.length = 0;
	}
}

function deleteCompany(node, idCompany){
	
	AjaxRequest();	
	
	if (Ajax != null){
	
		node.parentNode.removeChild(node);
		
		Ajax.onreadystatechange = verifyStatus;
		Ajax.open("GET", "php/xml/delete_company.php?idCompany=" + idCompany, true);
		Ajax.send(null);
		
	}
	
}


function deleteCourse(node, idCourse){
	
	AjaxRequest();	
	
	if (Ajax != null){
	
		node.parentNode.removeChild(node);
		
		Ajax.onreadystatechange = verifyStatus;
		Ajax.open("GET", "php/xml/delete_course.php?idCourse=" + idCourse, true);
		Ajax.send(null);
		
	}
	
}


function deleteLanguage(node, idLanguage){
	
	AjaxRequest();	
	
	if (Ajax != null){
	
		node.parentNode.removeChild(node);
		
		Ajax.onreadystatechange = verifyStatus;
		Ajax.open("GET", "php/xml/delete_language.php?idLanguage=" + idLanguage, true);
		Ajax.send(null);
		
	}
	
}


function loadUniversities(selectedId){
	
	AjaxRequest();	
	
	if (Ajax != null){
		
		Ajax.onreadystatechange = fillUniversities;
		Ajax.open("GET", "php/xml/universities.php?id=" + selectedId, true);
		Ajax.send(null);
		
	}
	
}


function fillUniversities(){
	
	var slt = document.getElementById("sltInstituicao");
	
	if (Ajax.readyState != 4){
		
		slt.options.length = 0;
		
		var opt = new Option('Carregando...', '');
		
		if (document.all){
			slt.add(opt);
		} else {
			slt.add(opt, null);
		}
		
	} else if (Ajax.readyState == 4){
		
		if (Ajax.status == 200){
			
			var xmlDoc = Ajax.responseXML;

			// alert(Ajax.responseText);
			
			if (xmlDoc.hasChildNodes()){

				var uni = xmlDoc.getElementsByTagName("university");

				slt.options.length = 0;

				for (i = 0; i < uni.length; i++){

					var opt = new Option(uni[i].childNodes[1].firstChild.nodeValue, uni[i].childNodes[0].firstChild.nodeValue);
		
					if (uni[i].getAttribute("selected") == "true")
						var selectedCounter = i; 
					
					
					if (document.all){
						//slt.appendChild(opt);
						slt.add(opt);
					} else {
						//slt.appendChild(opt);
						slt.add(opt, null);
					}
				
				}
				
				if (selectedCounter != null)
					slt[selectedCounter].selected = true;
				
			}
		
		}
		
	}
	
}


function verifyStatus(){
	
	if (Ajax.readyState == 4){
		
		if (Ajax.status == 200){
			
			var xmlDoc = Ajax.responseXML;
			
			if (xmlDoc.hasChildNodes()){

				var status = xmlDoc.getElementsByTagName("status");
				
				if (status[0].childNodes[0].firstChild.nodeValue == "OK")
					location.reload();
				else {}
					//alert ("Erro");
				
			} 
			
		}
		
	}
	
}

