﻿
//-------------------------------------------------------------------
function jsValue(objValue)
{
	var dblValue= 0;
	dblValue=parseFloat(objValue);
	if (isNaN(dblValue)) return 0;	
	return dblValue;
}

//-------------------------------------------------------------------
function jsLTRIM(strText)
{
	var i=0;
	for (i=0; i<strText.length; i++) if (strText.charCodeAt(i)!=32) break;
	return strText.substr(i);
}

function jsRTRIM(strText)
{
	var i=0;
	for (i=strText.length-1; i>=0; i--) if (strText.charCodeAt(i)!=32) break;
	return strText.substr(0, i+1);
}

function jsTRIM(strText)
	{return jsLTRIM(jsRTRIM(strText));}

//-------------------------------------------------------------------
function jsFormatNumber(cyValue, iDecPlaces) {	
	var strText, dblValue, dStr, pStr, i;
	strText='';
	cyValue= jsValue(cyValue );
	
	dblValue = jsRound(cyValue,iDecPlaces);
		
	dblValue=Math.abs(dblValue);
	strText=''+dblValue;
	
	if (strText.indexOf('.')<0) strText+='.'
	
	dStr=strText.substr(0,strText.indexOf("."));
	
	dNum=dStr-0;
	if (iDecPlaces) {
		pStr=strText.substr(strText.indexOf("."));
		while (pStr.length<(iDecPlaces+1)) pStr+="0";
		strText = dStr + pStr;
		}
	else strText = dStr;
	if (cyValue<0) strText='-' + strText;
	return strText;
}

function jsCurrency(cyValue)
	{return jsFormatNumber(cyValue,2);}

function jsRight(strValue, iCount)
{
	var strText = new String(strValue);
	if (iCount<1) iCount=0;
	if (iCount<strText.length) {
		var i=strText.length-iCount;
		return strText.substring(i,i+iCount);
	}
	else return strText;
}

function jsLeft( strValue, iCount ) {
	var strText = new String(strValue);
	if (iCount<1) iCount=0;
	return strText.substring(0,iCount);
}

//-------------------------------------------------------------------
function jsFormatDateTime( oDate ) {
	var d, m, y, h, mi;
	if (oDate == null) return '';
	d=oDate.getDate();
	m=oDate.getMonth()+1;
	y=oDate.getFullYear();				
	h=oDate.getHours();
	mi=oDate.getMinutes();	
	if (h || mi) return	jsRight('00' + d,2) + '/' + jsRight('00'+m,2) + '/' + jsRight('0000'+y,4) +' '+ jsRight('00'+h,2) + ':' + jsRight('00'+mi,2);
	return jsRight('00' + d,2) + '/' + jsRight('00'+m,2) + '/' + jsRight('0000'+y,4);
}

function jsFormatLongDateTime( oDate ) {
	var d, m, y, h, mi;
	if (oDate == null) return '';
	d=oDate.getDate();
	m=oDate.getMonth()+1;
	y=oDate.getFullYear();				
	h=oDate.getHours();
	mi=oDate.getMinutes();	
	if (h || mi) return	jsRight('00' + d,2) + ' ' + jsMonthName(m) + ' ' + jsRight('0000'+y,4) +' '+ jsRight('00'+h,2) + ':' + jsRight('00'+mi,2);
	return jsRight('00' + d,2) + ' ' + jsMonthName(m) + ' ' + jsRight('0000'+y,4);
}

function jsFormatLongDate( oDate ) {
	var d, m, y, h, mi;
	if (oDate == null) return '';
	d=oDate.getDate();
	m=oDate.getMonth()+1;
	y=oDate.getFullYear();				
	h=oDate.getHours();
	mi=oDate.getMinutes();	
	return jsRight('00' + d,2) + ' ' + jsMonthName(m) + ' ' + jsRight('0000'+y,4);
}

function jsFormatDate( oDate ) {
	var d, m, y;	
	if (oDate == null) return'';
	d=oDate.getDate();
	m=oDate.getMonth()+1;
	y=oDate.getFullYear();				

	return jsRight('00' + d,2) + '/' + jsRight('00'+m,2) + '/' + jsRight('0000'+y,4);
}

