var Formular = {

	Type : function (element, form) {

		return Formular.Find(element, form) ? Formular.Find(element, form).type : 'undefined';
	},

	Length : function () {

		return document.forms.length;
	},

	ElementsLength : function (form) {

		if (document.forms[form] != null) {

			return document.forms[form].elements.length;

		} return null;
	},

	Name : function (number) {

		if (document.forms[number] != null) {

			return document.forms[number].name;

		} return null;
	},

	Elements : function (form) {

		if (document.forms[form] != null) {

			return document.forms[form].elements;

		} return null;
	},

	Find : function (element, form) {

		if (typeof element == 'string') {

			if (typeof form == 'string' || typeof form == 'number') {

				return (Formular.Elements(form) != null) ? Formular.Elements(form)[element] : null;

			} else {

				for (var x = 0; x < Formular.Length(); ++x) {

					for (var y = 0; y < Formular.ElementsLength(x); ++y) {

						if (Formular.Elements(x)[y].name == element) {

							return Formular.Elements(x)[y];
						}
					}
				}

			} return null;

		} else if (element.tagName != null && element.nodeType == 1) {

			return element;

		} return false;
	},

	Value : function (element, form) {

		if (Formular.Type(element, form) == 'select-one') {

			return Formular.Find(element, form).options[Formular.Find(element, form).selectedIndex].value;

		} else return Formular.Find(element, form).value;
	}
};

var Search = {

	DateUpdate : function (FromDay, FromMonth, FromYear, ToDay, ToMonth, ToYear) {
	
		Formular.Find(ToDay).options[Formular.Find(FromDay).selectedIndex].selected = true;
		Formular.Find(ToMonth).options[Formular.Find(FromMonth).selectedIndex].selected = true;
		Formular.Find(ToYear).options[Formular.Find(FromYear).selectedIndex].selected = true;
	}
};

var Booking = {

	Tax		: null,
	Fee		: null,

	CleaningPrice 	: null,
	DayPrice	: null,
	WeekPrice	: null,

	RoundPrice : function(value, digits) {

		var price 	= String(Math.round((value * Math.pow(10, digits))) / Math.pow(10, digits));
		var seperat 	= (arguments[2] != null) ? arguments[2] : ".";
		    price 	= (price.indexOf(".") == -1) ? price + seperat + '00' : price.replace(/\./, seperat);

		var amount = price.split(seperat)[1].length;

		if(amount < digits) {

			for(var x = 0; x < (digits - amount); ++x) {

				price += '0';
			}

		} return price;
	},

	PriceUpdate : function (FromDay, FromMonth, FromYear, ToDay, ToMonth, ToYear, Persons, Which) {

		var Persons 	= Formular.Value(Persons);
		var Arrival 	= new Date(Formular.Value(FromYear), (Formular.Value(FromMonth)-1), Formular.Value(FromDay));
		var Departure 	= new Date(Formular.Value(ToYear), (Formular.Value(ToMonth)-1), Formular.Value(ToDay));

		if (Which != null) {

			Formular.Find(ToDay).options[Formular.Find(FromDay).selectedIndex].selected = true;
			Formular.Find(ToMonth).options[Formular.Find(FromMonth).selectedIndex].selected = true;
			Formular.Find(ToYear).options[Formular.Find(FromYear).selectedIndex].selected = true;
		}
		
		if (Persons == 0) {

			Persons = 1;
		}

		var Arrival 	= new Date(Formular.Value(FromYear), (Formular.Value(FromMonth)-1), Formular.Value(FromDay)).getTime();
		var Departure 	= new Date(Formular.Value(ToYear), (Formular.Value(ToMonth)-1), Formular.Value(ToDay)).getTime();

		var Durantion	= ((Departure - Arrival) / 60 / 60 / 24) / 1000;
		var Roomprice 	= 0;
		var Weeks 	= Math.floor(Durantion / 7);
		var Days  	= Durantion % 7;
		
		var PersonDayPrice 	= Booking.DayPrice[(Persons - 1)];
		var PersonWeekPrice 	= Booking.WeekPrice[(Persons - 1)];

		if (Weeks > 0) {

			Roomprice += Booking.WeekPrice[(Persons - 1)] * Weeks;
		}

		if (Days > 0) {

			Roomprice += Booking.DayPrice[(Persons - 1)] * Days;
		}

		var Fee			= (Roomprice * (Booking.Fee / 100));
		    Fee			= Math.round((Fee * Math.pow(10, 2))) / Math.pow(10, 2);

		var Net			= Fee / ((Booking.Tax + 100) / 100);
		var Tax			= Fee - Net;
		var Gross		= Fee;

		var Entirely		= Roomprice;

		Formular.Find('roompriceday').value	= Booking.RoundPrice(PersonDayPrice, 2, ',');
		Formular.Find('roompriceweek').value	= Booking.RoundPrice(PersonWeekPrice, 2, ',');
		Formular.Find('workpricenet').value	= Booking.RoundPrice(Net, 2, ',');
		Formular.Find('workpricetax').value	= Booking.RoundPrice(Tax, 2, ',');
		Formular.Find('workpricegross').value	= Booking.RoundPrice(Gross, 2, ',');
		Formular.Find('fullroomprice').value	= Booking.RoundPrice(Roomprice, 2, ',');
		Formular.Find('fullprice').value	= Booking.RoundPrice((Entirely + Booking.CleaningPrice), 2, ',');
	}
};