var popupStatus = 0;

function updateSearch()
{
	 disablePopup();
	 $("#search_terms").val($("#spellChecker").text());
}


function loadPopup(){  

//loads popup only if it is disabled  

	if(popupStatus==0){  

		$("#backgroundPopup").css({  "opacity": "0.7"  });  

		$("#backgroundPopup").fadeIn("slow");  

		$("#popupContact").fadeIn("slow");  

		popupStatus = 1;  

	}  

}

//disabling popup with jQuery magic!  

function disablePopup(){  

//disables popup only if it is enabled  

	if(popupStatus==1){  

		$("#backgroundPopup").fadeOut("slow");  

		$("#popupContact").fadeOut("slow"); popupStatus = 0;  

	}  

} 

//centering popup  

function centerPopup(){  

//request data for centering  

	var windowWidth = document.documentElement.clientWidth;  

	var windowHeight = document.documentElement.clientHeight;  

	var popupHeight = $("#popupContact").height();  

	var popupWidth = $("#popupContact").width();  

//centering  

	$("#popupContact").css({ "position": "absolute", "top": windowHeight/2-popupHeight/2, "left": windowWidth/2-popupWidth/2 });  

//only need force for IE6  

  

	$("#backgroundPopup").css({  "height": windowHeight  });  

  

}  
$(document).ready(function(){	
						   
		if($("#advanced").val() != 1)
			$(".advanceOptions").hide();
		$(".showAdvance").click(function()
		{
			if($("#advanced").val() == 1)
				$("#advanced").val(0);
			else
				$("#advanced").val(1);
				
			$(".advanceOptions").slideToggle();
			
		})
		
		$("#submit_btn").click(function(){
				if($('.advanceOptions').is(':visible') == 0)
				{
					$('.advanceOptions :input', form).each(function() {
						var type = this.type;
						var tag = this.tagName.toLowerCase();
						if (type == 'text' || type == 'password' || tag == 'textarea')
						  this.value = "";
						else if (type == 'checkbox' || type == 'radio')
						  this.checked = false;
						else if (tag == 'select')
						  this.selectedIndex = 0;
					});
				}
		});

		$("#preview_btn").submit(function(){

			return false;

		});

		$("#preview_btn").click(function()

		{

		

			var form = $("form").serialize();

			var search_terms = $("#search_terms").val();

			//CONTINUE ONLY IF THE QUERY IS NOT EMPTY

			$("#preview_div").slideUp("slow");

			if (search_terms.length > 0)

			{

				//DISABLE THE BUTTONS AND SLIDE UP EXAMPLE DIV

				$("#loader").css({	"display":	"inline-block"	});

				$(".button").attr("disabled","disabled");

				$.ajax(

				{

					type: "POST",

					url: "http://itemalarm.com/newsearch/ajax",

					data: form,

					success: function(message)

					{	

					

							centerPopup();

							$("#preview_div").empty();

							$("#preview_div").append(message);

							$(".button").removeAttr('disabled');

							$("#loader").css({	"display":	"none"	});

							loadPopup();  

						

					}

				});s

			}

			else

			{

				$("#loader").css({	"display":	"none"	});

				$("#preview_div").empty();

				return false;

			}

		});

		$("#popupContactClose").click(function(){  

			disablePopup();  

		});
		

		//Click out event!  

		$("#backgroundPopup").click(function(){  

			disablePopup();  

		});  

});	