/*
    Trims whitespaces from a string
    usage: string.trim()
*/
String.prototype.trim = function () {
    var regexp = /^\s*|\s*$/g;
    return this.replace(regexp,"");
}

//Using events_common.js to add eventhandler.
addEventHandler(window, "load", prepareToggleDescription);
addEventHandler(window, "load", prepareToggleHoliday);


//isToggleActivated - If toggle is activated or not.
var isToggleActivated = true;
/*
Name: toggleDescription
Purpose: Toggles helptexts for an element on page.
Arguments: e - fired event.
*/
function toggleDescription(e) {
    //Get target element (where event fired)
    var targ = getTargetElement(e);
    var style = document.getElementById("description_" + targ.id).style;
    if (style.display == 'block') {
        if (isToggleActivated) {
            style.display = 'none';
        }
    } else {
        style.display = 'block';
    }
}

/*
Name: toggleHoliday
Purpose: Toggles holiday opening times.
Arguments: e - fired event.
*/
function toggleHoliday(e) {
    //Get target element (where event fired)
    var targ = getTargetElement(e);
    var style = document.getElementById("holiday_" + targ.id).style;
    if (style.display == 'block') {
        if (isToggleActivated) {
            style.display = 'none';
        }
    } else {
        style.display = 'block';
    }
}

/*
Name: hideDescription
Purpose: Hides helptext for an element on page.
Arguments: e - fired event.
*/
function hideDescription(e) {
    //Get target element (where event fired)
    var targ = getTargetElement(e);
    var elem = targ.parentNode;

    var style = elem.style;
    if (style.display == 'block') {
        style.display = 'none';
    }
}

/*
Name: hideHoliday
Purpose: Hides holiday opening times.
Arguments: e - fired event.
*/
function hideHoliday(e) {
    //Get target element (where event fired)
    var targ = getTargetElement(e);
    var elem = targ.parentNode;

    var style = elem.style;
    if (style.display == 'block') {
        style.display = 'none';
    }
}

/*
Name: getForAttribute
Purpose: Get the for attribute for a given element (probably a label)
Arguments: element
Returns: for attribute
*/
function getForAttribute(element) {
    var attribute;
    if (element.getAttribute("for") == null) {
        attribute = element.attributes['for'].value; // Required by IE6 and IE7
    } else {
        attribute = element.getAttribute("for"); // Required by Safari (for Mac)
    }
    return attribute;
}

/*
Name: showHideOthersInGroup
Purpose: Show/hide a helptext and automatically hide others in the same group
Arguments: e - fired event.
*/
function showHideOthersInGroup(e) {
    var targ = getTargetElement(e);
    var labelForID = getForAttribute(targ);
    var groupName = document.getElementById(labelForID).name;
    var groupElements = document.getElementsByName(groupName);
    var labels = document.getElementsByTagName("label");

    for (var i = 0; i < groupElements.length; i++) {

        // Don't hide the helptext for the label that was clicked
        if (labelForID == groupElements[i].id) continue;
        // Get the group id
        var groupID = groupElements[i].getAttribute("id");

        // Hide other helptext descriptions
        for (var j = 0; j < labels.length; j++) {
            // Find labels with the class "hideOthers"
            if (labels[j].className.indexOf("hideOthers") != -1) {
                // Get the label's for-attribute
                var tempForValue = getForAttribute(labels[j]);
                // Compare the for-attribute with the clicked
                if (tempForValue == groupID) {
                    // Get the found label's id-attribute
                    var foundLabelId = labels[j].getAttribute("id");
                    // Find the matching description layer using id-attribute and hide the layer
                    var style = document.getElementById("description_" + foundLabelId).style;
                    style.display = 'none';
                }
            }
        }
    }
    // Show / hide clicked label description
    toggleDescription(e);
}

/*
Name: showHideOthersInGroupHoliday
Purpose: Show/hide a holiday opening times
Arguments: e - fired event.
*/
function showHideOthersInGroupHoliday(e) {
    var targ = getTargetElement(e);
    var labelForID = getForAttribute(targ);
    var groupName = document.getElementById(labelForID).name;
    var groupElements = document.getElementsByName(groupName);
    var labels = document.getElementsByTagName("label");

    for (var i = 0; i < groupElements.length; i++) {

        // Don't hide the helptext for the label that was clicked
        if (labelForID == groupElements[i].id) continue;
        // Get the group id
        var groupID = groupElements[i].getAttribute("id");

        // Hide other helptext descriptions
        for (var j = 0; j < labels.length; j++) {
            // Find labels with the class "hideOthers"
            if (labels[j].className.indexOf("hideOthersHoliday") != -1) {
                // Get the label's for-attribute
                var tempForValue = getForAttribute(labels[j]);
                // Compare the for-attribute with the clicked
                if (tempForValue == groupID) {
                    // Get the found label's id-attribute
                    var foundLabelId = labels[j].getAttribute("id");
                    // Find the matching description layer using id-attribute and hide the layer
                    var style = document.getElementById("holiday_" + foundLabelId).style;
                    style.display = 'none';
                }
            }
        }
    }
    // Show / hide clicked label description
    toggleHoliday(e);
}
/*
Name: prepareToggleDescription
Purpose: Adds eventhandlers for toggling descriptions (helptexts).
Arguments: None.
*/
function prepareToggleDescription() {
    var labels = document.getElementsByTagName("label");
    var divs = document.getElementsByTagName("div");

    //Add eventhandlers for label-elements with correct classname.
    for (var i = 0; i < labels.length; i++) {
        //Add eventhandlers for groups of descriptions that need to hide others
        if (labels[i].className.indexOf("hideOthers") != -1) {
            addEventHandler(labels[i], "click", showHideOthersInGroup);
        }
        if (labels[i].className == "descriptionShow") {
            addEventHandler(labels[i], "click", toggleDescription);
        }
    }
    //Add eventhandlers for div-elements with correct classname.
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].className == "descriptionClose") {
            addEventHandler(divs[i], "click", hideDescription);
        }
        if (divs[i].className == "descriptionShow") {
            addEventHandler(divs[i], "click", toggleDescription);
        }
    }
}

/*
Name: prepareToggleHoliday
Purpose: Adds eventhandlers for toggling holiday opening times.
Arguments: None.
*/
function prepareToggleHoliday() {
    var labels = document.getElementsByTagName("label");
    var divs = document.getElementsByTagName("div");

    //Add eventhandlers for label-elements with correct classname.
    for (var i = 0; i < labels.length; i++) {
        //Add eventhandlers for groups of descriptions that need to hide others
        if (labels[i].className.indexOf("hideOthersHoliday") != -1) {
            addEventHandler(labels[i], "click", showHideOthersInGroupHoliday);
        }
        if (labels[i].className == "holidayShow") {
            addEventHandler(labels[i], "click", toggleHoliday);
        }
    }
    //Add eventhandlers for div-elements with correct classname.
    for (var i = 0; i < divs.length; i++) {
        if (divs[i].className == "holidayClose") {
            addEventHandler(divs[i], "click", hideHoliday);
        }
        if (divs[i].className == "holidayShow") {
            addEventHandler(divs[i], "click", toggleHoliday);
        }
    }
}

/*
Name: setFocusOnload
Purpose: functionality for setting focus on a formfield onload.
Arguments: the Id of the formfield
*/
function setFocus(elementId){
    document.getElementById(elementId).focus();
}