var READY_STATE_COMPLETE=4
var peticion_http = null
function inicializa_xhr() {
	if(window.XMLHttpRequest)
		{return new XMLHttpRequest()
	}else if(window.ActiveXObject)
		{return new ActiveXObject("Microsoft.XMLHTTP")
	}
}

function sol_capital(){
	document.form3.solicita_capital.value = "ok";
	buscar('t')
}

function v_opciones1(){
	if(document.getElementById("op_2")){
		var op2 = document.getElementById("op_2");	
		if(op2.checked){op2.checked = false;}
		document.form3.solicita_capital.value = "";
	}
}
function v_opciones2(){
	var op1 = document.getElementById("op_1");	
	if(op1.checked){op1.checked = false;}
	document.form3.solicita_capital.value = "";
}

function buscar(dd){
	if(dd == "t"){
		var solicita_capital = document.form3.solicita_capital.value;	
	}else{
		var solicita_capital = "";	
	}
	notario = document.form1.notario.value
	codigo = document.form1.codigo.value
	if(notario == "0" || codigo == ""){alert("DEBE SELECCIONAR EL NOTARIO E INDICAR EL CODIGO DE VERIFICACION");return 0;}
	
	var opcion = "";
	
	if(solicita_capital != "ok"){
		var op1 = document.getElementById("op_1");
		if(op1.checked){opcion = "normal";}

		if(document.getElementById("op_2")){
			var op2 = document.getElementById("op_2");
			if(op2.checked){opcion = "urgente";}
		}

		if(opcion == ""){
			alert("SELECCIONE LA URGENCIA DE SU PUBLICACION !!");
			return 0;
		}
	}else{
		opcion = "normal";	
	}

	//document.getElementById("sincotizar").style.display = "none";
	var capa = document.getElementById("mensaje")
	capa.innerHTML = '<img border="0" src="img/buscando.gif" width="100" height="26">'
	peticion_http = inicializa_xhr()
	if(peticion_http){
		peticion_http.onreadystatechange = verificar555
		peticion_http.open("POST", "buscar.php", true)
		peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
		var buscar = "opcion="+opcion+"&notario="+notario+"&codigo="+codigo+"&solicita_capital="+solicita_capital+"&nocache=" + Math.random()
		peticion_http.send(buscar)
	}
}
function verificar555(){
	if(peticion_http.readyState == READY_STATE_COMPLETE){
		if(peticion_http.status == 200){
			var capa = document.getElementById("mensaje")
			capa.innerHTML = ""
			var r = peticion_http.responseText
			ops = r.split("/")
			if(ops[0] == "cotizando"){
				capa.innerHTML = "A solicitud del cliente se est&aacute; acreditando el capital social<br> por parte del Diario Oficial para efectos de cotizaci&oacute;n";
				return 0;
			}
			if(ops[0] == "sincotizar"){
				var reg = ops[1];
				if(reg == 13){
					document.getElementById("conservador_stgo").style.display = "block";	
				}
				if(reg == 12){
					document.getElementById("solicita_do").style.display = "block";	
				}
				document.getElementById("opciones_inicio").style.display = "none";
				capa.innerHTML = "Usted debe previamente Acreditar Capital con alguna de las opciones descritas abajo, o bien,<br>solicite la Publicaci&oacute;n Urgente (No requiere acreditaci&oacute;n, Costo 1 UTM)<p>";
				document.getElementById("sincotizar").style.display = "block";
				document.getElementById("verejemplo").style.display = "none";
				return 0
			}
			if(ops[0] == "er"){
				capa.innerHTML = "El documento no cumple con el formato<br>necesario para procesarlo"
				return 0
			}
			if(ops[0] == "pa"){
				capa.innerHTML = ops[1]
				return 0
			}
			if(ops[0] == "pu"){
				capa.innerHTML = "El documento se encuentra en publicaci&oacute;n"
				return 0
			}
			if(ops[0] == "sc"){
				capa.innerHTML = "El documento no ha sido cotizado<br>por favor intentelo mas tarde"
				return 0
			}			
			if(ops[0] == "ok"){
				//alert(ops[1])
				if(document.form3.solicita_capital.value ==  "ok" && ops[5] == "pri"){
					enviarpago();
				}else{
					document.form3.solicita_capital.value = "x"
					valor1 = ops[1]
					valor2 = ops[2]
					var capa2 = document.getElementById("pago")
					capa2.style.display = "block"
					document.getElementById("table100").style.display = "none";
					document.form2.valor.value = "$" + valor1
					//var capa3 = document.getElementById("verpdf")
					//capa3.innerHTML = '<a onclick="verpdf()">Ver documento PDF</a>'
					document.form2.fechapub.value = valor2
					document.form2.fechapub2.value = valor2				
					verpdf(ops[4]);
					document.form3.ccoo.value = document.form1.codigo.value

					var xnotx2 = document.form1.notario.value
					var xnotx = xnotx2.split("-")
				
					document.form3.nntt.value = xnotx[1]
				}
			}
			if(ops[0] != "ok"){
				//alert(ops[0]);
				capa.innerHTML = "ERROR<br>El c&oacute;digo del extracto o el notario es incorrecto (verif&iacute;quelo)<br>o el documento no ha sido enviado por la notar&iacute;a"
				return 0
			}
		}
	}
}
function verpdf(pdf){
	var capa = document.getElementById("verPDF2");
	if(capa.style.display == "block"){
		//
	}else{
		capa.style.display = "block"	
	}
	document.form2.PDF.value = "2";
	//document.getElementById("acepta").checked = true;

	obj = document.getElementById("iframe");
	obj.src="../cs/pdf/"+pdf+".pdf";
	obj.height="500";
}


