// plain old javascript
function correctPNG()
   {  
   alert('test');
   for(var i=0; i<document.images.length; i++)
      {     
     var img = document.images[i];
     var imgName = img.src.toUpperCase();
     if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
        {
       var imgID = (img.id) ? "id='" + img.id + "' " : "" ;
       var imgClass = (img.className) ? "class='" + img.className + "' " : "" ;
       var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
       var imgStyle = "display:inline-block;" + img.style.cssText           ;
       if (img.align == "left") imgStyle = "float:left;" + imgStyle  ;
       if (img.align == "right") imgStyle = "float:right;" + imgStyle    ;
       if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle  ;    
       var strNewHTML = "<span " + imgID + imgClass + imgTitle     
       + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" 
        + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"       
       + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" ;
       img.outerHTML = strNewHTML;
       i = i-1;
        }
      }
   }


function alphaBackgrounds(){
   var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
   var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
   for (i=0; i<document.all.length; i++){
      var bg = document.all[i].currentStyle.backgroundImage;
      if (itsAllGood && bg){
         if (bg.match(/\.png/i) != null){
            var mypng = bg.substring(5,bg.length-2);
            document.all[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+mypng+"', sizingMethod='scale')";
            document.all[i].style.backgroundImage = "url('/mambots/system/bot_ultimatepngfix/x.gif')";
         }
      }
   }
}        


  

// jQuery UI modal dialog options cart
var modalDialogCartOptions = {
    autoOpen: false,
    autoResize: false,
    closeOnEscape: false,
    resizable: false,
    draggable: false,
    bgiframe: false,
    modal: true,
    width: 600,
    height: 'auto',
    overlay: {backgroundColor: "#000", opacity: 0.75} 
};

// jQuery UI modal dialog options cart
var modalDialogContent = {
    autoOpen: false,
    autoResize: false,
    closeOnEscape: false,
    resizable: false,
    draggable: false,
    bgiframe: false,
    modal: true,
    width: 516,
    //width: 'auto',
    height: 'auto',
    overlay: {backgroundColor: "#000", opacity: 0.75} 
};    
// jQuery UI modal dialog options Socket
var modalDialogContentSocket = {
    autoOpen: false,
    autoResize: false,
    closeOnEscape: true,
    resizable: false,
    draggable: false,
    bgiframe: false,
    modal: true,
//    width: 516,
    width: 810,
    height: 'auto',
    overlay: {backgroundColor: "#000", opacity: 0.75},
    close: function(ev, ui) {
             if(isIE6()){
                $(this).remove(); window.location = window.location;       
             }
        } 
};

// CheckoutSuccessLightbox
var modalDialogCheckoutSuccessLightbox = {
    autoOpen: false,
    autoResize: false,
    closeOnEscape: true,
    resizable: false,
    draggable: false,
    bgiframe: false,
    modal: true,
    width: 500,
    height: 500,
    overlay: {backgroundColor: "#000", opacity: 0.75},
    close: function(ev, ui) {
             if(isIE6()){
                $(this).remove(); window.location = window.location;       
             }
        }  
};   

// jQuery UI modal dialog options review
var modalDialogReviewOptions = {
    autoOpen: false,
    autoResize: false,
    closeOnEscape: false,
    resizable: false,
    draggable: false,
    bgiframe: false,
    modal: true,
    width: 516,
    height: 'auto',
    overlay: {backgroundColor: "#000", opacity: 0.75} 
};

// jQuery UI modal dialog options products criteria
var modalDialogProductsCriteriaOptions = {
    autoOpen: false,
    autoResize: false,
    closeOnEscape: false,
    resizable: false,
    draggable: false,
    bgiframe: false,
    modal: true,
    width: 516,
    height: 'auto',
    overlay: {backgroundColor: "#000", opacity: 0.75} 
};

// jQuery UI modal dialog options customer product media
var modalDialogCustomerProductMediaOptions = {
    autoOpen: false,
    autoResize: false,
    closeOnEscape: false,
    resizable: false,
    draggable: false,
    bgiframe: false,
    modal: true,
    width: 516,
    height: 'auto',
    overlay: {backgroundColor: "#000", opacity: 0.75}
};

// jQuery UI modal dialog options comparison
var modalDialogBenchmarkOptions = { 
    autoOpen: false,
    autoResize: false,
    closeOnEscape: false,
    resizable: false,
    draggable: false,
    bgiframe: false,
    modal: true,
    width: 516,
    height: 'auto', 
    overlay: {backgroundColor: "#000", opacity: 0.75}
};

function isIE6(){
    var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
    var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5);
    if(itsAllGood){
        return true;
    }else{
        return false;
    }
}

// lighthouse
function windup (adress, refname, w, h, posLeft, posTop) {
    if (posLeft == '' && posTop == '') {
        var posLeft = (screen.availWidth - w) / 2;
        var posTop = (screen.availHeight - h) / 2;
    }
    win = window.open(adress, refname, "width="+w+",height="+h+",innerWidth="+w+",innerHeight="+h+",left="+posLeft+",top="+posTop+",scrollbars=no,resizable=no");
    win.focus();
}

