<?php
require_once APPLICATION_PATH . '/modules/alw/views/helpers/Loadjs.php';
require_once APPLICATION_PATH . '/modules/alw/views/helpers/Loadingimage.php';

$loadJs = new Zend_View_Helper_loadjs();
$loadimage = new Alw_View_Helper_Loadingimage();
$jqueryCdnPath = $loadJs->getCdnAlwScriptPath('jquery.autocomplete.js');
$calendarImageCdnPath = $loadimage->getCdnImagePath('calendar.svg');

$acInputWidth = 'acInputWidth';
?>
<script src="<?php echo $jqueryCdnPath ?>"></script>

<script type="text/javascript">
    var addValidation = function(currentSelector) {
        $(currentSelector).parents('.form-group').addClass('invalid-input');
    }
    var removeValidation = function(currentSelector) {
        $(currentSelector).parents('.form-group').removeClass('invalid-input');
    }
    var found_result = 0;
    $(document).ready(function(){
        var acInputWidth = $('#venue_name').innerWidth();
        $('#venue_name').autocomplete('/artisttourdates/searchvenueautocomplete'
            ,{
                width: <?=$acInputWidth?>,
                selectFirst:false,
                matchContains:true
                ,formatItem: function(row, i, max, term){
                    return str_replace(row[0], '|', row[1] + ', ' + row[4]);
                 }
                ,formatResult: function(row) {
                    return str_replace(row[0], '|', row[1]);
                }
            }
        ).result(function(event, data, formatted) {
            if(data[0]){
                $('#venue_name').val(data[1]);
                $('#address').val(data[3]);
                $('#city').val(data[4]);
                $('#country').val(data[5]);
                if (data[5] == 1) {
                    $('#state_id_li').show();
                    $('#other_state_li').hide();
                    $('#state_id').val(data[7]);
                }
                else {
                    $('#other_state_li').show();
                    $('#state_id_li').hide();
                    $('#other_state').val(data[8]);
                }
                $('#zip').val(data[6]);
                $('#website').val(data[10]);
            }
        });
    });

    //to display date picker for show date field
    function showDate() {
        var buttonImage = '<?php echo $calendarImageCdnPath ?>';
        $('#show_date').datepicker({
            dateFormat: 'yy-mm-dd',
            showOn: 'both',
            buttonImage: buttonImage,
            buttonImageOnly: true
        });
    }

    //to display date picker for ticket sales date field
    function ticketOnSaleDate() {
        var buttonImage = '<?php echo $calendarImageCdnPath ?>';
        $('#ticket_on_sale_date').datepicker({
            dateFormat: 'yy-mm-dd',
            showOn: 'both',
            buttonImage: buttonImage,
            buttonImageOnly: true
        });
    }

</script>

