
//--------------------------------------------------------------------------------
//- Function Ajax creation of object.
//--------------------------------------------------------------------------------
var goHttpRequest;

function createAjaxRequest() {
	try {
    	goHttpRequest = new XMLHttpRequest();
  	} catch (trymicrosoft) {
    	try {
      		goHttpRequest = new ActiveXObject("Msxml2.XMLHTTP");
    	} catch (othermicrosoft) {
      		try {
        		goHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      		} catch (failed) {
        		goHttpRequest = false;
      		}
    	}
  	}

	if (!goHttpRequest)
    	alert("Error initializing XMLHttpRequest!");
}
//--------------------------------------------------------------------------------
//- Function getScrollX - X scroll position of browser window.
//--------------------------------------------------------------------------------
function getScrollX() {
  var scrOfX = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfX = document.documentElement.scrollLeft;
  }
  return scrOfY;
}
//--------------------------------------------------------------------------------
//- Function getScrollY - Y scroll position of browser window.
//--------------------------------------------------------------------------------
function getScrollY() {
  var scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
  }
  return scrOfY;
}
//--------------------------------------------------------------------------------
//- Function ShowInfo - display/hide Information DIV
//--------------------------------------------------------------------------------
function doShowInfo(bFlag, pnTop, pnWidth, pnHeight) {

	if (pnWidth == 0) pnWidth = 600;

	obj = document.getElementById('popupwrapper');
	obj.style.width = pnWidth + 'px';

	obj = document.getElementById('popupheader');
	obj.style.width = pnWidth + 'px';

	obj = document.getElementById('popupscroll');
	obj.style.width = (pnWidth) + 'px';

	obj = document.getElementById('popupbody');
	obj.style.width = (pnWidth - 40) + 'px';

	if (pnHeight == 0) pnHeight = 207;

	obj = document.getElementById('popupwrapper');
	obj.style.height = pnHeight + 'px';

	obj = document.getElementById('popupscroll');
	obj.style.height = (pnHeight - 20) + 'px';

	// The top is set so that the popup is positioned
	nScrollY = getScrollY();
	if (nScrollY > pnTop)
		document.getElementById('popupwrapper').style.top = (nScrollY + 20) +'px';
	else
		document.getElementById('popupwrapper').style.top = pnTop +'px';

	if (document.body.offsetWidth > pnWidth)
		nOffset = (document.body.offsetWidth - pnWidth) / 2;
	else
		nOffset = 50;

    // If True then show the DIV else hide the DIV
	if (bFlag) {

        if (typeof(gsPopUpText) != 'undefined')
            document.getElementById('popupcontent').innerHTML = gsPopUpText;

		document.getElementById('popupwrapper').style.left = nOffset + 'px';

	} else
		document.getElementById('popupwrapper').style.left = '-1000px';
}
//--------------------------------------------------------------------------------
//- Function HideInfo - hide Information DIV and set pending flag for Page Exit
//--------------------------------------------------------------------------------
function doHideInfo(){
    PageExitSetPending();
    doShowInfo(false, 0, 0, 0);
}
//--------------------------------------------------------------------------------
//- Function beginDrag of Information Window.
//--------------------------------------------------------------------------------
function beginDragInfo(elementToDrag, event){
	var deltaX = event.clientX - parseInt(elementToDrag.style.left);
	var deltaY = event.clientY - parseInt(elementToDrag.style.top);
	if (document.addEventListener){
		document.addEventListener("mousemove", moveHandler, true);
		document.addEventListener("mouseup", upHandler, true);
	}
	else if (document.attachEvent){
		document.attachEvent("onmousemove", moveHandler);
		document.attachEvent("onmouseup", upHandler);
	}
	else {
		var oldmovehandler = document.onmousemove;
		var olduphandler = document.onmouseup;
		document.onmousemove = moveHandler;
		document.onmouseup = upHandler;
	}
	if (event.stopPropagation) event.stopPropagation();
	else event.cancelBubble = true;
	if (event.preventDefault) event.preventDefault();
	else event.returnValue = false;
	function moveHandler(e){
		if (!e) e = window.event;
		elementToDrag.style.left = (e.clientX - deltaX) + "px";
		elementToDrag.style.top = (e.clientY - deltaY) + "px";
		if (e.stopPropagation) e.stopPropagation();
		else e.cancelBubble = true;
	}
	function upHandler(e){
		if (!e) e = window.event;
		if (document.removeEventListener){
			document.removeEventListener("mouseup", upHandler, true);
			document.removeEventListener("mousemove", moveHandler, true);
		}
		else if (document.detachEvent){
			document.detachEvent("onmouseup", upHandler);
			document.detachEvent("onmousemove", moveHandler);
		}
		else {
			document.onmouseup = olduphandler;
			document.onmousemove = oldmovehandler;
		}
		if (e.stopPropagation) e.stopPropagation();
		else e.cancelBubble = true;
	}
}
//--------------------------------------------------------------------------------
//- Function isInteger
//--------------------------------------------------------------------------------
function isInteger(psValue, psEval) {

    if (psEval == undefined)
        alert('isInteger(psValue, psEval) - missing parameter psEval');

	sExpression = /^[0-9]+|^-[0-9]+$/;
	var bFlag = psValue.match(sExpression);
	if (bFlag && psEval != '') {
		bFlag = false;
		switch (psEval) {
			case 'LT':
				if (parseInt(psValue, 10) < 0)
					bFlag = true;
				break;
			case 'LE':
				if (parseInt(psValue, 10) <= 0)
					bFlag = true;
				break;
			case 'GT':
				if (parseInt(psValue, 10) > 0)
					bFlag = true;
				break;
			case 'GE':
				if (parseInt(psValue, 10) >= 0)
					bFlag = true;
				break;
		}
	}

	return bFlag;
}
//--------------------------------------------------------------------------------
//- Function isNumeric
//--------------------------------------------------------------------------------
function isNumeric(psValue, psEval) {

    if (psEval == undefined)
        alert('isNumeric(psValue, psEval) - missing parameter psEval');

	sExpression = /^[0-9]+$|^[0-9]+[.\.][0-9]+$|^[.\.][0-9]+$|^-[0-9]+$|^-[0-9]+[.\.][0-9]+$|^-[.\.][0-9]+$/;
	var bFlag = psValue.match(sExpression);
	if (bFlag && psEval != '') {
		bFlag = false;
		switch (psEval) {
			case 'LT':
				if (parseFloat(psValue) < 0)
					bFlag = true;
			case 'LE':
				if (parseFloat(psValue) <= 0)
					bFlag = true;
			case 'GT':
				if (parseFloat(psValue) > 0)
					bFlag = true;
			case 'GE':
				if (parseFloat(psValue) >= 0)
					bFlag = true;
		}
	}

	return bFlag;
}
//--------------------------------------------------------------------------------
//- Function isEmpty
//--------------------------------------------------------------------------------
function isEmpty(psValue) {
	if (psValue == '') return true;
	sExpression = /^[\s]+$/;
	return psValue.match(sExpression);
}
//--------------------------------------------------------------------------------
//- Class validateDate
//--------------------------------------------------------------------------------
function validateDateTime() {

    // Property.
    this.nMonthYearIsBeginning = true;  // If we detect a 'Month Year Only' format then if
                                        // this is true then set to first day else if false
                                        // then set to last day.

    this.nDay = 0;
    this.nMonth = 0;
    this.nYear = 0;
    this.sSeparator = '';
    this.sType = '';    // UK, US or Error.
    this.bWrongDayValue = false;
    this.sYYYYMMDDFormat = '';
    this.sDDMMYYYYFormat = '';
    this.nDateInDays = 0;

    this.nHour = 0;
    this.nMinute = 0;
    this.nSecond = 0;
    this.sAMPM = '';
    this.sTimeType = 0;
    this.nTotalMins = 0;    // (Hours * 60) + minutes
    this.sHHMMFormat = '00:00';

    function checkDate(psDate) {

		var DyMilli = 1000 * 60 * 60 * 24;
        var dtToday = new Date();
        var nShortYear = dtToday.getFullYear() - 2000;

        this.sType = '';
        this.sYYYYMMDDFormat = '';

        // This is the expression for checking dd/mm/yyyy or d/mm/yyyy or d/m/yyyy or d/m/yy etc.
        // The separators must be the same (the \2 refers to the second token in the expression.
        sUKExpression = "^([1-9]|0[1-9]|[12][0-9]|3[01])([- /.])([1-9]|0[1-9]|1[012])\\2((|18|19|20)\\d\\d)$";
        sUSExpression = "^([1-9]|0[1-9]|1[012])([- /.])([1-9]|0[1-9]|[12][0-9]|3[01])\\2((|18|19|20)\\d\\d)$";
        sUKRevExpression = "^((|18|19|20)\\d\\d)([- /.])([1-9]|0[1-9]|1[012])\\2([1-9]|0[1-9]|[12][0-9]|3[01])$";
        sMonthYearOnly = "^([1-9]|0[1-9]|1[012])([- /.])((|18|19|20)\\d\\d)$";
        sDayOnly = "^([1-9]|0[1-9]|[12][0-9]|3[01])$";

        if (arrToken = psDate.match(sUKExpression)) {
            this.nDay = parseInt(arrToken[1], 10);
            this.nMonth = parseInt(arrToken[3], 10);

            nTmpYear = parseInt(arrToken[4], 10);
            if (arrToken[4].length < 4) {
                if (nTmpYear > 0 && nTmpYear <= nShortYear)
                    nTmpYear += 2000;
            }

            this.nYear = nTmpYear;

            this.sSeparator = arrToken[2];
            this.sType = 'UK';

        } else if (arrToken = psDate.match(sUKRevExpression)) {
            this.nDay = parseInt(arrToken[4], 10);
            this.nMonth = parseInt(arrToken[3], 10);
            this.nYear = parseInt(arrToken[1], 10);
            this.sSeparator = arrToken[2];
            this.sType = 'UKRev';

        } else if (arrToken = psDate.match(sUSExpression)) {
            this.nDay = parseInt(arrToken[3], 10);
            this.nMonth = parseInt(arrToken[1], 10);
            this.nYear = parseInt(arrToken[4], 10);
            this.sSeparator = arrToken[2];
            this.sType = 'US';

        } else if (arrToken = psDate.match(sMonthYearOnly)) {

            this.nMonth = parseInt(arrToken[1], 10);

            nTmpYear = parseInt(arrToken[3], 10);
            if (arrToken[3].length < 4) {
                if (nTmpYear > 0 && nTmpYear <= nShortYear)
                    nTmpYear += 2000;
            }

            this.nYear = nTmpYear;

            // Work out if we need the first or last day in the month.
            if (this.nMonthYearIsBeginning)
                this.nDay = 1;
            else
                this.nDay = lastDayInMonth(this.nMonth, this.nYear);

            this.sSeparator = arrToken[2];
            this.sType = 'UK';

        } else if (arrToken = psDate.match(sDayOnly)) {
            this.nDay = parseInt(arrToken[1], 10);
            this.nMonth = dtToday.getMonth()+1;
            this.nYear = dtToday.getFullYear();
            this.sType = 'UK';
        }

        if (this.sType != '') {

            nCheckDay = lastDayInMonth(this.nMonth, this.nYear);

            // Format YYYYMMDD
            this.sYYYYMMDDFormat = this.nYear + '-';

            if (this.nMonth < 10)
                this.sYYYYMMDDFormat += '0';
            this.sYYYYMMDDFormat += this.nMonth + '-';

            if (this.nDay < 10)
                this.sYYYYMMDDFormat += '0';
            this.sYYYYMMDDFormat += this.nDay;

            // Format DD/MM/YYYY
            this.sDDMMYYYYFormat = '';
            if (this.nDay < 10)
                this.sDDMMYYYYFormat += '0';
            this.sDDMMYYYYFormat += this.nDay + '/';

            if (this.nMonth < 10)
                this.sDDMMYYYYFormat += '0';
            this.sDDMMYYYYFormat += this.nMonth + '/';

            this.sDDMMYYYYFormat += this.nYear;

    		dtToday.setDate(this.nDay);
    		dtToday.setMonth(this.nMonth-1);
    		dtToday.setYear(this.nYear);
    		t = dtToday.getTime();
    		this.nDateInDays = Math.round(t / DyMilli);

			if (this.nDay > nCheckDay) {
			    this.bWrongDayValue = true;
                return false;
            } else
                return true;

        } else
            return false;

    }

    function lastDayInMonth(pnMonthNo, pnYearNo){

        // List of days in a month for use later on. Remember to
        // modify for a leap year!!
        var sCheckMonth = '312831303130313130313031';

        // Last check to see if the day value is valid for the month.
        nIdx = (pnMonthNo-1)*2;
		var nCheckDay = parseInt(sCheckMonth.substr(nIdx, 2));
		if (pnMonthNo == 2) {
			if ((pnYearNo % 4) == 0)
				nCheckDay += 1;
		}

        return nCheckDay;
    }

    // Is the current date in the future?
	function isFutureDate() {

		var DyMilli = 1000 * 60 * 60 * 24;

		d = new Date();
		t = d.getTime();
		nDaysToday = Math.round(t / DyMilli);

		d.setDate(this.nDay);
		d.setMonth(this.nMonth-1);
		d.setYear(this.nYear);
		t = d.getTime();
		nDaysCurrentDate = Math.round(t / DyMilli);

		if (nDaysCurrentDate > nDaysToday)
			return true;
		else
			return false;
	}

    function checkTime(psTime){

        this.nTotalMins = 0;
        this.sTimeType = 0;
        this.sHHMMFormat = '';

        if (psTime == '0' || psTime == '') {
            this.sHHMMFormat = '00:00';
            this.sTimeType = 1;
            return true;
        }

        // This is the expression for checking hh:mm or h:mm or hh:m or h:m etc.
        sTime1Expression = "^([0-9]|0[0-9]|1[0-9]|2[0-4])([ :,.])([0-9]|0[0-9]|[0-5][0-9])$";
        if (arrToken = psTime.match(sTime1Expression)) {
            this.nHour = parseInt(arrToken[1], 10);
            this.nMinute = parseInt(arrToken[3], 10);
            this.sTimeType = 1;

            this.nTotalMins = (parseInt(arrToken[1],10) * 60) + parseInt(arrToken[3],10);

            if (this.nHour < 10)
                this.sHHMMFormat = '0';
            this.sHHMMFormat += this.nHour + ':';

            if (this.nMinute < 10)
                this.sHHMMFormat += '0';
            this.sHHMMFormat += this.nMinute;

        }

        // This is the expression for checking h or hh.
        sTime2Expression = "^([0-9]|0[0-9]|1[0-9]|2[0-4])$";
        if (arrToken = psTime.match(sTime2Expression)) {

            this.nHour = parseInt(arrToken[1], 10);
            this.nMinute = 0;
            this.sTimeType = 1;

            this.nTotalMins = (parseInt(arrToken[1],10) * 60);

            if (this.nHour < 10)
                this.sHHMMFormat = '0';
            this.sHHMMFormat += this.nHour + ':00';
        }

        if (this.sTimeType == 0)
            return false;
        else
            return true;
    }

    validateDateTime.prototype.checkDate = checkDate;
    validateDateTime.prototype.checkTime = checkTime;
    validateDateTime.prototype.isFutureDate = isFutureDate;

}
//--------------------------------------------------------------------------------
/**
 *
 * Decimal Degrees to Degrees and Decimal Minutes
 * @return formatted string
 * Mode 0 - HTML
 *      1 - Decimal Degrees
 *      2 - Array: Deg Min Dir.
 **/
