function makeSublist(parent,child,isSubselectOptional) {
	$("body").append("<select style='display:none' id='"+parent+child+"'></select>");
	$('#'+parent+child).html($("#"+child+" option"));
	$('#'+child).html("<option></option>");
	$('#'+parent).change(
		function()
		{
			var parentValue = $('#'+parent).attr('value');
			if (parentValue == 'null') {
				$('#'+child).attr('disabled','true');	
			}
			$('#'+child).html($("#"+parent+child+" .sub_"+parentValue).clone());
			if(isSubselectOptional) {
				if (parentValue != 'null') {
					$('#'+child).prepend("<option value='null'>-----------------------------</option>");
					$('#'+child).prepend("<option value='null'>Select city</option>");					
					$('#'+child).attr('disabled','');
				} else {
					$('#'+child).prepend("<option value='null'></option>");
				}
			}
			$('#'+child+' option:first').attr("selected", "selected");
		}
	);
	$('#'+child).attr("disabled","true");
}

$(document).ready(function(){
	makeSublist("state","city", true);
	
	$("#submit").each(function(){
		$(this).addClass("buttonwrapped");
		$(this).attr("value","");
	});
	

	jQuery.validator.addMethod( 
		"selectNone", 
		function(value, element) { 
			if (element.value == "null") { 
				return false; 
			} else return true; 
			}, 
	  "Please select your location" 
	); 
	
	if ($("#sendreminder").length > 0) {
		var validator = $("#sendreminder").validate({ 
			rules: { 
				firstname: "required", 
				lastname: "required",
				email: { 
					required: true, 
					email: true
				}, 
				state: {
					selectNone: true
				},
				city: {
					selectNone: true
				},
				checkbox: "required"
			}, 
			messages: { 
				firstname: "Please enter your first name", 
				lastname: "Please enter your last name", 
				email: "Please enter a valid email address", 
				state: "Please select your state", 
				city: "Please select your city", 
				checkbox: "Please select the checkbox to subscribe to the reminder"
			}, 
			errorPlacement: function(error, element) { 
				if (element.is(":checkbox") ) {
					element.siblings("label").after(error);
				} else {
					error.appendTo(element.parent()); 
				}
			}
		}); 
	}

	if ($("#unsubscribe").length > 0) {
		var validator2 = $("#unsubscribe").validate({ 
			rules: { 
				email: { 
					required: true, 
					email: true
				}
			}, 
			messages: { 
				email: "Please enter a valid email address"
			}, 
			errorPlacement: function(error, element) { 
				if (element.is(":checkbox") ) {
					element.siblings("label").after(error);
				} else {
					error.appendTo(element.parent()); 
				}
			}
		}); 
	}	
	

});

 
