var lastMouseX;
var lastMouseY;
var curPopupWindow = null;

function setLastMousePosition(e) {
	if (navigator.appName.indexOf("Microsoft") != -1) e = window.event;
	lastMouseX = e.screenX;
	lastMouseY = e.screenY;
}

function openPopup(url, name, pWidth, pHeight, features, snapToLastMousePosition) {
	openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, true);
}

function openPopupFocus(url, name, pWidth, pHeight, features, snapToLastMousePosition, closeOnLoseFocus) {
	closePopup();

	if (snapToLastMousePosition) {
		if (lastMouseX - pWidth < 0) {
			lastMouseX = pWidth;
		}
		if (lastMouseY + pHeight > screen.height) {
			lastMouseY -= (lastMouseY + pHeight + 50) - screen.height;
		}
		lastMouseX -= pWidth;
		lastMouseY += 10;
		features += "screenX=" + lastMouseX + ",left=" + lastMouseX + "screenY=" + lastMouseY + ",top=" + lastMouseY;
	}

	if (closeOnLoseFocus) {
		curPopupWindow = window.open(url, name, features, false);
		curPopupWindow.focus();
	} else {
		win = window.open(url, name, features, false);
		win.focus();
	}
}

function closePopup() {
	if (curPopupWindow != null) {
       
		if (!curPopupWindow.closed)
			curPopupWindow.close();
		
		curPopupWindow = null;
	}
}

