$(document).ready(function() {
	$('.hide-always').hide();
	$('#checkout_time_hidden').hide();
	/* handle show availability calendar. If for list all resort, don't show it. if for resort, show it */
	var resortSelected = $('#srchResort').val();
	if(resortSelected == "ALL"){
		$('#show_availability_calendar').hide();
		$('#promoCodegroupCode').hide();
		$('#searchByRoomTypeHide').hide();
	}
	$('#srchResort').bind("change", function() {
	    var hotel_code = $('#srchResort').val();
	    if(hotel_code == "ALL"){
	    	$('#show_availability_calendar').hide();
	    	$('#promoCodegroupCode').hide();
	    	$('#searchByRoomTypeHide').hide();
	    	$('.rate-search').hide();
	    }else{
	    	$('#show_availability_calendar').show();
	    	$('#promoCodegroupCode').show();
	    	$('#new-value').html($('#'+hotel_code).html());
	    	$('#searchByRoomTypeHide').show();
	    	$('.rate-search').show();
	    }
	    /*************/
		/*make ajax call to get hotel or chain info as user choose different hotels */

		jQuery.ajax({
			url:"/includes/hotel_address_info.jsp?hotel_code="+hotel_code,
			type:"get",
			cache:false,
			dataType:"html",
			success:function(response) {
				$('#hotel_addr_info').html(response);
				if($('#header_hotel_info').html()!=''){
					$('#header_hotel_info').html($('#hotel_addr_info').html());
				}
				if($('#footer_hotel_info').html()!=''){
					$('#footer_hotel_info').html($('#hotel_addr_info').html());
				}
				$('.hotel_name').html($('.nodisplay_hotelname').html());
				$('#home_hotel_desc').html($('.nodisplay_hotelDesc').html());
				$('#home_hotel_name').html($('.nodisplay_hotelname').html());
			}
		});
	    /****************/

		updateRatePlanLevels();

		jQuery.ajax({
					url:"/includes/lead_rate_include.jsp?srchResort="+hotel_code,
					type:"get",
					cache:false,
					dataType:"html",
					success:function(response) {
						$('#lrblock').html(response);
					}
		});

		//update links
		if($('#header-links li a')!=null) {
			$('#header-links li a').each(function(){
				if(this.id=='room-link' || this.id=='pkg-link' || this.id=='ams-link') {
					var link = this.href;
					if(link.indexOf('srchResort')>0) {
						link = link.substring(0,link.indexOf('srchResort')-1);
					}
					if(hotel_code=='ALL') {
						this.href = link;
					}
					else {
						this.href = link+((link.indexOf('?')>0)?'&':'?')+'srchResort='+hotel_code;
					}
				}
			});
		}

	});

	/* This is for printing user friendly booking confirmation page. Hide user friendly confirmation data first */
	$('#section_print').css("display","none");

	/*
	 * open join our newsletter dialog in the page that host pop up layer
	 */
	$('#show_join_newsletter').bind("click", function() {
		$('#join_newsletter').show().html("");
		$.post("/user/join_newsletter.jsp", {
		}, function(data) {
			$('#join_newsletter').show().html(data);
		});
		$('#join_newsletter').dialog({
			autoOpen: false,
			draggable: true,
			resizable: false,
			width:550,
			modal: true,
			closeText: 'Close Window',
			closeOnEscape: true,
			dialogClass: 'dialog-quickview'
		});
		$("#join_newsletter").dialog("open");
	});

	//close join newsletter pop up layer in footer.jsp
	$('#close-join-newsletter').bind("click",
			function() {
				$('#join_newsletter').dialog("close");
				return false;
			}
	);

	//hide package code in search_room_results_body.jsp
	$('.notdisplay').hide();

	//for terms and conditions in guest detail page show_condition_terms
	//for room policy property_policy in guest detail page
	$("#condition_terms").dialog({
		autoOpen: false,
		draggable: false,
		closeText: 'Close Window',
		title: 'Terms and Conditions',
		dialogClass: 'dialog-quickview',
		width:350,
		resizable: false
	});
	$('#show_condition_terms').bind("click", function() {

		$("#condition_terms").dialog('open');
		return false;
	});


	// to display Guarantee policy
	var guaranteePolicy = $('#guaranteePolicy-for-dialog').html();
	guaranteePolicy = guaranteePolicy == null ? "" : guaranteePolicy;
	guaranteePolicy = guaranteePolicy.length == 0 ? "No guarantee policy available" : guaranteePolicy;
	$("#guaranteePolicy-dialog").html(guaranteePolicy);
	$("#guaranteePolicy-dialog").dialog({
		autoOpen: false,
		draggable: false,
		closeText: 'Close Window',
		width:350,
		height:150,
		modal: true,
		title:'PAYMENT POLICY',
		dialogClass: 'dialog-quickview',
		resizable: false
	});
	$('#show_Payment_Policy').bind("click", function() {
		$("#guaranteePolicy-dialog").dialog('open');
		return false;
	});

	$("#rememberMeDialog").dialog({
		autoOpen: false,
		draggable: false,
		resizable: false
	});
	$('#rememberMeImg').bind("click", function() {
		var left = $(this).offset().left + 50;
		var top = $(this).offset().top - 250;
		$("#rememberMeDialog").dialog({ position: [left,top] });
		$("#rememberMeDialog").dialog('open');
	});

	/* close emailPolicyDialog if any fields clicked and emailPolicyDialog is open*/
	var isEmailPolicyDialogOpen = false;
	$('#manageProfileForm select').click(function(){
		if(isEmailPolicyDialogOpen){
			$("#emailPolicyDialog").dialog('close');
			isEmailPolicyDialogOpen = false;
		};
	});

	$('#manageProfileForm :text').click(function(){
		if(isEmailPolicyDialogOpen){
			$("#emailPolicyDialog").dialog('close');
			isEmailPolicyDialogOpen = false;
		};
	});

	//Email policy help on user registration page uses this.
	$("#emailPolicyDialog").dialog({
		autoOpen: false,
		draggable: false,
		resizable: false
	});
	$('#emailPolicyImg').bind("click", function() {
		isEmailPolicyDialogOpen = true;
		var left = $(this).offset().left + 50;
		var top = $(this).offset().top - 250;
		$("#emailPolicyDialog").dialog({ position: [left,top] });
		$("#emailPolicyDialog").dialog('open');
	});

	//Senshan(9/1/2010) start this function is used in My Reservation page, Retrieve My Reservation link
	$("#retrieveReservationDialog").dialog({
		autoOpen: false,
		draggable: false,
		resizable: false,
		width:300
	});

	$('#retrieveReservation').bind("click", function() {
		var left = $(this).offset().left + 50;
		var top = $(this).offset().top - 250;
		$("#retrieveReservationDialog").dialog({ position: [left,top] });
		$("#retrieveReservationDialog").dialog('open');
	});

	$('#close-retrieveReservationDialog').bind("click", function() {
		$("#retrieveReservationDialog").dialog('close');
	});

	$('#accept-retrieveReservationDialog').bind("click", function() { //added action for acceptance of dialog
		$("#retrieveReservationDialog").dialog('close');
		window.location = '../bp/retrieve_reservation.jsp';
	});

	/*
	 * this function is for country drop down. As country is selected, this function will be called.
	 * The function will return a list of states or proviences based on country selected.
	 */
	$('#reg-country').bind("change", function() {
		var countryCode = $('#reg-country').val();
		if(countryCode.length == 0){
			$('#countryflag').attr("src","/assets/images/country_flags/flag_missing.jpg");
		}else{
			$('#countryflag').attr("src","/assets/images/country_flags/country_flag_" + countryCode + ".gif");
		}

		$.post("/bp/includes/state_list.jsp", {
			countryCode : countryCode
		}, function(data) {
			$("#reg-state").show().html(data);

		});
	});
	/*
	 * open forgot username dialog
	 */
	$('#forgot_username').bind("click", function() {
		$("#forgotUsername").show().html("");
		$.post("/user/forgot_username.jsp", {
		}, function(data) {
			$("#forgotUsername").show().html(innerShiv(data));
		});
		$("#forgotUsername").dialog({
			autoOpen: false,
			draggable: true,
			resizable: false,
			width:450,
			modal: true,
			closeText: 'Close Window',
			closeOnEscape: true,
			dialogClass: 'dialog-quickview'
		});
		$("#forgotUsername").dialog("open");
		return false;
	});

	/*
	 * open forgot password dialog
	 */
	$('#forgot_password').bind("click", function() {
		$("#forgotPassword").show().html("");
		$.post("/user/forgot_password.jsp", {
		}, function(data) {
			$('#forgotPassword').show().html(innerShiv(data));
		});
		$('#forgotPassword').dialog({
			autoOpen: false,
			draggable: true,
			resizable: false,
			width:450,
			modal: true,
			closeText: 'Close Window',
			closeOnEscape: true,
			dialogClass: 'dialog-quickview'
		});
		$("#forgotPassword").dialog("open");
		return false;
	});

	/**
	 * get membership types by membership class
	 */
	$('#membershipClass').bind("change", function() {
		var membershipClassVal = $('select.membershipClass option:selected').val();
		$.post("/user/includes/membershipclass_list.jsp", {
			param1 : membershipClassVal
		}, function(data) {
			$('#membershipType').show().html(data);
			var tmp = $('#membershipTypeLabelHide').html();
			$('#membershipType').html().replace("Select Membership Type",tmp);
		});
	});

});
/*
 * This is general function for dialog pop up
 */
