// 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{
							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(f[i].title + ' is not selected.');f.Authorize[0].focus();return(false)}
										}
									}
								if (!RadioTmp)
									{alert(f[i].title + ' is not selected.');f.Authorize[0].focus();return(false)}
								}
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "rb1"){
						RadioTmp=false;
							if (f.rb1){
							if (!f.rb1.checked){
							alert(f[i].title + ' is not selected.');f.rb1.focus();return(false)
								 }
								}								
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "rb2"){
						RadioTmp=false;
							if (f.rb2){
							if (!f.rb2.checked){
							alert(f[i].title + ' is not selected.');f.rb2.focus();return(false)
								 }
								}								
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "rb3"){
						RadioTmp=false;
							if (f.rb3){
							if (!f.rb3.checked){
							alert(f[i].title + ' is not selected.');f.rb3.focus();return(false)
								 }
								}								
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "rb4"){
						RadioTmp=false;
							if (f.rb4){
							if (!f.rb4.checked){
							alert(f[i].title + ' is not selected.');f.rb4.focus();return(false)
								 }
								}								
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "rb5"){
						RadioTmp=false;
							if (f.rb5){
							if (!f.rb5.checked){
							alert(f[i].title + ' is not selected.');f.rb5.focus();return(false)
								 }
								}								
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "rb6"){
						RadioTmp=false;
							if (f.rb6){
							if (!f.rb6.checked){
							alert(f[i].title + ' is not selected.');f.rb6.focus();return(false)
								 }
								}								
							}
						//add specific radio or checkbox validation here
						if (f[i].name == "rb7"){
						RadioTmp=false;
							if (f.rb7){
							if (!f.rb7.checked){
							alert(f[i].title + ' is not selected.');f.rb7.focus();return(false)
								 }
								}								
							}
						//add specific radio or checkbox validation here
					}
				}
			}
		}
	}

	if (f.email && f.altemail) {
		if(f.email.value.trim() != f.altemail.value.trim()){
			alert("Emails do not match.");
			f.altemail.focus();
			return(false)
		}
	}

	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 (document.saveForm){
var today = new Date();
var thedate = parseInt(today.getMonth()+1) + "/" + parseInt(today.getDate()) + "/" + parseInt(today.getFullYear())
var theToday = new Date();
var theAvailabilityDate = f.AVmonth1.value + "/" + f.AVday1.value + "/" + f.AVyear1.value
var AvailabilityDate = Date.parse(theAvailabilityDate)

if (AvailabilityDate < Date.parse(thedate)){
f.AVmonth1.selectedIndex = parseInt(theToday.getMonth()+1);
f.AVday1.selectedIndex = parseInt(theToday.getDate());
f.AVyear1.selectedIndex = 1;
alert('Availability date is incorrect.\nDate has been reset too today.\nPlease select dates equal or greater then today.');
f.AVmonth1.focus();
return(false)
		}
	   }
}

if ((f.name == "saveform") || (f.name == "saveForm") || (f.name == "fsave")) {
		//If submitting form..disable Leave Page only for form page
		window.onbeforeunload = ""
	}
}

// pass the form to this function
// if form name is saveForm, then call:
// displayAnswered(document.saveForm);
function CheckAvailabilityDate(){
var today = new Date();
var thedate = parseInt(today.getMonth()+1) + "/" + parseInt(today.getDate()) + "/" + parseInt(today.getFullYear())
var AvailabilityDate = document.fsave.AVmonth1.value + "/" + document.fsave.AVday1.value + "/" + document.fsave.AVyear1.value
AvailabilityDate = parseFloat(document.fsave.AVmonth1.value) + parseFloat(document.fsave.AVday1.value) + parseFloat(document.fsave.AVyear1.value)

if (AvailabilityDate < Date.parse(thedate)){alert('Please select dates equal or greater then today.');document.fsave.AVday1.focus();return(false)}
}

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)
}

function Check_email() {
	var f = document.saveForm;
	var theemail;
	if (f.email && f.altemail) {
		if((f.email.value.trim() != f.altemail.value.trim()) || (f.email.value.trim() == '') || (f.altemail.value.trim() == '')){
			alert("Emails do not match.");
			f.altemail.focus();
			theemail = false;
			return(false);
		}
		else{
		theemail = true;
		}
	}
return(theemail);

}

	
function Check_password() {
	var f = document.saveForm;
	var thepassword;
	if (f.pwd && f.pwd1) {
		if((f.pwd.value.trim() != f.pwd1.value.trim()) || (f.pwd.value.trim() == '') || (f.pwd1.value.trim() == '')){
			alert("Passwords do not match.");
			f.pwd1.focus();
			thepassword = false;
			return(false);
		}
		else{
		thepassword = true;
		}
	}
return(thepassword);	
}