var selectedSearch = "";

// Check to see if this is a Netscape-based browser:
var NS4 = false;
if (navigator.appName == "Netscape") {
 NS4 = true;
}

if (NS4) document.captureEvents(Event.KEYPRESS);
document.onkeypress = doKey;

function doKey(e) {
  var whichASC = (NS4) ? e.which : event.keyCode;
  if (whichASC == 13) {

    if (selectedSearch == "globalsearch") {
      globalSearch();
    }

	if (selectedSearch == "globalsearchsubpage") {
      if(doGlobalSearchSubPage()) {
     	 document.forms[0].method='get';
		   document.forms[0].submit()
		  }
  }

    if (selectedSearch == "FRsearch") {
        globalFRSearch();
    }

    if (selectedSearch == "officesearch") {
    	doOfficeLocatorSearch();
    }

    if (selectedSearch == "glossarysearch") {
      if(doGlossarySearch()) {
	    	document.forms[0].method='get';
		    document.forms[0].submit()
		  }
    }
    if (selectedSearch == "secondaryglossarysearch") {
      if(doSecondaryGlossarySearch()) {
		   document.forms[0].method='get';
		   document.forms[0].submit()
		  }
    }

    if (selectedSearch == "globalresearch") {
      if(doGlobalReSearch()) {
		   document.forms[0].method='get';
		   document.forms[0].submit()
	    }
    }

    if (selectedSearch == "newglobalsearch") {
      if(doNewGlobalSearch()) {
		   document.forms[0].method='get';
		   document.forms[0].submit()
	    }
    }

	if (selectedSearch == "apllogin") {
	  doAPLLogin();
  }
 }
}

function globalSearch() {
	if (doGlobalSearch()) {
		document.forms[0].method='get';
		document.forms[0].submit();
	}
}

function globalFRSearch() {
	if (doFRDirectorySearch()) {
		document.forms[0].method='get';
		document.forms[0].submit();
	}
}

function globalNewSearch() {
	if (doNewGlobalSearch()) {
		document.forms[0].method='get';
		document.forms[0].submit();
  }
}

function globalReSearch() {
	if (doGlobalReSearch()) {
		document.forms[0].method='get';
		document.forms[0].submit();
	}
}

function globalSubPageSearch() {
	if (doGlobalSearchSubPage()) {
   	document.forms[0].method='get';
		document.forms[0].submit();
	}
}

function doGlobalSearch() {

  var temp = "http://www.nmfn.com/global--searches--searchresults";

  if (document.forms[0].GlobalSearchText.value == "" || document.forms[0].GlobalSearchText.value == null) {
    alert("Please enter a word or word(s) to search for.");
    document.forms[0].GlobalSearchText.focus();
    return false;
  }

  if (!NS4) {
     if(document.forms[0].HiddenAgentURL != null){
			if(document.forms[0].HiddenAgentURL.value != ""){
			   temp = document.forms[0].HiddenAgentURL.value + temp;
			} else {
			   temp = "http://www.nmfn.com/tn"  + temp;
			}
		 } else {
			 temp = "http://www.nmfn.com/tn"  + temp;
		 }
	  //document.forms[0].SearchText.value = document.forms[0].GlobalSearchText.value;
	  //document.forms[0].SearchType.value = 'searchcomponent';
	 
	  //for Netscape navigator the model_trigger comes through as ++ instead of "" like it should
	  //if this is a problem the form can be submitted with enter in Netscape in other ways.
	  // document.forms[0].model_trigger.value = '';
	  document.forms[0].action = temp;
	  return true;
  }

  if (NS4) {
   //for FR sites

	   if (document.forms[0].HiddenAgentURL) {
			 temp = document.forms[0].HiddenAgentURL.value + temp;
	   	 document.forms[0].SearchText.value = document.forms[0].GlobalSearchText.value;
		 } else {  //NMFN
		   temp = "http://www.nmfn.com/tn"  + temp;
		   document.forms[0].SearchText.value = document.forms[0].GlobalSearchText.value;
     }

    document.forms[0].SearchType.value = 'searchcomponent';
	  //for Netscape navigator the model_trigger comes through as ++ instead of "" like it should
 	  //if this is a problem the form can be submitted with enter in Netscape in other ways.
		//document.forms[0].model_trigger.value = '';
	  document.forms[0].action = temp;

    return true;
  }

}