function jsText2Date(strDateText) {	
	var d=-1, m=-1, y=-1, th=0, tm=0;
	var thPM = 0;	
	var IS_CHAR=1;
	var IS_NUM=2;
	
	var p = new Array();		
	var i, bNewWord=true, iWordCount = 0, j, last_char_type=0;	
	var strUKDate = new String(strDateText);
	for (i=0;i<strUKDate.length;i++) 
		{
			switch (strUKDate.charAt(i)) {
				case ' ':
				case '/':
				case ':':
				case '.':
				case '-':
					bNewWord=true;
					break;
					
				default:
					if (!bNewWord) {
						if (strUKDate.charAt(i)>='0' && strUKDate.charAt(i) <='9') {
							if (last_char_type != IS_NUM) bNewWord=true;
						}					
						else if (last_char_type != IS_CHAR) bNewWord=true;
					}
					if (bNewWord) {
						last_char_type=0;
						iWordCount++;
						p[iWordCount]='';
						bNewWord=false;												
						}
					if (strUKDate.charAt(i)>='0' && strUKDate.charAt(i) <='9') last_char_type = IS_NUM;
					else last_char_type = IS_CHAR;

					p[iWordCount] += strUKDate.charAt(i);
				}			
		};	

	//Test for PM
	thPM = 0;
	for (i=1; i<=iWordCount;i++) {
		if (p[i].toUpperCase() == 'PM') {
			thPM = 12;
			p[i]='';
			break;
			}
		}
	
	//Test for Year between 1900-2099
	for (i=1; i<=iWordCount;i++) {
		j=jsValue(p[i]);
		if (j>=1900 && j<=2099 && p[i].length) {
			y=j;
			p[i]='';
			break;
			}
		}
		
	//Test for Month Name
	for (i=1; i<=iWordCount;i++) {
		switch (p[i].toUpperCase()) {
			case 'JAN':
			case 'JANUARY':
				m=1;
				p[i]='';
				break;
			case 'FEB':
			case 'FEBRUARY':
				m=2;
				p[i]='';
				break;
			case 'MAR':
			case 'MARCH':
				m=3;
				p[i]='';
				break;
			case 'APR':
			case 'APRIL':
				m=4;
				p[i]='';
				break;
			case 'MAY':
				m=5;
				p[i]='';
				break;
			case 'JUN':
			case 'JUNE':
				m=6;
				p[i]='';
				break;
			case 'JUL':
			case 'JULY':
				m=7;
				p[i]='';
				break;
			case 'AUG':
			case 'AUGUST':
				m=8;
				p[i]='';
				break;
			case 'SEP':
			case 'SEPT':
			case 'SEPTEMBER':
				m=9;
				p[i]='';
				break;
			case 'OCT':
			case 'OCTOBER':
				m=10;
				p[i]='';
				break;
			case 'NOV':
			case 'NOVEMBER':
				m=11;
				p[i]='';
				break;
			case 'DEC':
			case 'DECEMBER':
				m=12;
				p[i]='';
				break;
			}
		}
		
	//Set Day to first non blank numeric field
	for (i=1; i<=iWordCount;i++) {
		j=jsValue(p[i]);
		if (j>=1 && j<=31 && p[i].length) {
			d=j;
			p[i]='';
			break;
			}
		}
		
	//set Month to next non blank numeric field if month isn't already set
	if (m<0) for (i=1; i<=iWordCount;i++) {
		j=jsValue(p[i]);
		if (j>=1 && p[i].length) {
			m=j;
			p[i]='';
			break;
			}
		}
		
	//set Year to next non blank numeric field if year isn't already set
	if (y<0) for (i=1; i<=iWordCount;i++) {
		j=jsValue(p[i]);
		if ( ((j>=0 && j<=99) || (j>=1900 && j<=2099) ) && !isNaN(p[i]) && p[i].length ) {
			y=j;
			p[i]='';
			break;
			}
		}

	//set Hour to next non blank numeric field if year isn't already set
	if (! th ) for (i=1; i<=iWordCount;i++) {
		j=jsValue(p[i]);
		if ( j>=0 && j<=23 && p[i].length ) {
			th=j;
			if (j>0 && j<11) th += thPM;
			p[i]='';
			break;
			}
		}

	//set Minutes to next non blank numeric field if year isn't already set
	if (! tm ) for (i=1; i<=iWordCount;i++) {
		j=jsValue(p[i]);
		if ( j>=0 && j<=59 && p[i].length ) {
			tm=j;
			p[i]='';
			break;
			}
		}
	
	if (y>=0)
	{
		if (y<40) y +=2000;
		if (y<=99) y +=1900;
	}

	if (m>0)
	{
		switch (m) {
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
				if (d>31) d=31;
				break;
			case 2:
				if ( (y % 4) ==0) if (d>29) d=29;
				if ( (y % 4) ) if (d>28) d=28;
				break;
			default:
				if (d>30) d=30;
			}
	}
	if (d<1 || m<1 || m>12 || y<1) return null;
	if (th || tm) return new Date(y,(m-1),d, th, tm);	
	return new Date(y,(m-1),d);	
}

