function resize(img){
    if(img.height > img.width){
        var wi = eval(img.width);
        var he = eval(img.height);
        img.width = Math.round(530 * (wi / he));
        img.height = 530;
    }
}

function resize_list(img){
    if(img.height > img.width){
        var wi = eval(img.width);
        var he = eval(img.height);
        img.width = Math.round(130 * (wi / he));
        img.height = 130;
    }
}

function is_resize(img){
    if(img.height > img.width){
        var wi = eval(img.width);
        var he = eval(img.height);
        img.width = Math.round(93 * (he / wi));
        img.height = 93;
    }
    else{
        img.width = 128;
        img.height = 93;
    }
}
function addComment(type, id) {
    myRef = window.open('add_comment.jsp?type=' + type + '&id=' + id, 'AddComment', 'left=250,top=250,width=500,height=200,toolbar=0,menubar=0,scrollbars=0,statusbar=0,resizable=1');
}

function viewComment(type, id) {
    myRef = window.open('view_comment.jsp?type=' + type + '&id=' + id, 'viewComment', 'left=250,top=250,width=500,height=500,toolbar=0,menubar=0,scrollbars=1,statusbar=0,resizable=1');
}

function recalculatePrice(type, id) {
    myRef = window.open('my_cart_recalculate_price.jsp?type=' + type + '&item_id=' + id, 'recalculatePrice', 'left=250,top=250,width=250,height=250,toolbar=0,menubar=0,scrollbars=0,statusbar=0,resizable=0');
}

function emailPage(page, id, cid, scid, pos) {
    myRef = window.open('email_page.jsp?page=' + page + '&id=' + id + '&cid=' + cid + '&scid=' + scid + '&pos=' + pos, 'emailPage', 'left=250,top=250,width=520,height=360,toolbar=0,menubar=0,scrollbars=0,statusbar=0,resizable=0');
}

function articlePrinterFriendly(id, cid, scid, pos) {
    myRef = window.open('article_details_printer.jsp?id=' + id + '&cid=' + cid + '&scid=' + scid + '&pos=' + pos, 'aritclePrinterFriendly', 'left=250,top=250,width=850,height=750,toolbar=1,menubar=1,scrollbars=1,statusbar=1,resizable=1');
}

function addImageComment(type, id, parentId, parentType){
    myRef = window.open('add_comment.do?type=' + type + '&id=' + id + '&parentId=' + parentId + '&parentType=' + parentType,'AddComment', 'left=250,top=250,width=500,height=200,toolbar=0,menubar=0,scrollbars=0,statusbar=0,resizable=1');
}

function showComments(type, id){
    myRef = window.open('show_comments.do?type=' + type + '&id=' + id,'ShowComments', 'left=250,top=200,width=500,height=400,toolbar=0,menubar=0,scrollbars=1,statusbar=0,resizable=1');
}

function showSubsectionComments(subsectionId, dateFrom, dateTo){
    myRef = window.open('show_subsection_comments.do?subsectionId=' + subsectionId + '&dateFrom=' + dateFrom + '&dateTo=' + dateTo,'ShowSubsectionComments', 'left=250,top=200,width=500,height=400,toolbar=0,menubar=0,scrollbars=1,statusbar=0,resizable=1');
}

function showItemComments(subsectionId, itemId, dateFrom, dateTo){
    myRef = window.open('show_item_comments.do?subsectionId=' + subsectionId + '&itemId=' + itemId + '&dateFrom=' + dateFrom + '&dateTo=' + dateTo,'ShowSubsectionComments', 'left=250,top=200,width=500,height=400,toolbar=0,menubar=0,scrollbars=1,statusbar=0,resizable=1');
}

function showUserComments(userId, dateFrom, dateTo, section, subsection){
    myRef = window.open('show_user_comments.do?userId=' + userId + '&dateFrom=' + dateFrom + '&dateTo=' + dateTo + '&section=' + section + '&subsection=' + subsection,'ShowUserComments', 'left=250,top=200,width=500,height=400,toolbar=0,menubar=0,scrollbars=1,statusbar=0,resizable=1');
}

