exp_email = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,5})+$/;
exp_regional_code = /^[0-9]{3}$/;
exp_cellphone_1 = /^[0-9]{3}$/;
exp_cellphone_2 = /^[0-9]{4}$/;
exp_postal_code = /^[a-zA-Z]\d[a-zA-Z] ?\d[a-zA-Z]\d$/;

function checkTopForm(form){
   jQuery(form.email).removeClass('input-error');
   select = form.city;

   if (form.email.value == 'Entrez votre courriel' || !exp_email.test(form.email.value)){
      alert('Courriel invalide!');
      jQuery(form.email).addClass('input-error');

      return false;
   }else if (select.options[select.selectedIndex].value == ""){
      alert('Veuillez choisir une ville svp!');
      jQuery(select).addClass('input-error');

      return false;
   }else{
      city_id = jQuery(select.options[select.selectedIndex]).attr('cityid');
      form.city_id.value = city_id;

      form.submit();
      return true;
   }

}

function checkLandingForm(form, type){
   jQuery(form.email).removeClass('input-error');

   if (type == 'VENUE'){
      select = form.venue;
      msg_error = 'Veuillez choisir une salle svp!';

      venue_id = jQuery(select.options[select.selectedIndex]).attr('venueid');
      form.venue_id.value = venue_id;

   }else if (type == 'CITY'){
      select = form.city;
      msg_error = 'Veuillez choisir une ville svp!';

      city_id = jQuery(select.options[select.selectedIndex]).attr('cityid');
      form.city_id.value = city_id;
   }

   if (form.email.value == 'Entrez votre courriel' || !exp_email.test(form.email.value)){
      alert('Courriel invalide!');
      jQuery(form.email).addClass('input-error');

      return false;
   }else if (type != 'CATEGORY' && type != 'ARTIST' && select.options[select.selectedIndex].value == ""){
      alert(msg_error);
      jQuery(select).addClass('input-error');

      return false;
   }else{
      form.submit();
      return true;
   }

}


function checkSmsForm(form){
   var b_ok = true;
   var b_cellphone = '';

   regional_code  = form.cellphone_regional_code;
   cellphone_1    = form.cellphone_1;
   cellphone_2    = form.cellphone_2;

   jQuery(regional_code).removeClass('input-error');
   jQuery(cellphone_1).removeClass('input-error');
   jQuery(cellphone_2).removeClass('input-error');

   for(i=0; i < form.b_cellphone.length; i++){
      if (form.b_cellphone[i].checked){
         b_cellphone = form.b_cellphone[i].value;
         break;
      }
   }

   if (b_cellphone == '1'){
      if (!exp_regional_code.test(regional_code.value)){
         jQuery(regional_code).addClass('input-error');
         b_ok = false;
      }

      if (!exp_cellphone_1.test(cellphone_1.value)){
         jQuery(cellphone_1).addClass('input-error');
         b_ok = false;
      }
      if (!exp_cellphone_2.test(cellphone_2.value)){
         jQuery(cellphone_2).addClass('input-error');

         b_ok = false;
      }
   }

   if(b_ok){
      form.submit();
      return true;
   }else{
      alert("Le numéro de cellulaire entré n'est pas valide!")
      return false;
   }

}

function checkPostalForm(form){
   var b_ok = true;

   postal_code  = form.postal_code;
   jQuery(postal_code).removeClass('input-error');

   if (!exp_postal_code.test(postal_code.value)){
      jQuery(postal_code).addClass('input-error');
      b_ok = false;
   }

   if(b_ok){
      form.submit();
      return true;
   }else{
      alert("Le code postal n'est pas valide! (Format: A1B 2C3)")
      return false;
   }
}

function checkPrefForm(form){
   var b_ok = true;

   if(b_ok){
      form.submit();
      return true;
   }else{
      alert("Le numéro de cellulaire entré n'est pas valide!")
      return false;
   }

}

function checkContactForm(form){

   if (!exp_email.test(form.courriel.value)){
      alert('Courriel invalide!');
      jQuery(form.courriel).addClass('input-error');

      return false;
   }else if (form.message.value == ""){
      alert('Veuillez écrire un message svp!');
      jQuery(form.message).addClass('input-error');

      return false;
   }else{
      jQuery.post('ajax/_contact_ajax.php',{action : 'send_message', data : jQuery(form).serialize()},function(data){
      alert("Formulaire envoyé! Merci!")
      },'text');
      return true;
   }

}

function checkContactProducerForm(form, path){

   if (!exp_email.test(form.email.value)){
      alert('Courriel invalide!');
      jQuery(form.email).addClass('input-error');

      return false;
   }else{
      jQuery.post(path+'ajax/_contact_ajax.php',{action : 'save_contact_producer', data : jQuery(form).serialize()},function(data){
         jQuery("#contact_div").hide();
         jQuery("#contact_thank_div").show();
      },'text');
      return true;
   }

}

function checkUnsubscribeForm(form){

   if (!exp_email.test(form.email.value)){
      alert('Courriel invalide!');
      jQuery(form.courriel).addClass('input-error');

      return false;
   }else{
      $("#form").hide();
      $("#loading").show();
      jQuery.post('ajax/_unsubscribe_ajax.php',{action : 'unsubscribe', email : form.email.value},function(data){
         $("#loading").hide();
         $("#success").show();
      },'text');
      return true;
   }

}


function autotab(current,to){
   max_length = jQuery(current).attr("maxlength");
   current_legnth = current.value.length;

   if (current_legnth == max_length) {
      to.focus();
   }
}

function toggleDiv(div_id){
   jQuery("#"+div_id).toggle();
   jQuery("#link_"+div_id).toggle();
}

function changePage(type,select){
   id = select.options[select.selectedIndex].value;

   jQuery.post('ajax/_landing_ajax.php',{action : 'get_url', item_type: type, item_id: id},function(data){
      if (data != ""){
         window.location.href = data;
      }
   },'text');
}