// lighthouse
function windupFullscreen (adress, refname) {
    win = window.open(adress, refname, "fullscreen=yes, scrollbars=yes, resizable=yes");
    win.focus();
}

// init slider if more than 4 visible items to compare are loaded
function checkCompareItems () {
    var totalwidth = $("#comparetable").innerWidth() - $("#slidebar").innerWidth() - 114;
    
    if($('#comparetable .itemhead:visible').length > 4) {        
        $("#slidebar").slider({
          value: 0,
          handle: ".ui-slider-handle",
          slide: function(e, ui){ $("#comparison").attr({scrollLeft: ui.value * (totalwidth/100)}); },
          stop: function(e, ui){ $("#comparison").attr({scrollLeft: ui.value * (totalwidth/100)}); }
        });
        
        $("#slider").show();
    }else{
        $("#slider").css("visibility","hidden");
    }
}    

// remove items to compare if wanted
function removeCompareItem (nr) {
    $(".item"+nr).hide();
    checkCompareItems ();
}

// anchor scrolling
function scrollme(anchor) {
    $.scrollTo('#' + anchor, 800, {
        offset : {
            top :-15
        }
    });
}

//count_faq
function count_faq(question_id){
    $.post("index.php?page=ajax_functions",
        {   
            remote_function: "Count FAQ",
            question_id: question_id            
        },
        function(data){}
    )     
}  



// NEKOM CK - delete product
function deleteCart(products_id, products_key){
    products_quantity = $("#products_quantity_" + products_id).val();
   
    $.post("index.php?page=ajax_functions", 
        { remote_function: "DeleteCart", products_id: products_id, products_key: products_key, products_quantity: products_quantity},
        function(data){
            if(data.state == 'true'){
                window.location = window.location;    
            }    
        },
        'json'
    );         
}
// NEKOM CK - update product
function updateCart(products_id, products_key, products_quantity, update_quantity){   
    
    $.post("index.php?page=ajax_functions", 
        { remote_function: "UpdateCart", products_id: products_id, products_key: products_key, products_quantity: products_quantity, update_quantity: update_quantity},
        function(data){
            if(data.state == 'true'){
                window.location = window.location;    
            }    
        },
        'json'
    );         
}


// opens the cart modal dialog
function openModalDialogCart(productId){

    productQuantity = $("#product_quantity_" + productId).val();
    
    if (!productQuantity) {
        productQuantity = 1;
    }
    
    // update cart in session
    $.post("index.php?page=ajax_functions", 
        { remote_function: "addToCart", 
            product_id: productId,
            product_quantity: productQuantity
        },
        function(data){
            
            if(data.state == 'true'){
                
                $("#modal-dialog-cart").html(data.content);
                $("#modal-dialog-cart").dialog("open");
                $(".ui-dialog-titlebar-close").hide();
                
                $(".ui-dialog-buttonpane").hide();        
            }else{

                $("#modal-dialog-cart-error").html(data.content); 
                $("#modal-dialog-cart-error").dialog("open");
                $(".ui-dialog-titlebar-close").hide();
                $(".ui-dialog-buttonpane").hide();    
            }
            
            // look for all forms, which's name starts with product
            var eleme = document.getElementsByTagName('select');
            for (var e = 0; e < eleme.length; e++) {
                eleme[e].style.visibility = 'hidden';
            }
            
        },
        'json'
    );   
}

// closes the cart modal dialog in case of an error
function closeModalDialogCartError(){
    $("#modal-dialog-cart-error").dialog("close");    
}

// opens the review modal dialog
function openModalDialogReview(review_id, action){
    // set review_customer_status = 0 or 1
    $.post("index.php?page=ajax_functions", 
        { remote_function: "Review", review_id: review_id, action: action},
        function(data){
            if(data.state == 'true'){
                $("#modal-dialog-review").html(data.content);
                $("#modal-dialog-review").dialog("open");
                $(".ui-dialog-titlebar-close").hide();
                $(".ui-dialog-buttonpane").hide();        
            }
        },
        'json'
    );   
}

// opens the products criteria modal dialog
function openModalDialogProductsCriteria(page_name){ 
    $.post("index.php?page=ajax_functions", 
        { remote_function: "Products Criteria", page_name: page_name},
        function(data){
            if(data.state == 'true'){
                $("#modal-dialog-products-criteria").html(data.content);
                $("#modal-dialog-products-criteria").dialog("open");
                $(".ui-dialog-titlebar-close").show();
                $(".ui-dialog-buttonpane").hide();        
            }
        },
        'json'
    );   
}

// opens the shipping information popup
function openModalDialogContent(content_id){ 
    $.post("index.php?page=ajax_functions", 
        { remote_function: "Show Content", content_id: content_id},
        function(data){
            if(data.state == 'true'){
                $("#modal-dialog-content").html(data.content);
                $("#modal-dialog-content").dialog('open');
                $("#modal-dialog-content").dialog('option', 'title', data.title);
                
                $(".ui-dialog-titlebar-close").show();
                $(".ui-dialog-buttonpane").hide();        
            }
        },
        'json'
    );   
}    
      
