// JavaScript Document
// JavaScript Document

function cpoIn(id, clsInicial, clsNormal, valDefault){
	if (document.getElementById(id).value == valDefault) {
		document.getElementById(id).value = "";
		document.getElementById(id).className = clsNormal;
	}
}

function cpoOut(id, clsInicial, clsNormal, valDefault){
	if (document.getElementById(id).value == "") {
		document.getElementById(id).value = valDefault;
		document.getElementById(id).className = clsInicial;
	}
}


function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' Direccion de correo no valida.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='El campo '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- El campo '+nm+' es requerido.\n'; }
    } if (errors) alert('El formulario contiene los siguientes errores:\n'+errors);
    document.MM_returnValue = (errors == '');
} }


// EJEMPLO DE FORM:
//<form action="contacto.php" method="post" name="form1" id="form1" onsubmit="MM_validateForm('nombre','','R','mail','','RisEmail','comentarios','','R');return document.MM_returnValue"> 
// -- con esa configuracion solo valida nombre lleno, el email y contenido en cpmentarios



function validateEmail(email)
{
    var splitted = email.match("^(.+)@(.+)$");
    if(splitted == null) return false;
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null) return false;
    }
    if(splitted[2] != null)
    {
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null) 
      {
	    var regexp_ip =/^\[\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\]$/;
	    if(splitted[2].match(regexp_ip) == null) return false;
      }// if
      return true;
    }
return false;
}


function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;
 
         return true;
      }



function CompDatos(){
	var Err = 0;
	var MEr = 'EL FORMULARIO CONTIENE LOS SIGUIENTES ERRORES \n \n';
	
//	VALIDACION DE FORMA DE PAGO	
	MEr = MEr + "FORMA DE PAGO: \n-------------------------------------------\n"
	if (document.getElementById('radio1').checked || document.getElementById('radio2').checked || document.getElementById('radio3').checked || document.getElementById('radio4').checked) { } else { Err = Err + 1; MEr = MEr + "• Seleccione una forma de pago \n" }


//	VALIDACION DE DATOS DEL COMPRADOR
	MEr = MEr + "\nDATOS DEL COMPRADOR: \n-------------------------------------------\n"
	var Nom = document.getElementById('nombre').value;
	var Mai = document.getElementById('mail').value;
	var Tel = document.getElementById('telefono').value;
	var Cal = document.getElementById('calleCobro').value;
	var Col = document.getElementById('coloniaCobro').value;
	var Ciu = document.getElementById('ciudadCobro').selectedIndex;
	
//	Comprobacion basica: Nombre + Mail + Telefono
	if (Nom == "" || Nom == "Nombre") { Err = Err + 1; MEr = MEr + "• Su nombre no puede estar vacio \n" }
	if (validateEmail(Mai) == false ) { Err = Err + 1; MEr = MEr + "• La direccion de correo no es valida \n" }	
	if (Tel == "") { Err = Err + 1; MEr = MEr + "• El campo de Telefono es obligatorio \n" }
	if (Tel.length != 10) { Err = Err + 1; MEr = MEr + "• El campo de Telefono debe contener 10 Digitos \n" }
	if (isNaN(Tel)) { Err = Err + 1; MEr = MEr + "• El campo de Telefono solo puede contener Numeros \n" }
	
//	Comprobacion Avanzada: Direccion en caso de seleccion de 4 en pagos
//	if (document.getElementById('radio4').checked) {
		if (Cal == "" || Cal == "Calle y Numero") { Err = Err + 1; MEr = MEr + "• El campo de Calle y Numero es obligatorio \n" }
		if (Col == "" || Col == "Colonia") { Err = Err + 1; MEr = MEr + "• El campo de Colonia es obligatorio \n" }
		if (Ciu == 0)  { Err = Err + 1; MEr = MEr + "• El campo de Ciudad es obligatorio \n" }
//	}
	
	
	
	
	
	
//	COMPROBACION DE EXISTENCIA DE ENVIO
	MEr = MEr + "\nDATOS DE ENVIO: \n-------------------------------------------\n"
//	Solo en caso de que la casilla no este seleccionada.
//	if (document.getElementById('checkbox').checked) { } else {

//	COMPROBACION DE DATOS DE ENVIO
	Nom = document.getElementById('nombreEnvio').value;
	Tel = document.getElementById('telefonoEnvio').value;
	Cal = document.getElementById('calleEnvio').value;
	Col = document.getElementById('coloniaEnvio').value;
	Ciu = document.getElementById('ciudadEnvio').selectedIndex;
	
	if (Nom == "" || Nom == "Nombre") { Err = Err + 1; MEr = MEr + "• Su nombre no puede estar vacio \n" }
	if (Tel == "") { Err = Err + 1; MEr = MEr + "• El campo de Telefono es obligatorio \n" }
	if (Tel.length != 10) { Err = Err + 1; MEr = MEr + "• El campo de Telefono debe contener 10 Digitos \n" }
	if (isNaN(Tel)) { Err = Err + 1; MEr = MEr + "• El campo de Telefono solo puede contener Numeros \n" }
	if (Cal == "" || Cal == "Calle y Numero") { Err = Err + 1; MEr = MEr + "• El campo de Calle y Numero es obligatorio \n" }
	if (Col == "" || Col == "Colonia") { Err = Err + 1; MEr = MEr + "• El campo de Colonia es obligatorio \n" }
	if (Ciu == 0)  { Err = Err + 1; MEr = MEr + "• El campo de Ciudad es obligatorio \n" }
	
//	FECHA Y HORA DE ENTREGA
	var Fe1 = document.getElementById('dia').selectedIndex;
	var Fe2 = document.getElementById('mes').selectedIndex;
	var Fe3 = document.getElementById('ano').selectedIndex;
	var Hr1 = document.getElementById('hora').selectedIndex;
	var Hr2 = document.getElementById('min').selectedIndex;
	var Hr3 = document.getElementById('ampm').selectedIndex;
	var Fec = Fe1 + Fe2 + Fe3;
	var Hrs = Hr1 + Hr2 + Hr3;
	
	if (Fec == 0)  { Err = Err + 1; MEr = MEr + "• Seleccione una Fecha de Entrega valida \n" }
	if (Hrs == 0)  { Err = Err + 1; MEr = MEr + "• Seleccione un Horario valido \n" }
//	}


	if (Err == 0) { 
		return true; 
	} else { 
		alert (MEr); 
		return false; 
	}
	
}