// JavaScript Document

// This is a custom script developed specifically for the Simply Smooth Jazz Band website
// Written by Leha Carpenter for Web People Media
// Copyright Web People Media 2007-8, all rights reserved
// Last modified 12/19/2007


//Data structure for dynamic creation of select box, based on category (developers, artists, etc.)
//Each category contains one or more sub-arrays, in JSON notation. Each sub-array contains a title and URL.

var data = {
	"developers" : [
					{"title": "Web People Media", "url": "http://www.webpeoplemedia.com"}
	],
	"artists" : [
				 {"title": "Teri Haggerty Studios", "url": "http://terihaggertystudios.com"},
				 {"title": "Artstorm Studio", "url": "http://www.artstormstudio.com"},
				 {"title": "Abby Bard Handwoven", "url": "http://www.abbybardhandwoven.com"}
	],
	"venues" : [
				{"title": "Upper Fourth Street", "url": "http://www.upperfourth.com"},
				{"title": "Bennett Valley Golf", "url": "http://www.bvgolf.org"}
	],
	"tech" : [
			  {"title": "SE Electronics", "url": "http://www.seelectronics.com"}
	],
	"production" : [
					{"title": "Talking House", "url": "http://www.thpro.com"}
	],
	"stores" : [
				{"title": "Zone Music", "url": "http://www.zonemusic.com"},
				{"title": "Michael's Harley", "url": "http://www.michaelsharleydavidson.com"}
	],
	"restaurants" : [
					 {"title": "Sizzling Tandoor", "url": "http://www.sizzlingtandoor.com"}
	],
	"luthiers" : [
				  {"title": "Jim Surles Guitars", "url": "http://www.jimsurlesguitars.com"}
	],
	"hairdressers" : [
					  {"title": "Debra's Corner", "url": "http://www.debrascorner.com"}
	],
	"fitness" : [
				 {"title": "Vertex Climbing", "url": "http://www.vertexclimbing.com"},
				 {"title": "Marin Dance Theater", "url": "http://www.mdt.org"},
				 {"title": "Kris Freewoman", "url": "http://www.krisfreewoman.com"}
	],
	"musicians" : [
				   {"title": "Gator Beat", "url": "http://www.gatorbeat.com"},
				   {"title": "Steve Rubardt", "url": "http://www.steverubardt.com"},
				   {"title": "Terry Bradford", "url": "http://www.terrybradford.com"},
				   {"title": "Kehau Aspen", "url": "http://www.cdbaby.com/cd/arvidson"},
				   {"title": "Elena Welch", "url": "http://www.elenawelch.com"},
				   {"title": "Blues Assassin", "url": "http://www.bluesassassinmusic.com"}
	],
	"studios" : [
				 {"title": "Studio Q Productions", "url": "http://www.studioqproductions.com"},
				 {"title": "Zone Recording", "url": "http://www.zonerecording.net"}
	],
	"entertain" : [
				 {"title": "Hot Illusions", "url": "http://www.hotillusions.com"}
	],
	"internetaccess" : [
				 {"title": "Sonic.net", "url": "http://www.sonic.net"}
	]
};

//Load a URL into the same page from a selection list passed to the function

function loadURL(list) { //"list" is the selection list
	var myurl = list.options[list.selectedIndex].value;
	if (myurl != "") {
		location.href = myurl;
	}
}

// Load a URL into a dynamically generated popup window from a selection list

function loadWindow(list) { //"list" is the selection list passed to the function
	var myurl = list.options[list.selectedIndex].value;
	if (myurl != "") {
		var features = "left=100,top=150,";
        features += "screenX=150,screenY=100,";
        features += "width=600,height=500,";
		features += "dependent=yes,";
		features += "menubar=no,";
		features += "toolbar=no,";
		features += "scrollbars=yes,";
		features += "resizable=yes";
		var myWindow = window.open(myurl, "", features);
	}
}

// Dynamically load the elements of the "data" data structure (above) into a select list, depending on the category selected by the user.

function loadElements(f) { //"f" is the form passed to the function
	with (f) {
		var category = elements["categories"].options[elements["categories"].selectedIndex].value; // get the category from the user's selection in the list
		if (category != "") { //if the user has selected something...
			var links = data[category]; //store it
			elements["urls"].options.length=0; //initialize the array
			elements["urls"].options[0]= new Option("Choose a Website...", "#"); //reset the first option to the linkless "choose" selection
			for(var i = 0; i < links.length; i++) { //build the select list of websites for the chosen category
				elements["urls"].options[elements["urls"].options.length] = new Option(links[i].title, links[i].url);
			}
		}
		else { //if the user re-selects the linkless "choose" option, reset the empty website select list
			elements["urls"].options.length=0;
			elements["urls"].options[0]= new Option("Choose a Website...", "#");
		}
	} 
}

