<?php
$loadbaroptions = array(array('loadingbarid' => 'oncheckartistexistence', 'loadingtext' => 'Loading...'));
echo $this->Loadingimage($loadbaroptions);
?>
<form id="edit_artist_form" action="/alw/artistinfo/save" method="post">
<div>
    <!-- Film Brand (Volume) Instructions -->
    <h4><?=$this->translate('General Info')?></h4>
    <?php if ($this->productType == 'Movie') { ?>
    <div class="form-group">
        <p>
            <?php
            if ($this->userObj->isWorkstationExperienceFilm()) {
                echo $this->translate('Create a name for your Collection. It will not be shown to customers and is used primarily to group your library in analytics and accounting. Use Collection to group your films by imprint, film series, brand, director, sport or however else you\'d like to group your films.');
            } else {
                echo $this->translate('Create a name for your Volume. It will not be shown to customers and is used primarily to group your library in analytics and accounting. Use Volume to group your films by imprint, film series, brand, director, sport or however else you\'d like to group your films.');
            }
            ?>
        </p>
    </div>
    <?php } ?>
    <div class="form-group">
        <label><?=$this->translate('Name')?><font class="required"> *</font></label>
        <?=$this->form->artistName->setAttrib('id', 'createArtistModalArtistName')->setAttrib('class', 'form-control')->renderViewHelper();?>
        <div id="artistNameError" class="missingForm"><?=($this->artistNameError)?></div>
    </div>
    <?php
    if ($this->productType != 'Movie') {
    ?>
    <div class="row">
        <div class="form-group col-md-6">
            <label><?=$this->translate('Country')?><font class="required"> *</font></label>
            <?php echo $this->form->artistCountry->setAttrib('class', 'form-control')->renderViewHelper(); ?>
            <div id="artistCountryError" class="missingForm"><?=($this->artistCountryError)?></div>
        </div>
        <?php
        if (!$this->isUpdateArtistBuilderEnabled) {
            if ($this->artistDetailAllowed) { ?>
            <div class="form-group col-md-6">
                <label><?=$this->translate('Postal Code')?></label>
                <?=$this->form->artistZip->setAttrib('class', 'form-control')->renderViewHelper();?>
            </div>
            <?php
            }
        }
            ?>
    </div>
    <?php
    }
    if ($this->artistDetailAllowed) { ?>
    <div class="form-group">
        <label><?=$this->translate('Website')?></label>
        <?=$this->form->artistUrl->setAttrib('class', 'form-control')->renderViewHelper();?>
        <p class="indent smalltext helptext clear"><?=$this->translate('example: http://www.website.com')?></p>
        <div id="artistUrlError" class="missingForm"><?=($this->artistUrlError)?></div>
    </div>
    <div class="form-group">
        <label>
        <?php
        if ($this->productType == 'Movie') {
            if ($this->userObj->isWorkstationExperienceFilm()) {
                echo $this->translate('Collection Description');
            } else {
                echo $this->translate('Volume Description');
            }
        } else {
            echo $this->translate('Biography');
        }
        ?>
        </label>
        <?=$this->form->biography->renderViewHelper();?>
    </div>
    <?php if ($this->productType != 'Movie') { ?>
    <div class="form-group">
        <label><?=$this->translate('Similar Artists')?></label>
        <?=$this->form->similarArtists->setAttrib('class', 'form-control')->renderViewHelper();?>
        <p class="indent smalltext helptext clear"><?=$this->translate('example: Artist Name, Artist Name')?></p>
    </div>
    <?php }
    } ?>
    <?=$this->form->artistType->setValue($this->productType=='Movie'? 'film_collection' : 'artist')->setAttrib('class', 'form-control')->renderViewHelper();?>
</div>
</form>
<script>
function validateSelect(value){
    if(!value || value <= 0) {return false;} return true;
}
function validateURL(value, required){
    if((jQuery.trim(value) != "") || required){
        var regexp = /^(((http||https||ftp):\/\/{1})|())[A-z0-9-]{2,}([.]{1}[A-z0-9-]+)*[.][A-z]{2,}([\/]{1}[A-z0-9-!:]+)*([\?]{1}[A-z0-9-!:+=&%]*)*$/;
        if (regexp.test(value)) { return true; } return false;
    }
    return true;
}

