// Validation of form on client 
// For form: profile.asp

FieldErrMsg = new Array ();       // Save error messsage for each field if an error is found
FieldLabel = new Array ();        // Display label for field where error occured  
StateIncorrectFields = false;     // Set to true if one or more of the input fields is/are incorrect

// Global variables to fill out 
TotFieldToValidate = 13;          // Total field to be validated
strFormName = "Profile";

// Fill in references for input field that are required to be validate BEFORE POSTING
// This information is used before the text of the error to indicate in which field 
// and error occured
FieldLabel[1] = "First Name";
FieldLabel[2] = "Middle Name";
FieldLabel[3] = "Last Name";
FieldLabel[4] = "Email";
FieldLabel[5] = "Home Number (e.g. 604-123-1234)";
FieldLabel[6] = "Cellular (e.g. 604-123-1234)";
FieldLabel[7] = "Pager (e.g. 604-123-1234)";
FieldLabel[8] = "Resume (specified format only)";
FieldLabel[9] = "Where did you hear about us?";
FieldLabel[10] = "Preference";
FieldLabel[11] = "Education";
FieldLabel[12] = "Province";
FieldLabel[13] = "Job Titles (maximum of 3)"

// end of global variables

function ValidateInput() { 

  StateIncorrectFields = false; 
  for (x = 1; x <= TotFieldToValidate; x++) {
    FieldErrMsg[x] = "OK";
  }   

  // Call functions to do the form validation on required input
  // Check First Name - at least one entry
  TxtInput = TestHasInput (1, "first_name"); 
  if (TxtInput != false) {
    TxtLen = TestMinLenght(1, "first_name", 2); 
    if (TxtLen != false) {
      TestAllLetters(1, "first_name"); 
    }
  } 
  
  // Call functions to do the form validation on optional middle name
  TxtInput = TestHasInput2 (2, "middle_name"); 
  if (TxtInput != false) {
    TxtLen = TestMinLenght(2, "middle_name", 2); 
    if (TxtLen != false) {
      TestAllLetters(2, "middle_name"); 
    }
  } 
 
  // Check Last Name
  TxtInput = TestHasInput (3, "last_name"); 
  if (TxtInput != false) {
    TxtLen = TestMinLenght(3, "last_name", 2); 
    if (TxtLen != false) {
      TestAllLetters(3, "last_name"); 
    }
  } 

  // Check email
  TxtInput = TestHasInput(4, "email");      
  if (TxtInput != false) {      
    TxtLen = TestMinLenght(4, "email", 2);
    if (TxtLen != false) {
      ChkEmail = TestIsEmail (4, "email");
    }
  }

  // Check phone no
    TxtInput = TestHasInput (5, "phone_no");               
  if (TxtInput != false) {  
    TxtLen = TestMinLenght(5, "phone_no", 12);
    if (TxtLen != false) {  
      ChkChac = TestIsPhone (5, "phone_no");
      if (ChkChac != false) {   
        TestNumberCount(5, "phone_no", 12)
      } 
    } 
  }

  // Check cellular no, if necessary
  TxtInput = TestHasInput2 (6, "cell_no");               
  if (TxtInput != false) {  
    TxtLen = TestMinLenght(6, "cell_no", 12);
    if (TxtLen != false) {  
      ChkChac = TestIsPhone (6, "cell_no");
      if (ChkChac != false) {   
        TestNumberCount(6, "cell_no", 12)
      } 
    } 
  }

   // Check pager no, if necessary
  TxtInput = TestHasInput2 (7, "pager_no");              
  if (TxtInput != false) {  
    TxtLen = TestMinLenght(7, "pager_no", 12);
    if (TxtLen != false) {  
      ChkChac = TestIsPhone (7, "pager_no");
      if (ChkChac != false) {   
        TestNumberCount(7, "pager_no", 12)
      } 
    } 
  }

  // Test for resume
  if (TestHasInput2(8, "resumeFile") == false) {
    TxtInput = TestHasInput (8, "cv"); 
    if (TxtInput != false) {
      TxtLen = TestMinLenght(8, "cv", 2);
    }
  } 
  else {
    TxtInput = ValidResumeFormat(8, "resumeFile");
  }

  TestMenuSelected(9, document.forms[0].source.selectedIndex);
  TestMenuSelected(10, document.forms[0].preference.selectedIndex);
  TestMenuSelected(11, document.forms[0].education.selectedIndex);
  TestMenuSelected(12, document.forms[0].province.selectedIndex);
  TestMenuSelectedTitles(13);

  // if found any errors - build error message and display it in alert message for client
  if (StateIncorrectFields == false) {
    document.forms[strFormName].submit("POST", "profile.asp");
  } else {  
    strErrorMsg = "Please supply the correct information so we can help you with your request." + "\n" + "Fill in the following field(s): \n\n";
    for (x=1; x <= TotFieldToValidate; x++) {
      if (FieldErrMsg[x] != "OK") {
        strErrorMsg = strErrorMsg + FieldErrMsg[x];
      }   
    }   
    alert (strErrorMsg);
  } 
  return false;
}


