function FDK_AddToValidateArray(FormName,FormElement,Validation,SetFocus)
{
    var TheRoot=eval("document."+FormName);
 
    if (!TheRoot.ValidateForm) 
    {
        TheRoot.ValidateForm = true;
        eval(FormName+"NameArray = new Array()")
        eval(FormName+"ValidationArray = new Array()")
        eval(FormName+"FocusArray = new Array()")
    }
    var ArrayIndex = eval(FormName+"NameArray.length");
    eval(FormName+"NameArray[ArrayIndex] = FormElement");
    eval(FormName+"ValidationArray[ArrayIndex] = Validation");
    eval(FormName+"FocusArray[ArrayIndex] = SetFocus");
 
}

function FDK_ValidateSelectionMade(FormElement,ErrorMsg)
{
  msg = "";

  var iPos = FormElement.selectedIndex;
  if ((iPos<=0 && FormElement.size<=1) || (iPos<0))
  {
    msg = ErrorMsg;
  }

  return msg;
}

function FDK_AddSelectionMadeValidation(FormName,FormElementName,SetFocus,ErrorMsg)  {
  var ValString = "FDK_ValidateSelectionMade("+FormElementName+","+ErrorMsg+")"
  FDK_AddToValidateArray(FormName,eval(FormElementName),ValString,SetFocus)
}

function FDK_StripChars(theFilter,theString)
{
	var strOut,i,curChar

	strOut = ""
	for (i=0;i < theString.length; i++)
	{		
		curChar = theString.charAt(i)
		if (theFilter.indexOf(curChar) < 0)	// if it's not in the filter, send it thru
			strOut += curChar		
	}	
	return strOut
}

function FDK_ValidateAlphaNum(FormElement,Required,ErrorMsg)
{
	var msg = "";
	var i, m, s, firstNonWhite
	var theString = FormElement.value;
 	var msgInvalid = ErrorMsg;

	if (FDK_StripChars(" ",theString).length == 0)	     {
		if (!Required)       {
          return "";		
        }
		else       {
          return msgInvalid;
        }
    }
	//Strip spaces off of the sides of the string
 	theString = FDK_Trim(theString);

    for (var n=0; n<theString.length; n++)     {
      theChar = theString.substring(n,n+1);
      if (!FDK_AllInRange("0","9",theChar) && !FDK_AllInRange("A","Z",theChar.toUpperCase()) && !(theChar == " "))     {
        return msgInvalid;
      }
    }

    return "";
}

function FDK_Trim(theString)
{
 var i,firstNonWhite

 if (FDK_StripChars(" \n\r\t",theString).length == 0 ) return ""

	i = -1
	while (1)
	{
		i++
		if (theString.charAt(i) != " ")
			break	
	}
	firstNonWhite = i
	//Count the spaces at the end
	i = theString.length
	while (1)
	{
		i--
		if (theString.charAt(i) != " ")
			break	
	}	

	return theString.substring(firstNonWhite,i + 1)

}

function FDK_AllInRange(x,y,theString)
{
	var i, curChar
	
	for (i=0; i < theString.length; i++)
	{
		curChar = theString.charAt(i)
		if (curChar < x || curChar > y) //the char is not in range
			return false
	}
	return true
}

function FDK_AddAlphaNumericValidation(FormName,FormElementName,Required,SetFocus,ErrorMsg)  {
  var ValString = "FDK_ValidateAlphaNum("+FormElementName+","+Required+","+ErrorMsg+")"
  FDK_AddToValidateArray(FormName,eval(FormElementName),ValString,SetFocus)
}

function FDK_ValidateNumChars(FormElement,Required,Minimum,Maximum,StripSpaces,ErrorMsg)     {	
	var theString = FormElement.value;
	var MinLength = Minimum;
	var MaxLength = Maximum;
	var msgInvalid = ErrorMsg;

	if (FDK_StripChars(" \n\r",theString).length == 0 && !Required)	{
       return "";
    }
	
	theString = FDK_Trim(theString);

	if (StripSpaces)      {
		theString = FDK_StripChars(" \n\r",theString);
    }
		
	if (MinLength > 0 && theString.length < MinLength)     {
		return msgInvalid;
    }
		
	if (MaxLength > 0 && theString.length > MaxLength)      {
		return msgInvalid;
	}

	// we passed the tests
	FormElement.value = theString;

	return "";
}

function FDK_AddNumCharsValidation(FormName,FormElementName,Required,Minimum,Maximum,StripSpaces,SetFocus,ErrorMsg)  {
  var ValString = "FDK_ValidateNumChars("+FormElementName+","+Required+","+Minimum+","+Maximum+","+StripSpaces+","+ErrorMsg+")";
  FDK_AddToValidateArray(FormName,eval(FormElementName),ValString,SetFocus)
}

function FDK_reformat(s)
{
    var arg;
    var sPos = 0;
    var resultString = "";

    for (var i = 1; i < FDK_reformat.arguments.length; i++) {
       arg = FDK_reformat.arguments[i];
       if (i % 2 == 1) 
           resultString += arg;
       else 
       {
           resultString += s.substring(sPos, sPos + arg);
           sPos += arg;
       }
    }
    return resultString;
}

