var global_previous_class	= null;


/**
 *	Returns true if passed char is a natural digit
 */
function isNatural( value ) {

	if ( parseInt(value, 10)>=0 && parseInt(value, 10)<=9 )
		return true;
	else
		return false;
}


/**
 *	Returns true if passed string is an integer
 */
function isInteger( value ) {

	for (var n=0;n<value.length;n++) {
		if (!isNatural(value.charAt(n)))
			return false;
	}
	return true;
}


/**
 *	Checks for the correct format of a passed email
 */
function checkEmail( email ) {

	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email))
		return true;
	else
		return false;
}


/**
 *	Checks for the real data of the passed NIF
 */
function checkNIF( nif ) {

	var carstring	= "TRWAGMYFPDXBNJZSQVHLCKET";

	var dni			= nif.substring(0, nif.length-1);
	var car			= nif.charAt(nif.length-1);

	var position	= dni % 23;
	var pos_car		= carstring.substring(position, position+1);

	if (pos_car!=car.toUpperCase())
		return false;
	else
		return true;
}


/**
 *	This creates the hidden container for the preloaded images
 */
function preLoadImages() {

	var images_div					= document.createElement('div');
		images_div.style.display	= 'none';
		document.body.appendChild( images_div );

	for (var n=0;n<pre_load_images.length;n++) {
		var aux_image				= document.createElement('img');
			aux_image.src			= pre_load_images[n].path;
			images_div.appendChild( aux_image );
	}
}


/**
 *
 */
function homeMenuItemMouseOver( item_handle ) {

	global_previous_class	= item_handle.className;

	if (item_handle.className.indexOf('hover')==-1)
		item_handle.className = item_handle.className + '_hover';
}


/**
 *
 */
function homeMenuItemMouseOut( item_handle ) {

	item_handle.className = global_previous_class;
}


/**
 *
 */
function usersUserFormSubmit() {

	var action		= document.getElementById('action');
	var id_user		= document.getElementById('id_user');

	var email		= document.getElementById('user_email');
	var password	= document.getElementById('user_passwd');
	var repasswd	= document.getElementById('user_repasswd');
	var nif			= document.getElementById('user_nif');
	var name		= document.getElementById('user_name');
	var surname		= document.getElementById('user_surname');
	var born_date	= document.getElementById('user_born_date');
	var address		= document.getElementById('user_address');
	var cp			= document.getElementById('user_cp');
	var city		= document.getElementById('user_city');
	var province	= document.getElementById('user_province');
	var country		= document.getElementById('user_country');
	var phone		= document.getElementById('user_phone');

	if (email.value=='') {
		alert(document.getElementById('users_email_field_language').innerHTML);
		email.focus();
		return false;
	}
	if (!checkEmail(email.value)) {
		alert(document.getElementById('users_email_incorrect_language').innerHTML);
		email.focus();
		return false;
	}
	if (action.value=='save_new_user' && (password.value=='' || repasswd.value=='')) {
		alert(document.getElementById('users_passwd_fields_language').innerHTML);
		password.focus();
		return false;
	}
	else {
		if (password.value!=repasswd.value) {
			alert(document.getElementById('users_different_pass_language').innerHTML);
			password.focus();
			return false;
		}
	}
	if (nif.value=='') {
		alert(document.getElementById('users_nif_field_language').innerHTML);
		nif.focus();
		return false;
	}
	if (name.value=='') {
		alert(document.getElementById('users_name_field_language').innerHTML);
		name.focus();
		return false;
	}
	if (surname.value=='') {
		alert(document.getElementById('users_surname_field_language').innerHTML);
		surname.focus();
		return false;
	}
	if (born_date.value!='' && !new c_ReservationsDates().checkDate(born_date.value)) {
		alert(document.getElementById('users_born_date_language').innerHTML);
		born_date.focus();
		return false;
	}
	if (address.value=='') {
		alert(document.getElementById('users_address_field_language').innerHTML);
		address.focus();
		return false;
	}
	if (cp.value=='') {
		alert(document.getElementById('users_cp_field_language').innerHTML);
		cp.focus();
		return false;
	}
	if (city.value=='') {
		alert(document.getElementById('users_city_field_language').innerHTML);
		city.focus();
		return false;
	}
	if (province.value=='') {
		alert(document.getElementById('users_province_field_language').innerHTML);
		province.focus();
		return false;
	}
	if (country.value=='') {
		alert(document.getElementById('users_country_field_language').innerHTML);
		country.focus();
		return false;
	}
	if (phone.value=='') {
		alert(document.getElementById('users_phone_field_language').innerHTML);
		phone.focus();
		return false;
	}

	if (action.value=='save_new_user')
		new c_Ajax().sendSimpleRequest( 'index.php?action=check_new_user_mail&mail='+email.value, 'GET', '', onUsersUserFormSubmit, global_loading_box );
	else
		new c_Ajax().sendSimpleRequest( 'index.php?action=check_edit_user_mail&id_user='+id_user.value+'&mail='+email.value, 'GET', '', onUsersUserFormSubmit, global_loading_box );

	return false;
}


