var MOUSE_X, MOUSE_Y;

function ajaxGetUpdate(outputId, url){
	new Ajax.Updater(outputId, url, {asynchonous:true, evalScripts:true});
}


function ajaxUpdateParentOutput(outputId){
	url = 'index.php?AJAX_PROC=' + outputId;
	window.opener.ajaxGetUpdate(outputId, url);
}

function ajaxPostUpdate(outputId, formId){
	if (formId){
		postVars = Form.serialize(formId);
	}
	
	url = 'index.php?AJAX_PROC=' + outputId;
	new Ajax.Updater(outputId, url, {asynchonous:true, method:"post", postBody:postVars, evalScripts:true});
}

function ajaxUpdate(outputId){
	url = 'index.php?AJAX_PROC=' + outputId;
	new Ajax.Updater(outputId, url, {asynchonous:true, method:"post", evalScripts:true});
}


function moveElementToMousePosition(element, x, y){
	xStr = x + 'px';
	yStr = y + 'px';

	element.style.left = xStr;
	element.style.top = yStr;
}

function showElement(id){
	Element.setStyle(id, {display: 'block'});
}

function showHelp(contentId){
   $('helpBox').innerHTML = $(contentId).innerHTML; 
	moveElementToMousePosition($('helpBox'), MOUSE_X + 20, MOUSE_Y + 10);
	showElement('helpBox');
}



function hideDialogs(){
	var dialogObjects = document.getElementsByClassName("dialogBox");
	var i=0; 
	
	for (i=0; i < dialogObjects.length; i++){
		Element.hide(dialogObjects[i]);
	}
}

function recordPosition(e) {
	// sets the values of the mouse pointer in the global mouse variables.
	MOUSE_X = Event.pointerX(e);
	MOUSE_Y = Event.pointerY(e);
}

function resizeWindowToImage(elementId, xBuffer, yBuffer) {
	elementWidth = $(elementId).width + xBuffer;
	elementHeight = $(elementId).height + yBuffer;
	if (elementWidth < 450){
		elementWidth = 450;	
	}

	
   self.resizeTo(elementWidth, elementHeight);
} 

function resizeWindow(x, y){
   self.resizeTo(x, y);
}

function confirmDelete(mesg){
	var agree;
	if (!mesg){
		mesg = "Are you sure you want to delete this?";
	} 
	agree = confirm(mesg);
	if (agree){
		return true ;
	} else {
		return false ;
	}	
}

/**
* Used to clear and re-enter the input value of a given input field in a form if it
* matches the defaultValue that is passed into it.
* The action parameter can be "blur" or "focus" depending on whether you want to 
* re-enter the default value onblur or if you want to clear the default value on enter. 
*/
function checkDefaultInputValue(id, defaultValue, action){
//	$('DEBUG').innerHTML += "Value: " + value + " default: "+ defaultValue +" action: "+ action +"<br />"; 
	var value; 
	value = $(id).value;
	if (action == 'focus'){
		$(id).style.color = '';
		if (value == defaultValue){
			value = ''; 
		}
	} else if (action == 'blur'){
		if (value == ''){
			value = defaultValue; 
			$(id).style.color = '#aaaaaa';
		} 
	}
	
	return value;
}



Event.observe(document, "mousemove", recordPosition, false);
Event.observe(document, "click", hideDialogs, false);