jQuery.fn.TxtSwap = function(content) {
  this.each(function(){
		var $this = $(this);
		if (typeof content == 'undefined') {
			if (typeof this.title != 'undefined' && this.title != '' ) {
				$this.content = this.title;
			}
		}
		else {
			$this.content = content;        
		}
	$this.click(function(){
			this.value = '';
		});
		$this.blur(function(){
			if(this.value == '')       
				this.value = $this.content;
		});
  });
};
var validationTimeout;
$(document).ready(function() 
{   
	$("div#login form.inner input[name='username']").TxtSwap("UserName");
	$("div#newsletter form.inner input[type='text']").TxtSwap("Your@Email.address.com");
	$("input,textarea,select,button").filter("[rel*='validate:1']").blur(function() {
		var $this = $(this);
		$x='';
		$validate = eval("{"+$this.attr("rel")+"}");
		$x= {
		  field:$this.attr("name"),
		  method:$validate[0],
		  target:$validate[1],
		  name:$validate[2],
		  value:escape($this.val())
		};
		//$x+= $this.attr("name")+'={"method":"'+$validate[0]+'","target":"'+$validate[1]+'","name":"'+$validate[2]+'","value":"'+escape($this.val())+'"}&';
		if (typeof validateTimeout != 'undefined') {
			validateTimeout.abort();
			validateTimeout = undefined;
		}
		validateTimeout = $.post("validation.php",$x,
			function(data){
				if (!data.valid) {
					if (typeof data.target != "undefined" && data.target != null && data.target.length > 0) {
						$(data.target).text(data.message);
					}
					$this.addClass('error');
				}
				else {
					$this.removeClass('error')
					if (typeof data.target != "undefined" && data.target != null && data.target.length >0) {
						$(data.target).text("");
					}
				}
			}
			,'json'
		);
	});
	$("input,textarea,select,button").filter("[rel*='validate:2']").blur(function() {
		var $this = $(this);
		$validate = eval("{"+$this.attr("rel")+"}");
		$against = $($validate[0]);
		$errorOut = $($validate[1]);
		if ($this.val() != $against.val()) {
			$this.addClass('error');
			$errorOut.text($validate[2]);
		}
		else {
			$this.removeClass('error');
			$errorOut.text('');
		}
	});
																				 
	$("#retailers a").click(
		function(){
			$("#retailers").toggleClass("active");
			}
		);
																		 
	$(".search_results_pages").click(
		function(){
			$("#the_results_pages").slideToggle();
			}
		);
																		 
	$(".search_results_sections").click(
		function(){
			$("#the_results_sections").slideToggle();
			}
		);
									 
	$(".search_results_users").click(
		function(){
			$("#the_results_users").slideToggle();
			}
		);

		$("#cat_id").change(function () {
		  var str = "";
		  $("#cat_id option:selected").each(function () {
				//  4 - Home Inspectors
				// 24 - Domestic          
				if (($(this).val() == 4 || $(this).val() == 24) && $('#certification').val() == '')
				{
					$("#certification").addClass('error');
					$("#certification_warning").html('You must enter your certification number');
				}
				if($("#cat_id").val() != 4  && $("#cat_id").val() != 24  && $('#certification').val() != '')
				{
					$("#certification").removeClass('error');
					$("#certification_warning").html('');
					$("#certification").val(''); 
				}
			  });
		}).change();
		
	if($("#cat_id").val() != 4  && $("#cat_id").val() != 24)
	{
			$("#certification").removeClass('error');
			$("#certification_warning").html(''); 
			$("#certification").val(''); 
	}
						 
	 $('a.show_retailer').click(function(){
	 $('#retailer').slideDown("slow");
	 $('a.show_retailer').hide(); 
	 $('a.hide_retailer').show();
   });
	 $('a.hide_retailer').click(function(){
		$('#retailer').slideUp("normal");
	 $('a.hide_retailer').hide();
	 $('a.show_retailer').show(); 
   });
   
   $('a.its_me').click(function(){
		$('#its_my_company').slideToggle("normal");  
   });
   
   
   /*
	 $('.view-on-google').click(function(){
	 $('#google_map').toggle();
   });
   */
   /*$("input.text, textarea.text").focusFields("#d40000", 1, "#f3e784", );*/
   
   $("#login h3").click( function(){ $("#login").toggleClass("collapsed");} ) ;
   
   $("#toggle_search").click( function(){ 
	$("#search_wrapper").slideToggle();
	/*$("#toggle_search").slideToggle();*/
	} ) ;
   
 /*  $("input.text, textarea.text").focusFields("#fbb894", 1, "#fef1ea");    */
   
   
	/*$("input:text.text, textarea.text, input:password.text").each(function(){
		if(this.value == '')
			this.value = this.title;
	});
	$("input:text.text, textarea.text, input:password.text").click(function(){
		if(this.value == this.title)
			this.value = '';
	});
	$("input:text.text, textarea.text, input:password.text").blur(function(){
		if(this.value == '')
			this.value = this.title;
	});*/
	//$("input:text.text, textarea.text, input:password.text").TxtSwap();
	$("input:image, input:button, input:submit").click(function(){
		$(this.form.elements).each(function(){
			if(this.type =='text' || this.type =='textarea' || this.type =='password' ){
				if(this.value == this.title && this.title != ''){
					this.value='';
				}
			}
		});
	});
});