
//-------------------------------------------------------------------------------//
//--------------------------- Beginning Function isblank ------------------------//
//-------------------------------------------------------------------------------//

function isblank(s){
	//This function returns true if a string contains only
	//white space.

	for (var i = 0; 1 < s.length; i++){
		var c = s.charAt(i);
		if ((c != ' ') && (c != '\n') && (c != '\t')) return false;
	}
	return true;
}

//--------------------------- End Function isblank ------------------------------//
//-------------------------------------------------------------------------------//

//-------------------------------------------------------------------------------//
//--------------------------- Beginning Function verify -------------------------//
//-------------------------------------------------------------------------------//

function verify(f){
	//This is the function that performs form verification.  It
	//will be invoked from the OnSubmit() event handler.  The 
	//handler should return whatever value this function returns.

	var msg;
	var empty_fields = "";
	var errors = "";
	
	//Loop through the elements of the form, looking for all
	//text and textarea elements that don't have an "optional" property
	//defined.  Then, check for fields that are empty and make a list of 
	//them. Also, if any of these elements have a "min" or a "max" property 
	//defined, then verify that they are numbers and that they are in the right
	//range.  Put together error messages for fields that are wrong.
	
	for (var i = 0; i < f.length; i++){
		var e = f.elements[i];
		if (((e.type == "text") || (e.type == "textarea")) && e.required){
			//First lets check to see if the field is empty
			if ((e.value == null) || (e.value == "") || isblank(e.value)){
				empty_fields += "\n        " + e.name;
				continue;
			}
			
			//Now lets check the fields that are supposed to e numeric
			if (e.numeric ||(e.min != null) || (e.max != null)){
				var v = parseFloat(e.value);
				if (isNaN(v) || ((e.min != null) && (v < e.min)) || ((e.max != null) && (v > e.max))){
					errors += " - The Field " + e.name + " must be a number";
					if (e.min != null)
						errors += " that is greater than " + e.min;
					if (e.max != null && e.min != null)
						errors += " and less than " + e.max;
					else if (e.max != null)
						errors += " that is less than " + e.max;
					errors += ".\n";
				}
			}
		}
	}
	
	//Now if there were any errors, display the messages, and reutrn
	//false to prevent the form from be submitted. Otherwise return true.
	if (!empty_fields && !errors) return true;
	
	msg = "_______________________________________________________\n\n"
	msg += "The form was not submitted becaues of the following error(s).\n";
	msg += "Please correct these error(s) and re-submit.\n";
	msg += "________________________________________________________\n\n"
	
	if (empty_fields){
		msg += " - The following required field(s) are empty:" + empty_fields + "\n";
		if (errors) msg += "\n";
	}
	msg += errors;
	alert(msg);
	return false;
}

//--------------------------- End Function verify -------------------------------//
//-------------------------------------------------------------------------------//

//-------------------------------------------------------------------------------//
//--------------------------- Beginning Function scrubdata ----------------------//
//-------------------------------------------------------------------------------//
function scrubdata(inputvalue){
	//This function deletes digits that aren't numbers or periods.  If a non money
	//based digit is encountered (something other than a "$" , a "," or "." or
	//space) an error is displayed.  This function is called by asmoney().
	
	var returnvalue = "";
	for (var i = 0; i < inputvalue.length; i++){
		var digit = inputvalue.charAt(i);
		if (parseFloat(digit) || digit == "." || digit == "0"){
			//the digit is a number or period. So keep it.
			returnvalue += digit
		}
		else {
			if (digit != " " && digit != "$" && digit != ","){
				//something wierd encountered
				alert("Please enter a non-zero numeric value.");
				break;
			}
		}
	}
	return returnvalue;
}

//--------------------------- End Function scrubdata ----------------------------//
//-------------------------------------------------------------------------------//