function toggleCentralImage(link){
    document.getElementById(link.id).style.display = 'none';
    document.getElementById('centralImgInput').style.visibility = 'visible';
}

function toggleUploadableFile(link, link2){
    document.getElementById(link.id).style.display = 'none';
    document.getElementById(link2).style.visibility = 'visible';
}

function toggleAudioFile(link){
    document.getElementById(link.id).style.display = 'none';
    document.getElementById('audioFileInput').style.visibility = 'visible';
    document.getElementById('audioFileInput').size = '50';
    document.getElementById('deleteAudioFile').value = 'yes';
}

function toggleVideoFile(link){
    document.getElementById(link.id).style.display = 'none';
    document.getElementById('videoFileInput').style.visibility = 'visible';
    document.getElementById('videoFileInput').size = '50';
    document.getElementById('deleteVideoFile').value = 'yes';
}

function toggleAuthorImage(link){
    document.getElementById(link.id).style.display = 'none';
    document.getElementById('authorImgInput').style.visibility = 'visible';
    document.getElementById('authorImgInput').size = '50';
    document.getElementById('deleteAuthorsPhoto').value = 'yes';
}
function toggleBookAuthorImage(link){
    document.getElementById(link.id).style.display = 'none';
    document.getElementById('bookAuthorImgInput').style.visibility = 'visible';
    document.getElementById('bookAuthorImgInput').size = '50';
    document.getElementById('deleteBookAuthorsPhoto').value = 'yes';
}

function hideControls(){
    if(document.getElementById('centralImgInput') != null){
        document.getElementById('centralImgInput').style.visibility = 'hidden';
    }

    if(document.getElementById('audioFileInput') != null){
        document.getElementById('audioFileInput').style.visibility = 'hidden';
    }

    if(document.getElementById('videoFileInput') != null){
        document.getElementById('videoFileInput').style.visibility = 'hidden';
    }

    if(document.getElementById('authorImgInput') != null){
        document.getElementById('authorImgInput').style.visibility = 'hidden';
    }
    if(document.getElementById('bookAuthorImgInput') != null){
        document.getElementById('bookAuthorImgInput').style.visibility = 'hidden';
    }

}

function MsgOkCancel(){
    array = document.forms[0].elements;
    n = document.forms[0].elements.length;
    hasChecked = false;
    for(i=0; i<n; i++){
        if(array[i].checked == true){
            hasChecked = true;
        }
    }

    if(hasChecked){
        return confirm('The Selected Items will be deleted!');
    } else {
        alert('There is nothing selected for deletion');
        return false;
    }
}


function loadCentralImage (linkElement)
{
    var imageContainer = document.getElementById ("centralImage"),
            currentImage = imageContainer.src;

    imageContainer.src = linkElement.href;
    linkElement.firstChild.src = currentImage;
    linkElement.href = currentImage;

    tmpImg= new Image();
    tmpImg.src = currentImage;


    if (tmpImg.height > tmpImg.width){
        var wi = eval(tmpImg.width);
        var he = eval(tmpImg.height);
        linkElement.firstChild.width = 93;
        linkElement.firstChild.height = Math.round(93 * (he / wi));
    }
    else {
        linkElement.firstChild.width=128;
        linkElement.firstChild.height=93;
    }

    tmpImg.src = imageContainer.src;


    var wi = eval(tmpImg.width);
    var he = eval(tmpImg.height);

    if (wi > he){
        imageContainer.width = 530;
        imageContainer.height = Math.round(530 * (he / wi));
    }
    else {
        imageContainer.height = 530;
        imageContainer.width = Math.round(530 / (he / wi));
    }

    return false;
}

