var DurationCheckIn = '';
var DurationCheckOut = '';
var DurationNumber = '';
var DurationAdults = '';
var DurationMinors = '';

//DurationAddCheck(1); } else { DurationRemoveCheck(1); }
function DurationAddCheck(value) {
	/*jQuery('img#AjaxLoadAnimationStep3').css('display', 'inline');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-duration.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&value=' + value + '&action=add_checkbox',
		success: function(data) {
			jQuery('img#AjaxLoadAnimationStep3').css('display', 'none');
		}
	});*/
	
	jQuery('img#AjaxLoadAnimationStep3').css('display', 'inline');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-duration.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&value=' + value + '&action=add_checkbox',
		success: function(data) {
			jQuery('img#AjaxLoadAnimationStep3').css('display', 'none');
		}
	});
}

function DurationRemoveCheck(value) {
	jQuery('img#AjaxLoadAnimationStep3').css('display', 'inline');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-duration.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&value=' + value + '&action=remove_checkbox',
		success: function(data) {
			jQuery('img#AjaxLoadAnimationStep3').css('display', 'none');
		}
	});
}

function DurationClearAllChecks(_object) {
	_object.removeClass('ok');
	_object.removeClass('error');
}

function DurationCalculate(check_in, check_out) {
	jQuery('img#AjaxLoadAnimationStep3').css('display', 'inline');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-duration.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&check_in=' + check_in + '&check_out=' + check_out + '&action=calculate_dates',
		success: function(data) {
			DurationNumber = data;
			jQuery('input[type=text]#duration').attr('value', data);
			if(data >= 1) {
				jQuery('input[type=text]#duration').removeClass('error');
				jQuery('input[type=text]#duration').addClass('ok');
			} else {
				jQuery('input[type=text]#duration').removeClass('ok');
				jQuery('input[type=text]#duration').addClass('error');
			}
			jQuery('img#AjaxLoadAnimationStep3').css('display', 'none');
		}
	});
}

function DurationCheckAndCalculate() {
	var one = jQuery('input[type=text]#check_in');
	var two = jQuery('input[type=text]#check_out');
	if(one.hasClass('ok')) {
		if(two.hasClass('ok')) {
			DurationCalculate(one.attr('value'), two.attr('value'));
		}
	}
}

function DurationAddDateAndUpdate(_object, _date) {
	_object.css('color', 'black');
	jQuery('img#AjaxLoadAnimationStep3').css('display', 'inline');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-duration.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&number=' + _object.attr('value') + '&date=' + _date.attr('value') + '&action=add_to_dates',
		success: function(data) {
			if(data != '0') {
				DurationCheckOut = data;
				jQuery('input[type=text]#check_out').attr('value', data).addClass('ok');
			}
			jQuery('img#AjaxLoadAnimationStep3').css('display', 'none');
		}
	});
}

function DurationCheckDate(_object, type) {
	_object.css('color', 'black');
	jQuery('img#AjaxLoadAnimationStep3').css('display', 'inline');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-duration.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&date=' + _object.attr('value') + '&action=check_date&type=' + type,
		success: function(data) {
			if(data == '0') {
				_object.css('color', 'red');
				_object.removeClass('ok');
				_object.addClass('error');
			} else {
				_object.css('color', 'green');
				_object.removeClass('error');
				_object.addClass('ok');
			}
			jQuery('img#AjaxLoadAnimationStep3').css('display', 'none');
			DurationCheckAndCalculate();
		}
	});
}

function DurationCheckNumber(_object) {
	_object.css('color', 'black');
	jQuery('img#AjaxLoadAnimationStep3').css('display', 'inline');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-duration.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&number=' + _object.attr('value') + '&action=check_number',
		success: function(data) {
			if(data == '0') {
				_object.css('color', 'red');
				_object.removeClass('ok');
				_object.addClass('error');
			} else {
				_object.css('color', 'green');
				_object.removeClass('error');
				_object.addClass('ok');
			}
			jQuery('img#AjaxLoadAnimationStep3').css('display', 'none');
			DurationAddDateAndUpdate(_object, jQuery('input[type=text]#check_in'));
		}
	});
}

