   var valikkono;

   function ajax(url,paikka) {
       $('#' + paikka).html("<img src='/tiedostot/sivusto/loading.gif' />");
       var req = false;
       if (window.XMLHttpRequest) { // Mozilla, Safari
           req = new XMLHttpRequest();
       }else if(window.ActiveXObject) { // IE
           req = new ActiveXObject("Microsoft.XMLHTTP");
       }
       req.open('POST', url, true);
       req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
       req.onreadystatechange = function() {
           if (req.readyState == 4) {
               $('#' + paikka).html(req.responseText);
           }
       };
       req.send('');
   }

   function info(id,mode) {
       var maaritykset = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=600,height=400,left=10,top=10";
       window.open("info.asp?id=" + id + "&_mode=" + mode,"Info",maaritykset).focus();
   }

   function info2(id,mode){
       if(!$("#info" + id + "_" + mode).html()){
          //ajax("info2.asp?_id=" + id + "&_mode=" + mode,"info" + id + "_" + mode);
          $.ajax({
             url: "info2.asp?_id=" + id + "&_mode=" + mode,
             success: function(transport){
                $("#info" + id + "_" + mode).html(transport);
             }
          });
       }
       $("#info" + id + "_" + mode).fadeToggle();
   }

   function lisatiedot2(id,tuoteid,omin1,omin2,omin3){
       if(!$("#" + id).html()){
          $.ajax({
             url: "rivitila2.asp?id=" + id + "&_tuoteid=" + tuoteid + "&_omin1=" + omin1 + "&_omin2=" + omin2 + "&_omin3=" + omin3,
             success: function(transport){
                $("#" + id).html(transport);
             }
          });
       }
       $("#" + id).fadeToggle();
   }

   function updater(paikka,osoite){
       $("#" + paikka).html('<img src="/tiedostot/sivusto/loading.gif" />');
       $("#" + paikka).load(
          encodeURI(osoite), function(response, status, xhr) {
              if (status == "error") {
                  $("#" + paikka).html(xhr.status + " " + xhr.statusText);
              }
          }
       );
   }

   function class_updater(paikka,osoite){
       $("." + paikka).html('<img src="/tiedostot/sivusto/loading.gif" />');
       $("." + paikka).load(encodeURI(osoite), function(response, status, xhr) {
               if (status == "error") {
                   var msg = "Sorry but there was an error: ";
                   $("#error").html(msg + xhr.status + " " + xhr.statusText);
               }
           }
       );
   }

   function animate_bg(ele, from, to) {
      from += from > to ? -1 : 1;
      if(!$.support.opacity){
         if(from != to){
            var opStr = (Math.round(from * 25.5)).toString(16);
            ele.css({background:'transparent',filter:"progid:DXImageTransform.Microsoft.gradient(startColorstr=#" + opStr + "FFFFFF, endColorstr=#" + opStr + "FFFFFF)"});
         }else{
            ele.css({background:'transparent',filter:"none"});
         }
      }else{
         ele.css("backgroundColor", "rgba(255, 255, 255, " + (from) / 10 + ")");
      }
      if(from != to) setTimeout(function() { animate_bg(ele, from, to) }, 50);
   }

   jQuery.fn.fadeToggle = function(speed, easing, callback) { 
      return this.animate({opacity: 'toggle'}, speed, easing, callback); 
   }; 

   function skandit4(response){
       if(response !== undefined){
           response = response.replace(/_auml_/g ,String.fromCharCode(228));
           response = response.replace(/_ouml_/g ,String.fromCharCode(246));
           response = response.replace(/_Auml_/g ,String.fromCharCode(196));
           response = response.replace(/_Ouml_/g ,String.fromCharCode(214));
           response = response.replace(/_aring_/g,String.fromCharCode(229));
           response = response.replace(/_Aring_/g,String.fromCharCode(197));
       }
       return response;
   }

   function skandit4x(response){
       if(response !== undefined){
           response = response.replace(new RegExp(String.fromCharCode(228),"g"), "_auml_");
           response = response.replace(new RegExp(String.fromCharCode(246),"g"), "_ouml_");
           response = response.replace(new RegExp(String.fromCharCode(196),"g"), "_Auml_");
           response = response.replace(new RegExp(String.fromCharCode(214),"g"), "_Ouml_");
           response = response.replace(new RegExp(String.fromCharCode(229),"g"), "_aring_");
           response = response.replace(new RegExp(String.fromCharCode(197),"g"), "_Aring_");
       }
       return response;
   }

   function rastita(checkname,statename) {
       for (i = 0; i < checkname.length; i++){
           checkname[i].checked = statename.checked? true:false;
       }
   } 


