$(function(){
	if($.browser.safari){
		$("div#resnet input#submit").addClass("sbutton");
	}
	
	build_drops("property");
	
	// FORM FIELD FUNCTIONS
	$("input#adults").blur(function(){
		if (this.value != "" && parseInt(this.value) != this.value) {
			alert("Please enter a number of adults between 1 and 4.\nThank you.");
			show_changes("input#adults"); 
		}
		if (this.value > 4) {
			this.value = 4;
			alert("To book more than 4 adults, please contact us directly at the number below:\n866-942-7772\nThank you.");
			show_changes("input#adults"); 
		}
	});
	$("input#nights").blur(function(){
		if (this.value != "" && parseInt(this.value) != this.value) {
			alert("Please enter a number of nights between 1 and 10.\nThank you.");
			show_changes("input#nights"); 
		}
		if (this.value > 10) {
			this.value = 10;
			alert("To book more than 10 nights, please contact us directly at the number below:\n866-942-7772\nThank you.");
			show_changes("input#nights"); 
		}
	});
	$("input#datebox").focus(function(){
		if (this.value == "MM/DD/YY") {
			this.value = "";
		}
	});
	$("input#datebox").blur(function(){
		if (this.value == "") {
			this.value = "MM/DD/YY";
		}
		auto_complete_date();
	});
	$("form#resnet_form").submit(function(){
		var prop = $("select#property").val();
		var nights = $("input#nights").val();
		var adults = $("input#adults").val();
		var checkin = $("input#datebox").val();
		
		rez(prop,nights,adults,checkin);
	});
	// $("input#datebox").keyup(function(){auto_complete_date();});
});

function rez(p,n,a,c) {
	//GET CURRENT FORM VALUES
	var checkin = c.replace(/\//g, "");
	var nights = n;
	var adults = a;
	var prop = p;
	
	//OPEN NEW RESNET WINDOW
	var nessie = "https://reservations.thepalmslasvegas.com/CGI-BIN/LANSAWEB?procfun+rn+resnet+";
	nessie += ""+prop+"+funcparms+UP(A2560):;;"+checkin+";"+nights+";"+adults;
	nessie += ";;;;;;;;;;;;;;;;;;?";
	nessiterasrhombopteryx = "/reservations.php?key=0&resmod="+escape(nessie);
	window.open(nessiterasrhombopteryx);
}

function build_drops(id) {
	// Select the SELECT and gather it's OPTIONs.
	var select = $("select#"+id);
	var options = select.children("option");
	
	// Check for a pre-selected OPTION or default to the first OPTION.
	for (var i=0; i<options.length; i++) {
		var selectedOption;
		if (options[i].selected) {
			selectedOpt = i;
			break;
		} else {
			selectedOpt = 0;
		}
	}
	
	// Start the replacment UL. I'm not sure about that class name.
	$("div#resnet form#resnet_form").append('<ul id="'+id+'_select" class="selectReplacement"></ul>');
	var ul = $("div#resnet form ul#"+id+"_select");
		
	// Loop through, creating one LI for each OPTION.
	for (var i=0; i<options.length; i++) {
		if (i == selectedOpt) {
			$(ul).append('<li class="selected" rel="'+options[i].index+'">'+options[i].text+'</li>');
		} else {
			$(ul).append('<li rel="'+options[i].index+'">'+options[i].text+'</li>');
		}
	}

	$("div#resnet ul").toggle(function(){
		$(this).addClass("selHover");
	},function(){
		$(this).removeClass("selHover");
	});
	
	$("div#resnet ul li").hover(function(){
		$(this).addClass("selHover");
	},function(){
		$(this).removeClass("selHover");
	});
	
	$("div#resnet ul li").click(function(){
		$(this).siblings("li").removeClass("selected");
		$(this).addClass("selected");
		set_val($(this).parent().attr("id"), $(this).attr("rel"));
	});
	
	$("select#"+id).hide();
}

function set_val(objID, selIndex) {
	objID = objID.replace(new RegExp("_select"), '');
	var obj = $("div#resnet form select#"+objID);
	// var childs = obj.children();
	obj.children().removeAttr("selected");
	var sel = $("div#resnet form select#"+objID+" option:eq("+selIndex+")");
	sel.attr("selected","selected");
}

function auto_complete_date() {
	var date = $("input#datebox").val();
	date = date.replace(/\-/g, "/");
	$("input#datebox").val(date);

	if (date.length >= 8) {
		full_date = date.split("/");
			month = full_date[0];
			day = full_date[1];
			year = full_date[2];
		
			if(month.length < 2) { month = "0"+month; show_changes("input#datebox"); }
			if(month.length > 2) { String(month).substr(0,1); show_changes("input#datebox"); }
			if(month < 1 || month > 12) { month = ""; show_changes("input#datebox"); }
			
			if(day.length < 2) { day = "0"+day; show_changes("input#datebox"); }
			if(day.length > 2) { String(day).substr(0,1); show_changes("input#datebox"); }
			if(day < 1 || day > 31) { day = ""; show_changes("input#datebox"); }
			
			if(year.length < 2) { year = "0"+year; show_changes("input#datebox"); }
			if(year.length > 2) { String(year).substr(0,1); show_changes("input#datebox"); }
			if(year < 08 || year > 09) { year = "08"; show_changes("input#datebox"); }
		
		display_date = (month||"MM") + "/" + (day||"DD") + "/" + (year||"YY");
		$("input#datebox").val(display_date);
	}	
}

function show_changes(target) {
	$(target).animate({
		'opacity': 0.75
	},350, function(){
		$(this).animate({
			'opacity': 1.0
		}, 350);
	});
}