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;

var urlToPhp = "http://yourdomain/sendEmail.php";

jQuery(function(){
    setSearchInputClickHandler();
    validateSendEmailForm();
    positionUlChildren();
});

/**
 *	Removes the text in the search text box when clicked on it.
 */

function setSearchInputClickHandler(){
    

/*
	if($("#searchInput").length != null) {
	    $("#searchInput").click(function(){
    		if(searchClicked==false){
    			this.value='';
    			searchClicked=true;
    		}
    	});
    }
     */
}

/**
 *	Positions the dropdown children of the menu.
 */
function positionUlChildren(){
	jQuery("#menu ul li").each(function(i){
		var childUl=jQuery(this).find("ul");
		var left=jQuery(this).find("a").offset().left-jQuery("#menu").offset().left;
		childUl.css({left:left});
		
		
		childUl.hover(function(){
			jQuery(this).parent("li").find("a").addClass("selected");
		},function(){
			jQuery(this).parent("li").find("a").removeClass("selected");
		});
	});
	
	jQuery("#menu ul li ul").hover(function(){
		jQuery(this).parent().find("a").addClass("hover");
	}, function(){
		jQuery(this).parent().find("a").removeClass("hover");
	});
}


/**
 *	Validates the send email form.
 */
function validateSendEmailForm(){
    
    jQuery("#sendButton").click(function(){

		//clear previous messages
		$("#nameError").hide();
		$("#emailError").hide(); 
		$("#questionError").hide();
		valid=true;  
		
		//verify whether the name text box is empty
		if(document.getElementById("nameTextBox").value=="" || document.getElementById("nameTextBox").value==null){
			$("#nameError").text(nullNameError);
			$("#nameError").fadeIn("slow");
			valid=false;
		}
		
		//verify whether the question text area is empty
		if(document.getElementById("questionTextArea").value=="" || document.getElementById("questionTextArea").value==null){
			$("#questionError").text(nullQuestionError);
			$("#questionError").fadeIn("slow");
			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); 
			$("#emailError").fadeIn("slow");
			valid=false;
		}
		
		//verify whether the email text box is empty
		if(document.getElementById("emailTextBox").value=="" || document.getElementById("emailTextBox").value==null){
			$("#emailError").text(nullEmailError);
			$("#emailError").fadeIn("slow");
			valid=false;
		}
		
		var name=document.getElementById("nameTextBox").value;
		var question=document.getElementById("questionTextArea").value;
		

		//if the inserted data is valid, then sumbit the form
		if(valid==true){
			urlToPhp=document.getElementById("url").value;
			var emailToSend=document.getElementById("emailToSend").value;
			
			var dataString = 'name='+ name + '&question=' + question + '&email=' + email + '&emailToSend=' + emailToSend;  

			$.ajax({  
				type: "POST",  
				url: urlToPhp,  
				data: dataString,  
				success: function() {  
				$("label#message").html("<h2>Your message has been sent!<br/><br/></h2>");
				$("#submitForm").each(function(){
					this.reset();
				});
				}
			}); 
		}
    });
}