// opens the Socket popup
function openModalDialogContentSocket(content_id){ 

    $.post("index.php?page=ajax_functions", 
        { remote_function: "Show Content", content_id: content_id},
        function(data){
            if(data.state == 'true'){
                $("#modal-dialog-contentSocket").html(data.content);
                     
                $("#modal-dialog-contentSocket").dialog('open');
                $("#modal-dialog-contentSocket").dialog('option', 'title', data.title);
                
                $(".ui-dialog-titlebar-close").show();
                $(".ui-dialog-buttonpane").hide();        
            }  
            // look for all forms, which's name starts with product
            if(isIE6()){
                var eleme = document.getElementsByTagName('select');
                for (var e = 0; e < eleme.length; e++) {
                    eleme[e].style.visibility = 'hidden';
                }
            }
            
        },
        'json'
    );   
}
          
// opens the CheckoutSuccessLightbox
function openModalDialogCheckoutSuccessLightbox(content_id){

    $.post("index.php?page=ajax_functions", 
        { remote_function: "Show Lightbox", content_id: content_id},
        function(data){
            if(data.state == 'true'){
                $("#modal-dialog-CheckoutSuccessLightbox").html(data.content);
                     
                $("#modal-dialog-CheckoutSuccessLightbox").dialog('open');
                $("#modal-dialog-CheckoutSuccessLightbox").dialog('option', 'title', data.title);
                
                $(".ui-dialog-titlebar-close").show();
                $(".ui-dialog-buttonpane").hide();        
            }  
            // look for all forms, which's name starts with product
            if(isIE6()){
                var eleme = document.getElementsByTagName('select');
                for (var e = 0; e < eleme.length; e++) {
                    eleme[e].style.visibility = 'hidden';
                }
            }
            
        },
        'json'
    );  
}

// reset of the slave dropdowns
function resetSlaveProductsCriteria(product_id, select, unique){
    
    // look for all forms, which's name starts with product
    var forms = document.forms;
    for (var f = 0; f < forms.length; f++) {

        if (forms[f].name.indexOf('product') >= 0) {
            
            // alway the first element in form must be the select
            var sele = forms[f].elements[1];
            
            if ((select && sele.name != select.name) || !select) {
                sele.options[0].selected = 'true';
                
                // reset dropdowns, so customers can start again
                for (var s = 0; s < sele.options.length; s++) {
                    sele.options[s].disabled = '';
                    //if (sele.options[s].style) {
                        
                        sele.options[s].style.color = '#000';
                        sele.options[s].style.fontStyle = 'normal';
                        sele.options[s].style.backgroundColor = '#fff';   
                    //}
                }
            }
            
            if (select) {
                sele.onchange = sele.ondblclick;
            }
        }
    }
    
    $.post("index.php?page=ajax_functions", 
        { 'remote_function': 'Slave Reset',
          'product_id': product_id} ,
        function(data) {
            if (data.state == 'true' && select) {
                redirectSlaveProductsCriteria(product_id, select, unique);
            }
        },
        'json'
    );     
}


    
// opens the products criteria modal dialog
function redirectSlaveProductsCriteria(product_id, select, unique){ 
    
    var id = 'id['+unique+']';
    $.post("index.php?page=ajax_functions", 
        { 'remote_function': 'Slave Products',
          'action': 'select_ms',
          'unique': unique,
          'product_id': product_id,
          id: select.options[select.options.selectedIndex].value} ,
        function(data){
            if(data.state == 'true'){
                window.location = data.content;
                
            } else {
                
                var match = ';';
                for (var i = 0; i < data.array_length+1; i++) {
                    if (data.content[i]) {
                        match += data.content[i]+';';
                    }
                }
                
                // look for all forms, which's name starts with product
                var forms = document.forms;
                for (var f = 0; f < forms.length; f++) {

                    if (forms[f].name.indexOf('product') >= 0) {
                        
                        // alway the first element in form must be the select
                        var sel = forms[f].elements[1];
                        
                        // disable all not valid possibilities
                        for (var s = 1; s < sel.options.length; s++) {
                            if (match.indexOf(';'+sel.options[s].value+';') < 0) {
                                
                                //if (sel.options[s].style) {
                                    sel.options[s].style.color = 'gray';
                                    sel.options[s].style.fontStyle= 'italic';
                                //}
                                sel.options[s].disabled = 'disabled';
                            } else {
                                sel.options[s].disabled = '';
                                //if (sel.options[s].style) {
                                    sel.options[s].style.color = '#000';
                                    sel.options[s].style.fontStyle = 'normal';
                                    sel.options[s].style.backgroundColor = '#fff';
                                //}
                            }
                        }
                    }
                }
            }
            
        },
        'json'
    ); 
      
}

