$(document).ready(function(){
	
	//$("embed").css({"z-index":"9"}).attr({"wmode":"opaque"});
	//$("embed").attr({"wmode":"transparent"});
	
	$(".lightbox").lightbox();
	$(".lightboxPic").lightbox();
	
	$("#ProdList .Box").hover(
   	function(){ $(this).addClass("hov"); },
		function(){ $(this).removeClass("hov"); }
	);
	
	jQuery(function($) { $("input:password, #NUFlogin, #MiniLogin, #Login").val("") });
	$("#Information").click(function(){
		$(this).animate({ height: 'hide', opacity: 'hide' }, "slow");
	});
	
	$("#Cart .short").change(function(){
		var q = $(this).val();
		if(!/^[0-9]{1,4}$/.test(q)) $(this).val(0);
		ReloadCart();
	});
	$("#Cart .cb").click(function(){
		ReloadCart();
	});
	
	$("#fvat").click(function(){
		if($(this).attr('checked')) var ch = false;
		else var ch = true;
		tabl = new Array("UFFfirmname","UFFname","UFFsurname","UFFstreet","UFFnumber","UFFcity","UFFkod1","UFFkod2","UFFnip");
		for(i=0;i<tabl.length;i++) {
			$("#"+tabl[i]).val("");
			$("#"+tabl[i]).attr({disabled: ch});
			if(ch) $("#"+tabl[i]).addClass("dis");
			else $("#"+tabl[i]).removeClass("dis");
		}
	});
	$("#CopyUserData").click(function(){
		if($("#fvat").attr('checked')) {
			$("#UFFname").val($("#UFname").val());
			$("#UFFsurname").val($("#UFsurname").val());
			$("#UFFstreet").val($("#UFstreet").val());
			$("#UFFnumber").val($("#UFnumber").val());
			$("#UFFcity").val($("#UFcity").val());
			$("#UFFkod1").val($("#UFkod1").val());
			$("#UFFkod2").val($("#UFkod2").val());
		}
		return false;
	});
	
	$("#PayMethods input").click(function(){
		if($("#DelPriceList").val()!='') {
			var dpl = eval($("#DelPriceList").val());
			var tv = $(this).val();
			$("#PayMet").val( tv );
			
			var idm = $("#DelMethods").find("input:radio");
			dma = new Array;
			idm.each(function(){ 
				dma[ $(this).val() ] = false; 
			});
			
			$.each(dpl, function(i,item){
				if(item['pid']==tv) {
					for(i=0;i<item["data"].length;i++) {
						dma[ item["data"][i]['dm'] ] = item["data"][i]['price'];
					}
				}
			});
			
			$.each(dma, function(i,item){
				if(item!=undefined) {
					if(!item) {
						$("#DelMethod"+i).animate({ height: 'hide', opacity: 'hide' }, "fast");
						if($("#DM"+i).attr("checked")==true) {
							$("#DelMet").val("");
							$("#DelPr").val("");
						}
						$("#DM"+i).attr({"checked":false});
						$("#DelMetPrice"+i).val("");
					}else{
						$("#DelMethod"+i).animate({ height: 'show', opacity: 'show' }, "slow");
						$("#DelMetPrice"+i).val(item);
					}
				}
			});
			
			ReloadOrder();
		}
	});
	
	$("#DelMethods input").click(function(){
		$("#DelMet").val( $(this).val() );
		$("#DelPr").val( $("#DelMetPrice"+$(this).val() ).val() );
		ReloadOrder();
	});
	
	$(".comboVar select").change(function(){
		var vdid = $(this).val();
		var id = $(this).attr("id").split("_");
		if(vdid!='') {
			$("#MiniPic_"+id[1]).html( '<img src="/foto.php?f=vd_'+vdid+'.jpg&amp;xmax=60&amp;ymax=60&amp;crop" alt="Podgląd" />' );
		}else $("#MiniPic_"+id[1]).html( '&nbsp;' );
		$("#BigProd .var").each(function(){
			if($(this).attr("id")=='VarImg'+vdid) $(this).addClass("Sel");
			else $(this).removeClass("Sel");
		});
	});
	
});

