﻿////////////// GLOBALS ///////////////////////
var _CONERR = {};
_CONERR['agre'] = {'en':"Click OK if you agree, otherways CANCEL", 'el':"Κλίκ OK αν συμφωνείτε, αλλιως CANCEL"};
_CONERR['exrg'] = {'en':"Would you like to select all the days in between?", 'el':"Θα θέλατε να συμπεριλαβετε ολες τις ενδιάμεσες ημερομηνίες;"};

////////////// CLASS simple_date ///////////////////////

function simple_date(dateobj, l) {
	if(!dateobj) dateobj = new Date();
	this.date = dateobj.getDate();
	this.month = dateobj.getMonth();
	this.year = dateobj.getFullYear();
	this.lg = l;
}

////////////// CLASS cal_date ///////////////////////

function cal_date(dateobj, l) {
	_Inheritance(new simple_date(dateobj, l), this);
	this.mfday = new Date(this.year, this.month, 1).getDay();
	this.getMdays = function(m, y) { return ((m==1) && ((y%4==0 && y%100!=0) || (y%400==0))) ? 29 : _CALINDATA.DAYSINMONTH[m];};
	this.mdays = this.getMdays(this.month, this.year);
	this.lmdays = (this.month==0) ? this.getMdays(11, this.year-1) : this.getMdays(this.month-1, this.year);
}


////////////// CLASS cal_base ///////////////////////

function cal_base(date, l) {
	
///////// METHODS //////////////////////
	this.container = {'tableid':'', 'div':null};
	this.aux = {'md':0, 'nm':0, 'tt':0, 'ti':0};
/////  set function ////////////////
	this.start = function(date, l) {
		if(this.caldate && !l) l = this.caldate.lg;
		this.caldate = new cal_date(date, l);
		this.rows = ((this.caldate.mfday + this.caldate.mdays) % 7>0) ? parseInt((this.caldate.mfday + this.caldate.mdays) / 7)+1 : (this.caldate.mfday + this.caldate.mdays) / 7;
		this.today = (!date) ? this.caldate : new simple_date();
	}
/////  table function ////////////////
	this.table = function(tableid) {
		if(tableid) this.container.tableid =  tableid;
		return "\n<table" +  ((this.container.tableid) ? " id='" + this.container.tableid + "'" : "") + " border='0' cellspacing='1' cellpadding='0'><tbody>" + this.mheader() + this.mrows() + "</tbody><table>\n";
	};
/////  make header function ////////////////
	this.mheader = function() {
		return "<tr><td class='month' colspan='7'>" + ((this.caldate.lg!='el') ? _CALINDATA.MONTH[this.caldate.lg][this.caldate.month].toUpperCase() : (_CALINDATA.MONTH[this.caldate.lg][this.caldate.month].substr(0, _CALINDATA.MONTH[this.caldate.lg][this.caldate.month].length-1) + _CALINDATA.MONTH.end).toUpperCase()) + " " + this.caldate.year + "</td></tr><tr class='days'>" + this.mheaderdays() + "</tr>";
	};
/////  make headerdays function ////////////////
	this.mheaderdays = function() {
		var r = "";
		for(var t=0;t<7;t++) r += "<td class='day'>" + _CALINDATA.WDAY[this.caldate.lg][t].substring(0,3).toUpperCase() + "</td>";
		return r;
	}
/////  make rows function ////////////////	
	this.mrows = function() {
		this.aux.md = this.aux.nm = 0;
		var r = "";
		for(this.aux.ti=0;this.aux.ti<this.rows;this.aux.ti++) {
			r += "<tr>";
			for(this.aux.tt=0;this.aux.tt<7;this.aux.tt++) {
				if(this.aux.ti==0 && this.aux.tt==this.caldate.mfday) this.aux.md = 1;
				r += this.mcell();
				if(this.aux.md>this.caldate.mdays) {
					this.aux.md = 0;
					this.aux.nm = 1;
				}
			}
			r += "</tr>";
		}
		return r;
	};
/////  make cell function ////////////////
	this.mcell = function() {
		return "<td" + ((this.aux.md && this.aux.md==this.today.date && this.caldate.month==this.today.month && this.caldate.year==this.today.year) ? " class='today'" : ((!this.aux.md) ? " class='omonth'" : "")) + ">" + ((this.aux.md) ? this.aux.md++ : ((this.aux.ti==0) ? this.caldate.lmdays - this.caldate.mfday + this.aux.tt + 1 : this.aux.nm++)) + "</td>";
	};
/////  display function ////////////////
	this.display = function(divid) {
		if(divid) this.container.div = getOBJ(divid);
		this.container.div.innerHTML = this.table(((divid) ? divid + "__t" : ""));
	}
/////  set new lang function ////////////////
	this.setnewlg = function(l) {
		this.caldate.lg = l;
		this.display();
	}
//////////// MAIN ///////////////////////////////	
	this.start(date, l);
}

