
var loader;
var loaderTimer;

function oSHOW() {
    RemoveTT();
    loader = window.setTimeout(function() {
        document.getElementById('loadingOpacity').style.display = 'block';
        document.getElementById('loadingOpacity').style.height =  window.innerHeight||(document.body.clientHeight||document.documentElement.clientHeight) + 'px';
        document.getElementById('loadingOpacity').style.width = '100%';

        var el = document.getElementById('loadingImg');
        if(el) {
            el.style.display='block';
            el.style.visibility='hidden';
            el.style.top = ((self.innerHeight||(document.documentElement.clientHeight||document.body.clientHeight) - el.offsetHeight) / 2 + (document.documentElement.scrollTop||document.body.scrollTop)||window.pageYOffset) + 'px';
            el.style.left = (self.innerWidth||(document.documentElement.clientWidth||document.body.clientWidth) - el.offsetWidth) / 2 + 'px';
            el.style.visibility='visible';
        }
        loader = null;
        //loaderTimer = window.setTimeout(oHIDE, 2000);
    },500);
}

function oHIDE() {
    if(typeof loader != 'undefined' || typeof loader != 'null') {
        window.clearTimeout(loader);
        loader = null;
    }
/*  if(typeof loaderTimer != 'undefined' || typeof loaderTimer != 'null') {
        window.clearTimeout(loaderTimer);
        loaderTimer = null;
    }*/
    window.setTimeout(function() {
        document.getElementById('loadingOpacity').style.display = 'none';
        document.getElementById('loadingImg').style.visibility='hidden';
    }, 510);
}

/* cart */

function saveXY(x, y) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.savexy.php',
        {'x': x, 'y': y, 'random': myrandom},
        function(result, errors) { canAddToCart = true; }, false);
}

function add2cart() {
    if(! canAddToCart) return false;
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.add2cart.php',
        {'random': myrandom},
        function(result, errors) {
            cartQtyRecall();
            oHIDE();
            ShowDinamicPopup('add2cart');
        }, false);

}

function cartQtyRecall() {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.cart.qty.recall.php',
        {'random': myrandom},
        function(result, errors) {
            document.getElementById('cart_qty').innerHTML = result;
        }, false);
}

function deleteFromCart(id) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.delete.from.cart.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            document.getElementById('form_del').innerHTML = result;
            countTop();
        }, false);

}

function countTop() {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.top.php',
        {'random': myrandom},
        function(result, errors) {
            document.getElementById('cart_qty').innerHTML = result;
        }, false);
}

function close_top() {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.close.top.php',
        {'random': myrandom},
        function(result, errors) {
            document.getElementById('cart_qty').innerHTML = result;
        }, false);
}
/*
function sendOrder() {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.order.php',
        {'random': myrandom},
        function(result, errors) {

        }, false);
}

*/

function pay_cart1(word1)
{
    JsHttpRequest.query(
        '/ajax.pay.carts.php',
        { 'word': word1.value },
        function(result, errors) {
            if (errors) {alert(errors);}
            if (result) {
                document.getElementById('promo1').innerHTML = '<div align="center"><br/><br/><b>Промо-скидка 10%</b></div>';
            } else {
                alert('Неверный промо-код.');
            }
        },
        true
    );
}

function pay_cart() {
    myrandom = Math.round(Math.random()*100000);
    pin = document.getElementById('login_field').value;
    JsHttpRequest.query('/ajax.pay.cart.php',
        {'pin': pin, 'random': myrandom},
        function(result, errors) {
            document.getElementById('login_field').value = '';
            if(result && result != 'bad_code') {

                if(result["free_ship"] == 't')
                    $freeShip = 'ДА';
                else
                    $freeShip = 'НЕТ';

                $('card_log').innerHTML = 'Номинал:<b> '+result["nominal"]+'</b> <br/>Бесплатная доставка:<b> '+ $freeShip +'</b>';
                ShowPopup('popup_cart');
                document.getElementById('ch_price').innerHTML = result["down"];
                document.getElementById('cart_qty').innerHTML = result["up"];
            }
            else{
                ShowPopup('popup_cart_error');
            }

        }, false);
}
/* eof cart */















function to_session(src) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.to_session.php',
        {'src': src, 'random': myrandom},
        function(result, errors) {
            //alert(result);
        }, false);
}