/**
 *
 */
function onUsersUserFormSubmit( response ) {

	if (response=='OK')
		document.getElementById('user_edit_form').submit();
	else
		alert(document.getElementById('users_user_email_exists_language').innerHTML);
}


/**
 *
 */
function usersLoginSubmit( sufix ) {

	var email			= document.getElementById('user_email'+sufix);
	var password		= document.getElementById('user_password'+sufix);

	if (email.value=='') {
		alert(document.getElementById('users_email_field_language').innerHTML);
		email.focus();
		return false;
	}
	if (!checkEmail(email.value)) {
		alert(document.getElementById('users_email_incorrect_language').innerHTML);
		email.focus();
		return false;
	}
	if (password.value=='') {
		alert(document.getElementById('users_passwd_field_language').innerHTML);
		password.focus();
		return false;
	}

	return true;
}


/**
 *
 */
function usersLoginBoxKeyPress(e) {

	//Event handle
	if (!e) e = window.event;

	var code;
	if (e.keyCode)
		code = e.keyCode;
	else if (e.which)
		code = e.which;

	if (code==13) {
		if (usersLoginSubmit('_box'))
			document.getElementById('login_box_form').submit();
	}
}


/**
 *
 */
function usersRememberPasswdSubmit() {

	var email		= document.getElementById('users_remember_pass_mail');

	if (email.value=='') {
		alert(document.getElementById('users_email_field_language').innerHTML);
		email.focus();
		return false;
	}
	if (!checkEmail(email.value)) {
		alert(document.getElementById('users_email_incorrect_language').innerHTML);
		email.focus();
		return false;
	}
	return true;
}


/**
 *
 */
function supportQueriesFormSubmit() {

	var email		= document.getElementById('queries_email');

	if (email.value=='') {
		alert(document.getElementById('users_email_field_language').innerHTML);
		email.focus();
		return false;
	}
	if (!checkEmail(email.value)) {
		alert(document.getElementById('users_email_incorrect_language').innerHTML);
		email.focus();
		return false;
	}

	return true;
}


/**
 *
 */
function guestbookAddImage() {

	var title	= document.getElementById('guestbook_add_image_title');
	var comment	= document.getElementById('guestbook_add_image_comment');

	if (title.value=='') {
		alert(document.getElementById('guestbook_add_title_field_language').innerHTML);
		title.focus();
		return false;
	}
	if (comment.value=='') {
		alert(document.getElementById('guestbook_add_comment_field_language').innerHTML);
		comment.focus();
		return false;
	}
	return true;
}


/**
 *
 */
function reservationCheckStep0Ctr() {

	var places		= document.getElementById('reservations_places');

	if (places.selectedIndex==0) {
		alert(document.getElementById('reservations_places_number_language').innerHTML);
		places.focus();
		return false;
	}

	return true;
}


/**
 *
 */
function reservationCheckStep1Ctr() {

	var start_date	= document.getElementById('reservations_start_date');
	var end_date	= document.getElementById('reservations_end_date');

	//Check date format and month-days values
	if (start_date.value=='' || !new c_ReservationsDates().checkDate(start_date.value)) {
		alert(document.getElementById('reservations_incorrect_date_language').innerHTML);
		start_date.focus();
		return false;
	}

	if (end_date.value=='' || !new c_ReservationsDates().checkDate(end_date.value)) {
		alert(document.getElementById('reservations_incorrect_date_language').innerHTML);
		end_date.focus();
		return false;
	}

	if (!new c_ReservationsDates().checkDateRange(start_date.value, end_date.value)) {
		alert(document.getElementById('reservations_start_bigger_end_language').innerHTML);
		return false;
	}

	return true;
}


/**
 *
 */
