var MyHoneyMoon = {
	HomePageWidget : {		
		Start:function( argSlideSpeed , argSlideFadeSpeed ) {
			new Protofade("bannerSlides" , { Delay:argSlideSpeed,Duration:argSlideFadeSpeed });
		}
	},
	Couple : {
		Options : {
			DoToggle:function(objType,id,checked) {
				var url = "index.cfm?action=Couple_Privacy&id="+id+"&objType="+objType+"&checked="+checked;
				new Ajax.Request( url, {asynchronous:true, onSuccess:MyHoneyMoon.Couple.Options.DoToggle_onSuccess, onFailure:errFunc});
			},
			DoToggle_onSuccess:function(t) {
				z = parseJSON( t.responseText );
			},
			Toggle : {
				Addition:function(obj,id) {
					var checked = obj.checked;
					if( !obj.checked ) {
						var confirmation = confirm("Are you sure you want to set this addition as private?\n\nGuests will not be able to see this when they log in");
					} else {
						var confirmation = true;
					}
					if( confirmation ) {
						$("additions_row_" + id).toggleClassName("private");
						MyHoneyMoon.Couple.Options.DoToggle( "addition", id , checked );
					} else {
						obj.checked=!checked;
					}
				},
				Excursion:function(obj,id) {
					var checked = obj.checked;
					if( !obj.checked ) {
						var confirmation = confirm("Are you sure you want to set this excursion as private?\n\nGuests will not be able to see this when they log in");
					} else {
						var confirmation = true;
					}
					if( confirmation ) {
						$("excursions_row_" + id).toggleClassName("private");
						MyHoneyMoon.Couple.Options.DoToggle( "excursion", id , checked );
					} else {
						obj.checked=!checked;
					}
				},
				Hotel:function(obj,id) {
					var checked = obj.checked;
					if( !obj.checked ) {
						var confirmation = confirm("Are you sure you want to set this hotel as private?\n\nGuests will not be able to see this when they log in");
					} else {
						var confirmation = true;
					}
					if( confirmation ) {
						$("hotels_row_" + id).toggleClassName("private");
						MyHoneyMoon.Couple.Options.DoToggle( "hotel", id , checked );
					} else {
						obj.checked=!checked;
					}
				},
				Destination:function(obj,id) {
					var checked = obj.checked;
					if( !obj.checked ) {
						var confirmation = confirm("Are you sure you want to set this destination as private?\n\nGuests will not be able to see this when they log in");
					} else {
						var confirmation = true;
					}
					if( confirmation ) {
						$("destinations_row_" + id).toggleClassName("private");
						MyHoneyMoon.Couple.Options.DoToggle( "destination", id , checked );				
						if( $("hotels_" + id ) ) {
							hotelTableObj = $("hotels_" + id );
							var hotelObjs = hotelTableObj.select("input");
							hotelObjs.each(
								function(name,index) {
									name.checked = checked;
									name.disabled=!checked;
								}
							);
							var hotelIndicatorObjs = hotelTableObj.select(".indicator");
							hotelIndicatorObjs.each(
								function(name,index) {
									name.toggleClassName("private");
								}
							);
						}
						
						if( $("excursions_" + id ) ) {
							excursionTableObj = $("excursions_" + id );
							var excursionObjs = excursionTableObj.select("input");
							excursionObjs.each(
								function(name,index) {
									name.checked = checked;
									name.disabled=!checked;
								}
							);
							var excursionIndicatorObjs = excursionTableObj.select(".indicator");
							excursionIndicatorObjs.each(
								function(name,index) {
									name.toggleClassName("private");
								}
							);
						}
					} else {
						obj.checked=!checked;
					}					
				} 
			}
		}
	},
	Forms : {
		HAL:function(id) {
				new Effect.Highlight(id);
				$(id).focus();
		},
		Contact : {
			Validate:function() {
								
				if( $("firstname").value.blank() ) {
					MyHoneyMoon.Forms.HAL("firstname");
					return false;
				}
				if( $("lastname").value.blank() ) {
					MyHoneyMoon.Forms.HAL("lastname");
					return false;
				}
				if( $("emailaddress").value.blank() ) {
					MyHoneyMoon.Forms.HAL("emailaddress");
					return false;
				}
				if( $("telephone").value.blank() ) {
					MyHoneyMoon.Forms.HAL("telephone");
					return false;
				}
				if( $("budget").value.blank() ) {
					MyHoneyMoon.Forms.HAL("budget");
					return false;
				}
				if ( isNaN( Number( $("budget").value ) ) ) {
					MyHoneyMoon.Forms.HAL("budget");
					alert("Please enter a budget. Use numbers only please.");
					return false;
				}
				if( Number( $("budget").value ) == 0 ) {
					MyHoneyMoon.Forms.HAL("budget");
					alert("Please enter a budget. Use numbers only please.");
					return false;
				}
				if( $("departureAirport").value.blank() ) {
					MyHoneyMoon.Forms.HAL("departureAirport");
					return false;
				}
				if( $("enquiry").value.blank() ) {
					MyHoneyMoon.Forms.HAL("enquiry");
					return false;
				}
				return true;
			}
		},
		Couple : {
			Login : {
				Validate:function() {
					if( $("mobileNumber").value.blank() || !isNumeric( $("mobileNumber").value ) ) {
						MyHoneyMoon.Forms.HAL("mobileNumber");
						return false;
					}
					if( $("pinNumber").value.blank() ) {
						MyHoneyMoon.Forms.HAL("pinNumber");
						return false;
					}
					return true;
				}
			},
			Account : {
				Details : {
					Bride : {
						Validate:function() {
							if( $("coupleBrideFirstname").value.blank() ) {
								MyHoneyMoon.Forms.HAL("coupleBrideFirstname");
								return false;
							}
							if( $("coupleBrideLastname").value.blank() ) {
								MyHoneyMoon.Forms.HAL("coupleBrideLastname");
								return false;
							}
							if( $("coupleBrideMobileNumber").value.blank() || !isNumeric( $("coupleBrideMobileNumber").value ) ) {
								MyHoneyMoon.Forms.HAL("coupleBrideMobileNumber");
								return false;
							}
							if( $("coupleBrideEmailAddress").value.blank() ) {
								MyHoneyMoon.Forms.HAL("coupleBrideEmailAddress");
								return false;
							}
							if( $("couplePIN").value.blank() || !isNumeric( $("couplePIN").value ) ) {
								MyHoneyMoon.Forms.HAL("couplePIN");
								return false;
							}
							return true;
						}
					},
					Groom : {
						Validate:function() {
							if( $("coupleGroomFirstname").value.blank() ) {
								MyHoneyMoon.Forms.HAL("coupleGroomFirstname");
								return false;
							}
							if( $("coupleGroomLastname").value.blank() ) {
								MyHoneyMoon.Forms.HAL("coupleGroomLastname");
								return false;
							}
							if( $("coupleGroomMobileNumber").value.blank() || !isNumeric( $("coupleGroomMobileNumber").value ) ) {
								MyHoneyMoon.Forms.HAL("coupleGroomMobileNumber");
								return false;
							}
							if( $("coupleGroomEmailAddress").value.blank() ) {
								MyHoneyMoon.Forms.HAL("coupleGroomEmailAddress");
								return false;
							}
							if( $("couplePIN").value.blank() || !isNumeric( $("couplePIN").value ) ) {
								MyHoneyMoon.Forms.HAL("couplePIN");
								return false;
							}
							return true;
						}
					},
					Address : {
						Validate:function() {
							if( $("coupleAddress1").value.blank() ) {
								MyHoneyMoon.Forms.HAL("coupleAddress1");
								return false;
							}
							if( $("coupleAddressCity").value.blank() ) {
								MyHoneyMoon.Forms.HAL("coupleAddressCity");
								return false;							}
							return true;
						}
					}
					
				}
			}
		},
		Guest : {
			Payment : {
				Contribute : {
					Validate:function() {
						if( $("giftTypeId").value==-1 ) {								
							alert("Please tell us what type of gift you would like to purchase");
							MyHoneyMoon.Forms.HAL("giftTypeId");
							return false;
						}
						if( !$("agreeTC").checked ) {								
							alert("You must agree to the Terms & Conditions before purchasing this gift");
							MyHoneyMoon.Forms.HAL("agreeTC");
							return false;
						}
						return true;
					}
				}				
			},
			Account : {
				Details : {
					ChangeCountry:function() {
						if( $("guestMobileCountry").value == "353" ) {
							$("guestMobilePrefix353").show();
							$("guestMobilePrefix44").hide();
						} else {
							$("guestMobilePrefix353").hide();
							$("guestMobilePrefix44").show();
						}
					},
					Validate:function() {
						if( $("guestFirstname").value.blank() ) {
							MyHoneyMoon.Forms.HAL("guestFirstname");
							return false;
						}
						if( $("guestLastname").value.blank() ) {
							MyHoneyMoon.Forms.HAL("guestLastname");
							return false;
						}
						if( $("guestEmailAddress").value.blank() ) {
							MyHoneyMoon.Forms.HAL("guestEmailAddress");
							return false;
						}
						if( $("guestMobileNumber").value.blank() || !isNumeric( $("guestMobileNumber").value ) ) {
							MyHoneyMoon.Forms.HAL("guestMobileNumber");
							return false;
						}
						if( $("guestPIN").value.blank() ) {
							MyHoneyMoon.Forms.HAL("guestPIN");
							return false;
						}
						if( $("guestAddress1").value.blank() ) {
							MyHoneyMoon.Forms.HAL("guestAddress1");
							return false;
						}
						if( $("guestCity").value.blank() ) {
							MyHoneyMoon.Forms.HAL("guestCity");
							return false;
						}
						return true;
					}
				}
			},
			Step1 : {
				Validate:function() {
					if( $("accessCode").value.blank() ) {
						MyHoneyMoon.Forms.HAL("accessCode");
						return false;
					}
					return true;
				}
			},
			Step2 : {
				ChangeCountry:function() {
					if( $("mobileCountry").value == "353" ) {
						$("mobilePrefix353").show();
						$("mobilePrefix44").hide();
					} else {
						$("mobilePrefix353").hide();
						$("mobilePrefix44").show();
					}
				},
				Cancel:function() {
					$("guestLogin_Step2_ChoiceHolder").show();
					$("existingUserFormHolder").hide();
					$("newUserFormHolder").hide();
				},
				Choose:function(action) {
					switch(action) {
						case "NEW":
							$("guestLogin_Step2_ChoiceHolder").hide();
							$("newUserFormHolder").show();
							$("existingUserFormHolder").hide();
							$("newGuestFirstname").focus();
						break;
						case "EXISTING":
							$("guestLogin_Step2_ChoiceHolder").hide();
							$("existingUserFormHolder").show();
							$("newUserFormHolder").hide();
							$("emailAddress").focus();
						break;
					}
				},
				ChangeLoginType:function() {					
					if( $("loginType").value=="EMAIL" ) {
						$("emailHolder").show();
						$("mobileHolder").hide();
						$("emailAddress").focus();
					} else {
						$("mobileHolder").show();
						$("emailHolder").hide();
						$("mobileNumber").focus();
					}
				},
				New : {
					ChangeCountry:function() {
						if( $("newGuestMobileCountry").value == "353" ) {
							$("newGuestMobilePrefix353").show();
							$("newGuestMobilePrefix44").hide();
						} else {
							$("newGuestMobilePrefix353").hide();
							$("newGuestMobilePrefix44").show();
						}
					},
					Validate:function() {
						if( $("newGuestFirstname").value.blank() ) {
							MyHoneyMoon.Forms.HAL("newGuestFirstname");
							return false;
						}
						if( $("newGuestLastname").value.blank() ) {
							MyHoneyMoon.Forms.HAL("newGuestLastname");
							return false;
						}
						if( $("newGuestEmailAddress").value.blank() ) {
							MyHoneyMoon.Forms.HAL("newGuestEmailAddress");
							return false;
						}
						if( $("newGuestMobileNumber").value.blank() || !isNumeric( $("newGuestMobileNumber").value ) ) {
							MyHoneyMoon.Forms.HAL("newGuestMobileNumber");
							return false;
						}
						if( $("newGuestAddress1").value.blank() ) {
							MyHoneyMoon.Forms.HAL("newGuestAddress1");
							return false;
						}
						if( $("newGuestCity").value.blank() ) {
							MyHoneyMoon.Forms.HAL("newGuestCity");
							return false;
						}
						return true;
					}
				},
				Existing : {
					Validate:function() {						
						if( $("loginType").value == "EMAIL" && $("emailAddress").value.blank() ) {
							MyHoneyMoon.Forms.HAL("emailAddress");
							return false;
						} 
							
						if( $("loginType").value == "MOBILE" ) {
							if( $("mobileNumber").value.blank() || !isNumeric( $("mobileNumber").value ) ) {
								MyHoneyMoon.Forms.HAL("mobileNumber");
								return false;
							}
						}
						
						if( $("pinNumber").value.blank() ) {
							MyHoneyMoon.Forms.HAL("pinNumber");
							return false;
						}

						return true;
					}
				}
			}
		}
	}, 
	Homepage : {
		Forms : {
			CoupleLogin : {
				MobileHint:"mobile number",
				PasswordHint:"pin number",
				ClickMobile:function() {
					if( $("mobileNumber").value==MyHoneyMoon.Homepage.Forms.CoupleLogin.MobileHint ) {
						$("mobileNumber").value = "";
					}
				},
				BlurMobile:function() {
					if( $("mobileNumber").value.blank() ) {
						$("mobileNumber").value = MyHoneyMoon.Homepage.Forms.CoupleLogin.MobileHint;
					}
				},
				ClickPassword:function() {
					if( $("password").value==MyHoneyMoon.Homepage.Forms.CoupleLogin.PasswordHint ) {
						$("password").value = "";
					}
				},
				BlurPassword:function() {
					if( $("password").value.blank() ) {
						$("password").value = MyHoneyMoon.Homepage.Forms.CoupleLogin.PasswordHint;
					}
				},
				Validate:function() {
					if( $("mobileNumber").value.blank() || $("mobileNumber").value==MyHoneyMoon.Homepage.Forms.CoupleLogin.MobileHint ) {
						MyHoneyMoon.Homepage.Forms.HAL("mobileNumber");
						return false;
					}
					if( $("password").value.blank() || $("password").value==MyHoneyMoon.Homepage.Forms.CoupleLogin.PasswordHint) {
						MyHoneyMoon.Homepage.Forms.HAL("password");
						return false;
					}
					return true;
				}
			},
			GuestLogin : {
				AccessCodeHint:"accessCode",
				ClickAccessCode:function() {
					if( $("accessCode").value==MyHoneyMoon.Homepage.Forms.GuestLogin.AccessCodeHint ) {
						$("accessCode").value = "";
					}
				},
				BlurAccessCode:function() {
					if( $("accessCode").value.blank() ) {
						$("accessCode").value = MyHoneyMoon.Homepage.Forms.GuestLogin.AccessCodeHint;
					}
				},
				Validate:function() {
					if( $("accessCode").value.blank() || $("accessCode").value==MyHoneyMoon.Homepage.Forms.GuestLogin.AccessCodeHint ) {
						MyHoneyMoon.Homepage.Forms.HAL("accessCode");
						return false;
					}
					return true;
				}
			},
			HAL:function(id) {
				new Effect.Highlight(id);
				$(id).focus();
			}
		}		
	}	
}

