

function ShowCiudades(selected){
	
	/*document.getElementById("form_comuna").selectedIndex = 0;
	document.getElementById("form_comuna").disabled = true;*/
	codRegion = document.getElementById("contacto_region").value;
	//document.getElementById("form_ciudad").selexIndex = 0;
	document.getElementById("form_ciudad").disabled = false;
	if(codRegion != ''){

	//selected = 25;
	//document.getElementById("form_region_definitivo").value = codRegion;*/
	datos = 'proceso.php?action=GetCiudades&codregion='+codRegion+'&selected='+selected;
//alert(datos);
	ajax = objetoAjax();
	ajax.open("GET", datos);
	ajax.onreadystatechange = function(){
		if (ajax.readyState == 4 && ajax.status == 200) {
			dato = ajax.responseText;
		    document.getElementById("select_ciudades_ajax").innerHTML = dato;
		}
	}
	ajax.send(null)
	
	}else{
		document.getElementById("form_ciudad").selectedIndex = 0;
		document.getElementById("form_ciudad").disabled = true;
	}
}


function ShowCiudadesAux(region, selected){
	
	if(region != ''){

	datos = 'proceso.php?action=GetCiudades&codregion='+region+'&selected='+selected;
	//alert(datos);
	ajax = objetoAjax();
	ajax.open("GET", datos);
	ajax.onreadystatechange = function(){
		if (ajax.readyState == 4 && ajax.status == 200) {
			dato = ajax.responseText;
		    document.getElementById("select_ciudades_ajax").innerHTML = dato;
		}
	}
	ajax.send(null)
	
	}else{
		document.getElementById("form_ciudad").selectedIndex = 0;
		document.getElementById("form_ciudad").disabled = true;
	}
}

function ShowComunas(selected){
	//alert("prueba");
	//document.getElementById("form_comuna").disabled = false;	
	cod = document.getElementById("form_ciudad").value;
	//alert(cod);
	if(cod != ''){
	datos = 'proceso.php?action=GetComunas&codciudad='+cod+'&selected='+selected;
	//alert(datos);
	ajax = objetoAjax();
	ajax.open("GET", datos);
	ajax.onreadystatechange = function(){
		if (ajax.readyState == 4 && ajax.status == 200) {
			dato = ajax.responseText;
		    document.getElementById("select_comunas_ajax").innerHTML = dato;
		}
	}
	ajax.send(null)
	}else{
		document.getElementById("form_comuna").selectedIndex = 0;
		document.getElementById("form_comuna").disabled = true;		
	}
}

function ShowComunasAux(){
	//alert("prueba");
	//document.getElementById("form_comuna").disabled = false;	
	//cod = document.getElementById("form_ciudad").value;
	//alert(cod);
	datos = 'proceso.php?action=GetComReg';
		
	ajax = objetoAjax();
	ajax.open("GET", datos);
	ajax.onreadystatechange = function(){
		if (ajax.readyState == 4 && ajax.status == 200) {
			dato = ajax.responseText;
			arraydatos = dato.split("&"); 
		    document.getElementById("select_comunas_ajax").innerHTML = arraydatos[0];
		    document.getElementById("select_ciudades_ajax").innerHTML = arraydatos[1];
		}
	}
	ajax.send(null)
	
	
}

function objetoAjax(){
    	var xmlhttp=false;
    	try {
    		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");

    	} catch (e) {
    		try {
    		   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    		} catch (E) {
    			xmlhttp = false;
      		}
    	}
    	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
    		xmlhttp = new XMLHttpRequest();
    	}
    	return xmlhttp;
}

function ValidarEmail(valor){
        if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valor)) {
            return true;
        }
        else {
            return false;
        }
    }