function reservationCheckStep1House() {

	var start_date	= document.getElementById('reservations_start_date');
	var end_date	= document.getElementById('reservations_end_date');
	var places		= document.getElementById('reservations_places');

	//Check date format and month-days values
	if (start_date.value=='' || !new c_ReservationsDates().checkDate(start_date.value)) {
		alert(document.getElementById('reservations_incorrect_date_language').innerHTML);
		start_date.focus();
		return false;
	}

	if (end_date.value=='' || !new c_ReservationsDates().checkDate(end_date.value)) {
		alert(document.getElementById('reservations_incorrect_date_language').innerHTML);
		end_date.focus();
		return false;
	}

	if (!new c_ReservationsDates().checkDateRange(start_date.value, end_date.value)) {
		alert(document.getElementById('reservations_start_bigger_end_language').innerHTML);
		return false;
	}

	if (places.selectedIndex==0) {
		alert(document.getElementById('reservations_places_number_language').innerHTML);
		places.focus();
		return false;
	}

	return true;
}


/**
 *
 */
var reservationPreviousSelectValue = null;
function reservationSelectRoomFocus( select_hand ) {

	reservationPreviousSelectValue = select_hand.selectedIndex;
}


/**
 *
 */
function reservationSelectRoom( select_hand ) {

	var parts						= select_hand.options[select_hand.selectedIndex].value.split('##');
	var previous_parts				= select_hand.options[reservationPreviousSelectValue].value.split('##');
	var places						= parseInt(parts[2], 10);
	var previous_places				= parseInt(previous_parts[2], 10);
	var remaining_places			= document.getElementById('h_num_places');
	var remaining_places_div		= document.getElementById('reservations_remaining_places');

	remaining_places.value			= parseInt(remaining_places.value, 10) - (places - previous_places);
	reservationPreviousSelectValue	= select_hand.selectedIndex;
	remaining_places_div.innerHTML	= (parseInt(remaining_places.value, 10)<0 ? 0 : remaining_places.value);
}


/**
 *
 */
function gotoStep0Center() {

	document.location='index.php?action=reservation&step=0&host=C';
}


/**
 *
 */
function gotoStep1House() {

	document.location='index.php?action=reservation&step=1';
}


/**
 *
 */
function gotoStep1Center() {

	document.getElementById('h_step').value	= 1;
	document.getElementById('h_step').form.submit();
	//document.location='index.php?action=reservation&step=1&host=C';
}


/**
 *
 */
function reservationsShowRoomDetails( id_room ) {

	function getClientWidth() {

		if (self.innerWidth) //Mozilla
			return self.innerWidth;
		else if (document.documentElement && document.documentElement.clientWidth)
			return document.documentElement.clientWidth;
		else if (document.body) //Internet Explorer
			return document.body.clientWidth;
	}


	function getClientHeight() {

		if (self.innerHeight) //Mozilla
			return self.innerHeight;
		else if (document.documentElement && document.documentElement.clientHeight)
			return document.documentElement.clientHeight;
		else if (document.body) //Internet Explorer
			return document.body.clientHeight;
	}

	var div_handle			= document.getElementById('reservations_room_details_'+id_room);

	div_handle.style.left	= ( (getClientWidth() - 600) / 2 );
	div_handle.style.left	= ( parseInt(div_handle.style.left, 10) < 0 ? 0 : div_handle.style.left );
	div_handle.style.top	= document.body.scrollTop + 50;

	div_handle.style.display = 'block';
}


/**
 *
 */
function reservationsRoomDetailsSwapImage( img_hand, id_room, image_width, image_height, image_path ) {

	var main_image				= document.getElementById('room_main_image_'+id_room);
		main_image.src			= new Date().getTime(); //This removes the old image while the new one is loading
		main_image.style.width	= image_width;
		main_image.style.height	= image_height;
		main_image.src			= image_path;

	document.getElementById('reservations_room_details_inner_'+id_room).scrollTop	= 0;
}


/**
 *
 */
function reservationsCloseRoomDetails( id_room ) {

	document.getElementById('reservations_room_details_'+id_room).style.display = 'none';
}


/**
 *
 */
function reservationCheckStep2() {

	var num_places	= document.getElementById('h_num_places');

	//Checks that the selected rooms have enough places number
	if (parseInt(num_places.value, 10)>0) {
		alert(document.getElementById('reservations_incorrect_places_language').innerHTML);
		return false;
	}

	return true;
}


/**
 *
 */
function gotoStep2() {

	document.getElementById('go_to_step_2_form').submit();
}


/**
 *
 */
function reservationCheckStep3() {

	var inputs		= document.getElementsByTagName('input');

	for (var n=0;n<inputs.length;n++) {
		if (inputs[n].id && inputs[n].id.indexOf('reservations_quantity_')!=-1) {
			if (inputs[n].value=='' || !isInteger(inputs[n].value)) {
				alert(document.getElementById('reservations_incorrect_quantity_language').innerHTML);
				inputs[n].focus();
				return false;
			}
		}
	}

	return true;
}


