/*These functions check the input into their respective fields
 * and output the appropriate error message if there is
 * a problem with the input.
 */
var days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
function checkDate(value)
{
	if(value=="") return "";

	try{
		var message = "";

		date_array = value.split("/");
		//Check whether there are 2 dashes, as required by format
		if(date_array.length != 3) {
			alert("Invalid Date format: There should be two(2) slashes");
			return "";
		}

		var month=date_array[0];
		var day=date_array[1];
		var year=date_array[2];

		//Check Month Format

		switch(month.length) {
			case 2: {
				if (!(month>0 && month<13)) {
					message += "Invalid Month format: Entry can only be between 1 and 12\n";
					month = "";
				}
				break;
			}
			case 1: {
				if (!(month>0 && month<10)) {
					message += "Invalid Month format: One digit entry can only be between 1 and 9\n";
					month = "";
				} else
					month = "0" + month;
				break;
			}
			default: {
				message += "Invalid Month format: Enter two(2) digits\n";
				month="";
				break;
			}
		}

		switch(day.length) {
			case 2: {
				if (!(day>0 && day<32)) {
					message += "Invalid Day format: Entry can only be between 1 and 31\n";
					day = "";
				}
				break;
			}
			case 1: {
				if (!(day>0 && day<10)) {
					message += "Invalid Day format: One digit entry can only be between 1 and 9\n";
					day = "";
				} else
					day = "0" + day;
				break;
			}
			default: {
				message += "Invalid Day format: Enter two(2) digits\n";
				day="";
				break;
			}
		}

		var bound = days[month-1];
		if (month == 2 && (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0))) {
			bound++; // leap day
		}
		if (day > bound) {
			message += "Invalid Day format: No such day in month\n";
		}
		if (year.length != 4 || !(year>0)) {
			message += "Invalid Year format: Enter four(4) digits\n";
			year = "";
		}

		value = month + "/" + day + "/" + year;
		if(message!="")
			alert(message);
		else {
			if(dateIsValid(value))
				return value;
			else
				return "";
		}
	} catch (e) {
		alert(e);
	}

	return "";
}

function checkRate(value, name)
{
	var negative=false;

	if (value == "") return "";

	value = value + "";
	if((value).charAt(0)=="-") return "";
	
	try{
		var amount_array = value.split(".");
		switch(amount_array.length) {
			case 2: {
				var number = amount_array[0];
				var fraction = amount_array[1];

				if(!(number>=0) || !(fraction>=0)) {
					alert("Invalid "+name+" format: Only digits are acceptable");
					return "";
				}
				return ((number*1) + "." + fraction) + "";
			}
			case 1: {
				if(!(amount_array[0]>=0)) {
					alert("Invalid "+name+" format: Only digits are acceptable");
					return "";
				}
				return (amount_array[0]*1) + ".00";
			}
			default: {
				alert("Invalid "+name+" format");
				return "";
			}
		}
	} catch (e) {
		alert("Exception in checkRate(): "+e);
		return "";
	}
	return "";
}

function checkNumber(value, name)
{
	var negative=false;
	if (name == "Total"){
		if(isNaN(value)){ return ""; }
	}
	if (name == "Amount_rf" && value == ""){ return ""; }
	if (value == "") return "0.00";

	value = value + "";
	if((value).charAt(0)=="-"){
		value = value.substring(1);
		negative=true;
	}

	value = verify(value, name);
	if(negative)
		return "-"+value;
	else
		return value;
}

function verify(value, name)
{
	try{
		var amount_array = value.split(".");

		switch(amount_array.length) {
			case 2: {
				var number = amount_array[0];
				var fraction = amount_array[1];

				if(!(number>=0) || !(fraction>=0)) { return ""; }

				var lngth = fraction.length*1;
				number = (number*1) + "";
				var temp;
				if(lngth > 2) {
					if((temp=fraction.charAt(1)*1 + Math.round(fraction.charAt(2)/10)) > 9)
						fraction = (fraction.charAt(0)*10 + temp);
					else
						fraction = fraction.charAt(0) + "" + temp;
				} else if(lngth == 1)
					fraction += "0";
				else if(lngth == 0)
					fraction = "00";

				if(fraction>99) {
					fraction="00";
					number++;
				}
				return number + "." + fraction;
			}
			case 1: {
				if(!(amount_array[0]>=0)) {
					if (name != 'expense_total'){

					alert(" We're sorry, only digits are allowed in the amount field.");
					}

					return "";
				} 
				return (amount_array[0]*1) + ".00";
			}
			default: {
				alert("Invalid "+name+" format");
				return "";
			}
		}
	} catch (e) {
		alert("Exception in checkNumber(): "+e);
	}
	return "";
}

function checkExpenseType(index)
{
	if(index == 1 || index == 8 || index == 12) { return 0; } 
	return index;
}

function checkSaveName(name)
{
	var i=0;
	var length=20;
	if(name.length>length) {
		alert("This field has exceeded the maximum length of: "+length+" by: "+(name.length - length)+" character(s).\nSave action aborted.");
		return false;
	}
	var array = name.split(" ");

	for(var i=0; i<array.length; i++) {
		if(!checkPartsOfName(array[i])) return false;
	}
	return true;
}

function checkPartsOfName(name)
{
	var prefilter = /\+/;
	if(prefilter.test(name)) {
		alert("The save name cannot contain a \"+\" symbol.\n");
		return false;
	}
	var filter = /^([\w-]+(?:\.[\w-]+)*)((?:[\w-]+\.)*\w[\w-]{0,66})$/i;
	if(filter.test(name) && !(name>=0) && !(name<0))
		return true;
	else {
		alert("The save name must contains letters and may contain digits.\nThe save name must be at least 2 characters.\n")
		return false;
	}
}

function checkLength(text, length)
{
	if(text.length > length) {
		alert("This field has exceeded the maximum length of: "+length+" by: "+(text.length - length)+" character(s).\nExtra characters have been deleted.");
		return text.substring(0, length);
	}
	return text;
}

function checkFileExtension(name)
{
	var array = name.split('.');
	if (array.length>1) {
		var ext = array[array.length-1];
		if (ext == "jpg" || ext == "JPG" || ext == "gif" || ext == "GIF")
			return true;
	}
	alert("Please specify a *.jpg or *.gif image.");
	return false;
}

function getTodaysDate()
{
	var date = new Date();
	var todays_date = new Array();
	todays_date[0] = date.getMonth() + 1;
	todays_date[1] = date.getDate();
	todays_date[2] = date.getFullYear();
	return todays_date;
}

function dateIsValid(date)
{
	var todays_date = getTodaysDate();
	date = date.split("/");
	if(date.length == 3) {
		date[0] = date[0]*1;
		date[1] = date[1]*1;
		date[2] = date[2]*1;
		if((date[2]>=1990) &&
			 (date[2] < todays_date[2] ||
			 date[2] == todays_date[2] && date[0] < todays_date[0] ||
			 date[2] == todays_date[2] && date[0] == todays_date[0] && date[1] <= todays_date[1]))
			return true;
	}
	alert("Any date between 01/01/1990 and today is acceptable.");
	return false
}
