/* ======================================================================

     JavaScript Source File -- Created with NetObjects ScriptBuilder

NAME: Scripts.js

AUTHOR: Donya M. Douglas

PURPOSE: Validate the data in any/all of the BC PTA Council forms.

====================================================================== */
//Get current date.  
	function dates() {
        months = new Array(13);
        months[0] = 'Jan';
        months[1] = 'Feb';
        months[2] = 'Mar';
        months[3] = 'Apr';
        months[4] = 'May';
        months[5] = 'Jun';
        months[6] = 'Jul';
        months[7] = 'Aug';
        months[8] = 'Sep';
        months[9] = 'Oct';
        months[10] = 'Nov';
        months[11] = 'Dec';
        
        days = new Array(7);
        days[0] = 'Sun';
        days[1] = 'Mon';
        days[2] = 'Tue';
        days[3] = 'Wed';
        days[4] = 'Thu';
        days[5] = 'Fri';
        days[6] = 'Sat';
                        
        today = new Date();
        tmonth = months[today.getMonth()];
        tdate = today.getDate();
        tyear = today.getFullYear();
        tday  = days[today.getDay()];
        document.write(tday + " " + " - " + "  " + tmonth +" "+ tdate +", "+tyear);
	}

function On(imgName) {
	contactOn= new Image();
	contactOn.src="images/ocontact.gif";
	feedbackOn= new Image();
	feedbackOn.src="images/ofeedback.gif";
	nptaOn= new Image();
	nptaOn.src="images/onpta.gif";
	bcpsOn= new Image();
	bcpsOn.src="images/obcps.gif";
	presidentOn= new Image();
	presidentOn.src="images/opresidents.gif";

	if(parseInt(navigator.appVersion) >=4) {
		if(document.images) {
			document[imgName].src = eval(imgName + "On.src");
		}
	}
}

function Off(imgName) {
	contactOff= new Image();
	contactOff.src="images/contact.gif";
	feedbackOff= new Image();
	feedbackOff.src="images/feedback.gif";
	nptaOff= new Image();
	nptaOff.src="images/npta.gif";
	bcpsOff= new Image();
	bcpsOff.src="images/bcps.gif";
	presidentOff= new Image();
	presidentOff.src="images/presidents.gif";


	if(parseInt(navigator.appVersion) >=4) {
		if(document.images) {
			document[imgName].src = eval(imgName + "Off.src");
		}
	}
}	
	
	
//Check to ensure email address is valid
function checkEmail() {
   	var form = document.mailinglist;
	if(trim(form.txtEmail.value).length==0 || !validEmailAdd(form.txtEmail.value)) {
		alert ("Email address is either empty or is invalid.");
		return false;
	}else {			
		return true;
	}
}
//Check for invalid characters in the email address
function validEmailAdd(str) {
	invalidEmailChar = "#$%^&*()<>~/:;"//variable that contains invalid characters

	if (str =="") {
		return false;
	}

	for ( var j=0; j < invalidEmailChar.length; j++) {//scan through the string
		badEmailChar=invalidEmailChar.charAt(j);
		if (str.indexOf(badEmailChar,0) != -1) {
			return false;
		}
	}
	
	atPos=str.indexOf("@", 1)
	if (atPos == -1) {
		return false;
	}

	if(str.indexOf("@", atPos+1) != -1) {
		return false;
	}

	periodPos = str.indexOf(".", atPos)
	if (periodPos+3 > str.length) {
		return false;
	}
	return true;//no bad character found
}

//Trim the leading and trailing spaces from the keyword string
//If string is all spaces, it returns and empty string.
function trim(str) {
   var i = str.length;
   var x = 0;

   while (str.substring(x,x+1) == ' ') x++;//get rid of the leading blank spaces
   while (str.substring(i-1,i) == ' ') i--;//get rid of the ending blank spaces

   if(x == str.length && i == 0)
      return '';//if the feild is all empty spaces return an empty string
   else
     	return str.substring(x,i);//return the stripped string
}		

