var cal = new CalendarPopup()

document.onmousemove = getMouseXY

function getElement(name) {
    if (document.getElementById) {
        return document.getElementById(name)
    }
    else if (document.all) {
        return document.all[name]
    }
    else {
        return null
    }
}

function addTo(srcselectname, dstselectname, duplicatesAllowed) {
	src = getElement(srcselectname)
	dest = getElement(dstselectname)
	
	for (var i = 0; i < src.options.length; i++) {
		if (src.options[i].selected) {
			var add = true
			src.options[i].selected = false
			if (!duplicatesAllowed) {
				for (var j = 0; j < dest.options.length; j++) {
					if (dest.options[j].value == src.options[i].value) {
						add = false
						break
					}
				}
			}
			
			if (add) {
				opt = new Option()
				opt.value = src.options[i].value
				opt.text = src.options[i].text
				dest.options[dest.options.length] = opt
			}
		}
	}
}

function removeFrom(selectname) {
	sel = getElement(selectname)
	
	main: while (true) {
		for (var i = 0; i < sel.options.length; i++) {
			if (sel.options[i].selected) {
				sel.remove(i)
				continue main
			}
		}
		break
	}
}

function displayErrorMessage(msg) {
	alert(msg);
	return false;
}

// get the mouse XY position
function getMouseXY(e) {// works on IE6,FF,Moz,Opera7

  if (!e) e = window.event; // works on IE, but not NS (we rely on NS passing us the 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;
      mousey = e.clientY + document.body.scrollTop;
      algor = '[e.clientX]';
      if (e.pageX || e.pageY) algor += ' [e.pageX] '
    }
  }
}

function selectAllOptions(name) {
	sel = getElement(name)
	for (var i = 0; i < sel.options.length; i++) {
		sel.options[i].selected = true
	}
	return true
}

var setSelection;
function showSelectionDialog(url, width, height, callbackfc) {
	setSelection = callbackfc
	popup = window.open(url, '_blank', this.windowProperties + ',titlebar=no,status=no,width=' + width + ',height=' + height + ',scrollbars=yes,screenX=' + mousex + ',left=' + mousex + ',screenY=' + mousey + ',top=' + mousey, false)
}

function isDateToLessDateFrom(pDateFrom,pDateTo){
	var rVal=false;
	var dFrom = pDateFrom.substring(6,10)+pDateFrom.substring(3,5)+pDateFrom.substring(0,2);
	var dTo = pDateTo.substring(6,10)+pDateTo.substring(3,5)+pDateTo.substring(0,2);

	if (dFrom>dTo){
		alert("Date from cannot be greater than Date to.");
		rVal = false;
	}else{
		rVal = true;
	}
	
	return rVal;
}
function isDateLessToday(pCurr,pDate){
	var rVal	= false;
	var today 	= new Date;
	
	//var dCurr = pCurr.substring(6,10)+pCurr.substring(3,5)+pCurr.substring(0,2);
	var dDate = pDate.substring(6,10)+pDate.substring(3,5)+pDate.substring(0,2);

	var myDate = new Date;
	myDate.setDate(pDate.substring(0,2));
  	myDate.setMonth(pDate.substring(3,5)-1); // January = 0
  	myDate.setFullYear(pDate.substring(6,10));
  	
  	//alert("mydate "+myDate+" todays date "+today);

	if (myDate<today){
		//alert("Date cannot be less than today's date.");
		rVal = true;
	}else{
		rVal = false;
	}
	
	return rVal;
}
function isNaNDesc(val,errMess){
	var rVal = false;
	var vVal = val;
	if (isNaN(vVal)){
		alert(errMess)
		rVal=true;
	}else{
		rVal = false;
	}
	return rVal;
}