
try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}


$(document).ready(function(){

  $('a.popup').click(function(){
    newWindow = window.open(this.href,'name','fullscreen=yes,resizable=yes');
    if(window.focus) {
      newWindow.focus();
    }
    return false;
  })
  
  $('a.live_docs_help').click(function(){
    newWindow = window.open(this.href,'help','resizable=yes,width=500,height=600,scrollbars=yes');
    if(window.focus) {
      newWindow.focus();
    }
    return false;
  })  
  
  $('#live_docs_edit').click(function(){
    newWindow = window.open(baseDir + 'admin/live_documents/edit/2','name','fullscreen=yes,resizable=yes');
    if(window.focus) {
      newWindow.focus();
    }
    return false;
  })  

  var focusElement = $('.form_error:eq(0)');
  var focusId = focusElement.attr('id');
  
  if(focusId != undefined && focusElement != undefined)
  {
    document.location.href = '#' + $('.form_error:eq(0)').attr('id');
    try{
			focusElement.focus();
		} catch(e)
		{
			// What do we do? Hmm.
		}
  }
  
  // Required labels
   $('div.required label').append('<span style="font-size:0.8em;color:#FF0000; display:inline; ">&nbsp;*</span>');
  
  // Form tooltips
  $('select,input,textarea').each(function(i){
    if(this.title != '')
    {
      $(this).before('<div class="tooltip" title="' + this.title + '"></div>');
      this.title = '';
    }
  }); 
  
  $('.tooltip').Tooltip({ track: true, delay: 0 });
  $('#AccountIndex tr td').Tooltip({ track: false, delay: 0 });
  $('#AccountIndex tr').hover(function(){ $(this).addClass('hover'); }, function(){ $(this).removeClass('hover'); });
  $('#AccountIndex tr').click(function(event){
		if(event.target.tagName != 'A' && event.target.tagName != 'TH')
		{
			document.location.href = $('a', $(this)).get(0).href;
		}
		//return false;
	});
	
	
  $('#partner_change').change(function(){
    document.location.href = '?partner=' + this.options[this.selectedIndex].value;
  });
  
 $('#unique_0, #unique_1').click(function(){
    
    switch(this.id)
    {
      case 'unique_0':
        $('#unique_1').get(0).checked = 'checked';
        $('#unique_1').get(0).focus();      
        confirm("By switching to default, you will lose unique content.\n\nAre you sure?") ? changeDefault() : changeUnique();
        break;
      
      case 'unique_1':
        changeUnique();
        break;
    }
    
  });
 
  if($('#unique_1').get(0) != undefined)
  {
    if($('#unique_1').get(0).checked || $('#unique_1').get(0).checked == 'checked')
    {
      $('.warningBox').hide();
    }
  }
  
  /* Update referrer */
  $('#RefererShortName').keyup(function(){
    $('#dynamic_url').html($(this).val().replace(/ /g, '_'));
    $(this).val($(this).val().replace(/_/g, ' '));
  });
  
  $('ul.left a, ul.right a').click(function(){
    var href = this.href.split('#');
    $('.selectedFaq').removeClass('selectedFaq');
    $('#faq_' + href[1]).addClass('selectedFaq');
  });
  

  //Document zipping on admin accounts and reports

  $('#download_selected').click(function(){
    var query = '?';
    var checked = false;
    $('.tick_downloaded .checkbox').each(function(i){
      if($(this).attr('checked'))
      {
        checked = true;
      }
      query += this.value + '=' + $(this).attr('checked') + '&';
    });
    
    if(!checked)
    {
      alert('You need to choose which files you want to download.');
    }
    else
    {
      document.location.href = $('#download_zip_url').val() + query;
    }
    return false;
  });
  
  $('#select_all').click(function(){
    $('.tick_downloaded .checkbox').attr('checked', 'checked');
  });
  
  $('#select_none').click(function(){
    $('.tick_downloaded .checkbox').attr('checked', '');
  });
      
  
  
  /* Flash Message close button */
  //$('#flashMessage').html($('#flashMessage').html() + '<a class="close" href="#" id="closeFlashMessage">[close this]</a>');
  //$('#closeFlashMessage').click(function(){ $('#flashMessage').hide(); return false; });
  
  
  // Sort code box skipping
  
  $('#AccountSortCode1, #AccountSortCode2, #AccountSortCode3, #AccountAccountNumber').attr('autocomplete', 'off');
  
  /*
  $('#AccountSortCode1, #AccountSortCode2, #AccountSortCode3').keyup(function(event){
    var this_obj = $(this);
    
    if(this_obj.val().length == 2)
    {
      switch(this_obj.attr('id'))
      {
        case 'AccountSortCode1':
          $('#AccountSortCode2').focus();
          break;
        
        case 'AccountSortCode2':
          $('#AccountSortCode3').focus();
          break;
      }
    }
  });
  
  $('#AccountSortCode2, #AccountSortCode3').keydown(function(event){
    var this_obj = $(this);
      
    if(this_obj.val().length == 0 && event.keyCode == 8)
    {
      switch(this_obj.attr('id'))
      {
        case 'AccountSortCode3':
          $('#AccountSortCode2').val($('#AccountSortCode2').val().substr(0,1));
          setTimeout(function(){$('#AccountSortCode2').focus(); setCaretPosition($('#AccountSortCode2').get(0), 1);}, 10);
          break;
        
        case 'AccountSortCode2':
          $('#AccountSortCode1').val($('#AccountSortCode1').val().substr(0,1));
          setTimeout(function(){$('#AccountSortCode1').focus(); setCaretPosition($('#AccountSortCode1').get(0), 1);}, 10);
          
          break;
      }      
    }
  });*/
  
});

function setCaretPosition(oField, iCaretPos)
{
  if($.browser.msie)
  {
    if (document.selection)
    {
      // Set focus on the element
      oField.focus ();
      
      // Create empty selection range
      var oSel = document.selection.createRange ();
      
      // Move selection start and end to 0 position
      oSel.moveStart ('character', -oField.value.length);
      
      // Move selection start and end to desired position
      oSel.moveStart ('character', iCaretPos);
      oSel.moveEnd ('character', 0);
      oSel.select ();
    }
  }
}

function changeDefault()
{
  $('#unique_0').get(0).checked = 'checked';
  $('#unique_0').get(0).focus();     
  $('.warningBox').show();
  
  if($('#sectionUnique').attr('id') == 'sectionUnique')
  {
    document.location.href = baseDir + 'admin/sections/revert/' + $('#SectionId').val();
  }
  else
  {
    document.location.href = baseDir + 'admin/emails/revert/' + $('#EmailName').val();
  }
  
  $('#mainInner').html('Please wait..');
}

function changeUnique()
{
  //console.log('Unique');
  $('#unique_1').get(0).checked = 'checked';
  $('#unique_1').get(0).focus(); 
  $('.warningBox').hide();
}


/**
 * Function to print just a HTML element on a page.
 **/
var win=null;
function printElement(element)
{
  var printThis = document.getElementById(element).innerHTML;
  win = window.open();
  self.focus();
  win.document.open();
  win.document.write('<'+'html'+'><'+'head'+'><'+'style'+'>');
  win.document.write('body, td { font-family: Times New Roman, Serif; font-size: 10pt;}');
  win.document.write('<'+'/'+'style'+'><'+'/'+'head'+'><'+'body'+'>');
  win.document.write(printThis);
  win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
  win.document.close();
  win.print();
  win.close();
} 