function openLookup(baseURL, tbl, frm, fld_id, fld_txt, param) {
	var str = baseURL + "/includes/lookup/lookup.cfm?tbl=" + tbl + "&frm=" + frm + "&fld_id=" + fld_id + "&fld_txt=" + fld_txt;

	if (param == undefined) param = '';
	
	str += '&param=' + param;
	
	openPopup(str, "lookup", 430, 300, "width=430,height=300,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}

function openCombo(baseURL, frm, fld, evt) {
	var str = baseURL + "/includes/lookup/combo.cfm?frm=" + frm + "&fld=" + fld + "&evt=" + evt;
	
	openPopup(str, "combo", 300, 200, "width=300,height=200,toolbar=no,status=no,directories=no,menubar=no,resizable=yes,scrollable=no", true);
}



/*function showDate(baseURL, fld, frm) {
	var str = baseURL + "/includes/lookup/cal.cfm?frm=" + frm + "&fld=" + fld;
	if (eval("document." + frm + "." + fld).value.length > 0)
		str = str + "&cdate=" + eval("document." + frm + "." + fld).value;
	
		openPopup(str, "calwin", 170, 190, "width=170,height=190,toolbar=no,status=no,directories=no,menubar=no,resizable=no,scrollable=no", true);
}*/


var base;
var calId;
var prevCalId;

function showDate(baseURL, fld, frm,id) {
	closeDate();
	base = baseURL;
	calId = id;
	
	var str = baseURL + "/includes/lookup/cal.cfm?frm=" + frm + "&fld=" + fld;
	if (eval("document." + frm + "." + fld).value.length > 0)
		str = str + "&cdate=" + eval("document." + frm + "." + fld).value;
		
	document.getElementById(calId).style.display = "block";
		
	var a = new Ajax.Request(str,
	{
		'onSuccess' : function (response){
			document.getElementById(calId).innerHTML = response.responseText;
			return true;
		},
		'onFailure' : function(response){
			document.body.innerHTML = response.responseText;
			return false;
		}
	});
}

function showDateDrp(baseURL, fld, frm,id,calid) {
	closeDate();
	if(prevCalId != calId){
		base = baseURL;
		calId = calid;
		prevCalId = calid;
		var str = baseURL + "/includes/lookup/cal.cfm?frm=" + frm + "&fld=" + fld+"&drp=1&id="+id;

		var dt = eval("document." + frm + "." + "monthdropdown" + id).value + '-' + eval("document." + frm + "." + "daydropdown" + id).value + "-" + eval("document." + frm + "." + "yeardropdown" + id).value;
		
		str = str + "&cdate=" + dt;
			
		document.getElementById(calid).style.display = "block";
	
		var a = new Ajax.Request(str,
		{
			'onSuccess' : function (response){
				document.getElementById(calId).innerHTML = response.responseText;
				return true;
			},
			'onFailure' : function(response){
				document.body.innerHTML = response.responseText;
				return false;
			}
		});
	}
	else{
		prevCalId = '';
		calId = calid;
	}
}

function showDate2(fld, frm, dt) {
	//alert('here');
	var str = base + "/includes/lookup/cal.cfm?frm=" + frm + "&fld=" + fld+"&cdate="+dt;
		
	document.getElementById(calId).style.display = "block";
		
	var a = new Ajax.Request(str,
	{
		'onSuccess' : function (response){
			document.getElementById(calId).innerHTML = response.responseText;

			return true;
		},
		'onFailure' : function(response){
			document.getElementById(calId).innerHTML = response.responseText;

			return false;
		}
	});
}

function showDateDrpCal(fld, frm, dt,id,drp) {
	//alert('here');
	var str = base + "/includes/lookup/cal.cfm?frm=" + frm + "&fld=" + fld+"&cdate="+dt+"&drp="+drp+"&id="+id;
	
	document.getElementById(calId).style.display = "block";
		
	var a = new Ajax.Request(str,
	{
		'onSuccess' : function (response){
			document.getElementById(calId).innerHTML = response.responseText;

			return true;
		},
		'onFailure' : function(response){
			document.getElementById(calId).innerHTML = response.responseText;

			return false;
		}
	});
}

function closeDate() {
	
	try {
		prevCalId = '';
		if(document.getElementById(calId) != null)
		document.getElementById(calId).style.display = "none";
		
	}catch(e){}
}

function updateDate(str,frm,fld) {
	document[frm][fld].value = str;
	document[frm][fld].focus();
	document.getElementById(calId).style.display = "none";
}
var calId = '';
function updateDateDrp(str,frm,fld,id) {
	//alert(str+','+frm+','+fld+','+id);
	
	if(fld.length > 0){
		document[frm][fld].value = str;
		//document[frm][fld].focus();
	}
	
	try{
		if(calId.length >0)
		document.getElementById(calId).style.display = "none";
	}catch(e){}
	
	spl = str.split("-");
	
	//alert('spl[0] = ' + spl[0] + ' spl[1] = ' + spl[1] + ' spl[2] = '+ spl[2]);
	
	if(spl[0].substring(0,1) == '0'){
		spl[0] = spl[0].substring(1,2);
	}
	
	if(spl[1].substring(0,1) == '0'){
		spl[1] = spl[1].substring(1,2);
	}
	
	if(spl[2].substring(0,1) == '0'){
		spl[2] = spl[2].substring(1,2);
	}

	
	var d = parseInt(spl[1]);
	var m = parseInt(spl[0]);
	var y = parseInt(spl[2]);
	
	//alert('m='+m);
	try{
		var dElem = document.getElementById('daydropdown'+id);
		var mElem = document.getElementById('monthdropdown'+id);
		var yElem = document.getElementById('yeardropdown'+id);
	
		if (typeof dElem.options == 'object') {
			for(i=0; i < dElem.options.length; i++){
				if(d == dElem.options[i].value){
					dElem.options[i].selected = true;
					break;
				}
			}
		} else if (typeof dElem.value == 'string') {
			dElem.value = d;
		}
	
		if (typeof mElem.options == 'object') {
			for(i=0; i < mElem.options.length; i++){
				if(m == mElem.options[i].value){
					
					mElem.options[i].selected = true;
					break;
				}
			}
		} else if (typeof mElem.value == 'string') {
			mElem.value = m;
		}
		
		if (typeof yElem.options == 'object') {
			for(i=0; i < yElem.options.length; i++){
				if(y == yElem.options[i].value){
					yElem.options[i].selected = true;
					break;
				}
			}
		} else if (typeof yElem.value == 'string') {
			yElem.value = y;
		}
	}catch(e){}
}




function openWindow(url) {
  var popupWin = window.open(url, 'newWin',  'status=no,scrollbars,resizable,menubar,toolbar,dependent,width=777,height=457,left=50,top=50');
	popupWin.focus();
}

function openPTWindow(url) {
  var popupWin = window.open(url, 'newWin',  'scrollbars,resizable,dependent,width=777,height=457,left=50,top=50');
	popupWin.focus();
}

function openMap(url) {
  var mapWin = window.open(url, 'mapWin',  'status,scrollbars,resizable,menubar,toolbar,dependent,width=770,height=502,left=50,top=50');
	mapWin.focus();
}

function openWindowGolf(url) {
  popupWin = window.open(url, 'golfWin',  'status,scrollbars,resizable,toolbar=yes,dependent,width=640,height=550,left=50,top=50');
	popupWin.focus();
}	

function openPolicy(url) {
  popupWin = window.open(url, 'PWin',  'status,scrollbars,resizable,toolbar=yes,dependent,width=610,height=550,left=50,top=50');
	popupWin.focus();
}	