//valida fechas

   function fechaMayorOIgualQue(fec0, fec1){  
    var bRes = false;  
    var sDia0 = fec0.substr(0, 2);  
    var sMes0 = fec0.substr(3, 2);  
    var sAno0 = fec0.substr(6, 4);  
    var sDia1 = fec1.substr(0, 2);  
    var sMes1 = fec1.substr(3, 2);  
    var sAno1 = fec1.substr(6, 4);  
    if (sAno0 > sAno1) bRes = true;  
    else {  
     if (sAno0 == sAno1){  
      if (sMes0 > sMes1) bRes = true;  
      else {  
       if (sMes0 == sMes1)  
        if (sDia0 >= sDia1) bRes = true;  
      }  
     }  
    }  
    return bRes;  
   }  

//
function enviarpago(){


var asig = document.form2.fechapub.value;		
var asig2 = document.form2.fechapub2.value;	

if(document.form3.solicita_capital.value == "ok"){
	var f = new Date();
	var d = parseInt(f.getDate());
	var m = parseInt(f.getMonth()+1);
	var a = f.getFullYear();
	if(d < 10){d="0" + d.toString();}
	if(m < 10){m="0" + m.toString();}
	
	var t = d+"-"+m+"-"+a;
	document.form2.fechapub.value = t;
	
	document.getElementById("table100").style.display = "none";
	document.form2.valor.value = "$" + "2.300"
	document.getElementById("fecha_pag_sol").innerHTML = "Fecha solicitud";
	document.getElementById("valor_sol").innerHTML = "Valor:";
	document.getElementById("solicitud_capital_msg").style.display = "block";

	document.form3.ccoo.value = document.form1.codigo.value
	var xnotx2 = document.form1.notario.value
	var xnotx = xnotx2.split("-")
	document.form3.nntt.value = xnotx[1]
}else{
	//document.write(f.getDate() + "/" + (f.getMonth() +1) + "/" + f.getFullYear());
	if(document.form2.PDF.value == 1){
		alert("DEBE VER EL DOCUMENTO PDF Y ACEPTAR LAS CONDICIONES ANTES DE CONTINUAR");
		return 0;
	}
	if(fechaMayorOIgualQue(asig, asig2)){
		//alert("fecha ok")					   
	}else{
		alert("LA FECHA DE PUB. NO PUEDE SER INFERIOR A LA ASIGNADA");
		document.form2.fechapub.value = document.form2.fechapub2.value
		return 0;
	}
}

	document.form3.ccoo.value = document.form1.codigo.value;
	document.form3.fechapub1.value = document.form2.fechapub.value
	document.form3.valor1.value = document.form2.valor.value	
	document.getElementById("verPDF2").style.display = "none";
	//document.getElementById("verPDF2").innerHTML = "";	
	document.getElementById("table103").style.display = "block";	
	document.getElementById("pagar1").style.display = "none";	
	document.getElementById("pago").innerHTML = "";	
	//alert(document.form3.valor1.value);
	if(document.form3.valor1.value == "$0"){
		document.getElementById("pago_nopago").style.display = "none";
		document.getElementById("pago_nopago2").style.display = "block";
		document.getElementById("iva_incluido").style.display = "none";
	}else{
		document.getElementById("pago_nopago").style.display = "block";
		document.getElementById("pago_nopago2").style.display = "none";
		var op22 = document.getElementById("op_2");	
		if(op22.checked){
			document.getElementById("advierte_limite").style.display = "block";
		}
	}
					
}
function buscarsocio(){
if(document.form3.pagador.value == "0"){
	document.getElementById('rut').readOnly=false
	document.getElementById('nomrz').readOnly=false
	document.getElementById('paterno').readOnly=false
	document.getElementById('materno').readOnly=false			
	document.getElementById('direccion').readOnly=false	
	document.getElementById('comuna').readOnly=false	
	document.form3.rut.value = ""	
	document.form3.nomrz.value = ""
	document.form3.paterno.value = ""
	document.form3.materno.value = ""			
	document.form3.direccion.value = ""	
	document.form3.comuna.value = 0		
	return 0
}else{
	document.getElementById('rut').readOnly=true
	document.getElementById('nomrz').readOnly=true
	document.getElementById('paterno').readOnly=true
	document.getElementById('materno').readOnly=true			
	document.getElementById('direccion').readOnly=true	
	document.getElementById('comuna').readOnly=true	
}
	socio = document.form3.pagador.value
	var capa = document.getElementById("mensaje2")
	capa.innerHTML = 'Buscando ...'
	peticion_http = inicializa_xhr()
	if(peticion_http){
		peticion_http.onreadystatechange = verificarsocio
		peticion_http.open("POST", "buscardatossocio.php", true)
		peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
		var buscar = "socio="+socio+"&nocache=" + Math.random()
		peticion_http.send(buscar)
	}
}
function verificarsocio(){
	if(peticion_http.readyState == READY_STATE_COMPLETE){
		if(peticion_http.status == 200){
			var capa = document.getElementById("mensaje2")
			capa.innerHTML = ""
			var r = peticion_http.responseText
			if(r == "no"){
				limpiar();
				return 0;
			}
			ops = r.split("/")
				document.form3.rut.value = ops[0]
				document.form3.nomrz.value = ops[1]
				if(ops[2] == "-"){document.form3.paterno.disabled = true; document.form3.paterno.value = ""}else{document.form3.paterno.disabled = false; document.form3.paterno.value = ops[2]}
				if(ops[3] == "-"){document.form3.materno.disabled = true; document.form3.materno.value = ""}else{document.form3.materno.disabled = false; document.form3.materno.value = ops[3]}				
				document.form3.direccion.value = ops[4]				
				if(ops[5] == "-"){document.form3.comuna.value = 0}else{document.form3.comuna.value = ops[5]}				
		}
	}
}
function limpiar(){
	document.form3.rut.value = "";
	document.form3.nomrz.value = "";
	document.form3.paterno.value = "";	
	document.form3.materno.value = "";	
	document.form3.direccion.value = "";	
	document.form3.comuna.value = "";	
}
function habilitar1(){
	//document.form3.direccion.disabled = false
	//document.form3.comuna.disabled = false	
}
function habilitar(){
	//document.form3.direccion.disabled = true
	//document.form3.comuna.disabled = true
}
function enviar(){

	if (document.form3.pagador.value == "0"){
		if(document.form3.rut.value == ""){
			alert("DEBE INGRESAR EL RUT DEL PAGADOR")
			return 0
		}
		if(document.form3.rut.value == ""){
			alert("DEBE INGRESAR EL RUT DEL PAGADOR")
			return 0
		}
		var r = document.form3.rut.value;
		r = r.replace(/\./g, ""); 
		r = r.substring(0,r.length-2);
		if(parseInt(r)<100000){
			alert("RUT NO VALIDO");
			return 0;
		}
			var nombre = document.form3.nomrz.value;
			if(nombre == ""){
				alert("DEBE INDICAR EL NOMBRE/RAZON SOCIAL DEL PAGADOR")
				return 0
			}
			
			if (nombre.indexOf(" ") == -1){
				alert("DEBE INDICAR NOMBRE/RAZON SOCIAL")
				return 0;
			}
			/*
			if(document.form3.paterno.value == ""){
				alert("DEBE INDICAR EL APELLIDO PATERNO DEL PAGADOR")
				return 0
			}
			if(document.form3.materno.value == ""){
				alert("DEBE INDICAR EL APELLIDO MATERNO DEL PAGADOR")
				return 0
			}
			*/
		//}else{
		//	if(document.form3.nomrz.value == ""){
		//		alert("DEBE INDICAR LA RAZON SOCIAL DEL PAGADOR")
		//		return 0
		//	}
		//}
		//CORREO
		var s = document.form3.email.value;
		var filter=/^[A-Za-z][A-Za-z0-9_.\-]*@[A-Za-z0-9-_.]+\.[A-Za-z0-9_.]+[A-za-z]$/;
		if (s.length == 0 ){
			alert("DEBE INDICAR UNA DIRECCION DE CORREO ELECTRONICO")
			return 0
		}
		if (filter.test(s)){
			//
		}else{
			alert("CORREO ELECTRONICO NO VALIDO");
			return 0
		}
	}
		if(document.form3.direccion.value == ""){
			alert("DEBE INGRESAR LA DIRECCION DEL PAGADOR")
			return 0
		}
		if(document.form3.comuna.value == "0"){
			alert("DEBE INGRESAR LA COMUNA DE RESIDENCIA DEL PAGADOR")
			return 0
		}
	document.form3.texcomuna.value = document.form3.comuna.options[document.form3.comuna.selectedIndex].text
	//alert(document.form3.ccoo.value);

	document.form3.submit();	
}
function verejemplo(){
	var capa = document.getElementById("verPDF2");
	if(capa.style.display == "block"){
		//capa.style.display = "none"
	}else{
		capa.style.display = "block"	
	}
	obj = document.getElementById("iframe");
	obj.src="img/ejemplo.png";
	obj.height="570";
}
function fpago(){
	var capa = document.getElementById("verPDF2");
	if(capa.style.display == "block"){
		//capa.style.display = "none"
	}else{
		capa.style.display = "block"	
	}
	obj = document.getElementById("iframe");
	obj.src="img/fpago.htm";
	obj.height="570";
}

