/*
var _gCliTouSep='.';
var _gCliDecSep=',';
var _gSerTouSep=',';
var _gSerDecSep='.';
*/	
/*
var z = '5.555,030'
var d = '01/05/2003';
var dat = new Date('2004/05/03');
*/
/*
alert(d._toDate('/','DD/MM/YYYY'))
alert(dat._toString('/','DD/MM/YYYY'))
	
alert(z._toNumber());
z = '5555.30';
alert(z._toNumberFormat('###.###'));
*/

// this trim was suggested by Tobias Hinnerup
String.prototype.trim = function() {
  return(this.replace(/^\s+/,'').replace(/\s+$/,''));
}
	
String.prototype._toNumberCli = function() 
{
	var dbl;
	var strWor = this;	
	try
	{
		if (strWor==null) return null;
		strWor = strWor.replace(_gCliTouSep,'');
		strWor = strWor.replace(_gCliTouSep,'');
		strWor = strWor.replace(_gCliTouSep,'');
		strWor = strWor.replace(_gCliTouSep,'');
		strWor = strWor.replace(_gCliTouSep,'');
		strWor = strWor.replace(_gCliDecSep,'.');
		if (strWor=='' || strWor=='.') return null;
		dbl = parseFloat(strWor);
		if (dbl=='NaN') return null;
		return dbl;
	}	
	catch(e)
	{
		return null;
	}
}

String.prototype._toNumberSer = function() 
{
	var dbl;
	var strWor = this;	
	try
	{
		if (strWor==null) return null;
		/*strWor = strWor.replace(_gSerTouSep,'');
		strWor = strWor.replace(_gSerTouSep,'');
		strWor = strWor.replace(_gSerTouSep,'');
		strWor = strWor.replace(_gSerTouSep,'');
		strWor = strWor.replace(_gSerTouSep,'');*/
		strWor = strWor.replace(',','.');
		if (strWor=='' || strWor=='.') return null;
		dbl = parseFloat(strWor);
		if (dbl=='NaN') return null;
		return dbl;
	}	
	catch(e)
	{
		return null;
	}
}

String.prototype._toNumberFormat = function(_mask) 
{
	var argvalue = this;
	var format = _mask;
	var strDot = _gCliDecSep;
	try{
		var numOfDecimal = 0;
		var blnMin = false;

		if (argvalue.indexOf("-") == 0) {
			blnMin = true;
			argvalue = argvalue.substring(1, argvalue.length);
		}
		if (format.indexOf(strDot) != -1) {
			numOfDecimal = format.substring(format.indexOf(strDot) + 1, format.length).length;
		}
		if (argvalue=='')argvalue=0;
		argvalue = this._formatDecimal(argvalue, true, numOfDecimal);
		argvalueBeforeDot = argvalue.substring(0, argvalue.indexOf("."));
		if (format.indexOf(strDot)>-1)
		{
			retValue = strDot + argvalue.substring(argvalue.indexOf(".") + 1, argvalue.length);	
			strBeforeDot = format.substring(0, format.indexOf(strDot));
		}
		else 
		{
			retValue = argvalue.substring(argvalue.indexOf(".") + 1, argvalue.length);	
			strBeforeDot = format;	
		}
		for (var n = strBeforeDot.length - 1; n >= 0; n--) {
			oneformatchar = strBeforeDot.substring(n, n + 1);
			if (oneformatchar == "#") {
				if (argvalueBeforeDot.length > 0) {
					argvalueonechar = argvalueBeforeDot.substring(argvalueBeforeDot.length - 1, argvalueBeforeDot.length);
					retValue = argvalueonechar + retValue;
					argvalueBeforeDot = argvalueBeforeDot.substring(0, argvalueBeforeDot.length - 1);
				}
			}else {
				if (argvalueBeforeDot.length > 0 || n == 0)
					retValue = oneformatchar + retValue;
				}
			}
		if (blnMin)
		{
			retValue = '-' + retValue;
		}
		return retValue;
	}catch(e){return '';}	
}
	
String.prototype._formatDecimal = function(argvalue, addzero, decimaln) 
{
	try{
		var numOfDecimal = (decimaln == null) ? 2 : decimaln;
		var number = 1;
		number = Math.pow(10, numOfDecimal);
		argvalue = Math.round(parseFloat(argvalue) * number) / number;
		// If you're using IE3.x, you will get error with the following line.
		// argvalue = argvalue.toString();
		// It works fine in IE4.
		argvalue = "" + argvalue;
		if (argvalue.indexOf(".") == 0)
		  argvalue = "0" + argvalue;
		if (addzero == true) {
		  if (argvalue.indexOf(".") == -1)
		    argvalue = argvalue + ".";
		  while ((argvalue.indexOf(".") + 1) > (argvalue.length - numOfDecimal))
		    argvalue = argvalue + "0";
		}
		return argvalue;
	}catch(e){return '';}	
}
	
