
function CancelOrder(newUrl)
{
   // Check
   var x = window.confirm("¿Esta seguro que desea cancelar esta orden de compra?");
   if (x) { location.href = newUrl; }
}


function IsInteger(valor)
{
     reInteger = new RegExp(/^\d+$/);
 
     // Check
     if (!reInteger.test(valor)) return false;
     else return true;
}


function IsFloat(valor)
{
     reFloat = new RegExp(/^\d+\.?\d*/);
 
     // Check
     if (!reFloat.test(valor)) return false;
     else return true;
}

function validate_client_check(theform)
{
   var email = 1;

   if (theform.email.value.length <= 0) email = 0;
   else {
      reEmail = new RegExp(/^[^@]+\@[^@\.]+\.[^@]+$/);
      if (!reEmail.test(theform.email.value))
      {
         alert("El Email ingresado no presenta un formato valido");
         return false;
      }
   }
   
  
   if (!email) {
     alert("Debe completar el campo 'Email'.");
     return false;
   }
   
  
   return true;
}

function validate_recovery_check(theform)
{
   var email = 1;
   var firstname = 1;
   var lastname = 1;

   if (theform.firstname.value.length <= 0) firstname = 0;
   if (theform.lastname.value.length <= 0) lastname = 0;

   if (!firstname || !lastname) {
      alert("Debe completar su Nombre Completo.");
      return false;
   }

   if (theform.email.value.length <= 0) email = 0;
   else {
      reEmail = new RegExp(/^[^@]+\@[^@\.]+\.[^@]+$/);
      if (!reEmail.test(theform.email.value))
      {
         alert("El Email ingresado no presenta un formato valido");
         return false;
      }
   }   
  
   if (!email) {
     alert("Debe completar el campo 'Email'.");
     return false;
   }
   
  
   return true;
}


function validate_new_client(theform)
{
   var firstname = 1;
   var lastname = 1;
   var password = 1;
   var password_confirm = 1;
   var dni = 1;
   var msn = 1;
   var phone = 1;
   var address = 1;
   var zipcode = 1;
   var id_city = 1;
   var among_streets = 1;
   var company_name = 1;
   var cuit = 1;
   var legal_address = 1;
   var legal_zipcode = 1;
   var legal_location = 1;

   if (theform.firstname.value.length <= 0) firstname = 0;
   if (theform.lastname.value.length <= 0) lastname = 0;
   if (theform.password.value.length <= 0) password = 0;
   if (theform.password_confirm.length <= 0) password_confirm = 0;
   if (theform.dni.value.length <= 0) dni = 0;
   if (theform.msn.value.length <= 0) msn = 0;
   if (theform.phone.value.length <= 0) phone = 0;
   if (theform.address.value.length <= 0) address = 0;
   if (theform.zipcode.value.length <= 0) zipcode = 0;
   if (theform.id_city.value.length <= 0) id_city = 0;
   if (theform.among_streets.value.length <= 0) among_streets = 0;
   if (theform.company_name.value.length <= 0) company_name = 0;
   if (theform.cuit.value.length <= 0) cuit = 0;
   if (theform.legal_address.value.length <= 0) legal_address = 0;
   if (theform.legal_zipcode.value.length <= 0) legal_zipcode = 0;
   if (theform.legal_location.value.length <= 0) legal_location = 0;

   if (!firstname) {
      alert("Debe completar el campo 'Nombre'");
      return false;
   }

   if (!lastname) {
      alert("Debe completar el campo 'Apellido'");
      return false;
   }

   if (!password) {
      alert("Debe completar el campo 'Password'");
      return false;
   }
   else if (theform.password.value != theform.password_confirm.value) {
      alert("El password y la confirmación del mismo no coinciden");
      return false;
   }

   if (dni && !IsInteger(theform.dni.value)) {
      alert("El DNI debe ser un valor numérico");
      return false;
   }

   if (address && (!zipcode || !id_city))
   {
      alert("Si desea ingresar su dirección debe ingresar tambien el Código Postal y la Ciudad");
      return false;
   }

   if (zipcode && (!address || !id_city)) {
      alert("Debe completar el campo Direccion y Ciudad, dado que completo el campo Código Postal");
      return false;
   }

   if (cuit) {
      if (!IsInteger(theform.cuit.value)) {
      alert("El CUIT debe ser un valor numérico (sin guiones)");
      return false;
      }
      else if (theform.cuit.value.length != 11) {
        alert("El CUIT debe poseer 11 digitos numéricos");
        return false;
      }
   }

   if (!cuit && (legal_address || legal_zipcode || legal_location || company_name)) {
      alert("Debe completar todos los campos fiscales.");
      return false;
   }

   if (cuit && (!legal_address || !legal_zipcode || !legal_location || !company_name)) {
      alert("Debe completar todos los campos fiscales para ingresar un CUIT");
      return false;
   }

   return(true);
}

function validate_shipping_address(theform)
{
   var address = 1;
   var zipcode = 1;
   var id_city = 1;

   if (theform.address.value.length <= 0) address = 0;
   if (theform.zipcode.value.length <= 0) zipcode = 0;
   if (theform.id_city.value.length <= 0) id_city = 0;
  
   if (theform.which_address[0].checked) return true;
   else
   {
     if (!address)
     {
        alert("Por favor ingrese una dirección válida para el envío");
        return false;
     }

     if (address && (!zipcode || !id_city))
     {
        alert("Si desea ingresar su dirección debe ingresar tambien el Código Postal y la Ciudad");
        return false;
     }

     if (zipcode && (!address || !id_city)) {
        alert("Debe completar el campo Direccion y Ciudad, dado que completo el campo Código Postal");
        return false;
     }

     return true;
  }
}