function checkSearch() {
	var sw = $("#SearchWord").val();
	if(sw.length==0) return false;
	else return true;
}

// =====================================================
// Zamówiernia 

function ReloadOrder() {
	var op = $("#OrdPr").val();
	var dp = $("#DelPr").val();
	if(!isNaN(op)&&!isNaN(dp)) {
		$("#AllPr").val( (op*1)+(dp*1) );
		$("#DelPriceTxT").text( Formatuj(dp)+" PLN" );
		$("#AllPriceTxT").text( Formatuj((op*1)+(dp*1))+" PLN" );
	}else{
		$("#AllPr").val( 0 );
		$("#DelPriceTxT").text( " - - - " );
		$("#AllPriceTxT").text( " - - - " );
	}
}

function CheckOrder() {
	
	if($("#PayMet").val().length==0) return ShowError("Proszę wybrać metodę płatności.");
	else if($("#DelMet").val().length==0) return ShowError("Proszę wybrać metodę dostawy");
		
	return true;
}

// =====================================================
// Koszyk 

function ReloadCart() {
	$("#ReloadAndSaveCart").animate({ width: 'show', opacity: 'show' }, "slow");
	var ic = $("#CartCount").val();
	var sum = qsum = q = p = pa = 0;
	var r = false;
	for(i=0;i<ic;i++) {
		if($("#DelRow"+i).attr("checked")) {
			$("#TabRow"+i).addClass("strike")
		}else{
			$("#TabRow"+i).removeClass("strike");
			p = $("#PriceRow"+i).val();
			q = $("#QuantRow"+i).val();
			op = $("#OtherPriceRow"+i).val();
			pa = Math.floor((p*q)*100)/100;
			qsum += $("#QuantRow"+i).val()*1;
			if(pa>0) $("#PriceRowTxt"+i).html(Formatuj(pa)+" PLN");
			else $("#PriceRowTxt"+i).html(" - - - ");
			sum += pa;
		}
	}
	sum = Math.floor(sum*100)/100;
	$("#AllPrice").html(Formatuj(sum));
	$("#AllQuant").html(qsum);

}

function ClearCart() {
	$("#OtherCartCommand").val("ClearCart");
	$("#Cart form").submit();
}

function Formatuj(p) {
	l = new Object();
	l.o = Math.round(p*100)/100;
	l.c = Math.floor(l.o);
	l.u = Math.round((l.o - l.c)*100);
	if(l.u<10) l.u = "0"+l.u;
	l.u = "."+l.u
	l.c += "";
	if(l.c.length>3) {
		for(var i=3;i<l.c.length;i+=4) l.c = l.c.substr(0,(l.c.length-i))+" "+l.c.substr((l.c.length-i),(2*l.c.length-i));
	}
	return l.c+l.u;
}
	
function Zaokr(p) {
	return  Math.round(p*100)/100;
}

// =====================================================

function CheckOrd() {
	var szawa = $("#req").val().split(",");
	for(var i=0;i<szawa.length;i++) {
		if($("#var_"+szawa[i]).val()=='') return ShowError("Proszę wybrać wszystkie wymagane opcje.");
	}
	return true;
}

// =====================================================
// Formularze