// closes the Products Criteria modal dialog
function closeModalDialogProductsCriteria(from_page_name){
    var criteria_dimmable = $("#criteria_dimmable").attr("checked");
    var criteria_sensor = $("#criteria_sensor").attr("checked"); 
    var criteria_warmlightcolor = $("#criteria_warmlightcolor").attr("checked"); 
    var criteria_coollightcolor = $("#criteria_coollightcolor").attr("checked"); 
    var criteria_coloredlight = $("#criteria_coloredlight").attr("checked"); 
    var criteria_dull = $("#criteria_dull").attr("checked"); 
    var criteria_bright = $("#criteria_bright").attr("checked"); 
    var criteria_motionsensor = $("#criteria_motionsensor").attr("checked"); 
    var criteria_battery = $("#criteria_battery").attr("checked"); 
    var criteria_solar = $("#criteria_solar").attr("checked"); 
    var criteria_remotecontrol = $("#criteria_remotecontrol").attr("checked"); 
    var criteria_waterresistent = $("#criteria_waterresistent").attr("checked"); 
    
    $.get("index.php?page=ajax_functions", 
        {   
            remote_function: "Set Products Criteria",
            from_page_name: from_page_name,
            criteria_dimmable: criteria_dimmable,
            criteria_sensor: criteria_sensor,
            criteria_warmlightcolor: criteria_warmlightcolor,
            criteria_coollightcolor: criteria_coollightcolor,
            criteria_coloredlight: criteria_coloredlight,
            criteria_dull: criteria_dull,
            criteria_bright: criteria_bright,
            criteria_motionsensor: criteria_motionsensor,
            criteria_battery: criteria_battery,
            criteria_solar: criteria_solar,
            criteria_remotecontrol: criteria_remotecontrol,
            criteria_waterresistent: criteria_waterresistent
        },
        function(data){

            $("#modal-dialog-products-criteria").dialog("close");
            if(data.link){
                window.location.href=data.link;    
            }            
        },
        'json'
    )       
}

// remove the Products Criteria modal dialog
function removeModalDialogProductsCriteria(from_page_name_id, criteria_id){
    $.post("index.php?page=ajax_functions",
        {   
            remote_function: "Remove Products Criteria",
            from_page_name_id: from_page_name_id,
            criteria_id: criteria_id            
        },
        function(data){

            if(data.link){
                window.location.href=data.link;    
            }          
        },
        'json'
    )    
}

// opens the customer product media modal dialog
function openModalDialogCustomerProductMedia(customer_product_media_id, type, action){
    // set status_customer = 0 or 1
    $.post("index.php?page=ajax_functions", 
        { remote_function: "customer_product_media", customer_product_media_id: customer_product_media_id, type: type, action: action},
        function(data){
            if(data.state == 'true'){
                $("#modal-dialog-customer-product-media").html(data.content);
                $("#modal-dialog-customer-product-media").dialog("open");
                $(".ui-dialog-titlebar-close").hide();
                $(".ui-dialog-buttonpane").hide();        
            } 
        },
        'json'
    );   
}

// opens the shipping information popup
function sendFeedback(email_address, order_id){ 
    
    var content = $("#feedback_content").val();
    $.post("index.php?page=ajax_functions", { remote_function: "Send Feedback", 
            content: content,
            email_address: email_address,
            order_id: order_id},
        function(data){
            if(data.state == 'true'){
                $("#modal-dialog-content").html(data.content);
                $("#modal-dialog-content").dialog('open');
                $("#modal-dialog-content").dialog('option', 'title', data.title);
                
                $(".ui-dialog-titlebar-close").show();
                $(".ui-dialog-buttonpane").hide();        
            }
            $("#feedback_content").val('');
        },
        'json'
    );   
}

// opens the benchmark modal dialog
function reloadShortBasket(){
    // set status_customer = 0 or 1
    $.post("index.php?page=ajax_functions", 
        { remote_function: "Reload Shoprtbasket"},
        
        function(data){
            if(data.state == 'true'){
                // fill econda data
                if(data.products_id){
                    emos_ecEvent('c_add',data.products_id,data.products_name,data.products_price,data.products_category,data.products_quantitiy,'NULL','NULL','NULL');
                }
                
                $("#box_cart").html(data.content);
                
                initToggle();  
                
                return true;
            } 
        },
        'json'
    );    
    
    return false;
}

// opens the benchmark modal dialog
function openModalDialogBenchmark(productId){
    // add to benchmark   
    $.post("index.php?page=ajax_functions", 
        { remote_function: "addToComparison", product_id: productId},
        function(data){
            
            var eleme = document.getElementsByTagName('select');
            for (var e = 0; e < eleme.length; e++) {
                eleme[e].style.visibility = 'hidden';
            }
            
            if(data.state == 'true'){
                
                $("#modal-dialog-benchmark").html(data.content);
                $("#modal-dialog-benchmark").dialog("open");
                $(".ui-dialog-titlebar-close").hide();
                $(".ui-dialog-buttonpane").hide();
                $("#modal-dialog-benchmark-error-common").hide();
                $("#modal-dialog-benchmark-error-exists").hide();
                $("#modal-dialog-benchmark-error-wrong-category").hide();             
            }else{
                $("#modal-dialog-benchmark").html(data.content);
                $("#modal-dialog-benchmark").dialog("open");
                $(".ui-dialog-titlebar-close").hide();
                $(".ui-dialog-buttonpane").hide();
                if(data.state == 'exists'){
                    $("#modal-dialog-benchmark-error-exists").show();
                    $("#modal-dialog-benchmark-success").hide();
                    $("#modal-dialog-benchmark-error-common").hide(); 
                    $("#modal-dialog-benchmark-error-wrong-category").hide();       
                }else{
                    if(data.state == 'wrong_category'){
                        $("#modal-dialog-benchmark-error-exists").hide();
                        $("#modal-dialog-benchmark-success").hide();
                        $("#modal-dialog-benchmark-error-common").hide(); 
                        $("#modal-dialog-benchmark-error-wrong-category").show();       
                    }else{
                        $("#modal-dialog-benchmark-error-common").show();
                        $("#modal-dialog-benchmark-success").hide();
                        $("#modal-dialog-benchmark-error-exists").hide();
                        $("#modal-dialog-benchmark-error-wrong-category").hide();       
                    }
                }
            }
        },
        'json'
    );    
}

