//<SCRIPT language=JavaScript1.1 type=text/javascript>
//<!--

// -------------------------------------------------------------------------------
// Function searchChecker()
//   When users enter a string into the "Keyword Search" box, this checks that at least 3 characters are entered before "posting" the form.
//   Parameter = name of form. [Note, the name of the field is hardcoded as "Query"
//  USE: <FORM NAME="phraseform" onsubmit="return searchChecker(this);" METHOD="get" ACTION="page.asp">
//       <INPUT TYPE="Text" NAME="Query" > 
//
function searchChecker(form)
{ 
if (form.Query.value == "" ) { alert ("You need to enter something to search for !"); return false; }
if (form.Query.value.length < 3  ) { alert ("Sorry, you will need to enter a longer phrase !"); return false; }
if (form.Query.value.indexOf("&") >= 0 || form.Query.value.indexOf("?") >= 0 || form.Query.value.indexOf(".") >= 0 || form.Query.value.indexOf("$") >= 0 || form.Query.value.indexOf("£") >= 0 || form.Query.value.indexOf("@") >= 0 || form.Query.value.indexOf(",") >= 0 || form.Query.value.indexOf("%") >= 0) { alert ("Sorry, we cannot search for special characters such as '&' "); return false; }
if (form.Query.value.indexOf("'") >= 0 )  { alert ("Sorry, we cannot search for text with an apostrophe"); return false; }
return true;
}

// -------------------------------------------------------------------------------
// Function fieldChecker()
//   When users select values from the 3 drop-down boxes, this checks that values for at least the first 2 boxes (Sector & Area) have been 
//    selected before "posting" the form. Returns "false" if the first 2 SELECT fields are left with a default value and when that default is blank.
//  USE: <FORM NAME="fieldlist" onsubmit="return fieldChecker(this);" METHOD="post" ACTION="page.asp">
//       <SELECT NAME="sectortype" id="sectortype1"> . . . . </SELECT>
//       <SELECT NAME="arealist" id="arealist1"> . . . . </SELECT>
//       <INPUT TYPE="Submit" NAME="infosubmit1" > 
//
function fieldChecker(form)
{ 
if (form.sectortype.options[0].selected && form.sectortype.options[0].text == "" ) { alert ("You did not choose a category to search in !"); return false; }
 else
 {
//   if (form.arealist.options[0].selected && form.arealist.options[0].text.length < 4 ) { alert ("You did not choose an area to search in !"); return false; }
   var chosenArea = form.arealist.selectedIndex
   var chosenSubArea 
   if (form.compasslist == null || form.compasslist.selectedIndex === null || form.compasslist.selectedIndex == undefined) { chosenSubArea = "" } else { chosenSubArea = form.compasslist.selectedIndex } 
   if (form.arealist.options[0].selected && form.arealist.options[chosenArea].text.length < 4 ) { alert ("You did not choose an area to search in !"); return false; }
    else
    { if ( chosenSubArea != "" )
           {              // This prevents a user from selecting a sub area when the main area is "AllAreas"
             if (form.compasslist.options[chosenSubArea].text.indexOf("AllSubAreas") < 0 && form.arealist.options[chosenArea].text.indexOf("AllAreas") >= 0 ) { alert ("You did not choose an area to search in !"); return false; }
           }
    }
 }
return true;
}

// -------------------------------------------------------------------------------
// Function validate()
//  When a user enters a string value, this checks that it contains valid characters - accented chars and punctuation are not accepted.
//  Param: field value to be tested.  Returns "yes" if valid chars, "no" if any char is not valid.
//  USE: <input type=text name="entry" onBlur="validate(this)">
//
function validate(field)
{
var valid = "abcdefghijklmnopqrstuvwxyz0123456789"
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++)
    { temp = "" + field.value.substring(i, i+1);
      if (valid.indexOf(temp) == "-1") ok = "no";
    }
if (ok == "no")
    { alert("Invalid entry!  Only characters and numbers are accepted!");
      field.focus();
      field.select();
    }
}

// -------------------------------------------------------------------------------
// Function recordChecker()
//   When users enter a form of a complete data record with all or some field values entered, this checks 
//    that a value in at least 1 field is entered before "posting" the form.
//  USE: <FORM NAME="fullrecord" onsubmit="return recordChecker(this);" METHOD="get" ACTION="record.asp">
//       <INPUT TYPE="Text" NAME="Fld1" > 
//       <INPUT TYPE="Text" NAME="Fld2" > 
//       <INPUT TYPE="Text" NAME="   etc. . .  
//
function recordChecker(form)
{ 
if (form.Fld1.value == "" ) { if (form.Fld2.value == "" ) { if (form.Fld3.value == "" ) { if (form.Fld4.value == "" ) { if (form.Fld5.value == "" ) { if (form.Fld6.value == "" ) { if (form.Fld7.value == "" ) { if (form.Fld8.value == "" ) { if (form.Fld9.value == "" ) { if (form.Fld10.value == "" ) { if (form.Fld11.value == "" )  { if (form.Fld12.value == "" ) { alert ("You need to enter something to search for !"); return false; } } } } } } } } } } } }
return true;
}