<div id="content" class="content-div">
    <div id="bread">
        <a href="/artist"><?=$this->translate('All Artists')?></a>
    </div>
    <h1>
        <span><?=$this->artistImages($this->artist->artist_id, $this->artist->name);?></span> <?=$this->artist->name?>
    </h1>
    <!-- Start Artist Builder Wrap-->
    <div id="artistBuilderWrap">
        <!-- Start Left Content-->
        <div id="contentLeftsm">
            <?=$this->artistBuilderNavigation('tour_date', '', $this->performanceArtistIndexEnabled);?>
        </div>
        <!-- End Left Content-->
        <!-- Start Right Content-->
        <div id="contentRightlg">
            <p class="col-md-12 back-to-tour-dates">
                <a class="btn btn-default" href="/artisttourdates/index/artist_id/<?=$this->artist->artist_id?>">
                    <img src="/images/icons/back-to.svg" class="right-space" /> <?=$this->translate('Back to current Tour Dates')?>
                </a>
            </p>
            <form id="edit_tourdate_form" action="/artisttourdates/edit<?=(isset($this->tourdate->id) ? "/id/{$this->tourdate->id}" : '');?>" method="post">
                <div id="artistFormWrap">
                    <div class="clear"></div>
                    <label class="require-text"><?=$this->translate('Required')?></label>
                    <div class="col-md-7">
                        <h2>
                            <?php if (isset($this->tourdate)) {
                                echo $this->translate('Edit A Tour Date');
                            } else {
                                echo $this->translate('Add A Tour Date');
                            } ?>
                        </h2>
                        <div class="tours-info-form">
                            <div class="form-group">
                                <label><?=$this->translate('Show Date')?>*</label>
                                <?php echo $this->form->show_date->setAttrib('onfocus', 'showDate')
                                    ->setAttrib('class', 'form-control')
                                    ->setAttrib('placeholder', 'YYYY-MM-DD')->renderViewHelper();
                                ?>
                                <p id="show_date_error" class="error top-space displayOff"></p>
                            </div>
                            <div class="form-group">
                                <label><?=$this->translate('Venue Name')?>*</label>
                                <?=$this->form->venue_name->setAttrib('class', 'form-control full-width')->renderViewHelper()?>
                                <p id="venue_name_error" class="error top-space displayOff"></p>
                            </div>
                            <div class="form-group">
                                <label><?=$this->translate('Address')?>*</label>
                                <?=$this->form->address->setAttrib('class', 'form-control')->renderViewHelper()?>
                                <p id="address_error" class="error top-space displayOff"></p>
                                <p id="address_length_error" class="error top-space displayOff"></p>
                            </div>
                            <div class="form-group">
                                <label><?=$this->translate('City')?>*</label>
                                <?=$this->form->city->setAttrib('class', 'form-control')->renderViewHelper()?>
                                <p id="city_error" class="error top-space displayOff"></p>
                                <p id="city_length_error" class="error top-space displayOff"></p>
                            </div>
                            <div class="form-group">
                                <label><?=$this->translate('Country')?>*</label>
                                <?=$this->form->country->setAttrib('class', 'form-control')->renderViewHelper()?>
                                <p id="country_error" class="error top-space displayOff"></p>
                            </div>
                            <div id="state_id_li" class="form-group">
                                <label><?=$this->translate('State')?>*</label>
                                <?=$this->form->state_id->setAttrib('class', 'form-control')->renderViewHelper()?>
                                <p id="state_id_error" class="error top-space displayOff"></p>
                            </div>
                            <div id="other_state_li" class="form-group">
                                <label><?=$this->translate('Region (Province)')?></label>
                                <?=$this->form->other_state->setAttrib('class', 'form-control')->renderViewHelper()?>
                                <p id="other_state_error" class="indent missing displayOff"></p>
                            </div>
                            <div class="form-group">
                                <label><?=$this->translate('Postal Code')?></label>
                                <?=$this->form->zip->setAttrib('class', 'form-control')->renderViewHelper()?>
                                <p id="zip_error" class="indent missing displayOff"></p>
                            </div>
                            <div class="form-group">
                                <label><?=$this->translate('Venue Website')?></label>
                                <?=$this->form->website->setAttrib('class', 'form-control')->renderViewHelper()?>
                                <p id="website_error" class="indent missing displayOff"></p>
                                <p class="top-space example-text clear"><?=$this->translate('Example: http://www.theorchard.com')?></p>
                            </div>
                            <div class="form-group">
                                <label><?=$this->translate('Show Time')?></label>
                                <?=$this->form->show_time->setAttrib('class', 'form-control')->renderViewHelper()?>
                                <p id="show_time_error" class="error top-space displayOff"></p>
                                <p class="top-space example-text clear"><?=$this->translate('Example: 9:00 pm')?></p>
                            </div>
                            <div class="form-group">
                                <label><?=$this->translate('Ticket Price')?></label>
                                <?=$this->form->ticket_price->setAttrib('class', 'form-control')->renderViewHelper()?>
                                <p id="ticket_price_error" class="indent missing displayOff"></p>
                                <p class="top-space example-text clear"><?=$this->translate('Example: $10.00')?></p>
                            </div>
                            <div class="form-group">
                                <label><?=$this->translate('Age Limit')?></label>
                                <?=$this->form->age_limit->setAttrib('class', 'form-control')->renderViewHelper()?>
                                <p id="age_limit_error" class="indent missing displayOff"></p>
                                <p class="top-space example-text clear"><?=$this->translate('Example: 21+')?></p>
                            </div>
                            <div class="form-group checkbox">
                                <?=$this->form->sold_out->renderViewHelper()?>
                                <label for="sold_out"><?=$this->translate('Sold Out')?></label>
                            </div>
                            <div class="form-group">
                                <label><?=$this->translate('Tickets On Sale')?></label>
                                <?php echo $this->form->ticket_on_sale_date->setAttrib('onfocus', 'ticketOnSaleDate')
                                    ->setAttrib('class', 'form-control')
                                    ->setAttrib('placeholder', 'YYYY-MM-DD')->renderViewHelper();
                                ?>
                            </div>
                            <div id="ticket_link_list">
                                <?php if (isset($this->tourdate)) {
                                    foreach ($this->tourdate->TourdateBuylinks as $tourDateBuyLink) { ?>
                                        <div class="form-group">
                                            <label><?=$this->translate('Ticket Link')?></label>
                                            <div class="row">
                                                <div class="col-xs-11">
                                                    <input name="ticket_links[]" value="<?=$tourDateBuyLink->url ?>" type="text" class="ticket_urls form-control" />
                                                </div>
                                                <a class="remove-link handPointer pull-left" onclick="$(this).parent().parent().remove()">
                                                    <img src="/images/icons/statusX-lg.png"/>
                                                </a>
                                            </div>
                                        </div>
                                        <div class="clear"></div>
                                    <?php }
                                } ?>
                            </div>
                            <div class="form-group">
                                <label><?=$this->translate('Ticket Link')?></label>
                                <div class="input-group zero-index">
                                    <input id="ticket_link" name="ticket_links[]" value="" type="text" class="ticket_urls form-control" tabindex="15"></input>
                                    <div class="input-group-btn space-left">
                                        <img src="/images/icons/add-link.svg" onClick="addTicketLink();" class="handPointer" />
                                    </div>
                                </div>
                                <p id="ticket_link_error" class="indent missing displayOff"></p>
                            </div>
                            <div class="form-group">
                                <label><?=$this->translate('More Info')?></label>
                                <?=$this->form->more_info->setAttrib('class', 'form-control')->renderViewHelper()?>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-12 submit-tab-info">
                        <a class="btn btn-default pageFormBtn" href="/artisttourdates/index/artist_id/<?=$this->artist->artist_id?>">
                            <span><?=$this->translate('Cancel')?></span>
                        </a>
                        <button type="button" class="btn btn-primary pageFormBtn" onclick="navigateAway('');">
                            <span><?=$this->translate('Save')?></span>
                        </button>
                    </div>
                    <div class="clear"></div>
                </div>
                <?=$this->form->artist_id->renderViewHelper();?>
                <?=$this->form->navigateAwayUrl->renderViewHelper();?>
            </form>
        <!-- End Right Content-->
        </div>
    <!-- End Artist Builder Wrap-->
    </div>
