//Function to format floats
//returns false if invalid
//dec = number of decimal places to show
function fmtFloat(obj, dec) {
	var lt = "", rt = "", str = ""
	var inc = 0, num = 0, tmp = 0
	
	if (obj.value.length == 0)
		obj.value = "0";		

	if (!checkChars(obj.value, ".,0123456789")) return false;		
	
	// Check for valid number
	stripChars(obj, ",");
	if (!checkNumber(obj.value)) return false;
	
	//Split number
	inc = Math.pow(10, dec);
	num = (obj.value * 1.0);
	num = Math.round(num * inc) / inc;
	str = "" + num;
	
	tmp = str.indexOf(".");
	if (tmp < 0) {
		str += ".";
		tmp = str.indexOf(".");						
	}
	
	if ((str.length - tmp) <= dec)
		for (var i = (str.length - tmp); i <= dec; i++)
			str += "0";
				
	obj.value = str;
	return true;	
}

//Function to format integers
//returns false if invalid
function fmtInt(obj) {
	if (obj.value.length == 0) {
		obj.value = "0";
		return true;
	}
	
	return (checkInteger(obj.value));
}

//Function to formats money fields
//returns false if invalid
function fmtMoney(obj) {
	var dec = 0, num = "", sign = 0
	var dollars = "", cents = ""
	var count = 0, tmp = ""
	
	if (obj.value.length == 0) {
		tmp = "0";
		cents = ".00";
	} else {
		if (!checkChars(obj.value, "$.,0123456789")) return false;

		// Check for valid number		
		stripChars(obj, ",");				
		sign = obj.value.indexOf("$");		
		if (sign > 0)
			return false;
		else {
			if (sign == 0)
				num = obj.value.substring(1,obj.value.length);
			else
				num = obj.value;

			if (!checkNumber(num))
				return false;
		}

		// Check precision, round-up if necessary
		if (num.indexOf(".") != -1)
			num = "" + (Math.round(num * 100) / 100.0);
		
		dec = num.indexOf(".");
		cents = ((dec > 0) ? num.substring(dec,num.length) : ".00");
		if (cents.length == 2) cents += "0";
	
		// Add commas
		dollars = "" + parseInt(num);
		for (var i = dollars.length - 1; i >= 0; i--) {
	      if (count == 3) {
	        tmp = "," + tmp;
	      	count = 1;
	      } else  
		  	count ++;
	
	      tmp = dollars.charAt(i) + tmp;
		}	
	}
		
	// Format number
	num = "$" + tmp + cents;
	obj.value = num;	
	return true;								
}

//Function to format phone numbers
//returns false if invalid
function fmtPhone(obj) {
	var phone = "";
	var oldphone = "";
	
	if (obj.value.length == 0) return true;
	
	//Remove formatting
	oldphone = obj.value;
	stripChars(obj, "- ()");
	phone = obj.value;
	if (!checkChars(phone, "0123456789")) return false;
		
	if (phone.length == 10)
		obj.value = "(" + phone.substring(0,3) + ") " + phone.substring(3,6) +"-" + phone.substring(6,10);
	else {
		obj.value = oldphone;
		return false;
	}
		
	return true;		
}

//Function to format social security numbers
//returns false if invalid
function fmtSSN(obj) {
	var ssn = ""
	
	if (obj.value.length == 0) return true;
	
	//Remove all dashes
	stripChars(obj, "-");
	ssn = obj.value;
	if (!checkChars(ssn, "0123456789")) return false;
		
	if (ssn.length == 9)
		obj.value = ssn.substring(0,3) + "-" + ssn.substring(3,5) + "-" + ssn.substring(5,9);
	else
		return false;
		
	return true;
}

//Function to verify valid URL
function fmtURL(obj) {
	if (obj.value.length == 0) return true;
	
	if (obj.value.substring(0,7) != "http://")
		obj.value = "http://" + obj.value;
		
	if (obj.value.length < 10) return false;		
	
	return true;
}

//Function to format zip codes
//returns false if invalid
function fmtZip(obj) {
	var numberlist = "0123456789", zip = ""
	
	if (obj.value.length == 0) return true;
	
	// Remove dashes
	stripChars(obj, "-");
	zip = obj.value;
	if (!checkChars(zip, numberlist)) return false;		

	if (zip.length == 5)
		obj.value = zip;
	else if (zip.length == 9)
		obj.value = zip.substring(0,5) + "-" + zip.substring(5,9)
	else
		return false;

	//Passed all tests
	return true;		
}		

//Returns true if every character is in string list
//otherwise false
function checkChars(val, charlist) {
	for (var i = 0; i < val.length; i++)
		if (charlist.indexOf(val.charAt(i)) < 0)
			return false;

	//String passed so it is valid							
	return true;
}

//Returns true if valid email
//otherwise returns false
function checkEmail(obj) {
	var email = obj.value, name = "", host = "", tld = ""
	var atSign = 0, dot = 0
	
  if (obj.value.length != 0) {
		atSign = email.lastIndexOf("@");
		// Check for more than one @
		if (email.indexOf("@") != atSign) return false;
			
		dot = email.lastIndexOf(".");
		if (atSign > dot) return false;
			
		// Break up email into 3 sections: name@host.tld			
		name = email.substring(0, atSign);
		host = email.substring(atSign + 1, dot);
		tld = email.substring(dot + 1, email.length);
		
		// Check for illegal chars
		if (findChars(name, ":/")) return false;
		if (findChars((host + tld), "[]<>")) return false;
		
		// Check for min length
		if (name.length < 1) return false;
		if (host.length < 2) return false;
    if (tld.length < 2) return false;
	}
	
	// Valid, passed all tests
	return true;
}

//Returns true if value is a number or is NULL
//otherwise returns false	
function checkInteger(object_value) {
    if (object_value.length == 0)
        return true;
			
	if (object_value.indexOf(".") < 1)
		return checkNumber(object_value);
  else
		return false;
}

//Returns true if value is a number or is NULL
//otherwise returns false	
function checkNumber(object_value) {
    if (object_value.length == 0)
        return true;

    //Returns true if value is a number defined as
    //   having an optional leading + or -.
    //   having at most 1 decimal point.
    //   otherwise containing only the characters 0-9.
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;

    //The first character can be + - .  blank or a digit.
	check_char = start_format.indexOf(object_value.charAt(0))
    //Was it a decimal?
	if (check_char == 1)
	    decimal = true;
	else if (check_char < 1)
		return false;
        
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++) {
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1) {
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true;
		} else if (check_char == 0) {
			if (decimal || digits)	
				trailing_blank = true;
        // ignore leading blanks
		} else if (trailing_blank)
			return false;
		else
			digits = true;
	}	
	
    //All tests passed
    return true
}

//Returns true if value is a posiitve integer or is NULL
//otherwise returns false	
function checkPosInteger(object_value) {
	if (object_value.indexOf("-") != -1)
		return false;
	else
		return (checkInteger(object_value));				
}

//Function to find any char in charlist in the passed string
//Returns true if a character is found
//Otherwise false
function findChars(str, charlist) {
	for (var i = 0; i < charlist.length; i++) {
		if (str.indexOf(charlist.charAt(i)) != -1)
			return true;			
	}
	
	return false;
}

//Function to remove all occurances of chars in charlist
function stripChars(obj, charlist) {
	var i = 0, pos = 0
	var val = obj.value
	
	while (i < val.length) {
		pos = charlist.indexOf(val.charAt(i));
		if (pos != -1)
			val = val.substring(0,i) + val.substring(i + 1, val.length);
		else
			i++;
	}						
	
	obj.value = val;
	return true;
}
