// JavaScript Document

// to preload images
// example how to call the function: $.preloadImages("image1.gif", "/path/to/image2.png", "some/image3.jpg");

(function($){
	$.preloadImages = function()
	{
		for(var i = 0; i<arguments.length; i++)
			{
				  $("<img>").attr("src", arguments[i]);
			}
		};
	}) (jQuery);


(function($)
  {
	$.formSender = function(selection)
	{
	  
	  var options =
	   {
		   //timeout:   30,
		  beforeSend: function()
		  {
		  	$(selection + " form").hide();
		  	$(selection + " p.infoText").html("<img src='images/ui/hat_blinking.gif' width='20' height='11' alt='hat' /> Validating data...");
		  },
		  error: function()
		  {
			  $(selection + " p.infoText").html("<span class='error'>An error occured while trying to submit data.</span><br />Please try later again.");
			  $(selection + " form").show();
		  },
		  success: function(data)
		  {
			  $(selection).html(data);
			  $.formSender(selection);
			  //$(selection + " form").show();
		  }
		};
	  
	  $(selection + " form").not(".noAjax").submit(function()
	  {
		$(this).ajaxSubmit(options);
		return false;
	});
  };
}) (jQuery);


(function($)
	{
	  $.formValidation = function(selection)
	  {
		$(selection + " form").submit(function()
		{
		  $(selection + " form").hide();
		  $(selection + " p.infoText").html("<img src='images/ui/hat_blinking.gif' width='20' height='11' alt='hat' /> Validating form...");
		  var inputs =[];
		  
		  $(":input", this).each(function() {
			inputs.push(this.name + "=" + escape(this.value));
		})
		  
		  $.ajax(
		  {
			data: inputs.join("&"),
			url: this.action,
			type: this.method,
			//timeout: 50,
			beforeSend: function()
			{
			
			},
			error: function()
			{
				$(selection + " p.infoText").html("<span class='error'>An error occured while trying to submit data.</span><br />Please try later again.");
				$(selection + " form").show();
			},
			success: function(data)
			{
				$(selection + " p.infoText").html(data);
			}
		  })
		  
		  return false;
	  });
	};
}) (jQuery);


(function($){
$.formFieldFocusBlur = function(selection)
{
	$(selection + ' input[type="text"]').addClass("idleField");  
    
	$(selection + ' input[type="text"]').focus(function () {
		$(this).removeClass("idleField").addClass("focusField");  
        if (this.value == this.defaultValue){  
            this.value = '';  
        }else{  
            //this.select();
		}
		if (this.id == "password"){  
            this.type = "password";
			this.select();
        }else{  
            //this.select();
		}
	   $(this).blur(function() {
			$(this).removeClass("focusField").addClass("idleField");  
			if (this.value == ''){
				if (this.type == 'password'){
					this.type = "text";
				}
				this.value = this.defaultValue;
			}
		});
	 });
};
}) (jQuery);
