// --------------------------------------------------------------------------------
// nsacc_adv.js
// --------------------------------------------------------------------------------
///<reference path="calendar.js">

var nsacc_SearchPrefix = "tmpl_nsacc_Search_nsaccSearch_";
var showOptions = false;

// --- Attach JS to document elements and execute simple statements. ---
ow_f_AppendLoadEvent(
function() {

    if (document.getElementById("nsacc_Search") != null) {
        var inp = document.getElementById("nsacc_Search").getElementsByTagName("input");
        for (var i = 0; i < inp.length; i++) {
            if (inp[i].type == "text") ow_f_AddEvent(inp[i], "keypress", nsacc_Search_Key, false);
        }
    }

    var img = document.getElementById(nsacc_SearchPrefix + "imgCalendarCheckin");
    var nsacc_text = document.getElementById(nsacc_SearchPrefix + "txtCheckinDate");
    if (img != null && nsacc_text != null) {
        ow_f_AddEvent(img, "click", function() { return nsacc_popupCalendar(nsacc_SearchPrefix + "imgCalendarCheckin", nsacc_SearchPrefix + "txtCheckinDate") }, false);
        ow_f_AddEvent(nsacc_text, "blur", function() { return nsacc_updateCheckout(nsacc_SearchPrefix); }, false);
    }
    img = document.getElementById(nsacc_SearchPrefix + "imgCalendarCheckout");
    nsacc_text = document.getElementById(nsacc_SearchPrefix + "txtCheckoutDate");
    if (img != null && nsacc_text != null)
        ow_f_AddEvent(img, "click", function() { return nsacc_popupCalendar(nsacc_SearchPrefix + "imgCalendarCheckout", nsacc_SearchPrefix + "txtCheckoutDate") }, false);


    var nsacc_btn = document.getElementById(nsacc_SearchPrefix + "subm");
    if (nsacc_btn != null)
        ow_f_AddEvent(nsacc_btn, "click", nsacc_Search_Click, false);

    var nsacc_imgSubmit = document.getElementById(nsacc_SearchPrefix + "imgSubmit");
    if (nsacc_imgSubmit != null)
        ow_f_AddEvent(nsacc_imgSubmit, "click", nsacc_Search_Click, false);



    // the visibility of the extra search fields is set by the status of the hidden field on page load, initially set to false
    // in the control but could be set by the query string
    var hid_Search = document.getElementById(nsacc_SearchPrefix + "hid_Search");
    if (hid_Search != null && hid_Search.value == "true") {
        showOptions = true;
    }

    // add event handler for the "More/Less" images.
    var nsacc_imgMoreOptions = document.getElementById("nsacc_imgMoreOptions");
    if (nsacc_imgMoreOptions != null) {
        ow_f_AddEvent(nsacc_imgMoreOptions, "click", nsacc_HideShowFields, false);
    }

    // add event handler for the "More/Less" link.
    var nsacc_lnkMoreOptions = document.getElementById("nsacc_lnkMoreOptions");
    if (nsacc_lnkMoreOptions != null) {
        ow_f_AddEvent(nsacc_lnkMoreOptions, "click", nsacc_HideShowFields, false);
        nsacc_HideShowFields(null);
    }        
        

}
);


// --------------------------------------------------------------------------------
// nsacc_HideShowFields()
// Shows/hides more search fields when clicked
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- nothing
// --------------------------------------------------------------------------------
function nsacc_HideShowFields(e) {

    var hid_Search = document.getElementById(nsacc_SearchPrefix + "hid_Search");

    if (showOptions) {
        //if div is not already open, open it
		if (jQuery("#srch_advanced").css("display") == "none") {
			jQuery("#nsacc_lnkMoreOptions").html("Moins d'information").toggleClass('plus').toggleClass('minus');
			jQuery("#srch_advanced").slideToggle(600);
		}
        // set the hidden field to maintain state of the extra search fields
        if (hid_Search != null) hid_Search.value = "true"
        showOptions = false;
    }
    else {
        //if div is not already closed, close it
		if (jQuery("#srch_advanced").css("display") == "block") {
			jQuery("#nsacc_lnkMoreOptions").html("Plus d'information").toggleClass('plus').toggleClass('minus');
			jQuery("#srch_advanced").slideToggle(600);
		}
        // set the hidden field to maintain state of the extra search fields
        if (hid_Search != null) hid_Search.value = "false"
		showOptions = true;
    }
}


