function editDiscount(id) {
   var wW = 300;
   var wH = 150;
   window.open("admin.php?section=edit_discount&id="+id,'ed' + id,"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=no"); 
}

function addDiscount(id) {
   var wW = 300;
   var wH = 150;
   window.open("admin.php?section=edit_discount",'ad',"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=no"); 
}

function deleteDiscount(id) {
   if (confirm('Удалить скидку?')) {
      window.location = "admin.php?section=delete_discount&id="+id;
   } 
}

function editDiscountCard(id) {
   var wW = 300;
   var wH = 150;
   window.open("admin.php?section=edit_discount_card&id="+id,'edс' + id,"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=no"); 
}

function addDiscountCard(id) {
   var wW = 300;
   var wH = 150;
   window.open("admin.php?section=edit_discount_card",'adс',"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=no"); 
}

function deleteDiscountCard(id) {
   if (confirm('Удалить карточку?')) {
      window.location = "admin.php?section=delete_discount_card&id="+id;
   } 
}

function editOrder(id) {
   var wW = 500;
   var wH = 550;
   window.open("admin.php?section=edit_order&id="+id,'eo' + id,"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=yes,toolbar=no,menubar=no,location=no,scrollbars=no"); 
}

function addOrder(id) {
   var wW = 500;
   var wH = 550;
   window.open("admin.php?section=edit_order",'ao',"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes"); 
}

function deleteOrder(id) {
   if (confirm('Удалить заказ?')) {
      var frm = document.forms['orders'];
      window.location = "admin.php?section=delete_order&id="+id +
         '&date_from=' + encodeURI(frm.date_from.value) +
         '&date_to=' + encodeURI(frm.date_to.value);
   } 
}

function editPresent(id) {
   var wW = 880;
   var wH = 600;
   window.open("admin.php?section=edit_present&id="+id,'eprs' + id,"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes"); 
}

function editPresentText(id) {
   var wW = 880;
   var wH = 600;
   window.open("admin.php?section=edit_present_text&id="+id,'eprst' + id,"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes"); 
}

function addPresent(id) {
   var wW = 880;
   var wH = 600;
   window.open("admin.php?section=edit_present&parent_id=" + id,'aprs',"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes"); 
}

function addPresentData(id) {
   var wW = 880;
   var wH = 600;
   window.open("admin.php?section=edit_present&present_id=" + id,'aprsd',"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=no"); 
}

function deletePresent(id) {
   if (confirm('Удалить подарок?')) {
      window.location = "admin.php?section=delete_present&id="+id;
   } 
}

function deletePresentImgFile(id) {
   if (confirm('Удалить файл?')) {
      window.location = "admin.php?section=delete_present_img_file&id="+id;
   } 
}

function editDeliveryType(id) {
   var wW = 300;
   var wH = 150;
   window.open("admin.php?section=edit_delivery_type&id="+id,'dl' + id,"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=no"); 
}

function addDeliveryType(id) {
   var wW = 300;
   var wH = 150;
   window.open("admin.php?section=edit_delivery_type",'adl',"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=no"); 
}

function addDeliveryTypeData(id) {
   var wW = 300;
   var wH = 150;
   window.open("admin.php?section=edit_delivery_type&delivery_type_id=" + id,'adld' + id,"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=no"); 
}

function deleteDeliveryType(id) {
   if (confirm('Удалить способ доставки?')) {
      window.location = "admin.php?section=delete_delivery_type&id="+id;
   } 
}

function editSettings(id) {
   var wW = 300;
   var wH = 500;
   window.open("admin.php?section=edit_settings&id="+id,'st' + id,"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=no,toolbar=no,menubar=no,location=no,scrollbars=yes"); 
}

function addOrderProduct() {
   var wW = 300;
   var wH = 500;
   window.open("admin.php?section=insert_product",'ip',"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");    
}

function travellingForm() {
   var wW = 440;
   var wH = 500;
   window.open("admin.php?section=travelling_form&date_from=2005-12-31+00%3A00%3A00&date_to=2008-12-31+23%3A59%3A00&status=U",'tf',"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");    
}

function viewBill(order_id) {
   var wW = 550;
   var wH = 500;
   window.open("admin.php?section=view_bill&id=" + order_id,'vb' + order_id,"left=" + (w - wW)/2 + ",top=" + (h - wH)/2 + ",width=" + wW + ",height=" + wH + ",status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes,resizable=yes");    
}

function calcTotal(productId) {
    priceEl = document.getElementById("order_data[products][" + productId + "][price]");
    quantityEl = document.getElementById("order_data[products][" + productId + "][quantity]");
    quantityEl.value = parseInt(quantityEl.value);
    if (isNaN(quantityEl.value)) {
        quantityEl.value = 0;
    }
    totalEL = document.getElementById('total[' + productId + ']');
    totalEL.innerHTML = priceEl.value*quantityEl.value;
}