/**
 *
 */
function gotoStep3() {

	document.getElementById('go_to_step_3_form').submit();
}


/**
 *
 */
function gotoStep4() {

	document.getElementById('go_to_step_4_form').submit();
}


/**
 *
 */
function reservationCheckStep5() {

	if (document.getElementById('reservations_payment_mode_radio_card').checked) { //Call to TPV
		document.getElementById('access_TPV_form').submit();
	}
	else if (document.getElementById('reservations_payment_mode_radio_bank').checked) {
		document.getElementById('step5_form').submit();
	}
}


/**
 *
 */
function discardReservation() {

	document.location	= "index.php?action=discard_reservation";
}


// ######################################################################################################################


/**
 *	Reservations Dates class. This performs operation with dates like check if the end date is later than start one
 */
function c_ReservationsDates() {

	this.month_days = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
}


/**
 * Returns the sum of the days to the passed month (included)
 */
c_ReservationsDates.prototype.publicMonthsSumDays = function ( month ) {

	var sum	= 0;
	for (var n=0;n<month;n++) {
		sum += this.month_days[n];
	}
	return sum;
}


/**
 * Returns the complete passed months for a number of days
 */
c_ReservationsDates.prototype.publicMonthsPassed = function ( value ) {

	var month_count	= 0;
	var n			= 0;
	while (value>=this.month_days[n] && n<12) {
		value -= this.month_days[n];
		month_count++;
		n++;
	}
	return month_count;
}


/**
 * Returns year day number of a passed date (dd/mm)
 */
c_ReservationsDates.prototype.publicDate2Number = function ( date ) {

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

	return this.publicMonthsSumDays(month-1)+day;
}


/**
 * Returns the day and month string (dd/mm) for a passed numeric year day
 */
c_ReservationsDates.prototype.publicNumber2Date = function ( value ) {

	var month	= this.publicMonthsPassed(value);
	var day		= value - this.publicMonthsSumDays(month);

	if (day==0)
		day	= this.month_days[month-1];
	else
		month++;

	return (day<10 ? '0'+day : day) + '/' + (month<10 ? '0'+month : month);
}


/**
 *	Checks for the correct format of a passed date (dd/mm/yyyy)
 */
c_ReservationsDates.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-1])
		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;
}


/**
 *
 */
c_ReservationsDates.prototype.checkDateRange = function ( start_date, end_date ) {

	//creates the dd/mm format dates
	var start_date_formatted	= start_date.charAt(0)+start_date.charAt(1)+'/'+start_date.charAt(3)+start_date.charAt(4);
	var start_date_year			= parseInt(start_date.charAt(6)+start_date.charAt(7)+start_date.charAt(8)+start_date.charAt(9), 10);
	var end_date_formatted		= end_date.charAt(0)+end_date.charAt(1)+'/'+end_date.charAt(3)+end_date.charAt(4);
	var end_date_year			= parseInt(end_date.charAt(6)+end_date.charAt(7)+end_date.charAt(8)+end_date.charAt(9), 10);

	//converts dd/mm format date to numeric date
	var numeric_start_date		= this.publicDate2Number(start_date_formatted);
	var numeric_end_date		= this.publicDate2Number(end_date_formatted);

	//checks that numeric_start_date < numeric_end_date
	if (start_date_year>end_date_year || (start_date_year==end_date_year && numeric_start_date>=numeric_end_date))
		return false;

	return true;
}

// ######################################################################################################################
// ###                        These classes are for reservations periods calendar funcionality                        ###
// ######################################################################################################################


/**
 *	c_PeriodDay CLASS: object that contains one day info into the one year calendar structure. Has the day periods info
 */
function c_PeriodDay( day, month, year, periodId, periodType, periodNumDays, periodStartDate, periodEndDate ) {

	this.day				= day;
	this.month				= month;
	this.year				= year;
	this.periodId			= periodId;
	this.periodType			= periodType;
	this.periodNumDays		= periodNumDays;
	this.periodStartDate	= periodStartDate;
	this.periodEndDate		= periodEndDate;
	this.current			= false;

	var date				= new c_MyDate();
	if (date.day==this.day && date.month==this.month && date.year==this.year)
		this.current = true;
}


/**
 *	Sets period info into calendar day
 */
c_PeriodDay.prototype.setPeriod = function ( periodId, periodType, periodNumDays, periodStartDate, periodEndDate ) {

	this.periodId			= periodId;
	this.periodType			= periodType;
	this.periodNumDays		= periodNumDays;
	this.periodStartDate	= periodStartDate;
	this.periodEndDate		= periodEndDate;
}


