
// ---------- script properties ----------


var results_location = "results.htm";


// ---------- end of script properties ----------


function search_form(jse_Form) {
	if (jse_Form.d.value.length > 0) {
		//document.cookie = "d=" + escape(jse_Form.d.value);
		
		//i changed this caz now am going to read the search text from querystring instead from cookie
		//window.location = results_location;
		//window.location = results_location + "?q=" + unescape(jse_Form.d.value);
		window.location = results_location + "?q=" + escape(jse_Form.d.value)
	}
}



//i did
function doSearch(searchText)
{

    searchText = unescape(searchText)
    if(searchText)
    {
        
        var m = 0;
        var searchArray;
        
        searchArray = searchText.replace(/"/g, '')//removes all occurances of quotes (") from the 'searchText'
        searchArray = unescape(searchArray)//decodes the string
            
        //if the sting is in between (<>) then search for the whole word
        if (searchArray.charAt(0) == '<' && searchArray.charAt(searchArray.length - 1) == '>'){
            searchArray = searchArray.replace(/[\<\>]/g,'');
            m = 1;
        }    
        else {//else search for each words
            searchArray = searchArray.split(/\s/);// decodes the word and splits with space
            m = 0;
        }
        
        var bodyText = document.body.innerHTML;
          
        if (m == 0)
        { 
            for (var i = 0; i < searchArray.length; i++) {
                //alert(searchArray[i])
                bodyText = doHighlight(bodyText, searchArray[i]);

            }
        }
        else if (m == 1)
            bodyText = doHighlight(bodyText, searchArray);
           
          
        document.body.innerHTML = bodyText;
         
    }
    
     return true;
}




/*
 * This is the function that actually highlights a text string by
 * adding HTML tags before and after all occurrences of the search
 * term. You can pass your own tags if you'd like, or if the
 * highlightStartTag or highlightEndTag parameters are omitted or
 * are empty strings then the default <font> tags will be used.
 */
function doHighlight(bodyText, searchTerm, highlightStartTag, highlightEndTag) 
{
  // the highlightStartTag and highlightEndTag parameters are optional
  if ((!highlightStartTag) || (!highlightEndTag)) {
    highlightStartTag = "<font style='color:blue; background-color:yellow;'>";
    highlightEndTag = "</font>";
  }
  
  // find all occurences of the search term in the given text,
  // and add some "highlight" tags to them (we're not using a
  // regular expression search, because we want to filter out
  // matches that occur within HTML tags and script blocks, so
  // we have to do a little extra validation)
  var newText = "";
  var i = -1;
  var lcSearchTerm = searchTerm.toLowerCase();
  var lcBodyText = bodyText.toLowerCase();
    
  while (bodyText.length > 0) {
    i = lcBodyText.indexOf(lcSearchTerm, i+1);
    if (i < 0) {
      newText += bodyText;
      bodyText = "";
    } else {
      // skip anything inside an HTML tag
      if (bodyText.lastIndexOf(">", i) >= bodyText.lastIndexOf("<", i)) {
        // skip anything inside a <script> block
        if (lcBodyText.lastIndexOf("/script>", i) >= lcBodyText.lastIndexOf("<script", i)) {
          newText += bodyText.substring(0, i) + highlightStartTag + bodyText.substr(i, searchTerm.length) + highlightEndTag;
          bodyText = bodyText.substr(i + searchTerm.length);
          lcBodyText = bodyText.toLowerCase();
          i = -1;
        }
      }
    }
  }
  
  return newText;
}


function getCookie(name) { 
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end)); 
}




//
//return the queryString.
//paramName parameter is the paramater in the querystring collection for which we need to get the value.
//
function QueryString(paramName) 
{
    var gotValue = false
    var queryStrs = window.location.search.substring(1);
    var vars = queryStrs.split("&");
    for (var i=0;i<vars.length;i++)
    {
        var varValue = vars[i].split("=");
        if (varValue[0].toUpperCase() == paramName.toUpperCase()) 
        {
            gotValue = true
            return varValue[1];
        }
    } 
    if (gotValue == false)
        {return ""}
}

