menu_status = new Array();

function showHide(theid){
    if (document.getElementById) {
    var switch_id = document.getElementById(theid);

        if(menu_status[theid] != 'show') {
           switch_id.className = 'show';
           menu_status[theid] = 'show';
        }else{
           switch_id.className = 'hide';
           menu_status[theid] = 'hide';
        }
    }
}

var last_expanded = '';

function showHide(id)
{
	var obj = document.getElementById(id);
	
	var status = obj.className;
	
	if (status == 'hide') {	
		if (last_expanded != '') {
			var last_obj = document.getElementById(last_expanded);
			last_obj.className = 'hide';
		}		
		obj.className = 'show';
		
		last_expanded = id;
	} else {
		obj.className = 'hide';
	}
}

function hide(theid,theid2){
    if (document.getElementById && theid!=theid2 && theid!=0 && theid2!=0) {
		var switch_id = document.getElementById(theid);
	    switch_id.className = 'hide';
    }
}


function show_image(imagename) {
    imgwin = window.open('', 'imgwin',
	'toolbar=no,width=100,height=100,resizable=yes');
    imgwin.document.write('<html><head><title>Kuva</title></head>'
	+ '<body onload="window.resizeTo('
	+ 'document.images[0].width + 40, document.images[0].height+130)">'
	+ '<img src="' + imagename + '"></body></html>');
    imgwin.document.close();
}

function menu_hover(that,color) {
//	that.style.backgroundColor="#4e8f71";
	that.style.backgroundColor=color;
}

function menu_hover_out(that,color) {
//	that.style.backgroundColor="#45745e";
	that.style.backgroundColor=color;
}

function lisaaTuote(tuotenro,tuoteid,hinta,aletuote) {
	var xmlHttp;
	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	} catch (e) {
	  // Internet Explorer
	  try {
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (e) {
		try {
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
		// selain ei tue Ajaxia		
		  return false;
		}
	  }
    }
    
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
		  var data = xmlHttp.responseText.split(';');   // response = yht_kpl;yht_hinta;tuote_kpl

		  var koritietoObj = document.getElementById('koritieto'+tuotenro);
		  if(koritietoObj!=null) koritietoObj.innerHTML = 'Korissa '+data[2]+' kpl ';
   		  
		  //if(koritietoObj!=null) koritietoObj.innerHTML = xmlHttp.responseText;    // debuggausta varten		  

		  paivitaYhteenveto(data[0],'tuotteet_yht',data[1],'tuotteet_summa');
		}
	}

	var korikplObj = document.getElementById('korikpl'+tuotenro);
	var kpl = parseInt(korikplObj.value);
	if(isNaN(kpl)) {
		kpl=1;
	}
	korikplObj.value='';
	
	var koko='';
	var korikokoObj = document.getElementById('koko_'+tuotenro);
	if(korikokoObj!=null) {
		var ix = korikokoObj.selectedIndex;
		if(Math.abs(Math.floor(ix))==ix) {
			var korierrorObj = document.getElementById('koko_error_'+tuotenro);
			if(ix!=0) {
				koko=korikokoObj.options[ix].text;
				if(korierrorObj!=null) {
					korierrorObj.innerHTML = '';
				}				
			} else {
				if(korierrorObj!=null) {
					korierrorObj.innerHTML = ' Valitse&nbsp;koko ';
					korikplObj.value=kpl;
					return;
				}				
			}
		}
	}
	
	var vaihtoehto='';
	var korivaihtoehtoObj = document.getElementById('vaihtoehdot_'+tuotenro);
	if(korivaihtoehtoObj!=null) {
		var ix = korivaihtoehtoObj.selectedIndex;
		if(Math.abs(Math.floor(ix))==ix) {
			var korierrorObj = document.getElementById('vaihtoehdot_error_'+tuotenro);
			if(ix!=0) {
				vaihtoehto=korivaihtoehtoObj.options[ix].text;
				if(korierrorObj!=null) {
					korierrorObj.innerHTML = '';
				}				
			} else {
				if(korierrorObj!=null) {
					korierrorObj.innerHTML = ' Valitse&nbsp;vaihtoehto ';
					korikplObj.value=kpl;
					return;
				}
			}
		}
	}
	
	xmlHttp.open("GET","../nettikauppa/ajaxtuote.php?tuoteid="+tuoteid+"&hinta="+hinta+"&kpl="+kpl+"&koko="+koko+"&vaihtoehto="+vaihtoehto+"&aletuote="+aletuote,true);
	xmlHttp.send(null);
}


