function valforcompra(){
validado = true
validado2 = true
var msg1='Faltan por introducir los siquientes datos: \n\n';
var msg='';
var msg2='';
var msg3='';
var msg4='';

//----------FIN DEFINICIÓN MENSAJES DE ERROR-----------------
if (document.forconfirmorden.sol_nombre.value==""){validado = false; msg=msg+'- Nombre Solicitante \n';}
if (document.forconfirmorden.sol_apellido.value==""){validado = false; msg=msg+'- Primer Apellido Solicitante\n';}
if (document.forconfirmorden.sol_apellido2.value==""){validado = false; msg=msg+'- Segundo Apellido Solicitante\n';}
if (document.forconfirmorden.sol_direccion.value==""){validado = false; msg=msg+'- Dirección Solicitante \n';}
if (document.forconfirmorden.sol_poblacion.value==""){validado = false; msg=msg+'- Población Solicitante \n';}
if (document.forconfirmorden.sol_provincia.value==""){validado = false; msg=msg+'- Provincia Solicitante \n';}
if (document.forconfirmorden.sol_tel.value==""){validado = false; msg=msg+'- Teléfono Solicitante \n';}
if (document.forconfirmorden.sol_email.value==""){validado = false; msg=msg+'- Email Solicitante \n';}


///---COMPRUEBO EMAIL CORRECTO
if (document.forconfirmorden.sol_email.value!="")
{
  if (document.forconfirmorden.sol_email.value.length < 5)
  { msg2 = msg2 + 'Por favor, introduzca una dirección de correo válida. \n';
    document.forconfirmorden.sol_email.focus();
    validado = false; }

  if (document.forconfirmorden.sol_email.value.indexOf("@",1) == -1)
  { if (msg2==""){msg2 = msg2 + 'Por favor, introduzca una dirección de correo válida. \n';}
    document.forconfirmorden.sol_email.focus();
    validado = false; }

  if (document.forconfirmorden.sol_email.value.indexOf(".",document.forconfirmorden.sol_email.value.indexOf("@")+1) == -1)
  { if (msg2==""){msg2 = msg2 + 'Por favor, introduzca una dirección de correo válida. \n';}
    document.forconfirmorden.sol_email.focus();
    validado = false; }
  
  if (!isValidEmail(document.forconfirmorden.sol_email.value)) 
  { if (msg2==""){msg2 = msg2 + 'Por favor, introduzca una dirección de correo válida. \n';}
    document.forconfirmorden.sol_email.focus();
    validado = false; } 
} 

if (document.forconfirmorden.dest_nombre.value==""){validado = false; msg=msg+'- Nombre Destinatario \n';}
if (document.forconfirmorden.dest_apellido.value==""){validado = false; msg=msg+'- Primer Apellido Destinatario \n';}
if (document.forconfirmorden.dest_apellido2.value==""){validado = false; msg=msg+'- Segundo Apellido Destinatario \n';}
if (document.forconfirmorden.dest_direccion.value==""){validado = false; msg=msg+'- Dirección Destinatario \n';}
if (document.forconfirmorden.dest_poblacion.value==""){validado = false; msg=msg+'- Población Destinatario \n';}
if (document.forconfirmorden.dest_provincia.value==""){validado = false; msg=msg+'- Provincia Destinatario \n';}
if (document.forconfirmorden.dest_pais.value==""){validado = false; msg=msg+'- País Destinatario \n';}
if (document.forconfirmorden.dest_tel.value==""){validado = false; msg=msg+'- Teléfono Destinatario \n';}
if (document.forconfirmorden.dest_cp.value==""){validado = false; msg=msg+'- CP Destinatario \n';}

if (document.forconfirmorden.dest_dd.value==""){validado = false; msg=msg+'- Día Fecha de Entrega \n';}
if (document.forconfirmorden.dest_mm.value==""){validado = false; msg=msg+'- Mes Fecha de Entrega \n';}
if (document.forconfirmorden.dest_aa.value==""){validado = false; msg=msg+'- Año Fecha de Entrega \n';}
//if (document.forconfirmorden.dest_aa.value.lenght < 4){validado3 = false; msg=msg+'- Año Fecha de Entrega completo \n';}

///---COMPRUEBO fecha
if (document.forconfirmorden.dest_dd.value!="" && document.forconfirmorden.dest_mm.value!="" && document.forconfirmorden.dest_aa.value!="")
{
  //alert('paso data');
  validado2 = valData(document.forconfirmorden.dest_dd.value,document.forconfirmorden.dest_mm.value,document.forconfirmorden.dest_aa.value )
} 

//---------FIN DE COMPROBACIÓN DE ERRORES. SI NO QUEDAN ERRORES SE ENVIA EL FORMULARIO .
if (validado && validado2){
	//alert('paso1');
	document.forconfirmorden.action = 'resumcompra2.php';
	//alert('paso2');
	document.forconfirmorden.onsubmit = '';
	//alert('paso3');
	//document.forconfirmorden.submit();
	//alert('paso4');	
}
else
{
	if (msg!=""){msg4 = msg1 + msg}
	else
	{
	    if (msg2!=""){msg4 = msg2 + msg}
	    else
	    {if (msg3!=""){msg4 = msg3 + msg}}
	}
	
	if (msg4!="")
	{
	alert(msg4);
	}
}

} //end function