/*
** Parameters fldname=name property of field to validate
** Test to see if field is empty
*/ 
function ValidResumeFormat(index, fldname) { 
  strFieldValue = document.forms[strFormName].elements[fldname].value;
  if (strFieldValue.lastIndexOf('.doc') > 0 || strFieldValue.lastIndexOf('.rtf') > 0 || strFieldValue.lastIndexOf('.txt') > 0) {
    return true;
  } else {
    StateIncorrectFields = true;
    FieldErrMsg[index] = "    - " + FieldLabel[index] + "\n";
  }
}


/*
** Parameters: index=index of field to validate, grpName = name of radio buttons group, NoItemsInGrp=Total radio buttons in group
*/
function TestButtonSelected (index, grpName, NoItemsInGrp) {
  RadioElementChecked = new Array ();
  RadioElementsValue = new Array ();
  RadioElementToCheckValue = "";
  FlagFoundSelected = false;
  
  // Test to see if any radio button was selected
  for (x=0; x < NoItemsInGrp; x++) {  
    RadioElementsValue[x] = document.forms[strFormName].elements[grpName][x].checked;   
    if (RadioElementsValue[x] == true) {
      RadioElementToCheckValue = document.forms[strFormName].elements[grpName][x].value;
      FlagFoundSelected = true;     
    }
  }
  
  if (FlagFoundSelected == true) {  
       return RadioElementToCheckValue;      
  } else {
      StateIncorrectFields = true;
      FieldErrMsg[index] = FieldLabel[index] + ": select a contact method and fill in the corresponding contact information." + "\n";   
      return "";
  }
}


/*
** function to check job titles selection
*/
function TestMenuSelectedTitles (index) {

  var iCount=0;
  var iMax=3;

  iCount = selectedCount(document.forms[0].soft);
  if (iCount <= iMax)
    iCount += selectedCount(document.forms[0].hard);
  if (iCount <= iMax)
    iCount += selectedCount(document.forms[0].tech);
  if (iCount <= iMax)
    iCount += selectedCount(document.forms[0].man);
  if (iCount <= iMax)
    iCount += selectedCount(document.forms[0].eng);
  if (iCount <= iMax)
    iCount += selectedCount(document.forms[0].tel);

  if (iCount > iMax || iCount == 0) {
    StateIncorrectFields = true;
    FieldErrMsg[index] = "    - " + FieldLabel[index] + " " + "\n";    
    return "";
  }

}


/*
** function to check the Where did you hear about us? drop-down menu
*/
function TestMenuSelected (index, selectedIndex) {
  if (selectedIndex == 0) {
    StateIncorrectFields = true;
    FieldErrMsg[index] = "    - " + FieldLabel[index] + " " + "\n";    
  }
}


function TestOptionalInput (index, fldname) {
  strFieldValue = document.forms[strFormName].elements[fldname].value;    
  if (strFieldValue == "") {    
    return false;
  } else {
    return true;
  }
}


