var naughty = {
  NaughtyWordAJAX : function(AJAXurl, imageurl, targeturl, formid, textid) {
    if (AJAXurl && imageurl && targeturl && formid && textid)
    {
        var param = $(textid).val();
        var result = null;
        if ($.trim($(textid).val())==="") {
            notification('Oops', 'Please enter a search term', imageurl);
            return;
        }

        $.ajax({
            type: 'GET',
            url: AJAXurl,
            data: {search: escape(param)},
            dataType: "json",
            success : function(html) {
                try {
                    //result = JSON.parse(html);
                    result = html;
                    }
                catch (e)
                    {
                    //On an error, just forward to the server error handling
                    $(formid).attr('action', targeturl);
                    $(formid).submit();
                    return;
                    }
                if (result != null && typeof(result) == "object" && result.naughty == "True")
                    {
                    //The search contains a naughty word. Tell the user to change it
                    notification('Oops', 'Please enter a search without naughty words', imageurl );
                    }
                else
                    {
                        //Modify the form to use the original URL
                        $(formid).attr('action', targeturl);
                        $(formid).submit();

                        //Add name to prefs if its not empty, for a regular search this is the query for now
                        searchquery = $.trim($('#kw').val());
                        if(searchquery !== "") {
                            // Set current name for the search submitted
                            $.Pref('lastsearchname', searchquery);
                            // Empty out the lastsearch to prevent naming mismatch
                            $.Pref('lastsearch', null);
                        }
                    }
                }
            });
    }
  },
  NaughtyWordAJAXAdvanced : function(AJAXurl, imageurl, targeturl, formid, text) {
    if (AJAXurl && imageurl && targeturl && formid)
    {
        var param = text;
        var result = null;
	var name = $('#searchname').val();

	if (naughty.checkSearchName(name)) {
            $.ajax({
              type: 'GET',
              url: AJAXurl,
              data: {search: escape(param)},
              dataType: "json",
              success : function(html) {
                  try {
                    //result = JSON.parse(html);
                    result = html;
                  }
                  catch (e)
                  {
                    //On an error, just forward to the server error handling
                    $(formid).attr('action', targeturl);
                    $(formid).submit();
                    return;
                  }
                  if (result != null && typeof(result) == "object" && result.naughty == "True")
                  {
                    //The search contains a naughty word. Tell the user to change it
                    notification('Oops', 'Please enter a search without naughty words', imageurl );
                  }
                  else
                  {
                      //Modify the form to use the original URL
                      $(formid).attr('action', targeturl);
                      $(formid).submit();
                      //Add name to prefs if its not empty
                      searchname = $.trim($('#searchname').val());
                      // Last search id is cleared here b/c it gets set when results page renders
                      if (searchname !== "") {
                          // Set current name for the search submitted
                          $.Pref('lastsearchname', searchname);
                          // Empty out the lastsearch to prevent naming mismatch
                          $.Pref('lastsearch', null);
                      }
                      //If no name was entered, empty out the lastsearchname, and clear last search id
                      else {
                          $.Pref('lastsearchname', null);
                          $.Pref('lastsearch', null);
                      }
                  }
              }
            });

	} else {
	    notification('Oops', 'Please give a valid name for the search', imageurl);
	}
    }
  },

  /*
  * Function to check if the search name meets the requirements.
  * @return true if the search name is valid; otherwise false.
  */
  checkSearchName : function(search_name) {
    var result = search_name.match(/[^a-zA-Z0-9_\-/\s]/);

    if (result != null) return false;
    else return true;
  }
};

