/*********************************************************************************************************************
 *	c_MyDate class constructor, receives a date to manage or creates it from the current date
 */
function c_MyDate( date ) {

	this.month_days		= new Array(null,31,28,31,30,31,30,31,31,30,31,30,31);
	this.month_names	= new Array(null,'Enero','Febrero','Marzo','Abril','Mayo','Junio','Julio','Agosto','Septiembre','Octubre','Noviembre','Diciembre');

	this.day			= null;
	this.month			= null;
	this.year			= null;

	this.loadDateComponents(date);
	this.setLeapYear();
}


/**
 *	Returns an identic date object
 */
c_MyDate.prototype.clone = function () {

	var date_string	= (this.day<10 ? '0' + this.day : this.day) + '/' + (this.month<10 ? '0' + this.month : this.month) + '/' + this.year;
	return new c_MyDate(date_string);
}


/**
 *	Lite format checking to get year and detect leap years. Format: (dd/mm/aaaa)
 */
c_MyDate.prototype.liteCheckDate = function ( date ) {

	if (date=='')
		return false;
	if (date.length!=10)
		return false;

	var day		= parseInt(date.charAt(0) + date.charAt(1), 10);
	var month	= parseInt(date.charAt(3) + date.charAt(4), 10);

	if (day<1 || day>31)
		return false;

	if (month<1 || month>12)
		return false;

//	if (day>this.month_days[month])
//		return false;

	if (isNatural(date.charAt(0)) && isNatural(date.charAt(1)) && date.charAt(2)=='/' && isNatural(date.charAt(3)) && isNatural(date.charAt(4)) && date.charAt(5)=='/' && isNatural(date.charAt(6)) && isNatural(date.charAt(7)) && isNatural(date.charAt(8)) && isNatural(date.charAt(9)))
		return true;
}


/**
 *	Checks for the correct format of a passed date (dd/mm/yyyy)
 */
c_MyDate.prototype.checkDate = function ( date ) {

	if (date=='')
		return false;
	if (date.length!=10)
		return false;

	var day		= parseInt(date.charAt(0) + date.charAt(1), 10);
	var month	= parseInt(date.charAt(3) + date.charAt(4), 10);

	if (day<1 || day>31)
		return false;

	if (month<1 || month>12)
		return false;

	if (day>this.month_days[month])
		return false;

	if (isNatural(date.charAt(0)) && isNatural(date.charAt(1)) && date.charAt(2)=='/' && isNatural(date.charAt(3)) && isNatural(date.charAt(4)) && date.charAt(5)=='/' && isNatural(date.charAt(6)) && isNatural(date.charAt(7)) && isNatural(date.charAt(8)) && isNatural(date.charAt(9)))
		return true;
}


/**
 *	Splits the passed formatted date (dd/mm/yyyy) in its atomic components
 */
c_MyDate.prototype.splitDate = function ( date ) {

	var aux_date			= new Array();
		aux_date['day']		= parseInt(date.charAt(0) + date.charAt(1), 10);
		aux_date['month']	= parseInt(date.charAt(3) + date.charAt(4), 10);// - 1;
		aux_date['year']	= parseInt(date.charAt(6) + date.charAt(7) + date.charAt(8) + date.charAt(9), 10);

	return aux_date;
}


/**
 *	Checks the passed date and loads its components into the calendar class varibles
 */
c_MyDate.prototype.loadDateComponents = function ( date ) {

	if (date && this.liteCheckDate(date)) {
		var aux_date		= this.splitDate(date);
		this.day			= aux_date['day'];
		this.month			= aux_date['month'];
		this.year			= aux_date['year'];
	} else if (window.current_date && this.liteCheckDate(window.current_date)) {
		var aux_date		= this.splitDate(window.current_date);
		this.day			= aux_date['day'];
		this.month			= aux_date['month'];
		this.year			= aux_date['year'];
	} else {
		var current_date	= new Date();
		this.day			= current_date.getDate();
		this.month			= current_date.getMonth() + 1;
		this.year			= current_date.getFullYear();
	}
}


/**
 *	Returns the name of the object month
 */
c_MyDate.prototype.getMonthName = function () {

	return this.month_names[this.month];
}


/**
 *	Returns the number of the object year
 */
c_MyDate.prototype.getYear = function () {

	return this.year;
}


/**
 *	Returns true if object date is smaller than passed date
 */
c_MyDate.prototype.smaller = function ( date ) {

	if (date && this.checkDate(date)) {
		var aux_date		= this.splitDate(date);
		var passed_date		= '' + aux_date['year'] + (aux_date['month']<10 ? '0' + aux_date['month'] : aux_date['month']) + (aux_date['day']<10 ? '0' + aux_date['day'] : aux_date['day']);
		var object_date		= '' + this.year + (this.month<10 ? '0' + this.month : this.month) + (this.day<10 ? '0' + this.day : this.day);

		return (object_date<passed_date);
	}
	else
		return false;
}


/**
 *	Returns true if object date is smaller or equals than passed date
 */
c_MyDate.prototype.smallerOrEquals = function ( date ) {

	if (this.smaller(date) || this.equals(date)) {
		return true;
	} else {
		return false;
	}
}


/**
 *	Returns true if object date is bigger than passed date
 */
c_MyDate.prototype.equals = function ( date ) {

	if (date && this.checkDate(date)) {
		var aux_date		= this.splitDate(date);
		var passed_date		= '' + aux_date['year'] + (aux_date['month']<10 ? '0' + aux_date['month'] : aux_date['month']) + (aux_date['day']<10 ? '0' + aux_date['day'] : aux_date['day']);
		var object_date		= '' + this.year + (this.month<10 ? '0' + this.month : this.month) + (this.day<10 ? '0' + this.day : this.day);

		return (object_date==passed_date);
	}
	else
		return false;
}


/**
 *	Returns true if object date is bigger than passed date
 */
c_MyDate.prototype.bigger = function ( date ) {

	if (date && this.checkDate(date)) {
		var aux_date		= this.splitDate(date);
		var passed_date		= '' + aux_date['year'] + (aux_date['month']<10 ? '0' + aux_date['month'] : aux_date['month']) + (aux_date['day']<10 ? '0' + aux_date['day'] : aux_date['day']);
		var object_date		= '' + this.year + (this.month<10 ? '0' + this.month : this.month) + (this.day<10 ? '0' + this.day : this.day);

		return (object_date>passed_date);
	}
	else
		return false;
}


/**
 *	Returns true if object date is bigger or equals than passed date
 */
c_MyDate.prototype.biggerOrEquals = function ( date ) {

	if (this.bigger(date) || this.equals(date)) {
		return true;
	} else {
		return false;
	}
}


/**
 *	Returns true if object year is a leap-year
 */
c_MyDate.prototype.setLeapYear = function () {

	var leap		= false;

	if ( (this.year%400)==0 ) {
		leap 	= true;
	} else if ( (this.year%4)==0 && (this.year%100)!=0 ) {
		leap 	= true;
	}

	if (leap) {
		this.month_days[2] = 29;
	} else {
		this.month_days[2] = 28;
	}
}


/**
 *	Returns an String representation of object date (dd/mm/aaaa)
 */
c_MyDate.prototype.toString = function () {

	var aux	= '';

	//day
	if (this.day<10)
		aux = '0' + this.day + '/';
	else
		aux = '' + this.day + '/';

	//month
	if (this.month<10)
		aux += '0' + this.month + '/';
	else
		aux += this.month + '/';

	//year
	aux += this.year;

	return aux;
}