// opens the benchmark modal dialog
function openModalDialogBenchmark2(productId){
    // add to benchmark
    $.post("index.php?page=ajax_functions", 
        { remote_function: "addToComparison", product_id: productId, page_name: 'comparison'},
        function(data){
            if(data.state == 'true'){
                $("#modal-dialog-benchmark").html(data.content);
                $("#modal-dialog-benchmark").dialog("open");
                $(".ui-dialog-titlebar-close").hide();
                $(".ui-dialog-buttonpane").hide();
                $("#modal-dialog-benchmark-error-common").hide();
                $("#modal-dialog-benchmark-error-exists").hide();
                $("#modal-dialog-benchmark-error-wrong-category").hide();             
            }else{
                $("#modal-dialog-benchmark").html(data.content);
                $("#modal-dialog-benchmark").dialog("open");
                $(".ui-dialog-titlebar-close").hide();
                $(".ui-dialog-buttonpane").hide();
                if(data.state == 'exists'){
                    $("#modal-dialog-benchmark-error-exists").show();
                    $("#modal-dialog-benchmark-success").hide();
                    $("#modal-dialog-benchmark-error-common").hide(); 
                    $("#modal-dialog-benchmark-error-wrong-category").hide();       
                }else{
                    if(data.state == 'wrong_category'){
                        $("#modal-dialog-benchmark-error-exists").hide();
                        $("#modal-dialog-benchmark-success").hide();
                        $("#modal-dialog-benchmark-error-common").hide(); 
                        $("#modal-dialog-benchmark-error-wrong-category").show();       
                    }else{
                        $("#modal-dialog-benchmark-error-common").show();
                        $("#modal-dialog-benchmark-success").hide();
                        $("#modal-dialog-benchmark-error-exists").hide();
                        $("#modal-dialog-benchmark-error-wrong-category").hide();       
                    }
                }
            }
        },
        'json'
    );    
}

/**
* Reloads the captcha image in a specific element.
*/
function captchaReload(element){
    var now = new Date();
    var html = '<img src="captcha.php?timestamp='+now.getTime()+'&page="/>';
    var id = '#'+element;
    $(id).html(html);    
}


function initToggle() {
    
    // shortbasket box
    // toggle divs to expand/collapse
    $("#mybasket div.toggle").click(function(){
        $("#shortbasket.toggle_content").slideToggle(300);  
        if($(this).hasClass("minus")) {
            $(this).removeClass("minus"); 
            $('#shortbasket_update').hide();
            $('#shortbasket_salesmen').hide();
            $('#mybasket_bg').show();
            $('#shortbasket_bg_1item').hide();
            $('#shortbasket_bg_2items').hide();
            $('#shortbasket_bg_3items').hide();
            $('#shortbasket_coupon').hide();
            $('#short_basket_123').css("border-bottom","1px solid #D5D5D5");
        }else{
            $(this).addClass("minus");
            $('#shortbasket_update').show();
            $('#shortbasket_salesmen').show();    
            $('#mybasket_bg').hide();
            $('#shortbasket_bg_1item').show();
            $('#shortbasket_bg_2items').show();
            $('#shortbasket_bg_3items').show();
            $('#shortbasket_coupon').show();
            $('#short_basket_123').css("border-bottom","none");
        }
    });
}    

// WITHOUT Plugin
var EventUtil = {

    addHandler: function(element, type, handler){
        if (element.addEventListener){
            element.addEventListener(type, handler, false);
        } else if (element.attachEvent){
            element.attachEvent("on" + type, handler);
        } else {
            element["on" + type] = handler;
        }
    },
    
    removeHandler: function(element, type, handler){
        if (element.removeEventListener){
            element.removeEventListener(type, handler, false);
        } else if (element.detachEvent){
            element.detachEvent("on" + type, handler);
        } else {
            element["on" + type] = null;
        }
    },
    
    getEvent: function(event) {
        return event ? event : window.event;
    },
    
    getTarget: function(event) {
        return event.target || event.srcElement;    
    },
    
    getWheelDelta: function(event) {
        if (event.wheelDelta){
            return event.wheelDelta;
        } else {
            return -event.detail * 40;
        }
    },
    
    preventDefault: function(event) {
        if (event.preventDefault){
            event.preventDefault();
        } else {
            event.returnValue = false;
        }
    }
    
};

