String.prototype.ltrim = function(){
   if( this == null ){ return null; }
   for(var i = 0; this.charAt(i) == " "; i++);
   return this.substring(i, this.length);
}

String.prototype.rtrim = function(){
   if( this == null ){ return null; }
   for(var i = this.length-1; this.charAt(i)==" "; i--);
   return this.substring(0, i+1);
}

String.prototype.trim = function(){
	return this.ltrim().rtrim();
}

function ValidateFld(_fld)
{
  if( _fld.getAttribute('rel') && _fld.getAttribute('rel').indexOf('required') > -1 && _fld.value.trim().length < 2 )
  {
//    alert('Please, fill this field');
    _fld.className = 'error';
//    _fld.focus();
    return false;
  }
  if( !ValidateFldAsNum(_fld) ) return false;
  if( !ValidateFldAsDate(_fld) ) return false;
  return true;
}

function ValidateFldAsNum(_fld)
{
  if( _fld.getAttribute('rel') && _fld.getAttribute('rel').indexOf('num') > -1 && _fld.value.trim().length > 0 && isNaN(_fld.value.trim()) )
  {
//    alert('Please, enter numeric value');
    _fld.className = 'error';
//    _fld.focus();
//    _fld.select();
    return false;
  }
  else if( _fld.type == 'text' || _fld.type.indexOf('select') > -1 ) _fld.className = '';
  return true;
}

function ValidateFldAsDate(_fld)
{
  if( _fld.getAttribute('rel') && _fld.getAttribute('rel').indexOf('date') > -1 && _fld.value.trim().length > 0 && !validateDate(_fld.value) )
  {
//    alert('Please, enter correct date value');
    _fld.className = 'error';
//    _fld.focus();
//    _fld.select();
    return false;
  }
  else if( _fld.type == 'text' ) _fld.className = '';
  return true;
}

function ValidateForm()
{
  var arrAllFlds = document.forms['frmFund'].elements;
  var bHasProbs = false;
  var objFirst = null;
  for( var x = 0, y = arrAllFlds.length; x < y; x++ )
  {
    if( arrAllFlds[x].type != 'submit' && arrAllFlds[x].type != 'file' )
    {
      if( !ValidateFld(arrAllFlds[x]) )
      {
        bHasProbs = true;
        if( objFirst == null ) objFirst = arrAllFlds[x];
      }
    }
  }
  if( bHasProbs )
  {
    alert('Please fill/correct highlighted fields');
    objFirst.focus();
    return false;
  }
  return confirm('Are you ready to submit the form?');
}

//window.onload = function()
//{
//  if(document.forms['frmFund'])
//  {
//    var arrAllFlds = document.forms['frmFund'].elements;
//  	for( x = 0, y = arrAllFlds.length; x < y; x++)
//  	{
//  		if(arrAllFlds[x].type != 'submit' && arrAllFlds[x].type != 'file')
//  		{
//    		arrAllFlds[x].onfocus = function()
//    		{
//          ValidateForm(this);
//    		}
//  		}
//  	}
//  }
//}

function addFile()
{
  var newFCnt = parseInt(document.getElementById('cacheFCnt').value) + 1;
  document.getElementById('cacheFCnt').value = newFCnt;
  var newContent = document.createElement("div");
  newContent.innerHTML = '<label for="submit_fund_file'+newFCnt+'">Upload</label><input type="file" name="submit_fund_file[]" id="submit_fund_file'+newFCnt+'" /><label>(<abbr title="MS Excel Stylesheet">xls</abbr>, <abbr title="Portable Document Format">pdf</abbr>, <abbr title="MS Word Document">doc</abbr>, <abbr title="ZIP archive format">zip</abbr> up to 10 <abbr title="Megabyte">Mb</abbr>)</label>';
  document.getElementById('fHolder').appendChild(newContent);
}

function validateDate( strValue )
{
  var objRegExp = /^\d{1,2}\/\d{1,2}\/\d{4}$/

  //check to see if in correct format
  if(!objRegExp.test(strValue))
  {
    return false; //doesn't match pattern, bad date
  }
  else{
    var strSeparator = '/'
    var arrayDate = strValue.split(strSeparator);
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31,
                        '04' : 30,'05' : 31,
                        '06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,
                        '10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[1],10);

    //check if month value and day value agree
    if(arrayLookup[arrayDate[0]] != null) {
      if(intDay <= arrayLookup[arrayDate[0]] && intDay != 0)
        return true; //found in lookup table, good date
    }

    var intMonth = parseInt(arrayDate[0],10);
    if (intMonth == 2) {
       var intYear = parseInt(arrayDate[2]);
       if (intDay > 0 && intDay < 29) {
           return true;
       }
       else if (intDay == 29) {
         if ((intYear % 4 == 0) && (intYear % 100 != 0) ||
             (intYear % 400 == 0)) {
              // year div by 4 and ((not div by 100) or div by 400) ->ok
             return true;
         }
       }
    }
  }
  return false; //any other values, bad date
}