////////////// CLASS cal_baseplus ///////////////////////

function cal_baseplus(date, e, l) {
	_Inheritance(new cal_base(date, l), this);
/////  make header function ////////////////
	this.mheader = function() {
		return "<tr><td class='month' colspan='7'><span style='width:100%'>" + ((this.caldate.lg!='el') ? _CALINDATA.MONTH[this.caldate.lg][this.caldate.month].toUpperCase() : (_CALINDATA.MONTH[this.caldate.lg][this.caldate.month].substr(0, _CALINDATA.MONTH[this.caldate.lg][this.caldate.month].length-1) + _CALINDATA.MONTH.end).toUpperCase()) + " " + this.caldate.year + "<span class='prev'><a href='javascript:void prevmonth" + e + "()' title='Click to see previous month'><<</a></span><span class='next'><a href='javascript:void nextmonth" + e + "()' title='Click to see next month'>>></a></span></span></td></tr><tr class='days'>" + this.mheaderdays() + "</tr>";
	};
/////  adjyear function ////////////////
	this.adjyear = function(previous) {
		this.start(new Date(((previous) ? this.caldate.year-1 : this.caldate.year+1), this.caldate.month));
	}

/////  adjmonth function ////////////////
	this.adjmonth = function(previous) {
		this.start(new Date(((previous && this.caldate.month==0) ? this.caldate.year-1 : this.caldate.year), ((previous && this.caldate.month==0) ? 11 : ((previous) ? this.caldate.month-1 : this.caldate.month+1))));
	}
}


////////////// CLASS cal_adv ///////////////////////

