var formPart = 1;

// Setup the event observer to load once the page is done.
new Event.observe(window,"load",function(ev) {

	$$('select').each(function(el) {
		selectReplacement(el);
	});
	
	$$(".form_column input").each(function(el) {
		el.hide();
		a = new Element("a", { name: el.value, href: "#" });
		a.addClassName("checkbox");
		a.observe("click",function(ev) {
			ev.stop();
			if (this.hasClassName("checkbox_checked")) {
				this.previous("input").checked = "";
				this.removeClassName("checkbox_checked");
			} else {
				this.previous("input").checked = "checked";
				this.addClassName("checkbox_checked");
			}
		});
		el.insert({ after: a });
	});

	// We'll observe it by class, that way if it's not on the current page we don't have to worry about errors being thrown.
	$$(".contactForm").invoke("observe","submit",function(ev) {
		ev.stop();
		// Move the validation into this function to keep the global namespace less polluted.
		error = false;
		if (formPart == 1) {
			// Greatly simplified checking of values -- check each one to see if it's blank, if it is throw an error.
			$$("#formPartOne input").each(function(el) {
				if (el.name == "email") {
					if (echeck(el.value)==false){
						error = true;
						el.previous().addClassName("form_error");
					} else {
						el.previous().removeClassName("form_error");
					}
				} else {			
					if (el.value.replace(/^\s*|\s*$/,"") == "") {
						error = true;
						el.previous().addClassName("form_error");
					} else {
						el.previous().removeClassName("form_error");
					}
				}
			});
			// Move to part two if there's no errors.
			if (!error) {
				$("errorString").hide();
				navItems = $$("#contact_form_nav li");
				navItems[0].className = "";
				navItems[1].className = "sel";
				
				$("formPartOne").hide();
				$("formPartTwo").show();
				
				$("submitContact").value = "Submit";
				$("submitContact").setStyle({ width: "85px" });
				
				formPart = 2;
			} else
				$("errorString").show();
		} else {
			// Simplified the checking of empty fields.
			$$("#formPartTwo select").each(function(el) {
				if (el.value.replace(/^\s*|\s*$/,"") == "") {
					error = true;
					el.up().previous().addClassName("form_error")
				} else
					el.up().previous().removeClassName("form_error");
			});
			$$("#formPartTwo textarea").each(function(el) {
				if (el.value.replace(/^\s*|\s*$/,"") == "") {
					error = true;
					el.previous().addClassName("form_error")
				} else
					el.previous().removeClassName("form_error");
			});
			// Submit the form if there are no errors.
			if (!error) {
				$("errorString").hide();
				new Ajax.Request("www_root/modules/contact.php", { method: "post", parameters: { form: $("contactForm").serialize() }, onComplete: function() {
					navItems = $$("#contact_form_nav li");
					navItems[1].className = "";
					navItems[2].className = "sel";
					$("contactForm").hide();
					$("formPartThree").show();
					$("contact_form_nav").scrollTo();
				}});
			} else
				$("errorString").show();
		}	
	});
});



//check for real email address
function echeck(str) {
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   return false
		}
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}
		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }
		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }
		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		 if (str.indexOf(" ")!=-1){
		    return false
		 }
 		 return true					
	}


function checkLength() {
	if(document.contactForm.pitch.value.length > 300) {
		document.contactForm.pitch.value = document.contactForm.pitch.value.substring(0, 300); 	
	}
}


//for custom drop down box
function selectReplacement(obj) {
	// append a class to the select
	obj.className += ' replaced';
	// create list for styling
	var ul = document.createElement('ul');
	ul.className = 'selectReplacement';
	var opts = obj.options;
	for (var i=0; i<opts.length; i++) {
		var selectedOpt;
		if (opts[i].selected) {
			selectedOpt = i;
			break;
		} else {
			selectedOpt = 0;
		}
	}
	for (var i=0; i<opts.length; i++) {
		var li = document.createElement('li');
		var txt = document.createTextNode(opts[i].text);
		li.appendChild(txt);
		li.selIndex = opts[i].index;
		li.selectID = obj.id;
		li.onclick = function() {
			selectMe(this);
		}
		if (i == selectedOpt) {
			li.className = 'selected';
			li.onclick = function() {
				this.parentNode.className += ' selectOpen';
				this.onclick = function() {
					selectMe(this);
				}
			}
		}
		if (window.attachEvent) {
			li.onmouseover = function() {
				this.className += ' hover';
			}
			li.onmouseout = function() {
				this.className = this.className.replace(new RegExp(" hover\\b"), '');
			}
		}
		ul.appendChild(li);
	}
	// add the input and the ul
	obj.parentNode.appendChild(ul);
}
function selectMe(obj) {
	var lis = obj.parentNode.getElementsByTagName('li');
	for (var i=0; i<lis.length; i++) {
		if (lis[i] != obj) { // not the selected list item
			lis[i].className='';
			lis[i].onclick = function() {
				selectMe(this);
			}
		} else {
			setVal(obj.selectID, obj.selIndex);
			obj.className='selected';
			obj.parentNode.className = obj.parentNode.className.replace(new RegExp(" selectOpen\\b"), '');
			obj.onclick = function() {
				obj.parentNode.className += ' selectOpen';
				this.onclick = function() {
					selectMe(this);
				}
			}
		}
	}
}
function setVal(objID, selIndex) {
	var obj = document.getElementById(objID);
	obj.selectedIndex = selIndex;
}