//-------------------------------------------------------------------
function jsMonthName(iMonth)
{
	var aMonth = new Array('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
	return aMonth[iMonth];
}

function jsValidateDate(strValue)
{
	var str=jsTRIM(strValue);	
	if (str != '') str = jsFormatLongDate(jsText2Date(str));
	return str;	
}

function jsValidateDateTime(strValue) {
	var str=jsTRIM(strValue);	
	if (str != '') str = jsFormatLongDateTime(jsText2Date(str));
	return str;	
}

function jsValidateDateWithWarning(strValue)
{
	var str=jsTRIM(strValue);
	if (str !='')
	{
		str=jsFormatLongDateTime(jsText2Date(str));
		if (str=='') alert('This is not a recognised date format.');
	}
	return str;
}

function jsValidateNumber(strValue)
	{return jsValidateNumberRange(strValue, -2147483648, 2147483647, 0)} 

function jsValidateNumberDP(strValue, dp) {
	var str=jsTRIM(strValue);	
	if (str != '') str = jsFormatNumber(str,dp );
	return str;	
} 

function jsValidateNumberRange(strValue, cyMin, cyMax, dp) {
	var str=jsTRIM(strValue);	
	var num;
	if (str != '') str = jsFormatNumber(str,dp );
	num = jsValue(str);
	if (num <= jsValue(cyMin) || num >= jsValue(cyMax)) str='';
	return str;	
} 

function jsValidateLargeCurrency(strValue)
	{return jsValidateNumberRange(strValue, -10000000, 10000000, 2);} 


function jsValidateCurrency(strValue)
	{return jsValidateNumberRange(strValue, -214000, 214000, 2);} 

function jsValidatePercentage(strValue)
	{return jsValidateNumberRange(strValue, -214000, 214000, 2) + '%';} 
function jsGetPercentage(strValue) {
	var str = jsTRIM(strValue);
	return eval(str.replace('%', ''));
} 

function jsValidateText(strValue, iLength)
{
	var str=jsTRIM(strValue);
	if (str != '') str = jsLeft(str, jsValue(iLength));
	return str;
} 

//-------------------------------------------------------------------
function jsValidateTime(strValue)
{
	var str=jsTRIM(strValue);
	if (str != '') str = jsFormatTime(jsText2Time(str));
	return str;
} 

function jsFormatTime(oDate)
{
	var d, m, y, h, mi;
	if (oDate == null) return '';
	h=oDate.getHours();
	mi=oDate.getMinutes();
	return jsRight('00'+h,2) + ':' + jsRight('00'+mi,2);
}

function jsText2Time(strTimeText)
{
	var d=1, m=1, y=2000, th=0, tm=0;
	var thPM = 0;	
	var IS_CHAR=1;
	var IS_NUM=2;
	var p = new Array();		
	var i, bNewWord=true, iWordCount = 0, j, last_char_type=0;	
	var str = new String(strTimeText);
	for (i=0; i<str.length; i++) 
	{
		switch (str.charAt(i))
		{
			case ' ':
			case '/':
			case ':':
			case '.':
			case '-':
				bNewWord=true;
				break;
				
			default:
				if (!bNewWord)
				{
					if (str.charAt(i)>='0' && str.charAt(i) <='9')
						{if (last_char_type != IS_NUM) bNewWord=true;}
					else if (last_char_type != IS_CHAR) bNewWord=true;
				}
				if (bNewWord)
				{
					last_char_type=0;
					iWordCount++;
					p[iWordCount]='';
					bNewWord=false;
				}
				if (str.charAt(i)>='0' && str.charAt(i) <='9') last_char_type = IS_NUM;
				else last_char_type = IS_CHAR;

				p[iWordCount] += str.charAt(i);
		}
	};

	//Test for PM
	thPM = 0;
	for (i=1; i<=iWordCount;i++) {
		if (p[i].toUpperCase() == 'PM' || p[i].toUpperCase() == 'P') {
			thPM = 12;
			p[i]='';
			break;
			}
		}

	//set Hour to next non blank numeric field if year isn't already set
	if (! th ) for (i=1; i<=iWordCount;i++) {
		j=jsValue(p[i]);
		if ( j>=0 && j<=23 && p[i].length ) {
			th=j;
			if (j>0 && j<11) th += thPM;
			p[i]='';
			break;
			}
		}

	//set Minutes to next non blank numeric field if year isn't already set
	if (! tm ) for (i=1; i<=iWordCount;i++) {
		j=jsValue(p[i]);
		if ( j>=0 && j<=59 && p[i].length ) {
			tm=j;
			p[i]='';
			break;
			}
		}
	return new Date(y,(m-1),d, th, tm);
}

function getPosition(e){
	var left = 0;
	var top  = 0;

	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}

	left += e.offsetLeft;
	top  += e.offsetTop;

	return {x:left, y:top};
}

