var mailregex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

$(document).ready(function(){

	$("#search_hotel").validationEngine();
	$("#search_hotel2").validationEngine();
	$("#booking_frm_login").validationEngine();
	$("#booking_frm_step1").validationEngine();
	$("#booking_frm_step2").validationEngine();
	$("#booking_frm_step3").validationEngine();
	$("#frm_contact").validationEngine();

	$('.cocukyasi').live('change', function() {
		var id = $(this).attr('id');
		var val = $(this).val();

		$('.childselect'+id).hide();
		if(val>0) {
			$('.childspan'+ id).show();
		} else  {
			$('.childspan'+ id).hide();
		}

		for(i=1; i<=val; i++) {
			$('#child'+ id +'-'+ i).show();
		}
	});

	$('.cocukyasi2').live('change', function() {
		var id = $(this).attr('id');
		var val = $(this).val();

		$('.childselect'+id+'2').hide();
		if(val>0) {
			$('.childspan'+ id +'2').show();
		} else  {
			$('.childspan'+ id +'2').hide();
		}

		for(i=1; i<=val; i++) {
			$('#child'+ id +'-'+ i +'2').show();
		}
	});


	$('#login_window').live('click', function() {
		tb_show('Login your BookinEarth', '/login?width=300&height=150&modal=true');
	});

	$('#register_window').live('click', function() {
		tb_show('Register to BookinEarth', '/register?width=360&height=300&modal=true');
		return false;
	});
	$('#register_window_on_booking').live('click', function() {
		tb_show('Register to BookinEarth', '/register?width=360&height=300&modal=true&');
		return false;
	});

	$('#forgot_pw_window').live('click', function() {
		tb_show('Forgot your password?', '/forgot_password?width=300&height=120&modal=true');
		return false;
	});

	$('#frm_login').live('submit', function(){
		if ($('#txt_pwd').val() != '' && $('#txt_e_mail').val() != '') {
			$('#login_loader').show();
			$.ajax({
				cache 	: false,
				url		: '/ajax/member/login',
				data	: $('#frm_login').serialize(),
				type	: 'post',
				success: function(rsp) {
					$('#login_loader').hide();
					if (rsp == -1) {
						$('#controller').html('<span id="err_controller">Please fill fields</span>');
					} else if (rsp == -2) {
						$('#controller').html('<span id="err_controller">Incorrect user information</span>');
					} else if (rsp == 1) {
						location.reload(true);
					}
				}
			});
		}
		return false;
	});

	$('#frm_contact').live('submit', function() {
		if ($('input[name=subject]').val() != '' && $('input[name=name]').val() != '' && $('input[name=email]').val() != '' && $('textarea[name=message]').val() != '') {
			$('#login_loader').show();
			$('#controller').html('');
			$('#contact_form_submit').attr('disabled', 'disabled');
			$.ajax({
				cache 	: false,
				url		: '/ajax/contact_form',
				data	: $('#frm_contact').serialize(),
				type	: 'post',
				success: function(rsp) {
					$('#login_loader').hide();
					if (rsp == -1) {
						$('#controller').html('<span id="err_controller">Please fill all fields</span>');
						$('#contact_form_submit').removeAttr('disabled');
					} else if (rsp == 1) {
						$('#controller').html('<span id="succ_controller">Your message has reached us! Thank You...</span>');
					} else if(rsp == -2) {
						$('#controller').html('<span id="err_controller">Incorrect Security Code.</span>');
						$('#contact_form_submit').removeAttr('disabled');
					} else {
						$('#controller').html('<span id="err_controller">An error occurred, Failed to send email. Please try again.</span>');
						$('#contact_form_submit').removeAttr('disabled');
					}
				}
			});
		}
		return false;
	});

	$('#logout').live('click', function(){
			$.ajax({
				cache 	: false,
				url		: '/ajax/member/logout',
				type	: 'get',
				success: function(rsp) {
					location.reload(true);
				}
			});
	});

	$('#frm_login_on_bookin').live('submit', function(){
		$('#controller').html('');
		if ($('#txt_pwd').val() != '' && $('#txt_e_mail').val() != '') {
			$('#login_loader').show();
			$.ajax({
				cache 	: false,
				url		: '/ajax/member/login',
				data	: $('#frm_login_on_bookin').serialize(),
				type	: 'post',
				success: function(rsp) {
					$('#login_loader').hide();
					if (rsp == -1) {
						$('#controller').html('<span id="err_controller">Please fill fields</span>');
					} else if (rsp == -2) {
						$('#controller').html('<span id="err_controller">Incorrect user information</span>');
					} else if (rsp == 1) {
						$('#controller').html('<span id="succ_controller">Login Successfull. Now You Can Click Next Button</span>');
						$('#btn_booking_next').show();
						/*$('#first_next').show();*/
						$('#secenek2').click();
					}
				}
			});
		}
		return false;
	});

	$('#frm_forgotpw').live('submit', function(){
		if ($('#txt_e_mail').val() != '') {
			$('#forgotpw_loader').show();
			$.ajax({
				cache 	: false,
				url		: '/ajax/member/forgot_password_1',
				data	: $('#frm_forgotpw').serialize(),
				type	: 'post',
				success: function(rsp) {
					$('#forgotpw_loader').hide();
					if (rsp == -2) {
						$('#controller').html('<span id="err_controller">In correct user information</span>');
					} else if (rsp == 1) {
						$('#controller').html('<span id="succ_controller">your confirmation mail was sent.</span>');
						setTimeout("tb_remove();", 1500);
					}
				}
			});
		}
		return false;
	});

	$('#frm_register').live('submit', function(){
		var err = false;
		$('#controller').empty();

		$('#frm_register .req').each(function() {
			var rel = $(this).attr('rel');
			var val = $.trim($(this).val());
			rel = rel.split('_');
			$('#err_'+ rel[0] +'').remove();
			$('#lbl_'+ rel[0]).removeClass('itReq');

			if (val.length < rel[1]) {
				err = true;
				$('#lbl_'+ rel[0]).addClass('itReq');
				$('#txt_'+ rel[0]).after('<span class="frm_err" id="err_'+ rel[0] +'"> must be greater than '+ rel[1] +'</span>');
			} else if (rel[2] == 'email' && !mailregex.test(val)) {
				err = true;
				$('#txt_'+ rel[0]).after('<span class="frm_err" id="err_'+ rel[0] +'"> this is not a valid email address</span>');
			}
		});

		if (!err) {
			$('#register_loader').show();
			$.ajax({
				cache 	: false,
				url		: '/ajax/member/register',
				data	: $('#frm_register').serialize(),
				type	: 'post',
				success: function(rsp) {
					$('#register_loader').hide();
					if (rsp == 0) {
						$('#controller').html('<span id="err_controller">Please fill all fields</span>');
					} else if (rsp == -1) {
						$('#controller').html('<span id="err_controller">E-Mail address unavailable</span>');
					} else if (rsp == -2) {
						$('#controller').html('<span id="err_controller">upps.</span>');
					} else if (rsp == 1) {
						$('#controller').html('<span id="succ_controller">thank you for registering</span>');
						try {
							$('#txt_e_mail').val($('#txt_email').val());
							$('#txt_pwd').val('şifre');
						} catch(e) {
							setTimeout("location.reload(true);",1500);
						}
						tb_remove();

					}
				}
			});
		}

		return false;
	});

	$('.tb_show').live('click', function(){
		var title = $(this).attr('title');
		tb_show(title, $(this).attr('href') +'&TB_iframe=true');

		return false;
	});

	Cufon.replace('h1');
	Cufon.replace('span.hotel_name');
	Cufon.replace('span.cufon_normal');
	Cufon.replace('span.cufon_bold');

});