(function(jQuery) {
    jQuery.fn.smartupdater = function (options, callback) {

        return this.each(function () {
                var elem = this;

                elem.settings = jQuery.extend({
                        url: '',// see jQuery.ajax for details
                        type: 'get', // see jQuery.ajax for details
                        data: '',   // see jQuery.ajax for details
                        dataType: 'text', // see jQuery.ajax for details
                        
                        minTimeout: 60000, // Starting value for the timeout in milliseconds; default 1 minute.
                        maxTimeout: ((1000 * 60) * 60), // Default 1 hour.
                        multiplier: 2,    //if set to 2, interval will double each time the response hasn't changed.
                        maxFailedRequests : 10 // smartupdater stops after this number of consecutive ajax failures; 
                        
                    }, options);
                
                elem.smartupdaterStatus = {};
                elem.smartupdaterStatus.state = '';
                elem.smartupdaterStatus.timeout = 0;

                var es = elem.settings;
                
                es.prevContent = '';
                es.originalMinTimeout = es.minTimeout;
                es.failedRequests = 0;
                es.response = '';
                
                function start() {
                    $.ajax({url: es.url,
                                type: es.type,
                                data: es.data,
                                dataType: es.dataType,
                                cache: false,
                                success: function (data, statusText, xhr) {
                                
                                if(es.dataType == 'json' && data.smartupdater) {
                                    es.originalMinTimeout = data.smartupdater;
                                }

                                if (es.prevContent != xhr.responseText) {
                                    es.prevContent = xhr.responseText;
                                    es.minTimeout = es.originalMinTimeout;
                                    elem.smartupdaterStatus.timeout = es.minTimeout;
                                    es.periodicalUpdater = setTimeout(start, es.minTimeout);
                                    callback(data);
                                } else if (es.multiplier > 1) {
                                    es.minTimeout = (es.minTimeout < es.maxTimeout) ? Math.round(es.minTimeout * es.multiplier) : es.maxTimeout;
                                    elem.smartupdaterStatus.timeout = es.minTimeout;
                                    es.periodicalUpdater = setTimeout(start, es.minTimeout);
                                } else if (es.multiplier <= 1) {
                                    es.minTimeout = es.originalMinTimeout;
                                    elem.smartupdaterStatus.timeout = es.minTimeout;
                                    es.periodicalUpdater = setTimeout(start, es.minTimeout);
                                }
                                
                                es.failedRequests = 0;
                                elem.smartupdaterStatus.state = 'ON';
                            }, 
                                
                                error: function() { 
                                if ( ++es.failedRequests < es.maxFailedRequests ) {
                                    es.periodicalUpdater = setTimeout(start, es.minTimeout);
                                    elem.smartupdaterStatus.timeout = es.minTimeout;
                                } else {
                                    clearTimeout(es.periodicalUpdater);
                                    elem.smartupdaterStatus.state = 'OFF';
                                }
                            }
                        });
                } 
                
                es.fnStart = start;
                start();
            });
    }; 
    
    jQuery.fn.smartupdaterStop = function () {
        return this.each(function () {
                var elem = this;
                clearTimeout(elem.settings.periodicalUpdater);
                elem.smartupdaterStatus.state = 'OFF';
            });
    }; 
        
    jQuery.fn.smartupdaterRestart = function () {        
        return this.each(function () {
                var elem = this;
                clearTimeout(elem.settings.periodicalUpdater);
                elem.settings.minTimeout = elem.settings.originalMinTimeout;
                elem.settings.fnStart();
            });
    }; 
    
    jQuery.fn.smartupdaterSetTimeout = function (period) {
        return this.each(function () {
                var elem = this;
                clearTimeout(elem.settings.periodicalUpdater);
                this.settings.originalMinTimeout = period;
                this.settings.fnStart();
            });
    }; 
    
})(jQuery);

