var kulmat="",zoomi=0,uniti=0,uniti_save="",ser1="",mat1="",seq1="",kul1="",nay1="",sex1="",product1="",colors1="",sCookie=1,winW,windowheight,divWidth,imgHeight,screenWidth,tdWidth,divHeight,pictureNameGlobal,figureNow="",popUp,lahjaOstos=false;
var sel,t,x,s,z,a,v,c,b,m,g,h,r,TimeToFade=500,TimeToFadeHelp,lisaTuloste1="",lisaTuloste2="",recentSpeech="",howHeight=false,repeatMove=0;
var figureMinHeight;

if(toPhp==="600")
	figureMinHeight = 580;
else if(toPhp==="648")
	figureMinHeight = 600;
else if(toPhp==="768")
	figureMinHeight = 660;
else if(toPhp==="1024")
	figureMinHeight = 730;
else if(toPhp==="720")
	figureMinHeight = 660;
else if(toPhp==="800")
	figureMinHeight = 670;
else if(toPhp==="900")
	figureMinHeight = 700;
else if(toPhp==="1050")
	figureMinHeight = 750;

function menuOver(id){ $("#"+id).attr("src","image/BGmenuHover.png"); }
function menuDown(id){ $("#"+id).attr("src","image/BGmenuActive.png"); }
function menuOut(id){ $("#"+id).attr("src","image/BGmenuButton.png"); }

function buttonMouseOver(id){ $("#"+id).attr("src","image/BGmenuButton.png"); }
function buttonMouseDown(id){ $("#"+id).attr("src","image/BGmenuButton.png"); }
function buttonMouseOut(id){ $("#"+id).attr("src","image/BGmenuButton.png"); }

function postPhp(site,send){
	
	var html = $.ajax({
		type: "POST",
 	 	url: site,
		data: send,
  		async: false
 	}).responseText;
	
	return html;
	
}

function aPhp(site,send){
	
	$.ajax({
		type: "POST",
 	 	url: site,
		data: send,
  		async: true
 	});
	
}

function ab(l,k){ $("#"+l).html(k); }

function figureClear(){
	if(a!==null){clearTimeout(a);}
	if(t!==null){clearTimeout(t);}
	if(x!==null){clearTimeout(x);}
	if(s!==null){clearTimeout(s);}
	if(z!==null){clearTimeout(z);}
	if(v!==null){clearTimeout(v);}
	if(c!==null){clearTimeout(c);}
	if(b!==null){clearTimeout(b);}
	if(m!==null){clearTimeout(m);}
	if(g!==null){clearTimeout(g);}
	if(h!==null){clearTimeout(h);}
	if(r!==null){clearTimeout(r);}
}

function scrollbar(tila,maara){
	var vertailuLeft;
	
	//if(tila==="auto"){
		winW  = $(window).width();
		windowheight = $(window).height();
	//}
	
	if(toPhp==="600")
	{
		divWidth = winW * 0.3;
		
		if(divWidth>220){ screenWidth = 220;}
		else if(divWidth<=220 && divWidth>=170){screenWidth=divWidth;}
		else if(divWidth<170){ screenWidth = 170;}	
		
		imgHeight = screenWidth / 2.2 + "px";
		tdWidth = screenWidth / 3.5 + "px";
		divWidth = screenWidth / 3.0 + "px";
		divHeight = screenWidth / 1.7 + "px";
	}
	else if(toPhp==="648")
	{
		divWidth = winW * 0.4;
		
		if(divWidth>420){ screenWidth = 420;}
		else if(divWidth<=420 && divWidth>=250){screenWidth=divWidth;}
		else if(divWidth<250){ screenWidth = 250;}	
		
		imgHeight = screenWidth / 3.0 + "px";
		tdWidth = screenWidth / 4.2 + "px";
		divWidth = screenWidth / 3.3 + "px";
		divHeight = screenWidth / 2.3 + "px";
	}
	else if(toPhp==="768")
	{
		divWidth = winW * 0.31;
		
		if(divWidth>340){ screenWidth = 340;}
		else if(divWidth<=340 && divWidth>=200){screenWidth=divWidth;}
		else if(divWidth<200){ screenWidth = 200;}	
		
		imgHeight = screenWidth / 2.2 + "px";
		tdWidth = screenWidth / 3.7 + "px";
		divWidth = screenWidth / 3.3 + "px";
		divHeight = screenWidth / 1.92 + "px";
	}
	else if(toPhp==="1024")
	{
		divWidth = winW * 0.35;
		
		if(divWidth>450){ screenWidth = 450;}
		else if(divWidth<=450 && divWidth>=340){screenWidth=divWidth;}
		else if(divWidth<340){ screenWidth = 340;}	
		
		imgHeight = screenWidth / 3.0 + "px";
		tdWidth = screenWidth / 4.4 + "px";
		divWidth = screenWidth / 3.5 + "px";
		divHeight = screenWidth / 2.3 + "px";
	}
	else if(toPhp==="720")
	{
		divWidth = winW * 0.35;
		
		if(divWidth>450){ screenWidth = 450;}
		else if(divWidth<=450 && divWidth>=330){screenWidth=divWidth;}
		else if(divWidth<330){ screenWidth = 330;}	
		
		imgHeight = screenWidth / 3.0 + "px";
		tdWidth = screenWidth / 4.4 + "px";
		divWidth = screenWidth / 3.5 + "px";
		divHeight = screenWidth / 2.3 + "px";
	}
	else if(toPhp==="800")
	{
		divWidth = winW * 0.35;
		
		if(divWidth>400){ screenWidth = 400;}
		else if(divWidth<=400 && divWidth>=310){screenWidth=divWidth;}
		else if(divWidth<310){ screenWidth = 310;}	
		
		imgHeight = screenWidth / 2.8 + "px";
		tdWidth = screenWidth / 4.2 + "px";
		divWidth = screenWidth / 3.3 + "px";
		divHeight = screenWidth / 2.2 + "px";
	}
	else if(toPhp==="900")
	{
		divWidth = winW * 0.4;
		
		if(divWidth>480){ screenWidth = 480;}
		else if(divWidth<=480 && divWidth>=340){screenWidth=divWidth;}
		else if(divWidth<340){ screenWidth = 340;}	
		
		imgHeight = screenWidth / 2.9 + "px";
		tdWidth = screenWidth / 4.3 + "px";
		divWidth = screenWidth / 3.4 + "px";
		divHeight = screenWidth / 2.2 + "px";
	}
	else if(toPhp==="1050")
	{
		divWidth = winW * 0.4;
		
		if(divWidth>600){ screenWidth = 600;}
		else if(divWidth<=600 && divWidth>=390){screenWidth=divWidth;}
		else if(divWidth<390){ screenWidth = 390;}	
		
		imgHeight = screenWidth / 3.2 + "px";
		tdWidth = screenWidth / 4.5 + "px";
		divWidth = screenWidth / 3.5 + "px";
		divHeight = screenWidth / 2.5 + "px";
	}
	
	$(".Tuote").css({"width" : tdWidth,"height" : imgHeight});
	$(".imageTuotekuva").css({"max-width" : tdWidth});
	$(".content_tuotteet").css({"min-width" : tdWidth,"max-width" : tdWidth});
	$(".tuotteetDIV").css({"height" : divHeight,"min-width" : divWidth,"max-width" : divWidth});
	
	var leftDiv=winW * 0.4;
	var rightDiv = winW * 0.6;
	
	if(toPhp==="600"){leftDiv = winW * 0.25;rightDiv = winW * 0.75;if(rightDiv<560){rightDiv=560;leftDiv=winW-rightDiv;vertailuLeft=90;}}
	else if(toPhp==="648"){if(rightDiv<640){rightDiv=640;leftDiv=winW-rightDiv;vertailuLeft=110;}}
	else if(toPhp==="720"){leftDiv = winW * 0.35;rightDiv = winW * 0.65;if(rightDiv<770){rightDiv=770;leftDiv=winW-rightDiv;vertailuLeft=130;}}
	else if(toPhp==="768"){leftDiv = winW * 0.30;rightDiv = winW * 0.70;if(rightDiv<680){rightDiv=680;leftDiv=winW-rightDiv;vertailuLeft=90;}}
	else if(toPhp==="800"){leftDiv = winW * 0.35;rightDiv = winW * 0.65;if(rightDiv<770){rightDiv=770;leftDiv=winW-rightDiv;vertailuLeft=130;}}
	else if(toPhp==="900"){if(rightDiv<795){rightDiv=795;leftDiv=winW-rightDiv;vertailuLeft=120;}}
	else if(toPhp==="1024"){leftDiv = winW * 0.35;rightDiv = winW * 0.65;if(rightDiv<780){rightDiv=780;leftDiv=winW-rightDiv;vertailuLeft=140;}}
	else if(toPhp==="1050"){if(rightDiv<900){rightDiv=900;leftDiv=winW-rightDiv;vertailuLeft=150;}}
	
	if(leftDiv<vertailuLeft){
		leftDiv=vertailuLeft;
		var apu = leftDiv+1;
		var right2 = winW-apu;
		
		$("#holder").width(leftDiv+"px");
		$("#leftDiv").width(leftDiv+"px");

		if($("#layoutMainPicture").length > 0){
			
			$("#layoutMainPicture").css("min-width",right2*0.65-1+"px");
			$("#layoutFigurePicture").width(right2*0.35-1+"px");
		}
		
		
		$("#rightDiv").width(right2+"px");
		rightApu = right2;

	}
	else{
		
		$("#holder").width(leftDiv+"px");
		$("#leftDiv").width(leftDiv+"px");		
		$("#rightDiv").width(rightDiv+"px");
		rightApu = rightDiv;
		
	}
	
	$("#layout_div").width(rightDiv+"px");
	
}

function showProduct(t1,sex,p1,product,tila){

	var leftDiv,string,products,i;
	sex1 = sex;
	product1 = product;
	figureClear();
	
	if($("#holder").length == 0 && tila==="")
	{
		 postPhp("phpIncludes/change_session.php","productView=1&sex="+sex+"&product="+product);
		 page($("#page").val(),'','update');
		 tila="auto";
	}
	
	$.ajax({
		type: "POST",
 	 	url: "phpIncludes/print_products.php",
		data: "sex="+sex+"&product="+product,
  		async: true,
		success: function(data){
			
			leftDiv = '<div class="left holder" id="holder"><div id="pane4" class="scroll-pane">';
			
			string = data.split("x2x");
			products = string[0].split("x3x");
			
			if(products.length===1){
			
				leftDiv = leftDiv + products[0];	
				
			}
			else{
			
				for(i=0;i<products.length-1;i++){
				
					leftDiv = leftDiv + '<div class="tuotteetDIV"><table class="layout_tuotteet"><tr><td class="layout_tuotteet"><table class="main_tuotteet"><tr><td><table class="box_tuotteet"><tr><td class="tuotteetTopBar"><div><img src="image/FormTop.png" alt="" /></div></td></tr><tr><td class="content_tuotteet">';
					leftDiv = leftDiv + products[i];
					leftDiv = leftDiv + '</div></td></tr><tr><td class="tuotteetBottomBar"><div><img src="image/FormBottom.png" alt="" /></div></td></tr></table></td></tr></table></td></tr></table></div>';
					
				}
				
			}
			
			leftDiv = leftDiv + '</div></div>';
			
			ab("leftDiv",leftDiv);
			
			scrollbar(tila,parseInt(string[1]));
			$('#pane4').jScrollPane({scrollbarOnLeft:true, showArrows:true, scrollbarWidth: 17});	
			
		}
 	});

}

