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

     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 orderID() {
//Returns an orderid for payment processing
	var orderid;
	var form = document.forms[0];
	var name = form.billName.value;
	number = "";
	firstinit= "";
	lastinit = "";
	index = 0;
   	
   	if(trim(form.billName.value).length==0 || !validField(form.billName.value)) {
		alert("Name field is either empty or contains an invalid character.");
		return false;
	} else {
		atPos = name.indexOf(" ",1)
		if (atPos == -1) {
		return false;
		} else {
			index = atPos + 1;
			number= IntRandom();
			firstinit = name.charAt(0);
			lastinit = name.charAt(index);
			orderid = firstinit+lastinit+"_"+number;
			form.OrderID.value=orderid;
			return true;
		}
	}
}

function On(imgName) {
	homeOn= new Image();
	homeOn.src="images/phome.gif";
	missionOn= new Image();
	essenceOn= new Image();
	essenceOn.src="images/pessence.gif";	
	missionOn.src="images/pmission.gif";
	tenthOn= new Image();
	tenthOn.src="images/ptenth.gif";
	whoOn= new Image();
	whoOn.src="images/pstory.gif";
	sponsorsOn= new Image();
	sponsorsOn.src="images/psponsors.gif";
	newsOn= new Image();
	newsOn.src="images/pwhats.gif";
	girlsOn= new Image();
	girlsOn.src="images/pgirls.gif";
	doctoralOn= new Image();
	doctoralOn.src="images/pdoctoral.gif";
	graduatesOn= new Image();
	graduatesOn.src="images/pgraduates.gif";
	contactOn= new Image();
	contactOn.src="images/pcontact.gif";
	eduseedOn= new Image();
	eduseedOn.src="images/oneduseed.gif";
	philosophyOn= new Image();
	philosophyOn.src="images/pphilosophy.gif";
	supportusOn= new Image();
	supportusOn.src="images/psupport.gif";
	aauwOn= new Image();
	aauwOn.src="images/paauw.gif";
	techsavvyOn= new Image();
	techsavvyOn.src="images/ptechsavvy.gif";
	mediaOn= new Image();
	mediaOn.src="images/pmedia.gif";
	bookOn= new Image();
	bookOn.src="images/pslbook.gif";
	patchOn= new Image();
	patchOn.src="images/ppatch.gif";
	roxieOn= new Image();
	roxieOn.src="images/proxie.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/bhome.gif";
	essenceOff= new Image();
	essenceOff.src="images/bessence.gif";	
	missionOff= new Image();
	missionOff.src="images/bmission.gif";
	tenthOff= new Image();
	tenthOff.src="images/btenth.gif";
	whoOff= new Image();
	whoOff.src="images/bstory.gif";
	sponsorsOff= new Image();
	sponsorsOff.src="images/bsponsors.gif";
	newsOff= new Image();
	newsOff.src="images/bwhats.gif";
	girlsOff= new Image();
	girlsOff.src="images/bgirls.gif";
	doctoralOff= new Image();
	doctoralOff.src="images/bdoctoral.gif";
	graduatesOff= new Image();
	graduatesOff.src="images/bgraduates.gif";
	contactOff= new Image();
	contactOff.src="images/bcontact.gif";
	eduseedOff= new Image();
	eduseedOff.src="images/offeduseed.gif";
	philosophyOff= new Image();
	philosophyOff.src="images/bphilosophy.gif";
	supportusOff= new Image();
	supportusOff.src="images/bsupport.gif";
	aauwOff= new Image();
	aauwOff.src="images/baauw.gif";
	techsavvyOff= new Image();
	techsavvyOff.src="images/btechsavvy.gif";
	mediaOff= new Image();
	mediaOff.src="images/bmedia.gif";
	bookOff= new Image();
	bookOff.src="images/bslbook.gif";
	patchOff= new Image();
	patchOff.src="images/bpatch.gif";
	roxieOff= new Image();
	roxieOff.src="images/broxie.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 textboxes  
function validPhone(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.RSVP201111;
   	var FirstName = "";
   	var LastName = "";
	var Street = "";
   	var EmailAdd = "";
	var PhoneNum = "";
	var valFN = "";
	var valLN = "";
	var valSA = "";
	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 Street Address isn't empty or doesn't have invalid characters
		if(trim(form.txtAddress.value).length==0 || !validField(form.txtAddress.value)) {
			valSA = "Street Address field is either empty or contains an invalid character.";
			valid = valid + 1
		}else {			
			LastName= trim(form.txtAddress.value);//passed validation
			valSA = "Street Address 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 || !validPhone(form.txtPhone.value)) {
			valPN = "Phone Number field is either empty or contains an invalid character.";
			valid = valid + 1
		}else {			
			LastName= trim(form.txtPhone.value);//passed validation
			valPN = "Phone Number 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=300')
			newWindow.document.write("<HTML><HEAD><TITLE>Error Message</TITLE></HEAD>")
			newWindow.document.write("<BODY text='white' BGCOLOR='#5B64BE'><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>Street Address:</TD><TD>" + valSA + "</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);
}