function cal_adv(date, e, datar, strobj, l) {
	_Inheritance(new cal_baseplus(date, e, l), this);
	this.data = (datar) ? datar : {};
	this.ydata = {'t':0, 'd':{}, 'r':{}, 'a':1, 'total':0};
	
/////  make cell function ////////////////
	this.mcell = function() {
		var tcy = this.caldate.year, tcm = this.caldate.month;
		var day = (this.aux.md) ? this.aux.md++ : ((this.aux.ti==0) ? this.caldate.lmdays - this.caldate.mfday + this.aux.tt + 1 : this.aux.nm++);
		var kid = iddate([tcy, tcm, day]);
		var r = "<td class='" + ((!this.aux.md) ? "omonth'" : ((this.data && this.data[tcy] && this.data[tcy][tcm] && this.data[tcy][tcm][day]) ? "booked'" : ((this.aux.md && this.aux.md==this.today.date+1 && tcm==this.today.month && tcy==this.today.year) ? "today'" : ((this.ydata.d[tcy] && this.ydata.d[tcy][tcm] && this.ydata.d[tcy][tcm][day]) ? "ybook' id='" + kid + "' onclick='" + strobj + ".bookday(this)'" : ((tcy>this.today.year  || (tcy==this.today.year && (tcm>this.today.month || (tcm==this.today.month && day>this.today.date)))) ? "book' id='" + kid + "' onclick='" + strobj + ".bookday(this)'" : "'"))))) + ">";
		var s = (r.indexOf("'book'")>0) ? window.findseason(new Date(tcy,tcm, day)) : null;
		if(s) r += "<span class='" + s + "'>" + day + "</span>";
		else r += day;
		return r + "</td>";
	};
/////  book-unbook function ////////////////
	this.dexport = function() {
		var r = {}, a = 0, d, dd, ddd;
		for(d in this.ydata.d) {
			r[d] = {};
			for(dd in this.ydata.d[d]) {
				r[d][dd] = {};
				for(ddd in this.ydata.d[d][dd]) {
					if(this.ydata.d[d][dd][ddd]) {
						a++;
						r[d][dd][ddd] = 1;
					}
				}
			}
		}
		return (a) ? {'t':a, 'd':r} : 0;
	}
/////  book-unbook function ////////////////
	this.bookday = function(td) {
		var d = iddate(td.id);
		if(this.ydata.d[d[0]] && this.ydata.d[d[0]][d[1]] && this.ydata.d[d[0]][d[1]][d[2]]) {
			this.setybook(td, d, 1);
			if(this.ydata.a) {
				var d2 = new Date(d[0], d[1], d[2]);
				if(this.ydata.r.s && window.dequals(this.ydata.r.s,d2)) this.ydata.r.s.setDate(this.ydata.r.s.getDate()+1);
				else if(this.ydata.r.e && window.dequals(this.ydata.r.e,d2)) this.ydata.r.e.setDate(this.ydata.r.e.getDate()-1);
				else this.derange();
			}
			if(this.ydata.total==0) this.ydata.a = 1;
		} else {
			if(!this.ydata.a || !this.ydata.t) {
				this.setybook(td, d);
				if(this.ydata.a && this.ydata.t==1) this.ydata.r.s = new Date(d[0], d[1], d[2]);
			} else {
				var d1 = new Date(d[0], d[1], d[2]);
				var chkb = this.chekbetw(new Date(d[0], d[1], d[2]));
				if(chkb!==false) {
					if(chkb) {
						getOBJ(this.msg.ms).innerHTML = "<p class='ms'>" + _CONERR.exrg[this.caldate.lg] + "<br>" + _CONERR.agre[this.caldate.lg] + "</p><p  class='bs'><a href='javascript:void document." + this.msg.o + ".conrange(\"" + td.id + "\")'>CANCEL</a> <a href='javascript:void document." + this.msg.o + ".conrange(new Date(" + d[0] + ", " + d[1] + ", " + d[2] + "), 1)'>OK</a></p>";
						shomsg(this.msg.ms);
					} else this.setrange(d1);
				}
				else {
				 	this.setybook(td, d);
				 	this.derange();
				}
			}
		}
	};
/////  prepmsg function ////////////////	
	this.failsub = function(m, b) {	
		getOBJ(this.msg.ms).innerHTML = "<p class='ms'>" + m + "</p><p  class='bs'><a href='javascript:void shomsg();'>" + b + "</a></p>";
		shomsg(this.msg.ms);
	}
/////  prepmsg function ////////////////	
	this.prepmsg = function(msg, cal) {
		if(cal) {
			var m = getOBJstyle(msg.ms);
			m.width = (getOWidth(cal) - 6) + "px";
			m.height = (getOHeight(cal) - 6) + "px";
		}
		this.msg = msg;
	}
/////  clearmsg function ////////////////	
	this.clearmsg = function() {
		var o = getOBJ(this.msg.ms);
		var m = o.style;
		o.innerHTML = "";
		m.width = "1px";
		m.height = "1px";
	}
/////  canrange function ////////////////	
	this.conrange = function(i, y) {
		shomsg();
		if(y) this.setrange(i);
		else {
			var td = getOBJ(i);
			this.setybook(td, iddate(i));
			this.derange();
		}
		getOBJ(this.msg.ms).innerHTML = "";
	}
/////  derange function ////////////////	
	this.derange = function() {
		this.ydata.t = 0; this.ydata.r = {}; this.ydata.a = 0;
	}
/////  setybook function ////////////////	
	this.setybook = function(td, d, n) {
		if(!this.ydata.d[d[0]]) this.ydata.d[d[0]] = {};
		if(!this.ydata.d[d[0]][d[1]]) this.ydata.d[d[0]][d[1]] = {};
		this.ydata.d[d[0]][d[1]][d[2]] = (n) ? 0 : 1;
		this.ydata.total = (n) ? this.ydata.total - 1 : this.ydata.total + 1;
		if(this.ydata.a) this.ydata.t = (n) ? this.ydata.t - 1 : this.ydata.t + 1;
		if(!(typeof td=='string' && !getOBJ(td))) setclass(td, ((n) ? 'book' : 'ybook'));
	}
/////  setrange function ////////////////		
	this.setrange = function(d1) {
		var temns = null, temne = null, tear = [];
		var d11 = this.ydata.r.s;
		var d12 = this.ydata.r.e;
		if(d1<d11) {
			if(!d12) this.ydata.r.e = new Date(d11.getFullYear(), d11.getMonth(), d11.getDate());
			temns = d1;
			temne = d11;
			this.ydata.r.s = new Date(d1.getFullYear(), d1.getMonth(), d1.getDate());
		} else {
			this.ydata.r.e = d1;
			temne = d1;
			temns = (!d12) ? new Date(d11.getFullYear(), d11.getMonth(), d11.getDate()) : new Date(d12.getFullYear(), d12.getMonth(), d12.getDate());
		}
		var temd;
		var temdays = temd = daysbetw(temns, temne);
		do {
			if(d1>d11 || temdays!=temd) temns.setDate(temns.getDate()+1);
			tear[0] = temns.getFullYear();
			tear[1] = temns.getMonth();
			tear[2] = temns.getDate();
			this.setybook(iddate(tear), tear);
			temdays--;
		} while(temdays>0)
	}
/////  check if data between start-end function ////////////////		
	this.chekbetw = function(d1) {
		var yds = this.ydata.r.s, yde = this.ydata.r.e, od1, od2, c = 0;
		if(!yde || window.dequals(yds, yde)) {
			od1 = (d1>yds) ? new Date(yds.getFullYear(), yds.getMonth(), yds.getDate()) : d1;
			od2 = (d1>yds) ? d1 : yds;
		}else {
			od1 = (yds>d1) ? d1 : new Date(yde.getFullYear(), yde.getMonth(), yde.getDate());
			od2 = (yds>d1) ? yds : d1;
		}
		do {
			c++;
			od1.setDate(od1.getDate()+1);
			if(this.data[od1.getFullYear()] && this.data[od1.getFullYear()][od1.getMonth()] && this.data[od1.getFullYear()][od1.getMonth()][od1.getDate()]) return false;
		} while(od1<od2)
		return --c;
	}
}

