function selectAll()
{
  var elements = document.getElementsByName('id[]');
  var length = elements.length;
  var count = 0;

  for(i = 0; i < length; i++)
  {
    if (elements[i].checked)
    count++
  }

  if (count == length)
  {
    for(i = 0; i < length; i++)
    {
      elements[i].checked = false
    }
  }
  else
  {
    for(i = 0; i < length; i++)
    {
      elements[i].checked = true
    }
  }
}

function selectE()
{
  // top check element
  var element = document.getElementById('chk_top');
  // listing check elements
  var elements = document.getElementsByName('id[]');
  var length = elements.length;
  var count = 0;

  for(i = 0; i < length; i++)
  {
    if (elements[i].checked)
    count++
  }

  if (count == length)
  element.checked = true;
  else
  element.checked = false;
}

function switchF(formID, uri)
{
  var form = document.getElementById(formID);

  form.action = uri;
  form.submit();
}

function changeFilter()
{
  var form = document.getElementById('list');
  if (form) form.submit();
}

function selectOne(id, e)
{
  // IE is retarded and doesn't pass the event object
  if (e == null)
  e = window.event;

  // IE uses srcElement, others use target
  var target = e.target != null ? e.target : e.srcElement;
  
	var fs = '#fs' + id;

  if (target.className == 'cart float-l')
  {
    var element = document.getElementById(id);
    if (element)
    {
      if (element.checked) {
				element.checked = false;
				if(jQuery(fs)) {
					jQuery(fs).removeAttr('class');
				}
			} else {
				element.checked = true;
				if(jQuery(fs)) {
					jQuery(fs).attr('class', 'bg_grey');
				}
			}
    }
  }
}

function selectCheckboxCart(id)
{
	var fs = '#fs' + id;
	
	var element = document.getElementById(id);
  if (element) {
    if (element.checked) {
			if(jQuery(fs)) {
				jQuery(fs).attr('class', 'bg_grey');
			}
		} else {	
			if(jQuery(fs)) {
				jQuery(fs).removeAttr('class');
			}
		}
  }
}

function selectQuantityCart(id)
{
	var fs = '#fsi' + id;
	var qty = '#quantity_' + id;
	
	jQuery('#i' + id).attr('checked', 'checked');
	jQuery(fs).attr('class', 'bg_grey');
	jQuery(qty).select();
}

function fullScreen(_url)
{
	window.open(_url, '', 'fullscreen=yes, scrollbars=auto');
}

function billingAddressSwitch()
{
	var _status = jQuery('#cnfrm-address').attr('checked');
	
	if(_status) {
		jQuery('#address1').attr('disabled', 'disabled').val(jQuery('#_address1').val());
		jQuery('#address2').attr('disabled', 'disabled').val(jQuery('#_address2').val());
		jQuery('#country').attr('disabled', 'disabled').val(jQuery('#_country').val());
		if(jQuery('#_country').val() == 'US') {
			jQuery('#state1').show().attr('disabled', 'disabled').val(jQuery('#_state').val());
			jQuery('#other_state1').hide();
		} else {
			jQuery('#other_state1').show().attr('disabled', 'disabled').val(jQuery('#_statename').val());
			jQuery('#state1').hide();
		}
		jQuery('#city').attr('disabled', 'disabled').val(jQuery('#_city').val());
		jQuery('#zip').attr('disabled', 'disabled').val(jQuery('#_zip').val());
	} else {
		jQuery('#address1').removeAttr('disabled').val('');
		jQuery('#address2').removeAttr('disabled').val('');
		jQuery('#country').removeAttr('disabled').val('US');
		if(jQuery('#state1').css('display') == 'none') {
			jQuery('#other_state1').removeAttr('disabled').val('');
		} else {
			jQuery('#state1').removeAttr('disabled').val('1');
		}
		jQuery('#state1').show(); jQuery('#other_state1').hide();
		jQuery('#city').removeAttr('disabled').val('');
		jQuery('#zip').removeAttr('disabled').val('');
	}
}

function search_checkall()
{
	var _status = jQuery('#search-all').attr('checked');
	
	if(_status) {
		jQuery('#search-articles').attr('checked', 'checked');
		jQuery('#search-webinars').attr('checked', 'checked');
		jQuery('#search-companies').attr('checked', 'checked');
		jQuery('#search-issues').attr('checked', 'checked');
	} else {
		jQuery('#search-articles').removeAttr('checked');
		jQuery('#search-webinars').removeAttr('checked');
		jQuery('#search-companies').removeAttr('checked');
		jQuery('#search-issues').removeAttr('checked');
	}
}

function search_check()
{
	var _checkall = true;
	var _checkid = ['#search-articles', '#search-webinars', '#search-companies', '#search-issues'];
	
	jQuery.each(_checkid, function(val, el) {
		if(!jQuery(el).attr('checked')) {
			_checkall = false;
		}
	});
	
	if(_checkall) {
		jQuery('#search-all').attr('checked', 'checked');
	} else {
		jQuery('#search-all').removeAttr('checked');
	}
}

function updateCcInformation(url, id, update_id)
{
	var card_id = $(id).value;
	new Ajax.Request(url + '/id/' + card_id, { 
		method: 'get', 
		onLoading: function() {
			$('ajax_loader').show();
		},
		onSuccess: function(transport) { 
			$('ajax_loader').hide();
			$(update_id).update(transport.responseText);
		} 
	}); 	
}

function setAsPrimary(url, id)
{
	new Ajax.Request(url + '/id/' + id, { 
		method: 'get', 
		onLoading: function() {
			$('loader'+id).show();
		},
		onSuccess: function(transport) { 
			$('loader'+id).hide();
			$('cards_on_file').update(transport.responseText);
		} 
	}); 	
}