function getMousePosition(e){
	if(!e) e=window.event;
	return {x:e.clientX, y:e.clientY};
}

function getWindowDimensions()
{
	var width = 0;
	var height = 0;

	// Window dimensions: 
	if (window.innerWidth) {
		width = window.innerWidth;
		height = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth) {
		width = document.documentElement.clientWidth;
		height = document.documentElement.clientHeight;
	}
	else if (document.body) {
		width = document.body.clientWidth;
		height = document.body.clientHeight;
	}

	return {width:width, height:height};
}

function getScrollDimensions()
{
	var width = 0;
	var height = 0;

	// Window dimensions: 
	if (window.innerWidth) {
		width = window.innerWidth;
		height = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.scrollWidth) {
		width = document.documentElement.scrollWidth;
		height = document.documentElement.scrollHeight;
	}
	else if (document.body) {
		width = document.body.scrollWidth;
		height = document.body.scrollHeight;
	}

	return {width:width, height:height};
}

function round(x,y){
	return jsRound(x,y);
	}

function jsRound(x,y) {
	var i, intFactor;
	intFactor=1;
	for (i=y;i>0;i--) intFactor *= 10;
	return Math.round(x * intFactor) / intFactor;	
	}

function loadXML(xml)
{
	var xmlDoc;
	//load xml file
	// code for IE

	if (document.all)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.loadXML(xml);
		return xmlDoc;
	}
	// code for Mozilla, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
	
	 //create a DOMParser
    var objDOMParser = new DOMParser();
     
    //create new document from string
    var xmlDoc = objDOMParser.parseFromString(xml, "text/xml");
 		return xmlDoc;
	}
	else
	{
		alert('Your browser cannot handle this script');
	}
}

function parseDataTableText(xmlText)
{
	return parseDataTableXML(loadXML(xmlText));
}

function parseDataTableXML(doc)
{
	var myTable;
	var i=1;
	var rowList = doc.getElementsByTagName("row");
	myTable = new Array();
	for (var j=0;j<rowList.length;j++) 
		{
			myTable[j] = new Object();
			var o=rowList[j];
			var t;
			for (k=0;k<o.childNodes.length;k++) 
				{				
				if (o.childNodes[k].childNodes[0] == null) 
					eval('myTable[j].' + o.childNodes[k].nodeName+'=\'\';');
				else
					eval('myTable[j].' + o.childNodes[k].nodeName+'=o.childNodes[k].childNodes[0].nodeValue;');
				
				}
		}
		
		return myTable;
}