// JavaScript Document
/*AJAX*/

var argumento = new Array();
var TheObject = false;

function CreateObject(){
	var args = CreateObject.arguments;
	argumento[0]=args[0];
	argumento[1]=args[1]
	if (window.XMLHttpRequest){
		TheObject = new XMLHttpRequest(); 
	}
	else if (window.ActiveXObject){
		try{
			TheObject = new ActiveXObject("Msxml2.XMLHTTP.4.0");
		}
		catch(e){
			try {
				TheObject = new ActiveXObject("Msxml2.XMLHTTP.3.0");
			}
			catch(e){
				try {
					TheObject = new ActiveXObject("Msxml2.XMLHTTP");
				}
				catch(e){
					try{
						TheObject = new ActiveXObject("Microsoft.XMLHTTP");
					}
					catch(e){
						TheObject = false;
					}
				}
			}
		}
	}
	AttempXMLRequest();
	//0=caminho; 1= nome da combobox ou campo; 
	//2= se value 1 é innerhtml, se 2 é select option mas value<>text se 3 value == text
}

function AttempXMLRequest(){
	if (TheObject){
		if (window.XMLHttpRequest){
			TheObject.open('GET',argumento[0],true);
		}
		else{
			TheObject.open('POST',argumento[0],true);
		}
		switch(argumento[1]){
			case 1:
				TheObject.onreadystatechange = Allocate1;
				break;
			case 2:
				TheObject.onreadystatechange = Allocate2;
				break;
			case 3:
				TheObject.onreadystatechange = dadosDj;
				break;
			case 4:
				TheObject.onreadystatechange = musicas;
				break;				
		}
		TheObject.setRequestHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		TheObject.setRequestHeader("Cache-Control", "post-check=0, pre-check=0");
		TheObject.setRequestHeader("Pragma", "no-cache");
		TheObject.setRequestHeader("Expires", "1");
		TheObject.send(null);
	}
	else{		
		alert("O objeto XML não pode ser criado");
		if (confirm("Deseja tentar novamente?")){
			CreateObject(argumento[0]);
		}
		else{
			return null;
		}
	}
}

function Allocate1(){
	if(TheObject.readyState == 4){	
		if(TheObject.status == 200){
			addMusica(TheObject.responseText);
		}
	}
}

function Allocate2(){
	if(TheObject.readyState == 4){	
		if(TheObject.status == 200){
			remMusica(TheObject.responseText);
		}
	}
}

function dadosDj(){
	if(TheObject.readyState == 4){	
		if(TheObject.status == 200){
			atualizarDj(TheObject.responseText);
		}
	}	
}

function musicas(){
	if(TheObject.readyState == 4){	
		if(TheObject.status == 200){
			atualizarPlaylist(TheObject.responseText);
		}
	}	
}

function TrazUmResultadoAjax(paginaComVariaveis,nomeDoContentReceberInfo,tipo){
	//se tipo=0 - pega innerHTL e soma com o resultado
	//se tipo=1 - zera o innerHTML e preenche com o resultado
	//se tipo=2 - serve para select, zera as informações e preenche com duas informações do resultado value=array[1] e text=array[2] - do allocate3
	//se tipo=3 - semelhante ao tipo2 mas preenchendo somente com o valor array[1] tanto no "text" quanto no "value"
	CreateObject(paginaComVariaveis,nomeDoContentReceberInfo,tipo);
}