function handleNewCryptos(data2){
	
	aQ=data2;
	var mess="";
	if(aQ.length<1){
		mess="There are no cryptograms matching that criteria";
	}else if (aQ.length==1){
		mess="There is one matching cryptogram";
	}else{
		mess="There are "+ (aQ.length) +" matching cryptograms";
	}
	
//	alert("out of handle with " +aQ.length+" records");
    i=0;
	rebuildDropDown();
	$("#systemmessage").empty();
	$("#systemmessage").append(mess);
$("#systemmessage").show();	
}
function rebuildDropDown(){
	$("#quoteselect").empty();
	for (var _g = 0; _g < aQ.length; _g++) {
		//if (_g == 0) {
		//	$("#quoteselect").append("<option value='" + _g + "' SELECTED>Today's Cryptogram</option>");
		//}
		//else {
			if (window.showAuthor) {
				$("#quoteselect").append("<option value='" + _g + "' >" + aQ[_g].quote_id + "--" +  aQ[_g].encauthor + "</option>");
			}else{
				$("#quoteselect").append("<option value='" + _g + "' >" + aQ[_g].quote_id + "--" +  aQ[_g].author + "</option>");
				
			}
//		}
	}	
	fixCode();	
	
}
function setCookies(){
		$.cookie("SB_CRYPTO",$("input[@name='showBoxes']:checked").val(),{expires:10000});
		$.cookie("TS_CRYPTO",$("#textSize").val(),{expires:10000});
	};
	

function handleBadCryptos(errorcode,errormessage){
	alert(errorcode +":" + errormessage);
	$("#loading").hide();
}
$(function() {
	
	$("#dialog").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 500,
		width:700,
		modal: true,
		buttons: {
			Cancel: function() {
				$(this).dialog('close');
			},
			'Get More': function(){
			/*
			 * 
			$("#fromdate").val($("#w_fromdate").val());
			$("#todate").val($("#w_todate").val());
			$("#rows").val($('select.#w_rows option:selected').val());
			$("#quote_id").val($("#w_quote_id").val());
			$("#authorsearch").val($("#w_authorsearch").val());
			$("#wordsearch").val($("#w_wordsearch").val());
			$(this).dialog('close');
			document.forms["crypto"].submit();
			*/
			//lets doe this by ajax
			$(this).dialog('close');
			$("#systemmessage").empty();
			$("#systemmessage").append("Loading, please wait "+ '<img src="/images/ajax-loader.gif">');
			$("#systemmessage").show();
			
			data={};
			data.fromdate=$("#w_fromdate").val();
			data.todate=$("#w_todate").val();
			data.rows=$('#w_rows').val();
			data.quote_id=$("#w_quote_id").val();
			if(data.quote_id==""){
				data.quote_id=-1;
			}
			data.authorsearch=$("#w_authorsearch").val();
			if(data.authorsearch==""){
				window.showAuthor=true;
			}else{
				window.showAuthor=false;
			}
				
			data.wordsearch=$("#w_wordsearch").val();
			//alert("data set");
			//alert(data.rows);
			oCrypto=new Cryptocfc(window.datasource);
			oCrypto.setCallbackHandler(handleNewCryptos);
			oCrypto.setErrorHandler(handleBadCryptos);
			oCrypto.getQuoteArray(data.authorsearch,data.wordsearch,data.fromdate,data.todate ,data.rows,data.quote_id,false,false,false,window.datasource);
			//alert("ajax call made");
				
			}
		},
		close: function() {
		},
		open: function() {
			$("#ui-datepicker-div").css("z-index",
			$(this).parents(".ui-dialog").css("z-index")+1);
		} 
	});
	$('#search').click(function() {
		$("#w_fromdate").val("");
			$("#w_todate").val("");
			$('#w_rows').val(10);
			$("#w_quote_id").val("");
			$("#w_authorsearch").val("");
			$("#w_wordsearch").val("");
			
		$('#dialog').dialog('open');
	})
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
		$(this).removeClass("ui-state-active");
	});
});
$(function() {
	$("#help").dialog({
		bgiframe: true,
		autoOpen: false,
		height: 400,
		width:500,
		modal: false,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
			}
		},
		close: function() {
		},
		open: function() {
			
		} 
	});
	$('#gethelp').click(function() {
		$('#help').dialog('open');
	})
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
		$(this).removeClass("ui-state-active");
	});
});
$(function() {

	//run the currently selected effect
	function runEffect(){
		$("#effect").show("explode");
	};
	//set effect from select menu value
	$("#button").click(function() {
		giveUp();
		runEffect();
		return false;
	})
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
		$(this).removeClass("ui-state-active");
	});
	$("#button2").click(function() {
		hint();
		runEffect();
		return false;
	})
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
		$(this).removeClass("ui-state-active");
	});
	$("#button3").click(function() {
		frequency();
		runEffect();
		return false;
	})
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
		$(this).removeClass("ui-state-active");
	});
	$("#button4").click(function() {
		fixCode();
		return false;
	})
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
		$(this).removeClass("ui-state-active");
	});
	$("#button5").click(function() {
		toggleButtons("No");
		window.print();
		toggleButtons("Yes");
		return false;
	})
	.hover(
		function(){ 
			$(this).addClass("ui-state-hover"); 
		},
		function(){ 
			$(this).removeClass("ui-state-hover"); 
		}
	)
	.mousedown(function(){
		$(this).addClass("ui-state-active"); 
	})
	.mouseup(function(){
		$(this).removeClass("ui-state-active");
	});
});
function toggleBoxes(on){
	if(on=="Yes"){
		window.inputClass="redcinput";
	}
	else{
		window.inputClass="redcnoboxes";
	}
	setCookies();
	reBuild();
	return true;
}
function toggleButtons(on){
	if(on=="Yes"){
		$("#botombuttons").show();
		$("#pb").show();
	}
	else{
			$("#botombuttons").hide();
			$("#pb").hide();
	}
	return true;
}
	