String.prototype._toDate = function(Sep,Typ)
{
	try{
		var CodDat = this;
		if (CodDat==null){
			return null;	
		}
		CodDat = CodDat.trim();
		var One = '';
		var Due = '';
		var Tre = '';
		var strDay = '';
		var strMon = '';
		var strYea = '';
		var arrDat = Typ.split(Sep);
		//if (CodDat.length == 6 || CodDat.length == 8 || CodDat.length == 10) {
			if (CodDat.split(Sep).length==3)
			{
				One = CodDat.split(Sep)[0];
				Due = CodDat.split(Sep)[1];
				Tre = CodDat.split(Sep)[2];
			}
			else
			{
				if (CodDat.split(Sep).length==0)
				{
					//we have 010101
					if (CodDat.length == 6 && CodDat.indexOf(Sep) == -1){
						One = CodDat.substr(0,2);
						Due = CodDat.substr(2,2);
						Tre = CodDat.substr(4,2);
					}
					//we have 01012001
					if (CodDat.length == 8 && CodDat.indexOf(Sep) == -1){
						One = CodDat.substr(0,2);
						Due = CodDat.substr(2,2);
						Tre = CodDat.substr(4,4);
					}
				}
			}
			/*
			//We have 01/01/01
			if (CodDat.length == 8 && CodDat.indexOf(Sep) == 2){
				One = CodDat.substr(0,2);
				Due = CodDat.substr(3,2);
				Tre = CodDat.substr(6,2);
			}
			
			//We have 01/01/2001
			if (CodDat.length == 10 && CodDat.indexOf(Sep) == 2){
				One = CodDat.substr(0,2);
				Due = CodDat.substr(3,2);
				Tre = CodDat.substr(6,4);
			}*/
			
			//test if user fill date in some good format
			if (One != '' && Due != '' && Tre != '') {
				//test what is first
				if (arrDat[0].indexOf('D') != -1) {strDay = One}	
				if (arrDat[0].indexOf('M') != -1) {strMon = One}
				if (arrDat[0].indexOf('Y') != -1) {strYea = One}
				//test what is second
				if (arrDat[1].indexOf('D') != -1) {strDay = Due}	
				if (arrDat[1].indexOf('M') != -1) {strMon = Due}
				if (arrDat[1].indexOf('Y') != -1) {strYea = Due}
				//test what is third
				if (arrDat[2].indexOf('D') != -1) {strDay = Tre}	
				if (arrDat[2].indexOf('M') != -1) {strMon = Tre}
				if (arrDat[2].indexOf('Y') != -1) {strYea = Tre}
				if (strYea.length == 2){
					if (strYea.substr(0,1) == '9'){
						strYea = '19' + strYea
					}else{
						strYea = '20' + strYea
					}
				}
				var DatDat = strMon + '/' + strDay + '/' + strYea
				var k = this._isValidDate(DatDat);
				if (k==0) {
					/*this.year=strYea; 
					this.month=strMon; 
					this.day=strDay;
					this.iyear=parseInt(strYea); 
					this.imonth=parseInt(strMon); 
					this.iday=parseInt(strDay);*/
					return new Date(strYea + '/' + strMon + '/' + strDay);
				} //{alert('OK');}
				if (k==1)return null;
				if (k==2)return null;
			}else{
				return null;
			}
		/*}else{
			return 'null';
		}*/
	}catch(e){return null;}	
}
	
String.prototype._isValidDate = function(date2check)
{
	try{
		// dgk-10/07/00
		// determines if the date string passed represents a valid date.
		// returns 0 if the date is valid
		// returns 1 if the date is not in the format of mm/dd/ccyy, mm-dd-ccyy, or mmddccyy
		//      m/d/ccyy & m-d-ccyy are also acceptable
		// returns 2 if the date is not a legal date (i.e. 02/30/1999)
		var retval = 0
		var aMMDDCCYY
		var dtest
		// use a regular expression pattern match to determine if the date format is valid
		if (/^(\d\d?-\d\d?-\d{4})|(\d\d?\/\d\d?\/\d{4})|(\d{8})$/.test(date2check)){
			dtest = new Date(date2check);
			if (/\//.test(date2check)){
			    aMMDDCCYY = date2check.split("/");
			}else{
				if (/-/.test(date2check)){
					aMMDDCCYY = date2check.split("-");
				}else{
					aMMDDCCYY = Array(date2check.substr(0,2), date2check.substr(2,2), date2check.substr(4,4))
					dtest = new Date(aMMDDCCYY[0] + "/" + aMMDDCCYY[1] + "/" + aMMDDCCYY[2]);
				}
			}
			if (dtest.getMonth() + 1 != aMMDDCCYY[0] || dtest.getDate() != aMMDDCCYY[1] || dtest.getFullYear() != aMMDDCCYY[2]){
				retval = 2
			}
		}else{
			retval = 1
		}
		return retval
	}catch(e){return retval;}
}
	