var Protofade = Class.create({

	initialize: function(element, options) {		
		this.options = {
      		Duration: 1,
			Delay: 4.0,
			Random: false,
			Slideshow:true,
			Controls:false
    	}
		Object.extend(this.options, options || {});

    	this.element        = $(element);
		this.slides			= this.element.childElements();
		this.num_slides		= this.slides.length;		
		this.current_slide 	= (this.options.Random) ? (Math.floor(Math.random()*this.num_slides)) : 0;
		this.end_slide		= this.num_slides - 1;
		
		this.slides.invoke('hide');
		this.slides[this.current_slide].show();
				
		if (this.options.Slideshow) { 
			this.startSlideshow();
		}				
		if (this.options.Controls) {
			this.addControls();
		}		
	},
	
	addControls: function() {
		this.wrapper 		= this.element.up();
		this.controls		= new Element('div', { 'class': 'controls' });
		this.wrapper.insert(this.controls);
		
		this.btn_next 		= new Element('a', { 'class': 'next', 'title': 'Next', href: '#' }).update('Next');
		this.btn_previous	= new Element('a', { 'class': 'previous', 'title': 'Previous', href: '#' }).update('Previous');
		this.btn_start		= new Element('a', { 'class': 'start', 'title': 'Start', href: '#' }).update('Start');
		this.btn_stop		= new Element('a', { 'class': 'stop', 'title': 'Stop', href: '#' }).update('Stop');
		
		this.btns = [this.btn_next, this.btn_previous, this.btn_start, this.btn_stop];
		this.btns.each(function(el){
			this.controls.insert(el);
		}.bind(this));
		
		this.btn_previous.observe('click', this.moveToPrevious.bindAsEventListener(this));
		this.btn_next.observe('click', this.moveToNext.bindAsEventListener(this));
		this.btn_start.observe('click', this.startSlideshow.bindAsEventListener(this));
		this.btn_stop.observe('click', this.stopSlideshow.bindAsEventListener(this));
	},

	startSlideshow: function(event) {
		if (event) { Event.stop(event); }
		if (!this.running)	{
			this.executer = new PeriodicalExecuter(function(){
	  			this.updateSlide(this.current_slide+1);
	 		}.bind(this),this.options.Delay);
			this.running=true;
		}
	},
	
	stopSlideshow: function(event) {	
		if (event) { Event.stop(event); } 
		if (this.executer) { 
			this.executer.stop();
			this.running=false;
		}	 
	},

	moveToPrevious: function (event) {
		if (event) { Event.stop(event); }
		this.stopSlideshow();
  		this.updateSlide(this.current_slide-1);
	},

	moveToNext: function (event) {
		if (event) { Event.stop(event); }
		this.stopSlideshow();
  		this.updateSlide(this.current_slide+1);
	},
	
	updateSlide: function(next_slide) {
		
		if (next_slide > this.end_slide) { 
				next_slide = 0; 
		} 
		else if ( next_slide == -1 ) {
				next_slide = this.end_slide;
		}
		
		this.fadeInOut(next_slide, this.current_slide);		
	},

 	fadeInOut: function (next, current) {		
		this.slides[current].setStyle({"zIndex":1});
		this.slides[next].setStyle({"zIndex":100});
		new Effect.Parallel([
			new Effect.Fade(this.slides[current], { sync: true }),
			new Effect.Appear(this.slides[next], { sync: true }) 
  		], { duration: this.options.Duration });
		
		this.current_slide = next;		
	}

});