</div>
<script type="text/javascript">

    $('#show_date').click(function() {
        $('#show_date_error').hide();
        removeValidation('#show_date_error');
    });
    $('#venue_name').click(function() {
        $('#venue_name_error').hide();
        removeValidation('#venue_name_error');
    });
    $('#address').click(function() {
        $('#address_error').hide();
        $('#address_length_error').hide();
        removeValidation('#address_error, #address_length_error');
    });
    $('#city').click(function() {
        $('#city_error').hide();
        $('#city_length_error').hide();
        removeValidation('#city_error, #city_length_error');
    });
    $('#country').click(function() {
        $('#country_error').hide();
        removeValidation('#country_error');
    });
    $('#website').click(function() {
        $('#website_error').hide();
    });
    $('#show_time').click(function() {
        $('#show_time_error').hide();
        removeValidation('#show_time_error');
    });

    function addTicketLink() {
        var new_ticket_link_value = $('#ticket_link').val();
        $('#ticket_link_list').append('\
            <div class="form-group">\
                <label>Ticket Link</label>\
                <div class="input-group zero-index">\
                    <input name="ticket_links[]" value="' + new_ticket_link_value + '" type="text" class="ticket_urls form-control">\
                    <a class="input-group-btn space-left handPointer" onclick="$(this).parent().parent().remove()"><img src="/images/icons/statusX-lg.png"/></a>\
                </div>\
            </div>');
        $('#ticket_link').val('');
        $('#ticket_link_error').text('');
        $('#ticket_link_error').hide();
    }

    function navigateAway(target_page_url) {

        // Setup default target page url
        if (!target_page_url) {
            target_page_url = '/artisttourdates/index/artist_id/<?=$this->artist->artist_id?>';
        }

        // Function definitions
        function validateSelect(value) {
            if (!value) {
                return false;
            }
            return true;
        }
        function validateURL(value) {
            /*
            if(jQuery.trim(value) == ""){
                return true;
            }
            var regexp = /^(?:\w*:\/\/)?((?:[\w-_]*\.?)+:?\d*(?:\/?[\w-_.]+\/?)*)[\?]?[\&]?([\w-_]*)?=?([\/\w-_\?\)%\.\(\*\[\]\^<>\\]*)?[\&]?([\w-_]*)?=?([\/\w-_\?\)%\.\(\*\[\]\^<>\\]*)?[\&]?([\w-_]*)?=?([\/\w-_\?\)%\.\(\*\[\]\^<>\\]*)?[\&]?([\w-_]*)?=?([\/\w-_\?\)%\.\(\*\[\]\^<>\\]*)?$/;
            if (regexp.test(value)) { return true; } return false;
            */
            return true;
        }

        function validateDate(value) {
            if (jQuery.trim(value) == '') {
                return true;
            }
            var regexp = /^\d{4}-\d{2}-\d{2}$/;
            if (regexp.test(value)) {
                return true;
            }
            return false;
        }

        function validateTime(value) {
            if (jQuery.trim(value) == '') {
                return true;
            }
            var regexp = /^([0-9]|1[0-2]|0[0-9])[:|.|,][0-5][0-9][\s]*(am|pm|AM|PM|Am|Pm)$/;
            if (regexp.test(value)) {
                return true;
            }
            return false;
        }

        function validateAddressLength($value) {
            if ($value.length > 100) {
                return false;
            }
            return true;
        }

        function validateCityLength($value) {
            if ($value.length > 100) {
                return false;
            }
            return true;
        }

        // Error checking
        var error = 0

        if (!validateAddressLength($('#address').val())) {
            error++;
            $('#address_length_error').show();
            $('#address_length_error').html('<?=$this->escapejs($this->translate('At max 100 characters are allowed.'))?>');
            addValidation('#address_length_error');
        }

        if (!validateCityLength($('#city').val())) {
            error++;
            $('#city_length_error').show();
            $('#city_length_error').html('<?=$this->escapejs($this->translate('At max 100 characters are allowed.'))?>');
            addValidation('#city_length_error');
        }

        if (!validateSelect($('#show_date').val())) {
            error++;
            $('#show_date_error').show();
            $('#show_date_error').html('<?=$this->escapejs($this->translate('required'))?>');
            $('#show_date_error').html('<?=$this->escapejs($this->translate('Date required'))?>');
            addValidation('#show_date_error');
        } else {
            if (!validateDate($('#show_date').val())) {
                error++;
                $('#show_date_error').show();
                $('#show_date_error').html('<?=$this->escapejs($this->translate('Invalid date format'))?>');
                addValidation('#show_date_error');
            }
        }

        if (!validateSelect($('#address').val())) {
            error++;
            $('#address_error').show();
            $('#address_error').html('<?=$this->escapejs($this->translate('required'))?>');
            $('#address_error').html('<?=$this->escapejs($this->translate('Address required'))?>');
            addValidation('#address_error');
        } else {
            $('#address_error').html('');
            removeValidation('#address_error');
        }

        if (!validateSelect($('#venue_name').val())){
            error++;
            $('#venue_name_error').show();
            $('#venue_name_error').html('<?=$this->escapejs($this->translate('required'))?>');
            $('#venue_name_error').html('<?=$this->escapejs($this->translate('Venue name required'))?>');
            addValidation('#venue_name_error');
        } else {
            $('#venue_name_error').html('');
            removeValidation('#venue_name_error');
        }

        if (!validateSelect($('#city').val())) {
            error++;
            $('#city_error').show();
            $('#city_error').html('<?=$this->escapejs($this->translate('required'))?>');
            $('#city_error').html('<?=$this->escapejs($this->translate('City required'))?>');
            addValidation('#city_error');
        } else {
            $('#city_error').html('');
            removeValidation('#city_error');
        }

        if (!validateSelect($('#country').val())) {
            error++;
            $('#country_error').show();
            $('#country_error').html('<?=$this->escapejs($this->translate('required'))?>');
            $('#country_error').html('<?=$this->escapejs($this->translate('Country required'))?>');
            addValidation('#country_error');
        } else if ($('#country option:selected').val() == '1') {
            if (!validateSelect($('#state_id').val())) {
                error++;
                $('#state_id_error').show();
                $('#state_id_error').html('<?=$this->escapejs($this->translate('required'))?>');
                $('#state_id_error').html('<?=$this->escapejs($this->translate('State required'))?>');
                addValidation('#state_id_error');
            } else {
                $('#country_error, #state_id_error').html('');
                removeValidation('#country_error, #state_id_error');
            }
        } else {
            $('#country_error').html('');
            removeValidation('#country_error');
        }

        if (!validateURL($('#website').val())) {
            error++;
            $('#website_error').show();
            $('#website_error').html('<?=$this->escapejs($this->translate('Invalid website url'))?>');
        }

        if (!validateTime($('#show_time').val())) {
            error++;
            $('#show_time_error').show();
            $('#show_time_error').html('<?=$this->escapejs($this->translate('Invalid time format'))?>');
            addValidation('#show_time_error');
        }
        /** validate URLs */
        if (!validateURL($('#ticket_link').val())) {
            error++;
            $('#ticket_link_error').text('<?=$this->escapejs($this->translate('Please Enter a Valid URL'))?>');
            $('#ticket_link_error').show();
        }

        /** validate any additional URLs */
        $('.ticket_urls').each(function(index) {
            if (!validateURL($(this).val())) {
                error++;
                $(this).next().next().text('<?=$this->escapejs($this->translate('Please Enter a Valid URL'))?>');
                $(this).next().next().show();
            }
        });

        // We're good to go - submit the form!
        if (error == 0) {
            $("#navigateAwayUrl").val(target_page_url);
            edit_tourdate_form.appendChild(CSRFP._getInputElt());
            $("#edit_tourdate_form").submit();
        }
    }

    $('#country').change(function() {
        if ($("#country option:selected").val() == '1') {
            $('#state_id_li').show();
            $('#other_state_li').hide();
        } else {
            $('#other_state_li').show();
            $('#state_id_li').hide();
        }
    }).trigger('change');

    /** DOM becomes ready, apply JQuery */
    $(document).ready(function () {
        showDate();
        ticketOnSaleDate();
        if ($('#country option:selected').val() == '') {
            $('#other_state_li').hide();
        }
        if ($('#country option:selected').val() != '1') {
            $('#state_id_li').hide();
        }
        <?php if (is_array($this->errors) && count($this->errors)) {
            foreach ($this->errors as $key => $error) { ?>
                $('#<?=$key?>_error').html('<?=$this->escape(array_shift($error))?>');
            <?php }
        } ?>
    });
</script>
