// added ' ['+f[i].name+ '] '+' to line 36 for debugging

String.prototype.trim = function()
{
	// replace leading and trailing spaces with the empty string
	return this.replace(/(^\s*)|(\s*$)/g, "");
}

function DaysInMonth(WhichMonth, WhichYear)
{
  var DaysInMonth = 31;
  if (WhichMonth == 4 || WhichMonth == 6 || WhichMonth == 9 || WhichMonth == 11) DaysInMonth = 30;
  if (WhichMonth == 2 && (WhichYear/4) != Math.floor(WhichYear/4))	DaysInMonth = 28;
  if (WhichMonth == 2 && (WhichYear/4) == Math.floor(WhichYear/4))	DaysInMonth = 29;
  return DaysInMonth;
}

function valid(f) {

	var i, j, RadioTmp;
	var l = f.length;
	for(i=0;i<l;i++){
		if (f[i].title)
		{if (!(f[i].style.display) || ((f[i].style.display) && !(f[i].style.display == "none")))
			{if (f[i].type)
				{if (f[i].type == "select-one")
					{if ((f[i].selectedIndex == -1) || (f[i].selectedIndex == 0))
						{alert(f[i].title+ ' is empty.');f[i].focus();return(false);}
					}
				else if (f[i].type == "select-multiple")
					{if (f[i].selectedIndex == -1)
						{alert(f[i].title+ ' is empty.');f[i].focus();return(false);}
					}
				else if (((f[i].type == "text") || (f[i].type == "textarea")) || (f[i].type == "password"))
					{if (f[i].value.trim() == "")
						{alert(f[i].title+' is empty.');f[i].focus();return(false);}
					}
				else
					{
						//add specific radio or checkbox validation here
						if (f[i].name == "Answer6")
							{RadioTmp=false;
							if ((f.Answer6) && (f.Answer6[0]))
								{for (j=0;j<f.Answer6.length;j++)
									{if (f.Answer6[j].checked)
										{RadioTmp=true;
										}
									}
								if (!RadioTmp)
									{alert(f[i].title+ ' is not selected.');f.Answer6[0].focus();return(false)}
								}
							}
							
							if (f[i].name == "Answer3")
							{RadioTmp=false;
							if ((f.Answer3) && (f.Answer3[0]))
								{for (j=0;j<f.Answer3.length;j++)
									{if (f.Answer3[j].checked)
										{RadioTmp=true;
										}
									}
								if (!RadioTmp)
									{alert(f[i].title+ ' is not selected.');f.Answer3[0].focus();return(false)}
								}
							}
							
							if (f[i].name == "Answer4")
							{RadioTmp=false;
							if ((f.Answer4) && (f.Answer4[0]))
								{for (j=0;j<f.Answer4.length;j++)
									{if (f.Answer4[j].checked)
										{RadioTmp=true;
										}
									}
								if (!RadioTmp)
									{alert(f[i].title+ ' is not selected.');f.Answer4[0].focus();return(false)}
								}
							}
							
							if (f[i].name == "Answer5")
							{RadioTmp=false;
							if ((f.Answer5) && (f.Answer5[0]))
								{for (j=0;j<f.Answer5.length;j++)
									{if (f.Answer5[j].checked)
										{RadioTmp=true;
										}
									}
								if (!RadioTmp)
									{alert(f[i].title+ ' is not selected.');f.Answer5[0].focus();return(false)}
								}
							}
							
							if (f[i].name == "Signature"){
							RadioTmp=false;
							if (f.Signature){
								if (f.Signature.checked){
								RadioTmp=true;
										}
									}
								if (!RadioTmp)
									{alert(f[i].title+ ' is not selected.');f.Signature.focus();return(false)}
								}
						//add specific radio or checkbox validation here
					}
				}
			}
		}
	}

	if (f.pwd && f.pwd1) {
		if(f.pwd.value.trim() != f.pwd1.value.trim()){
			alert("Password does not match.");
			f.pwd1.focus();
			return(false)
		}
	}

if (((f.AVday1) && (f.AVmonth1)) && (f.AVyear1)) {
var dd;
var mm;
var mmTxt
var yy;

	dd = f.AVday1[f.AVday1.selectedIndex].value;
	mm = f.AVmonth1[f.AVmonth1.selectedIndex].value;
	yy = f.AVyear1[f.AVyear1.selectedIndex].value;
	if(dd !=0 && mm != "" && yy !=0)
	{
		if(dd>DaysInMonth(mm,yy)){alert("You selected invalid date, please re-enter.");f.AVday1.focus();return(false)}
	}
}

}

function validate_form(f)
{
var formObj = self.document.forms['saveForm'];
    if(formObj == null) {
        formObj = self.document.forms['fsave'];
    }

if (valid(f)!= false){
        if(formObj['Authorize']){
			formObj['Authorize'].disabled = true;}
		if(formObj['resett']){
		    formObj['resett'].disabled = true;}
  }
  else return (false)
}
// pass the form to this function
// if form name is saveForm, then call:
// displayAnswered(document.saveForm);

function displayAnswered(f) {
	var i, j, RadioTmp;
	var l = f.length;
	for(i=0;i<l;i++){
		if (f[i].type)
			{if (f[i].type == "select-one")
				{if (!((f[i].selectedIndex == -1) || (f[i].selectedIndex == 0)))
					{if (f[i].style.display){f[i].style.display = '';}}
				}
			else if (f[i].type == "select-multiple")
				{if (!(f[i].selectedIndex == -1))
					{if (f[i].style.display){f[i].style.display = '';}}
				}
			else if (((f[i].type == "text") || (f[i].type == "textarea")) || (f[i].type == "password"))
				{if (f[i].value.trim() != "")
					{if (f[i].style.display){f[i].style.display = '';}}
				}
			else
				{
					//hope they don't have hidden radio buttons or checkboxes :)
				}
			}
		}
}


function appq_checkNumeric(f){
	if (f){
		if (f.value != ''){
			if (isNaN(f.value))
			{alert('Value must be numeric, if entering currency, omit "$" and ",".');f.focus();return(false);}
		}
	}
}
