// Blurring code for form field 'field'
function blurField(field) {
	field.disabled = true;
	field.style.background = "#BBBBBB";
	return true;
}

// Unblurring code for form field 'field'
function unblurField(field) {
	field.disabled = false;
	field.style.background = "#FFFFFF";
	return true;
}

// Formats float 'expr' to 'decplaces' number of decimal places
function format(expr, decplaces) {
	var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces))
	while (str.length <= decplaces) {
		str = "0" + str;
	}
	var decpoint = str.length - decplaces;
	if (decplaces > 0) return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
	else return str.substring(0,decpoint);
}

// Checks to see if 'theField' is a valid email address
function getFront(mainStr,searchStr){
	foundOffset = mainStr.indexOf(searchStr);
	if (foundOffset <= 0) {
		return null;
	} else {
		return mainStr.substring(0,foundOffset);
	}
}
function getEnd(mainStr,searchStr) {
	foundOffset = mainStr.indexOf(searchStr);
	if (foundOffset <= 0) {
		return "";
	} else {
		return mainStr.substring(foundOffset+searchStr.length,mainStr.length);
	}
}
function isValidEmail(theField) {
	if((getFront(theField.value,"@") != null) && (getEnd(theField.value,"@") != "")) {
		return true;
	} else {
		return false;
	}
}

// Checks to see if 'theField' is a valid phone number with area code
function LengthValid(theField) {
	newNumber = ""
	for (var i = 0; i < theField.value.length; i++) {
		parseResut = "" + (parseInt(theField.value.charAt(i)));
		if (parseResut.length == 1) {
			newNumber = newNumber + "" + parseResut;
		}
	}   
	if (newNumber.length >= 10) {
		return true;
	} else {
		return false;
	}
}
function isValidPhoneNumber(theField) {
	if (LengthValid(theField) == true) {
		return true;
	} else {
		return false;
	}       
}

// Check to see if 'adate' is a valid date in YYYY-MM-DD format
function isValidDate(adate) {

	var blown_date = adate.value.split("-");

	// If there are the correct amount of fields in the split array, proceed
	if (blown_date.length == 3) {
		// If the 3 fields in the split array are the right lengths, proceed
		if (
			(blown_date[0].length <= 4) && (blown_date[0].length > 0) &&
			(blown_date[1].length <= 2) && (blown_date[1].length > 0) &&
			(blown_date[2].length <= 2) && (blown_date[2].length > 0)
		) {
			// If the 3 fields in the split array are numbers, proceed
			if (
				(!isNaN(blown_date[0])) &&
				(!isNaN(blown_date[1])) &&
				(!isNaN(blown_date[2]))
			) {
				// Fix 0-in-front problem (stupid fucking javascript)
				if (blown_date[1].substr(0,1) == '0') blown_date[1] = blown_date[1].substr(1,1);
				if (blown_date[2].substr(0,1) == '0') blown_date[2] = blown_date[2].substr(1,1);

				// If the numbers are within their proper ranges, proceed
				if (
					(parseInt(blown_date[1]) >= 1) && (parseInt(blown_date[1]) <= 12) &&
					(parseInt(blown_date[2]) >= 1) && (parseInt(blown_date[2]) <= 31)
				) {
					return true;
				} else return false;
			} else return false;
		} else return false;
	} else return false;
}

// Check to see if 'num' is a valid percentage value
function isValidPercent(num) {
	if (
		(!isNaN(num.value)) &&
		(num.value != '') &&
		(parseFloat(num.value) >= 0) &&
		(parseFloat(num.value) <= 100)
	   ) return true;
	else return false;
}
