﻿function AddPricingToList(pricingHiddenField, pricesList, priceTextBox, priceTypeDropDown, otherPriceTypeTextBox) {
    AddPricingToList2(pricingHiddenField, pricesList, priceTextBox.value, priceTypeDropDown.value,
        priceTypeDropDown[priceTypeDropDown.selectedIndex].text.toLowerCase(), otherPriceTypeTextBox.value);
}

var otherPriceTypeID = 6;
function AddPricingToList2(pricingHiddenField, pricesList, price, priceTypeInt, priceTypeText, otherPriceType) {
    if (price != '' && priceTypeInt != '' && (priceTypeInt != otherPriceTypeID || otherPriceType != '')) {
        var nonIntRegEx = /^\d+$/;
        var result = nonIntRegEx.exec(price);
        if (result == undefined) {
            // non-digit characters found
            alert('Price must be an integer');
            return;
        }

        var className = 'Pricing';
        var optionToAdd = price + '-' + priceTypeInt + '-';

        var priceType = priceTypeText;
        if (priceTypeInt == otherPriceTypeID) {
            //Other
            optionToAdd = optionToAdd + otherPriceType.replace('_', ' ').replace('-', ' ');
            priceType = otherPriceType;
        }

        var innerHtml = '<div class="Price">$' + price + '</div><div class="PriceType">' + priceType +
            '</div><div class="FloatLeft"><a href="#" onclick="RemoveItemFromList(\'' +
            pricingHiddenField.id + '\',\'' + pricesList.id + '\',\'' + optionToAdd.replace(/'/g, '\\\'') +
            '\'); return false;">Remove</a></div><div class="Clear"></div>';

        AddItemToList(pricingHiddenField, pricesList, optionToAdd, innerHtml, className);
    }
    else {
        alert('Please specify a price and price type');
    }
}

function AddBusinessHoursToList(businessHoursHiddenField, hoursList, dayOfWeekDropDown, openingTimeTextBox, closingTimeTextBox) {
    if (AddBusinessHoursToList2(businessHoursHiddenField, hoursList, dayOfWeekDropDown.value, dayOfWeekDropDown[dayOfWeekDropDown.selectedIndex].text,
        openingTimeTextBox.value, closingTimeTextBox.value, dayOfWeekDropDown)) {
        if (dayOfWeekDropDown.selectedIndex < dayOfWeekDropDown.options.length - 1) {
            dayOfWeekDropDown.selectedIndex = dayOfWeekDropDown.selectedIndex + 1;
        }
        else {
            dayOfWeekDropDown.selectedIndex = 0;
        }
    }
}

function AddBusinessHoursToList2(businessHoursHiddenField, hoursList, dayOfWeekInt, dayOfWeekText, openingTimeText, closingTimeText, dayOfWeekDropDown) {
    if (dayOfWeekInt != '' && openingTimeText != '' && closingTimeText != '') {
        var openingTime = ParseTime(openingTimeText, 'am');
        var closingTime = ParseTime(closingTimeText, 'pm');
        if (openingTime == undefined || closingTime == undefined) {
            alert('Opening and closing time should be entered in the format Hours:Minutes(am/pm). eg: 9:30am');
            return;
        }

        openingTimeText = openingTime;
        closingTimeText = closingTime;

        var optionToAdd = dayOfWeekInt + '-' + openingTimeText + '-' + closingTimeText;
        var innerHtml = '<div class="DayOfWeek">' + dayOfWeekText + '</div>' +
                    '<div class="Time">' + openingTimeText + '</div>' + '<div class="Time">' + closingTimeText +
                    '</div><div class="FloatLeft"><a href="#" onclick="RemoveItemFromList(\'' +
                    businessHoursHiddenField.id + '\',\'' + hoursList.id + '\',\'' + optionToAdd +
                    '\'); return false;">Remove</a></div><div class="Clear"></div>';

        return AddItemToList(businessHoursHiddenField, hoursList, optionToAdd, innerHtml, 'BusinessHour');
    }
    else {
        alert('Please specify both opening and closing times for a day');
    }
    return false;
}

function AddItemToList(hiddenField, listContainer, optionToAdd, optionDisplayHtml, optionDivClass) {
    var delimitedOption = '|' + optionToAdd + '|';
    if (hiddenField.value.indexOf(delimitedOption) <= -1) {
        if (hiddenField.value == '') {
            hiddenField.value = '|';
        }
        hiddenField.value = hiddenField.value + optionToAdd + '|';

        var optionControl = document.createElement('div');
        optionControl.id = listContainer.id + '_' + optionToAdd;
        optionControl.className = optionDivClass;
        optionControl.innerHTML = optionDisplayHtml;

        listContainer.appendChild(optionControl);

        if (listContainer.childNodes.length > 0) {
            listContainer.style.display = '';
        }
        
        return true;
    }
    return false;
}

function RemoveItemFromList(hiddenFieldId, listId, optionToRemove) {
    var optionControlId = listId + '_' + optionToRemove;

    var selectedValuesField = document.getElementById(hiddenFieldId);
    var delimitedOption = '|' + optionToRemove + '|';

    selectedValuesField.value = selectedValuesField.value.replace(delimitedOption, "|");

    var selectedValuesContainer = document.getElementById(listId);
    var elementToDelete;
    for (var i = 0; i < selectedValuesContainer.childNodes.length; i++) {
        if (selectedValuesContainer.childNodes[i].id == optionControlId) {
            elementToDelete = selectedValuesContainer.childNodes[i];
        }
    }

    selectedValuesContainer.removeChild(elementToDelete);

    if (selectedValuesContainer.childNodes.length == 0) {
        selectedValuesContainer.style.display = 'none';
    }
}

function ParseTime(timeString, ampmDefault) {
    var timeRegEx = /(\d{1,2}):{0,1}(\d{0,2})\s*(am|pm){0,1}/;
    var result = timeRegEx.exec(timeString);
    if (result != undefined) {
        if (result.length >= 4) {
            if (result[0] != undefined && result[1] != undefined) {
                var hour = result[1];
                var minute = '00';
                var ampm = ampmDefault;
                if (result[2] != undefined && result[2] != '') {
                    minute = result[2];
                }

                if (result[3] != undefined && result[3] != '') {
                    ampm = result[3];
                }

                if (hour > 12) {
                    ampm = 'pm';
                    hour = hour - 12;
                }

                return hour + ':' + minute + ampm;
            }
        }
    }
    return undefined;
}

function ShowHideOnOther(dropDownID, showHideDivID, showOnOther, otherValue) {
    var dropDown = document.getElementById(dropDownID);
    var showHideDiv = document.getElementById(showHideDivID);

    var show;
    if (dropDown.value == otherValue) {
        show = showOnOther;
    }
    else {
        show = !showOnOther;
    }

    if (show) {
        showHideDiv.style.display = 'block';
    }
    else {
        showHideDiv.style.display = 'none';
    }
}

function ToggleAdAddressPanel(businessAddressContainerID, adAddressContainerID, currentHiddenID) {
    var currentHidden = $get(currentHiddenID);
    if (currentHidden.value == 'Advertisement') {
        currentHidden.value = 'Business';
        $get(businessAddressContainerID).className = '';
        $get(adAddressContainerID).className = 'Hidden';
    }
    else {
        currentHidden.value = 'Advertisement';
        $get(businessAddressContainerID).className = 'Hidden';
        $get(adAddressContainerID).className = '';
    }
    
}