Date.prototype._toString = function(Sep,Typ)
{
	try 
	{
		var CodDat = this;
		if (CodDat==null){
			return '';	
		}
		var One = '';
		var Due = '';
		var Tre = '';
		var arrDat = Typ.split(Sep);
		One = CodDat.getDate();
		Due = CodDat.getMonth();
		Tre = CodDat.getYear();
		//test if user fill date in some good format
		if (One >= 0 && Due >= 0 && Tre >= 0) {
			//test what is first
			if (arrDat[0].indexOf('D') != -1) {One = CodDat.getDate();}	
			if (arrDat[0].indexOf('M') != -1) {One = CodDat.getMonth()+1;}	
			if (arrDat[0].indexOf('Y') != -1) {One = CodDat.getYear();if(CodDat.getYear() < 200) One+=1900;}	
			//test what is second
			if (arrDat[1].indexOf('D') != -1) {Due = CodDat.getDate();}	
			if (arrDat[1].indexOf('M') != -1) {Due = CodDat.getMonth()+1;}
			if (arrDat[1].indexOf('Y') != -1) {Due = CodDat.getYear();if(CodDat.getYear() < 200) Due+=1900;}
			//test what is third
			if (arrDat[2].indexOf('D') != -1) {Tre = CodDat.getDate();}	
			if (arrDat[2].indexOf('M') != -1) {Tre = CodDat.getMonth()+1;}
			if (arrDat[2].indexOf('Y') != -1) {Tre = CodDat.getYear();if(CodDat.getYear() < 200) Tre+=1900;}
			if (One<10)One='0'+One;
			if (Due<10)Due='0'+Due;
			if (Tre<10)Tre='0'+Tre;
			return One + Sep + Due + Sep + Tre;
		}
		else
		{
			return '';	
		}	
	}catch(e){return '';}
}
String.prototype._setTime = function()
{
	var val = this;
	if (val == '') return '';
	var hh = Math.floor(val/60);
	var mm = val - (hh * 60);
	if (hh<10)hh='0'+hh;
	if (mm<10)mm='0'+mm;
	return hh + ':' + mm;
}
String.prototype._getTime = function()
{
	var val = this;
	if (val == '') return '';
	var Idx = val.indexOf(':');
	if (Idx == -1) 
	{
		return parseFloat(val) * 60;
	}
    val = val.substr(0, Idx) * 60 + val.substr(Idx + 1, 2) * 1;
    return val;
}
String.prototype._checkTime = function() 
{
	var val = this;
	var idx = val.indexOf(':');
	if (!val || val == '') {return ''; }
	if (idx == -1) {
	  if (val <= 24) { val = val + ':00'; } else { return ''; }
	} else {
	  var hh = val.split(':')[0], mm = val.split(':')[1];
	  if (!hh) return '';
	  if (hh > 24) return '';
	  if (hh.length == 1)hh = '0' + hh;
	  if (!mm) mm = '00';
	  if (mm > 60) return '';
	  if (mm.length == 1) mm = mm + '0';
	  val = hh +':'+ mm;
	}
	return val;
}
//**************************************
// Name: Number of days between dates - CORRECTLY!
// Description:There have been tons of date calculation functions that i've tried and used, but they are never 100%. Most of them use the getTime() function which can throw it off by a day. Here is a 100% accurate function.
//**************************************
function daysInMonth(MONTH,YEAR) {
	var m = [31,28,31,30,31,30,31,31,30,31,30,31];
	if (MONTH != 2) return m[MONTH - 1];
	if (YEAR%4 != 0) return m[1];
	if (YEAR%100 == 0 && YEAR%400 != 0) return m[1];
	
	return m[1] + 1;
} 
function GetDaysInYear(YEAR) {
	var DAYS = 0;
	for( var i = 1; i <= 12; i++ ) {
		DAYS += daysInMonth(i,YEAR);
	}
	return DAYS;
}
function CalcDaysBetweenDates(datStart,datFinish) {
	if (datStart==null){
		return 0;	
	}
	if (datFinish==null){
		return 0;	
	}
	var YEAR1 = datStart.getYear();
	var MON1 = datStart.getMonth()+1;
	var DAY1 = datStart.getDate();
	var YEAR2 = datFinish.getYear();
	var MON2 = datFinish.getMonth()+1;
	var DAY2 = datFinish.getDate();
	
	var MONTHS = (MON2 - MON1) + ((YEAR2 - YEAR1) * 12);
	var YEAR = YEAR1;
	var MONTH = MON1;
	var DAYS = DAY2 - DAY1;


	for(var i = 0; i < MONTHS; i++ ) {
		DAYS += daysInMonth(MONTH,YEAR);
		MONTH++;
		if( MONTH > 12 ) {
			MONTH = 1;
			YEAR++;
		}
	}
	return DAYS;
}