// for search to work on the sub pages with NS 4 as it does not accept getElementById()
function doGlobalSearchSubPage() {

 var temp = "http://www.nmfn.com/global--searches--searchresults";

 if (document.forms[0].GlobalSearchText.value == "" || document.forms[0].GlobalSearchText.value == null) {
  alert("Please enter a word or word(s) to search for.");
  document.forms[0].GlobalSearchText.focus();
  return false;
 }

  // for IE
  if (!NS4) {
  //for FR sites
	  if (document.forms[0].HiddenAgentURL != null){
 		 temp = document.forms[0].HiddenAgentURL.value + temp;
	  }
	  else{  //NMFN
		 temp = "http://www.nmfn.com/tn"  + temp;
	  }
      document.forms[0].SearchText.value = document.forms[0].GlobalSearchText.value;
      document.forms[0].SearchType.value = 'searchcomponent';
	  //for Netscape navigator the model_trigger comes through as ++ instead of "" like it should
	  //if this is a problem the form can be submitted with enter in Netscape in other ways.
	  //document.forms[0].model_trigger.value = '';
	  document.forms[0].action = temp;
	  return true;
  }

  if (NS4) {
    //for FR sites
     if (document.forms[0].HiddenAgentURL != null){
      temp = document.forms[0].HiddenAgentURL.value + temp;
     }
     else {  //NMFN
      temp = "http://www.nmfn.com/tn"  + temp;
     }
	  document.forms[0].SearchText.value = document.forms[0].GlobalSearchText.value;
	  document.forms[0].SearchType.value = 'searchcomponent';
	  //for Netscape navigator the model_trigger comes through as ++ instead of "" like it should
	  //if this is a problem the form can be submitted with enter in Netscape in other ways.
	  //document.forms[0].model_trigger.value = '';
	  document.forms[0].action = temp;
	  return true;
  }

}

function doAPLLogin() {
	if(!NS4){
	if (preview != "T"){
	    var userid = document.forms[0].USER.value;
		var pass = document.forms[0].PASSWORD.value;
	    if ((userid == "") || (pass == "" )){
			alert("Please enter a user id and password.");
		} else {
			winControl("apl","http://www.nmfn.com/tnetwork/apl_login.html?USER=" + userid + "&PASSWORD=" + pass);
			document.forms[0].USER.value = "";
			document.forms[0].PASSWORD.value = "";
		}
  }
}

if (NS4) {
	if (preview != "T"){
	    var userid = document.forms[0].USER.value;
		var pass = document.forms[0].PASSWORD.value;
	    if ((userid == "") || (pass == "" )) {
			alert("Please enter a user id and password.");
		} else {
			winControl("apl","http://www.nmfn.com/tnetwork/apl_login.html?USER=" + userid + "&PASSWORD=" + pass);
			document.forms[0].USER.value = "";
			document.forms[0].PASSWORD.value = "";
		}
  }
}

}

function doNameSearch() {
	if (document.forms[0].nameSearch.value.length > 0) {
		pCode = document.forms[0].nameSearch.value;
//		if (! test(pCode) && !/\d/.test(pCode)) {
		if (!/\d/.test(pCode)) {
		    document.forms[0].SearchText.value = document.forms[0].nameSearch.value;
		    document.forms[0].SearchType.value = 'searchcomponent';
			//document.forms[0].model_trigger.value = '';
			document.forms[0].action = "http://www.nmfn.com/tn/global--searches--frdirectorysearchresults";
			return true;
		} else {
			alert("Please enter alpha characters only.");
			document.forms[0].nameSearch.focus();
			return false;
		}
	} else {
		alert("Please enter at least the first character of a last name.");
		document.forms[0].nameSearch.focus();
		return false;
	}
}

function filterNumeric(thisControl, controlName) {
	if (/\d/.test(thisControl.value)){
		alert("The " + controlName + " cannot contain numbers.");
		thisControl.focus();
		return false;
	}
	return true;
}


function validateZip(thisControl) {
	if (thisControl.value.length > 0) {
		if ((thisControl.value.length < 5) || !(isNumeric(thisControl.value))) {
			alert("Please enter a 5-digit ZIP Code.");
			thisControl.focus();
			return false;
		}
		return true;
	}
}