function NewUserDataCheck() {
	
	var UFlogin = $("#UFlogin");
	var UFloginCheck = $("#UFloginCheck");
	if(UFlogin.val().length==0) {
		return ShowError("Proszę wpisać swój adres email jako login.");
	}else if(!checkEmail(UFlogin.val())) {
		return ShowError("Wpisany adres email wydaje się być błędny");
	}else if(UFlogin.val()!=UFloginCheck.val()) {
		var url = "/index.php?GoDo=CheckLogin&login="+UFlogin.val();
		var check = $.ajax({
			url: url,
			async: false
		}).responseText;
		if(check=='ok') {
			UFloginCheck.val(UFlogin.val());
		}else return ShowError("Niestety podany adres email już istnieje w serwisie...");
	}
	if($("#UFname").val().length==0) return ShowError("Proszę wpisać swoje imię.");
	else if($("#UFsurname").val().length==0) return ShowError("Proszę wpisać swoje nazwisko.");
	else if($("#UFphone").val().length==0) return ShowError("Proszę wpisać telefon kontaktowy.");
	else if($("#UFstreet").val().length==0) return ShowError("Proszę wpisać miejscowość w adresie dostawy.");
	else if($("#UFnumber").val().length==0) return ShowError("Proszę wpisać numer budynku/mieszkania w adresie dostawy.");
	else if($("#UFcity").val().length==0) return ShowError("Proszę wpisać miejscowość w adresie dostawy.");
	else if($("#UFkod1").val().length!=2 || $("#UFkod2").val().length!=3) return ShowError("Proszę wpisać kod pocztowy w adresie dostawy.");
	
	if($("#fvat").attr("checked")) {
		if($("#UFFfirmname").val().length==0) return ShowError("Proszę wpisać imię nazwę firmy w danych do faktury.");
		else if($("#UFFname").val().length==0) return ShowError("Proszę wpisać swoje imię w danych do faktury.");
		else if($("#UFFsurname").val().length==0) return ShowError("Proszę wpisać swoje nazwisko w danych do faktury.");
		else if($("#UFFstreet").val().length==0) return ShowError("Proszę wpisać miejscowość w danych do faktury.");
		else if($("#UFFnumber").val().length==0) return ShowError("Proszę wpisać numer budynku/mieszkania w danych do faktury.");
		else if($("#UFFcity").val().length==0) return ShowError("Proszę wpisać miejscowość w danych do faktury.");
		else if($("#UFFkod1").val().length!=2||$("#UFFkod2").val().length!=3) return ShowError("Proszę wpisać kod pocztowy w danych do faktury.");
		else if($("#UFFnip").val().length!=10) return ShowError("Proszę wpisać NIP w danych do faktury.");
	}
	if($("#pytanie1").val().length==0) return ShowError("Opisz na Swój ośrodek Spa/Gabinet Kosmetyczny.");
	else if($("#pytanie2").val().length==0) return ShowError("Opisz położenie Spa/Gabinetu w Twoim mieście.");
	else if($("#pytanie3").val().length==0) return ShowError("Wybierz rodzaj prowadzonej działalności.");
	else if($("#pytanie4").val().length==0) return ShowError("Ile pomieszczeń liczy Twoje centrum zabiegowe.");
	else if($("#pytanie5").val().length==0) return ShowError("Czy obecne są u Państwa inne linie profesjonalnych produktów ?");
	else if($("#pytanie6").val().length==0) return ShowError("Ile osób pracuje przy wykonywaniu zabiegów ?");
	else if($("#pytanie7").val().length==0) return ShowError("Czy uczestniczyli Państwo w szkoleniach IDI ?");
	else if($("#pytanie8").val().length==0) return ShowError("Jak dowiedzieli się Państwo o Dermalogica ?");
	
	return true;
}

function checkEmail(x){return/^[\w\.-]+@([\w-]+\.)*[a-z]{2,4}$/.test(x)}

// =====================================================

function LoadJs(scriptName) {
	$.ajaxSetup({ async: false });
	$.getScript("/js/" + scriptName + ".js");
	$.ajaxSetup({ async: true });
}

function ShowPictures(id) {
	$.nyroModalManual({
		url: '/'+id+'/pr-zdjecia.html',
		minHeight: 284
	});
}
function ShowError(txt) {
	var content = '<div id="Question"><div class="txt">'+txt+'</div><div class="butt2"><input type="submit" name="" value="Zamknij" class="Button nyroModalClose" /></div></div>';
	$.nyroModalManual({
		closeButton: '',
		modal: true,
   	bgColor: '#000',
		minHeight: 100,
   	content: content
   });
	return false;
}
function ShowQuest(txt,who) {
	var content = '<div id="Question"><div class="txt">'+txt+'</div><div class="butt2"><input type="button" value="Tak" class="Button nyroModalClose" onclick="'+who+'()" /> <input type="submit" name="" value="Nie" class="Button nyroModalClose" /></div></div>';
	$.nyroModalManual({
		closeButton: '',
		modal: true,
   	bgColor: '#000',
		minHeight: 100,
   	content: content
   });
	return false;
}