function lisaaOstoskoriin(tuoteid) {

    if (tuoteid.length == 0) {
        return;
    }

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert ("Browser does not support HTTP Request");
        return;
    }

    var url = "/sivuostoskori.php";
    url = url + "?lisaatuoteid=" + tuoteid;
    
    xmlHttp.onreadystatechange = stateChanged;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}

function paivitaSivuostoskori() {

    xmlHttp2 = GetXmlHttpObject();
    if (xmlHttp2 == null) {
        alert ("Browser does not support HTTP Request");
        return;
    }

    var url = "/sivuostoskori.php";

    xmlHttp2.onreadystatechange = paivitaSivuostoskoriStateChanged;
    xmlHttp2.open("GET", url, true);
    xmlHttp2.send(null);

}

function muokkaaOstoskoria(tuoteid, maara) {

    if (tuoteid.length == 0 || maara.length == 0) {
        return;
    }

    xmlHttp = GetXmlHttpObject();
    if (xmlHttp == null) {
        alert ("Browser does not support HTTP Request");
        return;
    }

    var url = "/ostoskori/";
    url = url + "?muokkaatuoteid=" + tuoteid;
    url = url + "&muokkaamaara=" + maara;

    xmlHttp.onreadystatechange = isoOstoskoriStateChanged;
    xmlHttp.open("GET", url, true);
    xmlHttp.send(null);

}

function stateChanged() {

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        document.getElementById("ostoskori").innerHTML = xmlHttp.responseText;
        if (!onkoNakyvissa(document.getElementById("ostoskori"))) {
            new Effect.ScrollTo('ostoskori');
        }
        new Effect.Vilku('ostoskori', { pulses: 5, duration: 1.5 });
    }

}

function paivitaSivuostoskoriStateChanged() {

    if (xmlHttp2.readyState == 4 || xmlHttp2.readyState == "complete") {
        document.getElementById("ostoskori").innerHTML = xmlHttp2.responseText;
    }

}

function isoOstoskoriStateChanged() {

    if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
        document.getElementById("isoostoskori").innerHTML = xmlHttp.responseText;
        new Effect.Pulsate('yhteishinta', { pulses: 1, duration: 0.5 });
    }
    paivitaSivuostoskori();
}

function GetXmlHttpObject() {

    var objXMLHttp = null;
    if (window.XMLHttpRequest) {
        objXMLHttp = new XMLHttpRequest();
    } else if (window.ActiveXObject) {
        objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    return objXMLHttp;
    
}

function tarkistaTilaus() {

    etunimi = document.getElementById("etunimi").value;
    sukunimi = document.getElementById("sukunimi").value;
    katuosoite = document.getElementById("katuosoite").value;
    postinumero = document.getElementById("postinumero").value;
    postitoimipaikka = document.getElementById("postitoimipaikka").value;
    puhelinnumero = document.getElementById("puhelinnumero").value;
    email = document.getElementById("email").value;
    virhe = false;
    
    if (email == "") {
        document.getElementById("email").focus();
        document.getElementById("emailrivi").className = 'punainen';
        virhe = true;
    }
    if (puhelinnumero == "") {
        document.getElementById("puhelinnumero").focus();
        document.getElementById("puhelinnumerorivi").className = 'punainen';
        virhe = true;
    }
    if (postitoimipaikka == "") {
        document.getElementById("postitoimipaikka").focus();
        document.getElementById("postitoimipaikkarivi").className = 'punainen';
        virhe = true;
    }
    if (postinumero == "") {
        document.getElementById("postinumero").focus();
        document.getElementById("postinumerorivi").className = 'punainen';
        virhe = true;
    }
    if (katuosoite == "") {
        document.getElementById("katuosoite").focus();
        document.getElementById("katuosoiterivi").className = 'punainen';
        virhe = true;
    }
    if (sukunimi == "") {
        document.getElementById("sukunimi").focus();
        document.getElementById("sukunimirivi").className = 'punainen';
        virhe = true;
    }
    if (etunimi == "") {
        document.getElementById("etunimi").focus();
        document.getElementById("etunimirivi").className = 'punainen';
        virhe = true;
    }
    if (virhe == true) {
        alert("Täytä kaikki yhteystietosi");
        return false;
    }
    return true;

}

function tarkistaKentta(obj) {
    if (obj.value != "") {
        obj.parentNode.parentNode.className = "musta";
    }
    return;
}

function onkoNakyvissa(obj) {

    scrollOffsets = document.viewport.getScrollOffsets();
    if (scrollOffsets.top > obj.offsetTop) {
        return false;
    } else {
        return true;
    }

}

function externalLinks() {
    if (!document.getElementsByTagName) return;
    var anchors = document.getElementsByTagName("a");
    for (var i=0; i<anchors.length; i++) {
        var anchor = anchors[i];
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
            anchor.target = "_blank";
    }
}
window.onload = externalLinks;

Effect.Vilku = function(element) {
  element = $(element);
  var options    = arguments[1] || { },
    oldOpacity = element.getInlineOpacity(),
    transition = options.transition || Effect.Transitions.linear,
    reverser   = function(pos){
      return 1 - transition((-Math.cos((pos*(options.pulses||5)*2)*Math.PI)/2) + .5);
    };

  return new Effect.Opacity(element,
    Object.extend(Object.extend({  duration: 2.0, from: 0,
      afterFinishInternal: function(effect) { effect.element.setStyle({opacity: 1}); }
    }, options), {transition: reverser})); 
};

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function naytaAina() {

    bottomlimit = 60;
    toplimit = 345;
    id1 = 'oikeapalkki';
    obj1 = document.getElementById(id1);
    scrollOffsets = document.viewport.getScrollOffsets();
    if ((scrollOffsets.top > obj1.offsetTop || obj1.offsetTop > toplimit) && (obj1.offsetHeight + bottomlimit) < document.documentElement.clientHeight) {
        var ymove = scrollOffsets.top - obj1.offsetTop;
        if (scrollOffsets.top > toplimit) {
            new Effect.Move(id1, { x: 0, y: ymove, mode: 'relative' });
        } else {
            new Effect.Move(id1, { x: 0, y: 0, mode: 'absolute' });
        }
    } 
  return false;

}
new PeriodicalExecuter(naytaAina, 1);

function poistaTeksti(elem) {

    if (elem.value == "Tuotehaku") {
        elem.value = "";
    }
    elem.className = "musta";

}
