// JavaScript Document

//Para confirmar que queremos eliminar
function confirm_box(text, url){
	var conf = confirm(text);
    if (conf == false) { return; }
	else { window.location = url; }
}

function abrirecomendacion (URL){ 
	window.open(URL,"window2","width=300, height=220, menubar=no, scrollbars=no");
} 

function abrirpubli (URL){ 
	window.open(URL,"window2","width=600, height=800, menubar=no, scrollbars=1");//845,1064
} 


function favoritos (url,titulo) {
	external.AddFavorite(url,titulo);
}

//Controla si se han rellenado ciertos campos de los formularios de nuevo usuario
function check_campos_usuario(){
	
	if (!document.registro.txtalias.value){
		alert('No ha rellenado el alias de usuario.');
	  	return false;
	}
	if (!document.registro.txtpass.value){
		alert('No ha rellenado el password.');
	  	return false;
	}
	if (!document.registro.txtpassbis.value){
		alert('No ha rellenado la repetición del password.');
	  	return false;
	}
	if (document.registro.txtpassbis.value!=document.registro.txtpass.value){
	  	alert('No coincide la contraseña con la repetición.');
	  	return false;
	}
	if (document.registro.txtmostrar.checked){
		if (!document.registro.txtnom.value){
			alert('No ha rellenado el nombre de usuario.');
	  		return false;
		}
		if (!document.registro.txtap1.value){
			alert('No ha rellenado el primer apellido.');
	  		return false;
		}
		if (!document.registro.txtap2.value){
			alert('No ha rellenado el segundo apellido.');
	  		return false;
		}
	}
	else{
		return true;
	}
}

//Controla si se han introducido ciertos campos al modificar un usuario
function check_campos_modUsuario(){
	
	if (document.registro.txtmostrar.checked){
		if (!document.registro.txtnom.value){
			alert('No ha rellenado el nombre de usuario.');
	  		return false;
		}
		if (!document.registro.txtap1.value){
			alert('No ha rellenado el primer apellido.');
	  		return false;
		}
		if (!document.registro.txtap2.value){
			alert('No ha rellenado el segundo apellido.');
	  		return false;
		}
	}
	else{
		return true;
	}
}

//Funcion que saca los asteriscos pertinentes cuando se debe mostrar o no el nombre
function muestra_asteriscos(){
	spans = document.getElementsByTagName("span");
	t = spans.length;
	i = 0;
	while(i<t){
		ast = spans[i];
		if (ast.getAttribute('id') == "asteriscos"){
			ast = ast.style;
			if (ast.visibility == "hidden"){
				ast.visibility = "visible";
			}else{
				ast.visibility="hidden"; 
			}	
		}
		i++;
	}
}

//Controla si se han rellenado ciertos campos de los formularios de nueva empresa
function check_campos_empresa(){

	if (!document.registro.txtalias.value){
		alert('No ha rellenado el nombre de usuario.');
	  	return false;
	}
	if (!document.registro.txtpass.value){
		alert('No ha rellenado el campo Contraseña.');
	  	return false;
	}
	if (!document.registro.txtpassbis.value){
		alert('No ha rellenado el campo Contraseña.');
	  	return false;
	}
	if (document.registro.txtpassbis.value!=document.registro.txtpass.value){
	  	alert('No coincide la contraseña con la repetición.');
	  	return false;
	}
	if (!document.registro.txtmail.value){
		alert('No ha rellenado el campo Mail.');
	  	return false;
	}
	if (!document.registro.txtnif.value){
		alert('No ha rellenado el campo CIF/NIF.');
	  	return false;
	}
	if (!document.registro.txtnom.value){
		alert('No ha rellenado el campo Nombre.');
	  	return false;
	}
	if (!document.registro.txttel.value){
		alert('No ha rellenado el campo Telefono.');
	  	return false;
	}
	else{
		return true;
	}
}

//Controla si se han rellenado ciertos campos de los formularios de actualizar empresa
function check_campos_modEmpresa(){

	if (!document.registro.txtmail.value){
		alert('No ha rellenado el campo Mail.');
	  	return false;
	}
	if (!document.registro.txtnif.value){
		alert('No ha rellenado el campo CIF/NIF.');
	  	return false;
	}
	if (!document.registro.txtnom.value){
		alert('No ha rellenado el campo Nombre.');
	  	return false;
	}
	if (!document.registro.txttel.value){
		alert('No ha rellenado el campo Telefono.');
	  	return false;
	}
	else{
		return true;
	}
}