/*
** Parameters fldname=name property of field to validate
** Test to see if field is empty
*/
function TestHasInput (index, fldname) {  
  strFieldValue = document.forms[strFormName].elements[fldname].value;    
  if (strFieldValue == "") {    
    StateIncorrectFields = true;
    FieldErrMsg[index] = "    - " + FieldLabel[index] + "\n";
    return false;
  }
}


/*
** Parameters fldname=name property of field to validate
** Test to see if field is empty
*/
function TestHasInput2 (index, fldname) { 
  strFieldValue = document.forms[strFormName].elements[fldname].value;    
  if (strFieldValue == "") {    
    return false;
  } else {
    return true;
  }
}


/*
** Parameters fldname=name property of field to validate, minlenght=minimum characters required for input
** Test minimum Lenght
*/
function TestMinLenght (index, fldname, minlenght) {
  strFieldValue = document.forms[strFormName].elements[fldname].value;            
  intItemLen = strFieldValue.length;  
  if (intItemLen < minlenght)  {
    StateIncorrectFields = true;    
    FieldErrMsg[index] = "    - " + FieldLabel[index] + "\n";
    return false;
  } else {
    return true;  
  }
}


/*
** Parameters fldname=name property of field to validate
** Test for repeat sequence ex: "aa"
*/
function TestLetterSequence (index, fldname) {  
  strFieldValue = document.forms[strFormName].elements[fldname].value;
  intItemLen = strFieldValue.length;  
  if (intItemLen == 2) {
    tmpFirstChac = strFieldValue.charAt(0);
    tmpSecondChac = strFieldValue.charAt(1);
    if (tmpFirstChac == tmpSecondChac) {
      StateIncorrectFields = true;
      FieldErrMsg[index] = "    - " + FieldLabel[index] + "\n";
    }   
  }

  if (intItemLen > 2) { 
    tmpFirstChac = strFieldValue.charAt(0);
    tmpSecondChac = strFieldValue.charAt(1);
    tmpThirdChac = strFieldValue.charAt(2);
    if ((tmpFirstChac == tmpSecondChac) && (tmpThirdChac == tmpFirstChac)) {        
      StateIncorrectFields = true;        
      FieldErrMsg[index] = "    - " + FieldLabel[index] + "\n";
    }   
  } 
}

/*
** test fields for all letters
*/
function TestAllLetters(index, fldname) { 
  strFieldValue = document.forms[strFormName].elements[fldname].value;
  var localStateIncorrectFields = false;
  var iChars = "*|,\":<>[]{}\;()@&$#%1234567890";
  for (var i = 0; i < strFieldValue.length; i++) {
    if (iChars.indexOf(strFieldValue.charAt(i)) != -1) {     
      localStateIncorrectFields = true;
    }
  }
  if (localStateIncorrectFields == true) {        
    FieldErrMsg[index] = "    - " + FieldLabel[index] + "\n";
    StateIncorrectFields = true;
  }     
}


/*
**
*/
function TestIsEmail(index, fldname){
  var FlagChacDot = false;
  var FlagChacAt = false; 
  var strEmail = document.forms[strFormName].elements[fldname].value;   

  for (var i = 0; i < strEmail.length; i++ ) {
    var c = strEmail.charAt(i);
    if (c == "@") FlagChacAt = true;
    if (c == ".") FlagChacDot = true;
  }

  if ((FlagChacAt == true) && (FlagChacDot == true)){
    return true;
  } else {          
    StateIncorrectFields = true;
    FieldErrMsg[index] = "    - " + FieldLabel[index] + "\n";
    return false;
  }
}


/*
**
*/
function TestIsPhone(index, namefield) {
  var FlagIsPhone = true;
  var strInvalidChar = "";
  var strMsg = "";
  var strValueCheck = document.forms[strFormName].elements[namefield].value;          
  var Str2 = strValueCheck;
  var iChars = "abcdefghijklmnopqrstuvwxyz*|,\":<>[]{}`\';@&$#%()";
  for (var i = 0; i < Str2.length; i++) {
    if (iChars.indexOf(Str2.charAt(i)) != -1) {        
      FlagIsPhone = false;      
    } 
  } 

  if (FlagIsPhone == true) {
    return true;
  } else {  
    StateIncorrectFields = true;
    strMsg = "    - " + FieldLabel[index] + "\n";
    FieldErrMsg[index] = strMsg;
    return false;   
  } 
}


