//----------------------------------------------------------------------
function initEmpPhones(f)
{
  var empPhone = f.telEmpPhone.value
  var empExt   = f.intEmpPhoneExt.value
  var empFax   = f.telEmpFax.value
  if (f.strIsPhoneEmployer[1].checked) {
    toggleMandatory(f, 'telEmpPhone', 'mandatoryEmpPhone', 'add')
  }
  else {
    toggleMandatory(f, 'telEmpPhone', 'mandatoryEmpPhone', 'remove')
  }
  f.telEmpPhone.value    = empPhone
  f.intEmpPhoneExt.value = empExt
  
  
  if (f.strIsFaxEmployer[1].checked) {
    toggleMandatory(f, 'telEmpFax', 'mandatoryEmpFax', 'add')
  }
  else {
    toggleMandatory(f, 'telEmpFax', 'mandatoryEmpFax', 'remove')
  }
  f.telEmpFax.value = empFax
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
function toggleMandatory(f, item, itemIMG, toggle)
{
  switch(toggle.toLowerCase())
  {
    case "add"    : 
      addToMandatory(item, f.mandatoryBase)
      switchImage(document.images[itemIMG], '../images/star.gif')
      f[item].value=""
      toggleReadOnly(f[item], 'off')
      break;
    case "remove" :
      removeFromMandatory(item, f.mandatoryBase)
      switchImage(document.images[itemIMG], '../images/blank.gif')
      f[item].value=""
      toggleReadOnly(f[item], 'on')
      break;
  }
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
function toggleReadOnly(item, toggle)
{

  switch (toggle.toLowerCase())
  {
    case "on" :
      item.onfocus=item.blur
      break;
    case "off" :
      item.onfocus=null
      break;
  }
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
function addMandatory(f)
{
  var tmp = ""
  var mandatoryList = f.mandatoryBase.value
 
  if (mandatoryList.length>0) {
    tmp += "," + mandatoryList
  }

  return tmp
 
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
function switchImage(imgRef, newSrc)
{
 imgRef.src=newSrc
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
function addToMandatory(toAdd, mandatoryList)
{
  var tmp = mandatoryList.value
  var pos = tmp.indexOf("'" + toAdd + "'")
  var isFound 
 
  (pos > -1) ? isFound=true : isFound=false

  if (!isFound){
    if (tmp.length==0) {
      tmp += "'" + toAdd + "'"
    }
    else {
      tmp += ",'" + toAdd + "'"
    }
  }
  mandatoryList.value = tmp
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
function removeFromMandatory(toRemove, mandatoryList)
{
  var tmp = mandatoryList.value
  var pos = tmp.indexOf("'" + toRemove + "'")
  var result
  var isFound 
 
  (pos > -1) ? isFound=true : isFound=false
  
  if (isFound) {
    if (pos==0) {
      result = tmp.slice( ("'" + toRemove + "',").length ) // remove item + ","
    }
    else if ((pos-1 + (",'" + toRemove + "'").length)==tmp.length) {
      result = tmp.slice(0,pos-1)  // remove "," + item
    }
    else {
      result = tmp.slice(0,pos) + tmp.slice(pos + (",'" + toRemove + "'").length)
    }
  }
  mandatoryList.value = result
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
function validateUSER(fieldVal, lowerLimit, upperLimit)
{
  var userMASK = eval("/^([a-zA-Z0-9]{" + lowerLimit + "," + upperLimit + "})$/")
  
  var inputArray = fieldVal.match(userMASK)
  
  if (inputArray==null)  {
    return false
  }
  return true
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
function checkLoginInfo(f)
{
 var user = f.strUserCode
 var password = f.strPassword
 var passwordConfirm = f.strPasswordConfirm
 
  if (validateUSER(user.value)){
    if (password.value.length>3 && password.value.length<11) {
      if (password.value==passwordConfirm.value) {
        return true
      }
      else {
        alert("Both passwords must match");
        password.select();
        passwordConfirm.value="";
        password.focus()
      }
    }
    else {
      alert("The password need to be between 4 and 10 characters long")
      password.select()
      password.focus()
    }
  }
  else {
    alert(user.value + " is not a valid user name.\nPlease enter 7 to 10 characters using only letters or numbers (no spaces)")
    user.select()
    user.focus()
  }
return false
}
//----------------------------------------------------------------------

//----------------------------------------------------------------------
function validate(f)
{
  if (eval('submitPage(f, f' + addMandatory(f) + ')')) {
    if (checkLoginInfo(f)) {
      f.submit()
    }
  }
}
//----------------------------------------------------------------------
