var invalid_item = null;


// sort of a forward declaration of these functions.  Later they can be
// redefined by a specific field
// so the form will validate them at submit time
function validate_dodaac(){null;};
function validate_otcsn(){null;};
function validate_otcpn(){null;};
function validate_nsn(){null;};
function validate_sn(){null;};
function validate_spc(){null;};
function validate_ero(){null;};
function validate_contract_number(){null;};
function validate_requisition_number(){null;};
function validate_work_order_number(){null;};

// this function finds out what needs to be validated...
function form_validate(){
   validate_dodaac();
  	if (invalid_item != null)
	   return false;
	
	validate_work_order_number();
  	if (invalid_item != null)
	   return false;

   validate_otcsn();
  	if (invalid_item != null)
	   return false;

   validate_otcpn();
  	if (invalid_item != null)
	   return false;

   validate_nsn();
  	if (invalid_item != null)
	   return false;

   validate_sn();
  	if (invalid_item != null)
	   return false;

   validate_spc();
  	if (invalid_item != null)
	   return false;

   validate_ero();
  	if (invalid_item != null)
	   return false;

   validate_contract_number();
  	if (invalid_item != null)
	   return false;

   validate_requisition_number();
  	if (invalid_item != null)
	   return false;
      
   return true;

}


// On submit code ---
// to check for errors or something like that...
// right now what we'll do is return false from this function
// so the user can't just hit enter to submit a one-field form.
function my_on_submit () {
   return form_validate();
   
}



// form_submit submits the form after checking to make sure all items are valid
function form_submit(form_name) {

   form_validate();
   
	if (invalid_item == null)
			document[form_name].submit();
	
}




function monthDays(month, year) {

	switch (month) {
		
		case "9":
		case "4":
		case "6":
		case "11":
			return 30;
		case "2":
			return 28 + isLeapYear(year);
		default:
 			return 31;
	}
}


function monthDesc (month) {

	switch (month) {
		
		case "1":
			return "January";
		case "2":
			return "February";
		case "3":
			return "March";
		case "4":
			return "April";
		case "5":
			return "May";
		case "6":
			return "June";
		case "7":
			return "July";
		case "8":
			return "August";
		case "9":
			return "September";
		case "10":
			return "October";
		case "11":
			return "November";
		case "12":
			return "December";
      default:
         return "Oops, month is greater than 12.";
				
	}

}

function isLeapYear(y) {

	var isLeapYear = 0;
	
    if (y%4 == 0) {
		isLeapYear = 1;
		
        if (y%100 == 0) {
        	isLeapYear = 0;
			
            if (y%400 == 0) {
            	isLeapYear = 1;
            }
        }
    }
    else { 
		isLeapYear = 0;
	}
		
	return isLeapYear;
}


function validate_numbers_and_letters(field, required) {
	var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
	var ok = "yes";
	var temp;
   
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == -1) 
			ok = "no";
	}
	
	if (ok == "no") {
		alert("Invalid entry:  Only characters and numbers are accepted.");
		window.location.reload();
		field.focus();
		field.select();
		invalid_item = field;
	}
	else if (required && field.value.length == 0) {
		alert("Invalid entry: This field requires a value.");
		window.location.reload();
		field.focus();
		field.select();
		invalid_item = field;
	}
	else {
		invalid_item = null;
   }
}



function validate_integer(field, required) {
	var valid = "0123456789";
	var ok = "yes";
	var temp;
	
   if (invalid_item != null && invalid_item != field)
       return false;
   
	
	for (var i=0; i<field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == -1) 
			ok = "no";
	}
	
	if (ok == "no") {
		alert("Invalid entry:  Only integer numbers are accepted.");
		field.focus();
		field.select();
		invalid_item = field;
	}
	else if (required && field.value.length == 0) {
		alert("Invalid entry: This field requires a value.");
		field.focus();
		field.select();
		invalid_item = field;
	}
	else {
		invalid_item = null;
   }

   
}