function next(kulma,kulmat,material,seq)
{
	zoomi=0;
	
	if($("#"+material.toUpperCase()+seq).length > 0)
		kulmat = $("#"+material.toUpperCase()+seq).val();	
	
	var strLeft = parseInt(kulmat.charAt(0));
	var strFront = parseInt(kulmat.charAt(3));
	var strBack = parseInt(kulmat.charAt(2));
	var strRight = parseInt(kulmat.charAt(1));
	var kulmaa="",kulmanext="";
	
	if(kulma=="L" && strLeft==1){ kulmaa = "L"; }
	else if(kulma=="F" && strFront==1){ kulmaa = "F"; }
	else if(kulma=="B" && strBack==1){ kulmaa = "B"; }
	else if(kulma=="R" && strRight==1){ kulmaa = "R"; }
	else{
		if(kulma=="L"){
			if(strFront==1){ kulmaa="F"; }
			else if(strBack==1){ kulmaa="B"; }
			else if(strRight==1){ kulmaa="R"; }
		}
		else if(kulma=="F"){
			if(strBack==1){ kulmaa="B"; }
			else if(strRight==1){ kulmaa="R"; }
			else if(strLeft==1){ kulmaa="L"; }
		}
		else if(kulma=="B"){
			if(strRight==1){ kulmaa="R"; }
			else if(strLeft==1){ kulmaa="L"; }
			else if(strFront==1){ kulmaa="F"; }
		}
		else if(kulma=="R"){
			if(strLeft==1){ kulmaa="L"; }
			else if(strFront==1){ kulmaa="F"; }
			else if(strBack==1){ kulmaa="B"; }
		}
	}
	
	if(kulmaa=="L"){
	
		if(strFront==1){ kulmanext="F"; }
		else if(strBack==1){ kulmanext="B"; }
		else if(strRight==1){ kulmanext="R"; }
		else{ kulmanext="L"; }
		
	}
	else if(kulmaa=="F"){
	
		if(strBack==1){ kulmanext="B"; }
		else if(strRight==1){ kulmanext="R"; }
		else if(strLeft==1){ kulmanext="L"; }
		else{ kulmanext="F"; }
		
	}
	else if(kulmaa=="B"){
	
		if(strRight==1){ kulmanext="R"; }
		else if(strLeft==1){ kulmanext="L"; }
		else if(strFront==1){ kulmanext="F"; }
		else{ kulmanext="B"; }
		
	}	
	else if(kulmaa=="R"){
	
		if(strLeft==1){ kulmanext="L"; }
		else if(strFront==1){ kulmanext="F"; }
		else if(strBack==1){ kulmanext="B"; }
		else{ kulmanext="R"; }
		
	}		
	
	return Array(kulmaa,kulmanext);
}

function turnAvailability(kulmat)
{
	var sum = kulmat.charAt(0)+kulmat.charAt(1)+kulmat.charAt(2)+kulmat.charAt(3);
	if(sum>1){ 
		return true; 
	}
	else{ 
		return false; 
	}
}

function colorAvailability(serial,mater,tila)
{
	var apu = $("#colors").val();

	if(tila!="auto")
		colors1 = apu;
		
	var varit=apu.split(",");
	
	if(varit.length>1) 
		return true; 
	else
		return false; 
}

function selectColor(serial,mater,seq,kulma,kulmat,pictureName)
{
	zoomi=0;
	uniti = 0;
	
	var apuKulma = next(kulma,kulmat,mater,seq);
	
	$('#tuotekuva_iso').attr('src',"../image/products/"+serial+"/"+pictureName+".png");
	$('#idTurn').attr('href',"javascript:turn(\""+serial+"\",\""+mater+"\",\""+seq+"\",\""+apuKulma[1]+"\",\""+kulmat+"\",\""+pictureName+"\");");
	$('#idColor').attr('href',"javascript:color(\""+serial+"\",\""+mater+"\",\""+seq+"\",\""+kulma+"\",\""+kulmat+"\",\""+pictureName+"\");");
	$('#idZoom').attr('href',"javascript:zoom(\""+mater+seq+"\");");
	seq1=seq;
	
	aPhp("phpIncludes/change_session.php","changeColor=true&now="+seq+"&picture="+pictureName);			
}

function productColors(tila,material,pictureName){
	
	if($("#productColorChecked").val()!==tila){
		remove = "color"+$("#productColorChecked").val();
		vali = "radioColors"+tila;
		vali2 = "color"+tila;
		$("#productColorChecked").val(tila);
		$("#productColorChecked").attr("title",material+tila);
		
		//add
		$("#"+vali2).css({"border-bottom-color":"#FFF" , "border-right-color":"#FFF" , "border-top-color":"#000" , "border-left-color":"#000"});
		
		//remove
		$("#"+remove).css({"border-bottom-color":"#000" , "border-right-color":"#000" , "border-top-color":"#FFF" , "border-left-color":"#FFF"});
		
	}
	
	if(pictureName===""){
		
		var apu2 = pictureNameGlobal.split("-");
		
		for(y=0;y<apu2.length;y++){
			if(pictureName==="")
				pictureName = apu2[y];
			else if(y==3)
				pictureName = pictureName + "-" + material+tila;			
			else
				pictureName = pictureName + "-" + apu2[y];
		}		
			
	}
	
	selectColor(ser1,mat1,tila,nay1,kul1,pictureName);
}

function paivitaOstoskoriDiv(tila){
	
	var ostoskoriNakyvissa = $("#ostoskoriNakyvissa").val();
	
	if(ostoskoriNakyvissa!=="" || tila=="new"){

		var help = postPhp("phpIncludes/ostoskori.php","action=count");
		if(help!="0"){
			ab("ostoskoriDiv",'<img alt="" onclick="page(17,\'\',\'\')" style="cursor:pointer" src="image/Basket'+help+'.png" />');
		}
		else{
			ab("ostoskoriDiv",'<img alt="" onclick="page(17,\'\',\'\');piilotaOstoskori();" style="cursor:pointer" src="image/Basket'+help+'.png" />');
		}
	}
	
	if(ostoskoriNakyvissa==="" && tila=="new" || ostoskoriNakyvissa=="true" && tila===""){
		
		if(ostoskoriNakyvissa==="")
			$("#ostoskoriNakyvissa").val("true");
		
		if(toPhp==600)
			$("#ostoskoriDiv").height("45px");
		else if(toPhp==648 || toPhp==768)
			$("#ostoskoriDiv").height("52px");
		else if(toPhp==720 || toPhp==800)
			$("#ostoskoriDiv").height("58px");
		else if(toPhp==1024 || toPhp==900)
			$("#ostoskoriDiv").height("63px");				
		else
			$("#ostoskoriDiv").height("70px");

	}
}

function naytaTuotekuva(serial,mater,seq,kulm,nayta,tila,unity,pictureName,offers,productid,returnid){
	var leveys,apu,leveys2;
	
	figureClear();
	kulmat = kulm;
	
	if(!$("#idButtons").length>0)
		ab("rightDiv",'<div id="test_right_div"><div id="tuotekuva" class="tuotekuva"><img id="tuotekuva_iso" class="tuotekuva_iso" src="image/Empty.png" alt="" /><div id="materiaaliZoom" class="materiaaliZoom"></div></div><div id="tuotekuvaus" class="tuotekuvaus"><table id="layout_tuotekuvaus"><tr><td class="layout"><table id="mainTuotekuvaus"><tr><td><form name="productForm" action=""><table class="box_tuotekuvaus"><caption class="header"><span><i id="idHeader1"></i><b><i id="idHeader2"></i></b></span></caption><tr><td class="topBar"><div><img src="image/FormTop.png" alt="" /></div></td></tr><tr><td class="content_tuotekuvaus" id="content_tuotekuvaus"></td></tr><tr><td class="bottomBar"><div><img src="image/FormBottom.png" alt="" /><center><div id="idButtons"></div></center></div></td></tr></table></form></td></tr></table></td></tr></table></div></div>');
	
	$.ajax({
		type: "POST",
 	 	url: "phpIncludes/productInformation.php",
		data: "x2x="+serial+"&offers="+offers+"&id="+productid+"&returnid="+returnid,
  		async: true,
		success: function(data){

			var descs = data.split("x92x");
			
			$("#content_tuotekuvaus").html(descs[1]);
			$("#idHeader1").text(descs[0]);
			$("#idHeader2").text(descs[0]);			
			$("#idButtons").html(descs[2]);	
			
			var nextKulma = next(nayta,kulm,mater,seq);
			
			pictureNameGlobal = pictureName;	
			
			ser1 = serial;
			mat1 = mater;
			seq1 = seq;
			kul1 = kulmat;
			nay1 = nayta;
			uniti = 0;
			
			$("#layout_tuotekuvaus").width($("#rightDiv").width(leveys));
			
			if(tila!="auto"){
				apu = $("#n"+serial+mater).val();
				$("#colors").val(apu);
			}
			else{
				apu = $("#colors").val();
			}
			
			var pictureMenuWidth = 0;
			
			if(toPhp=="600" || toPhp=="648" || toPhp=="720" || toPhp=="768"){
				leveys="36px";leveys2=36;pictureMenuWidth=28;
			}
			else{
				leveys="50px";leveys2=50;pictureMenuWidth=37;
			}
		
			var windowwidth = wWidth;
			
			if(toPhp=="600")
				windowwidth = windowwidth*0.31;
			else if(toPhp=="648")
				windowwidth = windowwidth*0.40;
			else if(toPhp=="720")
				windowwidth = windowwidth*0.38;
			else if(toPhp=="768")
				windowwidth = windowwidth*0.34;
			else if(toPhp=="800")
				windowwidth = windowwidth*0.38;
			else if(toPhp=="900")
				windowwidth = windowwidth*0.41;
			else if(toPhp=="1024")
				windowwidth = windowwidth*0.38;	
			else if(toPhp=="1050")
				windowwidth = windowwidth*0.42;	
		
			if(turnAvailability(kulm)===true){
				
				$("#jqpopup_turn").width(leveys);
				pictureMenuWidth = pictureMenuWidth + leveys2;
				
			}
			else{
				$("#jqpopup_turn").width("0px");
			}	
			
			if(colorAvailability(serial,mater,tila)===true){
		
				$("#jqpopup_color").width(leveys);
				pictureMenuWidth = pictureMenuWidth + leveys2;
				
			}
			else{
				$("#jqpopup_color").width("0px");
			}
			
			if(unity=="1"){
				
				$("#jqpopup_unit").css({"width":leveys,"display":"none"});
				pictureMenuWidth = pictureMenuWidth + leveys2;
				
			}
			else{
				$("#jqpopup_unit").css({"width":"0px","display":"none"});
			}
			
			$('#idZoom').attr('href',"javascript:zoom(\""+mater+seq+"\");");
			$("#jqpopup_zoom").width(leveys);
			pictureMenuWidth = pictureMenuWidth + leveys2;
		
			$("#pictureMenu").val("true");
			 
			if(tila!="auto")
				aPhp("phpIncludes/change_session.php","show=true&serial="+serial+"&material="+mater+"&seq="+seq+"&kulma="+kulm+"&nayta="+nayta+"&tila="+tila+"&color=n"+serial+mater+"&colors="+apu+"&unity="+unity+"&picture="+pictureName+"&offers="+offers+"&id="+productid+"&returnid="+returnid);
			 
			productColors(seq1,mat1,pictureName);
			paivitaOstoskoriDiv("");
			
			$("#draggable").css({ "position" : "absolute" , "width" : pictureMenuWidth+"px" , "left" : windowwidth+"px" , "display" : "block"});
	
			}
 	});	
	
}