// Päivittää ostoskorin hintatiedot, kun tuotemääriä muutetaan
// function paivitaHinta(tuotenro,hinta,tuoteRiviId,kplId,hintaId,yhtHintaId) {
function paivitaHinta(tuotenro,hinta) {
	var xmlHttp;
	try {
	  // Firefox, Opera 8.0+, Safari
	  xmlHttp=new XMLHttpRequest();
	} catch (e) {
	  // Internet Explorer
	  try {
		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
	  } catch (e) {
		try {
		  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {
		// selain ei tue Ajaxia
		  return false;
		}
	  }
    }
    
	xmlHttp.onreadystatechange=function() {
		if(xmlHttp.readyState==4) {
			var data = xmlHttp.responseText.split(';');   // response = kpl;hinta;yht_tuotteet;yht_hinta;alennus tai remove;-;yht_tuotteet;yht_hinta;alennus
			var yhtHintaObj2 = document.getElementById('yhteensa');
			
			if(data[0]=='remove') {
				var tuoteRiviObj = document.getElementById('rivi'+tuotenro);
				if(tuoteRiviObj!=null) tuoteRiviObj.parentNode.removeChild(tuoteRiviObj);
				if(parseInt(data[2])==0) {	// jos listan viimeinen tuote poistettu
					var ostoskoriTableObj = document.getElementById("ostoskori_table");
					if(ostoskoriTableObj!=null) ostoskoriTableObj.parentNode.removeChild(ostoskoriTableObj);
					var alakappaleObj = document.getElementById("ostoskori_alakappale");
					if(alakappaleObj!=null) alakappaleObj.innerHTML='Ostoskorisi on tyhj&auml;';
					var lahetaformObj = document.getElementById("laheta_form");
					if(lahetaformObj!=null) lahetaformObj.parentNode.removeChild(lahetaformObj);
				}
			} else {
				var hintatietoObj = document.getElementById('hintatieto'+tuotenro);
				if(hintatietoObj!=null) {
					var hintaStr = String(parseFloat(data[1]).toFixed(2));
					hintatietoObj.innerHTML = hintaStr.replace(".",",");
				}
				var korikplObj = document.getElementById('kpl'+tuotenro);
				if(korikplObj!=null) korikplObj.value = data[0];
			}
			var yhtHintaObj = document.getElementById('yhteensa');
			var yhteensa = parseFloat(data[3]).toFixed(2);
			if(yhtHintaObj!=null) yhtHintaObj.innerHTML = String(yhteensa).replace(".",",");

			var alenusObj = document.getElementById('alennus');
			var alennus = (0-parseFloat(data[4])).toFixed(2);
			if(alenusObj!=null) alenusObj.innerHTML = String(alennus).replace(".",",");
			var yhtAleHintaObj = document.getElementById('yhteensa_ale');
			if(yhtAleHintaObj!=null) yhtAleHintaObj.innerHTML = String((parseFloat(yhteensa)+parseFloat(alennus)).toFixed(2)).replace(".",",");
//			if(yhtAleHintaObj!=null) yhtAleHintaObj.innerHTML = xmlHttp.responseText;    // debuggausta varten
			
			paivitaYhteenveto(data[2],"tuotteet_yht",data[3],"tuotteet_summa");
		}
	}

	var korikplObj = document.getElementById('kpl'+tuotenro);
	var kpl = parseInt(korikplObj.value);
	if(isNaN(kpl) && kpl<0) {
		return;
	}

/*	var debug_tkulut_obj = document.getElementById('td_tkulut');
	var getstr = "../nettikauppa/ajaxostoskori.php?tuotenro="+tuotenro+"&hinta="+hinta+"&kpl="+kpl;
	if(debug_tkulut_obj!=null) debug_tkulut_obj.innerHTML = getstr;  /* debuggausta varten */
	
	xmlHttp.open("GET","../nettikauppa/ajaxostoskori.php?tuotenro="+tuotenro+"&hinta="+hinta+"&kpl="+kpl,true);
	xmlHttp.send(null);
}



function paivitaYhteenveto(n,nId,summa,summaId) {
	  var tuotteetkplObj = document.getElementById(nId);
	  if(tuotteetkplObj!=null) tuotteetkplObj.innerHTML = parseInt(n);
	
	  var tuotteetsummaObj = document.getElementById(summaId);
	  if(tuotteetsummaObj!=null) tuotteetsummaObj.innerHTML = (String(parseFloat(summa).toFixed(2))).replace(".",",");
}

