function requiredDecorator(){
    $('label.required').each(function(i){
        var tmp = $(this).text();
        $(this).html(tmp + '&nbsp;<span class="required">*</span>');
    });
}
function sanatizeString(str){
  var toReplace = {
      '|': '-',
      '!': '-',
      '"': '-',
      '\'': '-',
      '£': '-',
      '%': '-',
      '&': '-',
      '/': '-',
      '(': '-',
      ')': '-',
      '[': '-',
      ']': '-',
      '{': '-',
      '}': '-',
      '=': '-',
      '?': '-',
      '^': '-',
      '*': '-',
      '°': '-',
      '§': '-',
      '@': '-',
      '#': '-',
      '_': '-',
      '+': '-',
      ';': '-',
      ':': '-',
      '.': '-',
      ',': '-',
      '<': '-',
      ' ': '-',
      'à': 'a',
      'è': 'e',
      'é': 'e',
      'ì': 'i',
      'ò': 'o',
      'ù': 'u',
      'ç': 'c'
  };
  var string = str.replace(/./g, function (match) {
      return toReplace[match] || match;
  });
  var regex = /(^-+)([-a-z0-9]+)/;
  var tmp = string.toLowerCase().substring(0, 255);
  var found = tmp.match(regex);
  if(found !== null){
    return found[2];
  }
  return tmp;
}
function in_array (needle, haystack, argStrict) {
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
 
    return false;
}
function strip_tags (str, allowed_tags) {
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';
 
    var replacer = function (search, replace, str) {
        return str.split(search).join(replace);
    };
 
    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z0-9]+)/gi);
    }
 
    str += '';
 
    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);
 
    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }
 
        // Save HTML tag
        html = matches[key].toString();
 
        // Is tag not in allowed list? Remove from str!
        allowed = false;
 
        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;
 
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}
 
            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }
 
        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }
  return str;
}
function showQuickLoginLoader(sel) {
  var target= $(sel);
  var loader= $('<div id="quickLoginloader"></div>');
  loader.fadeTo("fast", 0.9);
  loader.css({
    width: target.width() + 'px',
    height: target.height() + 'px'
  }).hide().appendTo(target);
  loader.show();
}
function goToProtectedPage(data){
  window.location = data.redirect; // Members Area
}
function showLoginError(msg){
  var html = '<p id="loginError">'+msg+'</p>';
  if ($('#loginError').length > 0){
     $('#loginError').remove();
  }
  
  $('#frm-quick-login').prepend(html);
}


function toIdentify(){
  $.ajax({
        type: "POST",
        
        url:  $('#frm-quick-login').attr('action') ,
        dataType: 'json',
        data:$('#frm-quick-login :input') ,
        success: function(data){
            
            if ($('#quickLoginloader').length > 0){
                 $('#quickLoginloader').remove();
            }
            $('#submitQuickLogin').removeAttr("disabled"); 
            if (typeof data.error !== 'undefined'){
               showLoginError(data.error);

               return;
            }
            else{$("#frm-quick-login").hide();}
            setTimeout(function(){window.location = data.redirect;}, 1000);
            //alert('ok');
            //alert(data.redirect);
            
        },
        beforeSend: function(){
            showQuickLoginLoader('#quickLogin');
            $('#submitQuickLogin').attr('disabled','disabled');
            
        },
        error:function(xhr, str, er){
            alert('error status');
            if ($('#quickLoginloader').length > 0){
                 $('#quickLoginloader').remove();
            }
            $('#submitQuickLogin').removeAttr("disabled"); 
            return;
        }
    }); 
}
function xhrLogin(email,password,loginCallback){
  $.ajax({
        type: "POST",
        
        url:  sBasePath + '/auth/xhr' ,
        cache: false,
        dataType: 'json',
        data: {'email':email,'password':password} ,
        
        success: function(data){
           loginCallback(data);
           
        },
        error:function(xhr, str, er){
            alert('error');
            return;
        }
  }); 
}

