var msCounter = 0;

function stopMultiSubmit() {
	msCounter++;
	if(msCounter > 1) { return false; }
	return true;
}

// added ' ['+f[i].name+ '] '+' to line 28,32,36 for debugging. This one is for test server only.

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('Please complete all required fields\n' + f[i].title + ' is empty.');f[i].focus();return(false);}
					}
				else if (f[i].type == "select-multiple")
					{if (f[i].selectedIndex == -1)
						{alert('Please complete all required fields\n' + 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('Please complete all required fields\n' + f[i].title + ' is empty.');f[i].focus();return(false);}
						else{
							if (f[i].name == 'email'){
								if (checkMail(f[i]) == false){
									f[i].focus();
									return(false);
								}
							}
						}
					}
			
				else
					{
						//add specific radio or checkbox validation here
						if (f[i].name == "Authorize")
							{RadioTmp=false;
							if ((f.Authorize) && (f.Authorize[0]))
								{for (j=0;j<f.Authorize.length;j++)
									{if (f.Authorize[j].checked)
										{RadioTmp=true;
										if (f.Authorize[j].value != "Accept")
											{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.Authorize[0].focus();return(false)}
										}
									}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.Authorize[0].focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_246")
							{RadioTmp=false;
							if (f.appq_246){
								if (f.appq_246.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_246.focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_247")
							{RadioTmp=false;
							if (f.appq_247){
								if (f.appq_247.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_247.focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_248")
							{RadioTmp=false;
							if (f.appq_248){
								if (f.appq_248.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_248.focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_249")
							{RadioTmp=false;
							if (f.appq_249){
								if (f.appq_249.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_249.focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_250")
							{RadioTmp=false;
							if (f.appq_250){
								if (f.appq_250.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_250.focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_251")
							{RadioTmp=false;
							if (f.appq_251){
								if (f.appq_251.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_251.focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_252")
							{RadioTmp=false;
							if (f.appq_252){
								if (f.appq_252.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_252.focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_253")
							{RadioTmp=false;
							if (f.appq_253){
								if (f.appq_253.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_253.focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_254")
							{RadioTmp=false;
							if (f.appq_254){
								if (f.appq_254.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_254.focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_255")
							{RadioTmp=false;
							if (f.appq_255){
								if (f.appq_255.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_255.focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_256")
							{RadioTmp=false;
							if (f.appq_256){
								if (f.appq_256.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_256.focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_257")
							{RadioTmp=false;
							if (f.appq_257){
								if (f.appq_257.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_257.focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "appq_258")
							{RadioTmp=false;
							if (f.appq_258){
								if (f.appq_258.checked){RadioTmp=true;}
								if (!RadioTmp)
									{alert('Please complete all required fields\n' + f[i].title + ' is not selected.');f.appq_258.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("Passwords do 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've selected an invalid date, please re-enter.");f.AVday1.focus();return(false)}
	}
}
if(f.name == "saveForm" || f.name == "saveform" ) {
		//If submitting form..disable Leave Page only for form page
		window.onbeforeunload = ""
	}
//}
//Check and stop multiple submissions
    return(stopMultiSubmit());

}


// 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.');f.focus();return(false);}
		}
	}
}

function checkMail(f){
var email
var filter =/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
if (f.value !=''){
   if (filter.test(f.value)){
   email=true
    }
   else {
    alert("Please input a valid email address!")
	f.focus();
    email=false;
    }
}
return (email)
}