function doAlphaSearch(letter) {
	//If user user clicks on alpha search component from FRDirectory Page..
	if(typeof(document.forms[0].FRName)=="object" && typeof(document.forms[0].LOCity)=="undefined")
	window.location = "http://www.nmfn.com/tn/global--searches--frdirectorysearchresults?SearchText=E" + letter + "&BeginIndex=1&SearchAgainTarget=" + "http://www.nmfn.com/tn/global--forms--fin_rep_directory_pg";

	//If user clicks on alpha search component from Results Page(Regardless of what screen they came from)
       var docLnk = document.links;
    for (var i = 0; i < docLnk.length; i++) {
        if (docLnk[i].text) {
		//Netscape
            if (docLnk[i].text == "Search Again"){
             	if(docLnk[i].toString().indexOf("http://www.nmfn.com/tn/global--forms--fin_rep_directory_pg")!=-1)
			    window.location = "http://www.nmfn.com/tn/global--searches--frdirectorysearchresults?SearchText=E" + letter + "&BeginIndex=1&SearchAgainTarget=" + "http://www.nmfn.com/tn/global--forms--fin_rep_directory_pg";
			else
			    window.location = "http://www.nmfn.com/tn/global--searches--frdirectorysearchresults?SearchText=E" + letter + "&BeginIndex=1&SearchAgainTarget=" +"http://www.nmfn.com/tn/global--forms--local_office_locator_pg";

		}

        } else if (docLnk[i].innerText) {
		//IE
            if (docLnk[i].innerText == "Search Again") {
			if(docLnk[i].toString().indexOf("http://www.nmfn.com/tn/global--forms--fin_rep_directory_pg")!=-1)
			    window.location = "http://www.nmfn.com/tn/global--searches--frdirectorysearchresults?SearchText=E" + letter + "&BeginIndex=1&SearchAgainTarget=" + "http://www.nmfn.com/tn/global--forms--fin_rep_directory_pg";
			else
			    window.location = "http://www.nmfn.com/tn/global--searches--frdirectorysearchresults?SearchText=E" + letter + "&BeginIndex=1&SearchAgainTarget=" +"http://www.nmfn.com/tn/global--forms--local_office_locator_pg";

		}
        }
    }

}

function setSearchAgainTarget() {
	if (typeof(document.forms[0].LOCity)=="undefined")
		document.forms[0].SearchAgainTarget.value = "http://www.nmfn.com/tn/global--forms--fin_rep_directory_pg";
	else
		document.forms[0].SearchAgainTarget.value = "http://www.nmfn.com/tn/global--forms--local_office_locator_pg";
}

function doOfficeLocatorSearch() {
	var city = document.forms[0].LOCity.value;
	var street = document.forms[0].LOStreet.value;
	var state = document.forms[0].LOState.options[document.forms[0].LOState.selectedIndex].value;
	var zip = document.forms[0].LOZip.value;
	var radius = document.forms[0].LORadius.options[document.forms[0].LORadius.selectedIndex].value;
	var url = mapquestURL;  //mapquest url is defined in the office loc page - set in ini file.

	setSearchAgainTarget();

	if ((city != "") || (state != "")) {
		if ((city == "") && (state != "")) {
			alert ('Please enter the City.');
		}	else if ((city != "") && (state == ""))	{
			alert ('Please enter the State.');
		}	else if (filterNumeric(document.forms[0].LOCity, "City"))	{
			if ((street == "") && (zip != "" ))	{
				if (validateZip(document.forms[0].LOZip))	{
					url = url + "?link=autosearch&screen=find&closestn=20&closestprox=1&miles=" + radius + "&zip=" + zip;
					winControl("officeLocator", url);
				}
			}	else {
				url = url + "?link=autosearch&screen=find&closestn=20&closestprox=1&miles=" + radius + 					"&city=" + city + "&state=" + state  +  "&streetaddress=" + street;
				winControl("officeLocator", url);
			}
		}
	}	else if (zip != "" ) {
		if (validateZip(document.forms[0].LOZip)) {
			url = url + "?link=autosearch&screen=find&closestn=20&closestprox=1&miles=" + radius + "&zip=" + zip;
			winControl("officeLocator", url);
		}
	}	else {
		alert('Please complete the required fields');
	}

}

