$(function(){

// Dialog Link
			$('#dialog_link').click(function(){
				$("#forcedlogin").val = "false";
				$('.loginBox').dialog('open');
				$('#login_mail').focus();
				return false;
			});

			//hover states on the static widgets
			$('#dialog_link, ul#icons li').hover(
				function() { $(this).addClass('ui-state-hover'); }, 
				function() { $(this).removeClass('ui-state-hover'); }
			);
			
			$('#dialog_closeimage, #login_close_box').click(function(){
				$('.loginBox').dialog('close');
				return false;
			});

			$('#forgotPasswordPage').dialog({
				autoOpen: false,
				width: 486,
				height: 350,
				modal:true
			});

			// Dialog Link
			$('#join_reg').click(function(){
				$("#regCaptchaImg").attr("src", "/captcha?ts=" + (new Date().getTime()));
				$('#registrationPage').dialog('open');
				return false;
			});
			
			$('#reg_cancelBtn,#reg_closeText').click(function(){
				$('#registrationPage').dialog('close');
				return false;
			});
			
			$('#reg_cancelBtn').click(function(){
				$('#registrationPage').dialog('close');
				return false;
			});
			
			$('#reg_cancelBtn,#reg_closeText').click(function(){
				$('#registrationPage').dialog('close');
				return false;
			});

			$('#forgotPasswordLink').click(function(){
				$('#forgotPasswordPage').dialog('open');
				$("#fp_mail").focus();
				$('.loginBox').dialog('close');
				return false;
			});
			
			$('#fp_closeButton,#fpCancel').click(function(){
				$('#forgotPasswordPage').dialog('close');
				$("#fp_mail").val("Email Address");
				$('.loginBox').dialog('open');
				return false;
			});

			$('#1.midBottomCurveX,#2.midBottomCurveX,#3.midBottomCurveX,,#4.midBottomCurveX,#5.midBottomCurveX,#6.midBottomCurveX').click(function(event){
				//debugger;
				event.stopPropagation();
				deleteCity(event.target.id);
			});
			
			$('#x1.midBottomCurveX,#x2.midBottomCurveX').click(function(event){
				//debugger;
				event.stopPropagation();	
				deleteInterests(event.target.id);
			});
			
			$('.topHolidayCartDiv').click(function(event){
				ShowCart();
			});

			// Dialog Link
			$('#changepassword_link').click(function(){
				$('.password').dialog('open');
				return false;
			});
			
			//hover states on the static widgets
			$('#changepassword_link, ul#icons li').hover(
				function() { $(this).addClass('ui-state-hover'); }, 
				function() { $(this).removeClass('ui-state-hover'); }
			);
			
			$('#changepassword_closeText').click(function(){
				$('.password').dialog('close');
				return false;
			});
			
			$('#changepassword_closeimage').click(function(){
				$('.password').dialog('close');
				return false;
			});
			
				
				//Login form validator
				var validator = $("#LoginForm").validate( {
					focusCleanup: true,
					onkeyup: false,
					rules: {
						login_mail: {
							required: true,
							email: true,
							notEqualTo: "Email Address",
							maxlength: 30
						},
						login_password: {
							required: true,
							notEqualTo: "Discover91.com Password",
							minlength: 6,
							maxlength: 14
						}
					},
					messages : {
						login_mail: {
							required: "Invalid Email Address",
							email: "Invalid Email Address",
							notEqualTo: "Invalid Email Address",
							maxlength: "Max 30 characters"
						},
						login_password : "<img id='Valid_Password_Help' src='/images/help_icon.png' class='handCursor passwordHelp genericHelp'/>"
						}
					}
				);
				
				//Registration form validator
				var regValidator = $("#RegistrationForm").validate( {
					focusCleanup: true,
					onkeyup: false,
					rules: {
						registration_email: {
							required: true,
							email: true,
							notEqualTo: "Email Address",
							remote: {
								url: "/customer?page.action=CheckEmail&",
								type: "get"
							},
							maxlength: 30
						},
						registration_password: {
							required: true,
							minlength: 6,
							maxlength: 14,
							notEqualTo: "Password"
						},
						registration_confirmpass: {
							equalTo: "#registration_password",
							required: true,
							notEqualTo: "Confirm Password",
							minlength: 6,
							maxlength: 14
						},
						registration_question: {
							required: true,
							notEqualTo: "Password Recovery Question",
							maxlength: 50
						},
						registration_answer: {
							required: true,
							notEqualTo: "Password Recovery Answer",
							maxlength:50
						},
						registration_acceptTerms: {
							required: true
						},
						registration_captcha: {
							required: true,
							notEqualTo: "Enter verification text",
							maxlength: 23
						}
					},
					messages: {
						registration_email: {
							required: "Please enter your email address",
							email: "Invalid email address. Please retry",
							remote: "This email is already registered",
							maxlength: "Max 30 characters allowed"
						},
						registration_password: {
							required: "Please enter a password",
							notEqualTo: "Please enter a password",
							minlength: "Password must be at least 6 characters long",
							password: "Invalid password.",
							maxlength: "Password can be a maximum of 14 characters"
						},
						registration_confirmpass: "Passwords do not match",
						registration_question: {
							required: "Password recovery question is mandatory",
							notEqualTo: "Password recovery question is mandatory",
							maxlength: "Password recovery question cannot exceed 50 characters"
						},
						registration_answer: {
							required: "Password recovery answer is mandatory",
							notEqualTo: "Password recovery answer is mandatory",
							maxlength: "Password recovery answer cannot exceed 50 characters"
						},
						registration_acceptTerms: "Required",
						registration_captcha: "Required"
						}
				});
				
				
				//Forgot password email validator
				var emailFormValidator = $("#EMailForm").validate( {
					focusCleanup: true,
					onkeyup: false,
					rules: {
						email: {
							required: true,
							email: true,
							notEqualTo: "Email Address",
							maxlength: 30
						} 
					},
					messages : {
						email: "Invalid email address"
						}
				});
				/* */
				
				//Forgot password email validator
				var answerFormValidator = $("#ForgotPasswordForm").validate( {
					focusCleanup: true,
					onkeyup: false,
					rules: {
						forgotpassword_answer: {
							required: true,
							notEqualTo: "Answer",
							maxlength: 50
						} 
					},
					messages : {
/*						required: "Security answer is required",
						notEqualTo: "Security answer is required",
						maxlength: "Security answer cannot exceed 50 characters", */
						forgotpassword_answer: "Security answer is required"
						}
				});
				/* */
				
				// Dialog			
				$('#dialog').dialog({
					autoOpen: false,
					width: 340,
					height: 240,
					modal:true,
					open: function() {
						$("#LoginPopupMsg").text("Please enter your Email address and Password to Login to Discover91.com");
						$('#login_mail').focus();
						validator.resetForm();
						validator.submitted = {};
						$("#login_password").removeClass("error");
					},
					close: function () {
						
						//Reset value of Email field
						$('#login_mail').val('Email Address');
						
						//Replace password field with textbox
						if($("#login_password").attr("type") == "password")
							$("#login_password[type=password]").toggleInputType(false);
						$("#login_password").val($("#login_password").attr("title"));
						
						$("#login_remember").attr("checked", false);
						//Reset validator
						validator.resetForm();
						validator.submitted = {};
						
					} 
					
				});
				
				
				$('#registrationPage').dialog({
					autoOpen: false,
					width: 486,
					height: 630,
					modal:true,
					open: function () {
						$("#registration_email").focus();
					},
					close: function() {
						//Replace password field with textbox
						$("#registration_password[type=password], #registration_confirmpass[type=password]").toggleInputType(false);
						
						$("#registration_email").val("EMail Address");
						$("#registration_question").val("Password Recovery question");
						$("#registration_answer").val("Password Recovery Answer");
						$("#registration_acceptTerms").attr("checked", false);
						$("#registration_newsletter").attr("checked", false);
						$("#registration_captcha").val("Enter verification text");
						/* */
						regValidator.resetForm();
						regValidator.submitted = {};
					} 
					
				});
				
				$('#fpQuestion').dialog({
					autoOpen: false,
					width: 480,
					height: 350,
					modal:true,
					open: function() {
						emailFormValidator.resetForm();
						emailFormValidator.submitted = {};
					},
					close: function () {
						
						//Reset value of Email field
						$("#fp_mail").val("Email Address");
						
						emailFormValidator.resetForm();
						emailFormValidator.submitted = {};
					}
				});
				
				//@Sunil : Send data for registration
				///*
				$('#reg_submitBtn').click(function(){
					if(regValidator.form()){
						$.post("/captcha", $("#RegistrationForm").serialize(), function(data){
							if (data[0].code == "0") {
								$('#registrationPage').dialog('close');
								$.showAlert("Registration Successful", data[0].message);								
							}
							else {
								$.showAlert("Registration Error", data[0].message);
								if(data[0].message=="Invalid verification text"){
									$("#regCaptchaImg").attr("src", "/captcha?ts=" + (new Date().getTime()));
									$("#registration_captcha").focus();
								} else{
									$("#registration_email").focus();
								}
							}
						}, "json");
					}
					return false;
				});
				/* */
				
				//@Sunil : Toggle to password field
				$('#login_password[type=text], #registration_password[type=text], #registration_confirmpass[type=text], #current_password[type=text], #new_password[type=text], #confirm_password[type=text]').live("keyup", function(eventObject) {
				    if(eventObject.keyCode != 9) {
					    if($(this).val() == $(this).attr("title"))
					    {
					        $(this).toggleInputType(true).positionCursor(0);
					    }
					    else
					    {
						    var val = $(this).val();
						    var id = $(this).attr("id");
						    $(this).toggleInputType(true).val(val).positionCursor(val.length);
					    }
					}
					return false;
				});
				
				
				
					$("#LoginUser").click(function() {
					if($("#LoginForm").valid()){
						$.getJSON("/customer?page.action=Login&ts="+ new Date().getTime(), $("#LoginForm").serialize(), function(data){
							if (data[0].code == "0") {
								$('.loginBox').dialog('close');
								if (data[0].target != null && data[0].target != "" ) {
									window.location = data[0].target;
									return false;
								}
								else {
									location.reload(true);
									return false;
								}
							}
							else {
								$.showAlert("Login Error", data[0].message);
								$("#login_mail").focus();
							}
						});
					}
					return false;
				});

				//@Sunil : Logout
				$("#SignOut").click(function() {
					$.getJSON("/customer?page.action=LogOut&ts="+ new Date().getTime(), "", function(data){
							if (data[0].code == "0") {
								location.reload(true);
							}
							else {
								$.showAlert("Signout Error", data[0].message);
							}
						});
				});

				//@Sunil : Change captcha image
				$("#regChangeImageText").click( function() {
					$("#regCaptchaImg").attr("src", "/captcha?ts=" + (new Date().getTime()));
				});
				
						
				$('#fpsubmit').click(function(){
					if(emailFormValidator.form()){
						$.getJSON("/customer", "&page.action=ForgotPassword&forgotpassword_mail=" + encodeURIComponent($("#fp_mail").val())+"&ts="+ new Date().getTime(),
							function(data){
								if (data[0].code == "0") {
									$('#fpQuestion').dialog('open');
									$("#forgotpassword_mail").val($("#fp_mail").val());
									$("#forgotpassword_question").val(data[0].message);
									$("#fpUserQuestion").text(data[0].message);
									$("#forgotpassword_answer").focus();
									$('#forgotPasswordPage').dialog('close');
								}
								else {
									$.showAlert("Forgot Password", data[0].message);
									$("#fp_mail").val("Email Address");
								}
								
							});
					}
					return false;
				});
				
				$('#fpQuestionCancel,#fpQuestionClose').click(function(){
					$('#fpQuestion').dialog('close');
					$('#forgotPasswordPage').dialog('open');			
					$("#forgotpassword_answer").val("Answer");
					return false;
				});
				
				$('#fpQuestionSubmit').click(function(){
					if(answerFormValidator.form()){
						$.getJSON("/customer?page.action=ForgotPasswordSubmit&ts="+ new Date().getTime(), $("#ForgotPasswordForm").serialize(), function(data){
							if (data[0].code == "0") {
								$('#fpQuestion').dialog('close');
								$.showAlert("Forgot Password", data[0].message);								
							}
							else {
								$.showAlert("Error", data[0].message);
								$("#forgotpassword_answer").focus();
							}
						});
					}
					return false;
				});
				
				//@Sunil : Set URL to current page
				$('#targetURL').val(window.location);
				
				//@Sunil : Initialize validator for change password form
				var cpValidator = $("#ChangePasswordForm").validate( {
					focusCleanup: true,
					onkeyup: false,
					rules: {
						current_password:{
							required: true,
							notEqualTo: "Current Password",
							minlength: 6,
							maxlength: 14
						},
						new_password: {
							required: true,
							notEqualTo: "New Password",
							minlength: 6,
							maxlength: 14
						},
						confirm_password: {
							equalTo : "#new_password"
						}
					},
					messages : {
						current_password: "Current Password",
						new_password: "New Password (6 - 14 characters)",
						confirm_password: "Passwords dont match"
					}
				});
							
				// Dialog			
				$('#changepassword').dialog({
					autoOpen: false,
					width: 340,
					height: 240,
					modal:true,
					open: function() {
						
					},
					close: function () {

						$('#current_password[type=password], #new_password[type=password], #confirm_password[type=password]').each(function() {$(this).toggleInputType(false);});
						
						cpValidator.resetForm();
						cpValidator.submitted = {};
					}
				});
				
				//@Sunil : Check password field before submit
				$('#ChangePasswordForm').bind('submit', function() {
					if(cpValidator.form()){
						$.post("/customer?page.action=ChangePassword&ts="+ new Date().getTime(), $("#ChangePasswordForm").serialize(), function(data){
							if (data[0].code == "0") {
								$('#changepassword').dialog('close');
								$.showAlert("Change Password", data[0].message);								
							}
							else {
								$.showAlert("Change Password", data[0].message);
								$("#current_password").focus();
							}
						}, "json");
					}
					return false;
				});

				$(".passwordHelp").live("click", function(event) {
					$.showHelp(event.target.id);
					return false;
				});

								
					/*Customer contact form*/
				$('#CustomerContactDialog').dialog({
					autoOpen: false,
					width: 595,
					height:	360,
					resizable: false,
					modal:true
				});
					
		    	$('.CustomerDetailsDialogClose').bind('click',function(){
					$("#CustomerContactDialog").dialog('close');
					return false;
				});

				var	validator =	$("#formCustomerDetails").validate(	{
					focusCleanup: true,
					onkeyup: false,
					rules: {		
					  customer_name: {
						required: true,
						notEqualTo:	"Name",
						maxlength :30
						},
					  customer_email: {
						required: true,
						notEqualTo:	"Email Address",
						email: true,
						maxlength:50
						},
					  customer_details_captcha:{
							required: true,
							notEqualTo:"Enter verification text",
							maxlength:23
						}
							
					}, 
					messages: {	
					  customer_name: {
						required: "Name	is mandatory",
						notEqualTo:	"Name is mandatory",
						maxlength:"Max 30 Characters"
						 },
					  customer_email: {
						required: "Email is	mandatory",
						notEqualTo:	"Email is mandatory",
						email:"Invalid Email",
						maxlength:"Max 50 Characters"
						 },
					 customer_details_captcha:{
							 required: "Text is mandatory",
							 notEqualTo:"Text is mandatory",	
							 maxlength:"Text is too long"
						 }	
					}
				});	
			$('#customer_details_ChangeImageText').click(function() {
				$("#custDetailsCaptchaImg").attr("src",	"/captcha?ts=" +	(new Date().getTime()));
			});

			$('#customer_details_submit').bind('click',	function() {
					if($("#formCustomerDetails").valid()==false	){
							return false;
					}else if($("#hidden_customer_plan_type").val()=="create_holiday"){
							$("#hp_customer_name").val($("#customer_name").val());
							$("#hp_customer_email").val($("#customer_email").val());
					
							var	promo_code = $("#customer_promo_code").val();
							if(promo_code != null && promo_code	!= "" && promo_code	!="Discount Code"){
							$("#hp_customer_promo_code").val($("#customer_promo_code").val());
							}else{
								$("#hp_customer_promo_code").val("");
							}
							
							var	contact_no = $("#customer_contact_number").val();
							if(contact_no != null && contact_no	!= "" && contact_no	!="Contact Number"){
								$("#hp_customer_contact_no").val(contact_no);
							} else {
								$("#hp_customer_contact_no").val("");
							}
							
							var	post_url="/makeMyHoliday?page.action=customHoliday&customer_details_captcha="+$("#customer_details_captcha").val();
							$.post(post_url+"&ts="+ new Date().getTime(), $("#BPParametersForm").serialize(), function(data){
								if (data[0].code == "0") {
									$('#CustomerContactDialog').dialog('close');
									$.showAlert("Thank you", "Thank	you	for	your interest. We will get back to you in 48 hours.");
								} else if (data[0].code=="-1") {
									$("#custDetailsCaptchaImg").attr("src",	"/captcha?ts=" +	(new Date().getTime()));
									$.showAlert("Error-Send us your details", "Oops!! Server encountered a problem processing your request. Pleas retry again.");
									$("#customer_name").focus();
								} else if (data[0].code=="2") {
									$.showAlert("Error-Send us your details", data[0].message);
									$("#custDetailsCaptchaImg").attr("src",	"/captcha?ts=" +	(new Date().getTime()));
									$("#customer_details_captcha").focus();
								}
							}, "json");
							return false;
					}else {
							var	paln_type =	$("#hidden_customer_plan_type").val();
							var	post_url="/makeMyHoliday?page.action=experienceInterest";
							if(paln_type =="holiday"){
								 post_url="/makeMyHoliday?page.action=holidayInterest";
							}
							post_url = post_url+"&hp_customer_email="+$("#customer_email").val();
							post_url = post_url+"&hp_plan_name="+$("#hidden_customer_plan_name").val();
							post_url = post_url+"&customer_details_captcha="+$("#customer_details_captcha").val();
							post_url = post_url+"&hp_customer_name="+$("#customer_name").val();

							var	promo_code = $("#customer_promo_code").val();
							if(promo_code != null && promo_code	!= "" && promo_code	!="Discount Code"){
							post_url = post_url+"&hp_customer_promo_code="+promo_code;
							}
											
							var	contact_no = $("#customer_contact_number").val();
							if(contact_no != null && contact_no	!= "" && contact_no	!="Contact Number"){
								post_url = post_url+"&hp_customer_contact_no="+contact_no;
							}
							
							$.post(post_url+"&ts="+ new Date().getTime(),"", function(data){
							if (data[0].code == "0") {
								$('#CustomerContactDialog').dialog('close');
								if(paln_type =="holiday") {
									$.showAlert("Thank you", "Thank	you	for	your interest in the holiday "+$("#hidden_customer_plan_name").val()+" . We	will get back to you in	48 hours.");							
								} else {
									$.showAlert("Thank	you", "Thank you for your interest in the experience "+$("#hidden_customer_plan_name").val()+" . We	will get back to you in	48 hours.");								
								}
							} else if (data[0].code=="-1") {
								$("#custDetailsCaptchaImg").attr("src",	"/captcha?ts=" +	(new Date().getTime()));
								$.showAlert("Error-Send us your details", "Oops!! Server encountered a problem processing your request. Pleas retry again.");
								$("#customer_name").focus();
								
							} else if (data[0].code=="2") {
								$.showAlert("Error-Send us your details", data[0].message);
								$("#custDetailsCaptchaImg").attr("src",	"/captcha?ts=" +	(new Date().getTime()));
								$("#customer_details_captcha").focus();
							}
								}, "json");
						}
									
					return false;
			
				});	
			$(".imgEDLAction").click(function() {
				$(".imgEDLAction").removeClass("img-tabs-selected");
				$(this).removeClass("img-tabs-no-border");
				$(this).addClass("img-tabs-selected");
				$(".divHiddenDescShow").html("");
				$(".divHiddenDescShow").html($(".hiddenLocDesc", $(this).closest("div")).val());
				$(".descHiddenHedding").html($(this).attr("alt"));
				var locId=  $(".hiddenLocId", $(this).closest("div")).val();
				var locType= $(".hiddenLocType", $(this).closest("div")).val(); 
				var linkVal ="/holiday/destinations/" + locId + "/";
				if(locType==7) {
					linkVal = "/holiday/attractions/" + locId+ "/"+ "#"+locId;
				}
				var htmlMoreLink = " <a target='_new' style='color:#2070CA;font-weight:bold;' href='" + linkVal + "'>more</a>";
				$(".descHiddenHedding").attr("href", linkVal);
				$(".divHiddenDescShow").html($(".divHiddenDescShow").html()+htmlMoreLink);
				$('#locDescDetails').show();
				});
			});
			
				
		function deleteCity(value)
		{
			 
			 var parentDiv = document.getElementById('cityChild');
			 var olddiv = document.getElementById("div_"+value);
			 parentDiv.removeChild(olddiv);
		}
		
		function deleteInterests(value)
		{
			 var parentDiv = document.getElementById('intChild');
			 var olddiv = document.getElementById("int"+value);
			 parentDiv.removeChild(olddiv);
		}
		
		function clearAllCities()
		{
			var parentDiv = $('#citiesName');
			var children=parentDiv.children(); 
			parentDiv[0].removeChild(children[0]);	 
			 
			 var parentDiv = $('#travelInterests');
			 var children=parentDiv.children(); 
			 parentDiv[0].removeChild(children[0]);
		}
			$(function(){
				
				$("#planHolidayNo_days").sexyCombo({emptyText: "", autoFill: true, skin: "customCartPlan", triggerSelected: true});	
				$("#planHolidayStartDate").datepicker({showOn: 'both', buttonImage: '/images/calendar_icon.png', buttonImageOnly: true, dateFormat: 'M d yy', minDate: "3"});
				$('.holidaySlider').cycle({ 
					fx:     'fade', 
					speed: 'slow',
					timeout: 0, 
					next:   '#nextHoliday', 
					prev:   '#prevHoliday' 
				});
				$('.expsSlider').cycle({ 
					fx:     'fade', 
					speed: 'slow',
					timeout: 0, 
					next:   '#nextExps', 
					prev:   '#prevExps' 
				});
				var dt = new Date((new Date()).valueOf() + (1000 * 60 * 60 * 24 * 3));
				$("#planHolidayStartDate").val(dt.toDateString("mmm").substr(4));
			});

		