function onWheel(event) {
    // NEKOM CK - update products_quantity via scroll
    event = EventUtil.getEvent(event);
    var oldVal = $("#hidden_products_quantity_" + products_id).val();
    var curElem = EventUtil.getTarget(event);
    var curVal = parseInt(curElem.value);
    var delta = EventUtil.getWheelDelta(event);
    
    var addVal = 0;
    var last = curElem.id.lastIndexOf("_");
    var products_id = curElem.id.substr(last+1);
    var products_key = products_id + "_XT";    

    if (delta > 0) {
        curElem.value = curVal + 1;
        addVal = 1;
    } else{
        if(curVal!=0){
            curElem.value = curVal - 1;
            addVal = -1;
        } 
    }
    EventUtil.preventDefault(event);
        
    if(IsNumeric(curVal)){
        updateCart(products_id, products_key, curVal, addVal);
        return false;    
    }else{
        $("#products_quantity_" + products_id).val(oldVal);
        window.location = window.location;
        return true;
    }
}

// NEKOM CK - check if enter was hit
function submitenter(myfield, e, products_id, products_key){
    var keycode;
    
    oldVal = $("#hidden_products_quantity_" + products_id).val();
    curVal = $("#products_quantity_" + products_id).val();
    addVal = 0;
    
    if (window.event){
        keycode = window.event.keyCode;  
    } else if(e){
        keycode = e.which;    
    }else{
        return true;    
    }
    
    if (keycode==13 && IsNumeric(curVal)){
        //myfield.form.submit();
        updateCart(products_id, products_key, curVal, addVal);
        return false;
    }else{
        return true;
    }
}

// NEKOM CK - check if value was changed
function onChangeCart(products_id, products_key){
    var keycode;
    
    oldVal = $("#hidden_products_quantity_" + products_id).val();
    curVal = $("#products_quantity_" + products_id).val();
    addVal = 0;

    if(IsNumeric(curVal)){
        updateCart(products_id, products_key, curVal, addVal);
        return false;    
    }else{
        $("#products_quantity_" + products_id).val(oldVal);
        window.location = window.location;
        return true;
    }
}

//  check for valid numeric strings
function IsNumeric(strString){
   var strValidChars = "0123456789";
    var strChar;
    var blnResult = true;

    if(strString.length == 0){
        return false;    
    } 

    //  test strString consists of valid characters listed above
    for(i = 0; i < strString.length && blnResult == true; i++){
        strChar = strString.charAt(i);
        if (strValidChars.indexOf(strChar) == -1){
            blnResult = false;
        }
    }
    
    return blnResult;
}

$(function() {
    // NEKOM CK - scrollable input fields
    var sets = document.getElementsByTagName("input");
    for (var i = 0; i < sets.length; i++){ 
        if (sets[i].id.indexOf("products_quantity_") != -1){ 
            $(".wheelable").hover(function(){
                EventUtil.addHandler(document,'mousewheel',onWheel);
                EventUtil.addHandler(document,'DOMMouseScroll',onWheel);
            },
            function(){
                EventUtil.removeHandler(document,'mousewheel',onWheel);
                EventUtil.removeHandler(document,'DOMMouseScroll',onWheel);
            });        
        }    
    } 
});

function submitform(current_form){
    $("#"+current_form).submit();
}

