var invalid=0;
var required = new Array();
var mfields = new Array();
var customValidationFuncs = new Array();

//Validations
function val_amount(e, m){
  s = e.value;
  
  regex = /^\d+(\.\d{2})*$/
  if(regex.test(s) || s=="")
  {
    if(m!=""){
      if($(m).innerHTML!=""){
        $(m).innerHTML="";
      }
      invalid--;
      //alert("Currency validation passed, invalid fields: " + invalid.toString());
      return true;
    }
  } else {
    if(m!="")
      $(m).innerHTML = "Invalid amount.";
    invalid++;
    //alert("Currency validation failed, invalid fields: " + invalid);
    return false;
  }
}

function val_date(e,m){
  s = e.value;
  
  regex = /^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])/ //YYYY-MM-DD
  // (0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d    MM/DD/YYYY
  if(regex.test(s) || s=="")
  {
    if(m!=""){
      if($(m).innerHTML!=""){
        $(m).innerHTML="";
      }
      invalid--;
    }
  } else {
    if(m!="")
      $(m).innerHTML = "Invalid Date";
    invalid++
  }
}

function val_number(e, m){
  s = e.value;
  
  regex = /^\d+$/
  
  if(regex.test(s) || s=="")
  {
    if(m!=""){
      if($(m).innerHTML!=""){
        $(m).innerHTML="";
      }
      invalid--;
      //alert("Number validation passed, invalid fields: " + invalid.toString());
    }
  } else {
    if(m!="")
      $(m).innerHTML = "Invalid number.";
    invalid++;
    //alert("Number validation failed, invalid fields: " + invalid.toString());
  }
}

function val_required(x, m){
  required = x;
  mfields = m;
}

function val_checkrequired(){
  ret = true;
  if(required.length==0) return true;
  for(i=0;i<required.length;i++){
    if($(required[i]).value == ""){
      ret = false;
      $(mfields[i]).innerHTML = "This field is required.";
    }
  }
  return ret;
}

function addCustomValidationFunc(cvf) {
  customValidationFuncs.push(cvf);
}

function doCustomValidations() {
  var valid = true;
  for(i = 0; i < customValidationFuncs.length; i++) {
    // call the custom validation function, if it returns false, then the
    // validation failed and we should not continue.
    if(!customValidationFuncs[i]())
      valid = false;
  }
  
  return valid;
}

function reqValidationFunc(field, errorField)
{
  return function() {
    if($(field).value == "") {
      $(errorField).innerHTML = "This field is required.";
      return false;
    }
    
    $(errorField).value = "";
    return true;
  }
}

function reqIfValidationFunc(conditionStr, field, errorField)
{
  return function() {
    if(eval(conditionStr)) {
      if($(field).value == "") {
        $(errorField).innerHTML = "This field is required.";
        return false;
      }
    }
    
    $(errorField).innerHTML = "";   
    return true;
  }
}

function doAmendmentCheck()
{
  var trans_date = Date.parse($('f_date_b1').value.replace(/-/g, "/"));
  var file_date = Date.parse($('date_of_filing').value.replace(/-/g, "/"));
  
  if(trans_date < file_date) {
    $('schedule_submit').style.display = "none";
    $('schedule_file_amendment').style.display = "block";
    return false;
  } else {
    return true;
  }
}

function val_refunded()
{
  if($('totalRefunded') == null)
    return true;
  else
  {
    var refunded = Number($('totalRefunded').value);
    var amount = Number($('amount1').value);
    if(amount < refunded)
    {
      $('amount1_m').innerHTML = 'You can not reduce the amount of this transaction below the amount ($'+ refunded +') already refunded or repaid.';
      return false;
    }
    else
      return true;
  }
}

/****************************************************************************
	validatePartners()
	
	Checks that at least two valid partners have been identified if the
	disclosure of individual partners is required by the nature of the
	transaction.
 ****************************************************************************/
function validatePartners()
{
	// check that we're on schedule A
	var scheduleID = $('schedule_id');
	if(scheduleID != null && scheduleID.value == "A")
	{
		// check that we're entering a transaction for a partnership
		var contributionCode = $('contributioncode');
		if(contributionCode != null && contributionCode.value == "PART")
		{
			// check that the total amount of the contribution is over 2500
			var amount1 = $('amount1');
			if(amount1 != null && amount1.value > 2500)
			{
				// this function is defined in schedule_o_control.js
				var numEntries = numberOfEntries();
				if($('scheduleOTableEntryEditing') != null)
					numEntries--;
					
				if(numEntries < 2)
				{
					$('schedule_partner_error').style.display = "block";
					return false;
				} else {
					return true;
				}
			}
		}
	}
	
	return true;
}

function val_test(){
 if(invalid<=0 && val_checkrequired() && val_refunded()) return true;
 else return false; 
}