var generalDialog = {
	/*
	 * this is for forgot user name. After forgot username dialog open and
	 * button in the dialog is clicked, this function will be called.
	 */
	forgotUsername : function() {
		try {
			jQuery(document).ready(function() {
				var userName = jQuery("#userName").val();
				$.post("/user/forgot_username.cmd", {
					userName:userName
				}, function(data) {
					$('#forgotUsername').show().html(data);
				});
			});
		} catch(e) {
			//do nothing
		}
	},

	/*
	 * this is for forgot password. After forgot password dialog open and
	 * button in the dialog is clicked, this function will be called.
	 */
	forgotPassword : function() {
		try {
			jQuery(document).ready(function() {
				var userEmail = jQuery("#userEmail").val();
				$.post("/user/forgot_password.cmd", {
					userEmail:userEmail
				}, function(data) {
					$('#forgotPassword').show().html(data);
				});
			});
		} catch(e) {
			//do nothing
		}
	},

	/*
	 * this is for newsletter signup
	 */
	newsleterSignup : function() {
		try {
			jQuery(document).ready(function() {
				var firstName 		= jQuery("#firstName").val();
				var lastName 		= jQuery("#lastName").val();
				var emailAddress	= jQuery("#emailAddress").val();
				var emailType 		= jQuery("#emailType").val();
				var mailingList		= $('#mailingList').is(':checked') == true ? "YES" : "NO";
				$.post("/user/join_newsletter.cmd", {
					firstName		: firstName,
					lastName		: lastName,
					emailAddress	: emailAddress,
					emailType		: emailType,
					mailingList		: mailingList
				}, function(data) {
					$('#join_newsletter').show().html(data);
				});
			});
		} catch(e) {
			//alert(e);
		}
	},

	closeNewsletterPage : function() {
		try{
			jQuery(document).ready(function() {
				$('#join_newsletter').dialog("close");
			});
		} catch(e) {
			//alert(e);
		}
	}

}