function doFRDirectorySearch() {
	var city = document.forms[0].FRCity.value;
	var state = document.forms[0].FRState.options[document.forms[0].FRState.selectedIndex].value;
	var zip = document.forms[0].FRZip.value;
	var name = document.forms[0].FRName.value;
	setSearchAgainTarget();

	if (zip != "") {
		if (!validateZip(document.forms[0].FRZip))
			return false;
	}	else if ((city != "") || (state != ""))	{
		if ((city == "") && (state != "")) {
			alert ('Please enter the City.');
			return false;
		}	else if ((city != "") && (state == ""))	{
			alert ('Please enter the State.');
			return false;
		}	else if (!filterNumeric(document.forms[0].FRCity,"City") )
			return false;
	}	else if (name != "") {
		if (!filterNumeric(document.forms[0].FRName, "Last Name"))
			return false;
	}	else {
		alert('Please complete the required fields');
		return false;
	}

	if((name!="") && (city!="") && (state!="") && (zip!="")){
	 document.forms[0].SearchText.value = "B"+zip+name;
	}	else if((name!="") && (city!="") && (state!="")){
	 document.forms[0].SearchText.value = "A"+city+state+name;
	}	else if((name!="") && (city=="") && (state=="") && (zip!="")){
	 document.forms[0].SearchText.value = "B"+zip+name;
	}	else if(zip!=""){
	 document.forms[0].SearchText.value = "D"+zip;
	}	else if((city!="") && (state!="")){
	 document.forms[0].SearchText.value = "C"+city+","+state;
	}	else if(name!="" && city=="" && state=="" && zip==""){
	 document.forms[0].SearchText.value = "E"+name;
	}

  if(document.forms[0].SearchText.value!="")
	document.forms[0].SearchText.value = document.forms[0].SearchText.value.toUpperCase();
	document.forms[0].SearchType.value = 'searchcomponent';
	//document.forms[0].model_trigger.value = '';
	document.forms[0].action = "http://www.nmfn.com/tn/global--searches--frdirectorysearchresults";
	return true;

}

function doGlossarySearch() {
	document.forms[0].SearchText.value = document.forms[0].GlossarySearchText.value;
	document.forms[0].SearchType.value = 'searchcomponent';
	//for Netscape navigator the model_trigger comes through as ++ instead of "" like it should
	//if this is a problem the form can be submitted with enter in Netscape in other ways.
	//document.forms[0].model_trigger.value = '';
	document.forms[0].action = localServlet;
	return true;
}

function doSecondaryGlossarySearch() {
	document.forms[0].SearchType.value = 'searchcomponent';
	document.forms[0].SearchText.value = document.forms[0].searchfield.value;
	//document.forms[0].model_trigger.value = '';
	document.forms[0].action = "http://www.nmfn.com/tn/global--searches--glossarysearchresults";
   return true;
}

function doGlobalReSearch() {
	document.forms[0].SearchType.value = 'refinedsearch';

  if (document.forms[0].GlobalReSearchText.value == "" || document.forms[0].GlobalReSearchText.value == null) {
   alert("Please enter a word or word(s) to search for.");
   document.forms[0].GlobalReSearchText.focus();
   return false;
  }

	if (document.forms[0].HiddenAgentURL) {
		document.forms[0].SearchText.value = document.forms[0].GlobalReSearchText.value;
	} else {
		document.forms[0].SearchText.value = document.forms[0].GlobalReSearchText.value;
	}
	document.forms[0].OriginalSearchText.value = originalsearchtext;
	document.forms[0].action = reSearchUrl;
  return true;
}

function doNewGlobalSearch() {

	document.forms[0].SearchType.value = 'searchcomponent';

  if (document.forms[0].NewGlobalSearchText.value == "" || document.forms[0].NewGlobalSearchText.value == null) {
   alert("Please enter a word or word(s) to search for.");
   document.forms[0].NewGlobalSearchText.focus();
   return false;
  }

	if (document.forms[0].HiddenAgentURL)	{
		document.forms[0].SearchText.value = document.forms[0].NewGlobalSearchText.value;
		document.forms[0].SearchText.value = document.forms[0].NewGlobalSearchText.value;
		document.forms[0].SearchType.value = 'searchcomponent';
		document.forms[0].BeginIndex.value =  document.forms[0].BeginIndex.value;
		document.forms[0].EndIndex.value =  document.forms[0].EndIndex.value;
		document.forms[0].Phase.value = document.forms[0].Phase.value;
	} else {
		document.forms[0].SearchText.value = document.forms[0].NewGlobalSearchText.value;
	}

	document.forms[0].action = newSearchUrl;
	return true;
}

function isNumeric(str) {
    var strInput  = str;
    var strLength = strInput.length;
    var goodStr   = true;

 for(var i=0; i<strLength ; i++) {
   var Char = strInput.substring(i,i+1);
   if (Char < "0"  || Char > "9") {
     return false;
    }
  }

 return true;

}
