﻿var advancedHeight = 0;
var advancedWidth = 0;

//show send to friend popup
function showSendToFriend(nArticleId) {
    //display div modal dialog
    advancedWidth = 466;  
    
    //send ajax request
    sendAjaxRequest(rooturl + 'ajaxsendtofriend.aspx?articleid=' + nArticleId , 'modalWindow', 'checkResponse');

    //open dialog
    openModalDialog();
}

//send sent to friend popup
function sendSendToFriend() {
    //display div modal dialog
    advancedWidth = 466;

    //get parameters
    var nArticleId = $('hiddenarticleid').value;
    var sArticleTitle = $('articletitle').value;
    var sSenderName = $('sendername').value;
    var sSenderEmail = $('senderemail').value;
    var sRecipientEmail = $('sendtoemail').value;
    var sRecipientName = $('recipientname').value;
    var sMessage = $('sendtofriendtextarea').value;
    var nNavNodeid = $('navnodeid').value;

    //send ajax request
    sendAjaxRequest(rooturl + 'ajaxsendtofriend.aspx?articleid=' + nArticleId + '&articletitle=' + escape(sArticleTitle.replace("'","\'")) + '&sendername=' + escape(sSenderName) + '&senderemail=' + escape(sSenderEmail) + '&recipientemail=' + escape(sRecipientEmail) + '&message=' + escape(sMessage) + '&recipientname=' + sRecipientName + '&validate=1' + '&navnodeid='+ nNavNodeid, 'modalWindow', '');

    //open dialog
    openModalDialog();
}

//close dialog
function closeSendToFriend() {
    //hide div modal dialogs
    $('modalWindowSendToFriend').style.display = $('modalBackground').style.display = 'none';

    //clear content
    $('modalWindowSendToFriend').innerHTML = '';
}


// #######################################

//close dialog
function closeModalDialog() {
    //hide main div modal dialog
    if ($('modalWindow') != null) {
        //hide
        $('modalWindow').style.display = 'none';
        //clear content
        $('modalWindow').innerHTML = '';
    }
    //hide send to friend modal dialog
    if ($('modalWindowSendToFriend') != null) {
        //hide
        $('modalWindowSendToFriend').style.display = 'none';
        //clear content
        $('modalWindowSendToFriend').innerHTML = '';
    }
    //hide background
    $('modalBackground').style.display = 'none';
}

//open modal dialog
function openModalDialog() {
    //display div modal dialogs
    $('modalWindow').style.display = $('modalBackground').style.display = 'block';

    //call once to center everything
    OnWindowResize();

    //hook events
    if (window.attachEvent)
        window.attachEvent('onresize', OnWindowResize);
    else if (window.addEventListener)
        window.addEventListener('resize', OnWindowResize, false);
    else
        window.onresize = OnWindowResize;

    // we won't bother with using javascript in CSS to take care keeping the window centered
    if (document.all)
        document.documentElement.onscroll = OnWindowResize;
}

//event: on window resize
function OnWindowResize() {
    //we only need to move the dialog based on scroll position if
    //we're using a browser that doesn't support position: fixed, like < IE 7
    var left = window.XMLHttpRequest == null ? document.documentElement.scrollLeft : 0;
    var top = window.XMLHttpRequest == null ? document.documentElement.scrollTop : 0;
    var div = $('modalWindow');

    //set current position
    if (advancedHeight == 0 && advancedWidth == 0) {
        div.style.left = Math.max((left + (GetWindowWidth() - div.offsetWidth) / 2), 0) + 'px';
        div.style.top = Math.max((top + (GetWindowHeight() - div.offsetHeight) / 2), 0) + 'px';
    }
    else {
        if (GetWindowHeight() < advancedHeight) {
            div.style.overflowY = "scroll";
            div.style.width = advancedWidth + 20 + "px";
            div.style.height = GetWindowHeight() + "px";
        }
        else {
            div.style.overflowY = "hidden";
            div.style.width = advancedWidth + "px";
            if (advancedHeight > 0) {
                div.style.height = advancedHeight + "px";
            }
        }
        div.style.left = Math.max((left + (GetWindowWidth() - advancedWidth) / 2), 0) + 'px';
        div.style.top = Math.max((top + (GetWindowHeight() - advancedHeight) / 3), 0) + 'px';
    }
}

