﻿/*
* HCG JavaScript Library v0.1.1
* http://www.holidaycottagesgroup.com/
*
* Framework Copyright (c) 2009 Chris McKee
* Dual licensed under the MIT and GPL licenses.
* Licence URL: http://bit.ly/1804Oh
*
* Date: 2009-07-24 17:34:21 -0000 (Fri, 24 Jul 2009)
* Revision: 19 ($rev$)
* 
* Copyright (c) 2009 Chris McKee, http://www.holidaycottagesgroup.com/
* 	
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
* 	
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
* 	
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
* 
* Members
* 	  UpdateRegions, WriteToCombo, appendChild, countryId, 
*     createElement, createTextNode, error, getElementById, join, length, 
*     onchange, onsubmit, prepareData, prepareQuickSearch, regionId, 
*     regionName, replace, setAttribute, split, value
* 
* jslint: bitwise: true, browser: true, eqeqeq: true, immed: true, onevar: true, plusplus: true, regexp: true, undef: true
*/
function _$(id) { return document.getElementById(id); }
function _$er(e) { try { console.error(e); } catch (x) { var f = x; } }
function _$rMD(rID, rNme, cId) { this.regionId = rID; this.regionName = rNme; this.countryId = cId; }
function _$rsMD(rID, rNme, rId) { this.resortId = rID; this.resortName = rNme; this.regionId = rId; }

var rArr = [];
var rsArr = [];
var COM_HOLIDAYCOTTAGESGROUP = {};

COM_HOLIDAYCOTTAGESGROUP.prepareQuickSearch = function() {
    //Hide None JS Form
    _$("countrygo").style.display = "none";

    var qsForm = _$('quicksearch') ? _$('quicksearch') : _$('advsearch');

    if (qsForm) {
        //Register Page Objects
        var hcgCountry, hcgRegion, hcgStartDate, hcgDuration, hcgSleeps, dplnk, uri;
        hcgCountry = _$('country');
        hcgRegion = _$('areas');
        hcgStartDate = _$('date');
        hcgDuration = _$('duration');
        hcgSleeps = _$('sleeps');

        if (hcgStartDate.value === "Any") hcgStartDate.value = "";

        //Bit of Testing
        if ((!hcgCountry) || (!hcgRegion)) { _$er("Page REQUIRES that you have a select with id hcgcountry containing countries + an empty select with id hcgregion"); }
        if (hcgCountry && hcgCountry.length < 1) { _$er("Countries must be populated with data before operation will commence"); }
        if (!hcgStartDate || !hcgDuration || !hcgSleeps) { _$er("StartDate input(textbox) should be present with id hcgstartdate\n Duration select(Dropdown) should be present with id hcgduration \n and Sleeps select(dropdown) should be present with id hcgduration"); }

        //Prepare DataArray
        try {
            COM_HOLIDAYCOTTAGESGROUP.prepareData();
        } catch (xs) { _$er("Data Loading Function Failed to be Found" + xs); }
        //Update Regions
        if (rArr.length > 0) { COM_HOLIDAYCOTTAGESGROUP.UpdateRegions(hcgCountry.value, hcgRegion); } else { _$er("Data Array Failed to Initilise"); }

        //Capture onchange
        hcgCountry.onchange = function() {
          COM_HOLIDAYCOTTAGESGROUP.UpdateRegions(hcgCountry.value, hcgRegion);
          COM_HOLIDAYCOTTAGESGROUP.UpdateDuration(hcgCountry.value, hcgDuration);
        }; //END FUNC

        //Capture onsubmit
        qsForm.onsubmit = function() {
            //Date Handling
            var RAR = hcgRegion.value !== "" ? hcgRegion.value : hcgCountry.value;
            var message = null;

            if (hcgStartDate.value !== "") {
                hcgSleeps.value.replace("/", "-");
                hcgSleeps.value.replace(" ", "-");
                if (hcgSleeps.value === "") { COM_HOLIDAYCOTTAGESGROUP.AddCSSClass(hcgSleeps, "input-validation-error"); }
                if (hcgDuration.value === "") { COM_HOLIDAYCOTTAGESGROUP.AddCSSClass(hcgDuration, "input-validation-error"); }
                if ((hcgSleeps.value === "") || (hcgDuration.value === "")) return false;
            }

            return true;
        };

    }
};

COM_HOLIDAYCOTTAGESGROUP.UpdateDuration = function(cCD, cOBJ) {
    if (cOBJ) {
        cOBJ.options.length = 0;
        COM_HOLIDAYCOTTAGESGROUP.WriteToCombo("Please Select", "", cOBJ);
        var isUK = ["21", "14", "7", "4", "3", "2"];
        var notUK = ["21", "14", "7", "4"];
        var csn = null;
        csn = cCD === "united-kingdom" ? isUK : notUK;
        var ll = csn.length;
        while (ll--) COM_HOLIDAYCOTTAGESGROUP.WriteToCombo(csn[ll], csn[ll], cOBJ, false);
    }
};

//cCD: CountryDD Value; cOBJ Selected RAR Box
COM_HOLIDAYCOTTAGESGROUP.UpdateRegions = function(cCD, cOBJ) {
    if (cOBJ) {
        cOBJ.options.length = 0;
        COM_HOLIDAYCOTTAGESGROUP.WriteToCombo("-- All --", "", cOBJ);
        for (var i = 0; i < rArr.length; i++) {
            if (rArr[i].countryId === cCD) {
                COM_HOLIDAYCOTTAGESGROUP.WriteToCombo(rArr[i].regionName, rArr[i].regionId, cOBJ, false);
                for (var j = 0; j < rsArr.length; j++) {//Resort Handling
                    if ((rsArr[j].regionId === rArr[i].regionId) && (rsArr[j].resortName !== rArr[i].regionName)) {
                        COM_HOLIDAYCOTTAGESGROUP.WriteToCombo(rsArr[j].resortName, rsArr[j].resortId, cOBJ, true);
                    }
                }
            }
        }
    }
};

COM_HOLIDAYCOTTAGESGROUP.WriteToCombo = function(tNo, vNo, cmb, pfx) {
    if (cmb) {
        var ele, not;
        ele = document.createElement("option");
        not = pfx === true ? document.createTextNode('- ' + tNo + '') : document.createTextNode('' + tNo + '');
        ele.appendChild(not);
        ele.setAttribute("value", vNo);
        cmb.appendChild(ele);
    }
};

COM_HOLIDAYCOTTAGESGROUP.AddCSSClass = function(ele, val) {
    if (!ele.className) { ele.className = val; }
    else {
        newClassName = ele.className;
        newClassName += "";
        newClassName += val;
        ele.className = newClassName;
    }
};

// /home/jsquicksearch
