/*
The SoftAd Group
Copyright (c) 2000-2004 The SoftAd Group, Inc.  All rights reserved
*/
//To use this object the following files must be also included.
// CNDate.js

// -------------------------------------------------------------------------------
// File name        : CNDate.js
// -------------------------------------------------------------------------------
// Author           : Ken Wimberley
// Created on       : June 23, 2003
// 
// This file contains the the CNDate object that holds all the generic methods
// for Dates that are used in ChannelNet.  When this file is to be extended, create
// a new file with and add a suffix with the Context i.e. CNDate_Build.js  This way 
// it will be clear that the Build object will call the CNDate object for its basic 
// function and take advantage of any code that was writen generically.
// -------------------------------------------------------------------------------
// Last Updated     : 
// Updated by       : 
// -------------------------------------------------------------------------------
// Copyright (c) 2004 The SoftAd Group, Inc.


// -------------------------------------------------------------------------------
// Function         : CNDate()
// -------------------------------------------------------------------------------
// Author           : Ken Wimberley
// Created on       : June 23, 2003
// 
// This function creates the CNDate 
// -------------------------------------------------------------------------------
// Last Updated     : 
// Updated by       : 
// -------------------------------------------------------------------------------

/*Begin Class*/
function CNDate() {
	//	********************** Public properties *******************
	//	************************************************************
	this.date		= new Date();
	this.value		= this.date.toDateString();
	this.year		= "";
	this.month		= "";
	this.day		= "";
	this.weekday	= "";
	this.hour		= "";
	this.min		= "";
	this.sec		= "";
	this.msec		= "";
	
	//	********************** Public methods **********************
	//	************************************************************
	this.create = create;
	this.createFromDate = createFromDate;
	this.createFromFormat = createFromFormat;
	this.compare = compare;
	this.isBefore = isBefore;
	this.compareDates = compareDates;
	this.getSQLDate = getSQLDate;
	this.getFormattedDate = getFormattedDate;
	this.getDateFromFormat = getDateFromFormat;
	this.isDate = isDate;
	this.formatDate = formatDate;
	this.isValidDate = isValidDate;
	
	//	************************** Globals *************************
	//	************************************************************
	var bReturn;
	var iReturn;
	var sReturn = "";
	var aryMonthNames = new Array('January','February','March','April','May','June','July','August','September','October','November','December','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
	
	// -------------------------------------------------------------------------------
	// Function         : create(yyyy, mm, dd, hh, min, ss, mm)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : Wednesday, June 23, 2003
	// 
	// This function returns a new CNDate object.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME		REQUIRED	TYPE        DESCRIPTION
	// -------------------------------------------------------------------------------	
	// yyyy		Optional	string	The full year, for example, 1976 (and not 76).
	// mm		Optional	string	The month as an integer between 0 and 11 (January to December).
	// dd		Optional	string	The date as an integer between 1 and 31. 
	// hh		Optional	string	Must be supplied if minutes is supplied. An integer from 0 to 23 (midnight to 11pm) that specifies the hour. 
	// min		Optional	string	Must be supplied if seconds is supplied. An integer from 0 to 59 that specifies the minutes. 
	// sec		Optional	string	Must be supplied if milliseconds is supplied. An integer from 0 to 59 that specifies the seconds. 
	// ms		Optional	string	An integer from 0 to 999 that specifies the milliseconds. 
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------	
	function create(yyyy, mm, dd, hh, min, ss, mm){
		var oNewCNDate = new CNDate();
		yyyy	? oNewCNDate.year = yyyy	: oNewCNDate.year = oNewCNDate.date.getFullYear();
		mm		? oNewCNDate.month = mm		: oNewCNDate.month = oNewCNDate.date.getMonth();
		dd		? oNewCNDate.day = dd		: oNewCNDate.day = oNewCNDate.date.getDate();
		hh		? oNewCNDate.hour = hh		: oNewCNDate.hour = oNewCNDate.date.getHours();
		min		? oNewCNDate.min = min		: oNewCNDate.min = oNewCNDate.date.getMinutes();
		ss		? oNewCNDate.sec = ss		: oNewCNDate.sec = oNewCNDate.date.getSeconds();
		mm		? oNewCNDate.msec = mm		: oNewCNDate.date.getMilliseconds();
		oNewCNDate.date = new Date(	oNewCNDate.year, 
								oNewCNDate.month,
								oNewCNDate.day,
								oNewCNDate.hour,
								oNewCNDate.min,
								oNewCNDate.sec, 
								oNewCNDate.msec );
		this.value = oNewCNDate.date.toDateString();
		return oNewCNDate;		
	}
	
	// -------------------------------------------------------------------------------
	// Function         : createFromDate(oDate)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : Wednesday, July 1, 2003
	// 
	// This function returns a new CNDate object.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME		REQUIRED	TYPE        DESCRIPTION
	// -------------------------------------------------------------------------------	
	// oDate	Required	Date object	JavaScript Date object
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------	
	function createFromDate(oDate){
		var oNewCNDate		= new CNDate();		
		oNewCNDate.year		= oDate.getFullYear();
		oNewCNDate.month	= oDate.getMonth();
		oNewCNDate.weekday	= oDate.getDay();
		oNewCNDate.day		= oDate.getDate();
		oNewCNDate.hour		= oDate.getHours();
		oNewCNDate.min		= oDate.getMinutes();
		oNewCNDate.sec		= oDate.getSeconds();
		oNewCNDate.msec		= oDate.getMilliseconds();
		oNewCNDate.date		= oDate;
		oNewCNDate.value	= oNewCNDate.date.toDateString();
		return oNewCNDate;		
	}
	
	// -------------------------------------------------------------------------------
	// Function         : createFromFormat(sDate, sFormat)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : Wednesday, July 1, 2003
	// 
	// This function returns a new CNDate object.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME		REQUIRED	TYPE        DESCRIPTION
	// -------------------------------------------------------------------------------	
	// sDate	Required	string		A string representing a date in the format of sFormat
	// sFormat	Required	string		The date format of the sDate string
	// -------------------------------------------------------------------------------
	// Examples:
	// oCNDate.createFromFormat(sSunrise, 'yyyy-mm-dd');	Creates a CNDate object when sSunrise
	//														is formatted as 'yyyy-mm-dd'
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------	
	function createFromFormat(sDate, sFormat){
		var oDate = this.getDateFromFormat(sDate, sFormat)
		return this.createFromDate(oDate);
	}
	
	// -------------------------------------------------------------------------------
	// Function         : compare(_oDateCompare)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : Wednesday, July 1, 2003
	// 
	// This can either compare a CNDate object to another CNDate object or
	// a CNDate object to a JavaScript Date object
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME				REQUIRED	TYPE		DESCRIPTION
	// -------------------------------------------------------------------------------	
	// _oDateCompare	Required	object		CNDate object or JavaScript Date object.
	// -------------------------------------------------------------------------------
	// Examples:
	// oSunrise.compare(oSunset);	Returns:	-1 when oSunrise is before oSunset
	//											0 when oSunrise equals oSunset
	//											1 when oSunrise is after oSunset
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------		
	function compare(_oDateCompare){
		iReturn = 0;
		if(_oDateCompare.date){
			if(this.date < _oDateCompare.date){
				iReturn = -1;
			}		
			if(this.date > _oDateCompare.date){
				iReturn = 1;
			}
		} else {
			if(this.date < _oDateCompare){
				iReturn = -1;
			}		
			if(this.date > _oDateCompare){
				iReturn = 1;
			}
		}
		return iReturn;
	}
	
	// -------------------------------------------------------------------------------
	// Function         : isBefore(_oDateCompare)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : Wednesday, July 1, 2003
	// 
	// This can either compare a CNDate object to another CNDate object or
	// a CNDate object to a simple Date object and tests for whether one Date object
	// is before another.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME				REQUIRED	TYPE		DESCRIPTION
	// -------------------------------------------------------------------------------	
	// _oDateCompare	Required	object		CNDate object or JavaScript Date object.
	// -------------------------------------------------------------------------------
	// Examples:
	// oSunrise.isBefore(oSunset);	Returns:	true when oSunrise is before oSunset
	//											false when oSunrise is after or equal to oSunset
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------
	function isBefore(_oDateCompare){
		bReturn = false;
		if (this.compare(_oDateCompare) == -1){
			bReturn = true;
		}
		return bReturn;
	}
	
	// -------------------------------------------------------------------------------
	// Function         : compareDates(_oDateCompareA, _oDateCompareB)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : Wednesday, July 1, 2003
	// 
	// This can either compare a CNDate object to another CNDate object or
	// a CNDate object to a simple Date object or a JS Date object to another JS
	// Date object and tests for whether one Date object is before, after or equal to another.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME				REQUIRED	TYPE		DESCRIPTION
	// -------------------------------------------------------------------------------	
	// _oDateCompareA	Required	object		CNDate object or JavaScript Date object.
	// _oDateCompareB	Required	object		CNDate object or JavaScript Date object.
	// -------------------------------------------------------------------------------
	// Examples:
	// oCNDate.compareDates(oSunrise, oSunset);	Returns:	-1 when oSunrise is before oSunset
	//														0 when oSunrise equals oSunset
	//														1 when oSunrise is after oSunset
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------	
	function compareDates(_oDateCompareA, _oDateCompareB){
		iReturn = 0;
		if(_oDateCompareA.date){
			if(_oDateCompareB.date){
				if(_oDateCompareA.date < _oDateCompareB.date){
					iReturn = -1;
				}		
				if(_oDateCompareA.date > _oDateCompareB.date){
					iReturn = 1;
				}
			} else {
				if(_oDateCompareA.date < _oDateCompareB){
					iReturn = -1;
				}		
				if(_oDateCompareA.date > _oDateCompareB){
					iReturn = 1;
				}
			}
		} else {
			if(_oDateCompareB.date){
				if(_oDateCompareA < _oDateCompareB.date){
					iReturn = -1;
				}		
				if(_oDateCompareA > _oDateCompareB.date){
					iReturn = 1;
				}
			} else {
				if(_oDateCompareA < _oDateCompareB){
					iReturn = -1;
				}		
				if(_oDateCompareA > _oDateCompareB){
					iReturn = 1;
				}
			}
		}
		return iReturn;
	}
		
	// -------------------------------------------------------------------------------
	// Function         : getSQLDate(_oDate)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : Wednesday, July 1, 2003
	// 
	// This function return a string to be used for SQL formatted as: "yyyy-mm-dd hh:mm:sec:ms"
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME		REQUIRED	TYPE		DESCRIPTION
	// -------------------------------------------------------------------------------	
	// _oDate	Optional	object		CNDate object or JavaScript Date object.
	// -------------------------------------------------------------------------------
	// Examples:
	// oCNDate.getSQLDate(oSunrise);
	// oSunrise.getSQLDate();			Returns:	string formatted as "yyyy-mm-dd hh:mm:sec:ms"
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------	
	function getSQLDate(_oDate){
		sReturn = "";
		if(_oDate){
			if(_oDate.date){
				sReturn += _oDate.year + "-";
				sReturn += (_oDate.month + 1) + "-";
				sReturn += _oDate.day + " ";
				sReturn += _oDate.hour + ":";
				sReturn += _oDate.min + ":";
				sReturn += _oDate.sec + ":";
				sReturn += _oDate.msec;
			} else {
				var oNewCNDate = this.createFromDate(_oDate);
				sReturn += oNewCNDate.year + "-";
				sReturn += (oNewCNDate.month + 1) + "-";
				sReturn += oNewCNDate.day + " ";
				sReturn += oNewCNDate.hour + ":";
				sReturn += oNewCNDate.min + ":";
				sReturn += oNewCNDate.sec + ":";
				sReturn += oNewCNDate.msec;
			}
		} else {
			sReturn += this.year + "-";
			sReturn += (this.month + 1) + "-";
			sReturn += this.day + " ";
			sReturn += this.hour + ":";
			sReturn += this.min + ":";
			sReturn += this.sec + ":";
			sReturn += this.msec;
		}
		return sReturn;		
	}	
		
	// -------------------------------------------------------------------------------
	// Function         : getFormattedDate(sFormat)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : Wednesday, July 1, 2003
	// 
	// This function return a date string in the supplied format.
	// NOTES ABOUT THE FORMAT:
	// "yyyy" for 4 digit year
	// "yy" for 2 digit year 
	// "mm" for 2 digit month
	// "mmm" for abreviated month
	// "MMM" for complete month month
	// "dd" for 2 digit day.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME		REQUIRED	TYPE		DESCRIPTION
	// -------------------------------------------------------------------------------	
	// sFormat	Required	String		String used to describe the desired date string output
	// -------------------------------------------------------------------------------
	// Examples: (when oSunrise = July 1, 2003)
	// oSunrise.getFormattedDate(yyyy-mm-dd)			Returns:	string 2003-07-23
	// oSunrise.getFormattedDate(yyyy-mmm-dd)			Returns:	string 2003-Jul-23
	// oSunrise.getFormattedDate(mmm/dd/yyyy)			Returns:	string Jul/23/2003
	// oSunrise.getFormattedDate(MMM dd, yyyy)			Returns:	string July 23, 2003
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------	
	function getFormattedDate(sFormat){
		sReturn = formatDate(this.date, sFormat);
		return sReturn;
	}
		
	// -------------------------------------------------------------------------------
	// Function         : formatDate(oDate,sFormat)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : Wednesday, July 1, 2003
	// 
	// This function return a date string in the supplied format.
	// NOTES ABOUT THE FORMAT:
	// "yyyy" for 4 digit year
	// "yy" for 2 digit year 
	// "mm" for 2 digit month
	// "mmm" for abreviated month
	// "MMM" for complete month month
	// "dd" for 2 digit day.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME		REQUIRED	TYPE		DESCRIPTION
	// -------------------------------------------------------------------------------	
	// oDate	Required	object		Date Object		
	// sFormat	Required	String		String used to describe the desired date string output
	// -------------------------------------------------------------------------------
	// Examples: (when oSunrise = July 1, 2003)
	// oCNDate.formatDate(oSunrise,yyyy-mm-dd)			Returns:	string 2003-07-23
	// oCNDate.formatDate(oSunrise,yyyy-mmm-dd)			Returns:	string 2003-Jul-23
	// oCNDate.formatDate(oSunrise,mmm/dd/yyyy)			Returns:	string Jul/23/2003
	// oCNDate.formatDate(oSunrise,MMM dd, yyyy)		Returns:	string July 23, 2003
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------	
	function formatDate(oDate,sFormat) {
		var iFormat = 0;
		var c = "";
		var token = "";
		var year = oDate.getFullYear()+"";
		var month = oDate.getMonth()+1;
		var d = oDate.getDate();
		var h = oDate.getHours();
		var m = oDate.getMinutes();
		var s = oDate.getSeconds();
		var yyyy,yy,mm,MM,MMM,dd;
		// Convert real date parts into formatted versions
		// Year
		if (year.length < 4) {
			year = year-0+1900;
		}
		year = "" + year;
		yyyy = year;
		yy = year.substring(2,4);		
		// Month
		if (month < 10) {
			mm = "0" + month;
		} else { 
			mm = month; 
		}
		MM = aryMonthNames[month-1+12];
		MMM = aryMonthNames[month-1];
		// Date
		if (d < 10) {
			dd = "0"+d;
		} else { 
			dd = d;
		}
		// Now put them all into an object!
		var value = new Object();
		value["yyyy"] = yyyy;
		value["yy"] = yy;
		value["y"] = year;
		value["MM"] = MM;
		value["mm"] = mm;
		value["MMM"] = MMM;
		value["dd"] = dd;
		value["d"] = d;

		while (iFormat < sFormat.length) {
			// Get next token from format string
			c = sFormat.charAt(iFormat);
			token = "";
			while ((sFormat.charAt(iFormat) == c) && (iFormat < sFormat.length)) {
				token += sFormat.charAt(iFormat);
				iFormat++;
			}
			if (value[token] != null) {
				sReturn = sReturn + value[token];
			} else {
				sReturn = sReturn + token;
			}
		}
		return sReturn;
	}
	
	// -------------------------------------------------------------------------------
	// Function         : getDateFromFormat(sDate, sFormat)
	// -------------------------------------------------------------------------------
	// Author           : Ken Wimberley
	// Created on       : Wednesday, July 1, 2003
	// 
	// This function returns a new JavaScript Date object.
	// To return a CNDate object, use createFromFormat() instead.
	//
	// NOTES ABOUT THE FORMAT:
	// "yyyy" for 4 digit year
	// "yy" for 2 digit year 
	// "mm" for 2 digit month
	// "mmm" for abreviated month
	// "MMM" for complete month month
	// "dd" for 2 digit day.
	// -------------------------------------------------------------------------------
	// Parameters:
	// NAME		REQUIRED	TYPE        DESCRIPTION
	// -------------------------------------------------------------------------------	
	// sDate	Required	string		A string representing a date in the format of sFormat
	// sFormat	Required	string		The date format of the sDate string
	// -------------------------------------------------------------------------------
	// Examples:
	// oCNDate.createFromFormat(sSunrise, 'yyyy-mm-dd');	Creates a CNDate object when sSunrise
	//														is formatted as 'yyyy-mm-dd'
	// -------------------------------------------------------------------------------
	// Last Updated     : 
	// Updated by       : 
	// -------------------------------------------------------------------------------		
	function getDateFromFormat(sDate, sFormat) {
		var iDate = 0;
		var iFormat = 0;
		var c = "";
		var token = "";
		var token2= "";
		var x,y;
		var year  = 0;
		var month = 0;
		var date  = 0;
		var bYearProvided = false;
		while (iFormat < sFormat.length) {
			// Get next token from format string
			c = sFormat.charAt(iFormat);
			token = "";
			
			while ((sFormat.charAt(iFormat) == c) && (iFormat < sFormat.length)) {
				token += sFormat.charAt(iFormat);
				iFormat++;
			}
			
			// Extract contents of value based on format token
			if (token=="yyyy" || token=="yy" || token=="y") {
				if (token=="yyyy") { x=4;y=4; }// 4-digit year
				if (token=="yy")   { x=2;y=2; }// 2-digit year
				if (token=="y")    { x=2;y=4; }// 2-or-4-digit year
				year = _getInt(sDate,iDate,x,y);
				bYearProvided = true;
				if (year == null) {
					return 0;
					//Default to current year 
				}		
				if (year.length != token.length){
					return 0;
				}

				iDate += year.length;
			} else if (token=="MMM") { // Month name
				month = 0;
				for (var i=0; i<aryMonthNames.length; i++) {
					var month_name = aryMonthNames[i];
					if (sDate.substring(iDate,iDate+month_name.length).toLowerCase() == month_name.toLowerCase()) {
						month = i+1;
						if (month > 12) { month -= 12; }
						iDate += month_name.length;
						break;
					}
				}
				
				if (month == 0) { return 0; }
				if ((month < 1) || (month > 12)) {
					return 0
				}
			} else if (token=="MM" || token=="M") {
				month = 0;
				for (var i=0; i<aryMonthNames.length; i++) {
					var month_name = aryMonthNames[i];
					if (sDate.substring(iDate,iDate+month_name.length).toLowerCase() == month_name.toLowerCase()) {
						month = i+1;
						if (month > 12) { month -= 12; }
						iDate += month_name.length;
						break;
					}
				}
				
				if (month == 0) { return 0; }
				if ((month < 1) || (month > 12)) {
					return 0
				}
			} else if (token=="mm" || token=="m") {
				x=token.length; y=2;
				month = _getInt(sDate,iDate,x,y);
				if (month == null) { return 0; }
				if ((month < 1) || (month > 12)) { return 0; }
				iDate += month.length;
			} else if (token=="dd" || token=="d") {
				x=token.length; y=2;
				date = _getInt(sDate,iDate,x,y);
				if (date == null) { return 0; }
				if ((date < 1) || (date > 31)) { return 0; }
				iDate += date.length;
			} else {
				if (sDate.substring(iDate,iDate+token.length) != token) {
					return 0;
				}
				else {
					iDate += token.length;
				}
			}
		}
		// If there are any trailing characters left in the value, it doesn't match
		if (iDate != sDate.length) {
			return 0;
		}
		// Is date valid for month?

		if (month == 2) {
			// Check for leap year
			if ( ( (year%4 == 0)&&(year%100 != 0) ) || (year%400 == 0) ) { // leap year
				if (date > 29){ return false; }
			}
			else {
				if (date > 28) { return false; }
			}
		}
		if ((month==4)||(month==6)||(month==9)||(month==11)) {
			if (date > 30) { return false; }
		}

		//JS dates uses 0 based months.
		month = month - 1;

		if (bYearProvided==false) {
			//Default to current
			var dCurrent = new Date();
			year = dCurrent.getFullYear();
		}

		var lYear = parseInt(year);
		if (lYear<=20) {
			year = 2000 + lYear;
		}
		else if (lYear >=21 && lYear<=99) {
			year = 1900 + lYear;	
		}

		var oDate = new Date(year,month,date,0,0,0);

		return oDate;
	}
	
	// -------------------------------------------------------------------------------
	// Function         : isValidDate(day, month, year)
	// -------------------------------------------------------------------------------
	// Returns nulliff the given parameters correspond to a valid date or an
	// appropriate error message if they don't.
	// -------------------------------------------------------------------------------
	// Parameters
	// -------------------------------------------------------------------------------
	// NAME			TYPE        DESCRIPTION
	// day          integer     The day of the month
	// month        integer     The month
	// year         integer     The year
	// -------------------------------------------------------------------------------
	// History
	// -------------------------------------------------------------------------------
	// DATE			NAME				DESCRIPTION
	// 03/24/2000   George Pollard		Created
	// 10/25/2000   George Pollard		Updated
	// 07/02/2003	Ken Wimberley		Updated

	function isValidDate(day, month, year) {
		sReturn = "";
		var len = 0;
		month = parseInt(month, 10);
		day = parseInt(day, 10);
		if ((month<1)||(month>12))
			sReturn += month + " is not a valid month.";
			return sReturn;
		
		// A leap year occurs every four years except for those multiples of 100 which are not also multiples of 400.
		// eg 1600, 2000 and 2400 are leap years but 1700, 1800, 1900 and 2100 etc are not
		var isLeap = ( ((year%4)==0) && ((year%100)!=0) ) || ((year%400)==0);
		if (month==1) len=31; // jan
		if ((month==2)&&!isLeap) len=28; // feb (non-leap year)
		if ((month==2)&&isLeap) len=29;  // feb (leap year)
		if (month==3) len=31; // mar
		if (month==4) len=30; // apr
		if (month==5) len=31; // may
		if (month==6) len=30; // jun
		if (month==7) len=31; // jul
		if (month==8) len=31; // aug
		if (month==9) len=30; // sep
		if (month==10) len=31; // oct
		if (month==11) len=30; // nov
		if (month==12) len=31; // dec
		
		if ((day<1)||(day>len)) {
			if ((day==29)&&(month==2)){
				sReturn += year + " is not a leap year.";
				return sReturn;
			}
			if ((day>0)&&(day<32)){
				sReturn += day + " is not a valid day for month " + month + ".";
				return sReturn;
			}
			sReturn += day + " is not a valid day of the month.";
			return sReturn;
		}
		
		return null;
	}
	
		
	function _getInt(str, i, minlength, maxlength) {		
		for (x=maxlength; x>=minlength; x--) {
			var token = str.substring(i,i+x);
			if (_isInteger(token)) { 
				return token;
			}
		}
		return null;
	}
	
	function isDate(val,sFormat) {
		bReturn = true;
		var date = getDateFromFormat(val,sFormat);
		if (date == 0) { 
			bReturn = false;
		}
		return bReturn;
	}	
	
	function _isInteger(val) {
		var digits = "1234567890";
		for (var i=0; i < val.length; i++) {
			if (digits.indexOf(val.charAt(i)) == -1) { 
				return false; 
			}
		}
		return true;
	}
	
}
/*End Class*/

var oCNDate = new CNDate();

