/*
Used for form validation

*/

function field_empty(z,description) {
  var x = (z.value);
  if (z.value=='') {
    alert("The following information is required: " + description);
    z.focus();
    return true;
  }
  return false;
}

function CheckNum(z,description) {
  var x = (z.value);
  if (isNaN(x)){
    alert("Please enter a valid " + description + "\n (must be numeric).");
    z.select();
    z.focus();
    return true;
  }
  return false;
}

function CheckSelect(obj, description) {
	if (obj.options[obj.selectedIndex].value == 0) {
		alert('Please make a selection from the '+description+' menu');
		obj.focus();
		return true;
	} else {
		return false;
	}
}

function checkdate(dtfld,descr) {
	var err = 0;
	var dtstr = dtfld.value;
	var valid = "0123456789/";
	var temp;
	for (var i=0; i< dtstr.length; i++) {
		temp = "" + dtstr.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") err = 1;
	}

	if (dtstr.length == 0) return true;
	
	if (dtstr.length != 8) err=1 ;

	
	// If the date format is "mm/dd/yy" then change (0, 2) in the next line to (3, 5). It.s a good idea to change the day comment to month as well !
	
	d = dtstr.substring(0, 2) // day
	c = dtstr.substring(2, 3)// '/'
	
	// If the date format is "mm/dd/yy" then change (3, 5) in the next line to (0, 2).
	
	b = dtstr.substring(3, 5) // month
	e = dtstr.substring(5, 6)// '/'
	//f = dtstr.substring(6, 10) // year
	f = dtstr.substring(6, 8) // year
	
	if (b<1 || b>31) { err = 1; }
	
	if (c != '/') { err = 1; }
	if (d<1 || d>12) { err = 1; }
	if (e != '/') { err = 1; }
	
	// The next line contains the valid years
	
	//if (f<1900 || f>2099) err = 1
	//if (f<00 || f>99) err = 1
	if (d==4 || d==6 || d==9 || d==11){
		if (b==31) err=1 ; 
	}
	if (d==2){
		var g=parseInt(f/4)
		if (isNaN(g)) {
			err=1 ; 
		}
		if (b>29) err=1 ; 
		if (b==29 && ((f/4)!=parseInt(f/4))) err=1; 
	}
	
	// Test date is correctly formatted
	
	if (err==1) {
		alert('The ' + descr + ' field must be entered in dd/mm/yyyy format.');
		dtfld.focus();
		dtfld.select();
		return false;
	
	}else {
		return true;
	}

}