function hover(id)
{
	$("#"+id).attr("src","image/SwitchProd-Down.png");
}

function klick(id)
{
	$("#"+id).attr("src","image/SwitchProd-Klik.png");
}

function out(id)
{
	$("#"+id).attr("src","image/SwitchProd-Empty.png");
}

function zoom(material)
{
	if(zoomi===0){
		ab("materiaaliZoom","<div class=zoomText><p>"+material+"</p></div><img class='imgZoom' src='../image/materials/"+material+".png' alt='' />");
		zoomi=1;
	}
	else{
		$('#materiaaliZoom').html("");
		zoomi=0;
	}
}

function unitp(serial)
{
	if(uniti===0){
		uniti_save = [$("#tuotekuva_iso").attr("src"),$("#jqpopup_zoom").html()];
		$("#tuotekuva_iso").attr("src","../image/products/" + serial + "/Unit.png");
		uniti=1;
		
		if(zoomi==1)
			zoom('');
			
		ab("jqpopup_zoom","<img src='image/SwitchProd-Zoom.png' alt='' />");
	}
	else{
		$("#tuotekuva_iso").attr("src",uniti_save[0]);
		ab("jqpopup_zoom",uniti_save[1]);
		uniti=0;
	}
}

function turn(serial,mater,seq,kulma,kulmat,picture)
{
	var nextKulma = next(kulma,kulmat,mater,seq);
	uniti = 0;
	
	var apu = picture.split("-");
	var pictureName = "";
	
	for(i=0;i<apu.length-1;i++){
		if(pictureName==="")
			pictureName = apu[i];
		else
			pictureName = pictureName + "-" + apu[i];
	}
	
	pictureName = pictureName + "-" + nextKulma[0];
	
	pictureNameGlobal = pictureName;
		
	$('#tuotekuva_iso').attr('src',"../image/products/"+serial+"/"+pictureName+".png");		
	$('#idTurn').attr('href',"javascript:turn(\""+serial+"\",\""+mater+"\",\""+seq+"\",\""+nextKulma[1]+"\",\""+kulmat+"\",\""+pictureName+"\");");
	$('#idColor').attr('href',"javascript:color(\""+serial+"\",\""+mater+"\",\""+seq+"\",\""+nextKulma[0]+"\",\""+kulmat+"\",\""+pictureName+"\");");
	$('#idZoom').attr('href',"javascript:zoom(\""+mater+seq+"\");");
	
	nay1 = nextKulma[0];
	 
	aPhp("phpIncludes/change_session.php","changeTurn=true&now="+nextKulma[0]+"&picture="+pictureName);
	
}

function color(serial,mater,seq,kulma,kulmat,picture)
{
	zoomi=0;
	uniti = 0;
	
	if(kulma=="L") { apuKulma="F"; }
	else if(kulma=="F") { apuKulma="R"; }
	else if(kulma=="R") { apuKulma="B"; }
	else if(kulma=="B") { apuKulma="L"; }
	
	var apu = $("#colors").val();
	var varit=apu.split(",");
	x = varit.length;
	
	for(i=0;i<x;i++)
	{
		if(varit[i]==seq)
		{
			if(i!=x-1) {i++; break;}
			else { i=0; break;}
				
		}
	}
	
	var apu2 = picture.split("-");
	var pictureName = "";
	
	for(y=0;y<apu2.length;y++){
		if(pictureName==="")
			pictureName = apu2[y];
		else if(y==3)
			pictureName = pictureName + "-" + mater+varit[i];			
		else
			pictureName = pictureName + "-" + apu2[y];
	}
	
	$('#tuotekuva_iso').attr('src',"../image/products/"+serial+"/"+pictureName+".png");
	$('#idTurn').attr('href',"javascript:turn(\""+serial+"\",\""+mater+"\",\""+varit[i]+"\",\""+apuKulma+"\",\""+kulmat+"\",\""+pictureName+"\");");
	$('#idColor').attr('href',"javascript:color(\""+serial+"\",\""+mater+"\",\""+varit[i]+"\",\""+kulma+"\",\""+kulmat+"\",\""+pictureName+"\");");
	$('#idZoom').attr('href',"javascript:zoom(\""+mater+varit[i]+"\");");

	seq1=varit[i];
	 
	aPhp("phpIncludes/change_session.php","changeColor=true&now="+varit[i]);		
}

function language(lang){
	
	if(lang=="FI")
		lang2="EN";
	else
		lang2="FI";
		
	$('#radio'+lang).attr("checked","checked");
	$('#flag'+lang).attr("class","flagSelected");
	$('#flag'+lang2).attr("class","flag");
	
}

function languageSet(lang){
	
	if(lang=="FI")
		lang2="EN";
	else
		lang2="FI";
	
	$('#flag'+lang).attr("class","flagSelected");
	$('#flag'+lang2).attr("class","flag");
	
	postPhp("phpIncludes/checkLanguage.php","changeLanguage=true&newLanguage="+lang);
	
	location.reload(true);
	
}

function saveCookie(kieli){
	
	if(sCookie==1)
		sCookie=0;
	else if(sCookie===0)
		sCookie=1;
	
	postPhp("phpIncludes/logmein.php","cookie=true&saveCookie="+sCookie+"&kieli="+kieli);	
	
}

function avaaMittatilaus(sex,form){
	
	if($("#tuoteKoko").val()=="MITTATILAUS")
	{
		
		$("#avaaMittatilausHideAll").fadeIn('fast',function(){ $("#avaaMittatilaus").css("display","block"); });
		//$("#avaaMittatilausHideAll").css("display","block");
		//$("#avaaMittatilaus").css("display","block");		
		
		$.ajax({
			type: "POST",
			url: "phpIncludes/mittatilaus.php",
			data: "sukupuoli="+sex+"&mitat="+form.saveMittatilaus.value+"&serial="+form.serial.value+"&resolution="+toPhp+"&offers="+form.offers.value+"&offerID="+form.offerID.value+"&offerReturnID="+form.offerReturnID.value,
			async: true,
			success: function(data){
			
				ab("avaaMittatilaus",data);
				
				if($('#figureMen').length > 0)
					selectFigure('howOrder','howOrder','','',true);
				
			}
		});	

	}
	
}

function tallennaMittatilaus(form,luku){
	var mitat,apu;
	
	for(var i=1;i<luku;i++){
		
		apu = "mittatilausmitta"+i;
		
		if(i==1)
			mitat = form.elements[apu].value;
		else
			mitat = mitat+","+form.elements[apu].value;
			
	}
	
	$("#saveMittatilaus").val(mitat);
	ab("avaaMittatilaus","");
	//$("#avaaMittatilausHideAll").css("display","none");
	$("#avaaMittatilaus").css("display","none");
	$("#avaaMittatilausHideAll").fadeOut('fast');
			
}

function suljeMittatilaus(form){
	
	if($("#saveMittatilaus").val()==="")
		$("#tuoteKoko").val("");
		
	ab("avaaMittatilaus","");
	//$("#avaaMittatilausHideAll").css("display","none");
	$("#avaaMittatilaus").css("display","none");	
	$("#avaaMittatilausHideAll").fadeOut('fast');
}

function avaaMittatilausSivu(sukupuoli){

	if(sukupuoli=="w")
		page(6,'','');
	else if(sukupuoli=="m")
		page(5,'','');
	
	ab("avaaMittatilaus","");
	$("#avaaMittatilausHideAll").css("display","none");
	$("#avaaMittatilaus").css("display","none");	
}

function saveMitat(){
	
	var tLength = $("#mittatilausLength").val();
	var tBreastHigh = $("#mittatilausBreastHigh").val();
	var tArmLength = $("#mittatilausArmLength").val();
	var tChestMeasurement = $("#mittatilausChestMeasurement").val();
	var tWaistline = $("#mittatilausWaistline").val();
	var tHipGirth = $("#mittatilausHipGirth").val();
	var tThighMeasurement = $("#mittatilausThighMeasurement").val();
	var tOutsideLegLength = $("#mittatilausOutsideLegLength").val();
	var tSex = $("#mittatilausSex").val();
	
	postPhp("phpIncludes/tiedot.php","saveMittatilausVartalo=true&length="+tLength+"&breastHigh="+tBreastHigh+"&armLength="+tArmLength+"&chestMeasurement="+tChestMeasurement+"&waistline="+tWaistline+"&hipGirth="+tHipGirth+"&thighMeasurement="+tThighMeasurement+"&outsideLegLength="+tOutsideLegLength+"&sex="+tSex);
	
	$.ajax({
		type: "POST",
		url: "phpIncludes/mittatilaus.php",
		data: "sukupuoli="+$("#mSex").val()+"&mitat=&serial="+$("#mSerial").val()+"&resolution="+toPhp+"&offers="+$("#mOffers").val(),
		async: true,
		success: function(data){
		
			ab("avaaMittatilaus",data);
			
		}
	});	
	
}

function openWindow(){
	 window.open(window.location.href,'','scrollbars=no,directories=no,menubar=no,height='+height+',width='+width+',resizable=yes,toolbar=no,location=no,status=no');
}

function avaaLisaohjeita(){	

	ab("avaaMittatilaus",postPhp("howorder_text.php","lisaohjeet=true&resolution="+toPhp));									
	
	selectFigure('howOrder',"howOrder",'','',true);	
	$("#avaaMittatilausHideAll").fadeIn('fast',function(){ $("#avaaMittatilaus").css("display","block"); });
	//$("#avaaMittatilausHideAll").css("display","block");
	//$("#avaaMittatilaus").css("display","block");	

}

function suljeHowOrder(){
	
	ab("avaaMittatilaus","");
	//$("#avaaMittatilausHideAll").css("display","none");
	$("#avaaMittatilaus").css("display","none");
	$("#avaaMittatilausHideAll").fadeOut('fast');

}

function ostoskoriLaskeYhteen(plusToimitus){
	var summa,toimitus,yhteensa,lahjakortti;
	
	if($("#toimituskulut").length > 0){
		
		if(plusToimitus===true){
			if($("#kotipaketti").attr("checked")){
				if($("#pikatoimitus").attr("checked")){
					yhteensa = $("#ostoksetYhteensa").val();
					yhteensa = parseFloat(yhteensa.replace(",","."));
					yhteensa = yhteensa * 0.25;
					yhteensa = yhteensa + 15;
					yhteensa = yhteensa.toFixed(2);
					$('#toimituskulut').text(yhteensa.replace(".",","));
					$('#toimituskulutHidden').val(yhteensa);
				}
				else{
					$('#toimituskulut').text("15,00");
					$('#toimituskulutHidden').val("15.00");
				}
			}
			else if($("#pikatoimitus").attr("checked")){
				if($("#kotipaketti").attr("checked")){
					yhteensa = $("#ostoksetYhteensa").val();
					yhteensa = parseFloat(yhteensa.replace(",","."));
					yhteensa = yhteensa * 0.25;
					yhteensa = yhteensa + 15;
					yhteensa = yhteensa.toFixed(2);
					$('#toimituskulut').text(yhteensa.replace(".",","));
					$('#toimituskulutHidden').val(yhteensa);
				}
				else{
					yhteensa = $("#ostoksetYhteensa").val();
					yhteensa = parseFloat(yhteensa.replace(",","."));
					yhteensa = yhteensa * 0.25;
					yhteensa = yhteensa + 5;
					yhteensa = yhteensa.toFixed(2);
					$('#toimituskulut').text(yhteensa.replace(".",","));
					$('#toimituskulutHidden').val(yhteensa);
				}
			}
		}
		
		toimitus = $("#toimituskulut").html();
		yhteensa = $("#ostoksetYhteensa").val();
				
		toimitus = parseFloat(toimitus.replace(",","."));
		yhteensa = parseFloat(yhteensa.replace(",","."));		
		
		summa = toimitus + yhteensa;
		
		if($("#lahjakorttiHidden").length > 0){
			
			lahjakortti = $("#lahjakorttiHidden").val();
			lahjakortti = parseFloat(lahjakortti.replace(",","."));
			summa = summa - lahjakortti;
			summa = (summa<0) ? 0 : summa;
			
		}
		
		summa = summa.toFixed(2);
		
		$('#yhteensa').text(summa.replace(".",","));
		
	}
}

