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

var validacao_email     = 0;
var validacao_user      = 0;
var validacao_microsite = 0;


  function SetEnabled(ComboID, Isenabled)
  {
    // Grab the ComboBox object from ComboID
    //var MyComboObject = $(ComboID).jsObject;

    //MyComboObject.SetEnabled(Isenabled);
    // We return false so that the <a> tag doesnt do anything
    return false;
  }



function doBuscaFreguesias(tuf){
    var c=document.getElementById("freguesia")
    while(c.options.length>0)c.options[0]=null
    c.options[0]=new Option(" -- Aguarde ... -- "," -- Aguarde ... -- ")
    xmlhttp.open("GET", "/portal/sites/_getfreguesias.php?concelho="+tuf+"&time="+Date(),true);
	
    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            var c=document.getElementById("freguesia")
            while(c.options.length>0)c.options[0]=null
			if (xmlhttp.responseText!="") {
            	var aFreguesias=eval((xmlhttp.responseText))
            	for(var i=0;i<aFreguesias.length;i++){
                	aFreguesias[i]=unescape(aFreguesias[i])
                	c.options[c.options.length]=new Option(aFreguesias[i],aFreguesias[i])
            	}
			}
        }
    }
    xmlhttp.send(null)
}

function doBuscaAreas(tuf){
    var c=document.getElementById("sub_area")
    while(c.options.length>0)c.options[0]=null
    c.options[0]=new Option(" -- Aguarde ... -- "," -- Aguarde ... -- ")
    xmlhttp.open("GET", "/portal/sites/_getareas.php?area="+tuf+"&time="+Date(),true);

    xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            var c=document.getElementById("sub_area")
            while(c.options.length>0)c.options[0]=null
			if (xmlhttp.responseText!="") {
            	var aAreas=eval((xmlhttp.responseText))
            	for(var i=0;i<aAreas.length;i++){
                	aAreas[i]=unescape(aAreas[i])
                	c.options[c.options.length]=new Option(aAreas[i],aAreas[i])
            	}
			}
        }
    }
    xmlhttp.send(null)
}

function doBuscaAreasBackoffice(tuf, sub_area_activa){
    var c=document.getElementById("sub_area")
    while(c.options.length>0)c.options[0]=null
    c.options[0]=new Option(" -- Aguarde ... -- "," -- Aguarde ... -- ")
    xmlhttp.open("GET", "/portal/sites/_getareas.php?area="+tuf+"&time="+Date(),true);

	xmlhttp.onreadystatechange=function() {
        if (xmlhttp.readyState==4){
            var c=document.getElementById("sub_area")
            while(c.options.length>0)c.options[0]=null
			if (xmlhttp.responseText!="") {
            	var aAreas=eval((xmlhttp.responseText))
            	for(var i=0;i<aAreas.length;i++){
                	aAreas[i]=unescape(aAreas[i])
                	c.options[c.options.length]=new Option(aAreas[i],aAreas[i])
					if (aAreas[i]==sub_area_activa)
					{
						c.selectedIndex = i;
					}
            	}
			}
        }
    }
    xmlhttp.send(null)
}

function checkdados(fase) {

	validacao_email     = 0;
	validacao_user      = 0;
	validacao_microsite = 0;

	if (fase==1)
	{

		if (document.form_contacto.nome.value.length == 0) {
		  alert('Por favor escreva o Nome completo.');
		  document.form_contacto.nome.focus();
		  return false;
		}

		if (document.form_contacto.concelho.value=="0") {
			alert('Tem que escolher o concelho.');
			document.form_contacto.concelho.focus();
			return false;
		}
		if (document.form_contacto.email.value.length == 0) {
		  alert('Por favor escreva o E-mail.');
		  document.form_contacto.email.focus();
		  return false;
		}

		valida_microsite = 1;
		valida_username  = 1;

		ajax('/portal/sites/_validateemail.php?email='+document.form_contacto.email.value,'ValidaEmail');

	} else if (fase==2)  {

		if (document.form_contacto.url.value.length < 4) {
			alert('Por favor, preencha o URL do site.');
			document.form_contacto.url.focus();
			return false;
		}

		ajax('/portal/directorio/_validateurl.php?url='+document.form_contacto.url.value,'ValidaMicroSite');
	
	} else if (fase==3)  {

		if (document.form_contacto.area.value=="0") {
			alert('Tem que escolher uma área.');
			document.form_contacto.area.focus();
			return false;
		}

		document.form_contacto.submit();

	}

	return false;
}

// funcao geral ajax

function ajax(url,function2execute)
{
    req = null;
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest)
    {
        req = new XMLHttpRequest();
        req.onreadystatechange =  eval(function2execute);
        req.open("GET", url, true);
        req.send(null);

    // branch for IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        isIE = true;
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req)
        {
              //if (funcao != "")
                req.onreadystatechange = eval(function2execute);
            req.open("GET", url, true);
            req.send();
        }
    }
	
}

function ValidaMicroSite(){

        if (req.readyState == 4)
        {
            if (req.status == 200){
                var resposta = req.responseText;
                if (resposta=="ok"){
                    valida_microsite = 1;
					document.form_contacto.submit();
                } else{
                    valida_microsite=0;
                }
            }
        }
}

function ValidaUsername(){

        if (req.readyState == 4)
        {
            if (req.status == 200){
                var resposta = req.responseText;
                if (resposta=="ok"){
                    valida_username = 1;
                    ajax('/portal/sites/_validateemail.php?email='+document.form_contacto.email.value,'ValidaEmail');

                } else{
                    document.form_contacto.username.focus();
                    alert (resposta);
                    valida_username=0;
                }
            }
        }
}


function ValidaEmail(){
	if (req.readyState == 4)
	{
		if (req.status == 200){
			var resposta = req.responseText;
			if (resposta==1){
				valida_email = 1;
				if (valida_email==1){
					document.form_contacto.submit();
				}
			} else{
				document.form_contacto.email.focus();
				alert ("E-mail inválido ou já existe. Certifique-se que inseriu correctamente o seu e-mail.");
				valida_email=0;
			}
		}
	}
}
