// FRONTEND
/**
 *
 * @package  MediaLibrary
 * @copyright 2021 Andrey Kvasnevskiy-OrdaSoft(akbet@mail.ru); Rob de Cleen(rob@decleen.com);
 * Homepage: https://ordasoft.com
 * @version: 4.6 Free
 * @license GNU General Public license version 2 or later; see LICENSE.txt
 *
 */

function ml_buttonClick(button, notselected, deletemedias){
    if (button.name=='addmedia'){
        ml_submitform('show_add_media');
        return;
    }
    if (button.name=='addpeople'){
        ml_submitform('show_add_people');
        return;
    }
    var c = document.getElementsByName('mid[]');
    for (var i=0;i<c.length;i++){
        if(c[i].checked){
            var checkedmedias = true;
            break;
        }
    }
    if (!checkedmedias){
        alert(notselected)
        return;
    }
    if (button.name=='delete'){
        resultat = confirm(deletemedias);
        if (resultat){
            ml_submitform('delete');
        }
        return;
    }
    if (button.name=='unpublish'){
        ml_submitform('unpublish');
        return;
    }
    if (button.name=='publish'){
        ml_submitform('publish');
        return;
    }
}

function ml_buttonClickLend(button){
    if(button.name=='lendout'){
        ml_submitform('lend_return');
        return;
    }
    if(button.name=='lend'){
        ml_submitform('lend');
        return;
    }
    if(button.name=='lendout_save'){
        ml_submitform('lend_return');
        return;
    }
    if(button.name=='lend_save'){
        ml_submitform('lend');
        return;
    }
}

function ml_buttonClickRentRequest(b){
    if (b.name=='acceptButton'){
        document.getElementById('adminFormTaskInput').value = 'accept_lend_requests';
    } else{
        document.getElementById('adminFormTaskInput').value = 'decline_lend_requests';
    }
    document.getElementById('adminForm').submit();
}

function ml_checkAll(all){
    var c = document.getElementsByName('mid[]');
    for(var i=0;i<c.length;i++){
        if(all.checked!=true){
            c[i].checked=false;
        } else{
            c[i].checked=true;
        }
    }
}

function ml_submitform(submit){
    var button=document.getElementById('adminFormTaskInput');
    button.value=submit;
    document.forms["adminForm"].submit();
}


function ml_submitbutton(pressbutton, mediaidcheck, isbnasincheck, categorycheck){
  
    var check = checkVideoAudio();
    var form = document.mediaEditForm;
    if (pressbutton == 'save'){
        if (trim(form.media_type_id.value) == ''){
            alert(mediaidcheck);
            return;
        } else if (trim(form.asin_isbn.value) == ''){
            alert(isbnasincheck);
            return;
        } else if (form.catid.value == '0'){
            alert(categorycheck);
            return;
        } else if (!check) {
            return;
        }else{ 
            var el = document.forms["mediaEditForm"].elements["task"];
            el.value = "save";
            el = document.getElementById("media_type_field");
            var el2 = document.getElementById("media_type");
            el.value = el2.options[el2.selectedIndex].value;
            el = document.forms["mediaEditForm"];
            el.submit();
        }
    } else { console.info(document.forms["mediaEditForm"].elements["task"]);
        var el = document.forms["mediaEditForm"].elements["task"];
        el.value = "save";
        el = document.getElementById("media_type_field");
        var el2 = document.getElementById("media_type");
        el.value = el2.options[el2.selectedIndex].value;
        el = document.forms["mediaEditForm"];
        el.submit();
    }
}

function ml_checkboxs_on(temp){
    var ul = document.getElementById(temp);
    var elems = ul.getElementsByTagName('input');
    for(var i=0;i<elems.length;i++){
        elems[i].checked =true;
    }
}

function ml_checkboxs_off(temp){
    var ul = document.getElementById(temp);
    var elems = ul.getElementsByTagName('input');
    for(var i=0;i<elems.length;i++){
        elems[i].checked =false;
    }
}