function FDK_ValidateEmail(FormElement,Required,ErrorMsg)
{
   var msg = "";
   var val = FormElement.value;
   var msgInvalid = ErrorMsg;

   var theLen = FDK_StripChars(" ",val).length
   if (theLen == 0)	     {
     if (!Required) return "";
     else return msgInvalid;
   }

   if (val.indexOf("@",0) < 0 || val.indexOf(".")<0) 
   {
      msg = msgInvalid;
   }
   return msg;
}

function FDK_AddEmailValidation(FormName,FormElementName,Required,SetFocus,ErrorMsg)  {
  var ValString = "FDK_ValidateEmail("+FormElementName+","+Required+","+ErrorMsg+")"
  FDK_AddToValidateArray(FormName,eval(FormElementName),ValString,SetFocus)
}

function FDK_ValidateTargetEqualsSource(SourceElement,TargetElement,ClearFields,CaseSensitive,ErrorMsg)
{

	var msg = ""
	var sourceText = SourceElement.value;
	var targetText = TargetElement.value;
	var msgInvalid = ErrorMsg;
    
	if (!CaseSensitive)   {
	  sourceText = sourceText.toUpperCase();
	  targetText = targetText.toUpperCase();
	}
	
	if (sourceText != targetText)
	{
	  msg = msgInvalid
      if (ClearFields)     {
	    TargetElement.value = '';
	    SourceElement.value = '';
	  }
	}
	return msg	
}

function FDK_AddTargetEqualsSourceValidation(FormName,SourceElementName,TargetElementName,ClearFields,CaseSensitive,SetFocus,ErrorMsg)  {
  var ValString = "FDK_ValidateTargetEqualsSource("+SourceElementName+","+TargetElementName+","+ClearFields+","+CaseSensitive+","+ErrorMsg+")"
  FDK_AddToValidateArray(FormName,eval(SourceElementName),ValString,SetFocus)
}

function FDK_Validate(FormName, stopOnFailure, AutoSubmit, ErrorHeader)
{
 var theFormName = FormName;
 var theElementName = "";
 if (theFormName.indexOf(".")>=0)  
 {
   theElementName = theFormName.substring(theFormName.indexOf(".")+1)
   theFormName = theFormName.substring(0,theFormName.indexOf("."))
 }
 var ValidationCheck = eval("document."+theFormName+".ValidateForm")
 if (ValidationCheck)  
 {
  var theNameArray = eval(theFormName+"NameArray")
  var theValidationArray = eval(theFormName+"ValidationArray")
  var theFocusArray = eval(theFormName+"FocusArray")
  var ErrorMsg = "";
  var FocusSet = false;
  var i
  var msg
    
 
        // Go through the Validate Array that may or may not exist
        // and call the Validate function for all elements that have one.
  if (String(theNameArray)!="undefined")
  {
   for (i = 0; i < theNameArray.length; i ++)
   {
    msg="";
    if (theNameArray[i].name == theElementName || theElementName == "")
    {
      msg = eval(theValidationArray[i]);
    }
    if (msg != "")
    {
     ErrorMsg += "\n"+msg;                   
     if (stopOnFailure == "1") 
     {
       if (theFocusArray[i] && !FocusSet)  
      {
       FocusSet=true;
       theNameArray[i].focus();
      }
      alert(ErrorHeader+ErrorMsg);
      document.MM_returnValue = false; 
      break;
     }
     else  
     {
      if (theFocusArray[i] && !FocusSet)  
      {
       FocusSet=true;
       theNameArray[i].focus();
      }
     }
    }
   }
  }
  if (ErrorMsg!="" && stopOnFailure != "1") 
  {
   alert(ErrorHeader+ErrorMsg);
  }
  document.MM_returnValue = (ErrorMsg==""); 
  if (document.MM_returnValue && AutoSubmit)  
  {
   eval("document."+FormName+".submit()")
  }
 }
}

window.onload = function() {
	FDK_AddSelectionMadeValidation('regfrom','document.regfrom.Title',true,'\'Please enter your Title\'');
	FDK_AddAlphaNumericValidation('regfrom','document.regfrom.FirstName',true,true,'\'Please enter your First Name\'');
	FDK_AddAlphaNumericValidation('regfrom','document.regfrom.Surname',true,true,'\'Please enter your Surname\'');
	FDK_AddNumCharsValidation('regfrom','document.regfrom.Mobile',true,10,17,false,true,'\'Please enter a valid Telephone Number\'');
	FDK_AddEmailValidation('regfrom','document.regfrom.Email',true,true,'\'Please enter a valid Email Address.\\n(a valid email address has an \\\'@\\\' and a \\\'.\\\')\'');
	FDK_AddTargetEqualsSourceValidation('regfrom','document.regfrom.Email','document.regfrom.ConfirmEmail',false,false,true,'\'Your Email Address entries did not match.\\nPlease re-enter your Email and try again.\'')
	
	load()
}
window.onunload = function() {
	GUnload()
}