var g_emailPopOverId;
var g_emailPopOverCategory;

//**************************************************

var IE = document.all?true:false; //Test to see if IE
if (!IE) document.captureEvents(Event.MOUSEMOVE); //If not IE
document.onmousemove = getMouseLoc;

function Point(x,y) {  this.x = x; this.y = y; }
mouseLocation = new Point(0,0);

//Get the mouse position
function getMouseLoc(e) 
{
	if (IE) { 
		mouseLocation.x = event.clientX + document.body.scrollLeft;
		mouseLocation.y = event.clientY + document.body.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		mouseLocation.x = e.pageX;
		mouseLocation.y = e.pageY;
	}  
	// catch possible negative values 
	if (mouseLocation.x < 0){mouseLocation.x = 0}
	if (mouseLocation.y < 0){mouseLocation.y = 0}  
	return true;
}

function positionDiv()
{  
	//Set the position of the hidden div
	// alert("Positions Y:" + mouseLocation.y + " X: " + mouseLocation.x );         
	document.getElementById('theFormDiv').style.top = (mouseLocation.y + 0) + "px";
	document.getElementById('theFormDiv').style.left = (mouseLocation.x + 0) +"px";
	document.getElementById('theFormDiv').style.position = "absolute";  
}


//**************************************************

function SetError(error) {
	document.getElementById('sendMailError').innerHTML = "<font color='red'>" + error + "</font>";
}

function SendMail() {
	var retData = {};
	retData.fullName = document.getElementById('email-fullName').value;
	retData.fromEmail = document.getElementById('email-fromEmail').value;
	retData.message = document.getElementById('email-message').value;
	retData.id = g_emailPopOverId;
	retData.category = g_emailPopOverCategory;

	if ( ! VerifyEmail(retData.fromEmail) ) {
		SetError("Please enter a valid email address.");
		return;
	}

	if ( ! retData.message) {
		SetError("The message is empty. Please add some text.");
		return;
	}

	if ( ! retData.fullName) {
		SetError("Please enter your name.");
		return;
	}

	if ( ! retData.id) {
		SetError("This email form is mis-configured.");
		return;
	}
	
	if ( ! retData.category) {
		SetError("This email form is mis-configured.");
		return;
	}

	var jsonData = Object.toJSON(retData);
	var uriEncoded = encodeURI(jsonData);
	
	// new Ajax.Request('http://holyinn.org/wp-content/plugins/emailpopover/sendmail.php?encodedMailData=' + uriEncoded,
    // new Ajax.Request('http://dev.holyinn.org/wp-content/plugins/emailpopover/sendmail.php?encodedMailData=' + uriEncoded,
	new Ajax.Request('http://www.holyinn.org/wp-content/plugins/emailpopover/sendmail.php?encodedMailData=' + uriEncoded,
 	    {
 	        method:'get',
 	        onComplete: function(transport)
 	        {
 	            var response = transport.responseText;
 	            if ( ! response) {
					alert('Something\' not right. The admin\'s been notified.');
					HideForm();
 	            }

 	            var data = response.evalJSON();

 	            if (data.success == 1) {
 	 	            alert("Message Sent! Thanks!");
 	 	          	HideForm();
 	            }
 	            else
 	            {
					errorMessage = data.error.string;
 	 	            alert(errorMessage);
 	 	          	HideForm();

 	            } 	 	           
 	        },
 	        onFailure: function()
 	        {
				alert('Something\' not right. The admin\'s been notified.');
				HideForm();
 	        }
 	    });
}

function ShowForm(id, category) {
	g_emailPopOverId = id;
	g_emailPopOverCategory = category;
	document.getElementById('sendMailError').innerHTML = "";
	document.getElementById('email-message').value = "";
	oDiv = document.getElementById('theFormDiv');
	oDiv.style.display='block';
	return false;
}

function HideForm() {
	oDiv = document.getElementById('theFormDiv');
	oDiv.style.display='none';
	return false;
}

function VerifyEmail(fromEmail){
	var status = false;     
	var emailRegEx = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	return fromEmail.match(emailRegEx);
}