function get_all_images() {
    oSHOW();
    id = 0;
    page = 0;
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.images.php',
        {'id': id, 'page': page, 'random': myrandom},
        function(result, errors) {
            oHIDE();
        document.getElementById('images').innerHTML = result;
        document.getElementById('images_pathway').innerHTML = '';

            //document.getElementById('thumbs_devices').innerHTML = '<font color="#cccccc;">Здесь<br />будет<br />изображение</font>';
        }, false);
}

function get_my_favorit_devices(page) {
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/get_my_favorit_devices.php',
        {'page': page, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            document.getElementById('devices').innerHTML = result;
            document.getElementById('devices_pathway').innerHTML = '';

        }, false);
}










function get_all_devices() {
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.all.devices.php',
        {'random': myrandom},
        function(result, errors) {
            oHIDE();
            document.getElementById('devices').innerHTML = result;
            //document.getElementById('thumbs_devices').innerHTML = '<font color="#cccccc;">Здесь<br />будет<br />изображение</font>';
            document.getElementById('devices_pathway').innerHTML = '';

        }, false);
}





function get_my_images(page) {
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.my.images.php',
        {'page': page, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            document.getElementById('images').innerHTML = result;
            document.getElementById('images_pathway').innerHTML = '';

        }, false);
}




function get_ano_images(page) {
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.ano.images.php',
        {'page': page, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            document.getElementById('images').innerHTML = result;
            document.getElementById('images_pathway').innerHTML = '';

        }, false);
}




function get_my_favorits(page) {
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.my.favorits.php',
        {'page': page, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            document.getElementById('images').innerHTML = result;
            document.getElementById('images_pathway').innerHTML = '';

        }, false);
}


var models_last_page = 0;

function get_models(id, page, savepage) {
    if(typeof savepage != 'undefined' && savepage == 'save') models_last_page = page;
    if(typeof savepage != 'undefined' && savepage == 'load') page = models_last_page;

    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.models.php',
        {'id': id, 'page': page, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            document.getElementById('devices').innerHTML = result;
        }, false);
    get_pathway_models(id);
}



function get_pathway_models(id) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.pathway_models.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            document.getElementById('devices_pathway').innerHTML = result;
            //document.getElementById('thumbs_devices').innerHTML = '<font color="#cccccc;">Здесь<br />будет<br />изображение</font>';
        }, false);
}




function send_models(id) {
    make_javascript(id);
    make_name(id);
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.thumbs_devices.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            document.getElementById('thumbs_devices').innerHTML = result;
            FixPng();
        }, false);
}

function mk_jok(id) {
    make_javascript(id);
    make_name(id);
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.mk_jok.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            window.location=clientSideUrl+'joket/';
        }, false);
}


function mk_jok_favorit(id) {
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.mk_jok_favorit.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            window.location=clientSideUrl+'joket/';
        }, false);
}


function mk_img(id) {
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.mk_img.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            //document.getElementById('thumbs_images').innerHTML = result["image"];
            window.location=clientSideUrl+'joket/';
        }, false);
}

function mk_upload() {
    if(typeof ___cui != 'undefined' && ___cui >= 9) {
        ShowDinamicPopup('upload_limit');
        return false;
    }

    if(document.getElementById("myupl").value == '') return false;

    if(! checkMIME(document.getElementById("myupl").value)) {
        ShowDinamicPopup('upload_mime');
        return false;
    }
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.images_upload.php',
        {'upl': document.getElementById("myupl"), 'random': myrandom},
        function(result, errors) {
            document.getElementById('uploaded').innerHTML = result;
            document.getElementById('fileupload').reset();
            ___cui++;
        }, false);
}

function my_images(page) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.uploaded_images.php',
        {'page': page, 'random': myrandom},
        function(result, errors) {
            document.getElementById('uploaded').innerHTML = result;
        }, false);
}

function mk_ano_upload() {

    if(typeof ___cui != 'undefined' && ___cui >= 6) {
        ShowDinamicPopup('upload_limit_sp');
        return false;
    }

    if(document.getElementById("myupl").value == '') return false;

    if(! checkMIME(document.getElementById("myupl").value)) {
        ShowDinamicPopup('upload_mime');
        return false;
    }
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.ano_images_upload.php',
        {'upl': document.getElementById("myupl"), 'random': myrandom},
        function(result, errors) {
            oHIDE();
            document.getElementById('uploaded').innerHTML = result;
            document.getElementById('fileupload').reset();
            ___cui++;
        }, false);
}

function checkMIME(n) {
    n = n.replace(/(.*)\.(....?)/,'$2');
    n = n.toLowerCase();
    switch(n) {
        case 'jpg': return true;
        case 'png': return true;
        case 'wbmp': return true;
        case 'gif': return true;
        case 'jpeg': return true;
    }
    return false;
}