/* utiltiy functions */

//tools: get window width
function GetWindowWidth() {
    //calculate width
    var width =
		document.documentElement && document.documentElement.clientWidth ||
		document.body && document.body.clientWidth ||
		document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
		0;

    //return
    return width;
}

//tools: get window height
function GetWindowHeight() {
    //calculate height
    var height =
		document.documentElement && document.documentElement.clientHeight ||
		document.body && document.body.clientHeight ||
  		document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
  		0;

    //return
    return height;
}

//myaddresses add
function addAddress() {
    advancedHeight = 0;
    advancedWidth = 430;
    sendAjaxRequest(rooturl + 'ajaxdeliveryaddress.aspx?popupvalidate=1&businesspartnerdeliveryaddressid=0', 'modalWindow', 'checkResponse');
    openModalDialog();
}

//myadddresses edit partner
function editAddressPartner() {
    advancedHeight = 0;
    advancedWidth = 430;
    sendAjaxRequest(rooturl + 'ajaxdeliveryaddress.aspx?popupvalidate=3&businesspartnerdeliveryaddressid=0', 'modalWindow', 'checkResponse');
    openModalDialog();
}

//myadddresses edit
function editAddress(nBusinessPartnerDeliveryAddressId) {
    advancedHeight = 0;
    advancedWidth = 430;
    sendAjaxRequest(rooturl + 'ajaxdeliveryaddress.aspx?popupvalidate=2&businesspartnerdeliveryaddressid=' + nBusinessPartnerDeliveryAddressId, 'modalWindow', 'checkResponse');
    openModalDialog();
}

//myaddresses remove - fill data into form and submit
function removeAddress(nBusinessPartnerDeliveryAddressId) {
    sendAjaxRequest(rooturl + 'ajaxdeliveryaddress.aspx?popupvalidate=4&businesspartnerdeliveryaddressid=' + nBusinessPartnerDeliveryAddressId, 'modalWindow', 'checkResponse');
}

//save order delivery address
function saveDeliveryAddressPopup() {
    var validate = $('popupvalidate').value;
    var businesspartnerdeliveryaddressid = $('popupbusinesspartnerdeliveryaddressid').value;
    var name = $('popupname').value;
    var surname = $('popupsurname').value;
    var address = $('popupaddress').value;
    var housenumber = $('popuphousenumber').value;
    var zip = $('popupzip').value;
    var city = $('popupcity').value;
    var phone = $('popupphone').value;
    var country = $('popupcountry').value;

    sendAjaxRequest(rooturl + 'ajaxdeliveryaddress.aspx?validate=' + validate + '&businesspartnerdeliveryaddressid=' + businesspartnerdeliveryaddressid + '&name=' + escape(name) +
    '&surname=' + escape(surname) + '&address=' + escape(address) + '&housenumber=' + housenumber + '&zip=' + zip + '&city=' + escape(city) + '&phone=' + phone + '&country=' + escape(country), 'modalWindow', 'checkResponse');
    openModalDialog();
}


//resize catalog
function setVirtualCatalog() {
    OnWindowResizeCatalog();
    
    //hook events
    if (window.attachEvent)
        window.attachEvent('onresize', OnWindowResizeCatalog);
    else if (window.addEventListener)
        window.addEventListener('resize', OnWindowResizeCatalog, false);
    else
        window.onresize = OnWindowResizeCatalog;

    // we won't bother with using javascript in CSS to take care keeping the window centered
    if (document.all)
        document.documentElement.onscroll = OnWindowResizeCatalog;
}

//event: on window resize catalog
function OnWindowResizeCatalog() {
    document.getElementById('mainvirtualcatalogdiv').style.height = (GetWindowHeight() - 30) + 'px';
}