function DurationCheckAdultsAndMinors(_object, type) {
	_object.css('color', 'black');
	jQuery('img#AjaxLoadAnimationStep3').css('display', 'inline');
	jQuery.ajax({
		type: 'POST',
		cache: false,
		url: GLOBAL_SETTING_SERVER_NAME + 'wp-content/plugins/configurator/ajax/ajax-duration.php?server_name=' + GLOBAL_SETTING_SERVER_NAME + '&number=' + _object.attr('value') + '&action=check_adulst_and_minors&type=' + type,
		success: function(data) {
			if(data == '0') {
				_object.css('color', 'red');
				_object.removeClass('ok');
				_object.addClass('error');
			} else {
				_object.css('color', 'green');
				_object.removeClass('error');
				_object.addClass('ok');
			}
			jQuery('img#AjaxLoadAnimationStep3').css('display', 'none');
			DurationCheckAndCalculate();
		}
	});
}

function DurationTimerCheckIn() {
	var CheckIn = jQuery('input[type=text]#check_in');
	if(CheckIn.attr('value') != DurationCheckIn) {
		DurationClearAllChecks(CheckIn);
		DurationCheckIn = CheckIn.attr('value');
		DurationCheckDate(CheckIn, 'in');
	}
	setTimeout('DurationTimerCheckIn()', 500);
}

function DurationTimerCheckOut() {
	var CheckOut = jQuery('input[type=text]#check_out');
	if(CheckOut.attr('value') != DurationCheckOut) {
		DurationClearAllChecks(CheckOut);
		DurationCheckOut = CheckOut.attr('value');
		DurationCheckDate(CheckOut, 'out');
	}
	setTimeout('DurationTimerCheckOut()', 500);
}

function DurationTimerNumber() {
	var Number = jQuery('input[type=text]#duration');
	if(Number.attr('value') != DurationNumber) {
		DurationClearAllChecks(Number);
		DurationNumber = Number.attr('value');
		DurationCheckNumber(Number);
	}
	setTimeout('DurationTimerNumber()', 500);
}

function DurationTimerAdults() {
	var Number = jQuery('input[type=text]#adults');
	if(Number.attr('value') != DurationAdults) {
		DurationClearAllChecks(Number);
		DurationAdults = Number.attr('value');
		DurationCheckAdultsAndMinors(Number, 'adults');
	}
	setTimeout('DurationTimerAdults()', 500);
}

function DurationTimerMinors() {
	var Number = jQuery('input[type=text]#minors');
	if(Number.attr('value') != DurationMinors) {
		DurationClearAllChecks(Number);
		DurationMinors = Number.attr('value');
		DurationCheckAdultsAndMinors(Number, 'minors');
	}
	setTimeout('DurationTimerMinors()', 500);
}

jQuery(document).ready(function() {
	DurationTimerCheckIn();
	DurationTimerCheckOut();
	DurationTimerNumber();
	DurationTimerAdults();
	DurationTimerMinors();
	
	jQuery('input[type="submit"]#step3ButtonNext').click(function() {
		var return_value = false;
		if(jQuery('input[type=text]#check_in').hasClass('ok')) {
			if(jQuery('input[type=text]#check_out').hasClass('ok')) {
				if(jQuery('input[type=text]#duration').hasClass('ok')) {
					return true;
				} else {
					alert('Field Duration is not correct!');
					jQuery('input[type=text]#duration').focus();
				}
			} else {
				alert('Field Check Out is not correct!');
				jQuery('input[type=text]#check_out').focus();
			}
		} else {
			alert('Field Check In is not correct!');
			jQuery('input[type=text]#check_in').focus();
		}
		return return_value;
	});
});
