/// <reference path="jquery-1.3.1-vsdoc.js"/>
/// <reference path="jquery.hotelSelector.js"/>

$(document).ready(function() {
    // activate dropdownlists
    var titleSelectors = $("ul[id$='.titleSelector']");
    titleSelectors.each(function(i, item) {
        var titleSelector = new DropDownList($(item).attr("id"));
        titleSelector.bind();
    });

    var countrySelectors = $("ul[id$='.countrySelector']");
    countrySelectors.each(function(i, item) {
        var countrySelector = new DropDownList($(item).attr("id"));
        countrySelector.bind();
    });

    var cardSelector = new DropDownList("cardSelector");
    cardSelector.bind();

    var expiryMonthSelector = new DropDownList("expiryMonthSelector");
    expiryMonthSelector.bind();

    var expiryYearSelector = new DropDownList("expiryYearSelector");
    expiryYearSelector.bind();

    var startMonthSelector = new DropDownList("startMonthSelector");
    startMonthSelector.bind();

    var startYearSelector = new DropDownList("startYearSelector");
    startYearSelector.bind();

    $("input.book").click(function() {
        return validateDetails();
    });

    $("div.tabContainer span.message").hide();
    if ($("#useSameDetails").attr("checked")) {
        $("ul.tabs li[class!=selected]").hide();
        $("div.tabContainer span.message").show();
    }

    $(".useSameDetails").click(function() {
        if ($(this).attr("checked")) {
            $("ul.tabs li[class!=selected]").hide();
            $("div.tabContainer span.message").show();
            $(".useSameDetails:not(:checked)").attr("checked", "checked");
        } else {
            $("ul.tabs li[class!=selected]").show();
            $("div.tabContainer span.message").hide();
            $(".useSameDetails:checked").removeAttr("checked");
        }
    });


    // enable tabbed room navigation
    var tabs = $("ul.tabs li a");
    tabs.click(function() {
        // remove "selected" class from all tabs
        var relatedTabs = $(this).closest("ul").find("li");
        relatedTabs.removeClass("selected");

        // hide all rooms
        var rooms = $("div.rooms").children(".roomDetail");
        rooms.addClass("hide");

        // show room represented by the clicked tab
        var roomIndex = $(this).closest("li").prevAll("li").length; // number of preceding LI siblings == room FIELDSET index
        $(rooms[roomIndex]).removeClass("hide");

        var trajan = { src: '/css/trajan.swf' };

        sIFR.replace(trajan, {
            selector: '#EnterYourDetailsStep fieldset h3',
            css: '.sIFR-root { color:#433A37; text-align:left; padding:0; text-transform:uppercase; letter-spacing:0.3; font-size:13; } a { color:#433A37; font-size:60; letter-spacing:0.3; text-decoration: none; }',
            wmode: 'transparent',
            selectable: false
        });

        // add "selected" class to current tab
        $(this).closest("li").addClass("selected");

        return false;
    });

    $("a.securityNumberInfo").click(function() {
        window.open("/layouts/pages/booking/securitynumber.html", "SecurityNumberInfo", "location=0,status=0,scrollbars=0,width=300,height=500");
        return false;
    });
});


function validateDetails() {
	var errors = [];
	var numberOfRooms = $("ul.tabs li").length;
	
	// room validation
	if ($(".useSameDetails").attr("checked")) {
		validateRoom(errors, 0);
	} else {
		for (var i = 0; i < numberOfRooms; i++) {
			validateRoom(errors, i);
		}
	}
	// register
	if ($("#accept_registration").is(":checked")) {
	    if ($("#password").val() != $("#re_password").val()) { errors.push("Your passwords don't match"); }
	}

	// credit card
	if ($("#cardholderName").val() == "") { errors.push("Please enter a cardholder name"); }
	if ($("#card").val() == "") { errors.push("Please enter card type"); }

	if ($("#cardNumber").val() == "") {
		errors.push("Please enter card number");
	} else {
		// Regular expression "/^\d{15,16}$/" matches 15- or 16-digit number. Example: "1234567890123456"
		if (!regexValidate($("#cardNumber").val(), /^\d{15,16}$/)) { errors.push("Please enter a valid card number"); }
	}
	
	if ($("#expiryMonth").val() == "") { errors.push("Please enter expiry month"); }
	if ($("#expiryYear").val() == "") { errors.push("Please enter expiry year"); }
	
	if ($("#securityNumber").val() == "") {
		errors.push("Please enter security number");
	} else {
		// Regular expression "/^\d\d\d\d?$/" matches a 3- or 4-digit number. Example: "456" or "4567"
		if (!regexValidate($("#securityNumber").val(), /^\d\d\d\d?$/)) { errors.push("Please enter a valid security number"); }
	}

	if (errors.length > 0) {
		var errorMessage = "";
		var error = null;
		for (var i = 0; i < errors.length; i++) {
			errorMessage += errors[i] + "\n";
		}
		alert(errorMessage);
		return false;
	}
	
	return true;
}

function validateRoom(errors, index) {
    var roomPrefix = "#room\\[" + index + "\\]\\.";
	if ($(roomPrefix + "title").val() == "") { errors.push("Please enter a title for Room " + (index + 1)); }
	if ($(roomPrefix + "firstName").val() == "") { errors.push("Please enter firstname for Room " + (index + 1)); }
	if ($(roomPrefix + "surname").val() == "") { errors.push("Please enter surname for Room " + (index + 1)); }

	if ($(roomPrefix + "email").val() == "") {
		errors.push("Please enter email address for Room " + (index + 1));
	} else {
		// Regular expression "/[\w!#$%*/?|\^\{\}'~\.]+@(\w+\.)*\w{2,3}/" matches a valid email address. Example: "john.doe@company.com"
		if (!regexValidate($(roomPrefix + "email").val(), /[\w!#$%*\/?|\^\{\}\'~\.]+@(\w+\.)*\w{2,3}/)) { errors.push("Please enter a valid email address for Room " + (index + 1)); }
	}

	if ($(roomPrefix + "email").val() != $(roomPrefix + "confirmEmail").val()) { errors.push("Please confirm email address for Room " + (index + 1)); }
	
	if(index > 0) return;	// addresses for rooms 2-5 are not validated

	if ($(roomPrefix + "telephone").val() == "") { errors.push("Please enter telephone number for Room " + (index + 1)); }
	if ($(roomPrefix + "address1").val() == "") { errors.push("Please enter street address for Room " + (index + 1)); }
	if ($(roomPrefix + "city").val() == "") { errors.push("Please enter city for Room " + (index + 1)); }
//	if ($("#county_" + index).val() == "") { errors.push("Please enter county for Room " + (index + 1)); }

	if ($(roomPrefix + "postcode").val() == "") {
		errors.push("Please enter postcode for Room " + (index + 1));
       }

	if ($(roomPrefix + "country").val() == "") { errors.push("Please enter country for Room " + (index + 1)); }
}

function regexValidate(value, regexExpression) {
	if (!value || !regexExpression) return false;

	return regexExpression.test(value);
}

