// JavaScript Document

var mousex = 0;
var mousey = 0;

function ajaxSend(url, divid){

	// we use a javascript feature here called "inner functions"
	// using these means the local variables retain their values after the outer function
	// has returned. this is useful for thread safety, so
	// reassigning the onreadystatechange function doesn't stomp over earlier requests.
		
	function ajaxBindCallback(){
		if (ajaxRequest.readyState == 4) {
			if (ajaxRequest.status == 200) {
				document.getElementById(divid).innerHTML = ajaxRequest.responseText;
			}
		}
	}

	// use a local variable to hold our request and callback until the inner function is called...
	var ajaxRequest = null;
	
	document.getElementById(divid).innerHTML = '<img src="/library/images/loading.gif">';
	// bind our callback then hit the server...
	if (window.XMLHttpRequest) {
		// moz et al
		ajaxRequest = new XMLHttpRequest();
		ajaxRequest.onreadystatechange = ajaxBindCallback;
		ajaxRequest.open("GET", url, true);
		ajaxRequest.send(null);
	} else if (window.ActiveXObject) {
		// ie
		ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		if (ajaxRequest) {
			ajaxRequest.onreadystatechange = ajaxBindCallback;
			ajaxRequest.open("GET", url, true);
			ajaxRequest.send();
		}
	}
}


function openEmailWindow(email, orgid, e){
	
	if (!e) e = window.event;
	
	if (e)
	{ 
		if (e.pageX || e.pageY)
		{ // this doesn't work on IE6!! (works on FF,Moz,Opera7)
			mousex = e.pageX;
			mousey = e.pageY;
			//algor = '[e.pageX]';
			//if (e.clientX || e.clientY) algor += ' [e.clientX] '
		}
		else if (e.clientX || e.clientY)
		{ // works on IE6,FF,Moz,Opera7
			mousex = e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
			mousey = e.clientY + document.body.scrollTop + document.documentElement.scrollTop;
			
			//algor = '[e.clientX]';
			//if (e.pageX || e.pageY) algor += ' [e.pageX] '
		}  
	}
	
	// Create and display the email window over the current page
	var emailDiv = document.createElement("div");
	emailDiv.id = 'emailDiv';
	emailDiv.style.position = 'absolute';
	emailDiv.style.top = (mousey - 300).toString(10) + 'px';
	emailDiv.style.left = '150px';
	emailDiv.style.width = '600px';
	emailDiv.style.height = '400px';
	emailDiv.style.zIndex = '20';
	emailDiv.style.display = 'block';
	emailDiv.style.backgroundColor = 'white';
	emailDiv.style.border = 'thin solid';
	emailDiv.style.paddingLeft = '5px';
	
	var ajaxurl = '/ajax/dirs.cfc?method=openEmailWindow&email=' + email + '&orgID=' + orgid;
	
	document.getElementById('emailWindow').appendChild(emailDiv);
	init();
	
	// populate the email window
	ajaxSend( ajaxurl, emailDiv.id);

}

// Send the email
function sendEmail(){
	
	var email = document.getElementById('email').value;
	var orgID = document.getElementById('orgid').value;
	var reply = document.getElementById('reply').value;
	var subject = document.getElementById('subject').value;
	var emailText = document.getElementById('emailText').value;
	
	var ajaxurl = '/ajax/dirs.cfc?method=sendEmail&email=' + email + '&orgid=' + orgID + '&reply=' + reply + '&subject=' + subject + '&emailText=' + emailText;
	
	ajaxSend( ajaxurl, 'emailForm');
}