// Rutinas JavaScript para ASUNICO website - 2006 
// JavaScript Document

// PRECARGA IMAGENES
function preCarga() { 
if (!document.images) return; 
var ar = new Array(); 
var arguments = preCarga.arguments; 
for (var i = 0; i < arguments.length; i++) { 
	ar[i] = new Image(); 
	ar[i].src = arguments[i]; 
	}
} 


// VALIDACION DEL FORMULARIO DE CONTACTO
function ContactoValidator(elform){
var todok=true;
var msgerror='Los siguientes datos son obligatorios:\n\n';
if (elform.nombre.value==""){msgerror+=' - Su nombre.\n';todok=false;}
if (elform.comentarios.value==""){msgerror+=' - Su mensaje.\n';todok=false;}
if (!todok){
  alert(msgerror);
  return(false)
  } else {
    return(true)
  }
}


// VALIDA EL FORMULARIO DE REGISTRO
function LoginValidator(elform){
	if (elform.login.value==''){
		elform.login.focus(); alert('Introduzca su nombre de usuario'); return(false);}
	if (elform.clave.value==''){
		elform.clave.focus(); alert('Introduzca su clave'); return(false);}
	return(true);
}

// VALIDA FORMULARIO DE CONTACTO
function ArconetValidator(elform){
if (elform.nombre.value.length<=0){ alert('Ha olvidado rellenar el campo NOMBRE.');elform.nombre.focus();return(false); }
if (elform.email.value.length<=0){ 
	if (confirm('Ha olvidado indicarnos su direccion de correo para contactar con Usted, si dispone de una dirección de correo, ¿desea proporcionarla ahora?')){
		elform.email.focus();return(false);
	}
}
elform.botenv.disabled=true;

return(true);
}

function CambiaDisplay(eldiv){
var objeto = document.getElementById(eldiv);
if (objeto!=undefined){
	if (objeto.style.display=='block'){
		objeto.style.display='none'
		} else {
		objeto.style.display='block'
		}
	}
}

function preCarga() { 
if (!document.images) return; 
var ar = new Array(); 
var arguments = preCarga.arguments; 
for (var i = 0; i < arguments.length; i++) { 
ar[i] = new Image(); 
ar[i].src = arguments[i]; 
} 
} 
function EsNumerico (valor) { return !(isNaN(valor)); }

// Activa-Desactiva todos los checkbox ------------------------
function CheckAll(elform){
	len=elform.elements.length;
	valor = elform.marca.checked;
	var i=0;
	for( i=0 ; i<len ; i++){
		if (elform.elements[i].name=='selectos'){elform.elements[i].checked= valor;}
	}
}

// Comprueba todos los checkbox ------------------------
function ValidaChecked(elform){
len=elform.elements.length;
var ninguno = true;
var i=0;
while ((i<len) && (ninguno) ) {
	if ((elform.elements[i].name=='selectos') && (elform.elements[i].checked)) ninguno = false;
	i++;
}
if (ninguno){alert('No ha seleccionado ninguno.');return(false)}
return(true)
}

function ValidarSolicitud(elform){
var todok=true;
var msgerror='Los siguientes datos son obligatorios:\n\n';
if (elform.empresa.value==""){msgerror+=' - Nombre de la empresa.\n';todok=false;}
if (elform.direccion.value==""){msgerror+=' - Direccion de la empresa.\n';todok=false;}
if (elform.localidad.value==""){msgerror+=' - Localidad de la empresa.\n';todok=false;}
if (elform.provincia.value==""){msgerror+=' - Provincia de la empresa.\n';todok=false;}
if (elform.titular.value==''){msgerror+=' - Nombre del titular de la cuenta.\n';todok=false;}
if ((elform.entidad.value=='') || (elform.entidad.value.length<4)){
	msgerror+=' - Codigo de entidad de la cuenta.\n';todok=false;
} else {
	if (!EsNumerico(elform.entidad.value)){
	msgerror+=' - Solo se aceptan caracteres numericos en el codigo de entidad de la cuenta.\n';todok=false;
	}
}
if ((elform.oficina.value=='') || (elform.oficina.value.length<4)){
	msgerror+=' - Codigo de oficina de la cuenta.\n';todok=false;
} else {
	if (!EsNumerico(elform.oficina.value)){
	msgerror+=' - Solo se aceptan caracteres numericos en el codigo de oficina de la cuenta.\n';todok=false;
	}
}
if ((elform.dc.value=='') || (elform.dc.value.length<2)){
	msgerror+=' - Codigo del digito de control de la cuenta.\n';todok=false;
} else {
	if (!EsNumerico(elform.dc.value)){
	msgerror+=' - Solo se aceptan caracteres numericos en el digito de control de la cuenta.\n';todok=false;
	}
}
if ((elform.numcuenta.value=='') || (elform.numcuenta.value.length<10)){
	msgerror+=' - Numero de cuenta.\n';todok=false;
} else {
	if (!EsNumerico(elform.numcuenta.value)){
	msgerror+=' - Solo se aceptan caracteres numericosen el numero de cuenta.\n';todok=false;
	}
}


if (!todok){alert(msgerror);return(false)} else {return(true)}
}