function ostoskoriMuutaMaara(tuotekoodi,luku,form){
	
	value = form.elements["ostoskoriMaara"+luku].value;
	
	$.ajax({
		type: "POST",
		url: "phpIncludes/ostoskori.php",
		data: "action=maara&tuotekoodi="+tuotekoodi+"&kpl="+value,
		async: true,
		success: function(){
		
			page(17,'','');
			
		}
	});		
	
}

function ostoskoriPoista(tuotekoodi){
	
	$.ajax({
		type: "POST",
		url: "phpIncludes/ostoskori.php",
		data: "action=delete&tuotekoodi="+tuotekoodi,
		async: true,
		success: function(){
		
			page(17,'','');
			paivitaOstoskoriDiv("");
			
		}
	});
	
}

function ostoskoriToimitus(toimitus,form){
	var value,yhteensa,tTapa,toimitusHidden;
	
	if(toimitus=="kotipaketti"){
		value = form.elements.kotipaketti.checked;
		if(value===true){
			if(form.elements.pikatoimitus.checked===true){
				yhteensa = $("#ostoksetYhteensa").val();
				yhteensa = parseFloat(yhteensa.replace(",","."));
				yhteensa = yhteensa * 0.25;
				yhteensa = yhteensa + 15;
				yhteensa = yhteensa.toFixed(2);
				$('#toimituskulut').text(yhteensa.replace(".",","));
				toimitusHidden = yhteensa;
			}
			else{
				$('#toimituskulut').text("15,00");
				toimitusHidden = 15.00;
			}
		}
		else{
			if(form.elements.pikatoimitus.checked===true){
				yhteensa = $("#ostoksetYhteensa").val();
				yhteensa = parseFloat(yhteensa.replace(",","."));
				yhteensa = yhteensa * 0.25;
				yhteensa = yhteensa + 5;
				yhteensa = yhteensa.toFixed(2);
				$('#toimituskulut').text(yhteensa.replace(".",","));
				toimitusHidden = yhteensa;
			}
			else{
				$('#toimituskulut').text("5,00");
				toimitusHidden = 5.00;
			}
		}
	}
	else{
		value = form.elements.pikatoimitus.checked;
		if(value===true){
			if(form.elements.kotipaketti.checked===true){
				yhteensa = $("#ostoksetYhteensa").val();
				yhteensa = parseFloat(yhteensa.replace(",","."));
				yhteensa = yhteensa * 0.25;
				yhteensa = yhteensa + 15;
				yhteensa = yhteensa.toFixed(2);
				$('#toimituskulut').text(yhteensa.replace(".",","));
				toimitusHidden = yhteensa;
			}
			else{
				yhteensa = $("#ostoksetYhteensa").val();
				yhteensa = parseFloat(yhteensa.replace(",","."));				
				yhteensa = yhteensa * 0.25;
				yhteensa = yhteensa + 5;
				yhteensa = yhteensa.toFixed(2);
				$('#toimituskulut').text(yhteensa.replace(".",","));
				toimitusHidden = yhteensa;
			}
		}
		else{
			if(form.elements.kotipaketti.checked===true){
				$('#toimituskulut').text("15,00");
				toimitusHidden = 15.00;
			}
			else{
				$('#toimituskulut').text("5,00");
				toimitusHidden = 5.00;
			}	
		}
	}
	
	$('#toimituskulutHidden').val(toimitusHidden);
	
	if(form.kotipaketti.checked===true){if(form.pikatoimitus.checked===true){tTapa="molemmat";}else{tTapa="koti";}}
	else if(form.pikatoimitus.checked===true){if(form.kotipaketti.checked===true){tTapa="molemmat";}else{tTapa="pika";}}

	postPhp("phpIncludes/ostoskori.php","action=delivery&toimitustapa="+tTapa);
	ostoskoriLaskeYhteen(false);
}

function tyhjennaOstoskori(){
	postPhp("phpIncludes/ostoskori.php","action=clean");
	page(17,'','');
}

function tuoteOstoskoriin(form,message){
	var tuotekoodi,kpl;
	
	if(form.tuoteKoko.value!=="" && form.tuoteMaara.value!==""){
		
		if(form.tuoteKoko.value=="MITTATILAUS" && form.saveMittatilaus.value===""){
			alert('Ennen tuotteen siirtoa ostoskoriin sinun tulee antaa tuotteen valmistukseen tarvittavat mittasi.');
		}
		else{
			if(form.tuoteKoko.value=="MITTATILAUS"){
				tuotekoodi = form.serial.value+"_"+form.otsikko.value+"_"+form.productColorChecked.title+"_"+form.tuoteKoko.value+"_"+form.hinta.value+"_"+form.saveMittatilaus.value+"_"+form.sukupuoli.value;
			}
			else{
				tuotekoodi = form.serial.value+"_"+form.otsikko.value+"_"+form.productColorChecked.title+"_"+form.tuoteKoko.value+"_"+form.hinta.value;
			}
			kpl = form.tuoteMaara.value;
		
			postPhp("phpIncludes/ostoskori.php","action=add&tuotekoodi="+tuotekoodi+"&kpl="+kpl);
		}
		paivitaOstoskoriDiv("new");
	}
	else{
		alert(message);
	}
}

function tuoteOfferOstoskoriin(form,message,type,id,returnid,message2){
	var tuotekoodi,kpl;
	
	if(postPhp("phpIncludes/ostoskori.php","action=offerStatus&id="+id+"&returnid="+returnid) != '1') {
		
		alert(message2);
	
	}
	else {

		if(form.tuoteKoko.value!=="" && form.tuoteMaara.value!==""){
			
			if(form.tuoteKoko.value=="MITTATILAUS" && form.saveMittatilaus.value===""){
				alert('Ennen tuotteen siirtoa ostoskoriin sinun tulee antaa tuotteen valmistukseen tarvittavat mittasi.');
			}
			else{
				if(form.tuoteKoko.value=="MITTATILAUS"){
					tuotekoodi = form.serial.value+"_"+form.otsikko.value+"_"+form.productColorChecked.title+"_"+form.tuoteKoko.value+"_"+form.hinta.value+"_"+form.saveMittatilaus.value+"_"+form.sukupuoli.value;
				}
				else{
					tuotekoodi = form.serial.value+"_"+form.otsikko.value+"_"+form.productColorChecked.title+"_"+form.tuoteKoko.value+"_"+form.hinta.value+"_1_w";
				}
				kpl = form.tuoteMaara.value;
			
				postPhp("phpIncludes/ostoskori.php","action=offer&tuotekoodi="+tuotekoodi+"&kpl="+kpl+"&id="+id+"&returnid="+returnid);
			}
			paivitaOstoskoriDiv("new");
			
			if(type=='tilataan')
				page(17,'','');
			
		}
		else{
			alert(message);
		}
	
	}

}

function piilotaOstoskori(){
	
	$("#ostoskoriDiv").height("0px");
	$("#ostoskoriNakyvissa").val("");
	aPhp("phpIncludes/ostoskori.php","action=hide");	
	
}

function ostoskoriMittatilaus(serial,mitat,tuotekoodi,sex,offer,offerID,offerReturnID){
	
	$("#avaaMittatilausHideAll").fadeIn('fast',function(){ $("#avaaMittatilaus").css("display","block"); });
	
	$.ajax({
		type: "POST",
		url: "phpIncludes/mittatilausOstoskori.php",
		data: "sukupuoli="+sex+"&mitat="+mitat+"&resolution="+toPhp+"&serial="+serial+"&tuotekoodi="+tuotekoodi+"&offer="+offer+"&offerID="+offerID+"&offerReturnID="+offerReturnID,
		async: true,
		success: function(data){
		
			ab("avaaMittatilaus",data);
			
		}
	});

}

function tallennaOstoskoriMittatilaus(form,luku){
	var mitat,apu;
	
	for(var i=1;i<luku;i++){
		
		apu = "mittatilausmitta"+i;
		
		if(i==1)
			mitat = form.elements[apu].value;
		else
			mitat = mitat+","+form.elements[apu].value;
			
	}

	postPhp("phpIncludes/ostoskori.php","action=change&tuotekoodiOld="+form.tuotekoodi.value+"&tuotekoodi="+mitat);
	page(17,'','');
	ab("avaaMittatilaus","");
	
	$("#avaaMittatilaus").css("display","none");
	$("#avaaMittatilausHideAll").fadeOut('fast');	

}

function suljeOstoskoriMittatilaus(form){
	
	ab("avaaMittatilaus","");
	$("#avaaMittatilaus").css("display","none");
	$("#avaaMittatilausHideAll").fadeOut('fast');	
	
}

function avaaOstoskoriKuva(tuotekoodi){	

	popUp = $("#avaaMittatilaus").height();
	
	$("#avaaMittatilausHideAll").fadeIn('fast',function(){ $("#avaaMittatilaus").css({"display":"block","height":"98%"}); });

	$.ajax({
		type: "POST",
		url: "phpIncludes/ostoskoriKuva.php",
		data: "tuotekoodi="+tuotekoodi,
		async: true,
		success: function(data){
		
			ab("avaaMittatilaus",data);
			
		}
	});											

}

function suljeOstoskoriKuva(){
	
	ab("avaaMittatilaus","");
	$("#avaaMittatilaus").css("display","none");
	$("#avaaMittatilausHideAll").fadeOut('fast');
	$("#avaaMittatilaus").height(popUp);	
	
}

function ostoskoriJatka(form){
	
	if(form.ehdot.checked===true){
		
		$.ajax({
			type: "POST",
			url: "phpIncludes/ostoskori.php",
			data: "action=deliveryCost&toimituskulut="+form.toimituskulutHidden.value+"&yhteensa="+form.ostoksetYhteensa.value,
			async: true,
			success: function(){
			
				page(39,'','','','');
				
			}
		});	
		
	}
	else{
		
		if($("#ostoskoriEhdot").length > 0){
			setTimeout(function(){$("#ostoskoriEhdot").css("color","#F00");}, 200);
			setTimeout(function(){$("#ostoskoriEhdot").css("color","#000");}, 500);
			setTimeout(function(){$("#ostoskoriEhdot").css("color","#F00");}, 800);
			setTimeout(function(){$("#ostoskoriEhdot").css("color","#000");}, 1100);			
		}
		
	}
}