String.prototype.isEmail = function () { 
	var rx = new RegExp("\\w+([-+.\’]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*"); 
	var matches = rx.exec(this); 
	return (matches != null && this == matches[0]); 
}

function isNumeric(x) { 
	var y=parseInt(x); 
	if (isNaN(y)) {
	   return false; 
	} else {
		return true;
	}
} 

function LoadingAjax(txt) {
	if(typeof(txt)=="undefined") {
		txt = "Loading...";
	}
	return "<div class='ajaxMsg' id='widgetAjaxMsg'> "+txt+"</div>";
}

function parseJSON( json ){
	var o = eval('(' + json + ')');
	if( o.redirectURL )
	{
		document.location = o.redirectURL;
		return;
	}
	if( $("message") != null ) new Element.remove( "message" );
	if( o.errMsg )
	{
		if( o.errMsgTitle == null ) o.errMsgTitle = "Error";
		if( o.errMsgTimer == null ) o.errMsgTimer = 5000;
		tw.ShowMessage( o.errMsgTitle, o.errMsg, "error", o.errMsgTimer, o.errMsgPosition );
		return o;
	}
	if( o.msgTitle || o.msg )
	{
		if( o.msgTimer == null ) o.msgTimer = 5000;
		tw.ShowMessage( o.msgTitle, o.msg, o.msgClass, o.msgTimer, o.msgPosition );
	}
	return o;
}

var errFunc = function(t) {
	var win = window.open("", "win", "width=1024,height=700,resizable=yes,scrollbars=yes,status=no"); // a window object
	win.document.open("text/html", "replace");
	win.document.write( "<html><body style='margin:0'><div style='border-bottom:1px solid #222;background:#666;padding:10px;'><h1 style='color:#FFF;margin:0;padding:0;'>Digital Crew Ajax Error</h1></div><div style='padding:10px;'>"+t.responseText.replace(/^\s+|\s+$/, '') + "</div></body></html>" );
	win.document.close();
	win.focus();
}
