var searchClicked=false;

var nullNameError="Please insert your name";
var nullEmailError="Please insert your email";
var nullQuestionError="Please insert your question";
var invalidEmailError="Please insert a valid email address";

var valid=true;

window.onload=loadFunction;
var timer=-1;
var currentLi;
var inside=false;

function loadFunction(){
	setSearchInputClickHandler();
	validateSendEmailForm();

	positionUlChildren();
	//setDropdown();
}

/**
 *	Removes the text in the search text box when clicked on it.
 */
function setSearchInputClickHandler(){
	$("#searchInput").click(function(){
		if(searchClicked==false){
			this.value='';
			searchClicked=true;
		}
	});
}

/**
 *	Validates the send email form.
 */
function validateSendEmailForm(){
    $("#sendButton").click(function(){
		
		//clear previous messages
		$("#nameError").text("");
		$("#emailError").text("");  
		$("#questionError").text("");  
		valid=true;  
		
		var name=document.getElementById("nameTextBox").value;
		var question=document.getElementById("questionTextArea").value;
		
		//verify whether the name text box is empty
		if(name=="" || name==null){
			$("#nameError").text(nullNameError);
			valid=false;
		}
		
		//verify whether the question text area is empty
		if(question=="" || question==null){
			$("#questionError").text(nullQuestionError);
			valid=false;
		}
		
		//verify whether the inserted email address is valid
		var reg = "/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/";
		var email = document.getElementById("emailTextBox").value;
		if(!(email.indexOf(".") > 2) || !(email.indexOf("@") > 0)) {
			$("#emailError").text(invalidEmailError); 
			valid=false;
		}
		
		//verify whether the email text box is empty
		if(document.getElementById("emailTextBox").value=="" || document.getElementById("emailTextBox").value==null){
			$("#emailError").text(nullEmailError);
			valid=false;
		}

		//if the inserted data is valid, then sumbit the form
		if(valid==true){
				 
			var urlToPhp = document.getElementById("url").value;
			//urlToPhp=sendEmail.php;
			var emailToSend = document.getElementById("emailToSend").value;
			
			var dataString = 'name='+ name + '&email=' + email + '&question=' + question + '&emailToSend=' + emailToSend;  

			$.ajax({  
				type: "POST",  
				url: urlToPhp,  
				data: dataString,  
				success: function() {  
				$("label#message").html("Your message has been sent!<br/><br/>");
				$("#submitForm").each(function(){
					this.reset();
				});
				}
			}); 
			//document.getElementById("submitForm").submit();
			
		}
		
	});
}


/**
 *	Positions the dropdown children of the menu.
 */
function positionUlChildren(){
	$("#menu ul li").each(function(i){
		var childUl=$(this).find("ul.children");
		var left=$(this).find("a").offset().left-$("#menu").offset().left;
		childUl.css({left:left});
		
		childUl.hover(function(){
			$(this).parent("li").find("a").addClass("selected");
		},function(){
			$(this).parent("li").find("a").removeClass("selected");
		});
	});
}