// This function is just for debugging
function testScript() {
		alert("inside script");
}

//Preloader Code for the history navigation rollover buttons (all pages except home):

var preloader = new Array(); //an array to preload the files

//initialize each array element with an empty object
for (var i = 0; i < 6; i++) {
	preloader[i] = new Image();
}

//initialize the src property to an image filename for each element
for (var i = 0; i < 6; i++) {
	preloader[i].src = "../images/arrow" + (i+1) + ".jpg";
}

//Image rollover code for history navigation buttons:
function changeArrow(name, file) {
	if (document.images[name]){
		document.images[name].src = file;
	}
}

//Form validation and utilities for the contact form:

//Form field initial focus for the "first name" field

function prepForm(d) { //"d" is the document passed to the function
	d.contact.firstname.focus();
}

//Validate first name
//This and the last name and email functions all use the on Blur property of the input field, so I have to track and guide where the focus is to make the form validate correctly.
//I also decided to give them the option to cancel the whole form, since the validation would otherwise continue to nag them if they clicked any links to try to exit the form.
function validateFirstname(d) { //"d" is the document passed to the function
	if (d.contact.firstname.value.replace(/\s/g, "") == "") { //if the field is empty, let them choose between filling it and quitting the form 
		var choice = confirm("You must enter a first name to submit this form. Would you like to continue with the form? (If not, click \"Cancel\".)");
		if (!choice) {
			location.href = "http://www.simplysmoothjazz.com"; //they opted out, so send them back to the home page
		}
		else {
			d.contact.firstname.focus(); //they opted in, so return focus to the field (Note: the focus() method does not seem to work well in FireFox 2)
		}
	}
	if (d.contact.firstname.value.replace(/\s/g, "") != "") { // this and the code that follows ensure the focus will be in the right field after filling this field
		if (d.contact.lastname.value.replace(/\s/g, "") == "") {
			d.contact.lastname.focus();
		}
		else if (d.contact.email.value.replace(/\s/g, "") == "") {
			d.contact.email.focus();
		}
		else {
			return;
		}
	}
}

//Validate last name
//This function is similar to the last one, except for particulars with respect to the first field having come before, and fewer mandatory fields after it.
function validateLastname(d) {
	if ((d.contact.lastname.value.replace(/\s/g, "") == "") && (d.contact.firstname.value.replace(/\s/g, "") != "")) { //note the reg expr checks for any number of spaces
		var choice = confirm("You must enter a last name to submit this form. Would you like to continue with the form? (If not, click \"Cancel\".)");
		if (!choice) {
			location.href = "http://www.simplysmoothjazz.com";
		}
		else {
			d.contact.lastname.focus();
		}
	}
		if ((d.contact.firstname.value.replace(/\s/g, "") != "") && (d.contact.lastname.value.replace(/\s/g, "") != "")) {
			if (d.contact.email.value.replace(/\s/g, "") == "") {
				d.contact.email.focus();
			}
			else {
				return;
			}
	}
}

//Validate email
//This one is similar to the last two, except that it checks for a valid email address by testing the data against a longer and more exacting reg expr
function validateEmail(d) {
	validEmail = /^\S+@\S+\.(com|net|org|biz|gov|edu)$/i; //must have one or more chars, the @ sign, one or more chars, a period, and one of the 3-char extensions
	if (!(validEmail.test(d.contact.email.value)) && (d.contact.lastname.value.replace(/\s/g, "") != "") && (d.contact.firstname.value.replace(/\s/g, "") != "")) {
		var choice = confirm("You must enter a valid email address to submit this form. Would you like to continue with the form? (If not, click \"Cancel\".)");
		if (!choice) {
			location.href = "http://www.simplysmoothjazz.com";
		}
		else {
			d.contact.email.focus();
		}
	} 
}

//Validate message
//This function relies on the onSubmit() function and returns true or false depending on whether the user wants to submit the form without a message.
//You might think it strange if they did, but I was thinking maybe they would just want their name added to the mailing list and not have anything much to say,
//so I gave them the option, but warned them in case they'd simply forgotten.
function validateMessage(d) {
	if (d.contact.message.value.replace(/\s/g, "") == "") {
		var choice = confirm("Are you sure you have no message for us? (If you wish to add a message, click \"Cancel\".)");
		if (!choice) {
			d.contact.message.focus();
			return false;
		}
	}
	return true;
}
