//preload the submit over image
var preloadSubmit = document.createElement("img");
preloadSubmit.setAttribute("src", "images/submit_over.gif");

//########################################################################

function prepareEventForm(){
	//validate the browser compatibility
	if(!$("submitEvent") || !$("been_submitted")) return false;

	var submitEvent = $("submitEvent");

	submitEvent.onclick = clickSubmit;

	submitEvent.onmouseover = function(){
		submitEvent.src = "images/submit_over.gif";
	}

	submitEvent.onmouseout = function(){
		submitEvent.src = "images/submit.gif";
	}
	
	var been_submitted = $("been_submitted");
	
	been_submitted.setAttribute("value", "ajax");
}

//########################################################################

function clickSubmit(){
	//set the loading icon
	var loadingIcon = document.createElement("img");
	loadingIcon.setAttribute("src", "images/ajax_loader.gif");
	loadingIcon.setAttribute("id", "submitLoader");
	
	insertAfter(loadingIcon, $("submitEvent"));
	
	//setup url with vars
	var sendURL = Form.serialize($("eventForm"));

	new Ajax.Request($("eventForm").action, {method:"post", parameters:sendURL, onComplete:fromServer});

	return false;
}

//########################################################################

function fromServer(request){
	//get the xml response from the server
	var xmlDoc = request.responseXML;

	//read the status from the xml response
	var statusNode = xmlDoc.getElementsByTagName("status")[0].childNodes[0].nodeValue;

	if($("updateMessage").hasChildNodes()){
		while ($("updateMessage").hasChildNodes()){
			$("updateMessage").removeChild($("updateMessage").firstChild);
		}
	}

	//if event form  validation was bad...
	if(statusNode == "FAIL"){
		$("updateMessage").setAttribute("class", "error");
		$("updateMessage").setAttribute("className", "error");		/*IE Workaround*/

		//read the error fields from the xml response
		var errorFieldsNode = xmlDoc.getElementsByTagName("errorFields")[0].childNodes[0].nodeValue;

		var responseText = document.createTextNode("Please fill in the following fields: " + errorFieldsNode);

		$("updateMessage").appendChild(responseText);
		
		//now fade in the response <p>
		new Effect.Appear($("updateMessage"));
	}
	//else if validation was good...
	else{
		$("updateMessage").setAttribute("class", "success");
		$("updateMessage").setAttribute("className", "success");
		
		var responseText = document.createTextNode("Thank you for your request. ");
		var responseLink = document.createElement("a");
		responseLink.setAttribute("href", "events.php");
		responseLink.setAttribute("id", "showEventsForm");
		var linkText = document.createTextNode("Click here to display form.");
		responseLink.appendChild(linkText);
		$("updateMessage").appendChild(responseText);
		$("updateMessage").appendChild(responseLink);
		
		responseLink.onclick = redisplayForm;

		if($("companyLink")){
			$("fName").value = "";
			$("lName").value = "";
			$("company").value = "";
			$("phone").value = "";
			$("email").value = "";
			$("event").value = "";
			$("companyLink").value = "";
			$("eventDescription").value = "";
		}
		else if($("link")){
			$("Name").value = "";
			$("link").value = "";
			$("address").value = "";
			$("city").value = "";
			$("state").value = "";
			$("zip").value = "";
			$("phone").value = "";
			$("email").value = "";
			$("fax").value = "";
		}
		else if($("description")){
			$("Name").value = "";
			$("address").value = "";
			$("city").value = "";
			$("state").value = "";
			$("zip").value = "";
			$("phone").value = "";
			$("email").value = "";
			$("fax").value = "";
			$("description").value = "";
		}
		else{
			$("Name").value = "";
			$("Degree").value = "";
			$("Affiliation").value = "";
			$("address").value = "";
			$("city").value = "";
			$("state").value = "";
			$("zip").value = "";
			$("phone").value = "";
			$("email").value = "";
			$("fax").value = "";
		}

		//get rid of the form
		new Effect.Fade($("eventForm"));

		//fade in the response message
		var intervalName = setInterval(function(){
			if(Element.getStyle($("eventForm"), 'opacity') < 0.1 && Element.getStyle($("eventForm"), 'opacity') != null){
				$("eventForm").style.display = "none"; //this is for the mac, so the footer doesnt jump a little
				new Effect.Appear($("updateMessage"));
				clearInterval(intervalName);
			}//end if
		},1);
	}

	//get rid of ajax loading icon
	$("submitLoader").setAttribute("style", "display:none");
	$("submitLoader").setAttribute("className", "hide");	/*IE Workaround*/
}

//########################################################################
function redisplayForm(){
	//now fade in the response <p>
	new Effect.Fade($("updateMessage"));

	//fade in the response message
	var intervalName2 = setInterval(function(){
		if(Element.getStyle($("updateMessage"), 'opacity') < 0.1 && Element.getStyle($("updateMessage"), 'opacity') != null){
			$("updateMessage").style.display = "none";
			new Effect.Appear($("eventForm"));
			clearInterval(intervalName2);
		}//end if
	},1);

	return false;
}

//########################################################################

function insertAfter(newElement, targetElement){
	var parent = targetElement.parentNode;

	if(parent.lastChild == targetElement){
		parent.appendChild(newElement);
	}

	else{
		parent.insertBefore(newElement,targetElement.nextSibling);
	}
}

//########################################################################

function checkAlpha(){
	if(Element.getStyle($("eventForm"), 'opacity') == 0){
		new Effect.Appear($("updateMessage"));
		clearInterval(intervalName);//kill the looping setInterval function it's slowing down my computer
	}//end if
}//end function

//########################################################################

function prepareGetEvents(){
	$("selectEventMonth").onchange = function(){
		document.getEventsForm.submit();
	}
}

//########################################################################

window.onload = function(){
	if($("eventForm")){
		prepareEventForm();
	}

	if($("getEventsForm")){
		prepareGetEvents();
	}
}