function EnviarForm(){
	
		nombre = document.getElementById("cp_nombre").value;
		rut = document.getElementById("cp_rut").value;
		email = document.getElementById("cp_email").value;
		direccion = document.getElementById("cp_direccion").value;
		
		comuna = document.getElementById("form_comuna").value;
		region = document.getElementById("contacto_region").value;
		ciudad = document.getElementById("form_ciudad").value;
		

		
		como_supo = document.getElementById("form_como_supo").value;
		grupofamiliar = document.getElementById("grupo_familiar").value;
		
		
		telefono_celular = document.getElementById("cp_telefono_celular").value;
		telefono_particular = document.getElementById("cp_telefono_particular").value;
		telefono_comercial = document.getElementById("cp_telefono_comercial").value;
		comentario = document.getElementById("cp_comentario").value;
		
		
		

		
		//Ocultos


		tipo_form = document.getElementById("hd_proyecto_tipo").value;
		
		if(tipo_form == 'postventa'){
			proyecto = document.getElementById("form_proyecto_2").value;
		}else{
			proyecto = document.getElementById("hd_proyecto").value;	
		}
		
		
		unidad = document.getElementById("hd_unidad").value;
		formulario = document.getElementById("hd_formulario").value;
		//proyecto = document.getElementById("hd_proyecto").value;		
		motivo = document.getElementById("hd_motivo").value;
		document.getElementById("val_succces").style.display = 'none';
		document.getElementById("val_error").style.display = 'none';
		
		
		document.getElementById("cp_nombre").style.border = '1px solid #f5bb8b';
		document.getElementById("cp_rut").style.border = '1px solid #f5bb8b';
		document.getElementById("cp_email").style.border = '1px solid #f5bb8b';
		document.getElementById("div-regiones").style.border = '1px solid #f5bb8b';
		document.getElementById("select_ciudades_ajax").style.border = '1px solid #f5bb8b';
		document.getElementById("select_comunas_ajax").style.border = '1px solid #f5bb8b';
		
		
		
		if(nombre == ''){
			document.getElementById("cp_nombre").style.border = '1px solid red';
			document.getElementById("val_error").style.display = 'block';
		}

		if(Valida_Rut(rut) == false || rut == ''){
			document.getElementById("cp_rut").style.border = '1px solid red';
			document.getElementById("val_error").style.display = 'block';
		}

		if(ValidarEmail(email) == false){
			document.getElementById("cp_email").style.border = '1px solid red';
			document.getElementById("val_error").style.display = 'block';
		}

		if(document.getElementById("form_comuna").selectedIndex == 0){
			document.getElementById("select_comunas_ajax").style.border = '1px solid red';
			document.getElementById("val_error").style.display = 'block';
		}

		if(document.getElementById("contacto_region").selectedIndex == 0){
			document.getElementById("div-regiones").style.border = '1px solid red';
			document.getElementById("val_error").style.display = 'block';
		}
		
		if(document.getElementById("form_ciudad").selectedIndex == 0){
			document.getElementById("select_ciudades_ajax").style.border = '1px solid red';
			document.getElementById("val_error").style.display = 'block';
		}
		
		if(nombre == '' || Valida_Rut(rut) == false || ValidarEmail(email) == false || document.getElementById("form_comuna").selectedIndex == 0 || document.getElementById("contacto_region").selectedIndex == 0 || document.getElementById("form_ciudad").selectedIndex == 0){
			return false;
		}


		ProcesarFormulario(nombre, rut, email, direccion, comuna, telefono_celular, telefono_particular, telefono_comercial, comentario, unidad, formulario, motivo, proyecto, region, ciudad, como_supo, grupofamiliar);
		LimpiarForm();
		document.getElementById("val_succces").style.display = 'block';
		
	}



	function LimpiarForm(){
		document.getElementById("cp_nombre").value = '';
		document.getElementById("cp_rut").value = '';
		document.getElementById("cp_email").value = '';
		document.getElementById("cp_direccion").value = '';
		document.getElementById("form_comuna").value = '';
		document.getElementById("cp_telefono_celular").value= '';
		document.getElementById("cp_telefono_particular").value= '';
		document.getElementById("cp_telefono_comercial").value='';
		document.getElementById("cp_comentario").value='';
		document.getElementById("contacto_region").selectedIndex = 1;

		document.getElementById("grupo_familiar").selectedIndex = 0;
		document.getElementById("form_como_supo").selectedIndex = 0;
		
		ShowComunasAux();
		
		//ShowComunasAux(43, 346);
		
		//document.getElementById("btn-clear").onclick();
		//ShowComunasAux(43, 346);
		//ShowCiudadesAux(13, 43);
		//ShowCiudades(43);
		//ShowComunas(155);
		//id="contacto_region"
	}

	

	

function ProcesarFormulario(nombre, rut, email, direccion, comuna, telefono_celular, telefono_particular, telefono_comercial, comentario, unidad, formulario, motivo, proyecto, region, ciudad, como_supo, grupo_familiar){
		datos = 'proceso.php?nombre='+nombre+'&rut='+rut+'&email='+email+'&direccion='+direccion+'&comuna='+comuna+'&telefono_celular='+telefono_celular+'&telefono_particular='+telefono_particular+'&telefono_comercial='+telefono_comercial+'&comentario='+comentario;
		datos += '&unidad='+unidad+'&motivo='+motivo+'&formulario='+formulario+'&proyecto='+proyecto;
		datos += '&region='+region+'&ciudad='+ciudad;
		datos += '&como_supo='+como_supo;
		datos += '&grupo_familiar='+grupo_familiar;
		
		//alert(datos);
		ajax = objetoAjax();
		ajax.open("GET", datos);
		ajax.onreadystatechange = function(){
			if (ajax.readyState == 4 && ajax.status == 200) {
				dato = ajax.responseText;
			}
		}

		ajax.send(null)
}	

	



	

function Valida_Rut(rut){

		var tmpstr = "";
		var intlargo = rut;
		var bol;
		var retorno;

		if (intlargo.length> 0)
		{
			crut = rut
			largo = crut.length;
			if ( largo <2 )
			{
				bol = false;
			}

			for ( i=0; i <crut.length ; i++ )
			if ( crut.charAt(i) != ' ' && crut.charAt(i) != '.' && crut.charAt(i) != '-' )
			{
				tmpstr = tmpstr + crut.charAt(i);
			}
			rut = tmpstr;
			crut=tmpstr;
			largo = crut.length;

			if ( largo> 2 )
				rut = crut.substring(0, largo - 1);
			else
				rut = crut.charAt(0);

			dv = crut.charAt(largo-1);

			if ( rut == null || dv == null )
			return 0;

			var dvr = '0';
			suma = 0;
			mul  = 2;

			for (i= rut.length-1 ; i>= 0; i--)
			{
				suma = suma + rut.charAt(i) * mul;
				if (mul == 7)

					mul = 2;

				else

					mul++;

			}

		

			res = suma % 11;

			if (res==1)

				dvr = 'k';

			else if (res==0)

				dvr = '0';

			else

			{

				dvi = 11-res;

				dvr = dvi + "";

			}





			if(bol == false){

				retorno = false;

				

			}else if ( dvr != dv.toLowerCase() )

			{

				//alert('El Rut Ingreso es Invalido')

				//Objeto.focus()

				retorno = false;

			}else{

				retorno = true;

				}

			//alert('El Rut Ingresado es Correcto!')

			//Objeto.focus()

			return retorno;

		}

	}

	

