$(document).ready(function(){
	function GETvalue( name ) {
  		var results = new RegExp( "[\\?&]"+name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]")+"=([^&#]*)" ).exec( window.location.href );
  		return ( results ) ? results[1] : "";
	}
			
	$(".roundedcorners").corner({tl:{radius:6},tr:{radius:6},bl:{radius:6},br:{radius:6},autoPad:true});
	$(".roundedcorners2").corner({tl:{radius:6},tr:{radius:6},bl:{radius:6},br:{radius:6},autoPad:true});
		
/* NETTIKAUPAN KOODIT */	
	$(".shop_dropmenu li").hover(function(){$(this).toggleClass("shop_dropmenu_mouseover");},function(){$(this).toggleClass("shop_dropmenu_mouseover")});
	$(".shop_dropmenu li").toggle(function(){$(this).children("ul").show("slow")},function(){$(this).children("ul").hide("slow")});
	$(".shop_dropmenu li:has(ul)").click(function(){$(this).toggleClass("shop_dropmenu_arrowdown")});

	function tuoteiso(tama){
		$("#shop_divpieni").fadeOut("slow",function(){$("#shop_diviso").fadeIn("slow");});
		var vals = $(tama).prev().attr("value");
		var arr = vals.split("*");
		$("#tuote_esittely").fadeOut("slow",function(){$(this).children("img").attr({"src":'/shop_kuvat/'+arr[0]});}).fadeIn("slow");
		$("#tuote_esittely2").children("font:first").text(arr[1]);
		$("#tuote_esittely2").children("input:hidden").attr({"value":vals});
	}
	$(".tuotelink").click(function(){tuoteiso(this);});
	$(".tuotekuvapienitausta").click(function(){tuoteiso($(this).children("img"))});

	$(".alakategorialink").click(function(){
		var divid = '#'+$(this).next().attr("value");
		$("#shop_diviso").fadeOut("slow",function(){
			$("#shop_divpieni").show();
			$(divid).siblings().hide();
			$(divid).fadeIn("slow");
		});
	});
	
	function handlebasket(vals){
		var arr = vals.split("*");
		var text = false;
		if ( arr[2]%2 == 1 ) { var symbol = '<img src="/bodypics/male_symbol_small.png">'; }
		else				 { var symbol = '<img src="/bodypics/female_symbol_small.png">'; }

		$("#shop_ostoskori label").each(function(){
			var tuotevals = $(this).children("input").attr("value").split('*');
			if ( tuotevals[2] == arr[2] && tuotevals[4] == arr[4] ){
				text = $(this).children("#shop_kpl").text();
				$(this).children("#shop_kpl").text( (text*1+1) );
			}
		});

		if ( !text ) {
			$("#shop_ostoskori").prepend(' <label style="display:block;">'+symbol+' <input type="hidden" value="'+vals+'"><font id="showtuote" style="cursor:pointer;">'+arr[1]+'</font>'+
									 ' <font style="color:#ffc20e;font-weight:bold;">'+arr[4]+'</font> <font id="shop_kpl">1</font>kpl <font class="shop_poista">Poista</font></label>');
		}
		$(".shop_poista").click(function(){$(this).parent().fadeOut("slow",function(){$(this).remove();
			
			var hinta = 0;
			$("#shop_ostoskori label").each(function(){hinta += $(this).children("#shop_kpl").text()*20});
			if ( hinta > 0){ hinta += 5; }
			$("#shop_hinta").text(hinta);		
		
		})});
		$("#showtuote").click(function(){tuoteiso(this);});
	}
	
	$("#lisaa_ostoskoriin").click(function(){
		var vals = $(this).prev().attr("value")+'*'+$(this).prev().prev().attr("value");
    	$("#tuote_esittely2").fadeOut("fast");
    	$("#tuotekuva").css("position","relative").animate({width:"100px",height:"100px",top:"310px",left:"-360px"},1000,function(){
	        $(this).fadeOut("slow",function(){
				$(this).css({"position":"static","top":"auto","left":"auto","width":"100%","height":"100%"}).fadeIn("slow",function(){$("#tuote_esittely2").fadeIn("slow")})
			});
	        handlebasket(vals);
		});
	});
	
	$("#shop_emptybasket").click(function(){
		if ( $(this).attr("value")=='Jatka Shoppailua' ){
			$("#shop_div_ostoskori").css("position","relative").animate({
				height:"133px",
				top:"0px"
				},1000);
			$("#shop_ostoskori").css("border","0px").animate({height:"78px"},1000);
			$("#shop_tilaajatiedot").fadeOut("slow");
			$(this).attr("value","Tyhjennä ostoskori");
			$("#shop_ehdot").hide();
			$("#shop_divpieni").show();
			$('#'+$(".alakategorialink:first").next().attr("value")).fadeIn("slow");
		}else {	$("#shop_ostoskori").empty() }
	});

	function tarkistay(yt){
    	yt = yt.replace(/^\s+|\s+$/g, '');        
		tnum = yt.substr(yt.length*1-1,1);
        yt = ((yt.substr(0,(yt.length*1-2))).length==6 ? 0 : '' ) + yt.substr(0,(yt.length*1-2));
        sum = 0;
        
        if ( yt.length == 7 )
			for ( var i in arr = new Array(7,9,10,5,8,4,2) )
				sum += yt.substr(i,1)*1 * arr[i];
        return ( sum%11!=1 && sum!=0 && (sum%11==0 && sum%11==tnum || 11-sum%11==tnum ) ? true : false );
	}
	function tarkistahetu(hetu) {		
		if ((hetu.length)==11 && hetu.substr(0,2)<32 && hetu.substr(2,2)<13 && hetu.substr(6,1)=='-' || hetu.substr(6,1)=='+' || hetu.substr(6,1)=='A') {
			tarkaste=hetu.substr(10,1);
			jaannos=((hetu.substr(0,6)+hetu.substr(7,3))%31);												
			if (jaannos==tarkaste && jaannos<=9 && jaannos>=0  ) return true;				
			if (jaannos==10 && tarkaste=='A') return true;
			if (jaannos==11 && tarkaste=='B') return true;
			if (jaannos==12 && tarkaste=='C') return true;
			if (jaannos==13 && tarkaste=='D') return true;
			if (jaannos==14 && tarkaste=='E') return true;
			if (jaannos==15 && tarkaste=='F') return true;
			if (jaannos==16 && tarkaste=='H') return true;
			if (jaannos==17 && tarkaste=='J') return true;
			if (jaannos==18 && tarkaste=='K') return true;
			if (jaannos==19 && tarkaste=='L') return true;
			if (jaannos==20 && tarkaste=='M') return true;
			if (jaannos==21 && tarkaste=='N') return true;
			if (jaannos==22 && tarkaste=='P') return true;
			if (jaannos==23 && tarkaste=='R') return true;
			if (jaannos==24 && tarkaste=='S') return true;
			if (jaannos==25 && tarkaste=='T') return true;
			if (jaannos==26 && tarkaste=='U') return true;
			if (jaannos==27 && tarkaste=='V') return true;
			if (jaannos==28 && tarkaste=='W') return true;
			if (jaannos==29 && tarkaste=='X') return true;
			if (jaannos==30 && tarkaste=='Y') return true;
			return false;
		} else {
			return false;
		}
	}
	
	$('#sel_tunnus').change(function(){
		if ( $(this).val() == 1 )
			$('#txt_tunnus').html('Y-tunnus');
		else
			$('#txt_tunnus').html('Henkilötunnus');
	});
	
	$("#shop_tilaa").click(function(){				
		var hinta = 0;
		$("#shop_ostoskori label").each(function(){hinta += $(this).children("#shop_kpl").text()*20});
		if( hinta == 0 ) {
			alert("Ostoskori on tyhjä.");
		}else{
			hinta+=5;
			$("#shop_hinta").text(hinta);
			$("#shop_div_ostoskori").css("position","relative").animate({height:"+440px",top:"-307px"},1000);
			$("#shop_ostoskori").animate({height:"+100px"},1000).css("border","1px solid white");
			$("#shop_tilaajatiedot").show();
			$("#shop_divpieni").hide();
			$("#shop_diviso").hide();
			$("#shop_ehdot").show();
			if ( $(this).next().attr("value") == "Jatka Shoppailua" ){
				var syottovirhe = false;
				$("#shop_tilaajatiedot input").each(function(){	
					if($(this).attr("value").replace(/^\s+|\s+$/g,'')==''){
						if($(this).attr("name")=='tunnus') {}							
						else syottovirhe=true;
					} 
				});	
				if ( $('#sel_tunnus').val() == 0 && tarkistahetu($('#tunnus').val()) == false ) {
					alert('Henkilötunnus väärin. Tarkista ja yritä uudestaan');
					syottovirhe = true;
				}else if ( $('#sel_tunnus').val() == 1 && tarkistay($('#tunnus').val()) == false ) {
					alert('Y-tunnus väärin. Tarkista ja yritä uudestaan');
					syottovirhe = true;
				}else if ( syottovirhe ) alert("Muistathan täyttää kaikki tietosi.");
							
				if ( !syottovirhe  ) {
					if ( confirm('Tilausvahvistus.\n\nVahvistan tietoni oikeiksi ja haluan tilata\n'+((hinta-5)/20)+'kpl paitoja hintaan '+hinta+'€(sisältää postikulut)? \n') == true ){
						var ids = '';
						var ui = '';
						var text = '';
						$("#shop_ostoskori input").each(function(){
							var arr = $(this).attr("value").split('*');
							var kpl = $(this).siblings("#shop_kpl").text()
							ids += arr[2]+':'+arr[4]+':'+kpl+',';
							if ( arr[2]%2==1 ) { var sex = 'Miesten paita|'; } 
							else			   { var sex = 'Naisten paita|'; }
							text+= sex+arr[1]+'|'+arr[4]+'|'+kpl+':';
						});
						$("#shop_tilaajatiedot input").each(function(){
							ui += $(this).attr("value")+':';
						});
						$("#shop_maintable").hide("slow").before('<div class="yellowfont" style="position:absolute;top:50%;left:30%;font-size:22px;">Odota, tilaustasi käsitellään</div>');
						
						$.post("includes/shop_tilaus.php",{ids:ids,ui:ui,text:text,tyyppi:$('#sel_tunnus').val()},function(response){
							if(response){
								alert("Tilauksesi on vastaanotettu. Saat hetken kuluttua antamaasi sähköpostiosoitteeseen tilausvahvistuksen.");
								$("#shop_maintable").prev().remove();
								document.location = '?s=shop';		
							}else{ alert("Tilauksen vastaanotto epäonnistui. Kokeile uudestaan.") }                            
						});						
					}
				}
			}
			$(this).next().attr("value","Jatka Shoppailua");
		}
	});
 	$("#shop_maintable").show();
	$('#'+$(".alakategorialink:first").next().attr("value")).fadeIn("slow");
/* NETTIKAUPAN KOODIT LOPPUU TÄHÄN*/	
});

