/*ajax form*/

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 toggleEvent(){
	var selectedOption = $("select.typespecify option:selected").val();
	if (selectedOption == 2){
		$("tr.activityHide").addClass("hidden");
		$("input.text.required.date").removeClass("required");
		}
	else{
		$("tr.activityHide").removeClass("hidden");
		$("input.text.date").addClass("required");
	}
}
	
function roomPreview(){
	itemID = 'roomID=' + $("#roomID option:selected").val();
	var resultF = '#roomRHpreview';
	$.ajax({
	  data: itemID,
	  url: 'http://'+document.location.host+'/includes/booking/generatePreview.php',
	  success: function(data) {
		$(resultF).html('<img src="http://'+document.location.host+'/images/loading.gif" height="20">');
		myTimer = $.timer(1000,function(){
			$(resultF).html(data); // hide the loading gif after 2 seconds
		});
	  }
	});
}

$().ready(function() {
	
	$("input#large").click(function(){
		$("tr.toggleHide").toggleClass("hidden");
	});
	
	//ajax form submit
	
	$("div#form-error").hide();
	$("div#form-message").hide();
	$("div#form-success").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
			// other available options: 
			//url:       url         // override for form's 'action' attribute 
			//type:      type        // 'get' or 'post', override for form's 'method' attribute 
			//dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
			//clearForm: true        // clear all form fields after successful submit 
			//resetForm: true        // reset the form after successful submit 
	 
			// $.ajax options can be used here too, for example: 
			//timeout:   3000 
		}; 

	// 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); 

	    // jqForm is a jQuery object encapsulating the form element.  To access the 
	    // DOM element for the form do this: 
	    // var formElement = jqForm[0]; 
		
		$("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
		});

	    // here we could return false to prevent the form from being submitted; 
	    // returning anything other than false will allow the form submit to continue 
	    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)
			});
		
	}
	
	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
		});
	
	
		$("#message-form input, #contact #message-form textarea").each(function(){
			$(this).blur(function(){
				if($(this).attr("value") == ""){
				var newVal = $(this).attr('rel');
				$(this).attr("value", newVal);
				}
			});
		});
	
		$("#message-form input, #contact #message-form textarea").each(function(){
			$(this).focus(function(){
				var oldVal = $(this).attr('rel');
				if($(this).attr("value") == oldVal){
				$(this).attr("value", "");
				}
			});
		});
		
		
		});
/*ajax form*/


