//funcao de suporte ao ajax 1
function GetXMLHttp() {
    var xmlHttp;
    try {
        xmlHttp = new XMLHttpRequest();
    }
    catch(ee) {
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch(e) {
                xmlHttp = false;
            }
        }
    }
    return xmlHttp;
}
var xmlRequest = GetXMLHttp();

//funcao de suporte ao ajax 2
function get_id(ID) {
	return document.getElementById(ID);
}

function get_tag(tag) {
	return document.getElementsByTagName(tag);
}

function abre_ajax() {
	var core;
	if (window.XMLHttpRequest) {
		core = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		core= new ActiveXObject("Msxml2.XMLHTTP");
		if (!core) {
			core = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	else {
		alert("Seu Navegador não possui suporte a essa aplicação");
	}
	return core;
}

//funcao pra carregar
function carrega(valor){
    var url = valor;
    xmlRequest.onreadystatechange = muda;
    xmlRequest.open("GET",url,true);
    xmlRequest.send(null);
        if (xmlRequest.readyState == 1) {
            document.getElementById("conteudo").innerHTML = "<br><br><center><img src='js/graphics/loader.big.white.gif'></center><br><br>";
        }
    return url;
}
function muda(){
    if (xmlRequest.readyState == 4){
        document.getElementById("conteudo").innerHTML = xmlRequest.responseText;
    }
}

// ajax form pedido
var next_open_pedido;
var nomePed_send;
var cidadePed_send;
var musicaPed_send;
var recadoPed_send;

function grava_pedido() {
	nomePed_send = get_id('nomePed').value;
	cidadePed_send = get_id('cidadePed').value;
	musicaPed_send = get_id('musicaPed').value;
	recadoPed_send = get_id('recadoPed').value;

	next_open_pedido = abre_ajax();
	if (next_open_pedido) {
		next_open_pedido.onreadystatechange = function() {
			if (next_open_pedido.readyState != 4) {
				get_id("formPed").innerHTML = "<br/><center><img src='js/graphics/loader.big.white.gif'/></center><br>";		
			}
			if (next_open_pedido.readyState == 4) {
				if (next_open_pedido.status==200) {
					resposta = next_open_pedido.responseText;
					if (resposta == 0) {
						get_id("formPed").innerHTML = "Não foi possivel enviar seu email!";
					}
					else {
						get_id("formPed").innerHTML = resposta;
					}
					return false;
				} 
				else {
					alert('Erro ao carregar os dados. Tente novamente.');
				}	
			}
		}
		next_open_pedido.open("POST",'ajax_pedido_gravar.php?ms='+ new Date().getTime(), true);
		next_open_pedido.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		data_post = 'enviar=1';
		data_post += '&nomePed='+nomePed_send;
		data_post += '&cidadePed='+cidadePed_send;
		data_post += '&musicaPed='+musicaPed_send;
		data_post += '&recadoPed='+recadoPed_send;
		next_open_pedido.send(data_post);
	}
	else {
		alert('Seu navegador não tem suporte para essa tecnologia.');
	}
}
