// Define global variables
var SEARCHANY     = 1;
var SEARCHALL     = 2;
var SEARCHURL     = 4;
var searchType  = '';
var showMatches   = 10;
var currentMatch  = 0;
var copyArray   = new Array();
var inputEntry	='';

// Determine the type of search, and make
// sure the user has entered something
function validate(entry) {
	if(entry.length == 0){
		alert("Please enter string to search");
		document.forms[0].string.focus();
		return false;
	}
	inputEntry = entry;
  if (entry.charAt(0) == "+") {
    entry = entry.substring(1,entry.length);
    searchType = SEARCHALL;
    }
  else if (entry.substring(0,4) == "url:") {
    entry = entry.substring(5,entry.length);
    searchType = SEARCHURL;
    }
  else { searchType = SEARCHANY; }
  while (entry.charAt(0) == ' ') {
    entry = entry.substring(1,entry.length);
    document.forms[0].string.value = entry;
    }
  while (entry.charAt(entry.length - 1) == ' ') {
    entry = entry.substring(0,entry.length - 1);
    document.forms[0].string.value = entry;
    }
  convertString(entry);
  }

// Put the search terms in an array and
// and call appropriate search algorithm
function convertString(reentry) {
  var searchArray = reentry.split(" ");
  if (searchType == (SEARCHANY | SEARCHALL)) { requireAll(searchArray); }
  else { allowAny(searchArray); }
  }

// Define a function to perform a search that requires
// a match of any of the terms the user provided
function allowAny(t) {
  var findings = new Array(0);
  for (i = 0; i < profiles.length; i++) {
    var compareElement  = profiles[i].toUpperCase();
    if(searchType == SEARCHANY) {
	var refineElement  = compareElement.substring(0,compareElement.indexOf('|HTTP'));
    }else { 
	var refineElement = compareElement.substring(compareElement.indexOf('|HTTP'), compareElement.length);
    }
    for (j = 0; j < t.length; j++) {
      var compareString = t[j].toUpperCase();
      if (refineElement.indexOf(compareString) != -1) {
        findings[findings.length] = profiles[i];
        break;
        }
      }
    }
  verifyManage(findings);
  }

// Define a function to perform a search that requires
// a match of all terms the user provided
function requireAll(t) {
  var findings = new Array();
  for (i = 0; i < profiles.length; i++) {
    var allConfirmation = true;
    var allString       = profiles[i].toUpperCase();
    var refineAllString = allString.substring(0,allString.indexOf('|HTTP'));
    for (j = 0; j < t.length; j++) {
      var allElement = t[j].toUpperCase();
      if (refineAllString.indexOf(allElement) == -1) {
        allConfirmation = false;
        continue;
        }
      }
    if (allConfirmation) {
      findings[findings.length] = profiles[i];
      }
    }
  verifyManage(findings);
  }

// Determine whether the search was successful
// If so print the results; if not, indicate that, too
function verifyManage(resultSet) {
  //alert(resultSet);
  if (resultSet.length == 0) { noMatch(); }
  else {
    copyArray = resultSet.sort();
    formatResults(copyArray, currentMatch, showMatches);
    }
  }

// Define a function that indicates that the returned no results
function noMatch() {
  document.all["searchResults"].innerHTML = "";
    document.all["searchResults"].innerHTML += '<TABLE WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLSPACING="1"><TR><TD VALIGN=TOP WIDTH="27%"><FONT FACE="Arial">To search...</FONT><TD VALIGN=TOP WIDTH="39%"><FONT FACE="Arial">Enter...</FONT><TD VALIGN=TOP WIDTH="33%"><FONT FACE="Arial">For example..</FONT></TR><TR><TD COLSPAN="3"><HR NOSHADE WIDTH="100%"></TD></TR><TR><TD VALIGN=TOP WIDTH="27%"><FONT FACE="Arial">Any terms</FONT><TD VALIGN=TOP WIDTH="39%"><FONT FACE="Arial">words with spaces</FONT><TD VALIGN=TOP WIDTH="33%"><font face="Arial">APA Optronics (I) Pvt. Ltd.</font></TR><TR><TD VALIGN=TOP WIDTH="27%"><FONT FACE="Arial">By URL</FONT><TD VALIGN=TOP WIDTH="39%"><FONT FACE="Arial">url:[url portions with spaces]</FONT><TD VALIGN=TOP WIDTH="33%"><FONT FACE="Arial">url:profiler</FONT></TR><TR><TD COLSPAN="3" align="center"><HR NOSHADE WIDTH="100%"></TD></TR></TABLE><TABLE WIDTH=90% BORDER=0 ALIGN=CENTER><TR><TD VALIGN=TOP><FONT FACE=Arial><B>' +
    '"' + document.forms[0].string.value +
    '" returned no results.<HR NOSHADE WIDTH=100%></TD></TR></TABLE>';

  document.forms[0].string.select();
  }