var bp = {
	login:function() {
		try {
			jQuery(document).ready(function() {
				// build hidden form with required form fields for the user sign in
				var username = jQuery("#login-username").val();
				var password = jQuery("#login-password").val();

				var bpLoginForm = jQuery("#bpLoginForm");
				var bpLoginFormUsername = jQuery("#bpLoginFormUsername");
				var bpLoginFormPassword = jQuery("#bpLoginFormPassword");

				if (bpLoginForm.length == 0) {
					bpLoginForm = jQuery("<form/>");
					bpLoginForm.attr("id", "bpLoginForm");
					bpLoginForm.attr("action", "/bp/login.cmd");
					bpLoginForm.attr("method", "post");
					bpLoginForm.appendTo(jQuery("html"));
				}

				if (bpLoginFormUsername.length == 0) {
					bpLoginFormUsername = jQuery("<input/>");
					bpLoginFormUsername.attr("id", "bpLoginFormUsername");
					bpLoginFormUsername.attr("type", "hidden");
					bpLoginFormUsername.attr("name", "userName");
					bpLoginFormUsername.appendTo(bpLoginForm);
				}

				if (bpLoginFormPassword.length == 0) {
					bpLoginFormPassword = jQuery("<input/>");
					bpLoginFormPassword.attr("id", "bpLoginFormPassword");
					bpLoginFormPassword.attr("type", "hidden");
					bpLoginFormPassword.attr("name", "password");
					bpLoginFormPassword.appendTo(bpLoginForm);
				}

				bpLoginFormUsername.val(username);
				bpLoginFormPassword.val(password);

				// append hidden form fields required for the room book
				var hiddens = jQuery("form[name='bookForm'] > input[type='hidden']");

				for (var i = 0; i < hiddens.length; i++) {
					var hidden = jQuery(hiddens[i]);

					if (hidden.val() == "") {
						continue;
					}

					var idLookup = (hidden.attr("id") == "") ? "bpLoginForm_" + hidden.attr("name") : "bpLoginForm_" + hidden.attr("id");
					var cloned = jQuery("#" + idLookup);

					cloned = jQuery(hidden).clone();
					cloned.attr("id", idLookup);
					cloned.appendTo(bpLoginForm);
				}

				bpLoginForm.submit();
			});
		} catch(e) {
			// alert(e);
		}
	},
	create:function() {
		try {
			jQuery(document).ready(function() {
				var username = jQuery("#reg-username").val();
				var password = jQuery("#reg-password").val();
				var passwordConfirm = jQuery("#reg-verifyPassword").val();
				var operaNameId = jQuery("#reg-operaNameId").val();
				var emailType = jQuery("#reg-emailType").val();
				var emailAddress = jQuery("#reg-emailAddress").val();
				var firstName = jQuery("#reg-firstName").val();
				var lastName = jQuery("#reg-lastName").val();
				var address1 = jQuery("#reg-address1").val();
				var address2 = jQuery("#reg-address2").val();
				var city = jQuery("#reg-city").val();
				var state = jQuery("#reg-state").val();
				var phone = jQuery("#reg-phone").val();
				var zipCode = jQuery("#reg-zipCode").val();
				var countryCode = jQuery("#reg-countryCode").val();
				var companyName = jQuery("#reg-companyName").val();

				var postData = "username=" + encodeURIComponent(username);
				postData += "&";
				postData += "password=" + encodeURIComponent(password);
				postData += "&";
				postData += "verifyPassword=" + encodeURIComponent(passwordConfirm);
				postData += "&";
				postData += "operaNameId=" + encodeURIComponent(operaNameId);
				postData += "&";
				postData += "emailType=" + encodeURIComponent(emailType);
				postData += "&";
				postData += "emailAddress=" + encodeURIComponent(emailAddress);
				postData += "&";
				postData += "firstName=" + encodeURIComponent(firstName);
				postData += "&";
				postData += "lastName=" + encodeURIComponent(lastName);
				postData += "&";
				postData += "address1=" + encodeURIComponent(address1);
				postData += "&";
				postData += "address2=" + encodeURIComponent(address2);
				postData += "&";
				postData += "city=" + encodeURIComponent(city);
				postData += "&";
				postData += "state=" + encodeURIComponent(state);
				postData += "&";
				postData += "phone=" + encodeURIComponent(phone);
				postData += "&";
				postData += "zipCode=" + encodeURIComponent(zipCode);
				postData += "&";
				postData += "countryCode=" + encodeURIComponent(countryCode);
				postData += "&";
				postData += "companyName=" + encodeURIComponent(companyName);

				jQuery.ajax({
					url:"/bp/ajax/registerFromConfirmationPage.cmd",
					type:"post",
					cache:false,
					data:postData,
					dataType:"html",
					success:function(response) {
						jQuery("#userCreateOnConfirmation").html(response);

						var hiddens = jQuery("input[type='hidden']", response);

						if (hiddens.length == 0) {
							// show response for the short period with the mesasge
							// that user account was successfully created and then redirect it
							// to the dash board page
							var redirect = function() {
								window.parent.location = "/user/view_dashboard.cmd";
							};

							setTimeout(redirect, 2000);
						}
					}
				});
			});
		} catch(e) {
			// alert(e);
		}
	}
};

