$(function () {
    if ($.browser.msie) {
        $('input:radio').click(function () {
            this.blur();
            this.focus();
        });
    }
});

$(document).ready(function(){
	$("#usefulnesspoll>input[name='response']:radio").change(function(event){
		var page = $("#pageid").val();
		var userid = $("#userid").val();
		var response = $("#usefulnesspoll input[name='response']:radio:checked").val();
		submitUsefulnessPoll( page, userid, response );
	});
});

function submitUsefulnessPoll ( page, userid, response, comment ) {
	var proxy = new AesRemoteProxy();
	proxy.setCallbackHandler(handleUsefulnessPollResponse);
	proxy.setErrorHandler(errorUsefulnessPollResponse);
	if (comment) {
		proxy.saveUsefulnessPollResponse( page, userid, response, comment );
	} else {
		proxy.saveUsefulnessPollResponse( page, userid, response );
	}
}

var handleUsefulnessPollResponse = function (result) {
	
	var linkHTML = '<a href="';
	
	linkHTML = linkHTML + "javascript:openPageCommentWindow(";
	linkHTML = linkHTML + "'" + result.PAGE + "'";
	linkHTML = linkHTML + ",";
	linkHTML = linkHTML + "'" + result.USERID + "'";
	linkHTML = linkHTML + ');">comments</a>';
	
	if (result.SUCCESS) {
		$('#usefulnesspoll').html('<span class="success">Thank you for your response!</span> | ' + linkHTML);
	} else {
		$('#usefulnesspoll').html('<span class="error">There was an error saving your response.  The developer has been notified.</span>');
	}
	try {
	
		$('#wdwUsefulnessPoll').remove();
		
	} catch (err) {
		
	}
}

var errorUsefulnessPollResponse = function (error) {
	$('#usefulnesspoll').html('<span class="error">There was an error saving your response.  The developer has been notified.</span>');
	try {
		$('#wdwUsefulnessPoll').remove();
	} catch (err) {
		
	}
}

function openPageCommentWindow ( pageid, userid ) {
	openAWindow(600,400,'wdwUsefulnessPoll','/usefullnesspoll.cfm?userid=' + userid + '&pageid=' + pageid,'Was this page useful?');
}

function openAWindow(width,height,name,uri,title) {

	var html = "";
	
	$.ajax({
		async: false,
		url: uri,
		dataType: "html",
		success: function (data,status,request) {
			html = data;
		},
		cache: false
	});	
	
	$('body').append('<div id="' + name + '" style="display: none">' + html + '</div>');
	
	$('#' + name).dialog({
		height: height,
		width: width,
		draggable: true,
		modal: true,
		position: 'center',
		resizable: true,
		title: title,
		close: function (event, ui) {
			$('#' + name).remove();
		}
	});
	
}

