/* Filtros validación */
var filters = {
	/*
	*	requerido
	*		Se encarga de validar si el elemento (el) está relleno o seleccionado
	*/
	requerido: function(el) {
              return ($(el).val() != '' && $(el).val() != -1);
	},
	email: function(el) {
              return /^[A-Za-z][A-Za-z0-9_.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/.test($(el).val());
	},
	creditcard: function(el) {
              return creditCard.check($(el).val(), $("select.cardtype").val());
	},
	cvc: function(el) {
              return /\d+/.test($(el).val());
	}
	
}

/*
 *  utf8_decode()
 *  
 *  Parámetros:
 *    - str_data (String): Texto a convertir en UTF-8
 *
 *  Descripción:
 *    Función que se encarga de pasar cualquier String a UTF-8
 * 
 **/

$.extend({
    utf8_decode: function( str_data ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Webtoolkit.info (http://www.webtoolkit.info/)
    // +      input by: Aman Gupta
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // *     example 1: utf8_decode('Kevin van Zonneveld');
    // *     returns 1: 'Kevin van Zonneveld'
 
    var tmp_arr = [], i = ac = c = c1 = c2 = 0;
 
    while ( i < str_data.length ) {
        c = str_data.charCodeAt(i);
        tmp_arr[ac++] = String.fromCharCode(c); 
        i++;
        
      /* Se comenta esta parte del código porque no está convirtiendo bien algunos carácteres
       * (P.ej: á,ä,è,...)
       * if (c < 128) {
              tmp_arr[ac++] = String.fromCharCode(c); 
              i++;
          } else if ((c > 191) && (c < 224)) {
              c2 = str_data.charCodeAt(i+1);
              tmp_arr[ac++] = String.fromCharCode(((c & 31) << 6) | (c2 & 63));
              i += 2;
          } else {
              c2 = str_data.charCodeAt(i+1);
              c3 = str_data.charCodeAt(i+2);
              tmp_arr[ac++] = String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
              i += 3;
          }
      }*/
    }
    return tmp_arr.join('');
  }
});


/*
 * lang()
 * 
 * Parámetros:
 *  - str (String): Función encargada de escapar una cadena para mostrar por pantalla.
 * 
 * Descripción:
 *  Función que simula a Gettext de PHP y permite un sistema de MultiIdioma en Javascript.  
 * 
 */
$.extend({
    lang: function(str) {
        if (typeof messages == 'undefined') return $.utf8_decode(str);
        return (typeof messages[str] == 'undefined' || messages[str] == '')?$.utf8_decode(str):$.utf8_decode(messages[str]);
    }
});

/*
 *  cookie()
 *  
 *  Parámetros:
 *    - name (String): Nombre de la cookie
 *    - value (String): Valor que se almacena en la cookie
 *    - expires (Number): Número en días que indican la duración de la cookie.
 *    
 *   Descripción:
 *    Gestión de cookies desde jQuery.
 *      $.cookie("cookie");  // Nos devuelve la cookie con nombre "cookie"
 *      $.cookie("cookie", "valor"); // Almacena valor en la cookie "cookie"
 *      $.cookie("cookie", ""); // Borramos la cookie "cookie".
 **/
    
$.extend({
  cookie: function() {
      // getCookie
      var getCookie = function(name) {
          var start = document.cookie.indexOf( name + "=" );
          var len = start + name.length + 1;
          if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
            return null;
          }
          if ( start == -1 ) return null;
          var end = document.cookie.indexOf( ';', len );
          if ( end == -1 ) end = document.cookie.length;
          return unescape( document.cookie.substring( len, end ) );
      }
      
      // setCookie
      var setCookie = function(name, value, expires) {
          var today = new Date();
          today.setTime( today.getTime() );
          if ( expires ) {
              expires = expires * 1000 * 60 * 60 * 24;
          }
          var expires_date = new Date( today.getTime() + (expires) );
          document.cookie = name+'='+escape( value ) +( ( expires ) ? ';expires='+expires_date.toGMTString() : '' );
          return getCookie(name);
      }
      
      //deleteCookie
      var deleteCookie = function( name, path, domain ) {
        if ( getCookie( name ) ) {
            var cookie = getCookie(name);
              document.cookie = name + '=' +
                ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
            return cookie;
        }
      }
      
      if (arguments.length == 1) {
      return getCookie(arguments[0]);
      } else if (arguments.length == 2) {
          if (arguments[1] == '') 
              return deleteCookie(arguments[0]);
          else  {
              return setCookie(arguments[0], arguments[1], arguments[2])
          }           
      }
  }
});

function hotelFactSheet(hotelCodeDetail){
    $("#hotelCodeDetail").val(hotelCodeDetail);
    $("#detail").submit();
}

function ticketFactSheet(ticketCodeDetail,nameTicket){
    $("#ticketCodeDetail").val(ticketCodeDetail);
    $("#nameTicket").val(nameTicket);    
    $("#detail").submit();
}

$(document).ready(function(){
	if ($('.pngfix').length > 0 ) $('.pngfix').pngfix();

        if ($('.bgiframe').length > 0 ) $('.bgiframe').bgiframe();
        
        $('#yourBooking').click(function(){
            if($('.content_top_identification')){
              $('.content_top_identification').slideToggle(500);
              $('.modify_error').hide();
            }
            if('.content_top_identification_small'){
              $('.content_top_identification_small').slideToggle(500);
              $('.modify_error').hide();
            }
        });
        
        $('form#search_booking').bind("submit", function() {
            $('form#search_booking #fDestino').val('your_booking.jsp');                
            if($('#email_id').val() == ''){
              $('.modify_error').slideDown(500);
              return false;
            } 
            if($('#booking_id').val() == ''){
              $('.modify_error').slideDown(500);
              return false;
            }             
            if(!filters.email($('#email_id'))){
              $('.modify_email_error').slideDown(500);
              return false;
            }                  
        });
        
      /*
 *   validable
 *   
 *   Descripción:
 *      Función que valida los elementos que encontremos dentro del formulario
 *      con class "validable". A cada elemento se le aplican los filtros 
 *      relacionados con la class que contengan.
 *
  $("form.validable").bind("submit", function(e){
  	    $.loading($(this).find("input:submit"));
	    if (typeof filters == 'undefined') return;
            var e = e || window.event;
            var msg = '';
	    $(this).find("input, select, textarea, button").each(function(x,el){ 
	        if ($(el).attr("className") != 'undefined') {  
	        $.each(new String($(el).attr("className")).split(" "), function(x, klass){
	            if ($.isFunction(filters[klass]))
	                if (!filters[klass](el)) {
	                	$(el).addClass("error");
                                if (message && message[klass]) {
                                    msg += message[klass].replace("%%", ($(el).attr("title"))?$(el).attr("title"):$(el).attr("name"))
                                } else {
                                  msg += "%% es un elemento " + klass + ".".replace("%%", ($(el).attr("title"))?$(el).attr("title"):$(el).attr("name"))
                                }
                                msg = msg.replace("$$", $(el).val());
                                msg += '<br />';
                                $(el).bind("change", function() { $(this).removeClass("error");})
	                }
	        });
	        }
	    });
            if (msg != '') {
              $.alert(msg)
              $.stop(e);
              $("#overlay").remove();
              return false;
            }
	    return true;
	});
*/  
});