// -------------------------------------------------------------------------------
// Function buildSelectedValues()
//   When users Click SEARCH at the "Keyword Search" box, this checks that at least 3 characters are entered before "posting" the form.
//    USE: <TD colspan="2"><FORM NAME="phraseform" onsubmit="return searchChecker(this); buildSelectedValues(window.document.fieldlist.sectortype, window.document.fieldlist.arealist, window.document.fieldlist.compasslist)" METHOD="get" ACTION="keywordsearch.asp" id=queryform>
//    Parameters:WAS 1, the selected value from the Sector menu; 2, the selected value from the Area menu; 3, the selected value from the sub-area menu;
//    Parameter: NOW 1, the name of the form which holds the drop-down values
function buildSelectedValues(form)
{ 

//var BuildSector = form.sectortype.options[form.sectortype.selectedIndex].text
var BuildSector = form.sectortype.options[selectedIndex].text
var BuildArea = form.arealist.options[selectedIndex].text
var BuildCompass = form.compasslist.options[selectedIndex].text 
form.hiddenSelection = "SetTo: " + BuildSector + BuildArea + BuildCompass
form.SelectedBoxValues = "SetTo: " + BuildSector + BuildArea + BuildCompass
return ;
}

// -------------------------------------------------------------------------------
// Function saveSelectedValues()
//   When users Click SEARCH at the "Keyword Search" box, this checks that at least 3 characters are entered before "posting" the form.
//    USE: <TD colspan="2"><FORM NAME="phraseform" onsubmit="return searchChecker(this); buildSelectedValues(window.document.fieldlist.sectortype, window.document.fieldlist.arealist, window.document.fieldlist.compasslist)" METHOD="get" ACTION="keywordsearch.asp" id=queryform>
//    Parameters:WAS 1, the selected value from the Sector menu; 2, the selected value from the Area menu; 3, the selected value from the sub-area menu;
//    Parameter: NOW 1, the name of the form which holds the drop-down values  2, the name of the form which will hold the hidden copies of these values
function saveSelectedValues(from_form, to_form)
{ 
var SaveSector = from_form.sectortype.options[selectedIndex].text
var SaveArea = from_form.arealist.options[selectedIndex].text
var SaveCompass = from_form.compasslist.options[selectedIndex].text 
// net save them again in global variables!
SavedSector = from_form.sectortype.options[selectedIndex].text
SavedArea = from_form.arealist.options[selectedIndex].text
SavedCompass = from_form.compasslist.options[selectedIndex].text 

to_form.SelectedBoxValues = "SetTo: " + SaveSector + SaveArea + SaveCompass
to_form.SelectedBoxValues.value = "SetTo: " + SaveSector + SaveArea + SaveCompass
window.document.testform.displayit.value='FunctionSetTo:' + SavedSector + SavedArea + SavedCompass;
return ;
}


// -------------------------------------------------------------------------------
//  function spaces_2_underscore()
//   replace any text spaces with underscores and ampersands with &amp
//   Note that there is also a similar VB function
 function spaces_2_underscore(InStr)
{
var OutStr, RExp
   OutStr = InStr
   RExp = / /g  // ([\s_])
//   RExp = new RegExp(" ", "g") // ([\s_])
//   OutStr = InStr.replace(" ", "_")
   OutStr = InStr.replace(RExp, "_")
//   RExp = new RegExp(\&)
//   OutStr = OutStr.replace(RExp, "XXXampXXX")
   OutStr = OutStr.replace("\&", "XXXampXXX")
return(OutStr)
}

//  function emailUpdateStart()
//  2 functions to send a message using the users' email client
// use emailUpdateStart() to send the message and open the clickable link, 
// followed by a text or graphic, 
// followed by emailUpdateStop() to terminate the link
// Parameter 1: string explaining which host partner org the message comes from
// Parameter 2: the organisation name (or other identifier) of the record referred to
function emailUpdateStart(userHost, recordID)
{
 var display_org, subject_string

  subject_string= escape("META update from a " + userHost + " Re " + recordID)
  body_string = escape("Database information for\n" + recordID + "\n\n should be corrected to show . . . \n\nWe appreciate your help. These records are updated once or twice a month and this correction should appear then\nThank you")
  document.write("<a href=mailto:admin@generator.org.uk?subject=" + subject_string + "&body=" + body_string +">");
}
function emailUpdateStop(userHost, recordID)
{
  document.write("</a>");
}
//--></script>