function validateArtistName(){
    var error = 0;
    var artistName = $("input#createArtistModalArtistName").val();
    if(!validateSelect(artistName)){
        error = 1; $("#artistNameError").html("<?=$this->translate('Missing Name')?>");
    }else{
        $("#artistNameError").html("");
    }
    return error;
}
function validateArtistUrl(){
    var error = 0;
    if(!validateURL($("#artistUrl").val(), false)){
        error = 1; $("#artistUrlError").html("<?=$this->translate('Invalid Website')?>");
    }else{
        $("#artistUrlError").html("");
    }
    return error;
}
function validateArtistCountry(){
    var error = 0;
    if(!validateSelect($("#artistCountry").val())){
        error = 1; $("#artistCountryError").html("<?=$this->translate('Missing Artist Country')?>");
    }else{
        $("#artistCountryError").html("");
    }
    return error;
}

function validateForm(){
    var error = 0
    error += validateArtistName();
    error += validateArtistUrl();
    <?php if ($this->productType != 'Movie') { ?>
        error += validateArtistCountry();
    <?php } ?>
    return error;
}
function navigateAway(target_page_url){
    if(!target_page_url){
        target_page_url = "/artistinfo/index/<?=isset($this->artist->artist_id) ? 'artist_id/'.$this->artist->artist_id : ''?>";
    }
    var error = 0;
    error = validateForm();
    if(error<=0){
        var artistName = $("input#createArtistModalArtistName").val();
        var id = 'oncheckartistexistence';
        var top = ( ($(window).height() - $(id).height() ) / 2) + $(window).scrollTop() - 300 + 'px';
        var left = ( ($(window).width() - $(id).width() ) / 2) + $(window).scrollLeft() - 374 + 'px';
        showLoadingImage(id,top,left);
        $.ajax({
            type: "POST",
            url: "/artistinfo/checkartistexistence",
            data: "format=json&artistName=" + encodeURIComponent(artistName),
            contentType: 'application/x-www-form-urlencoded; charset=UTF-8',
            success: function(xhr, textStatus, errorThrown){
                <?php
                if ($this->ajax) {
                ?>
                        tinyMCE.triggerSave();//explicitly call to populate original boxes, as the form is not getting submmited
                        var form_data = $("#edit_artist_form").serialize();
                        $.ajax({
                            type: "POST",
                            url: "/artistinfo/save",
                            data: "format=json&ajax=1&"+form_data,
                            success: function(xhr, textStatus, errorThrown){
                                $('select#artistName').html($('select#artistName').html()+'<option value="'+xhr.artist_id+'">'+xhr.artist_name+'</option>');
                                $('select#artistName').val(xhr.artist_id);
                                $('#artistName').change();
                                <?=$this->modal_window_id?>.close();
                                showControls();
                            },
                            error: function(res, textStatus, errorThrown) {
                                var error = eval("("+res.responseText+")");
                                var error_arr = new Array();
                                for(var i in error.error){
                                    error_arr.push(error.error[i]);
                                }
                                alert(error_arr.join("\n"));
                            }
                        });
                    <?php
                } else { ?>
                    $("#edit_artist_form").submit();
                <?php
                }
                ?>
            },
            error: function(res, textStatus, errorThrown) {
                var obj = jQuery.parseJSON(res.responseText);
                if(obj.code === 'ARTIST_ALREADY_EXISTS') {
                    $('#oncheckartistexistence').hide();
                    $("#artistNameError").html(obj.error);
                }
            }
        });
    }
}
function navMouseOver(element){
    $(element).stop().animate({width: '303px'}, "fast");
}
function navMouseOut(element){
    $(element).stop().animate({width: '284px'}, "fast");
}
$(document).ready(function(){
    $("input#createArtistModalArtistName").blur(validateArtistName);
    <?php if ($this->productType != 'Movie') { ?>
        $("select#artistCountry").blur(validateArtistCountry);
    <?php } ?>
    $("input#artistUrl").blur(validateArtistUrl);
});

<?=$this->Richtextbox('biography', array('width' =>560, 'theme_advanced_resizing' => 'true', 'theme_advanced_resizing_max_width' => 550, 'height' => 400))?>
/** hide the controls behind this modal to not have an overflowing behind this modal, modal */
var tooltipClassName = '.tooltip';
$(document).ready(function() {
    $('#createReleaseControls').hide();
    $(tooltipClassName).hide();
    $('.close').click(function() {
        showControls();
    });
});

/**
 * show the controls again on the behind modal
 */
function showControls() {
    $('#createReleaseControls').fadeIn();
    $(tooltipClassName).each(function(index) {
        if ($('p' , $this).text().length > 0) {
            $this.fadeIn();
        } else {
            $this.fadeOut();
        }
    });
}

function showLoadingImage(loadingbarid,top,left){
    var id = "#"+loadingbarid;
    $(id).css('top', top);
    $(id).css('left', left);
    $(id).show();
}
</script>
