function $(id) {
	return document.getElementById(id);	
}

function loguear() {
	document.frm_logear.submit();	
}

 function postDataServer(url, vars){
  	var xml = null;
  	try {
  		xml = new ActiveXObject('Microsoft.XMLHTTP');
  	}catch(exception){
  		xml = new XMLHttpRequest();
  	}
  	xml.open('POST',url,false);
  	xml.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
  	xml.send (vars);
  	if(xml.status == 404) alert('Url no valida');
  	return xml.responseText;
  }
  
  function createRequestsObject() {
  	var xml = null;
  	try {
  		xml = new ActiveXObject('Microsoft.XMLHTTP');
  	}catch(exception){
  		xml = new XMLHttpRequest();
  	} 	
  	return xml;
  }
  
  
function popup(URL,w,h,vars) {
	
    var left = (screen.width - w) / 2;
    var top  = (screen.height - h) / 2
	
	window.open(URL,"","left="+left+",top=" + top + ",width="+w+",height="+h+vars);
	
}

/* REGISTRAR */

function guardar_distribuidor() {
	document.frm_registrar.submit();	
}

/* FORMULARIO AJAX */

function enviar_presupuesto() {
	
	//Vemos q todo este bien
	if(isNaN($("id_productos").value)) {
		alert("Primero tiene que seleccionar un toldo");
	}
	else if($("medidas_linea").value < 100) {
		alert("El minimo de la Medida en Linea es de 100 cm");
	}
	else if($("medidas_linea").value > 1000) {
		alert("El maximo de la Medida en Linea es de 1000 cm");
	}
	else if($("medidas_salida").value < 50) {
		alert("El minimo de la Medida en Salida es de 50 cm");
	}
	else if($("medidas_salida").value > 400) {
		alert("El maximo de la Medida en Salida es de 400 cm");
	}
	else if(document.frm_presupuesto.color_aluminio[0].checked == false && document.frm_presupuesto.color_aluminio[1].checked == false  ) {
		alert("Especificar Color");
	}
	else if(document.frm_presupuesto.accionamiento[0].checked == false && document.frm_presupuesto.accionamiento[1].checked == false  ) {
		alert("Especificar Accionamiento");
	}
	else if($("id_localidades").value == "" ) {
		alert("Especificar Localidad");
	}
	else if($("nombre").value == "") {
		alert("Especificar su nombre");
	}
	else if($("apellido").value == "") {
		alert("Especificar su apellido");
	}
	else if($("direccion").value == "") {
		alert("Especificar su direccion");
	}
	else if($("telefono").value == "") {
		alert("Especificar su telefono");
	}
	else if($("email").value == "") {
		alert("Especificar su Email");
	}
	else if(document.frm_presupuesto.medio_comunicacion[0].checked == false && document.frm_presupuesto.medio_comunicacion[1].checked == false  ) {
		alert("Especificar Medio de comunicacion");
	}
	else {	
		document.frm_presupuesto.submit();
	}
	
}

function formulario_medidas_cm2m(cm) {
	if(isNaN(cm)) {
		return 0;
	}
	else {
		return (cm/100);
	}
}

function clean_formulario_observacion() {
	if($("observaciones").value == "Obsevaciones para definir mejor el trabajo a hacer...") {
		$("observaciones").value = "";
	}
}

function get_formulario_localidades(id) {

	xml = createRequestsObject();
	
	xml.onreadystatechange = function()
    {
        switch(xml.readyState)
        {
        case 4: // Done!        	
        	$("formulario_localidades").innerHTML = xml.responseText;
            break;
        default:
            break;
        }
    }

    xml.open ('GET', "ajax.php?act=get_localidades_select&id_zonas="+id, true);
    xml.send (null);	
	
}

function set_formulario_toldo(id) {
		
	$("datostoldo").style.display = "block";
	$("comomido_image").setAttribute("src","images/productos/"+id+"/comomido.jpg");
	
	$("id_productos").value = id;
	
}

function set_productos_tema(id) {
	
	xml = createRequestsObject();
	
	xml.onreadystatechange = function()
    {
        switch(xml.readyState)
        {
        case 0: // Uninitialized
            break;
        case 1: // Loading
        	$("loading").style.display = "block";
        	$("prodAjax").style.display = "none";
            break;
        case 2: // Loaded
       		$("loading").style.display = "none";
       		$("prodAjax").style.display = "block";
            break;
        case 3: // Interactive
            break;
        case 4: // Done!        	
        	$("prodAjax").innerHTML = xml.responseText;
        	$("productos_listado").style.display = "block";
            break;
        default:
            break;
        }
    }

    xml.open ('GET', "ajax.php?act=get_productos&id_productos_temas="+id, true);
    xml.send (null);
}

function votar_distribuidor(id,rank,c) {
	
	xml = createRequestsObject();
	
	xml.onreadystatechange = function()
    {
        switch(xml.readyState)
        {
        case 0: // Uninitialized
            break;
        case 1: // Loading
        	$("loading_"+id).style.display = "block";
        	$("datos_"+id).style.display = "none";
        	$("voto_"+id).style.display = "none";
            break;
        case 2: // Loaded
       		$("loading_"+id).style.display = "none";
       		$("voto_"+id).style.display = "block";
            break;
        case 3: // Interactive
            break;
        case 4: // Done!        	
//        	$("prodAjax").innerHTML = xml.responseText;
//        	$("productos_listado").style.display = "block";

			$("frm_"+id).rank.style.display = 'none';
			$("boton_vote_"+id).style.display = 'none';
			
			if(xml.responseText == 'vote_ok')
			{
				$("voto_"+id).innerHTML = 'Su votacion fue: <b>'+rank+'</b> <br /><br />Gracias por votar';
			}
			else if(xml.responseText == 'ya_voto')
			{
				$("voto_"+id).innerHTML = 'Solo se puede votar una vez.';
			}
			else
			{
				alert("Codigo Incorrecto");	
			}
            break;
        default:
            break;
        }
    }

    xml.open ('GET', "inc/rank.php?rank="+rank+"&id_formularios_enviados="+id+"&act=rank&c="+c, true);
    xml.send (null);
}
