function handleError() {
	return true;
}
window.onerror = handleError;

// This function swaps the link color depending on whether the submenu is open or closed
function toggleStyle(foo) {
	var button = document.getElementById(foo).firstChild;
	if(button.style.color==='#FFDE00') button.style.color='#FFF';
	else button.style.color='#FFDE00';
}

// This function puts the word 'search' into an empty search box
function searchText(foo,bar,blah) {
	var box = document.getElementById(blah);
	if (foo === 'hide') {
		if (box.value === bar) {
			box.value = '';
			box.style.color = '#000';
		}
	} else {
		if (box.value === '') {
			box.value = bar;
			box.style.color = '#999';
		}
	}
}

// These functions open and close the boxes at the top of the page
function toggleTop(foo) {
	closeMe();
	document.getElementById(foo).style.display = 'block';
}
function closeMe(foo) {
	var topBoxes;
	topBoxes = document.getElementsByTagName('div');
	for (i=0 ; i<topBoxes.length ; i++) {
		if (topBoxes[i].className === 'topBox') topBoxes[i].style.display = 'none';
	}
}

// These functions open and close the boxes on Article Central - these should really be consolidated with the ones above.  Consider it on the "to-do" list.
function toggleCats(foo) {
	closeMe2();
	document.getElementById('article'+foo).style.display = 'block';
}
function closeMe2(foo) {
	var catBoxes;
	catBoxes = document.getElementsByTagName('div');
	for (i=0 ; i<catBoxes.length ; i++) {
		if (catBoxes[i].className === 'articleSubcat') catBoxes[i].style.display = 'none';
	}
}

// This function makes the sidebar video interface appear to advance
var count = 0;
function nexty() { 
	count++; 
	if (count<10) {
		document.getElementById('tvControls').style.backgroundPosition = '-' + (count*124) + 'px -168px';
	} else {
		count = 0;
		document.getElementById('tvControls').style.backgroundPosition = '0px -168px';
	}
}
function lasty() {  
	count--; 
	if (count>0) {
		document.getElementById('tvControls').style.backgroundPosition = '-' + (count*124) + 'px -168px';
	} else {
		count = 10;
		document.getElementById('tvControls').style.backgroundPosition = '0px -168px';
	}
}

// This function makes the video interface appear to advance in main content areas
countMain = 0;
allCounters = new Array();
allVids = new Array();
function nexty2(foo) {
	countMain++;
	if(foo !== '') countMain = foo-1;
	for (i=0 ; i<allCounters.length ; i++) allCounters[i].style.fontWeight = 'normal';
	for (i=0 ; i<allVids.length ; i++) allVids[i].style.display = 'none';
	if(countMain === allCounters.length) {
		countMain = 0;
	}
	allCounters[countMain].style.fontWeight = 'bold';
	allVids[countMain].style.display = 'block';
}

function lasty2() {
	countMain--;
	for (i=0 ; i<allCounters.length ; i++) allCounters[i].style.fontWeight = 'normal';
	for (i=0 ; i<allVids.length ; i++) allVids[i].style.display = 'none';
	if(countMain<0) {
		countMain = allCounters.length-1;
	}
	allCounters[countMain].style.fontWeight = 'bold';
	allVids[countMain].style.display = 'block';
}

// This function "hooks up" all contact forms - greatly reduces spam
function activate(brillig) {
	document.getElementById('config').name = "config";
	document.getElementById('config').value = brillig;
}

// This function runs whenever any page loads
function setup() {
	if(document.getElementById('mainVids')) {
		allCounters = document.getElementById('mainVidControl').getElementsByTagName('span');
		allVids = document.getElementById('mainVids').getElementsByTagName('li');
	}

	// OUTBOUND LINKS OPEN IN A NEW WINDOW UNLESS EXPLICITLY TOLD OTHERWISE
	// FORCE SAME WINDOW: rel="self" in anchor
	// FORCE EXTERNAL WINDOW: rel="external" in anchor
	var domain = window.location+'';
	domain = domain.replace('//','');
	domain = domain.substring(domain.indexOf(':')+1,domain.indexOf('/'));
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i=0; i<anchors.length; i++) {
		var URL = anchors[i].href;
		if ((URL.search(domain)===-1 && URL.search('http')!==-1 && anchors[i].rel !== 'self') || anchors[i].rel === 'external') anchors[i].target = "_blank";
	}
}

window.onload=setup; 

jQuery(document).ready(function() {                       
  if($('#reg-form input[id=user_reg_magazine]:checked').val() == null) {
  	$('#reg-form .row-address').hide();
  }
  
  var showFields = function() {
    $('#reg-form .row-address').toggle();
  }
  
  $('#reg-form input[id=user_reg_magazine]').click(showFields);
 
  $('#user_job').change(function() {
    if ($(this).val() == 'Other') {  
      $('.other-job').show();
      $('#user_job_other').focus();
    }
    else {
      $('.other-job').hide();
    }
  });
  
  $('#user_company_type').change(function() {
    if ($(this).val() == 'Other') {  
      $('.other-company-type').show();
      $('#user_company_type_other').focus();
    }
    else {
      $('.other-company-type').hide();
    }
  });
  
  $('#user_referrer').change(function() {
    if ($(this).val() == 'Other') {
      $('.other-referrer').show();
      $('#user_referrer_other').focus();
      $('.other-tradeshow').hide();
    }
    else if ($(this).val() == 'Conference or Trade Show') {
      $('.other-tradeshow').show();
      $('#user_tradeshow').focus();
      $('.other-referrer').hide();
    }
    else {
      $('.other-tradeshow').hide();
      $('.other-referrer').hide();
    }
  });
  
  $('.trigger-non-generic').click(function() {
    $('.generic-author td input').val('');
    $('.non-generic-author').toggle();
    $('.generic-author').hide();
    return false;
  });

  $('.trigger-generic-author').click(function() {
    $('.generic-author').show();
    $('.non-generic-author').hide();
    return false;
  });
  
  $('.select-image span a').click(function() {
    $(this).parent().parent().find('span a').each(function() {
      $(this).removeClass('chosen');
    });            
    $(this).parent().parent().find('input').attr('value', $(this).find('img').attr('src'));
    $(this).addClass('chosen');
    return false;
  });
});

