/******************
PRE-LOAD FUNCTIONS
******************/
function g(x){var i = document.getElementById(x);
return i;}
var email = /^[0-9a-zA-Z_\.$!*]+@[0-9a-zA-Z_\.$!*]+\.\w{2,4}$/
var name = /^[a-zA-Z]+[a-zA-Z\s]+$/// contains letters or spaces
var subject = /^[a-zA-Z0-9\?\.,!$]+[a-zA-Z0-9\s\?\.,!$]*$/// contains letters, numbers, spaces, or certain special characters
function validate_checkout(){
	if(
		name.test(g("name").value) &&
		email.test(g("email").value) &&
		email.test(g("email2").value) &&
		g("payment").value != "--Please Select--"
		){
		return true;
	}
	else{
	validate(name,g("name"),"Your Name",1);
	validate(email,g("email"),"someone@something.com",1);
	validate(email,g("email2"),"someone@something.com",1);
	alert("Your information did not pass validation, please ensure that you have selected a payment option and that you have followed the on-screen instructions.");
	return false;
	}
}
function validate_contact_form(){
	if(
		email.test(g("email").value) &&
		subject.test(g("subject").value) &&
		g("body").value != " " &&
		g("body").value != "Message Body"
	){
		return true;
	}
	else{
		validate(subject,g("subject"),"A Subject",1);
		validate(email,g("email"),"someone@something.com",1);
		alert("Your input did not pass validation, please follow the on-screen helps, and/or ensure that your message body has a value(other than \"Message Body\" or empty space)");
		return false;
	}
}
function emails_match(email1, email2){
	if(email1.value == email2.value && email1.value != "" && email2.value != "" && email.test(email1.value) && email.test(email2.value)){// the emails match
		return true;
	}
	else{// the emails don't match
		return false;
	}
}
function fields_empty(f1){
	if(f1.value == ""){// the field is empty
		return true
	}
	else{// the fields are not empty
		return false
	}
}
function validate_subscription(){
//	alert();
	e1 = g("subscriber_email");
	e2 = g("subscriber_email2");
	n = g("subscriber_name");
	h = g("info_header");
//	alert(g("subscriber_email").value);
//	alert(g("subscriber_email2").value);
//	alert(g("subscriber_name").value);
	if(email.test(e1.value) && email.test(e2.value) && e1.value == e2.value && n.value != ""){// the info passes validation
		return true;
	}
	else{
		h.innerHTML = "Can not send yet. Please ensure that: 1. The name field has a value. 2. The emails you entered in are valid. 3. The emails you entered in are a match.";
		h.setAttribute("class", "error");
		return false;
	}
}
function validate_unsubscription(){
	if(email.test(g("unsubscribe_email").value)){// passes validation
		return true;
	}
	else{// doesn't pass validation
		g("info_header").innerHTML = "Please ensure that you have entered a valid email address into the form.";
		g("info_header").setAttribute("class", "error");
		return false;
	}
}
function validate(rex,elem,msg,run){if(rex.test(elem.value)){elem.nextSibling.innerHTML = "Correct";elem.nextSibling.setAttribute("class", "correct");return true;}else{if(run == 1){elem.nextSibling.setAttribute("class", "error");elem.nextSibling.innerHTML = "Please enter a value in the form of: "+msg;}return false;}}
/******************
POST-LOAD FUNCTIONS
******************/
window.onload = function(){
	if(navigator.appName == "Microsoft Internet Explorer"){// If the user is using IE
		if(g("search_bar_main_input")){// there is a search bar
			// prep the search bar
			focusin = function(){
				if(sbm.value == "Search"){
					sbm.value = "";
					sbm.style.color = "#000000";
				}
			}
			focusout = function(){
				if(sbm.value == ""){
					sbm.value = "Search";
					sbm.style.color="#263a94";
				}
			}
			var sbm = g("search_bar_main_input");// the search bar
			sbm.attachEvent("onfocus",focusin);
			sbm.attachEvent("onblur",focusout);
		}
		// SUBSCRIBE
		if(g("subscribe") && g("subscriber_email") && g("subscriber_email2") && g("submit")){// we are dealing with the subscription page
			g("submit").attachEvent("onclick",validate_subscription);
		}
		// UNSUBSCRIBE
		if(g("unsubscribe") && g("unsubscribe_email") && g("submit")){// we are dealing with the unsubscription page
			g("submit").attachEvent("onclick",validate_unsubscription);
		}
		// CHECK OUT
		if(g("name") && g("email") && g("email2")){// the check out form is present
			g("submit").attachEvent("onclick",validate_checkout);
			// name
			function focus_name(){// fires when the name field comes into focus
				if(g("name").value == "Name"){
					g("name").value = "";
					g("name").style.color="#000000";
				}
			}
			function blur_name(){
				if(g("name").value == ""){// fires when the name field is blurred
					g("name").value = "Name";
					g("name").style.color="#999999";
				}
			}
			g("name").attachEvent("onfocus",focus_name);
			g("name").attachEvent("onblur",blur_name);
			// email
			function focus_on_email(){// fires when the email field comes into focus
				if(g("email").value == "Email"){
					g("email").value = "";
					g("email").style.color="#000000";
				}
			}
			function focus_out_email(){
				if(g("email").value == ""){// fires when the email field is blurred
					g("email").value = "Email";
					g("email").style.color="#999999";
				}
			}
			g("email").attachEvent("onfocus",focus_on_email);
			g("email").attachEvent("onblur",focus_out_email);
			// email2
			function focus_email2(){// fires when the email2 field comes into focus
				if(g("email2").value == "Email Again (Confirm)"){
					g("email2").value = "";
					g("email2").style.color="#000000";
				}
			}
			function blur_email2(){
				if(g("email2").value == ""){// fires when the email2 field is blurred
					g("email2").value = "Email Again (Confirm)";
					g("email2").style.color="#999999";
				}
			}
			g("email2").attachEvent("onfocus",focus_email2);
			g("email2").attachEvent("onblur",blur_email2);
		}
		// CONTACT PAGE
		if(g("submit") && g("subject") && g("email") && g("body")){// the appropriate eliments are present
			// validation
			g("submit").attachEvent("onclick",validate_contact_form);
			// the email field
			function focus_email(){// fires when the email field comes into focus
				if(g("email").value == "Email Address"){
					g("email").value = "";
					g("email").style.color="#000000";
				}
			}
			function blur_email(){
				if(g("email").value == ""){// fires when the email field is blurred
					g("email").value = "Email Address";
					g("email").style.color="#999999";
				}
			}
			g("email").attachEvent("onfocus",focus_email);
			g("email").attachEvent("onblur",blur_email);
			// the subject field
			function focus_subject(){// fires when the email field comes into focus
				if(g("subject").value == "Subject"){
					g("subject").value = "";
					g("subject").style.color="#000000";
				}
			}
			function blur_subject(){
				if(g("subject").value == ""){// fires when the email field is blurred
					g("subject").value = "Subject";
					g("subject").style.color="#999999";
				}
			}
			g("subject").attachEvent("onfocus",focus_subject);
			g("subject").attachEvent("onblur",blur_subject);
			// the main message body
			function focus_body(){// fires when the email field comes into focus
				if(g("body").value == "Message Body"){
					g("body").value = "";
					g("body").style.color="#000000";
				}
			}
			function blur_body(){
				if(g("body").value == ""){// fires when the email field is blurred
					g("body").value = "Message Body";
					g("body").style.color="#999999";
				}
			}
			g("body").attachEvent("onfocus",focus_body);
			g("body").attachEvent("onblur",blur_body);
		}// end if contact form is present
	}
/***************
NON IE
***************/
	else{// If the user is using something other than IE
		if(g("search_bar_main_input")){// there is a search bar
			// get the search bar ready
			var sbm = g("search_bar_main_input");// the search bar
			sbm.addEventListener("focus",function(e){
				if(sbm.value == "Search"){
					sbm.value = "";
					sbm.style.color = "#000000";
				}
			},false);
			sbm.addEventListener("blur",function(e){
				if(sbm.value == ""){
					sbm.value = "Search";
					sbm.style.color="#263a94";
				}
			},false);
		}
		// SUBSCRIBE
		if(g("subscribe") && g("subscriber_email") && g("subscriber_email2") && g("submit")){// we are dealing with the subscription page
			g("submit").addEventListener("click",function(e){
				if(emails_match(g("subscriber_email"), g("subscriber_email2")) && !fields_empty(g("subscriber_name"))){
				// the emails match and the name field is filled
					return true;
				}
				else{// the emails don't match
					e.preventDefault();
					g("info_header").setAttribute("class", "error");
					g("info_header").innerHTML = "<h1>Can not send yet</h1>Please ensure that:<br />1. The name field has a value<br />2. The emails you entered in are valid<br />3. The emails you entered in are a match.";
					return false;
				}
			},false);
		}
		// UNSUBSCRIBE
		if(g("unsubscribe") && g("unsubscribe_email") && g("submit")){// we are dealing with the unsubscription page
			g("submit").addEventListener("click",function(e){
				if(validate_unsubscription()){
					return true;
				}
				else{
					e.preventDefault();
					return false;
				}
			},false);
		}
		// CHECK OUT
		if(g("name") && g("email") && g("email2")){// the check out form is present
			// validate
			g("submit").addEventListener("click",function(e){
				if(validate_checkout()){
					return true;
				}
				else{
					e.preventDefault();
					return false;
				}
			},false);
			g("name").addEventListener("focus",function(e){
				if(g("name").value == "Name"){
					g("name").value = "";
					g("name").style.color = "#000000";
				}
			},false);
			g("name").addEventListener("blur",function(e){
				if(g("name").value == ""){
					g("name").value = "Name";
					g("name").style.color = "#999999";
				}
			},false);
			g("email").addEventListener("focus",function(e){
				if(g("email").value == "Email"){
					g("email").value = "";
					g("email").style.color = "#000000";
				}
			},false);
			g("email").addEventListener("blur",function(e){
				if(g("email").value == ""){
					g("email").value = "Email";
					g("email").style.color = "#999999";
				}
			},false);
			g("email2").addEventListener("focus",function(e){
				if(g("email2").value == "Email Again (Confirm)"){
					g("email2").value = "";
					g("email2").style.color = "#000000";
				}
			},false);
			g("email2").addEventListener("blur",function(e){
				if(g("email2").value == ""){
					g("email2").value = "Email Again (Confirm)";
					g("email2").style.color = "#999999";
				}
			},false);
		}
		// CONTACT US
		if(g("submit") && g("subject") && g("email") && g("body")){// the contact form is present
			// validate
			g("submit").addEventListener("click",function(e){
				if(validate_contact_form()){
					return true;
				}
				else{
					e.preventDefault();
					return false;
				}
			},false);
			// Email
			g("email").addEventListener("focus",function(e){
				if(g("email").value == "Email Address"){
					g("email").value = "";
					g("email").style.color = "#000000";
				}
			},false);
			g("email").addEventListener("blur",function(e){
				if(g("email").value == ""){
					g("email").value = "Email Address";
					g("email").style.color = "#999999";
				}
			},false);
			// Subject
			g("subject").addEventListener("focus",function(e){
				if(g("subject").value == "Subject"){
					g("subject").value = "";
					g("subject").style.color = "#000000";
				}
			},false);
			g("subject").addEventListener("blur",function(e){
				if(g("subject").value == ""){
					g("subject").value = "Subject";
					g("subject").style.color = "#999999";
				}
			},false);
			// Message Body
			g("body").addEventListener("focus",function(e){
				if(g("body").value == "Message Body"){
					g("body").value = "";
					g("body").style.color = "#000000";
				}
			},false);
			g("body").addEventListener("blur",function(e){
				if(g("body").value == ""){
					g("body").value = "Message Body";
					g("body").style.color = "#999999";
				}
			},false);
		}
	}
}// window.onload