// jQuery
$(document).ready(function()
{

    // short_basket
    $("#coupon_anzeige_auto_2").css("top","49px");
    $("#coupon_anzeige_auto_2").css("right",$("#coupon_anzeige_auto_1").css("right"));
    $("#coupon_anzeige_auto_2").css("bottom",$("#coupon_anzeige_auto_1").css("bottom"));
    $("#coupon_anzeige_auto_2").css("left",$("#coupon_anzeige_auto_1").css("left"));

    $("#cart_anzeige_2").css("width",$("#cart_anzeige_1").width());
    
    // checkout login radio buttons
    $("#radio_login").click(function(){
        if($("#radio_login").val()=='login'){
            $("#radio_register").attr("checked","");
            $("#radio_shipping").attr("checked","");
            $("#submit_login_1").show();
            $("#submit_login_2").show();
            $("#submit_register_1").hide();
            $("#submit_register_2").hide();
            $("#submit_shipping_1").hide();
            $("#submit_shipping_2").hide();
            $("#checkout_login_1").css("border","1px solid #FF8533");
            $("#checkout_login_2").css("border","1px solid #FFFFFF");
            $("#checkout_login_3").css("border","1px solid #FFFFFF");
        }
    });

    $("#radio_register").click(function(){
        if($("#radio_register").val()=='register'){
            $("#radio_login").attr("checked","");
            $("#radio_shipping").attr("checked","");
            $("#submit_login_1").hide();
            $("#submit_login_2").hide();
            $("#submit_register_1").show();
            $("#submit_register_2").show();
            $("#submit_shipping_1").hide();
            $("#submit_shipping_2").hide();
            $("#checkout_login_1").css("border","1px solid #FFFFFF");
            $("#checkout_login_2").css("border","1px solid #FF8533");
            $("#checkout_login_3").css("border","1px solid #FFFFFF");                                        
        }
    });
    
    $("#radio_shipping").click(function(){
        if($("#radio_shipping").val()=='shipping'){
            $("#radio_login").attr("checked","");
            $("#radio_register").attr("checked","");
            $("#submit_login_1").hide();
            $("#submit_login_2").hide();
            $("#submit_register_1").hide();
            $("#submit_register_2").hide();
            $("#submit_shipping_1").show();
            $("#submit_shipping_2").show();
            $("#checkout_login_1").css("border","1px solid #FFFFFF");
            $("#checkout_login_2").css("border","1px solid #FFFFFF");
            $("#checkout_login_3").css("border","1px solid #FF8533");                        
        }
    });
        
    // PNG fix for all needed browser
    $("img[@src$=png]").pngfix();
      
    // login Box
    $("#show_login").click(function(){
        $("#layer_login_bg").show();
        $("#layer_login").show();
        $("#show_login").css("color", "#ffffff");
    });
    $("#layer_login #h3").click(function(){
        $("#layer_login").hide();
        $("#layer_login_bg").hide();
        $("#show_login").css("color", "");
    });
    $("#layer_login input.text").focus(function () {
        if($(this).val()=='E-Mail-Adresse' || $(this).val()=='Passwort'){
            $(this).val('');    
        } 
    });  
    
    
    if($("#pack_station_value").val()=='true'){
        $("#pack_station_checkbox").attr("checked", true);
        $("#shipping_address_checkout").hide();
        $("#customers_pack_station_left").hide();
        $("#customers_pack_station_data").show();
        $("#customers_pack_station_line").hide();     
    }
    
    // Aus-/Einblenden der Packstation im Checkout
    if(window.show_shipping_address_checkbox !== undefined && show_shipping_address_checkbox=='on'){
         
            // disable pack_station
            $("#pack_station_checkbox_1").attr("checked","");
            $("#customers_pack_station_right").attr("style", "display: none;");
            $("#customers_pack_station_data").attr("style", "display: none;");
            
            // enable shipping_address
            $("#shipping_address_data").attr("style", "display: block;");
            $("#shipping_address_checkbox_1").attr("checked","checked");
            
    }
     
    if(window.show_pack_station_checkbox !== undefined && show_pack_station_checkbox=='on'){
            
            // disable shipping_address
            $("#shipping_address_checkbox_1").attr("checked","");
            $("#shipping_address_checkout").attr("style", "display: none;");
            
            // enable pack_station            
            $("#customers_pack_station_right").attr("style", "display: block;");
            $("#customers_pack_station_data").attr("style", "display: block;");
            $("#customers_pack_station_line").attr("style", "display: block;");
            $("#shipping_address_checkbox_2").attr("checked","");
            $("#pack_station_checkbox_2").attr("checked","checked");  
                 
    }     
    // ------------------------------------ //
    
    // Aus-/Einblenden der Packstation im Checkout
    $("#pack_station_checkbox_1").click(function(){
        if($("#pack_station_checkbox_1:checked").val()!=null){
            
            // disable shipping_address
            $("#shipping_address_checkbox_1").attr("checked","");
            $("#shipping_address_checkout").attr("style", "display: none;");
            
            // enable pack_station            
            $("#customers_pack_station_right").attr("style", "display: block;");
            $("#customers_pack_station_data").attr("style", "display: block;");
            $("#shipping_address_checkbox_2").attr("checked","");
            $("#pack_station_checkbox_2").attr("checked","checked");
            
        }
    });
    
    $("#pack_station_checkbox_2").click(function(){
        if($("#pack_station_checkbox_2:checked").val()!=null){
            
            // enable pack_station
            $("#customers_pack_station_right").attr("style", "display: block;");
            $("#customers_pack_station_data").attr("style", "display: block;");
            $("#pack_station_checkbox_1").attr("checked","checked");
            
            // disable shipping_address
            $("#shipping_address_checkout").attr("style", "display: none;");
            
        }else{
            
            // enable shipping_address
            $("#shipping_address_checkout").attr("style", "display: block;");
            $("#shipping_address_data").attr("style", "display: none;");
            
            // disable pack_station
            $("#customers_pack_station_right").attr("style", "display: none;");
            $("#customers_pack_station_data").attr("style", "display: none;");
            $("#pack_station_checkbox_1").attr("checked","");
            
        }
    });
    // ------------------------------------ //
    
    // Aus-/Einblenden der Lieferadresse im Checkout
    $("#shipping_address_checkbox_1").click(function(){
        if($("#shipping_address_checkbox_1:checked").val()!=null){
            
            // disable pack_station
            $("#pack_station_checkbox_1").attr("checked","");
            $("#customers_pack_station_right").attr("style", "display: none;");
            $("#customers_pack_station_data").attr("style", "display: none;");
            
            // enable shipping_address
            $("#shipping_address_data").attr("style", "display: block;");

        }else{
            
            // disable shipping_address
            $("#shipping_address_data").attr("style", "display: none;");
            
        }
    });

    $("#shipping_address_checkbox_2").click(function(){
        if($("#shipping_address_checkbox_2:checked").val()!=null){
            
            // disable checkbox & pack_station
            $("#pack_station_checkbox_2").attr("checked","");
            $("#customers_pack_station_right").attr("style", "display: none;");
            $("#customers_pack_station_data").attr("style", "display: none;");
            
            // enable shipping_address
            $("#shipping_address_checkout").attr("style", "display: block;");
            $("#shipping_address_data").attr("style", "display: block;");
            $("#shipping_address_checkbox_1").attr("checked","checked");
            $("#pack_station_checkbox_1").attr("checked","");
            
        }
    });    
    // ------------------------------------ //
    
    // Checkout_Confirmation Checkboxen synchronisieren
    $("#form_agb1").click(function(){
        if($("#form_agb1").is(":checked")){
           $("#form_agb2").attr("checked","checked");
        }else{
           $("#form_agb2").attr("checked",""); 
        }
    });    
    $("#form_agb2").click(function(){
        if($("#form_agb2").is(":checked")){
           $("#form_agb1").attr("checked","checked");
        }else{
           $("#form_agb1").attr("checked",""); 
        }
    }); 
    $("#form_newsletter1").click(function(){
        if($("#form_newsletter1").is(":checked")){
           $("#form_newsletter2").attr("checked","checked");
        }else{
           $("#form_newsletter2").attr("checked",""); 
        }
    });    
    $("#form_newsletter2").click(function(){
        if($("#form_newsletter2").is(":checked")){
           $("#form_newsletter1").attr("checked","checked");
        }else{
           $("#form_newsletter1").attr("checked",""); 
        }
    })    
       
//    customers_pack_station_data_note
//    shipping_address_data    
        
    // newsletter box
    $("#newsletterform input.text").focus(function () {
         $(this).val('');
    });
    
    // search box
    $(".quicksearch .text").focus(function () {
         //$(this).val('');
    });
    
    initToggle();
    
    // registration form
    // toggle divs to expand/collapse
    $("#registration-addresses div.toggle").click(function () {
        $("#registration-addresses div.toggle_content").slideToggle(300);
        if($(this).hasClass("minus")) {
            $(this).removeClass("minus");
        } else {
            $(this).addClass("minus");
        }
    });
    $("#registration-contact div.toggle").click(function () {
        $("#registration-contact div.toggle_content").slideToggle(300);
        if($(this).hasClass("minus")) {
            $(this).removeClass("minus");
        } else {
            $(this).addClass("minus");
        }
    });
    
    // dates

    $(".dates .text").focus(function () 
    {
         $(this).val('');
    });
    
    // product comparison
    /* init comparison table */
    $('#comparetable tbody tr:even').addClass('odd');
    checkCompareItems();
    
    // go top links
    $("p.top a").click( function() {
        $.scrollTo('#page', 800, {
            offset : {
                top :0
            }
        });
    });
    
    // faq anchor links

    $("ul.faq li a").click( function() 
    {          
        var questionid = $(this).attr("id");
        var jumpto = questionid.substr(1, 10);
    

        $.scrollTo('#answ'+jumpto, 800, 
        {
            offset : 
            {
                top :-10
            }
        });
    });   
    
    // product detail page
    // tabs functionality
    $("#tabs").tabs();
    // initialize modal dialog cart and modla dialog cart error
    $("#modal-dialog-cart").dialog(modalDialogCartOptions);     
    $("#modal-dialog-cart-error").dialog(modalDialogCartOptions); 
    $("#modal-dialog-content").dialog(modalDialogContent);
    $("#modal-dialog-contentSocket").dialog(modalDialogContentSocket);
    
    // initialize modalDialogCheckoutSuccessLightbox
    $("#modal-dialog-CheckoutSuccessLightbox").dialog(modalDialogCheckoutSuccessLightbox);    
    
    // initialize modal dialog review
    $("#modal-dialog-review").dialog(modalDialogReviewOptions);
    // initialize modal dialog review
    $("#modal-dialog-products-criteria").dialog(modalDialogProductsCriteriaOptions);
    // initialize modal dialog customer product media
    $("#modal-dialog-customer-product-media").dialog(modalDialogCustomerProductMediaOptions);
    // initialize modal dialog cart 
    $("#modal-dialog-benchmark").dialog(modalDialogBenchmarkOptions);
   
    /**
    * mask defines
    */
    /*$('input:text').setMask();
    $.mask.rules =  {
                'z': /[a-z]/,
                'Z': /[A-Z]/,
                'a': /[a-zA-Z]/,
                'n': /[0-9:blank:]/,
                '@': /[0-9a-zA-ZçÇáàãéèíìóòõúùü]/,
                '+': /[+]/,
                '-': /[-]/,
                '0': /[0]/,
                '1': /[0-1]/,
                '2': /[0-2]/,
                '3': /[0-3]/,
                '4': /[0-4]/,
                '5': /[0-5]/,
                '6': /[0-6]/,
                '7': /[0-7]/,
                '8': /[0-8]/,
                '9': /[0-9]/};
                
    $.mask.masks = {
                'phone'     : { mask : '+99-9999-999nnnnnn' },
                'mobile'     : { mask : '+99-999-999nnnnnn' },
                'date'      : { mask : '39.19.9999' }, //uk date
                'time'      : { mask : '29:69' },
                'cc'        : { mask : '9999 9999 9999 9999' }, //credit card mask
                'integer'   : { mask : '999.999.999.999', type : 'reverse' },
                'decimal'   : { mask : '99,999.999.999.999', type : 'reverse', defaultValue: '000' },
                'decimal-us'    : { mask : '99.999,999,999,999', type : 'reverse', defaultValue: '000' }
                }; */

});

