jQuery('document').ready(function() {
   jQuery('a[rel*=lightbox]').lightBox();
});

function updateTips(tips, text) 
{
   tips.show().find('span.text').html(text);

   setTimeout(function() {
      tips.removeClass('ui-state-highlight', 1500);
   }, 500);
}

function checkLength(tips, o, n, min, max) 
{
   if (o.val().length > max || o.val().length < min) 
   {
      updateTips(tips, "Length of " + n + " must be between "+min+" and "+max+".");
      return false;
   } 
   else 
   {
     return true;
   }
}

function checkRegexp(tips, o, regexp, n) 
{
   if (!( regexp.test( o.val() ) ) ) 
   {
      updateTips(tips, n);
      return false;
   } 
   else 
   {
      return true;
   }
}

function checkEmail(tips, o) 
{
   return checkRegexp(tips, o,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,'Please enter correct email address eg. team@samknows.com');
}

function TextareaLimitChars(textarea, limit, infodiv)
{
   var text = textarea.value; 
   var textlength = text.length;

   if(textlength > limit)
   {
      jQuery(infodiv).text('You cannot write more then '+limit+' characters!');
      textarea.value = text.substr(0,limit);

      return false;
   }
   else
   {
      jQuery(infodiv).text('You have '+ (limit - textlength) +' characters left.');

      return true;
   }
}

/**
 * Export exchanges list in CSV format and send file to user
 *
 * @param String filename - Name of file
 * @param String url - URL of action that will generate CSV file
 */     
 function ExportExchangesCSV(url, filename)
 {  
    if(filename != '') {
       url += '?filename=' + filename;
    } else {
       url += '?filename=Unknown';
    }

    SendFile(BuildExchangeUrl(url));
 }

/**
 * Export exchanges list in XML format and send file to user
 *
 * @param String filename - Name of file
 * @param String url - URL of action that will generate XML file
 */
 function ExportExchangesXML(url, filename)  
 {
    if(filename != '') {
       url += '?filename=' + filename;
    } else {
       url += '?filename=Unknown';
    }

    SendFile(BuildExchangeUrl(url));
 }

/**
 * Create hidden iframe and loaded insed specified file
 *
 * @param String url - URL of file that will be sent to user
 */
 function SendFile(url)
 {
    jQuery('#export-iframe').remove();
    jQuery('<iframe id="export-iframe">').css({'visibility': 'hidden'}).attr('src', url).appendTo('body');
 }


/**
 * Update exchanges grid
 *
 * @param String url - URL of action that returns list of exchanges
 */
 function UpdateExchangesGrid(url)
 {
    var statusFilter       = jQuery('#status-filter :radio[name=status-filter]:checked');
    var contentFilterBy    = jQuery('#content-filter #filter-by option:selected').val();
    var contentFilterValue = jQuery('#content-filter #filter-value').val();

    jQuery('#Grid').setGridParam({
       url: BuildExchangeUrl(url),
       page: 1
    }).trigger('reloadGrid');
}

/**
 * Return URL with specified filters for exchanges
 *
 * @param String url - URL of action
 */
 function BuildExchangeUrl(url)
 {
    var statusFilter       = jQuery('#status-filter :radio[name=status-filter]:checked');

    var parameters = 
    {
       'status'      :   jQuery(statusFilter).attr('status'),
       'service'     :   jQuery(statusFilter).attr('service'),
       'filterby'    :   jQuery('#content-filter #filter-by option:selected').val(),
       'filtervalue' :   jQuery('#content-filter #filter-value').val(),
       'rand'	     :   Math.random()
    }

    jQuery.each(parameters, function(key, value) {
       if(value != undefined && value != '') {
          url += (url.indexOf('?') == -1 ? '?' : '&') + key + '=' + value;
       }
    });

    return url;
 }