function ostoskoriAddressJatka(kohde,form,loggedin){
	
	if(kohde=="ostoskori"){
		
		page(17,'','');
		
	}
	else{
		
		var returnBoolean = true;
		
		if(form.firstName.value==="")
			returnBoolean = false;
		else if(form.lastName.value==="")
			returnBoolean = false;	
		else if(form.streetAddress.value==="")
			returnBoolean = false;
		else if(form.postalCode.value==="")
			returnBoolean = false;	
		else if(form.city.value==="")
			returnBoolean = false;
		else if(form.countryIso.value==="")
			returnBoolean = false;	
		else if(form.phonenumber.value==="")
			returnBoolean = false;
		else if(!checkEmail(form.email.value))
			returnBoolean = false;																					
		
		if(returnBoolean){
			
			if(loggedin){
				postPhp("phpIncludes/ostoskori.php","action=address&nimi="+form.firstName.value+"&sukunimi="+form.lastName.value+"&katu="+form.streetAddress.value+"&pnumero="+form.postalCode.value+"&kaupunki="+form.city.value+"&maa="+form.countryIso.value+"&puhelin="+form.phonenumber.value+"&email="+form.email.value+"&samaOsoite="+form.ostoskoriAddress.checked+"&kieli="+form.language.value);	
			}
			else{
				postPhp("phpIncludes/ostoskori.php","action=address&nimi="+form.firstName.value+"&sukunimi="+form.lastName.value+"&katu="+form.streetAddress.value+"&pnumero="+form.postalCode.value+"&kaupunki="+form.city.value+"&maa="+form.countryIso.value+"&puhelin="+form.phonenumber.value+"&email="+form.email.value+"&kieli="+form.language.value);
			}	
			
			if(kohde == 'maksutavat') {		
				page(40,'','','true','');
			}
			else if(kohde == 'tilaa') {
				postPhp("phpIncludes/ostoskoriTilaus.php","");
				location.reload(true);
			}
			
		}
		else {
			
			figureSite("addressPerson");
			
		}
		
	}
	
}

function ostoskoriAddressCheckbox(form){
	
	if(form.ostoskoriAddress.checked===false){
		$("#firstName").val("");
		$("#lastName").val("");
		$("#streetAddress").val("");
		$("#postalCode").val("");
		$("#city").val("");
		$("#countryIso").val("");
		$("#phonenumber").val("");
		$("#email").val("");
	}
	else{
		postPhp("phpIncludes/ostoskori.php","action=samaOsoite");
		page(39,'','');
	}
	
}

function tuoteTilataan(form,message){
	var tuotekoodi,kpl;
	
	if(form.tuoteKoko.value!=="" && form.tuoteMaara.value!==""){
		
		if(form.tuoteKoko.value=="MITTATILAUS" && form.saveMittatilaus.value===""){
			alert('Ennen tuotteen siirtoa ostoskoriin sinun tulee antaa tuotteen valmistukseen tarvittavat mittasi.');
		}
		else{
			if(form.tuoteKoko.value=="MITTATILAUS"){
				tuotekoodi = form.serial.value+"_"+form.otsikko.value+"_"+form.productColorChecked.title+"_"+form.tuoteKoko.value+"_"+form.hinta.value+"_"+form.saveMittatilaus.value+"_"+form.sukupuoli.value;
			}
			else{
				tuotekoodi = form.serial.value+"_"+form.otsikko.value+"_"+form.productColorChecked.title+"_"+form.tuoteKoko.value+"_"+form.hinta.value;
			}
			kpl = form.tuoteMaara.value;
			postPhp("phpIncludes/ostoskori.php","action=add&tuotekoodi="+tuotekoodi+"&kpl="+kpl);
		}
		paivitaOstoskoriDiv("new");
		page(17,'','');
	}
	else{
		alert(message);
	}	
}

function popUpDeliveryTerms(kohde){
	
	$("#figureTemp").html('');
	
	if(kohde == 'delivery_back' || kohde == 'return')
		hahmo = $("#figureMen").html();
	
	if(kohde == 'delivery' || kohde == 'delivery_back') {

		if(kohde == 'delivery')
			$("#avaaMittatilausHideAll").fadeIn('fast',function(){ $("#avaaMittatilaus").css("display","block"); });		
		
		$.ajax({
			type: "POST",
			url: "phpIncludes/deliveryterms.php",
			async: true,
			success: function(data){
			
				ab("avaaMittatilaus",data);
				
				if(kohde == 'delivery_back')
					selectFigure('howOrder',"popUp_delivery",hahmo,'',true);
				else
					selectFigure('howOrder',"popUp_delivery",'','',true);
				
			}
		});
	
	}
	else if(kohde == 'return') {
		
		$.ajax({
			type: "POST",
			url: "phpIncludes/return.php",
			async: true,
			success: function(data){
			
				ab("avaaMittatilaus",data);
				selectFigure('howOrder',"popUp_delivery",hahmo,'',true);
				
			}
		});
	
	}
	
}

function mittatilausTarkista(numero,message1,message2pieni,message2suuri,suoraleikkaus){
	var vali = $("#asiakasmitta"+numero).val();

	var minMax = vali.split(",");
	var asiakasmitta = minMax[2];
	var syotetty = parseInt($("#mittatilausmitta"+numero).val());
	
	if(syotetty<minMax[0]){
		$("#mittatilausmitta"+numero).val(asiakasmitta);
		
		if(suoraleikkaus == 1)
			copyMeasures('mittatilausmitta',numero);		
		
		$("#mittatilausText"+numero).css("color","#F00");
		alert(message1+syotetty+message2pieni+minMax[0]+".\n\n");
		$("#mittatilausText"+numero).css("color","#000");
	}
	else if(syotetty>minMax[1]){
		$("#mittatilausmitta"+numero).val(asiakasmitta);
		
		if(suoraleikkaus == 1)
			copyMeasures('mittatilausmitta',numero);
			
		$("#mittatilausText"+numero).css("color","#F00");
		alert(message1+syotetty+message2suuri+minMax[1]+".\n\n");
		$("#mittatilausText"+numero).css("color","#000");
	}
	else if(suoraleikkaus == 1) {
		
		copyMeasures('mittatilausmitta',numero);
		
	}
}

function tilauskantaan(){

	postPhp("phpIncludes/ostoskoriTilaus.php","");
	
}

function payment(id,tunnus,viite){

	if(postPhp("phpIncludes/ostoskoriTilaus.php","bankID="+id+"&tunnus="+tunnus+"&viite="+viite)==="true")
		return true;
	else
		return false;
	
}

function lahja(id,tunnus,viite){

	if(postPhp("phpIncludes/lahjakorttiTilaus.php","bankID="+id+"&tunnus="+tunnus+"&viite="+viite)==="true")
		return true;
	else
		return false;
	
}

function lahjakortit(nextPage,form,summa){
	var send,toimitustapa,error = false,pvm,klo,sex;
	
	if(nextPage==43){
		
		send = "action=summa&summa="+summa;
		
	}
	else if(nextPage==44){

		send = "action=onnittelu&kupla1="+form.kupla1.value;
		
	}
	else if(nextPage==45){
		
		var cEmail = form.toimitustapaE.checked;
		var cMail = form.toimitustapaM.checked;
		
		if(cEmail)
			toimitustapa = 1;
		else if(cMail)
			toimitustapa = 2;				
		
		send = "action=postitus&toimitustapa="+toimitustapa+"&yhteensa="+form.yhteensa.value;
	}
	else if(nextPage==46){
	
		pvm = form.pvmDay.value+"."+form.pvmMonth.value+"."+form.pvmYear.value;
		/*
		if(form.yhteystiedot.value=="1")
			klo = form.kloHour.value+":"+form.kloMin.value;
		*/	
		if(form.yhteystiedot.value=="2"){
		
			if(form.firstName.value==="")
				error = true;
			else if(form.lastName.value==="")
				error = true;
			else if(form.streetAddress.value==="")
				error = true;
			else if(form.postalCode.value==="")
				error = true;
			else if(form.city.value==="")
				error = true;
			else if(form.countryIso.value==="")
				error = true;
			else if(form.phonenumber.value==="")
				error = true;
			else if(!checkEmail(form.email.value))
				error = true;				
			else if(form.antaja.value==="")
				error = true;																							
		
			send = "action=address&nimi="+form.firstName.value+"&sukunimi="+form.lastName.value+"&osoite="+form.streetAddress.value+"&numero="+form.postalCode.value+"&kaupunki="+form.city.value+"&maa="+form.countryIso.value+"&puhelin="+form.phonenumber.value+"&email="+form.email.value+"&antaja="+form.antaja.value+"&pvm="+pvm+"&kieli="+form.kieli.value;
			
		}
		else{
		
			if(form.antaja.value==="")
				error = true;
			else if(!checkEmail(form.email.value))
				error = true;
		
			send = "action=address&email="+form.email.value+"&antaja="+form.antaja.value+"&pvm="+pvm+"&kieli="+form.kieli.value;
		
		}
	}

	if(!error){
	
		$.ajax({
			type: "POST",
			url: "phpIncludes/lahjakortit.php",
			data: send,
			async: true,
			success: function(){ page(nextPage,'','','true') }
		});
	
	}
	else{
	
		figureClear();
		fadeOut(recentSpeech);	
		a=setTimeout(function(){figureErrorOther("addressPerson");}, 600);	
		
	}
	
}

function lahjakortitOnnittelu(box){
	
	var str = $("#kupla"+box).val();
	
	if(str.length > 200){
		str = str.slice(0, 200);
		$("#kupla"+box).val(str);
	}
	
	$("#kupla"+box+"kpl").text(str.length);
	
}


function checkdate(input){
	
	var re=/^\d{1,2}.\d{1,2}.\d{4}$/;
	var returnval = true;
	
	if(input != '' && !input.match(re))
		returnval = false;
	else{
			
		var day = input.split(".")[0];
		var month = input.split(".")[1];
		var year  =input.split(".")[2];	
		
		var dayobj = new Date(year, month-1, day);
		
		if ((dayobj.getMonth()+1!=month)||(dayobj.getDate()!=day)||(dayobj.getFullYear()!=year))
			returnval = false;		
		
	}
	
	return returnval;

}

function checktime(input){
	
	var re=/^\d{1,2}:\d{2}$/;
	var returnval = true;
	
	if(input != '' && !input.match(re))
		returnval = false;
	else{
			
		var hour = input.split(":")[0];
		var minutes = input.split(":")[1];
		
		var timeobj = new Date();
		timeobj.setHours(hour,minutes,0,0);
		
		if((timeobj.getHours()!=hour)||(timeobj.getMinutes()!=minutes))
			returnval = false;		
		
	}
	
	return returnval;

}

function checkEmail(value){
	
	var results;
	var filter = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if (filter.test(value)){
		results=true;
	}
	else{
		results=false;
	}
	return results;
	
}

function lahjakortitSex(sex){

	if(sex=="w"){
		
		if($("#sukupuoliN").attr("checked")=="checked")
			$("#sukupuoliM").removeAttr("checked");
		else
			$("#sukupuoliM").attr("checked","checked");
		
	}
	else{
	
		if($("#sukupuoliM").attr("checked")=="checked")
			$("#sukupuoliN").removeAttr("checked");
		else
			$("#sukupuoliN").attr("checked","checked");
		
	}
	
}

function lahjakortitPostitus(toimitus){

	if(toimitus==1){
	
		if($("#tEmail").attr("checked")==true){
			$("#tMail").removeAttr("checked");			
			lahjakortitPostitusMuuta(0);
		}
		else{
			$("#tMail").attr("checked","checked");
			lahjakortitPostitusMuuta(3);
		}
		
	}
	else{
		
		if($("#tMail").attr("checked")==true){
			$("#tEmail").removeAttr("checked");			
			lahjakortitPostitusMuuta(3);
		}
		else{
			$("#tEmail").attr("checked","checked");
			lahjakortitPostitusMuuta(0);
		}
		
	}
	
}

