// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
try{
Ajax.Responders.register({
  onCreate: function() {
    if($$('body').first() && Ajax.activeRequestCount > 0 && !navigator.userAgent.match(/Safari/))
        $$('body').first().addClassName('busy');
  },
  onComplete: function() {
    if($$('body.busy') && Ajax.activeRequestCount == 0)
        $$('body.busy').first().removeClassName('busy');
  }
})
}catch(err) {};

function copy_html_to_text(from, to) {
  $(to).value = $(from).getValue().stripScripts().stripTags();	
}

function displayHTML(from) {
  var inf = $(from).value;
  win = window.open(", ", 'popup', 'toolbar = no, status = no');
  win.document.write("" + inf + "");
}

function numbered_icon(num){
    var icon = new GIcon(G_DEFAULT_ICON);
    icon.image = "/images/map/marker-"+(num+1)+".png";
    icon.shadow = "/images/map/marker-shadow.png";
    icon.iconSize = new GSize(22.0, 36.0);
    icon.shadowSize = new GSize(41.0, 36.0);
    icon.iconAnchor = new GPoint(11.0, 18.0);
    icon.infoWindowAnchor = new GPoint(11.0, 18.0);
    return icon;
}

function showBlock(block, linkText){
	$(block).show();
	if($("settings-builder")) {
    $("settings-builder").hide();
  	text = linkText.getElementsByTagName('span')[0].innerHTML;
	  $('sub-title').innerHTML = text;
	  $('main-title').hide();
	  $('sub-title').show();
  }
}

function hideBlock(block){
	$(block).hide();
	if($("settings-builder")) {
    $("settings-builder").show();
	  $('sub-title').hide();
	  $('main-title').show();
  }
}

function toggleGoBack(block) {
	$$('a.m-button.left.global').each(function(global){$(global).hide()});
	$$('a.m-button.left.local').each(function(local){
		$(local).onclick = function(){
			hideBlock(block);
			if($('send_section')) $('send_section').hide();
			if($('schedule_section')) $('schedule_section').hide();
			$$('.local-hide').each(function(e){$(e).hide()});
			$$('a.m-button.left.local').each(function(global){$(global).hide()});
			$$('a.m-button.left.global').each(function(global){$(global).show()});
			return false;
		};
		$(local).show();
	});
}

function section_activated(link) {
  $$('.sliding.sections a.black').each(function(link) { $(link).removeClassName('black') });
  $$('.sliding.sections a.bold').each(function(link) { $(link).removeClassName('bold') });
  
  
  $$('.sliding.sections .section').each(function(section){

    if ($(section).hasClassName('static')) {
      // static content we just hide
      $(section).hide();
    } else {
      // Content updated by XHR so just remvove the content
      $(section).innerHTML = '';
    }
  })

  var parent = $(link).parentNode;
  $(parent).down('.section').show();
  $(link).addClassName('black');
  $(link).addClassName('bold');
}
function toggle(id)
{
	var e = document.getElementById(id);
	if (e) e.style.display = (e.style.display == "block") ? 'none' : 'block';
}

function checkAll(field)
{
  for (var i = 0; i < field.length; i++)
    field[i].checked = true ;
}

function uncheckAll(field)
{
  for (var i = 0; i < field.length; i++)
    field[i].checked = false ;
}

function enableAll(fields) {
  for (var i = 0; i < fields.length; i++)
    fields[i].disabled = false;
}

function disableAll(fields) {
  for (var i = 0; i < fields.length; i++)
    fields[i].disabled=true;
}

var _textarea_regexp = /([^,;\n]*?[,;\n]){250,}/;
function limitTextArea(field, message) {
  if(field.value.match(_textarea_regexp)) {
    $('limit-message').show();
    return false;
  } else {
    $('limit-message').hide();
    return true;
  }
}

function submitFormOnEnter(form, e) {
  if(e.keyCode == 13) {
    form.submit();
    return true;
  }
  return false;
}

/* resize image */
function resizeImage(img, maxwidth, maxheight)
{
  if ( img.width == 0 || img.height == 0){
    image = new Image();
    image.src = img.src;
  }else
    image = img;

  w = image.width;
  h = image.height;

  if( w == 0 || h == 0 )
  {
    img.width = maxwidth;
    img.height = maxheight;
  }
  else if (w > h)
  {
    (w > maxwidth) ? img.width = maxwidth : img.width = img.width;
  }
  else
  {
    (h > maxheight) ? img.height = maxheight : img.height = img.height;
  }
}

function rpSuccessMessage(message, reloadPg) {
  jQuery('#complete .complete p.message').text(message);
  tb_show('', '#TB_inline?height=140&width=250&inlineId=complete', '');
}

function rpFailMessage(message) {
  jQuery('#failed .failed p.message').text(message);
  tb_show('', '#TB_inline?height=140&width=250&inlineId=failed', '');
}

function updateFlashMessage(flash_type, message) {
  $('div').remove('#flash')
  $('#col_mid_console').prepend('<div id="flash" class="flash ' + flash_type + '">' + message + '</div>')
}

function filesizeformat(bytes, suffixes){
  var b = parseInt(bytes, 10);
  var s = suffixes || ['byte', 'bytes', 'kb', 'Mb', 'Gb'];
  if (isNaN(b) || b === 0) { return '0 ' + s[0]; }
  if (b == 1)              { return '1 ' + s[0]; }
  if (b < 1024)            { return  b.toFixed(2) + ' ' + s[1]; }
  if (b < 1048576)         { return (b / 1024).toFixed(2) + ' ' + s[2]; }
  if (b < 1073741824)      { return (b / 1048576).toFixed(2) + ' '+ s[3]; }
  else                     { return (b / 1073741824).toFixed(2) + ' '+ s[4]; }
}


function showGreyLoader() {
  jQuery('#TB_closeWindowButton').click();
  jQuery('.loader').show();
}

function hideGreyLoader() {
  jQuery('.loader').hide();
}

function escape_html(string) {
  var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
  return string.replace(regexp, "")
}