function GetDD2DDM(pnMode, psType, pnValue) {

    if (psType == 'LAT') {

		var sDir = msSouth;
		if (pnValue >= 0) sDir = msNorth;
		nNSDeg = Math.floor(Math.abs(pnValue));
		nRem = Math.abs(pnValue) - Math.floor(Math.abs(pnValue));
		nNSMin = nRem * 60;
		sTmp = nNSMin + '0000';
		if (sTmp.indexOf('.') >= 0)
			nNSMin = sTmp.substr(0, sTmp.indexOf('.')+3);

        if (pnMode == 0) {
		    sResult = nNSDeg + '<sup>o</sup> ' + nNSMin + '\' ' + sDir;

        } else if (pnMode == 1) {
    		nMlt = 1;
    		if (pnValue < 0) nMlt = -1;
    		sResult = (parseInt(nNSDeg, 10) + (parseFloat(nNSMin, 10) / 60)) * nMlt;

        } else if (pnMode == 2) {
            var sResult = new Array;
            sResult[0] = nNSDeg;
            sResult[1] = nNSMin;
            sResult[2] = sDir;

        } else
            alert('Mode must be 0 - HTML; 1 - Decimal Degrees; 2 - Array: Deg Min Dir.');

    } else if (psType == 'LONG') {

		var sDir = msWest;
		if (pnValue >= 0) sDir = msEast;
		nDeg = Math.floor(Math.abs(pnValue));
		nRem = Math.abs(pnValue) - Math.floor(Math.abs(pnValue));
		nEWDeg = Math.floor(Math.abs(pnValue));
		nEWMin = nRem * 60;
		sTmp = nEWMin + '0000';
		if (sTmp.indexOf('.') >= 0)
			nEWMin = sTmp.substr(0, sTmp.indexOf('.')+3);

        if (pnMode == 0) {
    		sResult = nEWDeg + '<sup>o</sup> ' + nEWMin + '\' ' + sDir;

        } else if (pnMode == 1) {
			nMlt = 1;
			if (pnValue < 0) nMlt = -1;
			sResult = (parseInt(nEWDeg, 10) + (parseFloat(nEWMin, 10) / 60)) * nMlt;

        } else if (pnMode == 2) {
            var sResult = new Array;
            sResult[0] = nEWDeg;
            sResult[1] = nEWMin;
            sResult[2] = sDir;

        } else
            alert('Mode must be 0 - HTML; 1 - Decimal Degrees; 2 - Array: Deg Min Dir.');

    } else
        alert('GetDD2DDM incorrect Type. Type must be LAT or LONG');

    return sResult;
}