function lahjakortitPostitusMuuta(toimitus){
	
	var lahja = $("#pLahjakortti").html();
	var lahjakortti = parseFloat(lahja.replace(",","."));
	
	toimitus = parseFloat(toimitus);
	
	var yhteensa = lahjakortti + toimitus;
	
	toimitus = toimitus.toFixed(2);
	toimitus = toimitus.replace(".",",")
	$('#pToimituskulut').text(toimitus);
	
	yhteensa = yhteensa.toFixed(2);
	yhteensa = yhteensa.replace(".",",");
	$('#pYhteensa').text(yhteensa);
	$('#yhteensa').val(yhteensa);
	
}

function ostoskoriLahjakortti(){

	$("#avaaMittatilausHideAll").fadeIn('fast',function(){ $("#avaaMittatilaus").css("display","block"); });	
	
	$.ajax({
		type: "POST",
		url: "phpIncludes/ostoskoriLahjakortti.php",
		data: "",
		async: true,
		success: function(data){
		
			ab("avaaMittatilaus",data);
			
			if($('#lahjakorttiHidden').length > 0)
				lahjaOstos = true;
			else
				lahjaOstos = false;
			
			selectFigure('lahjakortti','index','','',false);
			
		}
	});	
	
}

function checkLahjakortti(){

	var text = $('#textLahjakortti').val();
	
	$.ajax({
		type: "POST",
		url: "phpIncludes/ostoskoriLahjakorttiCheck.php",
		data: "text="+text,
		async: true,
		success: function(data){
		
			if(data!==""){
							
				if($('#figureMen').length > 0){
					document.getElementById('figureMen').onclick = "";
					document.getElementById('figureWomen').onclick = "";
					$('#figureMen').css('cursor','auto');
					$('#figureWomen').css('cursor','auto');
				}							
								
				var apuSplit = data.split("x92x");
				
				if(apuSplit[0] != '')
					ab("fMessage1",apuSplit[0]);
					
				ab("arvoLahja",apuSplit[3]);
				ab("kaytettyLahja",apuSplit[4]);
				ab("kaytettavissaLahja",apuSplit[5]);
				$('#buttonOK').css("display","none");
				$('#textLahja1').css("display","none");
				$('#textLahja2').css("display","block");
				if($('#buttonClose').length > 0)
					document.getElementById('buttonClose').onclick = function(){ page(17,'','');suljeHowOrder(); }	
	
				figureMain("lahjakortti",1,false);
				
			}
			else{
			
				figureError("lahjakortti");
			
			}
			
		}
	});	
	
}

function copyMeasures(id,kirjain) {
	
	var value = $('#'+id+kirjain).val();
	
	switch(kirjain) {
	
		case 'B':
		
			$('#'+id+'C').val(value);
			$('#'+id+'K').val(value);
			break;
			
		case 'C':
		
			$('#'+id+'B').val(value);
			$('#'+id+'K').val(value);
			break;
			
		case 'K':
		
			$('#'+id+'B').val(value);
			$('#'+id+'C').val(value);
			break;	
		
	}
	
}

//HAHMOT

function figureText(sPage,teksti){
	var sivu;
	
	if(sPage=="index_logout"){
		if($("#index_logout_text").val()=="testrun")
			sivu="index_logout";
		else if($("#index_logout_text").val()=="loggedin")
			sivu="index_loggedin";
	}
	else{
		sivu=sPage;
	}
	 
	return postPhp("figure/figure_text.php","page="+sivu+"&text="+teksti);
}

function fadeIn(eid){
	if(figureFade===true){
		$('#'+eid).fadeIn('slow');
	}
	else{
		$("#"+eid).css("display","block");
		$("#"+eid).css("visibility","visible");
		ieKupla=true;
	}		
}

function fadeOut(eid){
	if(figureFade===true){
		$('#'+eid).fadeOut('slow');
	}
	else{
		$("#"+eid).css("display","none");
		$("#"+eid).css("visibility","hidden");
		ieKupla=false;
	}		
}

function fade(eid)
{	
	if(figureFade===true){
		$('#'+eid).fadeOut('slow');
		$('#'+eid).fadeIn('slow');
	}
	else{
		if(ieKupla===false){
			$("#"+eid).css("display","block");
			$("#"+eid).css("visibility","visible");
			ieKupla=true;
		}
		else{
			$("#"+eid).css("display","none");
			$("#"+eid).css("visibility","hidden");
			ieKupla=false;
		}
	}
}

function figureHead(sPage,again,puhe,part){
	var apuHeight,values,arrayValues;
	
	if(part=="body" && $("#figureHidden").length > 0){
		
		values = $("#figureHidden").val();
		arrayValues = values.split(",");
		
		if($("#figureHeight").length > 0)
			apuHeight=figureMinHeight;
		else
			apuHeight=wHeight;
			
		$.post("figure/figureSite_body.php", { animation: "true", sukupuoli: arrayValues[3],hahmo: sPage, resolution: toPhp, height: apuHeight, folderNumber: arrayValues[0], folderSex: arrayValues[1], folder: arrayValues[2], "body": "head", hei: "", kerroin: arrayValues[5], puhe: puhe },
		function(data){
			var apuSplit = data.split("x92x");
			ab("figureKeskivartalo",apuSplit[0]);
			ab("figureHead",apuSplit[1]);
		}, "html")		
	
	}
	else{
		
		if($("#figureHidden").length > 0){
			
			values = $("#figureHidden").val();
			arrayValues = values.split(",");
			
			if($("#figureHeight").length > 0)
				apuHeight=figureMinHeight;
			else
				apuHeight=wHeight;
			
			$.post("figure/figureSite.php", { animation: "true", sukupuoli: arrayValues[3],hahmo: sPage, resolution: toPhp, height: apuHeight, folderNumber: arrayValues[0], folderSex: arrayValues[1], folder: arrayValues[2], picture: arrayValues[4], "body": "head", selected: "true", puhe: puhe, kerroin: arrayValues[5] },
			function(data){
				$("#figureHead").html(data);
			}, "html");	
					
		}
		else if($("#figureAlaHidden").length > 0){
			
			values = $("#figureAlaHidden").val();
			arrayValues = values.split(",");
			
			if($("#figureHeight").length > 0)
				apuHeight=figureMinHeight;
			else
				apuHeight=wHeight;
			
			$.post("figure/figureSite.php", { animation: "true", sukupuoli: arrayValues[3],hahmo: sPage, resolution: toPhp, height: apuHeight, folderNumber: arrayValues[0], folderSex: arrayValues[1], picture: arrayValues[2], "body": "head", selected: "true", puhe: puhe, kerroin: arrayValues[4] },
			function(data){
				ab("figureHead",data);
			}, "html");			

		}	
	
	}

	
	if(again=="again" && repeatMove<2){repeatMove=repeatMove+1;h=setTimeout(function(){figureHead(sPage,'again','false',"body");}, 8000);}
}

function figureError(sPage){
	var values,arrayValues,hahmo,indexError="",divName,rNumber;
	figureClear();
	
	if($("#figureHidden"+figureNow).length > 0){
		
		values = $("#figureHidden"+figureNow).val();
		arrayValues = values.split(",");
		if(recentSpeech!=="")
			fadeOut(recentSpeech);
			
		var mHeight = (sPage=="lahjakortti" || sPage=="ostoskoriLahjakortti") ? figureMinHeight : wHeight;
		
		if(sPage=="indexError" || sPage=="lahjakortti" || sPage=="ostoskoriLahjakortti"){
			
			hahmo = "indexError";
			indexError = "true";
			rNumber = randomMinToMax(1, 5);
			divName = 'fError'+rNumber;
			sPage = "indexError";
			
		}
		else if(sPage=="addressWrongPassword" || sPage=="addressNoCurrentPassword"){
		
			hahmo = "addressPerson";
			divName = sPage;
			sPage = "error";
			
		}
		else{
			
			hahmo = "addressPerson";
			rNumber = randomMinToMax(1, 5);
			divName = 'fError'+rNumber;	
			
		}
			
		$.post("figure/figureSite_error.php", { animation: "true", indexError: indexError, sukupuoli: arrayValues[3], hahmo: hahmo, resolution: toPhp, height: mHeight, folderNumber: arrayValues[0], folderSex: arrayValues[1], folder: arrayValues[2], "body": "keski", hei: "hei", kerroin: arrayValues[5], puhe: "true", figureNow: figureNow },
		function(data){
			var apuSplit = data.split("x92x");
			ab("figureKeskivartalo"+figureNow,apuSplit[0]);
			ab("figureHead"+figureNow,apuSplit[1]+tulostaKupla(sPage,arrayValues[3]));
	
			ab("figureText",$("#"+divName).html());
			fadeIn(recentSpeech);
			
			if($('#figureMen').length > 0){
				document.getElementById('figureMen').onclick = "";
				$("#figureMen").css("cursor","auto");
			
				if(sPage=="indexError"){
					document.getElementById('figureWomen').onclick = "";
					$("#figureWomen").css("cursor","auto");
				}
				
			}
			
		}, "html");
	
	}
		
}

function figureErrorOther(sPage){
	var values,arrayValues,hahmo,indexError="",divName,rNumber;
	figureClear();
	
	if($("#figureHidden"+figureNow).length > 0){
		
		values = $("#figureHidden"+figureNow).val();
		arrayValues = values.split(",");
		if(recentSpeech!=="")
			fadeOut(recentSpeech);
			
		var mHeight = (sPage=="lahjakortti" || sPage=="ostoskoriLahjakortti") ? figureMinHeight : wHeight;
		
		if(sPage=="indexError" || sPage=="lahjakortti" || sPage=="ostoskoriLahjakortti"){
			
			hahmo = "indexError";
			indexError = "true";
			rNumber = randomMinToMax(1, 5);
			divName = 'fError'+rNumber;
			sPage = "indexError";
			
		}
		else if(sPage=="addressWrongPassword" || sPage=="addressNoCurrentPassword"){
		
			hahmo = "addressPerson";
			divName = sPage;
			sPage = "error";
			
		}
		else{
			
			hahmo = "addressPerson";
			rNumber = randomMinToMax(1, 5);
			divName = 'fError'+rNumber;	
			
		}
			
		$.post("figure/figureSite_error.php", { animation: "true", indexError: indexError, sukupuoli: arrayValues[3], hahmo: hahmo, resolution: toPhp, height: mHeight, folderNumber: arrayValues[0], folderSex: arrayValues[1], folder: arrayValues[2], "body": "keski", hei: "hei", kerroin: arrayValues[5], puhe: "true", figureNow: figureNow },
		function(data){
			var apuSplit = data.split("x92x");
			ab("figureKeskivartalo"+figureNow,apuSplit[0]);
			ab("figureHead"+figureNow,apuSplit[1]);
	
			ab("figureText",$("#"+divName).html());
			fadeIn(recentSpeech);
			
			if($('#figureMen').length > 0){
				document.getElementById('figureMen').onclick = "";
				$("#figureMen").css("cursor","auto");
			
				if(sPage=="indexError"){
					document.getElementById('figureWomen').onclick = "";
					$("#figureWomen").css("cursor","auto");
				}
				
			}
			
		}, "html");
	
	}
		
}

function randomMinToMax(min_v, max_v){
	
	var range = max_v - min_v + 1;
	return Math.floor(Math.random()*range + min_v);
	
}

function figureAnswer(sPage){
	
	figureClear();
	fadeOut(recentSpeech);
	
	if(sPage=="howOrder1" || sPage=="howOrder2" || sPage=="howOrder3" || sPage=="howOrder4" || sPage=="howOrder5" || sPage=="howOrder6" || sPage=="howOrder7" || sPage=="howOrder8")
		howHeight=true;
	else
		howHeight=false;	
	
	figureHead(sPage,'','true',"head");
	m=setTimeout(function(){ab("figureText",$("#"+sPage).html());
	fadeIn(recentSpeech);
	if($('#figureMen').length > 0){
		document.getElementById('figureMen').onclick = "";
		$("#figureMen").css("cursor","default");
	}
	},700);
	
}