var oldCheckinDate = "";
var oldCheckoutDate = "";

function disableDateArea(obj) {
	if(obj.checked) {
		oldCheckinDate = $('#checkin_date').val();
		oldCheckoutDate = $('#checkout_date').val();

		$('#checkin_date').removeClass('validate[required]');
		$('#checkout_date').removeClass('validate[required]');
	} else {
		$('#checkin_date').val(oldCheckinDate);
		$('#checkout_date').val(oldCheckoutDate);

		$('#checkin_date').addClass('validate[required]');
		$('#checkout_date').addClass('validate[required]');
	}
}
function disableDateArea2(obj) {
	if(obj.checked) {
		oldCheckinDate = $('#checkin_date2').val();
		oldCheckoutDate = $('#checkout_date2').val();

		$('#checkin_date2').removeClass('validate[required]');
		$('#checkout_date2').removeClass('validate[required]');
	} else {
		$('#checkin_date2').val(oldCheckinDate);
		$('#checkout_date2').val(oldCheckoutDate);

		$('#checkin_date2').addClass('validate[required]');
		$('#checkout_date2').addClass('validate[required]');
	}
}

// Geniş arama alanı tab tetikleyicileri
$('.search_tab').live('click', function() {
	switch($(this).attr('id')) {
		case 'hotel_search_tab':
			$('#hotel_search_layer').css('display', '');
			$('#hotel_search_tab > h1').removeClass('passive').addClass('active');

			$('#car_rental_layer').css('display', 'none');
			$('#car_rental_tab > h1').removeClass('active').addClass('passive');
			break;
		case 'car_rental_tab':
			$('#car_rental_layer').css('display', '');
			$('#car_rental_tab > h1').removeClass('passive').addClass('active');

			$('#hotel_search_layer').css('display', 'none');
			$('#hotel_search_tab > h1').removeClass('active').addClass('passive');
			break;
	}

	Cufon.replace('h1');
});

