function addQuate(value) {
	if (isEmpty(value))
		return value;

	if (value.match(/\W/))
		return '"' + value + '"';

	return value;  
}

function search() {
	var openInNewWindow = "";
	var resultAmount = "";
	
	if (isEmpty($("input[name=andWords]").val()) && isEmpty($("input[name=phraseWords]").val()) && isEmpty($("input[name=orWords]").val()) && isEmpty($("input[name=siteWords]").val())) {
		if (!isEmpty($("input[name=notWords]").val()) && isEmpty($("input[name=notSiteWords]").val())) {
			$("#errMsg").html('You\'ve mentioned only a "without the words" keyword. Please add a keyword or a site name to search for');
		}
		else if (isEmpty($("input[name=notWords]").val()) && !isEmpty($("input[name=notSiteWords]").val())) {
			$("#errMsg").html('You\'ve mentioned only a "without site" site name. Please add a keyword or a site name to search for');
		}
		else {
			$("#errMsg").html('Please mention a keyword or a site name you wish to search for');
		}
	}
	else {
		if ($("input[type=checkbox]").attr("checked")) {
			openInNewWindow = "&open=new";
		}
		else {
			openInNewWindow = "&open=same";
		}
		
		resultAmount = $("#projectAmount > option:selected").val();
		
		resultAmount = "l=" + resultAmount; 
		
		window.location.href = getSearchURL($("#queryText").text(), resultAmount + openInNewWindow);
	}
}

$(document).ready(function(){
	initSearchBar();
	
	$("#btnClear").click(function(e) {
		$("input[type=text]").val("");
		$("#projectAmount > option[value=10]").attr("selected", "selected");
		$("input[type=checkbox]").removeAttr("checked");
		$("#queryText").text("");
		
		e.preventDefault();
	});
	
	$("div.middle > input").keypress(function(e) {
		if (e.which == 13)
		{
			search();
			e.preventDefault();
		}
	});

	$("#btnSearch").click(function(e) {
		search();
		e.preventDefault();
	});
	
	var calcQueryFunc = function() {
		var newText = "";
		var tempText;
		
		tempText = jQuery.trim($("input[name=andWords]").val());
		if (tempText != "")
		{
			newText += " " + tempText;
		}

		tempText = jQuery.trim($("input[name=phraseWords]").val());
		if (tempText != "")
		{
			newText += " \"" + tempText + "\"";
		}

		tempText = jQuery.trim($("input[name=orWords]").val());
		if (tempText != "")
		{
			newText += ' (' + tempText.split(/ +/).join(' OR ') + ')';
		}

		tempText = jQuery.trim($("input[name=notWords]").val());
		if (tempText != "")
		{
			newText += ' NOT ' + tempText.split(/ +/).join(' NOT ');
		}

		tempText = jQuery.trim($("input[name=siteWords]").val());
		if (tempText != "")
		{
			newText += ' site:' + addQuate(tempText);
		}

		tempText = jQuery.trim($("input[name=notSiteWords]").val());
		if (tempText != "")
		{
			newText += ' NOT site:' + addQuate(tempText);
		}
		
		if ($("#queryText").text() != jQuery.trim(newText)) {
			$("#errMsg").html("&nbsp;");
			$("#queryText").text(jQuery.trim(newText));
		}
	}
	
	$("input[type=text]").keyup(calcQueryFunc);
	calcQueryFunc();
});