function figurePuhe(sPage){

	figureClear();
	
	if(sPage=="index"){
		
		ab("figureText",$("#fText1").html());
		fadeIn(recentSpeech);
		b=setTimeout("fadeOut('"+recentSpeech+"')", 11400);
		a=setTimeout("figureMain('index',2,false)", 12000);

	}
	else if(sPage=="index_logout"){
		
		ab("figureText",$("#fText1").html());
		fadeIn(recentSpeech);
		b=setTimeout("fadeOut('"+recentSpeech+"')", 11400);
		a=setTimeout("figureMain('index_logout',2,false)", 12000);
		
	}
	else if(sPage=="lahjakortti"){
		
		ab("figureText",$("#fMessage1").html());
		fadeIn(recentSpeech);
		if($('#figureMen').length > 0){
			document.getElementById('figureMen').onclick = function(){fadeOut(recentSpeech);figureMain(sPage,2,false);}
			document.getElementById('figureWomen').onclick = function(){fadeOut(recentSpeech);figureMain(sPage,2,false);}
		}
		$("#figureMen").css("cursor","pointer");
		$("#figureWomen").css("cursor","pointer");		
		//b=setTimeout("fadeOut('"+recentSpeech+"')", 11400);
		//a=setTimeout("figureMain('lahjakortti',2,false)", 12000);
		
	}
	else if(sPage=="ostoskoriLahjakortti"){
		
		ab("figureText",$("#fText1").html());
		fadeIn(recentSpeech);
		if($('#figureMen').length > 0){
			document.getElementById('figureMen').onclick = function(){fadeOut(recentSpeech);figureMain(sPage,2,false);}
			document.getElementById('figureWomen').onclick = function(){fadeOut(recentSpeech);figureMain(sPage,2,false);}
		}
		$("#figureMen").css("cursor","pointer");
		$("#figureWomen").css("cursor","pointer");

	}		
	else{
		
		ab("figureText",$("#fText1").html());
		figureHead(sPage,'','true',"head");
		fadeIn(recentSpeech);
		if(sPage!="error" && $('#figureMen').length > 0)
			document.getElementById('figureMen').onclick = function(){fadeOut(recentSpeech);figurePuheToinen(sPage);}
		
	}
	
}

function figurePuheToinen(sPage){

	figureClear();	
		
	if(sPage=="index"){
		
		ab("figureText",$("#fText2").html());
		fadeIn(recentSpeech);
		b=setTimeout("fadeOut('"+recentSpeech+"')", 11400);
		a=setTimeout("figureMain('index',3,false)", 12000);

	}
	else if(sPage=="index_logout"){
		
		ab("figureText",$("#fText2").html());
		fadeIn(recentSpeech);
		b=setTimeout("fadeOut('"+recentSpeech+"')", 11400);
		a=setTimeout("figureMain('index_logout',3,false)", 12000);
		
	}
	else if(sPage=="lahjakortti"){
		
		ab("figureText",$("#fMessage2").html());
		fadeIn(recentSpeech);
		if(sPage!="error" && $('#figureMen').length > 0){
			document.getElementById('figureMen').onclick = function(){ figureOut(sPage); }
			document.getElementById('figureWomen').onclick = function(){ figureOut(sPage); }
		}		
		//b=setTimeout("fadeOut('"+recentSpeech+"')", 11400);
		//a=setTimeout("figureMain('lahjakortti',3,false)", 12000);
		
	}
	else if(sPage=="ostoskoriLahjakortti"){
		
		ab("figureText",$("#fText2").html());
		fadeIn(recentSpeech);
		if(sPage!="error" && $('#figureMen').length > 0){
			document.getElementById('figureMen').onclick = function(){ figureOut(sPage); }
			document.getElementById('figureWomen').onclick = function(){ figureOut(sPage); }
		}
		
	}		
	else{
		
		b=setTimeout(function(){
		ab("figureText",$("#fText2").html());
		fadeIn(recentSpeech);}, 700);
		figureHead(sPage,'','true',false,"head");
		if(sPage!="error" && $('#figureMen').length > 0)
			document.getElementById('figureMen').onclick = function(){fadeOut(recentSpeech);$("#figureMen").css("cursor","auto");figureHead(sPage,'again','false',"body");document.getElementById('figureMen').onclick = "";}
		
	}
	
}

function figureOut(sPage){

	fadeOut(recentSpeech);
	$("#figureMen").css("cursor","auto");
	$("#figureWomen").css("cursor","auto");
	document.getElementById('figureMen').onclick = "";
	document.getElementById('figureWomen').onclick = "";
	figureMain(sPage,3,false);
	
}

function figureMain(sPage,type,stopMove){
    var apuSel;
	
	if(figureFade===true){
		ab("figure_speech","");
		ab("figure_speech_left","");
	}
	else{
		ab("puhekuplaMainRight","");
		ab("puhekuplaMain","");
	}
	
	if($("#figureHidden").length > 0){
	
		var values = $("#figureHidden").val();
		var aV = values.split(",");
		
		var valuesToinen = $("#figureHiddenToinen").val();
		var aV2 = valuesToinen.split(",");
		
		if(sPage=="index" || sPage=="lahjakortti" || sPage=="ostoskoriLahjakortti"){
			
			var sPuhe = sPage;
			var mHeight = (sPage=="lahjakortti" || sPage=="ostoskoriLahjakortti") ? figureMinHeight : wHeight;
			sPage = "index";			
			
			if(type==1){
				
				figureNow = "";
				$.post("figure/figureSite_index_bodyAndHead.php", { animation: true, sukupuoli: aV[3], puhe: "true", puheToinen: "false", resolution: toPhp, height: mHeight, folderNumber: aV[0], folderSex: aV[1], folder: aV[2], hei: "hei", kerroin: aV[5], folderNumberToinen: aV2[0], folderSexToinen: aV2[1], folderToinen: aV2[2], heiToinen: "", kerroinToinen: aV2[5] },
				function(data){
					var apuSplit = data.split("x92x");
					ab("figureKeskivartalo",apuSplit[0]);
					ab("figureHead",apuSplit[1]+tulostaKupla(sPage,sel));
					ab("figureKeskivartaloToinen",apuSplit[2]);
					ab("figureHeadToinen",apuSplit[3]);
					figurePuhe(sPuhe);
					
				}, "html");
				
			}
			else if(type==2){
				
				figureNow = "Toinen";
				apuSel = (sel=="men") ? "women" : "men";
				var hei = (mHeight==figureMinHeight) ? "" : "hei";
				
				$.post("figure/figureSite_index_bodyAndHead.php", { animation: true, sukupuoli: aV[3], puhe: "false", puheToinen: "true", resolution: toPhp, height: mHeight, folderNumber: aV[0], folderSex: aV[1], folder: aV[2], hei: "", kerroin: aV[5], folderNumberToinen: aV2[0], folderSexToinen: aV2[1], folderToinen: aV2[2], heiToinen: hei, kerroinToinen: aV2[5] },
				function(data){
					var apuSplit = data.split("x92x");
					ab("figureKeskivartalo",apuSplit[0]);
					ab("figureHead",apuSplit[1]);
					ab("figureKeskivartaloToinen",apuSplit[2]);
					ab("figureHeadToinen",apuSplit[3]+tulostaKupla(sPage,apuSel));
					figurePuheToinen(sPuhe);
				}, "html");
				
			}
			else if(type==3){
				
				$.post("figure/figureSite_index_bodyAndHead.php", { animation: true, sukupuoli: aV[3], puhe: "false", puheToinen: "false", resolution: toPhp, height: mHeight, folderNumber: aV[0], folderSex: aV[1], folder: aV[2], hei: "", kerroin: aV[5], folderNumberToinen: aV2[0], folderSexToinen: aV2[1], folderToinen: aV2[2], heiToinen: "", kerroinToinen: aV2[5] },
				function(data){
					var apuSplit = data.split("x92x");
					ab("figureKeskivartalo",apuSplit[0]);
					ab("figureHead",apuSplit[1]);
					ab("figureKeskivartaloToinen",apuSplit[2]);
					ab("figureHeadToinen",apuSplit[3]);
					if(!stopMove)
						a=setTimeout("figureMain('"+sPuhe+"',"+type+",true)", 12000);
				}, "html");
				
			}
		}
		else if(sPage=="index_logout"){
		
			if(type==2){
				
				figureNow = "Toinen";
				apuSel = (sel=="men") ? "women" : "men";
				
				$.post("figure/figureSite_index_bodyAndHead.php", { animation: true, sukupuoli: aV[3],puhe: "false", puheToinen: "true", resolution: toPhp, height: wHeight, folderNumber: aV[0], folderSex: aV[1], folder: aV[2], hei: "", kerroin: aV[5], folderNumberToinen: aV2[0], folderSexToinen: aV2[1], folderToinen: aV2[2], heiToinen: "", kerroinToinen: aV2[5]},
				function(data){
					var apuSplit = data.split("x92x");
					ab("figureKeskivartalo",apuSplit[0]);
					ab("figureHead",apuSplit[1]);
					ab("figureKeskivartaloToinen",apuSplit[2]);
					ab("figureHeadToinen",apuSplit[3]+tulostaKupla(sPage,apuSel));
					figurePuheToinen(sPage);
				}, "html");
				
			}
			else if(type==3){
				
				figureNow = "Toinen";
				apuSel = (sel=="men") ? "women" : "men";				
				
				$.post("figure/figureSite_index_bodyAndHead.php", { animation: true, sukupuoli: aV[3],puhe: "false", puheToinen: "false", resolution: toPhp, height: wHeight, folderNumber: aV[0], folderSex: aV[1], folder: aV[2], hei: "", kerroin: aV[5], folderNumberToinen: aV2[0], folderSexToinen: aV2[1], folderToinen: aV2[2], heiToinen: "", kerroinToinen: aV2[5] },
				function(data){
					var apuSplit = data.split("x92x");
					ab("figureKeskivartalo",apuSplit[0]);
					ab("figureHead",apuSplit[1]);
					ab("figureKeskivartaloToinen",apuSplit[2]);
					ab("figureHeadToinen",apuSplit[3]);
					if(!stopMove)
						a=setTimeout("figureMain('index_logout',3,true)", 12000);
				}, "html");
				
			}		
			
		}
	
	}
	
}

function figureSite(sPage){
	figureClear();	
	
	if(sPage=="index") {
		a=setTimeout("figureMain('"+sPage+"',1,false)", 1500);
	}
	else if(sPage=="addressPerson" || sPage=="getUsername" || sPage=="indexError") {
		a=setTimeout(function(){figureError(sPage);}, 600);
	}
}

