function validIncomeInput(obj)
{
 	if (! isBlank(obj.incomedesc.value,"Please specify a description or name for this income source.") ) {
		return false;	
	}
 	if (! isBlank(obj.incomeamount.value,"Please specify an income amount.") ) {
		return false;	
	}
	if (! isCurrency(obj.incomeamount.value,"Please specify a valid income amount (for example: 123.12).") )	{
		return false;
	}
	if ( obj.incomeamount.value < 0 ) {
		alert("Income amount must be greater than or equal to \$0.00.");
		return false;
	}
	return true;
}

function validAccountsInput(obj)
{
 	if (! isBlank(obj.accountshortdesc.value,"Please specify a description or name for this account.") ) {
		return false;	
	}
 	if (! isBlank(obj.amountrequired.value,"Please specify an monthly amount required.") ) {
		return false;	
	}
	if (! isCurrency(obj.amountrequired.value,"Please specify a valid monthly amount required (for example: 123.12).") ) {
		return false;
	}
	if ( obj.amountrequired.value < 0 ) {
		alert("Income amount must be greater than or equal to \$0.00.");
		return false;
	}
	return true;
}

function validEditTransactionInput(obj)
{
 	if (! isBlank(obj.transactiondatetime.value,"Please specify a transaction date.") ) {
		return false;	
	}
	if (! checkDate(obj.transactiondatetime.value) ) {
		return false;
	}
 	if (! isBlank(obj.transactiondesc.value,"Please specify a transaction description.") ) {
		return false;	
	}
 	if (! isBlank(obj.transactionaccountid.value,"Please specify an account.") ) {
		return false;	
	}
 	if (! isBlank(obj.transactionamount.value,"Please specify a transaction amount.") ) {
		return false;	
	}
	if (! isCurrency(obj.transactionamount.value,"Please specify a valid transaction amount (for example: 123.12).") ) {
		return false;
	}
	return true;
}


var transactiontype = "";
function validTransactionInput(obj)
{
 	if (! isBlank(obj.transactiondatetime.value,"Please specify a transaction date.") ) {
		return false;	
	}
	if (! checkDate(obj.transactiondatetime.value) ) {
		return false;
	}
 	if (! isBlank(obj.transactiondesc.value,"Please specify a transaction description.") ) {
		return false;	
	}

	if ( transactiontype == 'Deposit' ) {
 		if (! isBlank(obj.depositamount.value,"Please specify a deposit amount.") ) {
			return false;	
		}
		if (! isCurrency(obj.depositamount.value,"Please specify a valid deposit amount (for example: 123.12).") ) {
			return false;
		}
		if ( obj.depositamount.value <= 0 ) {
			alert("Deposit amount must be greater than \$0.00.");
			return false;
		}
 		if (! isBlank(obj.acctto.value,"Please specify a deposit \"To:\" account.") ) {
			return false;	
		}
		if ( obj.acctto.value == "ALL ACCOUNTS" ) {
			if (obj.depositamount.value < 1 ) {
				alert ("All equal distribution deposits must be at least \$1.00 or more.  Please re-enter a valid deposit amount and try again.");
				return false;
			}
		}
	}
	else if ( transactiontype == 'Withdrawal' ) {
 		if (! isBlank(obj.withdrawlamount.value,"Please specify a withdrawal amount.") ) {
			return false;	
		}
		if (! isCurrency(obj.withdrawlamount.value,"Please specify a valid withdrawal amount (for example: 123.12).") ) {
			return false;
		}
		if ( obj.withdrawlamount.value <= 0 ) {
			alert("Withdrawal amount must be greater than \$0.00.");
			return false;
		}
 		if (! isBlank(obj.acctfrom.value,"Please specify a withdrawal \"From:\" account.") ) {
			return false;	
		}
	}
	else if ( transactiontype == 'Transfer' ) {
 		if (! isBlank(obj.transferamount.value,"Please specify a transfer amount.") ) {
			return false;	
		}
		if (! isCurrency(obj.transferamount.value,"Please specify a valid transfer amount (for example: 123.12).") ) {
			return false;
		}
		if ( obj.transferamount.value <= 0 ) {
			alert("Transfer amount must be greater than \$0.00.");
			return false;
		}
 		if (! isBlank(obj.acctto.value,"Please specify a destination \"To:\" account.") ) {
			return false;	
		}
		if ( obj.acctto.value == "ALL ACCOUNTS" ) {
			alert ("Transfer transactions cannot be distributed equally to all accounts, please select a single account and try again.");
			return false;
		}
 		if (! isBlank(obj.acctfrom.value,"Please specify a source \"From:\" account.") ) {
			return false;	
		}
	}

	return true;

}

/***************************************************************************************************/

function checkDate(input) {
	var validformat=/^\d{2}\/\d{2}\/\d{4}$/ //Basic check for format validity
	var returnval=false
	if (!validformat.test(input))
		alert("Invalid Date Format. Valid format is \"mm/dd/yyyy\". Please correct and submit again.")
	else { //Detailed check for valid date ranges
		var monthfield=input.split("/")[0]
		var dayfield=input.split("/")[1]
		var yearfield=input.split("/")[2]
		var dayobj = new Date(yearfield, monthfield-1, dayfield)
		if ((dayobj.getMonth()+1!=monthfield)||(dayobj.getDate()!=dayfield)||(dayobj.getFullYear()!=yearfield))
			alert("Invalid Day, Month, or Year detected. Please correct and submit again.")
		else
			returnval=true
	}
	if (returnval==false)
		return false;
	else
		return true;
}




function isBlank(input, errormsg) {
      	if (input==null) {
		alert(errormsg);
		return false;
	}
      	if (input.length==0) {
		alert(errormsg);
		return false;
	}
      	return true;     
}

function isNumeric(input, errormsg) {
 	var ValidChars = "0123456789.";
   	var Char;
 
   	for (i = 0; i <= input.length; i++) {
   		Char = input.charAt(i); 
      		if (ValidChars.indexOf(Char) == -1) {
			alert(errormsg);
			return false;
			break;
        	}
 	}
   	return true;
}

function isCurrency (input,errormsg) {
 	var isCurrency_re = /^\s*(\+|-)?((\d+(\.\d\d)?)|(\.\d\d))\s*$/;
	if (!isCurrency_re.test(input)) {
		alert(errormsg);
		return false;
	}
	return true;
}
