// JavaScript Document

 function updateConfMsg(msg,classname){
  	 var textNode = document.createTextNode(msg);
  	 var divNode = document.createElement('div');
  	 divNode.appendChild(textNode)
  	 divNode.className = classname;
  	 divNode.id = "confMsg";
  	 document.getElementById('emailMsg').appendChild(divNode)
   }

  function lbConf(email){
 	var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/
     var result = pattern.test(email);
 	return result;
   }

   function errorFunction(data){
     alert('there was an error');

   }
   function sendEmailInfo(){
 	   $('#confMsg').remove();
 	   $('#emailError').css('display','none');
 	   var email = false;
 	   var str = "{";

 		if(lbConf($('#emailAddress').val()) === false){  // check for malformed email address

 		  updateConfMsg('The email you entered appears to be invalid. Please try again.','error');
 		  email = false;
 		  return;
 		}
 		else{email = true}

 		str +=  "\"" + $('#emailAddress').attr('name') + "\":\"" + $('#emailAddress').val() + "\"";


 		str += "}";

 	  if(email){
 	     updateConfMsg("Thanks for your interest. We'll add you to our list.",'success')
 	     sendInfo();
 	  }

 	  function sendInfo(){
 	    alert(str);
 	    $.ajax({
 			url: "mailList.php",
 			global: false,
 			type: "POST",
 			data:({'eform':str}),
 			dataType: "json",
 			async:false,
 			error: errorFunction(),
 			success: function(){}
 	  })
     }

   }
   
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
    obj.addEventListener(evType, fn, true); 
    return true; 
 } else if (obj.attachEvent){ 
    var r = obj.attachEvent("on"+evType, fn); 
    return r; 
 } else { 
    return false; 
 } 
} 


function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

 Function.prototype.bind = function(obj) { 
	  var method = this, 
	   temp = function() { 
		return method.apply(obj, arguments); 
	   }; 
	  
	  return temp; 
	 } 


	function turnOn(){
	  this.src =  "img/" + this.id + "On.jpg";
	}
	
	function turnOff(){
	 this.src =  "img/" +this.id + "Off.jpg";
	}


   
 startList = function() {
		
	if (document.all&&document.getElementById) {
		
		  
		var navRoot = document.getElementById("nav");
		var children = navRoot.childNodes[0];
		
			for (var i=0; i<children.childNodes.length; i++) {
			var node = children.childNodes[i];
			  if (node.nodeName=="LI") {
			    node.onmouseover=function() {
			    this.className+=" over";
			    }
				node.onmouseout=function() {
				this.className=this.className.replace(" over", "");
			    }
		     }
		  }
		  
	   }
   }
   
   function initSideNav(){
	var links = document.getElementById('links');   
	for(var i=0;i<links.childNodes.length;i++){
		var node = links.childNodes[i]
		if(node.className == "link"){
		    node.onmouseover=function(){
			this.className += " over";
		 }
		   node.onmouseout=function() {
		   this.className=this.className.replace(" over", "");
		}
	 }
   }
 }
   
  addEvent(window,'load', function(){
		 startList();
		// initSideNav();
       for(var i=0;i<images.length;i++){
	    document.getElementById(images[i]).onmouseover = turnOn.bind(document.getElementById(images[i]));
	    document.getElementById(images[i]).onmouseout = turnOff.bind(document.getElementById(images[i]));
	   }

     })
