
reserveCalendar = function(msdate){
	this.darr = null;
	this.ddep = null;
	this.date = new Date(msdate);
	this.cell_darr = null;
	this.cell_ddep = null;
	this.price = 0;
	this.nights = 0;
	this.iarrive = 'arrive';
	this.ideparture = 'departure';
	this.inights = 'nights';
	this.iprice = 'price';
	
	function getSecondsDate(date, format){
		if(date){
			if(format == 'euro'){
				var dt = date.split('/');
				return Date.parse(parseInt(dt[1]) + '/' + parseInt(dt[0]) + '/' + parseInt(dt[2]));
			}else{
				var dt = date.split('-');
				return Date.parse(parseInt(dt[1]) + '/' + parseInt(dt[2]) + '/' + parseInt(dt[0]));
			}
		}else
			return '';
	}
	
	function getNormalDate(date, format){
		if(date){
			var dt = new Date(date);		
			if(format == 'euro')
				return (dt.getDate() + '/' + (dt.getMonth() + 1) + '/' + dt.getFullYear());			
			else			
				return (dt.getFullYear() + '-' + (dt.getMonth() + 1) + '-' + dt.getDate());
		}else{
			return '';
		}
	}	
	
	function getPrice(price){
		return (price != '?') ? parseInt(price) : NaN;
	}
	
	function getNights(arr, dep){
		var da = new Date(arr);
		var dd = new Date(dep);
		var oday = 1000*60*60*24
		return Math.ceil((dd.getTime() - da.getTime())/(oday));
	}	

	reserveCalendar.prototype.reset = function(){
		this.darr = null;
		this.ddep = null;
		this.cell_darr = null;
		this.cell_ddep = null;		
		this.price = 0;
		this.nights = 0;
		this.avail = false;
		var elements = $g('parrive');
		if(elements){
			this.iarrive = 'parrive';
			this.ideparture = 'pdeparture';
			this.inights = 'pnights';
			this.iprice = 'pprice';
		}else{
			this.iarrive = 'arrive';
			this.ideparture = 'departure';		
			this.inights = 'nights';
			this.iprice = 'price';
		}			
		elements = $g(this.iarrive, this.ideparture);
		if(elements[0]){
			this.darr = (elements[0].value) ? getSecondsDate(elements[0].value, 'euro') : null;
			this.ddep = (elements[1].value) ? getSecondsDate(elements[1].value, 'euro') : null;
		}		
	}
	
	reserveCalendar.prototype.selectRange = function(arrive, departure, prefx){	
		var darrive = getSecondsDate(arrive, 'euro');
		var ddeparture = getSecondsDate(departure, 'euro');
		if(!emptyPeriod(darrive, ddeparture, prefx)) return;

		var elements = $g(this.iarrive, this.ideparture, this.inights, this.iprice);		
		this.darr = darrive;
		this.ddep = ddeparture;
		var arr = new Date(darrive);
		var dep = new Date(ddeparture);
		var table = null;
		var total = 0, price = null, evaluate;
		if(arr.getMonth() == dep.getMonth()){
			table = document.getElementById(prefx + '_' + (arr.getMonth() + 1) + '_' + arr.getFullYear());
			var tbody = table.getElementsByTagName('tbody')[0];
			var trs = tbody.getElementsByTagName('td');
			var darr = arr.getDate();
			var ddep = dep.getDate();
			for(x = 0; x < trs.length; x++)
			{
				if(trs[x].childNodes[0].data == darr){ // arr
					trs[x].className = trs[x].className == 'gw' ? 'gc' : (trs[x].className == 'gg' ? 'gc' : (trs[x].className == 'rw' ? 'rc' : 'wc'));
					evaluate = trs[x].title.split(' ');
					if(!isNaN(total)){
						price = getPrice(evaluate[0]);
						total = isNaN(price) ? NaN : (total + price);
					}		
					this.cell_darr = trs[x];
				}else if(trs[x].childNodes[0].data == ddep){ // dep
					trs[x].className = trs[x].className == 'wg' ? 'cg' : (trs[x].className == 'gg' ? 'cg' : (trs[x].className == 'wr' ? 'cr' : 'cw'));
					this.cell_ddep = trs[x];					
				}else if(trs[x].childNodes[0].data > darr && trs[x].childNodes[0].data < ddep){
					evaluate = trs[x].title.split(' ');
					trs[x].className = 'cc';
					if(!isNaN(total)){
						price = getPrice(evaluate[0]);
						total = isNaN(price) ? NaN : (total + price);
					}
				}
			}
		}
		else
		{
			var months = (arr.getFullYear() != dep.getFullYear()) ? ((12 - arr.getMonth()) + dep.getMonth()) : Math.abs(arr.getMonth() - dep.getMonth());
			var dcmonth = new Date(darrive);
			var day = 0;
			for(x = 0; x <= months; x++){
				dcmonth.setDate(1);			
				dcmonth.setMonth(dcmonth.getMonth() + (x ? 1 : 0));
				table = document.getElementById(prefx + '_' + (dcmonth.getMonth() + 1) + '_' + dcmonth.getFullYear());
				var tbody = table.getElementsByTagName('tbody')[0];
				var trs = tbody.getElementsByTagName('td');
				var day;
				for(y = 0; y < trs.length; y++){
					day = parseInt(trs[y].childNodes[0].data);
					if(!isNaN(day)){
						dcmonth.setDate(day);
						if(dcmonth.getTime() == arr.getTime()){ // arr
							trs[y].className = trs[y].className == 'gw' ? 'gc' : (trs[y].className == 'gg' ? 'gc' : (trs[y].className == 'rw' ? 'rc' : 'wc'));
							evaluate = trs[y].title.split(' ');
							if(!isNaN(total)){
								price = getPrice(evaluate[0]);
								total = isNaN(price) ? NaN : (total + price);
							}
						}else if(dcmonth.getTime() == dep.getTime()){ //dep
							trs[y].className = trs[y].className == 'wg' ? 'cg' : (trs[y].className == 'gg' ? 'cg' : (trs[y].className == 'wr' ? 'cr' : 'cw'));
						}else if(dcmonth.getTime() > arr.getTime() && dcmonth.getTime() < dep.getTime()){
							evaluate = trs[y].title.split(' ');	
							trs[y].className = 'cc';
							if(!isNaN(total)){
								price = getPrice(evaluate[0]);
								total = isNaN(price) ? NaN : (total + price);
							}							
						}
					}
				}		
			}
		}
		
		elements[0].value = getNormalDate(this.darr, 'euro');
		elements[1].value = getNormalDate(this.ddep, 'euro');
		elements[2].value = getNights(this.darr, this.ddep);
		if(elements[3]) elements[3].value = isNaN(total) ? '?' : total + ' €';
	}
	
	reserveCalendar.prototype.selectDay = function(day, month, year, cell, prefx){
		var elements = $g(this.iarrive, this.ideparture, this.inights, this.iprice);
		var evdate = Date.parse(month + '/' + day + '/' + year);
		var res = null, evaluate;
		if(this.darr == evdate){ // unselect 			
			evaluate = cell.title.split(' ');
			if(evaluate.length == 3)
				cell.className = (cell.className == 'rc' ? 'rg' : (cell.className == 'gc' ? 'gg'  : (cell.className == 'cc' ? 'gg' : (cell.className == 'wc' ? 'wg' : 'ww'))));
			else
				cell.className = (cell.className == 'rc' ? 'rw' : (cell.className == 'gc' ? 'gw'  : 'ww'));
			this.cell_darr = null;
			if(this.ddep != null){ 
				this.nights = 1;			
				res = cleanRest(this.darr, this.ddep, prefx);
				if(!isNaN(res)){
					if(!isNaN(this.price)){
						this.price = Math.abs(this.price - res); 
						this.avail = true;
					}else
						this.avail = false;				
				}else
					this.avail = false;				
			}else{
				this.nights = 0;
				this.price = 0;				
			}
			this.darr = null;
		}else if(this.ddep == evdate){ // unselect
			evaluate = cell.title.split(' ');	
			if(evaluate.length == 3)
				cell.className = (cell.className == 'cr' ? 'gr' : (cell.className == 'cg' ? 'gg'  : (cell.className == 'cc' ? 'gg' : (cell.className == 'cw' ? 'gw' : 'ww'))));
			else
				cell.className = (cell.className == 'cr' ? 'wr' : (cell.className == 'cg' ? 'wg'  : 'ww'));			
			if(this.darr != null){
				this.nights = 1;
				res = cleanRest(this.darr, this.ddep, prefx);
				if(!isNaN(res)){
					if(!isNaN(this.price)){
						this.price = Math.abs(this.price - res); 
						this.avail = true;
					}else
						this.avail = false;				
				}else
					this.avail = false;				
			}else{
				this.nights = 0;
				this.price = 0;
			}
			this.ddep = null;

		}else if(this.darr == null){ // select
			if(cell.className == 'wr') return;
			if(this.ddep != null)
			{			
				if(this.ddep < evdate){
					if(!emptyPeriod(evdate, this.ddep, prefx)) return;
			  		this.darr = this.ddep;
			  		this.ddep = evdate;
			  		cell.className = (cell.className == 'wr' ? 'cr' : (cell.className == 'wg' ? 'cg'  : (cell.className == 'gg' ? 'cg' : 'cw'))); // dep
			  		this.cell_ddep.className = (cell.className == 'rw' ? 'rc' : (cell.className == 'gw' ? 'gc'  : (cell.className == 'gg' ? 'gc' : 'wc'))); // arr
			  		this.cell_darr = this.cell_ddep;
			  		this.cell_ddep = cell;			  		
			  	}else{
					if(!emptyPeriod(this.darr, evdate, prefx)) return;			
					cell.className = (cell.className == 'rw' ? 'rc' : (cell.className == 'gw' ? 'gc'  : (cell.className == 'gg' ? 'gc'  : 'wc')));
					this.cell_darr = cell;
					this.darr = evdate;			  	
			  	}
			  	this.nights = getNights(this.darr, this.ddep, prefx);
				res = fullRest(this.darr, this.ddep, prefx);
				if(!isNaN(res)){
					if(!isNaN(this.price)){
						this.price = this.price + res;
						this.avail = true;
					}else
						this.avail = false;						
				}else
					this.avail = false; 
				
			}else{
				cell.className = (cell.className == 'rw' ? 'rc' : (cell.className == 'gw' ? 'gc'  : (cell.className == 'gg' ? 'gc'  : 'wc')));
				this.cell_darr = cell;
				this.darr = evdate;			
				this.nights = 1;
				evaluate = cell.title.split(' ');
				this.price = getPrice(evaluate[0]);
			}			

		}else if(this.ddep == null){
			if(evdate < this.darr){
				if(!emptyPeriod(evdate, this.darr, prefx)) return;			
				this.ddep = this.darr;
				this.darr = evdate;
			  	cell.className = (cell.className == 'rw' ? 'rc' : (cell.className == 'gw' ? 'gc'  : (cell.className == 'gg' ? 'gc' : 'wc'))); // arr
			  	this.cell_darr.className = (cell.className == 'wr' ? 'cr' : (cell.className == 'wg' ? 'cg'  : (cell.className == 'gg' ? 'cg' : 'cw'))); // dep
				this.cell_ddep  = this.cell_darr;
				this.cell_darr = cell;
			}else{		
				if(!emptyPeriod(this.darr, evdate, prefx)) return;
				cell.className = (cell.className == 'wr' ? 'cr' : (cell.className == 'wg' ? 'cg'  : (cell.className == 'gg' ? 'cg'  : 'cw')));
				this.cell_ddep = cell;
				this.ddep = evdate;
			}
			this.nights = getNights(this.darr, this.ddep);
			res = fullRest(this.darr, this.ddep, prefx);
			if(!isNaN(res)){
				if(!isNaN(this.price)){
					this.price = this.price + res;
					this.avail = true;
				}else
					this.avail = false;
			}else
				this.avail = false; 

		}
		else
			alert(lang == 'en' ? 'There is already a selected period, unselect first the current period to change the dates' : 'Ya hay un período seleccionado, deseleccione el período actual para cambiar las fechas');
		
		elements[0].value = getNormalDate(this.darr, 'euro');
		elements[1].value = getNormalDate(this.ddep, 'euro');
		elements[2].value = this.nights;
		if(elements[3]) elements[3].value = this.avail ? this.price + ' €' : '?';
	}
	
	emptyPeriod = function(darrive, ddeparture, prefx){
		var arr = new Date(darrive);
		var dep = new Date(ddeparture);
		var table = null;	
		if(arr.getMonth() == dep.getMonth()){
			table = document.getElementById(prefx + '_' + (arr.getMonth() + 1) + '_' + arr.getFullYear());
			var tbody = table.getElementsByTagName('tbody')[0];
			var trs = tbody.getElementsByTagName('td');
			var darr = arr.getDate();
			var ddep = dep.getDate();
			for(z = 0; z < trs.length; z++){		
				if(trs[z].childNodes[0].data > darr && trs[z].childNodes[0].data < ddep)
					if(trs[z].className == 'rr' || trs[z].className == 'rw') return false;
			}
		}
		else
		{
			var months = (arr.getFullYear() != dep.getFullYear()) ? ((12 - arr.getMonth()) + dep.getMonth()) : Math.abs(arr.getMonth() - dep.getMonth());
			var dcmonth = new Date(darrive);
			var day = 0;
			for(v = 0; v <= months; v++){
				dcmonth.setDate(1);			
				dcmonth.setMonth(dcmonth.getMonth() + (v ? 1 : 0));
				table = document.getElementById(prefx + '_' + (dcmonth.getMonth() + 1) + '_' + dcmonth.getFullYear());
				var tbody = table.getElementsByTagName('tbody')[0];
				var trs = tbody.getElementsByTagName('td');
				var day;
				for(y = 0; y < trs.length; y++){
					day = parseInt(trs[y].childNodes[0].data);
					if(!isNaN(day)){
						dcmonth.setDate(day);
						if(dcmonth.getTime() > arr.getTime() && dcmonth.getTime() < dep.getTime())
							if(trs[y].className == 'rr' || trs[y].className == 'rw') return false;
					}
				}		
			}
		}
		return true;	
	}
			
	fullRest = function(darrive, ddeparture, prefx){
		var arr = new Date(darrive);
		var dep = new Date(ddeparture);
		var table = null;
		var total = 0, price = null, evaluate;
		if(arr.getMonth() == dep.getMonth()){
			table = document.getElementById(prefx + '_' + (arr.getMonth() + 1) + '_' + arr.getFullYear());
			var tbody = table.getElementsByTagName('tbody')[0];
			var trs = tbody.getElementsByTagName('td');
			var darr = arr.getDate();
			var ddep = dep.getDate();
			for(x = 0; x < trs.length; x++){					
				if(trs[x].childNodes[0].data > darr && trs[x].childNodes[0].data < ddep){
					evaluate = trs[x].title.split(' ');	
					trs[x].className = 'cc';
					if(!isNaN(total)){
						price = getPrice(evaluate[0]);
						total = isNaN(price) ? NaN : (total + price);
					}
				}
			}
		}
		else
		{
			var months = (arr.getFullYear() != dep.getFullYear()) ? ((12 - arr.getMonth()) + dep.getMonth()) : Math.abs(arr.getMonth() - dep.getMonth());
			var dcmonth = new Date(darrive);
			var day = 0;
			for(x = 0; x <= months; x++){
				dcmonth.setDate(1);			
				dcmonth.setMonth(dcmonth.getMonth() + (x ? 1 : 0));
				table = document.getElementById(prefx + '_' + (dcmonth.getMonth() + 1) + '_' + dcmonth.getFullYear());
				var tbody = table.getElementsByTagName('tbody')[0];
				var trs = tbody.getElementsByTagName('td');
				var day;
				for(y = 0; y < trs.length; y++){
					day = parseInt(trs[y].childNodes[0].data);
					if(!isNaN(day)){
						dcmonth.setDate(day);
						if(dcmonth.getTime() > arr.getTime() && dcmonth.getTime() < dep.getTime()){
							evaluate = trs[y].title.split(' ');	
							trs[y].className = 'cc';
							if(!isNaN(total)){
								price = getPrice(evaluate[0]);
								total = isNaN(price) ? NaN : (total + price);
							}							
						}
					}
				}		
			}
		}
		
		return total;
	}
	
	cleanRest = function(darrive, ddeparture, prefx){
		var arr = new Date(darrive);
		var dep = new Date(ddeparture);
		var table = null;
		var total = 0, price = null, evaluate;
		var valprev = null;
		var valnext = null;
		var evaluate;
		if(arr.getMonth() == dep.getMonth()){
			table = document.getElementById(prefx + '_' + (arr.getMonth() + 1) + '_' + arr.getFullYear());
			var tbody = table.getElementsByTagName('tbody')[0];
			var trs = tbody.getElementsByTagName('td');
			var darr = arr.getDate();
			var ddep = dep.getDate();
			for(x = 0; x < trs.length; x++)
			{
				if(trs[x].childNodes[0].data > darr && trs[x].childNodes[0].data < ddep){
					evaluate = trs[x].title.split(' ').length;	
					valprev = (trs[x - 1] ? trs[x - 1].title.split(' ').length : null);
					valnext = (trs[x + 2] ? trs[x + 1].title.split(' ').length : null);
					trs[x].className = (evaluate == 3 ? (valprev == 3 && valnext == 3 ? 'gg' : (valprev == 3 ? 'gw' : 'wg')) : 'ww');
					if(!isNaN(total)){
						price = getPrice(evaluate[0]);
						total = isNaN(price) ? NaN : (total + price);
					}
				}
			}		
		}else{
			var months = (arr.getFullYear() != dep.getFullYear()) ? ((12 - arr.getMonth()) + dep.getMonth()) : Math.abs(arr.getMonth() - dep.getMonth());
			var dcmonth = new Date(darrive);
			var day = 0;
			for(x = 0; x <= months; x++){
				var y = 0;			
				dcmonth.setDate(1);			
				dcmonth.setMonth(dcmonth.getMonth() + (x ? 1 : 0));
				valprev = (trs && y > 0 && trs[y] ? trs[y].title.split(' ').length : null);				
				table = document.getElementById(prefx + '_' + (dcmonth.getMonth() + 1) + '_' + dcmonth.getFullYear());
				var tbody = table.getElementsByTagName('tbody')[0];
				var trs = tbody.getElementsByTagName('td');
				valnext = (trs[y + 1] ? trs[y + 1].title.split(' ').length : null);				
				var day;
				for(y = 0; y < trs.length; y++){
					day = parseInt(trs[y].childNodes[0].data);
					if(!isNaN(day)){
						dcmonth.setDate(day);
						if(dcmonth.getTime() > arr.getTime() && dcmonth.getTime() < dep.getTime()){
							evaluate = trs[y].title.split(' ').length;	
							trs[y].className = (evaluate == 3 ? (valprev == 3 && valnext == 3 ? 'gg' : (valprev == 3 ? 'gw' : 'wg')) : 'ww');
							valprev = evaluate;
							valnext = (trs[y + 2] ? trs[y + 2].title.split(' ').length : null);							
							if(!isNaN(total)){
								price = getPrice(evaluate[0]);
								total = isNaN(price) ? NaN : (total + price);
							}

						}
					}
				}
			}
		}
		return total;	
	}
}
