var gnPageYOffset = 0;

//--------------------------------------------------------------------------------
//- 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 setScrollY(pnOffset) {
//alert('about to scroll down ' + pnOffset + ' px');
  window.scrollTo(0, pnOffset);
/*
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    window.pageYOffset = pnOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    document.body.scrollTop = pnOffset;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    document.documentElement.scrollTop = pnOffset;
  }
*/
}
//--------------------------------------------------------------------------------
//- Function ShowInfo - display/hide Information DIV
//--------------------------------------------------------------------------------
function doShowInfo(bFlag, pnTop, pnWidth, pnHeight) {
    gnPageYOffset = getScrollY();

	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;

        // If IE6 or below then do this.
        objShaddow = document.getElementById('popupshaddow');
        objShaddow.style.width = (parseInt(document.getElementById('popupwrapper').style.width, 10) + 2) + 'px';
        objShaddow.style.height = (parseInt(document.getElementById('popupwrapper').style.height, 10) + 2) + 'px';
        objShaddow.style.top = document.getElementById('popupwrapper').style.top;
        objShaddow.style.left = nOffset + 'px';

		document.getElementById('popupwrapper').style.left = nOffset + 'px';

	} else {

        // If IE6 or below then do this.
        document.getElementById('popupshaddow').style.left = '-1000px';

		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 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 === null) bFlag = false;

	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;
			default:
				bFlag = true;
		}
	}

	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);
}
//--------------------------------------------------------------------------------
//- Function bothTrim
//--------------------------------------------------------------------------------
function bothTrim(sString) {
    sTmp = leftTrim(sString);
    return rightTrim(sTmp);
}
//--------------------------------------------------------------------------------
//- Function leftTrim
//--------------------------------------------------------------------------------
function leftTrim(sString) {
    // Remember "substring" is index-start, index-end - not length!!
    while (sString.substring(0,1) == ' ')
        sString = sString.substring(1, sString.length);

    return sString;
}
//--------------------------------------------------------------------------------
//- Function rightTrim
//--------------------------------------------------------------------------------
function rightTrim(sString) {
    // Remember "substring" is index-start, index-end - not length!!
    while (sString.substring(sString.length-1) == ' ')
        sString = sString.substring(0, sString.length-1);

    return sString;
}
//--------------------------------------------------------------------------------
//- 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.arrCurrentYearMonths = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
    this.arrYearMonths = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

    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;

        // Set for LEAP year current year months
        if (nShortYear % 4 == 0)
            this.arrCurrentYearMonths[1]++;

        this.sType = '';
        this.sYYYYMMDDFormat = '';

        // This is the expression for checking UK dates in the format 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)$";

        // This is the expression for checking USA dates in the format mm/dd/yyyy or mm/d/yyyy or m/d/yyyy or m/d/yy etc.
        sUSExpression = "^([1-9]|0[1-9]|1[012])([- /.])([1-9]|0[1-9]|[12][0-9]|3[01])\\2((|18|19|20)\\d\\d)$";

        // This is the expression for checking reverse UK dates in the format yyyy/mm/dd or yyyy/mm/d or yyyy/m/d or yy/m/d etc.
        sUKRevExpression = "^((|18|19|20)\\d\\d)([- /.])([1-9]|0[1-9]|1[012])\\2([1-9]|0[1-9]|[12][0-9]|3[01])$";

        // This is the expression for checking month and year format mm/yyyy or m/yyyy or m/yy
        sMonthYearOnly = "^([1-9]|0[1-9]|1[012])([- /.])((|18|19|20)\\d\\d)$";

        sDayOnly = "^([1-9]|0[1-9]|[12][0-9]|3[01])$";

        // 4 characters is taken as Day & Month - '1205' 12th May {Current Year}
		if (psDate.length == 4 && isInteger(psDate, 'GT')) {
            this.nDay = parseInt(psDate.substr(0, 2), 10);
            this.nMonth = parseInt(psDate.substr(2, 2), 10);
			this.nYear = nShortYear + 2000;

            this.sSeparator = '/';
            this.sType = 'UK';

        // 6 characters is taken as Day & Month & Year (YY)
		} else if (psDate.length == 6 && isInteger(psDate, 'GT')) {
            this.nDay = parseInt(psDate.substr(0, 2), 10);
            this.nMonth = parseInt(psDate.substr(2, 2), 10);
			this.nYear = parseInt(psDate.substr(4, 2), 10) + 2000;

            this.sSeparator = '/';
            this.sType = 'UK';

        // 6 characters is taken as Day & Month & Year (YYYY)
		} else if (psDate.length == 8 && isInteger(psDate, 'GT')) {
            this.nDay = parseInt(psDate.substr(0, 2), 10);
            this.nMonth = parseInt(psDate.substr(2, 2), 10);
			this.nYear = parseInt(psDate.substr(4, 4), 10);

            this.sSeparator = '/';
            this.sType = 'UK';

        } else 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 != '') {

            this.arrYearMonths[1] = 28;

            // Set for LEAP year current year months
            if (this.nYear % 4 == 0)
                this.arrYearMonths[1]++;

            bFlag = true;
            if (this.nMonth > 0 && this.nMonth <= 12) {
                if (this.nDay <= 0 || this.nDay > this.arrYearMonths[this.nMonth-1])
                    bFlag = false
            } else
                bFlag = false;


            if (bFlag) {

                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;

        } 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;
}

function getFormattedDate() {
		date = new Date();
		day = date.getDate();
		month = date.getMonth();
		year = date.getFullYear();
		hours = date.getHours();
		minutes = date.getMinutes();

		sResult = day;

		if ((day == 1) || (day == 21) || (day == 31)) {
			sResult += "st ";
		}
		else if ((day == 2) || (day == 22)) {
			sResult += "nd ";
		}
		else if ((day == 3) || (day == 23)) {
			sResult += "rd ";
		}
		else {
			sResult += "th ";
		}

		months = ["January","February","March","April","May","June","July","August","September","October","November","December"];
		sResult += months[month];

		if (hours.length == 1) {
			hours = "0" + hours;
		}
		if (minutes.length == 1) {
			minutes = "0" + minutes;
		}
		sResult += " " + year + " " + hours + ":" + minutes;

		return sResult;
}

function findPosY (obj)
{
    var curtop = 0;
    if(obj.offsetParent) {
        while(1)
        {
          	curtop += obj.offsetTop;
          	if(!obj.offsetParent) {
            	break;
            }
          	obj = obj.offsetParent;
        }
    }
    else if (obj.y) {
        curtop += obj.y;
    }
    return curtop;
}


