/* ======================================================================

     JavaScript Source File -- Created with NetObjects ScriptBuilder

NAME: Scripts.js

AUTHOR: 

PURPOSE: Scripts for BBC Technologies Web Site
====================================================================== */

function Random() {
//Returns a random realnumber in the rangeof 0 - 1, inclusive
	today = new Date();	//uses today's date & time to randomly set the seed
	RandomSeed = today.getTime()%214783647;
	return RandomSeed/214783647;
}

function IntRandom() {
//Returns a random integer in the range 0 - 100000, inclusive
	return Math.round(Random()*99990);
}



function On(imgName) {
	homeOn= new Image();
	homeOn.src="images/lthome.gif";
	missionOn= new Image();
	missionOn.src="images/ltmission.gif";
	projectsOn= new Image();
	projectsOn.src="images/ltprojects.gif";
	contactOn= new Image();
	contactOn.src="images/ltcontact.gif";
	boardOn= new Image();
	boardOn.src="images/ltboard.gif";

	if(parseInt(navigator.appVersion) >=4) {
		if(document.images) {
			document[imgName].src = eval(imgName + "On.src");
		}
	}
}

function Off(imgName) {
	homeOff= new Image();
	homeOff.src="images/dkhome.gif";
	missionOff= new Image();
	missionOff.src="images/dkmission.gif";
	projectsOff= new Image();
	projectsOff.src="images/dkprojects.gif";
	boardOff= new Image();
	boardOff.src="images/dkboard.gif";
	contactOff= new Image();
	contactOff.src="images/dkcontact.gif";

	if(parseInt(navigator.appVersion) >=4) {
		if(document.images) {
			document[imgName].src = eval(imgName + "Off.src");
		}
	}
}	


//Clear Form
function blankFields(){
	var form=document.forms[0];	

	//clear all the text fields
	form.txtFName.value="";
	form.txtLName.value="";
	form.txtEmail.value="";
	form.txtPhone.value="";
	form.txtRole.value="";
	form.textareaComments.value="";
}

//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
}

//Check for invalid characters in the email address  
function validEmailAdd(str) {
	invalidEmailChar = "#$%^&*()<>~/:;"//variable that contains invalid characters

	if (str =="") {
		return true
	}

	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
}

//Validate data
function validate() {
   	var form = document.forms[0];
   	var FirstName = "";
   	var LastName = "";
   	var EmailAdd = "";
	var PhoneNum = "";
	var valFN = "";
	var valLN = "";
	var valEmail = "";
	var valPN = "";
	var valid = 0;

//Check to ensure field for First Name isn't empty or doesn't have invalid characters
		if(trim(form.txtFName.value).length==0 || !validField(form.txtFName.value)) {
			valFN = "First Name field is either empty or contains an invalid character.";
			valid = valid + 1
		}else {
			FirstName= trim(form.txtFName.value);//passed validation
			valFN = "First Name passed validation."
		}

//Check to ensure field for Last Name isn't empty or doesn't have invalid characters
		if(trim(form.txtLName.value).length==0 || !validField(form.txtLName.value)) {
			valLN = "Last Name field is either empty or contains an invalid character.";
			valid = valid + 1
		}else {			
			LastName= trim(form.txtLName.value);//passed validation
			valLN = "Last Name passed validation."
		}
//Check to ensure field for Phone Number isn't empty or doesn't have invalid characters
		if(trim(form.txtPhone.value).length==0 || !validField(form.txtPhone.value)) {
			valPN = "Phone Number field is either empty or contains an invalid character.";
			valid = valid + 1
		}else {			
			LastName= trim(form.txtLName.value);//passed validation
			valPN = "Last Name passed validation."
		}

//Check to ensure email address is valid
		if(trim(form.txtEmail.value).length==0 || !validEmailAdd(form.txtEmail.value)) {
			valEmail = "Email Address field is either empty or contains an invalid character.";
			valid = valid + 1
		}else {			
			EmailAddress= trim(form.txtEmail.value);//passed validation
			valEmail = "Email passed validation."
		}

//Is there invalid data
		if(valid==0) {
			return true;
		} else {
			newWindow = window.open ('','newWin','resizable=yes,width=640,height=200')
			newWindow.document.write("<HTML><HEAD><TITLE>Error Message</TITLE></HEAD>")
			newWindow.document.write("<BODY text='white' BGCOLOR='#c60000'><TABLE BORDER='0'ALIGN='CENTER'>")
			newWindow.document.write("<TR><TD COLSPAN='2'ALIGN='CENTER'>Error processing form.  Please review the following table for errors.</TD>")
			newWindow.document.write("<TR><TD COLSPAN='2'><HR></TD><TD>")
			newWindow.document.write("<TR><TD>First Name:</TD><TD>" + valFN + "</TD>")
			newWindow.document.write("<TR><TD>Last Name:</TD><TD>" + valLN + "</TD>")
			newWindow.document.write("<TR><TD>Phone Number:</TD><TD>" + valPN + "</TD>")
			newWindow.document.write("<TR><TD>Email Address:</TD><TD>" + valEmail + "</TD>")
			newWindow.document.write("<TR><TD COLSPAN='2'><HR></TD><TD>")
			newWindow.document.write("<TR><TD ALIGN='CENTER'COLSPAN='2'><FORM><INPUT TYPE='button' VALUE='Click Here To Close Window' onClick='self.close()'></FORM></TD>")
			newWindow.document.write("</TABLE></BODY></HTML>")
			return false;
		}			
}

//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] = 'Thur';
        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);
}
