jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};

function generateOptions(id, selectID, source){
	
	$("#newcat0").html("");
	
	var parent = 0;
	var idVal = 0;

	var resultField = $("#parent"+id);
	idVal = "#parent" + (id - 1);
	parent = $(idVal+" option:selected").val();
	
	if (parent == 0) $("#newcat0").html("New Category");
	
	$("#parent1").focus(function(){
		$("#parent3, #parent4").html("");
	});
	
	$("#parent2").focus(function(){
		$("#parent4").html("");	 
		$("#newcat1").html("");
	});
	
	$("#parent3").focus(function(){ 
		$("#newcat2").html("");
	});
	
	var QRY = 'parent='+parent+'&selectID='+selectID+'&source='+source;
	$.ajax({
	  type: "POST",
	  data: QRY,
	  url: 'http://'+document.location.host+'/exec/category-options',
	  success: function(data) {
		$(resultField).html(data);
	  }
	});

}

$().ready(function() {

	//ajax form submit

	$("div#form-error").hide();
	$("div#form-success").hide();
	$("div#form-message").hide();

	var options = { 
        target:        '#form-success',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback 
 		resetForm: false
    }; 
 


	// pre-submit callback 
	function showRequest(formData, jqForm, options) { 
	    // formData is an array; here we use $.param to convert it to a string to display it 
	    // but the form plugin does this for you automatically when it submits the data 
	    var queryString = $.param(formData); 

		$("div#form-error").hide();
		$("div#form-loading").show().html('<img src="http://'+document.location.host+'/images/loading.gif" height="23">');
			myTimer = $.timer(2000,function(){
				$("div#form-loading").fadeOut("fast"); // hide the loading gif after 2 seconds
			});

		
	    return true; 
	} 

	// post-submit callback 
	function showResponse(responseText, statusText)  { 
		myTimer = $.timer(2000,function(){
			$("div#form-success").fadeOut("fast").fadeIn("fast").fadeTo(3000, 1.0); // reveal the success after 2 seconds (e.g. when the loading gif has gone)
		});
	}

	
	// validate the comment form when it is submitted

	//default states for form fields
	$("#message-form #name").blur(function(){
		if($(this).attr("value") == ""){
			$(this).attr("value","Name");
		}
	});
	
	$("#message-form #name").focus(function(){
		if($(this).attr("value") == "Name"){
			$(this).attr("value","");
		}
	});

	jQuery.validator.addMethod("nameRequired", function(value, element) {
		return value != element.defaultValue;
	}, "");

	jQuery.validator.addMethod("messageRequired", function(value, element) {
		return value != element.defaultValue;
	}, "");

	jQuery.validator.messages.required = ""; 
	//$("#message-form").validate();
	$("#message-form").validate({
			rules: {
				name: "nameRequired",
				message: "messageRequired",
				email: {
					required: true,
					email: true
				}
			},
			invalidHandler: function(e, validator) {
				var errors = validator.numberOfInvalids();
				if (errors) {
					var message = errors == 1
					? 'You missed 1 field. It has been highlighted above'
					: 'You missed ' + errors + ' fields. They have been highlighted above';
					$("div#form-error").html('<p>'+message+'</p>');
					$("div#form-message").hide();
					$("div#form-error").fadeIn("fast");
				}
			},
			onkeyup: false,
			submitHandler: function(form) {
				$("div#form-error").fadeOut("fast");
				$(form).ajaxSubmit(options); 
				
				
				//alert("submit! use link below to go to the other step");
			},
			messages: {
				name: "",
				message: "",
				email: ""
				},
			debug: true
		});
	
});