function cal_edi(date, e, datar, strobj) {
	_Inheritance(new cal_adv(date, e, datar, strobj, "el"), this);
	
/////  make cell function ////////////////
	this.mcell = function() {
		var tcy = this.caldate.year, tcm = this.caldate.month;
		var day = (this.aux.md) ? this.aux.md++ : ((this.aux.ti==0) ? this.caldate.lmdays - this.caldate.mfday + this.aux.tt + 1 : this.aux.nm++);
		var onm = 0;
		if(this.aux.md) {
			onm = (this.aux.md==this.today.date+1 && tcm==this.today.month && tcy==this.today.year) ? "today" : ((tcm==this.today.month && this.aux.md<=this.today.date) ? 0 : "book");
			if(this.data && this.data[tcy] && this.data[tcy][tcm] && this.data[tcy][tcm][day]) onm = "bookede";
			if(this.ydata.d[tcy] && this.ydata.d[tcy][tcm] && this.ydata.d[tcy][tcm][day]) onm = (onm=="bookede") ? "book" : "bookede";
			var kid = iddate([tcy, tcm, day]);
			onm = "'" + onm + "' id='" + kid + "' onclick='" + strobj + ".bookday(this)'";
		}
		return "<td class=" + ((!onm) ? "'omonth'" : onm) + ">" + day + "</td>";
	};
/////  book-unbook function ////////////////
	this.bookday = function(td) {
		var d = iddate(td.id);
		if(this.ydata.d[d[0]] && this.ydata.d[d[0]][d[1]] && this.ydata.d[d[0]][d[1]][d[2]]) this.setybook(td, d, 1);
		else this.setybook(td, d);
		this.ydata.total
	};
/////  setybook function ////////////////	
	this.setybook = function(td, d, n) {
		if(!this.ydata.d[d[0]]) this.ydata.d[d[0]] = {};
		if(!this.ydata.d[d[0]][d[1]]) this.ydata.d[d[0]][d[1]] = {};
		this.ydata.d[d[0]][d[1]][d[2]] = (n) ? 0 : 1;
		var tb = this.ydata.total;
		this.ydata.total = (n) ? this.ydata.total - 1 : this.ydata.total + 1;
		if(this.ydata.total==0) this.wrbut();
		else if(this.ydata.total==1 && !tb) this.wrbut(1);
		var c = "bookede";
		if(this.data && this.data[d[0]] && this.data[d[0]][d[1]] && this.data[d[0]][d[1]][d[2]]) {
			if(this.ydata.d[d[0]][d[1]][d[2]]) c = "book";
		} else if(!this.ydata.d[d[0]][d[1]][d[2]]) c = "book";
		if(!(typeof td=='string' && !getOBJ(td))) setclass(td, c);
	}
/////  estart function ////////////////
	this.estart = function(dt, da) {
		this.data = da;
		this.start(new Date(dt[0], dt[1]));
	}
/////  clear ydata function ////////////////
	this.cydata = function() {
		this.ydata = {'t':0, 'd':{}, 'r':{}, 'a':1, 'total':0};
	}
/////  write button function ////////////////
	this.wrbut = function(o) {
//------- hardcoded-------------------------
		var f = "<button type='button' class='fbut' onclick='if(_MSG_.o) initmsg();FLOBJS[FLAV.d].doneflo(\"closeflo\")'>" + FLWIN.buts.c.el + "</button>";
		var s = "<button type='button' class='fbut' onclick='FLOBJS[FLAV.d].doneflo(\"closeflo\", 3)'>Μεταβολη</button>";
//-----------------------------------------
		var bx = getOBJ("flo_but");
		if(o) bx.innerHTML = f + s;
		else bx.innerHTML = f;
	}
}
/////  find how many days between dates function ////////////////		
function daysbetw(d1, d2) {
	var DSTajust = 0, m = 1000*60, d = m*60*24;
	d1.setHours(0);
	d1.setMinutes(0);
	d1.setSeconds(0);
	d2.setHours(0);
	d2.setMinutes(0);
	d2.setSeconds(0);
	if(d2>d1) DSTajust = ((d2>d1) ? d2.getTimezoneOffset()-d1.getTimezoneOffset() : d1.getTimezoneOffset()-d2.getTimezoneOffset())*m;
	return Math.ceil((Math.abs(d2.getTime()-d1.getTime())-DSTajust)/d);
}