//-------------------------------------------------------------------------------//
//--------------------------- Beginning Function asmoney ------------------------//
//-------------------------------------------------------------------------------//
function asmoney(inputvalue){
	//First lets make sure the input data is clean
	var scrubvalue =  scrubdata(inputvalue)
	
	//Lets declare some temp variables
	var returnstring = ""
	var tempnumber = 0
	var tempstring = ""
	tempnumber = Math.round(scrubvalue * 100)
	
	//Manipulate the number so we know what we're dealing with
	if (tempnumber < 10) {
		tempstring = "00" + tempnumber
	}
	else if (tempnumber < 100){
		tempstring = "0" + tempnumber
	}
	else {
		tempstring = "" + tempnumber
	}
	
	if (tempstring.length > 9){
		alert("Sorry, can't process numbers greater than 1 million")
		return scrubvalue
	}
	
	//This next session builds the return string by starting with
	//a dollar sign, then breaking up the number into sections.
	//then placing commas and periods between the sections as appropriate.
	
	returnstring = "$"
	if (tempstring.length > 5) {
		//Need to add at least 1 comma
		if (tempstring.length == 6) {
			returnstring += tempstring.substring(0,1) + "," +
											tempstring.substring(1,tempstring.length - 2)
		}
		else if (tempstring.length == 7) {
			returnstring += tempstring.substring(0,2) + "," +
											tempstring.substring(2,tempstring.length - 2)
		}
		else if (tempstring.length == 8) {
			returnstring += tempstring.substring(0,3) + "," +
											tempstring.substring(3,tempstring.length - 2)
		}
		else if (tempstring.length == 9) {
			//Need to add 2 commas
			returnstring += tempstring.substring(0,1) + "," +
											tempstring.substring(1,4) + "," +
											tempstring.substring(4,tempstring.length - 2);
		}
	}
	else {  //no commas necessary
		returnstring = "$" + tempstring.substring(0,(tempstring.length-2))
	}
	//Add the cents
	//returnstring += "." + tempstring.substring((tempstring.length - 2),tempstring.length)
	//return the newly formatted string back to the caller
	return returnstring
}

//--------------------------- End Function asmoney ------------------------------//
//-------------------------------------------------------------------------------//

//-------------------------------------------------------------------------------//
//--------------------------- Beginning Function isanumber ----------------------//
//-------------------------------------------------------------------------------//
function isanumber(inputvalue){
	if (inputvalue != ""){
		if (inputvalue != "0"){
			if (!parseFloat(inputvalue)){
				alert("please enter a numeric value.")
			}
			else {
				for (var i=0; i < inputvalue.length; i++){
					if (inputvalue.charAt(i) != " ") {
						if (!parseFloat(inputvalue.charAt(i))){
							alert("Please enter a numeric value.")
							break
						}
					}
				}
			}
		}
	}
}

//--------------------------- End Function isanumber ----------------------------//
//-------------------------------------------------------------------------------//
function isadecimal(inputvalue)
{
	if (inputvalue != "")
	{
		if (inputvalue != "0")
		{
			for (var i=0; i < inputvalue.length; i++)
			{
				if (inputvalue.charAt(i) != " ")
				{
					if (inputvalue.charAt(i) != ".")
					{
						if (inputvalue.charAt(i) != "0")
						{
							if (!parseFloat(inputvalue.charAt(i)))
							{
								alert("Please enter a numberic value.")
								break
							}
						}
					}
				}
			}
		}
	}
}


//----------------------------------------------------------------
//-- dateCheck()
//----------------------------------------------------------------
function dateCheck(w_date) {

  /*
  Attempts to check a date (this is a little hard with JavaScript)
  Rules:  
      must be in the format of mm/dd/yyyy
      1 <= w_month <= 12
      1 <= w_days  <= Appropriate # of days for that month/year combo
  */

  var w_ok=true;  
  var w_d=w_date.toString();

  if (w_d.length!=10) w_ok=false;
  if (!allDigitsDate(w_d)) w_ok=false;
  
  // Extract values
  var w_year=parseInt(w_date.substring(6,10));
  var w_days=parseInt(w_date.substring(3,5)); 
  var w_month1=parseInt(w_date.substring(0,2));  
  var w_month2=parseInt(w_date.substring(1,2));
  var w_month= (w_month1 <= w_month2) ? w_month2 : w_month1 ;
  if (w_month<1 || w_month>12) w_ok=false;
  if (w_days<1 || w_days>getDaysInMonth(w_month,w_year) ) w_ok=false;
  return w_ok;
  
}

function getDaysInMonth(month,year)  {

    var days;
    if (month==1 || month==3 || month==5 || month==7 || month==8 ||
        month==10 || month==12)  days=31;
    else if (month==4 || month==6 || month==9 || month==11) days=30;
    else if (month==2)  days=28;
    return (days);
}
//----------------------------------------------------------------