// Define a function to print the results of a successful search
function formatResults(results, reference, offset) {
	var currentRecord = results.length;
  document.all["searchResults"].innerHTML = "";
  document.all["searchResults"].innerHTML += '<TABLE WIDTH="100%" BORDER="0" ALIGN="CENTER" CELLSPACING="1"><TR><TD VALIGN=TOP WIDTH="27%"><FONT FACE="Arial">To search...</FONT><TD VALIGN=TOP WIDTH="39%"><FONT FACE="Arial">Enter...</FONT><TD VALIGN=TOP WIDTH="33%"><FONT FACE="Arial">For example..</FONT></TR><TR><TD COLSPAN="3"><HR NOSHADE WIDTH="100%"></TD></TR><TR><TD VALIGN=TOP WIDTH="27%"><FONT FACE="Arial">Any terms</FONT><TD VALIGN=TOP WIDTH="39%"><FONT FACE="Arial">words with spaces</FONT><TD VALIGN=TOP WIDTH="33%"><font face="Arial">APA Optronics (I) Pvt. Ltd.</font></TR><TR><TD VALIGN=TOP WIDTH="27%"><FONT FACE="Arial">By URL</FONT><TD VALIGN=TOP WIDTH="39%"><FONT FACE="Arial">url:[url portions with spaces]</FONT><TD VALIGN=TOP WIDTH="33%"><FONT FACE="Arial">url:profiler</FONT></TR><TR><TD COLSPAN="3" align="center"><HR NOSHADE WIDTH="100%"></TD></TR></TABLE><TABLE WIDTH=100% BORDER=0 ALIGN=CENTER CELLPADDING=0><TR><TD>' +
    '</TD></TR><TR><TD VALIGN=TOP align=left><FONT FACE=Arial><B>' +
    'Search Query: '+inputEntry+'<BR>\n' +
    'Search Results: '+results.length + '<BR><BR></FONT>' +
    '<FONT FACE=Arial SIZE=-1><B>' + '\n\n<!-- Begin result set //-->\n\n\t';
  if (searchType == SEARCHURL) {
    for (var i = reference; i < currentRecord; i++) {
      var divide = results[i].split("|");
      //alert(divide[2]);
      document.all["searchResults"].innerHTML += '\t<table><tr><td>' + '<A HREF="' + divide[2] + '">' + divide[2] + '</A>' +
        '<br><br>' + '' + divide[1].substring(0, 200) + '...</td></tr></table><br>';
      }
    }
  else {
    for (var i = reference; i < currentRecord; i++) {
      var divide = results[i].split('|');
      var sss = ""+divide[2]+"";
      //alert(sss.substring(((sss.length)-4),sss.length););
      if(sss.substring(sss.length-4,sss.length) == "_new")
      {
      document.all["searchResults"].innerHTML += '\n\n\t<table><tr><td>' + '<A HREF="' + sss.substring(0,sss.length-4) + '" target="_blank">' + divide[0] + '</A>' +
        '<br><br>' + '' + divide[1].substring(0, 200) + '...</td></tr></table><br>';
        }else{
      document.all["searchResults"].innerHTML += '\n\n\t<table><tr><td>' + '<A HREF="' + divide[2] + '">' + divide[0] + '</A>' +
        '<br><br>' + '' + divide[1].substring(0, 200) + '...</td></tr></table><br>';        
        }
      }
    }
  document.all["searchResults"].innerHTML += '\n\t\n\n<!-- End result set //-->\n\n';
  document.all["searchResults"].innerHTML += '<HR NOSHADE WIDTH=100%></TD>\n</TR>\n</TABLE>\n';

  document.forms[0].string.select();
  document.forms[0].string.focus();
  }