$(document).ready(function() {
	// Anytime a radio button is clicked in a ranking row, make sure no other radio buttons are clicked in the same row.
	$('tr.rankingrow td input').click( function(){ 
		var selectedInput = $(this);
		$(this).parent().parent().find('input').each(function(){
			if ($(this).attr('name') != selectedInput.attr('name'))
				$(this).attr('checked', false);
		});
	});
	
	// If a writein option is selected from a dropdown, need to display the write in box, and it needs to be required
	$('tr.selectrow td select').change( function(){ 
		if ($(this).find('option.other:selected').length){
			$(this).parent().parent().parent().find('td.otherrow').show();
			$(this).parent().parent().parent().find('td.otherrow input.otherrequire').attr('value','true');
		} else {
			$(this).parent().parent().parent().find('td.otherrow').hide();
			$(this).parent().parent().parent().find('td.otherrow input.other').attr('value','');
			$(this).parent().parent().parent().find('td.otherrow input.otherrequire').attr('value','false');
		}
	});
	
	// If a write in radio/check is selected the text needs to be required, if text has value auto select
	$('tr.selectrow td input').change( function(){ 
		if ($(this).parent().find('.writeinselect').is(":checked")){
			$(this).parent().parent().parent().find('input.otherrequire').attr('value','true');
		} else {
			$(this).parent().parent().parent().find('input.otherrequire').attr('value','false');
			$(this).parent().parent().parent().find('input.writeinfield').attr('value','');
		}
	});
	// XXX: IE was not getting triggered on the change for the first click only, so we've added a click handler as well.
	$('tr.selectrow td input').click( function(){ 
		if ($(this).parent().find('.writeinselect').is(":checked")){
			$(this).parent().parent().parent().find('input.otherrequire').attr('value','true');
		} else {
			$(this).parent().parent().parent().find('input.otherrequire').attr('value','false');
			$(this).parent().parent().parent().find('input.writeinfield').attr('value','');
		}
	});
	
	$('input.writeinfield').keyup( function(){ 
		if ($(this).val() != ''){
			$(this).parent().find(':radio').attr('checked','checked');
			$(this).parent().find(':checkbox').attr('checked','checked');
		} else {
			$(this).parent().find(':radio').removeAttr('checked');
			$(this).parent().find(':checkbox').removeAttr('checked');
		}
	});
	
});


var popUpWin = 0;
function popup(url, width, height, bars, uleft, utop) {
	var left, top;

  if(popUpWin)
  {
    if(!popUpWin.closed) popUpWin.close();
  }

  if(!isNumber(uleft)){
		left = (screen.width/2) - width/2;
	} else {
		left = uleft;
	}

  if(!isNumber(utop)){
		top = (screen.height/2) - height/2;
	} else {
		top = utop;
	}

	agent = navigator.userAgent;
	windowName = 'popUpWin';
	params  = '';
	params += 'toolbar='+ bars +',';
	params += 'location=0,';
	params += 'directories=0,';
	params += 'status=0,';
	params += 'menubar='+ bars +',';
	params += 'scrollbars=1,';
	params += 'resizable=1,';
	params += 'width='+ width +',';
	params += 'height='+ height +',';
	params += 'left='+ left +',';
	params += 'top='+ top;
	popUpWin = window.open(url, windowName , params);
	if (!popUpWin.opener) {
		popUpWin.opener = window;
	}

	function isNumber(inputString)
	{
		return (!isNaN(parseInt(inputString))) ? true : false;
	}
}

function log(s){
	if(typeof console!="undefined" && typeof console.debug!="undefined"){
		console.log(s);
	} else{
		alert(s);
	}
}