//Check for invalid characters in the textboxes  
function validField(str) {
	invalidChar = "@#$%^&*()<>~/:;"//create a variable that contains invalid characters
	for ( var j=0; j < invalidChar.length; j++) {//start a loop that scans through the invlaidChar string
		badChar=invalidChar.charAt(j);//save the position of the invalidChar
		if (str.indexOf(badChar,0) != -1) {/*look for position of character in str, if position is -1,
the character isn't in the string*/
			return false;//return false if the string has a bad character
		}
	}
	return true;//no bad character found
}


//Validate questionaire data
function validateQuest() {
   	var form = document.forms[0];
   	var FirstName = "";
   	var LastName = "";
   	var EmailAdd = "";
	var valCat = ""
	var valFN = ""
	var valLN = ""
	var valEmail = ""
	var valid = 0

//Check to ensure that a catagory has been selected

		var catChoice=form.lstCategory.selectedIndex;
		if(form.lstCategory.options[catChoice].value=="") {
			alert("Please select a category.");
			return false;
		}

//Check to ensure field for First Name isn't empty or doesn't have invalid characters
		if(trim(form.txtFirst.value).length==0 || !validField(form.txtFirst.value)) {
			alert("First Name field is either empty or contains an invalid character.");
			return false;
		}

//Check to ensure field for Last Name isn't empty or doesn't have invalid characters
		if(trim(form.txtLast.value).length==0 || !validField(form.txtLast.value)) {
			alert("Last Name field is either empty or contains an invalid character.");
			return false;
		}

//Check to ensure email address is valid
		if(trim(form.txtEmail.value).length==0 || !validEmailAdd(form.txtEmail.value)) {
			alert("Email is invalid.");
			return false;
		}

//Is there invalid data
		if(valid==0) {
			return true;
		}
			
}	

//Clear Form
function blankQuestFields(){
	var form=document.forms[0];	

	//clear all the text fields
	form.txtFirst.value="";
	form.txtLast.value="";
	form.txtTitle.value="";
	form.txtTime.value="";
	form.txtEmail.value="";
	form.txtPhone.value="";
	form.txtZipCode.value="";

	//set list boxes back to first option in list
	form.lstCategory.options[0].selected=true;
	form.lstState.options[0].selected=true;

	//set radio button values to false
	form.quest1[1].checked=true;
	form.quest2[1].checked=true;
	form.quest3[1].checked=true;
	form.quest4[1].checked=true;
	form.quest5[1].checked=true;
	form.quest6[1].checked=true;
	form.quest7[1].checked=true;
	form.quest8[1].checked=true;
	form.quest9[1].checked=true;
	form.quest10[1].checked=true;
}

//Clear Form
function blankdonationFields(){
	var form=document.forms[0];	

	//clear all the text fields
	form.txtName.value="";
	form.txtAddress.value="";
	form.txtDayPhone.value="";
	form.txtEvePhone.value="";
	form.txtCity.value="";

	//set list boxes back to first option in list
	form.lstAttend.options[0].selected=true;
	form.lstDonation.options[0].selected=true;
	form.lstSponsor.options[0].selected=true;
	form.lstTraining.options[0].selected=true;
}

//Validate donation form data
function validateDonation() {
   	var form = document.forms[0];

//Check to ensure field for Name isn't empty or doesn't have invalid characters
		if(trim(form.txtName.value).length==0 || !validField(form.txtName.value)) {
			alert("Name field is either empty or contains an invalid character.");
			return false;
		}

//Check to ensure field for Address isn't empty or doesn't have invalid characters
		if(trim(form.txtAddress.value).length==0) {
			alert("Address field is empty.");
			return false;
		}

//Check to ensure email address is valid
		if(trim(form.txtDayPhone.value).length==0) {
			alert("Please provide a day time phone number.");
			return false;
		}

//Check to ensure field for City, State, and Zip Code isn't empty or doesn't have invalid characters
		if(trim(form.txtCity.value).length==0) {
			alert("Please provide the city state and zip code where you live.");
			return false;
		}
}	
