﻿function clearResume() {
  document.getElementById('fuResume').value = '';
}


function validateIC() 
{
  var cnt = 0;
  var o = document.forms[0].fuResume;

  
  if (o.value.length > 0) {
    var s = o.value;
    var iPos = s.lastIndexOf('.');
    var sExt = 'doc,docx,rtf,pdf,txt,html';
    
    s = s.substring(iPos + 1);

    if (sExt.indexOf(s) == -1) {
      cnt++;
      _setErrorState(true, document.getElementById('lblResume'));
      alert('The type of document you are trying to upload is not supported. Only DOC, DOCX, RTF, PDF, TXT and HTML documents are allowed.');
    };
  };
     

  o = document.forms[0].tbEmail;
  if (_checkValue(o, 'lblEmail') == 0) {
    if (!_isValidEmail(o.value)) {
      cnt++;
      _setErrorState(true, document.getElementById('lblEmail'));
      o.focus();
      alert('Please enter a valid email address');      
    } else _setErrorState(false, o);
    
  } else cnt++;
        
  o = document.forms[0].tbLastName;
  cnt = cnt + _checkValue(o, 'lblLName');

  o = document.forms[0].tbFirstName;
  cnt = cnt + _checkValue(o, 'lblFName');

  if (cnt > 0) {
    if (cnt == 1) {
      alert('There is an error on this form.  Please correct.');
    } else {
      alert('There are ' + cnt + ' errors on this form.  Please correct.');
    };

    return false;
    
  } else return true;


};

function _checkValue(_cntrl,_labelName) {
  var o = document.getElementById(_labelName);

  if (o == 'undefined') return 0;
  
  if (_cntrl.value == '' || _cntrl.value.length < 2) {    
    _setErrorState(true, o);
    _cntrl.focus();
    return 1;
  };

  _setErrorState(false, o);
  return 0;  
}

function _setErrorState(_on,_cntrl) {
  if (_on) {
    _cntrl.style.color = 'red';
    _cntrl.style.fontWeight = 'bold';    
  } else {
    _cntrl.style.color = '';
    _cntrl.style.fontWeight = '';    
  };
};

function _isValidEmail(str) {
  var iPos = str.indexOf("@");
  var iPos2;

  if (str.search(/[^A-Za-z0-9@._\- ]/) != -1) return false;

  if (str.indexOf("@", iPos + 1) != -1) {
    return false;
  };

  if (iPos > 0) {
    iPos2 = str.lastIndexOf(".")
    if (iPos2 > iPos) {
      if (str.length - iPos2 > 2) {
        return true;
      } else {
        return false;
      };
    };
  };

  return false;
};