//------------------------------------------------------------------------------
//- List functions - Clear the list but leave the first entry alone.
//------------------------------------------------------------------------------
function  qpqListClearNotFirst(poObj) {
	while (poObj.options.length > 1) {
		poObj.remove(1);	// Note: Always remove(0) and NOT remove(i)
	}
}

//------------------------------------------------------------------------------
//- List functions - Clear the list of everything.
//------------------------------------------------------------------------------
function  qpqListClear(poObj) {
	while (poObj.options.length > 0) {
		poObj.remove(0);	// Note: Always remove(0) and NOT remove(i)
	}
}

//------------------------------------------------------------------------------
//- List functions - Add an item to the end of the list.
//------------------------------------------------------------------------------
function qpqListAddItem(poObj, psValue, psText) {
	var oNewOption = document.createElement('option');
  	oNewOption.text = psText;
  	oNewOption.value = psValue;
	try {
    	poObj.add(oNewOption, null); // standards compliant
    } catch(ex) {
    	poObj.add(oNewOption); // IE only
    }
}

//------------------------------------------------------------------------------
//- List functions - Add the contents of the ~ separated string to the list.
//					 The values are in pairs; value + text.
//------------------------------------------------------------------------------
function qpqListLoadItems(poObj, psValues) {

	var arrItems = psValues.split("~");
	for (i = 0; i < arrItems.length-1; i += 2) {
		var oNewOption = document.createElement('option');
	  	oNewOption.value = arrItems[i];
	  	oNewOption.text  = arrItems[i+1];
		try {
	    	poObj.add(oNewOption, null); // standards compliant
	    } catch(ex) {
	    	poObj.add(oNewOption); // IE only
	    }
	}
}