function validate_date(field, required) {


	var valid = "0123456789";
	var delim = "/.-\|,";
	var ok = "yes";
	var temp;
	var message = "";
	var slash1 = -1;  // where is the first delimiter
	var slash2 = -1;  // where is the second delimiter
	var format = "";

	if (field.value.length == 0)  
		null; // no value is okay...
		
	else if (field.value.length > 5) { // normal date format...
		
		format  = "'mm/dd/yyyy'";
		
		if (field.value.length < 8) {   // must be long enough for mm/dd/yyyy
		   ok      	= "no";
		   message 	= "The value you have entered is too short.";
		}
		
		else if (field.value.length > 10) {  // must be short enough for mm/dd/yyyy
		   ok 		= "no";
		   message 	= "The value you have entered is too long.";
		}
		
		else {
		
			// loop through all the chars of the string
			for (var i=0; i < field.value.length && ok == "yes"; i++) {  
			
				temp = "" + field.value.substring(i, i+1);   // grab the next char
				
				// this means we have a valid char
				if (valid.indexOf(temp) != -1) {             
					// check to see if this is the last char and we don't have 2 slashes yet.    
					if (i == (field.value.length - 1) && slash2 == -1 && slash1 != -1) {
						ok = "no";
						message = "Two slashes are required for this type of date.";
					} 
				}
					
				// this means we have found a delimiter
				else if (delim.indexOf(temp) != -1) {
					
					if (slash1 == -1)		// found the 1st slash - OK
						slash1 = i;
					else if (slash2 == -1)	// found the 2nd slash - OK
					 	slash2 = i;
					else {					// found the 3rd slash - Uh-Oh!
						ok = "no";
						message = "Only two slashes are allowed for this type of date.";
					}
				}
				// Oops! found something besides a number or delimiter
				else {
					ok = "no";
					message = "Only numbers and slashes are allowed for dates.";
				}
	
			}
		}
				
		if (ok == "yes"){
			var month = field.value.substring(0,slash1);
			var day   = field.value.substring(slash1+1,slash2);
			var year  = field.value.substring(slash2+1,field.value.length);

			if (month > 12 || month < 1 || month.length > 2) {
				ok = "no";
				message = message + "The month must be between 1 and 12.";
			}
			else if ( (day > monthDays(month, year)) || day < 1 || day.length > 2) {
				ok = "no";
				message = message + "A day in " + monthDesc(month) + ", " + year + " must be between 1 and " + (monthDays(month, year)) + ".";
			}
			else if (year > 9999 || year < 1000 || year.length > 4) {
				ok = "no";
				message = message + "The year must be between 1000 and 9999.";
			}
		}
					
			
	}
	else {
		format = "'yyddd'";
		
		if (field.value.length < 5) {   // must be long enough for yyddd
		   ok      	= "no";
		   message 	= "The value you have entered is too short.";
		}
		
		else if (field.value.length > 5) {  // must be short enough for yyddd
		   ok 		= "no";
		   message 	= "The value you have entered is too long.";
		}
		
		else {
		
			// loop through all the chars of the string
			for (var i=0; i < field.value.length && ok == "yes"; i++) {  
			
				temp = "" + field.value.substring(i, i+1);   // grab the next char
				
				// this means we have a valid char
				if (valid.indexOf(temp) != -1)
					null; // everything's still okay
					
				else {
					ok = "no";
					message = "Only numbers are allowed for this type of date.";
				}
	
			}
	


			if (ok == "yes"){
				var year = field.value.substring(0,2);
				var day  = field.value.substring(2,5);
	
				if ( year > 99 || year < 00){
					ok = "no";
					message = message + "The year must be between 00 and 99.";
				}
				
				else if (day > (365 + isLeapYear(year))  || day < 1){
					ok = "no";
					message = message + "The day of the year " + year + " must be between 1 and " + (365 + isLeapYear(year)) + ".";
				}
	
			}
			
					
			
		}
		
	}


	message = "It looks like you're trying to enter a " + format + " date.  " + message;
	
	if (ok == "no") {
		alert("Invalid Date:  " + message);
		field.focus();
		field.select();
		invalid_item = field;
	}
	else if (required && field.value.length == 0) {
		alert("Invalid entry: This field requires a value.");
		field.focus();
		field.select();
		invalid_item = field;
	}
	else {
		invalid_item = null;
    }
   
  
}