//Controla si se han rellenado ciertos campos de los formularios de nueva oferta
function check_campos_oferta(){

	if (!document.registro.txtcargo.value){
		alert('No ha rellenado el campo Cargo.');
	  	return false;
	}
	else{
		return true;
	}
}

//FUNCIONES PARA LA TIENDA VIRTUAL ----------------------------------------------------------
function redondeo(num_float){
	return ( Math.round(num_float*10000)/10000 );
}

function actualizaTotal(){
	var inputs = document.getElementsByTagName('input');
	var total = document.getElementById('TOTAL');
	var valor_total = parseFloat(0);
	
	var precios = new Array;
	precios[0] = parseFloat("10");
	precios[1] = parseFloat("80");	
	
	total.value = 0;


	var i = 0;
	var j = 0;
	while (i < inputs.length){
		if (inputs[i].getAttribute('id') == 'precio[]'){
			valor_total += parseFloat(inputs[i].value) * parseFloat(precios[j]);
			j++;
		}
		i++;
	}
	total.value = redondeo(valor_total);
}

function esNumero(sText){
   var ValidChars = "0123456789";
   var IsNumber=true;
   var car;

   for (i = 0; i < sText.length && IsNumber == true; i++){ 
      car = sText.charAt(i); 
      if (ValidChars.indexOf(car) == -1){
         IsNumber = false;
      }
   }
   return IsNumber;
}
//-------------------------------------------------------------------------------------------


//Para controlar el formato de fecha y si es o no valida-------------------------------------
function IsNumeric(valor){
	var log=valor.length; var sw="S";
	for (x=0; x<log; x++){ 
		v1=valor.substr(x,1);
		v2 = parseInt(v1);
		//Compruebo si es un valor numérico
		if (isNaN(v2)) { sw= "N";}
	}
	if (sw=="S") {return true;} 
	else {return false; }
}

var primerslap=false;
var segundoslap=false;
function formateafecha(fecha){
	var l=fecha.length;
	var dia;
	var mes;
	var ano;

	if ((l>=2) && (primerslap==false)) { 
		dia=fecha.substr(0,2);
		if ((IsNumeric(dia)==true) && (dia<=31) && (dia!="00")) { 
			fecha=fecha.substr(0,2)+"/"+fecha.substr(3,7); 
			primerslap=true; 
		}
		else { 
			fecha=""; 
			primerslap=false;
		}
	}
	else { 
		dia=fecha.substr(0,1);
		if (IsNumeric(dia)==false) {fecha="";}
		if ((l<=2) && (primerslap=true)) {
			fecha=fecha.substr(0,1); 
			primerslap=false; 
		}
	}
	if ((l>=5) && (segundoslap==false)) { 
		mes=fecha.substr(3,2);
		if ((IsNumeric(mes)==true) &&(mes<=12) && (mes!="00")) { 
			fecha=fecha.substr(0,5)+"/"+fecha.substr(6,4); 
			segundoslap=true; 
		}
		else { 
			fecha=fecha.substr(0,3); 
			segundoslap=false;
		}
	}
	else { 
		if ((l<=5) && (segundoslap=true)) { 
			fecha=fecha.substr(0,4); 
			segundoslap=false; 
		} 
	}
	if (l>=7) { 
		ano=fecha.substr(6,4);
		if (IsNumeric(ano)==false) { fecha=fecha.substr(0,6); }
		else { 
			if (l==10){ 
				if ((ano==0) || (ano<1900) || (ano>2100)) { fecha=fecha.substr(0,6); } 
			} 
		}
	}

	if (l>=10) {
		fecha=fecha.substr(0,10);
		dia=fecha.substr(0,2);
		mes=fecha.substr(3,2);
		ano=fecha.substr(6,4);
		// Año no visiesto y es febrero y el dia es mayor a 28
		if ( (ano%4 != 0) && (mes ==02) && (dia > 28) ) { fecha=fecha.substr(0,2)+"/"; }
	}
	return (fecha);
}
//-----------------------------------------------------------------------------------------------------













