/* IMG ROLLOVER **********************************/
function SimpleSwap(el,which){
	el.src=el.getAttribute(which||"origsrc");
}

function SimpleSwapSetup(){
  var x = document.getElementsByTagName("img");
  for (var i=0;i<x.length;i++){
    var oversrc = x[i].getAttribute("oversrc");
    if (!oversrc) continue;
    // preload image
    // comment the next two lines to disable image pre-loading
    x[i].oversrc_img = new Image();
    x[i].oversrc_img.src=oversrc;
    // set event handlers
    x[i].onmouseover = new Function("SimpleSwap(this,'oversrc');");
    x[i].onmouseout = new Function("SimpleSwap(this);");
    // save original src
    x[i].setAttribute("origsrc",x[i].src);
  }
  buttons(); // Setup the form rollovers
}

function buttons() {
  var inputs = document.getElementsByTagName("input");
  var i=0;
  for (i=0; i<inputs.length; i++) {
    if(inputs[i].getAttribute('type') == 'image') {
      var image = inputs[i];
      image.offImage = new Image();
      image.offImage.src = image.src;
      image.onImage = new Image();
      image.onImage.imageElement = image;
      if (navigator.userAgent.toLowerCase().indexOf('safari') != - 1) {
        image.onmouseover = function() { this.src = this.onImage.src; };
        image.onfocus = function() { this.src = this.onImage.src; };
        image.onmouseout = function() { this.src = this.offImage.src; };
        image.onblur = function() { this.src = this.offImage.src; };
      } else {
        image.onImage.onload = function() {
        this.imageElement.onmouseover = function() { this.src = this.onImage.src; };
        this.imageElement.onfocus = function() { this.src = this.onImage.src; };
        this.imageElement.onmouseout = function() { this.src = this.offImage.src; };
        this.imageElement.onblur = function() { this.src = this.offImage.src; };
      };
    }
    image.onImage.src = image.src.replace(/.gif/, '_hover.gif');
    }
  }
}

/* FORM VALIDATION **********************************/
function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "You didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "The email address contains illegal characters.\n";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits
function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "The phone number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
	error = "The phone number is the wrong length. Make sure you included an area code.\n";
    } 
return error;
}


// non-empty textbox
function checkTextfield(strng,field) {
var error = "";
  if (strng.length == 0) {
	  if (field == 'firstName') {
     error = "You don't have a First Name?!\n"
	  } else if (field == 'lastName') {
     error = "You don't have a Last Name?!\n"
	  } else if (field == 'heardAbout') {
     error = "How did you hear about us?\n"
	  }
  }
return error;	  
}


// exactly one radio button is chosen
function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "Please check a radio button.\n";
    }
return error;
}

// call each function to validate
function checkWholeForm(theForm,page) {
    var why = "";
    if(page=='contact'){
		why += checkTextfield(theForm.firstName.value,'firstName');
		why += checkTextfield(theForm.lastName.value,'lastName');
		why += checkEmail(theForm.emailAddress.value);
		why += checkPhone(theForm.phoneNumber.value);
		why += checkTextfield(theForm.heardAbout.value,'heardAbout');
	}
	if(page=='event'){
		why += checkTextfield(theForm.firstName.value,'firstName');
		why += checkTextfield(theForm.lastName.value,'lastName');
		why += checkEmail(theForm.emailAddress.value);
		why += checkPhone(theForm.phoneNumber.value);
	}
    /*for (i=0, n=theForm.radios.length; i<n; i++) {
        if (theForm.radios[i].checked) {
            var checkvalue = theForm.radios[i].value;
            break;
        } 
    }
    why += checkRadio(checkvalue);
    why += checkDropdown(theForm.choose.selectedIndex);*/
    if (why != "") {
       alert(why);
       return false;
    }
return true;
}



/* PHOTO ALBUM **********************************/
function loadNewPhoto(el,thesrc){
		var text = el.getAttribute('alt');
		var imageLoc = el.getAttribute(thesrc);
		var divCont = document.getElementById("currentPhoto");
		divCont.innerHTML = '<img src="' + imageLoc + '" /><p>' + text + '</p>';
}

function photoAlbumSetup(){
  var x = document.getElementsByTagName("img");
  for (var i=0;i<x.length;i++){
    var albumsrc = x[i].getAttribute("albumsrc");
    if (!albumsrc) continue; //This says to skip this item and move onto the next looped item
    x[i].onclick = new Function("loadNewPhoto(this,'albumsrc');"); // set event handlers
  }
}


/* LOAD UP THE FUNCTIONS **************************/
function init() {
	photoAlbumSetup();
	SimpleSwapSetup();
}
window.onload = init;


/* MISC **********************************/

// Window opener
function openWindow(location,winName,features){
	var doit = window.open(location,winName,features);
}

// Clear form field
function clearFormField(el){
	if(el.value=="your@email.com"){
			el.value = "";
	}
}