// VALIDACION DEL FORMULARIO DE ALTA DE USUARIOS
function ValidarAltaUsuarios(elform){
var todok=true;
var msgerror='Los siguientes datos son obligatorios:\n\n';
if (elform.empresa.value==""){msgerror+=' - Nombre de la empresa.\n';todok=false;}
if (!todok){alert(msgerror);return(false)} else {return(true)}
}

// Funciones para el chequeo del formato de fecha dd/mm/yyyy ----------------------
function isDigit(theDigit){ 
var digitArray = new Array('0','1','2','3','4','5','6','7','8','9'),j; 
for (j = 0; j < digitArray.length; j++)
		{if (theDigit == digitArray[j]); return true}
return false 
} 
/**********************************************************************/ 
function isPositiveInteger(theString){ 
var theData = new String(theString) 

if (!isDigit(theData.charAt(0))) 
	if (!(theData.charAt(0)== '+')) 
		return false 
		for (var i = 1; i < theData.length; i++) 
			if (!isDigit(theData.charAt(i))) 
			return false 
		return true 
}
/**********************************************************************/ 
function isDate(s,f){
var a1=s.split("/"); var a2=s.split("-"); var e=true; 
if ((a1.length!=3) && (a2.length!=3)){
		e=false;
	} else {
		if (a1.length==3) var na=a1; 
		if (a2.length==3) var na=a2; 
		if (isPositiveInteger(na[0]) && isPositiveInteger(na[1]) && isPositiveInteger(na[2])){
			if (f==1){
				var d=na[1],m=na[0];
			} else {
				var d=na[0],m=na[1];
			} 
		var y=na[2]; 
		if (((e) && (y<1000)||y.length>4)) e=false 
		if (e){ 
			v=new Date(m+"/"+d+"/"+y); 
			if (v.getMonth()!=m-1) 
			e=false;
			}
		} else { 
			e=false; 
		}
	}
return e 
} 
function checkDate(a){ 
var s=a.value; 
	if (isDate(s,0))	//dd/mm/yyyy format 
		//alert("El formato de fecha es valido"); 
		return true;
	else 
		//alert("El formato de fecha NO es valido"); 
		return false; 
}

// COMPARACION ENTRE FECHAS - Devuelve true si fechaIni es mayor que fechaFin
function FechaMayor (fechaIni,fechaFin){ 
	var aux1;
	var aux2;
	var fechalimite;
	var fecha;
	
	aux1 = fechaFin.split("/");
	aux2 = fechaIni.split("/");
	
	fechalimite = aux1[2] + aux1[1] + aux1[0];
	fecha = aux2[2] + aux2[1] + aux2[0];

	return (fechalimite < fecha);
}


// VALIDACION DEL FORMULARIO DE OFERTA DE USUARIOS
function ValidarOferta(elform){
var todok=true;
var msgerror='Los siguientes datos son obligatorios:\n\n';
if (elform.fecha.value==""){msgerror+=' - Fecha de inicio.\n';todok=false;}
	else if (!checkDate(elform.fecha)){msgerror+=' - La fecha de inicio no tiene el formato correcto (dd/mm/aaaa).\n';todok=false;}
if (elform.fecha_fin.value==""){msgerror+=' - Fecha final.\n';todok=false;}
	else {
	if (!checkDate(elform.fecha_fin)){msgerror+=' - La fecha final no tiene el formato correcto (dd/mm/aaaa).\n';todok=false;}
	if (FechaMayor(elform.fecha.value,elform.fecha_fin.value)){msgerror+=' - La fecha de inicio debe ser anterior a la fecha final.\n';todok=false;}
	}
if (elform.titulo.value==""){msgerror+=' - Oferta.\n';todok=false;}
if (!todok){alert(msgerror);return(false)} else {return(true)}
}

//Cambia la fecha dd/mm/yyyy por el formato mm/dd/yyyy <-- necesario para el calendario
function fechaFormatoCal(fecha){
if (fecha != "") {
	var tmp=fecha.split('/');
	fecha=tmp[1]+'/'+tmp[0]+'/'+tmp[2];
	} 
	else {fecha= new Date();}
return(fecha);
}
