try{
    xmlhttp = new XMLHttpRequest();
}catch(ee){
    try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
    }catch(e){
        try{
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }catch(E){
            xmlhttp = false;
        }
    }
}

function checkMail(mail){
    var er = new RegExp(/^[A-Za-z0-9_\-\.]+@[A-Za-z0-9_\-\.]{2,}\.[A-Za-z0-9]{2,}(\.[A-Za-z0-9])?/);
    if(typeof(mail) == "string"){
        if(er.test(mail)){ return true; }
    }else if(typeof(mail) == "object"){
        if(er.test(mail.value)){
                    return true;
                }
    }else{
        return false;
        }
}

function getDados(objForm) {
		
		var params = new Array();
	
		for (var i=0 ; i < document.getElementById(objForm).elements.length; i++) {
			var parametro = encodeURIComponent(document.getElementById(objForm).elements[i].name);
			parametro += "=";
			parametro += encodeURIComponent(document.getElementById(objForm).elements[i].value);
			params.push(parametro);
		}
	
		return params.join("&");
}
function setDados(objForm){
	for(var i=0; i<document.getElementById(objForm).elements.length-1;i++){
		document.getElementById(objForm).elements[i].value="";
	}
}


var ajax = {
	
		insertNews : function(){
		nome = document.getElementById('nome').value;
		email = document.getElementById('email').value;
		cidade = document.getElementById('cidade').value;
		error = false;		
		
		if(nome == "" || nome == null){
			document.getElementById('nome').style.backgroundColor = '#FFCECF';
			error = true;
		} else {
			document.getElementById('nome').style.backgroundColor = '#E6FFDD';	
		}
		
		if(email == "" || email == null || !checkMail(email)){
			document.getElementById('email').style.backgroundColor = '#FFCECF';
			error = true;
		}  else {
			document.getElementById('email').style.backgroundColor = '#E6FFDD';	
		}
		
		if(cidade == "" || cidade == null){
			document.getElementById('cidade').style.backgroundColor = '#FFCECF';
			error = true;
		} else {
			document.getElementById('cidade').style.backgroundColor = '#E6FFDD';	
		}
		
		if(error == false) {
			document.getElementById('submitB').value = "Enviando...";
			param = "nome="+nome+"&email="+email+"&cidade="+cidade;
			xmlhttp.open("POST", "php/news_insert.php", true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 1) {
					//document.getElementById('formulario').innerHTML = carregandoHTML;
				}
				if(xmlhttp.readyState == 4) {
					if(xmlhttp.status == 200){
						document.getElementById('nome').value = "";
						document.getElementById('email').value = "";
						document.getElementById('cidade').value = "";
						document.getElementById('submitB').value = "Cadastrar";
						document.getElementById('nome').style.backgroundColor = '#ffffff';
						document.getElementById('email').style.backgroundColor = '#ffffff';
						document.getElementById('cidade').style.backgroundColor = '#ffffff';
						document.getElementById('sucess').innerHTML = "Email cadastrado.";
					}
				}
			}
			xmlhttp.send(param);	
		}
		},
		
		
		
		vota : function(){
			id = document.getElementById('hidden').value;
			opcao = document.getElementById('hidden_opcao').value;			
			param = "hidden="+id+"&opcao="+opcao;
			xmlhttp.open("POST", "enquete.php", true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 1) {
					//document.getElementById('formulario').innerHTML = carregandoHTML;
				}
				if(xmlhttp.readyState == 4) {
					if(xmlhttp.status == 200){
						document.getElementById('results').innerHTML = xmlhttp.responseText;
						document.getElementById('buttonV').innerHTML = "";
					}
				}
			}
			xmlhttp.send(param);	
		
		
		},
		
		resultado : function(){
			id = document.getElementById('hidden').value;
			opcao = document.getElementById('hidden_opcao').value;			
			param = "hidden="+id;
			xmlhttp.open("POST", "enquete.php", true);
			xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
			xmlhttp.onreadystatechange = function(){
				if(xmlhttp.readyState == 1) {
					//document.getElementById('formulario').innerHTML = carregandoHTML;
				}
				if(xmlhttp.readyState == 4) {
					if(xmlhttp.status == 200){
						document.getElementById('results').innerHTML = xmlhttp.responseText;
						document.getElementById('buttonV').innerHTML = "";
					}
				}
			}
			xmlhttp.send(param);	
		
		
		}
}
