$(function() {
	$('#room-data').hide();
	$('.rates-dates, #rates-package').tabs();	
	$('.rates-table th a').click(function() {
		var href = $(this).attr('href');
		var change = $(this).parents('.rates-content').prevAll('.room-image');
		$(change).html($(href).html());
		return false;
	});
	
	$('.room-content .included').each(function() {
		var obj = $(this);
		var toggle = $('<span />').addClass('toggle').appendTo(obj);
		var control = $('<a />').attr('href', '#')
						.addClass('toggler')
						.html(' ...More')
						.appendTo(toggle)
						.toggle(function() { $('.include-extra', obj).css('display', 'inline'); $(this).html(' ...Less').addClass('open'); },
								function() { $('.include-extra', obj).hide(); $(this).html(' ...More').removeClass('open'); });
		$('.include-extra', obj).hide();
	});

	$('.extra-friend').hide();
	$('#add-friend').click(function() {
		var hidden = $('.extra-friend:hidden');	
		if(hidden.length)
			$(hidden[0]).show();
	});
	
	$('.home #flash_push').flash(
		{src:"flash/halfmoon-masthead3.swf",
		 width:380,
		 height:479,
		 wmode:"transparent",
		 quality:"high"},
		 {version:"8"});
	
    $('#epostcard-gallery').flash(
        {src:"/flash/photo_gallery.swf",
         width:500,
         height:400,
         wmode:"transparent",
         quality:"high"},
         {version:"9"},
            function(htmlOptions) { //Use this to specify a query string, take out if not needed.
                htmlOptions.flashvars.epostConfigXML = '/flash/xml/epostConfig.xml';
                htmlOptions.flashvars.imagesXML = '/flash/xml/villa-images.xml';
                htmlOptions.flashvars.langXML= 'flash/xml/i18n/en_formLabels.xml';
                htmlOptions.flashvars.browser = BROWSER;  //Used for SSP Postcard feature, comment out if not needed.
                $(this).html($.fn.flash.transform(htmlOptions));
        });
    
	$('#res_console').each(function() {
		$('.code input', this).each(function() {
			var val = this.value;
			$(this).focus(function() {
				var val2 = this.value;
				if(val2=='group code'||val2=='promo code')
					$(this).val('');
			}).blur(function() {
				if(this.value=='')
					$(this).val(val);
			});
		});
		$(this).submit(function() {
			$('.code input', this).each(function() {
				var val = this.value;
				if(val=='group code'||val=='promo code')
					$(this).val('');
			});
		});
	});

	$('#prereg_form .submit').click(function(ev) {
		if($(this).is('#submit'))
			$(this).parents('form').attr('action','prereg-process.php');
		else
			$(this).parents('form').attr('action','preferences.php');
	});
	
	$('#how_learned').change(function() {
		$('#heard_other, #other_name').hide().find('input').val('');
		if($(this).val()==99)
			$('#heard_other').show();
		if($(this).val()==22)
			$('#other_name').show();
	});
	
	$('input[name=codetype]').click(function() {
		if($(this).val()==1)
			$('#iatan-other').show();
		else
			$('#iatan-other').hide();
	});
	
	$("table#room-rates thead tr th:nth-child(even)").addClass("alt_thead_even");
	$("table#room-rates tbody tr:nth-child(even) td:nth-child(even)").addClass("alt_single_even");
	$("table#room-rates tbody tr:nth-child(even) td:nth-child(odd)").addClass("alt_double_even");
	$("table#room-rates tbody tr:nth-child(odd) td:nth-child(odd)").addClass("alt_double_odd");
	$("table#room-rates tbody tr:nth-child(odd) td:nth-child(even)").addClass("alt_single_odd");
	$("table#room-rates tbody tr:nth-child(even) th").addClass("alt_even");
	$("table#meal-plans tbody tr td:nth-child(even)").addClass("alt-cell");
	
	
		//styles for send-to-friend.php form -- Add a Friend
	$('select#restaurant').change( function() {
		switch($(this).val()){
			case 'Seagrape Terrace':
				$('form select#time').html('<option value="7:00 PM">7:00 PM</option>\n\
					<option value="7:30 PM">7:30 PM</option>\n\
					<option value="8:00 PM">8:00 PM</option>\n\
					<option value="8:30 PM">8:30 PM</option>\n\
					<option value="9:00 PM">9:00 PM</option>\n\
					<option value="9:30 PM">9:30 PM</option>\n\
					<option selected="selected" value="">Select a time</option>');
			  	break;    
			case 'Il Giardino':
			 	$('form select#time').html('<option value="6:30 PM">6:30 PM</option>\n\
					<option value="7:00 PM">7:00 PM</option>\n\
					<option value="7:30 PM">7:30 PM</option>\n\
					<option value="8:00 PM">8:00 PM</option>\n\
					<option value="8:30 PM">8:30 PM</option>\n\
					<option value="9:00 PM">9:00 PM</option>\n\
					<option selected="selected" value="">Select a time</option>');			  
			  break;
			case 'The Sugar Mill':
			 	$('form select#time').html('<option value="6:30 PM">6:30 PM</option>\n\
					<option value="7:00 PM">7:00 PM</option>\n\
					<option value="7:30 PM">7:30 PM</option>\n\
					<option value="8:00 PM">8:00 PM</option>\n\
					<option value="8:30 PM">8:30 PM</option>\n\
					<option value="9:00 PM">9:00 PM</option>\n\
					<option selected="selected" value="">Select a time</option>');		
				break;
			case 'The Akbar Restaurant':
				$('form select#time').html('<option value="7:00 PM">7:00 PM</option>\n\
					<option value="7:30 PM">7:30 PM</option>\n\
					<option value="8:00 PM">8:00 PM</option>\n\
					<option value="8:30 PM">8:30 PM</option>\n\
					<option value="9:00 PM">9:00 PM</option>\n\
					<option value="9:30 PM">9:30 PM</option>\n\
					<option value="10:00 PM">10:00 PM</option>\n\
					<option selected="selected" value="">Select a time</option>');
			  	break;    
			default:
				break;
		}
	});


	var i = 1;
	$('a#add-rez').click(function() {
		i++;
	
		$('a#add-rez').prev().before('<fieldset id="rez-'+i+'" class="added-form"><h5>Reservation #'+i+'</h5>\n\
			<a href="#" class="remove-form">remove</a>\n\
			<div class="field">\n\
				<label for="restaurant" class="required">Restaurant:</label>\n\
				<select id="restaurant'+i+'" class="required textfield restaurantadded" size="1" name="restaurant[]">\n\
					<option selected="selected" value="">Choose a restaurant</option>\n\
					<option value="Seagrape Terrace">Seagrape Terrace</option>\n\
					<option value="Il Giardino">Il Giardino</option>\n\
					<option value="The Sugar Mill">The Sugar Mill</option>\n\
					<option value="The Akbar Restaurant">The Akbar Restaurant</option>\n\
				</select>\n\
			</div>\n\
			<div class="field">\n\
				<label for="guests" class="required">No. of Guests:</label>\n\
				<input type="text" name="guests[]" id="guests" maxlength="100" class="textfield required" value="" />\n\
			</div>\n\
			<div class="field">\n\
				<label for="date" class="required">Date:</label>\n\
				<input type="text" name="date[]" id="date" maxlength="100" class="textfield required" value="" />\n\
			</div>\n\
			<div class="field">\n\
				<label for="time" class="required">Time:</label>\n\
				<select id="time'+i+'" class="required textfield" size="1" name="time[1]">\n\
					<option selected="selected" value="">Select a time</option>\n\
					<option value="7:00 PM">7:00 PM</option>\n\
					<option value="7:30 PM">7:30 PM</option>\n\
					<option value="8:00 PM">8:00 PM</option>\n\
					<option value="8:30 PM">8:30 PM</option>\n\
					<option value="9:00 PM">9:00 PM</option>\n\
					<option value="9:30 PM">9:30 PM</option>\n\
				</select>\n\
			</div>\n\
			<div class="field">\n\
				<label for="special_request">Special Request:</label>\n\
				<input type="text" name="special_request[]" id="special_request" maxlength="100" class="textfield" value="" />\n\
			</div></fieldset>');
	
		$('form select.restaurantadded').bind("change", function() { 	
			var restval = $(this).val();
			var restnum = $(this).attr("id");
			restnum = restnum.substring(restnum.length-1);
			switch( restval ){
				case 'Seagrape Terrace':
					$('form select#time'+restnum).html('<option value="7:00 PM">7:00 PM</option>\n\
						<option value="7:30 PM">7:30 PM</option>\n\
						<option value="8:00 PM">8:00 PM</option>\n\
						<option value="8:30 PM">8:30 PM</option>\n\
						<option value="9:00 PM">9:00 PM</option>\n\
						<option value="9:30 PM">9:30 PM</option>\n\
						<option selected="selected" value="">Select a time</option>');
					break;    
				case 'Il Giardino':
					$('form select#time'+restnum).html('<option value="6:30 PM">6:30 PM</option>\n\
						<option value="7:00 PM">7:00 PM</option>\n\
						<option value="7:30 PM">7:30 PM</option>\n\
						<option value="8:00 PM">8:00 PM</option>\n\
						<option value="8:30 PM">8:30 PM</option>\n\
						<option value="9:00 PM">9:00 PM</option>\n\
						<option selected="selected" value="">Select a time</option>');			  
				  break;
				case 'The Sugar Mill':
					$('form select#time+restnum').html('<option value="6:30 PM">6:30 PM</option>\n\
						<option value="7:00 PM">7:00 PM</option>\n\
						<option value="7:30 PM">7:30 PM</option>\n\
						<option value="8:00 PM">8:00 PM</option>\n\
						<option value="8:30 PM">8:30 PM</option>\n\
						<option value="9:00 PM">9:00 PM</option>\n\
						<option selected="selected" value="">Select a time</option>');		
					break;
				case 'The Akbar Restaurant':
					$('form select#time'+restnum).html('<option value="7:00 PM">7:00 PM</option>\n\
						<option value="7:30 PM">7:30 PM</option>\n\
						<option value="8:00 PM">8:00 PM</option>\n\
						<option value="8:30 PM">8:30 PM</option>\n\
						<option value="9:00 PM">9:00 PM</option>\n\
						<option value="9:30 PM">9:30 PM</option>\n\
						<option value="10:00 PM">10:00 PM</option>\n\
						<option selected="selected" value="">Select a time</option>');
					break;    
				default:
					break;
			}
															
		});
                                    
		$('a.remove-form').bind("click", function(){
			$(this).parent().remove();	
			
			$('#add-rez').html('<img src="images/interior/dining/btn-add.gif" alt="" />');
			i--;
		});
		if (i>=4) {
			$('#add-rez img').remove();
		}
	});
	
	var monthWrapper = $('#month-wrapper');
	if (monthWrapper.length) {
	    var daysList = $('.days-list'), 
	    currentDay = currentDay || 'monday', 
	    daysAnchors = $('.days-list li a'), 
	    dayCells = $('td.' + currentDay),
	    activeRows=[];

	    //hideEmptyCells();

	    function addZebraStripes() {
		$('table.special-calendar tr:even').addClass('odd');
		/*
		$(activeRows).each(function(i) {
		    if (i % 2 == 0) {
			$(this).removeClass('odd');
		    } else {
			$(this).addClass('odd');
		    }
		});
		*/
	    }
	    addZebraStripes();

	    function hideEmptyCells() {
		var tableCells = $('td.' + currentDay);
		tableCells.each(function() {
		    // this regex matches a pattern that starts and ends with a whitespace character.
		    // if a match is found, it hides the table row ($(this.parent()), if not it shows the table row (and some may be hidden)
		    var whiteSpaceRegex = /^\s*$/;
		    if (whiteSpaceRegex.test( $(this).text()) ) {
			$(this).parent().hide();
		    } else {
			$(this).parent().show();
			activeRows.push($(this).parent());
		    }
		});
		addZebraStripes();
	    };

	    daysAnchors.click(function() {
		// hide the current day cells
		dayCells.hide();

		// take the value of the parent elements class attribute (since the mcc doesnt allow id attributes on lis) and extract the remaining chars after 'days-' via String.prototype.slice
		var classAttribute = $(this).parent().attr('class');
		currentDay = classAttribute.slice(5,classAttribute.length);
		dayCells = $('td.' + currentDay);

		// show the new current day cells
		dayCells.show();

		// update the current state
		$('ul.days-list .current').removeClass('current');
		$(this).parent().addClass('current');

		// commented out for now: hideEmptyCells(); 
		addZebraStripes();

		return false;
	    });
	}
	
	// Dining Insets
	$("div#inset-signature").flash({src:"flash/OverviewInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-seagrape").flash({src:"flash/seagrapeInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-sugar").flash({src:"flash/sugarMillInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-19").flash({src:"flash/19thHoleInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-bbq").flash({src:"flash/beachBBQInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-royal").flash({src:"flash/royalStocksInset.swf ", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-il").flash({src:"flash/ilGiardinoInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-beach").flash({src:"flash/barOverviewInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-casual").flash({src:"flash/casualOverviewInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-beachbar").flash({src:"flash/BeachBarInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-baguette").flash({src:"flash/BaguetteInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-lesters").flash({src:"flash/LestersBarInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-pavilion").flash({src:"flash/RPavillionInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
	$("div#inset-hibiscus").flash({src:"flash/HibiscusPoolInset.swf", width:183, height:288, wmode:"transparent", quality:"high"});
});
		

var request = new Object();
var target = new Object();
var xpos;
var ypos;
		
// callback function
result_check = function(){     
	if(request.readyState == 4){
		if(request.status == 200){
			if(parseInt(request.responseText) > 0){                      
			  var hiddenDiv = document.getElementById('alert');
			  hiddenDiv.style.display = "block";
			  if(target.name == 'email_address'){
				  document.getElementById('alertText').innerHTML = "<p><strong>Our records indicate that you have already registered.  Please login to update your profile.<br />If you have forgotten your password use the link in the login form to request a new password</strong></p>" +
										"<p><span onclick=\"hide('alert');\">[ Click Here To Close ]</span></p>";
				  hiddenDiv.style.top = '57%';                                                
			  } else if(target.name == 'username'){                          
				  document.getElementById('alertText').innerHTML = "<p><strong>The username you entered already exists. Please choose another.</strong></p>" +
										"<p><span onclick=\"hide('alert');\">[ Click Here To Close ]</span></p>"; 
				  hiddenDiv.style.top = '62%';
			  }
			  hiddenDiv.style.left = '45%';
			  target.value = '';
			  target.focus();
			}
		}
	}
}

function checkUser(obj){
	target = obj;                  
	url = "http://www.halfmoon.com/agents/verifyUser.php?val=" + target.value + "&field=" + target.name;
	try{
		request = new XMLHttpRequest();
	} catch (e1){
		try {
			request = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e2){
			request = new ActiveXObject("Msxml2.XMLHTTP");
		}
	}
	
	request.onreadystatechange = result_check;
	request.open("GET", url, true);
	request.send(null);
	
}

function hide(str){
	var el = document.getElementById(str);
	el.style.display = "none";
	target.focus();
}
	
function updatePass(frm){
	if(frm.pwd.value != frm.confirm_pwd.value){
		frm.confirm_pwd.value = '';
		frm.confirm_pwd.focus();
		alert("The password and confirmed password do not match.\n Please re-enter the password.");
		return false;
	} else {
		params = Form.serialize(frm);
	   // alert(params);
		url = '/agents/updatePass.php?'+params;
		try{
			request = new XMLHttpRequest();
		} catch (e1){
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e2){
				request = new ActiveXObject("Msxml2.XMLHTTP");
			}
		}
	
		request.onreadystatechange = confirmPassChange;
		request.open("GET", url, true);
		request.send(null);               
	  
		obj = $('#alert');
		$('#alertText').html('<p><strong>Updating your password please wait....</strong></p>'+
									'<p style="text-align: center;"><img src="images/interior/updating.gif" alt="" /></p>');
		obj.show();
		
	}
}

confirmPassChange = function(){
	if(request.readyState == 4){
		if(request.status == 200){
			//alert(request.responseText);
			obj = $('#alert');
			if(request.responseText == '0'){
				$('#alertText').html('<p style="font-size: 1em; font-weight: bold; color:#73715A"><strong>Your password has been updated.</strong></p>' +
									   '<p><span onclick="hide(\'alert\');">[ Click Here To Close ]</span></p>');    
			} else {
				//alert(request.status);
				$('#alertText').html('<p style="font-size: 1em; font-weight: bold; color:#73715A"><strong>Your password could not be updated.  Please make sure that you are not entering a current password.</strong></p>' +
									   '<p><span onclick="hide(\'alert\');">[ Click Here To Close ]</span></p>');
			} 
			obj.show();
		}
	}             
}

function changePass(id){
	obj = $('#alert');
	$('#alertText').html('<p><strong>Enter your new password and click submit.</strong></p>'+
								'<form action="javascript:;" method="post" onsubmit="updatePass(this); return false;">' +
								'<input type="hidden" value="'+id+'" name="agent_id" />' +
								'<table id="changePass">' +
									'<tr>' +
										'<th>Password:</th>' +
										'<td><input type="password" name="pwd" id="pwd" /></td>' +
									'</tr>' +
									'<tr>' +
										'<th>Confirm Password:</th>' +
										'<td><input type="password" name="confirm_pwd" id="confirm_pwd" /></td>' +
									'</tr>' +
									'<tr>' +
										'<td>&nbsp;</td>' +                                                               
										'<td><input type="submit" value="Submit" name="submit_pass" id="submit2" /></td>' +
									'</tr>' +
								'</table>' +
								'</form>' +
								'<p><span onclick="hide(\'alert\');">[ Click Here To Close ]</span></p>');
	obj.show();
	return false;
}

function confirmUpdate(){
	obj = $('#alert');
	$('#alertText').html('<p style="font-size: 1em; font-weight: bold; color:#73715A"><strong>Your profile has been updated.</strong></p>' +
								'<p><span onclick="hide(\'alert\');">[ Click Here To Close ]</span></p>');
	obj.show();
}

function denyUpdate(reason){
	obj = $('#alert');
	$('#alertText').html('<p style="font-size: 1em; font-weight: bold; color:#73715A"><strong>Your profile has not been updated.</strong></p>' +
								'<p>'+reason+'</p>'+
								'<p><span onclick="hide(\'alert\');">[ Click Here To Close ]</span></p>');
	obj.show();
}

function verifyWarning(item,value)
{
	if(!confirm('Warning.  If you change this field your account will be disabled and you will be unable to log back in until it is reviewed and re-activated by an administrator.'+  
		'Please make sure you have completed all other critical tasks before changing this information. You may click cancel to undo your most recent changes now.'))
			item.value=value;
}