function showMsgBox(event, mbId, nearEl) {
   var msgBox = document.getElementById(mbId);
   var xPos = (nearEl.style.left != undefined && nearEl.style.left) ? nearEl.style.left : event.clientX;
   var yPos = (nearEl.style.top != undefined && nearEl.style.top) ? nearEl.style.top : event.clientY;
   xPos += 10;
   yPos += 10;
   msgBox.style.left = xPos;
   msgBox.style.top = yPos;
   msgBox.style.visibility = 'visible';
}

function hideMsgBox(mbId) {
   var msgBox = document.getElementById(mbId);
   msgBox.style.visibility = 'hidden';
}

function onFormSubmit(returnPage)
{
    frm = document.forms['products'];
    if (frm.payment_type != undefined && frm.payment_type.value == 'cashless' &&
    frm.person_type != undefined && frm.person_type.value == 'legal_person')
    {
    toRemove = document.getElementById('cash_or_private');
    } else {
    toRemove = document.getElementById('cashless_org');
    }
    if (toRemove != undefined) {
    toRemove.innerHTML = '';
    }

    frm.elements['return_url'].value = encodeURI(returnPage);
    return true;
}
function useDiscount()
{
    frm = document.forms['products'];
    frm.elements['use_discount'].value = 'Y';
    onFormSubmit('shop/basket.html');
    frm.submit();
}
function tosClick(tosCBEl)
{
    if (tosCBEl.checked) {
    tosCBEl.form.send_order.removeAttribute('disabled');
    tosCBEl.form.send_order.className = 'button';
    } else {
    tosCBEl.form.send_order.setAttribute('disabled', 'true');
    tosCBEl.form.send_order.disabled = 'true';
    tosCBEl.form.send_order.className = 'disabled_button';
    }
}

var selects = {
    Moscow: {
    show: "payment,urgent_delivery,moscow_tos",
    hide: "cash_or_private,cashless_org,person,tos,regions_tos",
    restore : "",
    default_select : "payment_type"
    },
    Regions: {
    show: "person,regions_tos",
    hide: "cash_or_private,cashless_org,payment,tos,urgent_delivery,moscow_tos",
    restore : "",
    default_select : "person_type"
    },
    cash: {
    show: "cash_or_private,tos",
    hide: "cashless_org,person",
    restore : "cash_or_private",
    default_select : ""
    },
    cashless: {
    show: "person",
    hide: "cash_or_private,cashless_org,tos",
    restore : "",
    default_select : "person_type"
    },
    private_person: {
    show: "cash_or_private,tos",
    hide: "cashless_org",
    restore : "cash_or_private",
    default_select : ""
    },
    legal_person: {
    show: "cashless_org,tos",
    hide: "cash_or_private",
    restore : "cashless_org",
    default_select : ""
    }
};
var cash_or_private_innerHTML = null;
var cashless_org_innerHTML    = null;
function processSelect(selectObj)
{
    if (!cashless_org_innerHTML) {
        cash_or_private_innerHTML = document.getElementById('cash_or_private').innerHTML;
        cashless_org_innerHTML = document.getElementById('cashless_org').innerHTML;
    }

    var show = '';
    var hide = '';
    var restore = '';
    var default_select = '';
    if (selectObj != undefined) {
    selectValue = selectObj.value;
    /*
    if (selectObj.options[0].value == "") {
        var selectedValue = selectObj.value;
        selectObj.options[0].removeNode(true);
        for (i = 0; i < selectObj.options.length; i++) {
            if (selectObj.options[i].value == selectedValue) {
                selectObj.options[i].selected = true;
                break;
            }
        }
    }
    */
    if (selectObj.value == "" && selectObj.options[1] != undefined) {
        if (selectObj.settedValue != undefined) {
            for (i = 1; i < selectObj.options.length; i++) {
                if (selectObj.options[i].value == selectObj.settedValue) {
                selectObj.options[i].selected = true;
                }
            }
        }
        return;
    }
    selectObj.setAttribute('settedValue', selectObj.value);
    selectObj.settedValue = selectObj.value;
    var show = selects[selectValue]['show'].split(',');
    var hide = selects[selectValue]['hide'].split(',');
    var restore = selects[selectValue]['restore'].split(',');
    var default_select = selects[selectValue]['default_select'].split(',');

    for (i = 0; i < show.length; i++) {
        var el = document.getElementById(show[i]);
        if (el != undefined) {
            el.style.display = 'block';
        }
            
    }
    for (i = 0; i < hide.length; i++) {
        var el = document.getElementById(hide[i]);
        if (el != undefined) {
            el.style.display = 'none';
        }
    }
    for (i = 0; i < restore.length; i++) {
        var el = document.getElementById(restore[i]);
        if (el != undefined) {
            var restoreHtml = restore[i] + "_innerHTML";
            document.getElementById(restore[i]).innerHTML = eval(restoreHtml);
        }
    }
    for (i = 0; i < default_select.length; i++) {
        var el = selectObj.form.elements[default_select];
        if (el != undefined && el.options.length > 0) {
            el.options[0].selected = true;
        }
    }
    }
}