// --------------------------------------------------------------------------------
// nsacc_Search_Key()
// Fires when a textfield had focus and ENTER was pressed.
function nsacc_Search_Key(e) {
    var code;
    if (!e) var e = window.event;
    if (e.keyCode) code = e.keyCode;
    else if (e.which) code = e.which;

    if (code == 13) {
        document.getElementById(nsacc_SearchPrefix + "subm").click();
        if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
    }
}

function nsacc_popupCalendar(image, textfield) {
    setDateField(document.getElementById(textfield));
    var address = document.getElementById(image).src;
    address = address.substring(0, address.lastIndexOf("/") + 1) + "calendar.html";
    top.newWin = window.open(address, "cal", "dependent=yes,width=210,height=230,screenX=200,screenY=400,titlebar=yes");
    top.newWin.focus();
    return false;
}

function nsacc_updateCheckout(prefix) {
    var checkin = document.getElementById(prefix + "txtCheckinDate");
    var checkout = document.getElementById(prefix + "txtCheckoutDate");
    if (checkin != null && checkout != null && checkin.value.length > 0) {
        var ci = new Date(checkin.value);
        if (!isNaN(ci)) {
            if (checkout.value.length === 0) {
                var co = new Date(ci.getFullYear(), ci.getMonth(), ci.getDate() + 1);
                checkout.value = formatDate(co);
            } else if (!isNaN(new Date(checkout.value)) && ci >= new Date(checkout.value)) {
                var co = new Date(ci.getFullYear(), ci.getMonth(), ci.getDate() + 1);
                checkout.value = formatDate(co);
            }
        }
    }
    return false;
}

// --------------------------------------------------------------------------------
// nsacc_Search_Click()
// Fires when the submit button was clicked.
// --------------------------------------------------------------------------------
function nsacc_Search_Click(e) {
    if (!nsacc_Search_Valid()) {
        if (!e) var e = window.event;
        if (e.preventDefault) e.preventDefault(); else e.returnValue = false;
    } else {
        return;
    }
}

// --------------------------------------------------------------------------------
// nsacc_Search_Valid()
// Validates the data entered in the Accommodations Search.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- the results of the validation [boolean]
// --------------------------------------------------------------------------------
function nsacc_Search_Valid() {

    var checkin = document.getElementById(nsacc_SearchPrefix + "txtCheckinDate");
    var checkout = document.getElementById(nsacc_SearchPrefix + "txtCheckoutDate");

    var minCheckin = new Date();
    minCheckin.setDate(minCheckin.getDate() + 1);

    //var filter  = /^[01]?\d\/[0-3]?\d\/200[6-9]$/;

    var filter = /(0[1-9]|1[012])\/(0[1-9]|[12][0-9]|3[01])\/20[0-9][0-9]$/;

    // rule is that either check in and check both are input or both are empty
    if (checkin != null && checkout != null) {
        if (checkin.value.length == 0 && checkout.value.length == 0) return true;
        else if ((checkin.value.length > 0 && checkout.value.length == 0) || (checkin.value.length == 0 && checkout.value.length > 0)) {
            alert(document.getElementById(nsacc_SearchPrefix + "cvDatesEntry").value);
            return false;
        } else if (!checkin.value.match(filter)) {
            alert(document.getElementById(nsacc_SearchPrefix + "regvCheckinDate").value);
            document.getElementById(nsacc_SearchPrefix + "imgCalendarCheckin").focus();
            return false;
        } else if (Date.parse(checkin.value) + (1000 * 60 * 60 * 24) < minCheckin.valueOf()) {
            alert(document.getElementById(nsacc_SearchPrefix + "cvCheckinDate").value);
            document.getElementById(nsacc_SearchPrefix + "imgCalendarCheckin").focus();
            return false;
        } else if (!checkout.value.match(filter)) {
            alert(document.getElementById(nsacc_SearchPrefix + "regvCheckoutDate").value);
            document.getElementById(nsacc_SearchPrefix + "imgCalendarCheckout").focus();
            return false;
        } else if (Date.parse(checkout.value) < Date.parse(checkin.value)) {
            alert(document.getElementById(nsacc_SearchPrefix + "cvCheckoutDate").value);
            //alert("'Check-out date' must be after 'Check-in date'.")
            document.getElementById(nsacc_SearchPrefix + "imgCalendarCheckout").focus();
            return false;
        } 
    }

    return true;
}