// ######################################################################################################################


/**
 *	c_PeriodMonth CLASS: this object represents a month table, including c_PeriodDay objects into the populated days
 */
function c_PeriodMonth( month, year ) {

	this.date			= new c_MyDate('01/'+(parseInt(month,10)<10 ? '0'+parseInt(month,10) : parseInt(month,10))+'/'+(parseInt(year,10)<10 ? '0'+parseInt(year,10) : parseInt(year,10)));
	this.month_table	= new Array();

	this.week_day		= this.getFirstWeekDay();

	for (var n=0;n<6;n++)
		this.month_table[n]	= new Array();

	var aux_current_day	= 1;

	for (var i=0;i<6;i++) {
		for (var j=0;j<7;j++) {
			if ( ((i*7)+j+1)>=this.week_day && aux_current_day<=this.date.month_days[this.date.month]) {
				this.month_table[i][j]				= new c_PeriodDay(aux_current_day,this.date.month,this.date.year,null,null,null,null);
				aux_current_day++;
			}
		}
	}
}

/**
 *	Returns week day of the first day for the object month
 */
c_PeriodMonth.prototype.getFirstWeekDay = function () {

	var aux_week_day		= new Date(this.date.year, this.date.month-1, 1).getDay();
	return (aux_week_day==0 ? 7 : aux_week_day);
}


// ######################################################################################################################


/**
 *	c_PeriodYear CLASS: this object represents one complete year, including 12 c_PeriodMonth objects. From the current month to
 *	december, and january of next year to the previous month of current one into the next year
 */
function c_PeriodYear() {

	this.date	= new c_MyDate();
	this.months	= new Array();

	for (var n=0;n<12;n++) {
		if ((n+1)<this.date.month) {
			this.months[n] = new c_PeriodMonth(n+1, this.date.year+1);
		} else {
			this.months[n] = new c_PeriodMonth(n+1, this.date.year);
		}
	}
}


/**
 *	Returns a day object by date
 */
c_PeriodYear.prototype.getDay = function ( day, month ) {

	for (var n=0;n<this.months[month-1].month_table.length;n++) {
		for (var m=0;m<this.months[month-1].month_table[n].length;m++) {
			if (this.months[month-1].month_table[n][m]
				&& this.months[month-1].month_table[n][m].month==month
				&& this.months[month-1].month_table[n][m].day==day) {
					return this.months[month-1].month_table[n][m];
				}
		}
	}
	return false;
}


// ######################################################################################################################

/**
 *	Creates a floating yellow div (like title tips) with a message inside
 */
function c_FloatingTitleBox() {

	this.container							= document.createElement('div');
	this.container.align					= 'center';
	this.container.style.padding			= 5;
	this.container.style.border				= '2px solid #FFCC00';
	this.container.style.backgroundColor	= '#FFFF99';
	this.container.style.cursor				= 'pointer';
	this.container.style.fontSize			= 13;
	this.container.style.fontFamily			= 'Arial';
	this.container.style.position			= 'absolute';
	this.container.style.display			= 'none';

	this.container.onclick = function() {
		this.style.display = 'none';
	}

	document.body.appendChild(this.container);
}


/**
 *	Set c_FloatingTitleBox left position
 */
c_FloatingTitleBox.prototype.setXPosition = function( reference, correction ) {

	var offsetElement			= reference;
	var x 						= 0;

	while (offsetElement && offsetElement.offsetLeft!=null) {
		x 						+= offsetElement.offsetLeft;
		offsetElement			= offsetElement.offsetParent;
	}

	this.container.style.left	= (correction ? x+parseInt(correction,10) : x);
}


/**
 *	Set c_FloatingTitleBox left position
 */
c_FloatingTitleBox.prototype.setYPosition = function( reference, correction ) {

	var offsetElement			= reference;
	var y 						= 0;

	while (offsetElement && offsetElement.offsetTop!=null) {
		y 						+= offsetElement.offsetTop;
		offsetElement			= offsetElement.offsetParent;
	}
	this.container.style.top	= (correction ? y+parseInt(correction,10) : y);
}


/**
 *	Set c_FloatingTitleBox message
 */
c_FloatingTitleBox.prototype.setMessage = function( message ) {

	this.container.innerHTML = message;
}


/**
 *	Show c_FloatingTitleBox
 */
c_FloatingTitleBox.prototype.show = function() {

	this.container.style.display = 'block';
}


/**
 *	Hide c_FloatingTitleBox
 */
c_FloatingTitleBox.prototype.hide = function() {

	this.container.style.display = 'none';
}