function my_devices(page) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.my_devices.php',
        {'page': page, 'random': myrandom},
        function(result, errors) {
            document.getElementById('user_devices').innerHTML = result;
        }, false);
}

function my_favs(page) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.my_favs.php',
        {'page': page, 'random': myrandom},
        function(result, errors) {
            document.getElementById('favorit').innerHTML = result;
        }, false);
}


function send_models2user(id) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.devices2user.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            document.getElementById('user_devices').innerHTML = result;
        }, false);
}

function delete_dev_from_user(id, page) {
    if(typeof page == 'undefined') page = 0;
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.delete_dev_from_user.php',
        {'id': id, 'page': page, 'random': myrandom},
        function(result, errors) {
            document.getElementById('user_devices').innerHTML = result;
        }, false);
}


function delete_image_from_user(id, page) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/delete_image_from_user.php',
        {'id': id, 'page': page, 'random': myrandom},
        function(result, errors) {
            ___cui--;
            document.getElementById('uploaded').innerHTML = result;
        }, false);
}


function delete_image_from_ano(id) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/delete_image_from_ano.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            document.getElementById('uploaded').innerHTML = result;
        }, false);
}


function delete_image_from_favorits(id, page) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/delete_image_from_favorits.php',
        {'id': id, 'page': page, 'random': myrandom},
        function(result, errors) {
            document.getElementById('favorit').innerHTML = result;
        }, false);
}

function make_javascript(id) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.make_js.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            eval(result);
            if(Bild.length > 1) {
                document.getElementById("left").style.visibility    = 'visible';
                document.getElementById("right").style.visibility   = 'visible';
            } else {
                document.getElementById("left").style.visibility    = 'hidden';
                document.getElementById("right").style.visibility   = 'hidden';
            }
        }, false);
}

function make_price(id) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.make_price.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            document.getElementById('price').innerHTML = result;
        }, false);
}

function make_name(id) {
    make_price(id);
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.make_name.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            document.getElementById('device_name').innerHTML = result;
        }, false);
}



function get_images(id, page) {
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.images.php',
        {'id': id, 'page': page, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            document.getElementById('images').innerHTML = result;
            FixPng();
        }, false);
    get_pathway_images(id);
}




function get_pathway_images(id) {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.pathway_images.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            document.getElementById('images_pathway').innerHTML = result;
            //document.getElementById('thumbs_images').innerHTML = '';
        }, false);
}




function send_images(id) {
    /*if(device != 0) {*/
        //make_javascript(device);
        //send_models(device);
    /*}*/
    oSHOW();
    stopDrag();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.thumbs_images.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            //document.getElementById('thumbs_images').innerHTML = result;
            document.getElementById('thumbs_images').innerHTML = result["image"];
            document.getElementById('thumbs_devices').innerHTML = result["device"];
            FixPng();
            window.setTimeout(startDrag, 100);
        }, false);
}



function send_favorit_images(id) {
    stopDrag();
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.thumbs_favrit_images.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            //document.getElementById('thumbs_images').innerHTML = result;
            document.getElementById('thumbs_images').innerHTML = result["image"];
            document.getElementById('thumbs_devices').innerHTML = result["device"];
            FixPng();
            startDrag();
        }, false);
}




function send_my_images(id) {
    oSHOW();
    stopDrag();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.thumbs_my_images.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            //document.getElementById('thumbs_images').innerHTML = result;
            document.getElementById('thumbs_images').innerHTML = result["image"];
            document.getElementById('thumbs_devices').innerHTML = result["device"];
            FixPng();
            startDrag();
        }, false);
}




function send_ano_images(id) {
    oSHOW();
    stopDrag();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.thumbs_ano_images.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            oHIDE();
            //document.getElementById('thumbs_images').innerHTML = result;
            document.getElementById('thumbs_images').innerHTML = result["image"];
            document.getElementById('thumbs_devices').innerHTML = result["device"];
            FixPng();
            startDrag();
        }, false);
}






function AjaxGetOrderId() {
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.get_order_id.php',
        {'random': myrandom},
        function(result, errors) {
            $('order_id').innerHTML = result;
        }, false);

}



function add2favorit(id, cat, page) {
    oSHOW();
    myrandom = Math.round(Math.random()*100000);
    JsHttpRequest.query('/ajax.add2favorit.php',
        {'id': id, 'random': myrandom},
        function(result, errors) {
            oHIDE();
        }, false);
        get_images(cat, page);
}
