// JavaScript Document

function getXMLHTTPRequest() {
	var req;
	try {
		req = new XMLHttpRequest();
	} catch(err1) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (err2) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (err3) {
				req = false;
			}
		}
	}
	return req;
}

function foco(id){
	document.getElementById(id).focus();
}

function get_id(id){
	var valor = document.getElementById(id).value;
	return valor;
}

function disabled_boton(id,msj){
	document.getElementById(id).disabled = true;
	document.getElementById(id).value = msj;
}

function enabled_boton(id,txt){
	document.getElementById(id).disabled = false;
	document.getElementById(id).value = txt;
}

ajax = getXMLHTTPRequest();

extArray = new Array(".gif", ".jpg", ".png");
extFila = new Array(".txt", ".csv",".xls",".xlsx");
function verificar(form) {
	imagen = false; fichero = false;
	var file = get_id('img');
	var txt = get_id('fi');
if(get_id('rs').length <= 2 || /^\s+$/.test(get_id('rs'))){
	alert("Escriba su razon social!");
	foco('rs'); return
}
else if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(get_id('correo'))){
	alert("Escriba el correo se su Empresa o Razon social!");
	foco('correo');
	return
}
else if(!file){
	alert("Seleccione un imagen");
	foco('img'); return
}
else if(!txt){
	alert("Seleccione un archivo");
	foco('fi'); return
}
while (file.indexOf("\\") != -1)
	file = file.slice(file.indexOf("\\") + 1);
	ext = file.slice(file.indexOf(".")).toLowerCase();
	for (var i = 0; i < extArray.length; i++)
	{
		if (extArray[i] == ext) { imagen = true; break; }
	}
while (txt.indexOf("\\") != -1)
	txt = txt.slice(txt.indexOf("\\") + 1);
	ex = txt.slice(txt.indexOf(".")).toLowerCase();
	for (var i = 0; i < extFila.length; i++){
		if (extFila[i] == ex) { fichero = true; break; }
	}
	if (imagen && fichero){
		disabled_boton('boton','Subiendo...');
		form.submit();
	}
	else if(!imagen && fichero){
		alert("Solo puede subir imagenes del tipo:  " + extArray.join("  "));
		file = '' ;
		foco('img');
	}
	else if(!fichero && imagen){
		alert("Solo puede subir un fichero del tipo:  " + extFila.join("  "));
		txt = '' ;
		foco('fi');
	}
	else if(!imagen && !fichero){
		alert("Los datos introducidos no son correctos..!");
		document.getElementById('img').value = '' ;
		document.getElementById('fi').value = '' ;
		foco('img');
	}
}

/* Validadion del formulario de registro */
function registrar(form){
	if(get_id('nom').length <= 2 || /^\s+$/.test(get_id('nom'))){
		alert("Escriba su nombre");
		foco('nom'); return false
	}
	else if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(get_id('mail'))){
		alert("Escriba un correo valido");
		foco('mail'); return false
	}
	else if(get_id('rs').length <= 2 || /^\s+$/.test(get_id('rs'))){
		alert("Escriba su razon social");
		foco('rs'); return false
	}
	else if(isNaN(get_id('tel')) || get_id('tel').length <= 7 || /^\s+$/.test(get_id('tel'))){
		alert("Escriba su telefono");
		document.getElementById('tel').value = '';
		foco('tel'); return false
	}
	else if(get_id('pass1').length <= 2 || /^\s+$/.test(get_id('pass1'))){
		alert("Escriba su contraseña");
		foco('pass1'); return false
	}
	else if(get_id('pass2').length <= 2 || /^\s+$/.test(get_id('pass2'))){
		alert("Repita su contraseña");
		foco('pass2'); return false
	}
	else if(get_id('pass1') == get_id('pass2')){
		disabled_boton('registra','Registrarse');
		registro();
	}
	else{
		alert("Las contraseñas no coinciden");
		foco('pass1');
		return false
	}
}

function registro() {
	rand = parseInt(Math.random()*999999999999);
	file = "../procesos/registrar.php?ra="+rand+"&nom="+get_id('nom')+"&mail="+get_id('mail')+"&rs="+get_id('rs')+"&tel="+get_id('tel')+"&pass1="+get_id('pass1')+"&pass2="+get_id('pass2');
	ajax.open("GET",file,true);
	ajax.onreadystatechange = estado;
	ajax.send(null);
}
 
function estado(){ 
	capa = document.getElementById('loading');
	if (ajax.readyState==4){
		if(ajax.status==200){
	   		capa.innerHTML = ajax.responseText;
		}
	}
	else{
		capa.innerHTML = "<img src='../img/loading.gif' /> Procesando...";
	}
}

/* Scrips para envio de comentario */
function no_vacio(form){
	if(!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(get_id('mail'))){
		alert("Escriba una direccion de correo valido");
		foco('mail'); return false
	}
	else if(get_id('txt').length <= 3 || /^\s+$/.test(get_id('txt'))){
		alert("Escriba su mensaje");
		foco('txt'); return false
	}
	else{
		disabled_boton('comentar','Enviar');
		comentar();
	}
}

function comentar(){ 
	mail = get_id('mail');
	txt = get_id('txt');
	rand = parseInt(Math.random()*9999999999999999);
	file = 'procesos/comentario.php?rand='+ rand +'&mail='+ mail +'&msj='+ txt;
	ajax.open("GET",file,true);
	ajax.onreadystatechange = monitoriar;
	ajax.send(null);
}

function monitoriar(){
	capa = document.getElementById('capa');
	if(ajax.readyState == 4)	{
		if(ajax.status == 200){
			foco('mail');
			document.getElementById('com').reset();
			capa.innerHTML = ajax.responseText;
			enabled_boton('comentar','Enviar');
		}
	}
	else{
		capa.innerHTML = "<img src='img/loading.gif' align='absmiddle' />&nbsp;Enviando..!";
	}
}