function copy(formName) {
    if (document.forms[formName] != null) {
        if (document.forms[formName].elements['shippingStreetAddress'] != null) {
            document.forms[formName].elements['contactStreetAddress'].value=document.forms[formName].elements['shippingStreetAddress'].value;
        }
        if (document.forms[formName].elements['shippingStreetAddress2'] != null) {
            document.forms[formName].elements['contactStreetAddress2'].value=document.forms[formName].elements['shippingStreetAddress2'].value;
        }
        if (document.forms[formName].elements['shippingStreetAddress3'] != null) {
            document.forms[formName].elements['contactStreetAddress3'].value=document.forms[formName].elements['shippingStreetAddress3'].value;
        }
        if (document.forms[formName].elements['shippingCity'] != null) {
            document.forms[formName].elements['contactCity'].value=document.forms[formName].elements['shippingCity'].value;
        }
        if (document.forms[formName].elements['shippingPostalCode'] != null) {
            document.forms[formName].elements['contactPostalCode'].value=document.forms[formName].elements['shippingPostalCode'].value;
        }
        if (document.forms[formName].elements['shippingCountry'] != null) {
            document.forms[formName].elements['contactCountry'].options[document.forms[formName].elements['shippingCountry'].selectedIndex].selected=true;
        }
    }
}



function cancelRedirect(redirect) {
    document.location.href = redirect;
}

function change_state(in_state, in_form_name) {
    for(i = 0; i < document.forms[in_form_name].elements.length; i++) {
        if(document.forms[in_form_name].elements[i].type == 'checkbox') {
            document.forms[in_form_name].elements[i].checked = in_state;
        }
    }
}


function open_popup(in_uri, in_name, in_size) {
    if (in_size == null)
        in_size = 'width=500,height=500';
    var popupWin = window.open(in_uri, in_name, in_size + ',left=0,top=0,scrollbars=yes,Resizable=1,toolbar=0,menubar=0,Location=0');
    popupWin.focus();
}


function copy_billing_to_shipping_details(form_name) {
    if (document.forms[form_name] != null) {
        if (document.forms[form_name].elements['contactStreetAddress'] != null) {
            document.forms[form_name].elements['shippingStreetAddress'].value=document.forms[form_name].elements['contactStreetAddress'].value;
        }
        if (document.forms[form_name].elements['firstName'] != null) {
            document.forms[form_name].elements['shippingFirstName'].value=document.forms[form_name].elements['firstName'].value;
        }
        if (document.forms[form_name].elements['lastName'] != null) {
            document.forms[form_name].elements['shippingLastName'].value=document.forms[form_name].elements['lastName'].value;
        }
        if (document.forms[form_name].elements['contactCity'] != null) {
            document.forms[form_name].elements['shippingCity'].value=document.forms[form_name].elements['contactCity'].value;
        }
        if (document.forms[form_name].elements['contactPostalCode'] != null) {
            document.forms[form_name].elements['shippingPostalCode'].value=document.forms[form_name].elements['contactPostalCode'].value;
        }
        if (document.forms[form_name].elements['contactPhoneNumber'] != null) {
            document.forms[form_name].elements['shippingPhoneNumber'].value=document.forms[form_name].elements['contactPhoneNumber'].value;
        }
        if (document.forms[form_name].elements['contactState'] != null) {
            document.forms[form_name].elements['shippingState'].value=document.forms[form_name].elements['contactState'].value;
        }
        if (document.forms[form_name].elements['contactCountry'] != null) {
            document.forms[form_name].elements['shippingCountry'].options[document.forms[form_name].elements['contactCountry'].selectedIndex].selected=true;
        }
    }
}

function toggleComments(formName, elementName, status) {
    if (document.forms[formName]) {
        var theForm = document.forms[formName];
        if (theForm.elements[elementName]) {
            var theEl = theForm.elements[elementName];
            if (theEl[0]) {
                for (var i = 0; i < theEl.length; i++) {
                    theEl[i].checked = status;
                }
            } else {
                theEl.checked = status;
            }
        }
    }
}