var $j = jQuery.noConflict();
var backward = false;

//$j(document).ready(function(){
//	$j('#breedselect-content').remove();
//	$j('body').append('<div id="breedselect-content" class="breedselect-content" style="display: none; background-color: #FFF; display: none; width: 902px; height: 601px; overflow: auto; cursor: default;"></div>');
//});


//function breedSelectStart(){
//	showWaitBubble('#breedselect-content');
//	$j.ajax({type:'GET',
//		url:'/Shop?dsp=500000',
//		success:function (responseText) {
//			$j('#breedselect-content').html(responseText);
//		},
//		complete:function(XMLHttpRequest, textStatus) {
//			breedSelectBindForm();
//			$j.blockUI({message: $j('#breedselect-content'),
//            			css: {border: 'none',
//                            background: 'none',
//            				  top: 100 + 'px',
//                			  left: ($j(window).width() - 902) /2 + 'px',
//            				  height: 601 + 'px',
//            				  width: 902 + 'px'},
//            			allowBodyStretch: true
//            		});
//		$j('#breedselect-content').fadeIn('slow');
//
//		}
//	});
//	hideWaitBubble('#breedselect-content');
//}

function breedSelectContinue() {
	if(breedSelectValidate())
		$j('#breedselect-form').submit();
}

function breedSelectBack() {
	showWaitBubble('#breedselect-content');
	if(breedSelectPreviousPage() == 1) {
		$j.ajax({type:'GET',
			url:'/Shop?dsp=500000',
			success:function (responseText) {
				$j('#breedselect-content').html(responseText);
			},
			complete:function (XMLHttpRequest, textStatus){
				breedSelectBindForm();
			}
		});
	}else {
		$j('#page').val((breedSelectPreviousPage()).toString());
		backward = true;
		$j('#breedselect-form').submit();
	}
	hideWaitBubble('#breedselect-content');
}

function breedSelectClose() {
	//$j.unblockUI();
	//$j('#breedselect-content').remove();
	window.location='/Shop';
}

function breedSelectBindForm(){
	$j('#breedselect-form').submit( function() {
		var options = {target: '#breedselect-content', beforeSubmit: breedSelectValidate, success: breedSelectDisplayPage};
		$j(this).ajaxSubmit(options);
			return false;
	});
}

function breedSelectCurrentPage() { return $j('#page').val(); }
function breedSelectPreviousPage() { return breedSelectCurrentPage() - 1; }

function breedSelectValidate(formData, jqForm, options) {
	showWaitBubble('#breedselect-content');

	if(backward){
		backward = false;
		return true;
	}

	return breedSelectValidateCheckboxes() == true ? breedSelectValidateRadios() : false;
}

function breedSelectValidateCheckboxes() {
	var checkboxes = $j('#breedselect-content input:checkbox');

	if(checkboxes.length == 0)
		return true;

	for(i = 0; i < checkboxes.length; i++){
		if($j('input[@name=' + checkboxes[i].name + ']').is(':checked'))
			return true;
	}

	showBreedSelectError();
	return false;
}
function breedSelectValidateRadios() {
	var radios = $j('#breedselect-content input:radio');

	if(radios.length == 0)
		return true;

	for(i = 0; i < radios.length; i++){
		if(!$j('input[@name=' + radios[i].name + ']').is(':checked')) {
			showBreedSelectError();
			return false;
		}
	}

	return true;
}

function showBreedSelectError() {
	hideWaitBubble('#breedselect-content');
	alert("Please ensure you've selected the correct number of choices from each question.");
}

function breedSelectDisplayPage(responseText, statusText){
	$j('#breedselect-content').html(responseText);
	breedSelectBindForm();
	hideWaitBubble('#breedselect-content');
}