function showFigure(hahmoSivu,maaratty,howorder,frommenu,hahmoSpeech){
	
	var lisariMen="",lisariWomen="",puhe;
	
	if(howorder===true)
		howHeight=true;
	else
		howHeight=false;	
	
	if(maaratty==="" && hahmoSivu=="addressPerson" && hahmoSpeech!="lahjakortit" || maaratty==="" && hahmoSivu=="addressPerson" && hahmoSpeech!="lahjakorttiOnnittelu" || maaratty==="" && hahmoSivu=="addressPerson" && hahmoSpeech!="lahjakorttiPostitus" || maaratty==="" && hahmoSivu=="addressPerson" && hahmoSpeech!="lahjakorttiAddress" || maaratty==="" && hahmoSivu=="addressPerson" && hahmoSpeech!="lahjakorttiMaksu" || maaratty==="" && hahmoSivu!="addressPerson" || maaratty==="" && hahmoSivu=="addressPerson" && hahmoSpeech!="popUp_delivery"){
	
		var selectSex = [ "men", "women" ];
		sel = selectSex[Math.floor(Math.random()*selectSex.length)];
	
		if(hahmoSivu=="index"){
			
			if(sel=="men")
				lisariMen=tulostaKupla(hahmoSivu,"men");
			else
				lisariMen="";
				
			if(sel=="women")
				lisariWomen=tulostaKupla(hahmoSivu,"women");
			else
				lisariWomen="";		
			 
			$.post("figure/figureSite_index.php", { resolution: toPhp , height: wHeight , selected: sel },
			function(data){
				var apuSplit = data.split("x92x");
				ab("figureMen",apuSplit[0]+lisariMen);
				ab("figureWomen",apuSplit[1]+lisariWomen);
				figureSite("index");
			}, "html");			 
			 	 
		}
		else if(hahmoSivu=="index_logout"){
			
			$("#figureTemp").html('');
			figureNow = "";
			if(sel=="men"){
				lisariMen=tulostaKupla(hahmoSivu,"men");
				puhe="men";
			}
			else{
				lisariMen="";
			}
			if(sel=="women"){
				lisariWomen=tulostaKupla(hahmoSivu,"women");
				puhe="women";
			}
			else{
				lisariWomen="";
			}
			
			if(frommenu=="frommenu")
				puhe="";
			
			$.post("figure/figureSite_indexLogout.php", { resolution: toPhp , height: wHeight , selected: sel, puhe: puhe },
			function(data){
				var apuSplit = data.split("x92x");
				ab("figureMen",apuSplit[0]+lisariMen);
				ab("figureWomen",apuSplit[1]+lisariWomen);
				if(frommenu!="frommenu")
					figurePuhe("index_logout");
				else
					h=setTimeout("figureMain('index_logout',3,false)", 3000);
			}, "html");			
			 
		}	
		else if(hahmoSivu=="lahjakortti"){
			
			figureNow = "";
			if(sel=="men")
				lisariMen=tulostaKupla("index","men");
			else
				lisariMen="";
				
			if(sel=="women")
				lisariWomen=tulostaKupla("index","women");
			else
				lisariWomen="";	
			 
			$.post("figure/figureSite_indexLogout.php", { resolution: toPhp , height: figureMinHeight , selected: sel, puhe: "true" },
			function(data){
				var apuSplit = data.split("x92x");
				ab("figureMen",apuSplit[0]+lisariMen);
				ab("figureWomen",apuSplit[1]+lisariWomen);
				
				if(lahjaOstos)
					figureMain("lahjakortti",1,false);
				else
					figurePuhe("ostoskoriLahjakortti");
					
			}, "html");			 
			 	 
		}		
		else if(hahmoSivu=="howOrder"){
			
			 var hoHeight;
			 if(howorder===true){
				 hoHeight=figureMinHeight;
				 howHeight=true;
			 }
			 else{
				 hoHeight=wHeight;
			 }
			 
			$.post("figure/figure2.php", { hahmo: hahmoSivu, resolution: toPhp, height: hoHeight, puhe: "true" },
			function(data){
				ab("figureMen",data+tulostaKupla(hahmoSivu,""));
				
				ab("figureText",$("#fText1").html());
				
				if(hahmoSpeech != "popUp_delivery") {
				
					fadeIn(recentSpeech);
				
					if($('#figureMen').length > 0){
						document.getElementById('figureMen').onclick = function(){fadeOut(recentSpeech);figurePuheToinen(hahmoSivu,'');}
						$("#figureMen").css("cursor","pointer");
					}
				}
				else {
					
					if($('#figureMen').length > 0)
						document.getElementById('figureMen').onclick = function(){figurePuhe(hahmoSivu);};
					$("#figureMen").css("cursor","pointer");
					
				}
				
			}, "html");			 

		}
		else{
			
			if(hahmoSivu=="tilaushistoria" || hahmoSivu=="tilausseuranta" || hahmoSivu=="vakiomitat" || hahmoSivu=="toimipisteet" || hahmoSivu=="faq" || hahmoSivu=="ostoskori" || hahmoSivu=="ostoskoriMaksu" || hahmoSivu=="ostoskoriAddress" || hahmoSivu=="ostoskoriAddressLogged" || hahmoSpeech=="lahjakorttiOnnittelu" || hahmoSpeech=="lahjakorttiAddress" || hahmoSpeech=="lahjakorttiMaksu")
				puhe="true";
			else
				puhe="";
			 
			$.post("figure/figure2.php", { hahmo: hahmoSivu, resolution: toPhp, height: wHeight, puhe: puhe },
			function(data){
				ab("figureMen",data+tulostaKupla(hahmoSivu,""));				
				ab("figureTemp",$("#figureKeskivartalo").html());
				
				if(hahmoSivu=="tilaushistoria" || hahmoSivu=="tilausseuranta" || hahmoSivu=="vakiomitat" || hahmoSivu=="toimipisteet" || hahmoSivu=="faq" || hahmoSivu=="ostoskori" || hahmoSivu=="ostoskoriMaksu" || hahmoSivu=="ostoskoriAddress" || hahmoSivu=="ostoskoriAddressLogged" || hahmoSpeech=="lahjakorttiOnnittelu" || hahmoSpeech=="lahjakorttiAddress" || hahmoSpeech=="lahjakorttiMaksu"){
					
					ab("figureText",$("#fText1").html());
					fadeIn(recentSpeech);				
					
					if($('#figureMen').length > 0){
						document.getElementById('figureMen').onclick = function(){fadeOut(recentSpeech);figurePuheToinen(hahmoSivu,'');}
						$("#figureMen").css("cursor","pointer");
					}					
					
				}
				else{
					
					if($('#figureMen').length > 0)
						document.getElementById('figureMen').onclick = function(){figurePuhe(hahmoSivu);};
					$("#figureMen").css("cursor","pointer");
				}
				
			}, "html");
			 
		}
	}
	else{
		
		$("#figureMen").html(maaratty);	
		$("#"+recentSpeech).css("display","none");
		
		if(hahmoSpeech=="lahjakorttiOnnittelu" || hahmoSpeech=="lahjakorttiAddress" || hahmoSpeech=="lahjakorttiMaksu" || hahmoSpeech=="lahjakortit" || hahmoSpeech=="ostoskoriMaksu" || hahmoSpeech=="ostoskoriAddress" || hahmoSpeech=="ostoskoriAddressLogged"){
		
			ab("figureText",$("#fText1").html());						
			
			if($('#figureMen').length > 0){
				document.getElementById('figureMen').onclick = function(){fadeOut(recentSpeech);figurePuheToinen(hahmoSivu,'');}
				$("#figureMen").css("cursor","pointer");
			}			
			
		}
		else{

			if($('#figureMen').length > 0)
				document.getElementById('figureMen').onclick = function(){figurePuhe(hahmoSivu);$("#"+recentSpeech).css("visibility","visible");};
			$("#figureMen").css("cursor","pointer");
			
		}
				
		if(hahmoSpeech=="lahjakortit" || hahmoSpeech=="lahjakorttiMaksu" || hahmoSpeech=="lahjakorttiPostitus"){
			
			var temp = $("#figureTemp").html();
			$("#figureKeskivartalo").html(temp);
			
			if(hahmoSpeech=="lahjakorttiMaksu"){
				fadeIn(recentSpeech);
				figureHead(hahmoSivu,'','true',"head");
			}
			else{
				figureHead(hahmoSivu,'','false',"head");
			}
			
		}
		else if(hahmoSpeech=="lahjakorttiAddress" || hahmoSpeech=="lahjakorttiOnnittelu" || hahmoSpeech=="ostoskoriMaksu" || hahmoSpeech=="ostoskoriAddress" || hahmoSpeech=="ostoskoriAddressLogged"){
		
			figureHead(hahmoSivu,'','true',"head");
			fadeIn(recentSpeech);
			
		}
		
	}
}

function selectFigure(hahmoSivu,hahmoSpeech,hahmo,frommenu,howorder){
	ieKupla=false;
	repeatMove=0;
	figureClear();
	
	showFigure(hahmoSivu,hahmo,howorder,frommenu,hahmoSpeech);

}

function tulostaKupla(tyyppi,sex){
	
	var tuloste;
	
	if(tyyppi=="index" || tyyppi=="index_logout" || tyyppi=="indexError"){
		
		if(sex=="women"){
			if(figureFade===true){
				tuloste = '<div id="figure_speech_left" style="display:none;"><img class="figureTextSpeechLeft" alt="" src="image/figureSpeechLeftSmall.png" /><img class="figureTextTop" alt="" src="image/figureSpeechTop.png" /><p id="figureText"></p><img class="figureTextBottom" alt="" src="image/figureSpeechBottom.png" /></div>';
				recentSpeech="figure_speech_left";
			}
			else{
				tuloste = '<div id="puhekuplaMain"><div id="puhekuplaTop"><img id="puhekuplaImgTop" alt="" src="image/figureSpeechTop.png" /></div><div id="puhekuplaDiv"><p id="figureText"></p></div><div id="puhekuplaBottom"><img id="puhekuplaImgBottom" alt="" src="image/figureSpeechBottom.png" /></div><div id="puhekuplaDir"><img id="puhekuplaImgDir" alt="" src="image/figureSpeechLeftSmall.png" /></div></div>';
				recentSpeech="puhekuplaMain";
			}
		}
		else{
			if(figureFade===true){
				tuloste = '<div id="figure_speech" style="display:none;"><img class="figureTextSpeech" alt="" src="image/figureSpeechSmall.png" /><img class="figureTextTop" alt="" src="image/figureSpeechTop.png" /><p id="figureText"></p><img class="figureTextBottom" alt="" src="image/figureSpeechBottom.png" /></div>';
				recentSpeech="figure_speech";
			}
			else{
				tuloste = '<div id="puhekuplaMainRight"><div id="puhekuplaDirRight"><img id="puhekuplaImgDir" alt="" src="image/figureSpeechSmall.png" /></div><div id="puhekuplaTopRight"><img id="puhekuplaImgTop" alt="" src="image/figureSpeechTop.png" /></div><div id="puhekuplaDivRight"><p id="figureText"></p></div><div id="puhekuplaBottomRight"><img id="puhekuplaImgBottom" alt="" src="image/figureSpeechBottom.png" /></div></div>';
				recentSpeech="puhekuplaMainRight";
			}
		}
	}
	else{
		if(figureFade===true){
			tuloste = '<div id="figure_speech_others" style="display:none;"><img class="figureTextSpeech_others" alt="" src="image/figureSpeech.png" /><img class="figureTextTop" alt="" src="image/figureSpeechTop.png" /><p id="figureText"></p><img class="figureTextBottom" alt="" src="image/figureSpeechBottom.png" /></div>';
			recentSpeech="figure_speech_others";
		}
		else{
			tuloste='<div id="puhekuplaMainOthers"><div id="puhekuplaDirOthers"><img id="puhekuplaImgDirOthers" alt="" src="image/figureSpeech.png" /></div><div id="puhekuplaTopOthers"><img id="puhekuplaImgTop" alt="" src="image/figureSpeechTop.png" /></div><div id="puhekuplaDivOthers"><p id="figureText"></p></div><div id="puhekuplaBottomOthers"><img id="puhekuplaImgBottom" alt="" src="image/figureSpeechBottom.png" /></div></div>';
			recentSpeech="puhekuplaMainOthers";
		}
	}

	return tuloste;
}

