$(document).ready(function(){
	if ($("input:first").attr('type') != 'button' && $("input:first").attr('type') != 'submit') {
		$("input:first").focus().select();
	}
	
	var tabindex = 1;
	$('input,select').each(function() {
		if (this.type != "hidden") {
			var $input = $(this);
			$input.attr("tabindex", tabindex);
			tabindex++;
		}
	});
	
	$("input[type=submit].next").click(function(){
		$("span.red").remove();
		$(".red").removeClass("red");
		$("#question input").each(function(){
			if ($(this).val().length == 0 && $(this).attr("type") != "hidden" && $(this).attr("type") != "radio" && $(this).parent("div").css("display") != "none") {
				$(this).addClass("red").after(' <span class="red"><label style="display: inline; padding-top: 0;" class="red">&laquo; This is required.</label></span>');
			}
			if ($(this).attr("type") == "radio" && $("input[name="+$(this).attr("name")+"]:checked").length == 0 && $(this).parent("div").css("display") != "none") {
				//$("label[for="+$(this).attr("id")+"]").addClass("red");
				if($(this).hasClass("no")) {
					$("span.red").remove();
					$(this).next("label.radio").after('<span class="red"><label style="display: inline; padding-top: 0;" class="red">&laquo; Please make a selection.</label></span>');
				}
			}
		});
		if ($(".red").length > 0) {
			return false;
		}
	});
	
	if ($("#preview").length > 0) {
		$("#preview a").each(function(){
			if ($("#"+$(this).attr("rel")).length > 0) {
				$(this).addClass("active");
				$(this).click(function(){
					$("#"+$(this).attr("rel")).focus();
				});
			}
		});
	}
	$('.bool').click(function(){
		$("span.red").remove();
		var state;
		var unstate;
		if ($(this).hasClass('yes')) {
			state = 'yes';
			unstate = 'no';
		} else {
			state = 'no';
			unstate = 'yes';
		}
		
		$("#"+$(this).attr("name")+"_"+state).show();
		$("#"+$(this).attr("name")+"_"+unstate).hide();
		
		$("#preview p").each(function(){
			if($("#"+$(this).attr('rel')).length > 0) {
				//there is an input associated with this paragraph
				if($("#"+$(this).attr('rel')+":checked").length > 0 && $("#"+$(this).attr('rel')+":checked").is(':visible')) {
					$(this).show();
				} else {
					$(this).hide();
				}
			}
		});
		
		$("#preview p").html(updatePolicy());
		
		$("input").live("change", function() {
			updateText($(this));
		});
		$("input").live("keyup", function() {
			updateText($(this));
		});
		$("input").live("click", function() {
			updateText($(this));
		});
	});
	$('.bool[checked=checked]').click();
	$("#preview a").live("click", function(){
		$("#"+$(this).attr("rel")).focus().select();
	});
});
function updateText(input) {
	if ($("a[rel="+input.attr("id")+"]").length > 0) {
		test = $("a[rel="+input.attr("id")+"]");
		if (test.attr("rel").length > 0) {
			if (input.val().length > 0) {
				$("a[rel="+input.attr("id")+"]").text(input.val());
			} else {
				$("a[rel="+input.attr("id")+"]").text("________");
			}
		}
	}
	if ($("#preview p").length > 0) {
		$("hr.showhide").hide();
		$("#preview p").each(function(){
			if ($(this).css("display") != "none") {
				$("hr.showhide").show();
			}
		});
		$("#preview p:first strong").remove();
		$("#preview p:first").prepend("<strong>Preview: </strong>");
	} else {
		$("hr.showhide").hide();
	}
}
function updatePolicy() {
	if ($("#preview p").length > 0) {
		$("#preview p").each(function(){
			var posopen = 0;
			var posclose = 0;
			var i = -1;
			var paragraph = $(this).html();
			while(posopen != -1) {
				posopen = paragraph.indexOf("[", i+1);
				posclose = paragraph.indexOf("]", i+1);
				var replace = paragraph.substring(posopen, posclose+1);
				var nobrackets = replace.replace("[", "");
				nobrackets = nobrackets.replace("]", "");
				nobrackets = "<a rel='"+nobrackets+"'>________</a>";
				if (nobrackets.length > 0 && replace.length > 0) {
					paragraph = paragraph.replace(replace, nobrackets);
				}
				i = posclose+1;
			}
			$(this).html(paragraph);
		});
	} else {
		return null;
	}
}

function generatepolicy() {
	$.ajax({
		url: '/index.php/welcome/ajax_policy',
		type: 'post',
		data: 'policy='+$('#policy').text(),
		/*data: 'fullname='+$('#fullname').val()+'&email='+$('#email').val()+'&contact='+$('input:checked').val()+'&policy='+$('#policy').text(),*/
		success: function(msg) {
			$("#policy").show();
			$("input, #question, hr").hide();
			$(document).scrollTo("#header");
		}
	});
}
$(document).ready(function() {
	$('#agreement').dialog( {
		autoOpen: false,
		width: 780, minWidth: 780, maxWidth: 780,
		height: 500, minHeight: 400, maxHeight: 800, 
		modal: true,
		draggable: true, closeOnEscape: true, 
		buttons: { 
			"CLOSE": function() { 
				$('#agreement').dialog('close');
			}
		} 
		
	} );
} );
function opendialog() {
	$('#agreement').dialog('open');
}