var reservation = {
	cancel:{
		ui:{
			show:function(html) {
				try {
					jQuery(document).ready(function() {
						var w = 500;
						var h = 360;

						jQuery.blockUI({
							fadeIn:0,
							fadeOut:0,
							message:html,
							css: {
								top:(jQuery(window).height() - h) / 2 + "px",
								left:(jQuery(window).width() - w) / 2 + "px",
								width:w + "px" ,
								height:h + "px" ,
								cursor:'default',
								textAlign:"left"
							}
						});
					});
				} catch(e) {
					// alert(e);
				}
			}
			,
			close:function() {
				try {
					jQuery(document).ready(function() {
						jQuery.unblockUI();
					});
				} catch(e) {
					// alert(e);
				}
			}
			,
			showCancelPopup:function(confirmationNumber, legNum, hotelCode) {
				try {
					jQuery(document).ready(function() {
						jQuery.ajax({
							url:"/bp/includes/reservation_cancel.jsp",
							type:"get",
							cache:false,
							data:"confirmationNumber=" + encodeURIComponent(confirmationNumber)
							   + "&legNum=" + encodeURIComponent(legNum)
							   + "&srchResort=" + encodeURIComponent(hotelCode),
							dataType:"html",
							success:function(response) {
								jQuery('#cancelResLayer').remove();
								jQuery("body").append('<div id="cancelResLayer"></div>');
								jQuery("#cancelResLayer").html(innerShiv(response,false)).css("display","none");
								reservation.cancel.ui.show(jQuery('#cancelResLayer'));
								// jQuery("#cancelResLayer").remove();
							}
						});
					});
				} catch(e) {
					// alert(e);
				}
			}
		}
	}
};

/*open user friendly confirmation page*/
$(document).ready(function() {
    $('#open_print_confirmation').click( function() {
    	var my_window = window.open( $(this).attr('href') );
        my_window.document.write($('#section_print').html());
        my_window.document.close();
        return false;
    });
});