function validarut(){

	if(document.form3.pagador.value == "0"){
		if(document.form3.rut.value != ""){
			Rut(document.form3.rut.value)		
		}
		if(document.form3.rut.value != ""){
			busca_cliente(document.form3.rut.value);
		}
	}
} 

//busca datos del cliente
function busca_cliente(rt){
	peticion_http = inicializa_xhr()
	if(peticion_http){
		peticion_http.onreadystatechange = busca_cliente_dos
		peticion_http.open("POST", "busca_cliente.php", true)
		peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
		var buscar = "rt="+rt+"&nocache=" + Math.random()
		peticion_http.send(buscar)
	}	
}
function busca_cliente_dos(){
	if(peticion_http.readyState == READY_STATE_COMPLETE){
		if(peticion_http.status == 200){
			var capa = document.getElementById("mensaje")
			capa.innerHTML = ""
			var r = peticion_http.responseText
			r = r.split("<->");
			if(r[0] == "OK"){
				document.form3.nomrz.value = r[2];
				document.form3.email.value = r[1];
				document.form3.telefono.value = r[3];
				document.form3.direccion.value = r[4];
				document.form3.comuna.value = r[5];
				var est = document.getElementById("estadistica_cl");
				est.innerHTML = "Total de publicaciones: " + r[6] + ", &uacute;ltima publicaci&oacute;n: " + r[7];
				est.style.display = "block";
			}else{
				 document.getElementById("mensaje").innerHTML = "ERROR AL PROCESAR - INTENTELO NUEVAMENTE";
				 return 0;
			}
		}
	}	
}