function ml_showDate(){
    if (document.mediaEditForm.search_date_box.checked){
        var x=document.getElementById("search_date_from");
        document.mediaEditForm.search_date_from.type="text";
        var x=document.getElementById("search_date_until");
        document.mediaEditForm.search_date_until.type="text";
    } else{
        var x=document.getElementById("search_date_from");
        document.mediaEditForm.search_date_from.type="hidden";
        var x=document.getElementById("search_date_until");
        document.mediaEditForm.search_date_until.type="hidden";
    }
}

function ml_lend_request_submitbutton(name, email, mailing, until){
    var form = document.userForm;
    if (form.user_name.value == ""){
        alert(name);
    } else if (form.user_email.value == "" || !ml_isValidEmail(form.user_email.value)){
        alert(email);
    } /*else if (form.user_mailing == ""){
        alert(mailing);
    } */else if (form.lend_until.value == ""){
        alert(until);
    } else{
        form.submit();
    }
}
    
function ml_isValidEmail(str){
    return (str.indexOf("@") > 1);
}

function ml_allreordering(){
    if(document.orderForm.order_direction.value=='asc'){
        document.orderForm.order_direction.value='desc';
    } else{
        document.orderForm.order_direction.value='asc';
    }
    document.orderForm.submit();
}

function ml_review_submitbutton(title, comment, rating){
    var form = document.review_m;
    // do field validation
    var rating_checked = false;
    for (c = 0;  c < form.rating.length; c++){
        if (form.rating[c].checked){
            rating_checked = true;
        } 
    }
    if (form.title.value == ""){
        alert(title);
    } else if (form.comment.value == ""){
        alert(comment);
    } else if (!rating_checked){                
        alert(rating);
    } else{
        form.submit();
    }
}
    
function ml_button_hidden(is_hide){
    var el  = document.getElementById('button_hidden_review');
    var el2 = document.getElementById('hidden_review');
    if(is_hide){
        el.style.display = 'none';
        el2.style.display = 'block';
    } else{
        el.style.display = 'block';
        el2.style.display = 'none';
    }
}


// BACKEND
function ml_submitButton(pressbutton, frmName, pageName) {
    //submit the admin form
    var f = eval( "document."+frmName );
    if( pressbutton == "cancel" ) {
        if( parent.closePanel ){
            parent.closePanel( getURLParam('panelid') );
        }
        f.func.value = "";
    }
    f.target = '_self';
    f.task.value = pressbutton;
    try {
        f.onsubmit();
    }
    catch(e){}
    f.submit();
}

function MM_findObj(n, d) { //v4.01
    var p,i,x;
    if(!d) d=document;
    if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);
    }
    if(!(x=d[n])&&d.all) x=d.all[n];
    for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n);
    return x;
}

function MM_swapImage() { //v3.0
    var i,j=0,x,a=MM_swapImage.arguments;
    document.MM_sr=new Array;
    for(i=0;i<(a.length-2);i+=3)
    if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x;
    if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_swapImgRestore() { //v3.0
    var i,x,a=document.MM_sr;
    for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function ml_submitListFunc( pressbutton, frmName, funcName ) {
    var f = eval( "document."+frmName );
    f.task.value = pressbutton;
    f.func.value = funcName;
    try {
        f.onsubmit();
    }
    catch(e){}
    f.submit();
}


// BACKEND & FRONTEND
function type_select(){
    var el = document.forms["mediaEditForm"].elements["task"];
    el.value = "show_add_media";
    el = document.getElementById("media_type_field");
    var el2 = document.getElementById("media_type");
    el.value = el2.options[el2.selectedIndex].value;
    el = document.forms["mediaEditForm"];
    el.submit();
    return;
}

/**
*    MediaLibrary Debug Function, that works like print_r for Objects in Javascript
*/
function var_dump(obj) {
    var vartext = "";
    for (var prop in obj) {
        if( isNaN( prop.toString() )) {
            vartext += "\t->"+prop+" = "+ eval( "obj."+prop.toString()) +"\n";
        }
    }
       if(typeof obj == "object") {
        return "Type: "+typeof(obj)+((obj.constructor) ? "\nConstructor: "+obj.constructor : "") + "\n" + vartext;
       } else {
          return "Type: "+typeof(obj)+"\n" + vartext;
    }
}//end function var_dump

function trim(string) {
    return string.replace(/(^\s+)|(\s+$)/g, "");
}