function valData(dia, mes, any){   
  if (dia<1 || dia>31 || mes<1 || mes >12) 
    valData=false; 
  else 
    if ((mes==4 || mes==6 || mes==9 || mes==11) && dia > 30) 
      valData=false; 
    else 
      if (mes==2 && dia>28 && !bisiesto(any)) 
	    valData=false; 
      else 
        valData=true; 
 
  if(valData) { 
	  avui=new Date();
	  dataposa = new Date(any, mes-1, dia, 17, 00, 00);
	  //alert(dataposa);
	  //alert(avui);
	  if ( dataposa < avui )
		{ //alert('Por favor, introduzca una fecha de entrega válida.');
		 valData=false;
		}
  }
  if(!valData)
    alert('Por favor, introduzca una fecha de entrega válida.');
  
  if(valData){	
	if (!vmeses(dia, mes, any)){
		alert('Por favor, introduzca una fecha de entrega inferior a 2 meses a partir del día de hoy.');
		valData = false ;
	}
  }
  
  return valData;
} //end valdata


function bisiesto(any) 
{
if ((any % 4 == 0) && (( any % 100 != 0) || (any % 400 ==0)))
  return true;
else
  return false;
}


function vmeses(eldia, elmes, elany){	
	vmeses=true;
	avui=new Date();
	meslimit=avui.getMonth();
	//alert(meslimit);
	// ojo el getMonth da del 0 al 11 el muy cabron
	// para los calculos se debe tener en cuenta por que a la hora
	// de hacer el date tambien hay que darle de 0 a 11 en el mes
	meslimit=meslimit+2;
	anylimit=avui.getFullYear();
	dialimit=avui.getDate();
	//alert(dialimit);
	//alert(meslimit);
	if (meslimit > 11)
	{
		meslimit= meslimit -12;
		anylimit = anylimit +1;
	}
	//meses dia 30 abril,juny,sep,nov
	if ((meslimit==3 || meslimit==5 || meslimit==8 || meslimit==10) && dialimit > 30) 
	{
		dialimit = 1;
		meslimit = meslimit +1;
	}
    if (meslimit==1 && ( dialimit>28 && bisiesto(anylimit)) )
	{
        dialimit = dialimit-(28+bisiesto(anylimit));
		meslimit = 2;
	} 
	datalimit = new Date(anylimit, meslimit,dialimit);
	//alert(datalimit);
 	datadest=new Date(elany, elmes-1, eldia);
	//alert(datadest);
    //alert(datadest-datalimit);
	if ( datadest > datalimit )
	{
	vmeses = false;
	}
return vmeses;
}

function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}
