//disable text selection for IE
//function disableSelection() {
//  var element = document.getElementById("timetable2");
//  if (element!=null) {
//  	element.ondrag = function () { return false; };
//  	element.onselectstart = function () { return false; };
//  }
//}

//set left & right menu button handlers
document.oncontextmenu = mRightCl;
document.onmousedown = mLeftCl;
//disableSelection();

var tt2MouseOverFieldId=-1;     //id of training field under mouse (-1 if none)
var tt2MouseOverMenu=false; //menu over opened menu? THIS IS SET FROM EVERY MENU DIV (see TimeTable2Component.java: renderPopupAndContextMenuDiv)
var tt2MenuOpenedForFieldId=-1; //id of training field having menu opened for (-1 if none)
var tt2PopupEnabled=true; //is popup enabled?

//mouse click on field
function fCl(trainingId, action) {

	var actionId = document.getElementById("tt2_action");
	if (actionId!=null) actionId.value=action;
	
	var trainingIdId = document.getElementById("tt2_trainingId");
	if (trainingIdId!=null) trainingIdId.value=trainingId;
	
	//submit form
	var frm = document.getElementById("tt2_prebookForm");
	if (frm!=null) {
		frm.submit();
		frm.reset();	
	}
}

//mouse click on full field
function fClS(trainingId, action) {

	if (substituteConfirm()) {

		var actionId = document.getElementById("tt2_action");
		if (actionId!=null) actionId.value=action;
		
		var trainingIdId = document.getElementById("tt2_trainingId");
		if (trainingIdId!=null) trainingIdId.value=trainingId;
		
		//submit form
		var frm = document.getElementById("tt2_prebookForm");
		if (frm!=null) {
			frm.submit();
			frm.reset();	
		}
	}
}

//show/hide popup of field
function fP(trainingId, trainingPrice, action, e) {
	try {
		var pDiv=document.getElementById("tt2_pp_"+trainingId); //gecko
	} 
	catch (exc) {
		var pDiv=document.all("tt2_pp_"+trainingId);	//ie
	}
	if (pDiv==null) return false;
    if (e==null) e=event; //ie has global "event", gecko local (e)
		
	pDiv.innerHTML=pDiv.innerHTML.replace(/%1/,trainingPrice);
	
    pDiv.style.left = e.clientX+document.body.scrollLeft + 5;
    pDiv.style.top = e.clientY+document.body.scrollTop;
    
    if (action=="s") {
    	if (tt2PopupEnabled) pDiv.style.display = "";
    	tt2MouseOverFieldId=trainingId;
    }
    if (action=="h") {
    	if (tt2PopupEnabled) pDiv.style.display = "none";
    	tt2MouseOverFieldId=-1;
    }
}
//show/hide context menu of field
function fC(trainingId, action, e) {
	try {
		var pDiv=document.getElementById("tt2_cm_"+trainingId);	//gecko
	}
	catch (exc) {
		var pDiv=document.all("tt2_cm_"+trainingId);	 //ie
	}
	
	if (pDiv==null) return false;
    if (e==null) e=event; //ie has global "event", gecko local (e)

    
    pDiv.style.left = e.clientX+document.body.scrollLeft + 5;
    pDiv.style.top = e.clientY+document.body.scrollTop;
    
    if (action=="s") {
    	pDiv.style.display = "";
    	tt2MenuOpenedForFieldId=trainingId;
    }
    
    if (action=="h") {
   		pDiv.style.display = "none";
   		tt2MenuOpenedForFieldId=-1;
    }

}

function dH(e,sel) {
 if (sel) {
   e.style.fontWeight="bold";
 } else {
   e.style.fontWeight="";
 }
}

//global mouse right click handler
function mRightCl(event) {
	if (tt2MouseOverFieldId != -1) {
	    var _id=tt2MouseOverFieldId;
	    fP(_id, 0, "h", event);
		
		tt2PopupEnabled=false; //disable popups
		fC(_id, "s", event); //show context menu
		return false;
	}
}

//global mouse left click handler
function mLeftCl(event) {
	if (!tt2MouseOverMenu) {
		fC(tt2MenuOpenedForFieldId, "h", event); //hide context menu
		tt2PopupEnabled=true; //enable popups again
	}
	return true;
}