/*
**
*/
function isAllLetters(IndexRefInputValue) {
  var FlagFoundError = false;
  var Str3 = RefInputValue[IndexRefInputValue]
  var iChars = "*|,\":<>[]{}\;()@&$#%1234567890";
  for (var i = 0; i < Str3.length; i++) {
    if (iChars.indexOf(Str3.charAt(i)) != -1) {     
      FlagFoundError = true;
    }
  }

  if (FlagFoundError == true) {
    return true;
  } else {
    StateIncorrectFields = true;
    return false;
  }
}


/*
**
*/
function selectedCount(oSelect) {

  var iCount = 0;
  for (var i = 0; i < oSelect.options.length; i++) {   
    if (oSelect.options[i].selected)      
      iCount++;
  }
  return iCount;
}


/*
**
*/
function isAllNumbers(ValueOfInputField) {
  var FlagIsNumber = false;
  var Str2 = ValueOfInputField;
  var iChars = "abcdefghijklmnopqrstuvwxyz*|,\":<>[]{}`\';@&$#%()-";

  for (var i = 0; i < Str2.length; i++) {
    if (iChars.indexOf(Str2.charAt(i)) != -1) {     
      FlagIsNumber = true;
    }
  }

  if (FlagIsNumber == true) {
    return true;
  } else {
    StateIncorrectFields = true;
    return false;
  }
}


/*
**
*/
function TestNumberCount(index, namefield, iTotNumberSerie) {
  var FlagIsPhone = true;
  var iFoundcount = 0;
  var strMsg = "";
  var strValueCheck = document.forms[strFormName].elements[namefield].value;          
  var Str2 = strValueCheck;
  var iChars = "0123456789-";

  for (var i = 0; i < Str2.length; i++) {   
    if (iChars.indexOf(Str2.charAt(i)) != -1) {        
      iFoundcount = iFoundcount + 1;      
    } 
  } 

  if (iFoundcount == iTotNumberSerie) {
    return true;
  } else {  
    StateIncorrectFields = true;
    strMsg = "    - " + FieldLabel[index] +  "\n ";
    FieldErrMsg[index] = strMsg;
    return false;   
  } 
}


/*
**
*/
function MM_swapImgRestore() { //v3.0

  var i,x,a=document.MM_sr; 
  for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) 
    x.src=x.oSrc;
}


/*
**
*/
function MM_preloadImages() { //v3.0
  var d=document;   if(d.images) {     if(!d.MM_p) d.MM_p=new Array();
  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; 
  for (i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0) { 
      d.MM_p[j]=new Image; 
      d.MM_p[j++].src=a[i];
    }
  }
}


/*
**
*/
function MM_findObj(n, d) { //v4.0
  var p,i,x;  
  
  if(!d) 
    d=document; 
  
  if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; 
    n=n.substring(0,p);
  }

  if (!(x=d[n])&&d.all) 
    x=d.all[n]; 

  for (i=0;!x&&i<d.forms.length;i++) 
    x=d.forms[i][n];

  for (i=0;!x&&d.layers&&i<d.layers.length;i++) 
    x=MM_findObj(n,d.layers[i].document);

  if(!x && document.getElementById) 
    x=document.getElementById(n); 
  return x;
}


/*
**
*/
function MM_swapImage() { //v3.0

  var i,j=0,x,a=MM_swapImage.arguments; 
  document.MM_sr=new Array; 
  for(i=0;i<(a.length-2);i+=3)
    if ((x=MM_findObj(a[i]))!=null) {
      document.MM_sr[j++]=x; 
      if(!x.oSrc) x.oSrc=x.src; 
      x.src=a[i+2];
    }
}