// Dar arama alanı tab tetikleyicileri
$('.mini_search_tab').live('click', function() {
	switch($(this).attr('id')) {
		case 'mini_hotel_search_tab':
			$('#mini_hotel_search_layer').css('display', '');
			$('#mini_hotel_search_tab > h1').removeClass('passive').addClass('active');

			$('#mini_car_rental_layer').css('display', 'none');
			$('#mini_car_rental_tab > h1').removeClass('active').addClass('passive');
			break;
		case 'mini_car_rental_tab':
			$('#mini_car_rental_layer').css('display', '');
			$('#mini_car_rental_tab > h1').removeClass('passive').addClass('active');

			$('#mini_hotel_search_layer').css('display', 'none');
			$('#mini_hotel_search_tab > h1').removeClass('active').addClass('passive');
			break;
	}

	Cufon.replace('h1');
});

// Geniş otel arama kısmındaki Adult ve Child alanlarını klonlar
function kopyala(sayi) {
	$('#_tmp').html('');
	var html = '';
	for(i=1;i<sayi;i++) {
		html += $('#tmp_ex_container').html().replace(/_0/gi, '_'+(i+1));
	}
	//alert(html);
	$('#_tmp').html($('#_tmp').html()+html);
}
function kopyala2(sayi) {
	$('#_tmp2').html('');
	var html = '';
	for(i=1;i<sayi;i++) {
		html += $('#tmp_ex_container2').html().replace(/_0/gi, '_'+(i+1));
	}
	//alert(html);
	$('#_tmp2').html($('#_tmp2').html()+html);
}



jQuery(function() {
	//$('#search_hotel').bind('submit', function() {return false;} ) ;

	var options = {
		script:"/ajax/get/hotel_info?json=true&",
		varname:"input",
		json:true,						// Returned response type
		shownoresults:true,				// If disable, display nothing if no results
		noresults:"No Results",			// String displayed when no results
		maxresults:8,					// Max num results displayed
		cache:false,					// To enable cache
		minchars:2,						// Start AJAX request with at leat 2 chars
		timeout:100000,					// AutoHide in XX ms
		callback: function (obj) { 		// Callback after click or selection
		}
	};
	var as_json = new bsn.AutoSuggest('destination', options);


});

