function validField( f,d ){
	validFieldAb(f,d,Rfp.links.fm+'action/shop.system.core/axform/');
}
function cpValidField(f,d){
	validFieldAb(f,d,Rfp.links.fm+'action/shop.client.core/axform/');
}

function expandAddress(check,card,vf){
	var el=$('#'+card);
	vf = vf || 'validField';
	if(check.checked){
		el.slideDown();
	}else{
		el.slideUp();
	}
	this[vf](check,{name:check.name,value:check.checked});
}

function validFieldAb(f,d,url){
	d=d||f;
	//globalWait=true;
	$.getJSON( url, {field:d.name,value:d.value},
		function(ob){
			var act=ob.acts[0],
				el=$(f),
				par=el.parent('.valid');
			if(par){
				var icon=par.children('.icon');
				if(icon)
					icon.remove();
				if(act){
					if(act.valid==false){
						el.css('border','#D10301 1px solid');
						par.append('<em class="icon novalid"></em>');
					}else{
						el.css('border','#eceaea 1px solid');
						par.append('<em class="icon valid"></em>');
					}
				}
			}
		});
}

function basketSupplyAct(e){
	$.getJSON( Rfp.links.fm+'action/shop.system.core/axsupply/'
		,{supply:e.value}
		,function(ob){
			$(ob.elems).each(refreshElems);
			//Ext.each(ob.elems,refreshElems);
		});
}

function basketPaymentAct(e){
	$.getJSON( Rfp.links.fm+'action/shop.system.core/axpayment/'
			,{payment:e.value}
			,function(ob){
				$(ob.elems).each(refreshElems);
				//Ext.each(ob.elems,refreshElems);
			});
}

function refreshElems(index,it){
	var pr=$('#'+it.id),
		el=$(pr).children('.content'),
		ne=$(el).html(it.html);
	$(pr).css('backgroundColor','#ffffbb').animate({backgroundColor:"#ffffff"},'slow');
}

function submitProductForm(quantity)
{
	var fplatnosci = $('input:radio[name=fplatnosci]:checked').length > 0 ?$('input:radio[name=fplatnosci]:checked').val():null ;
	var kdostawy = $('input:radio[name=kdostawy]:checked').length > 0 ?$('input:radio[name=kdostawy]:checked').val():null ;
	if(kdostawy && fplatnosci)
	{
		$("#productOrderSettingsForm input[name=formaplatnosci]").val(fplatnosci);
		$("#productOrderSettingsForm input[name=kosztdostawy]").val(kdostawy);
	}
	$("#productOrderSettingsForm input[name=quantity]").val(quantity) ;
	$("#productOrderSettingsForm").submit();
	
}

function ffAddProdToBasket(dumpId,option)
{
	option = option == undefined?0:1;
	var quantity = $("#"+dumpId).val();
	if(quantity == '' )
	{
		alert("Nie podales ilosci");
		
	}
	else if(option)
	{
		var sOptions = $('input:radio[name=option]:checked').length > 0 ?$('input:radio[name=option]:checked').attr('val'):null ;
		if( sOptions ===  null)
			alert("Musisz wybrac opcje");
		else
		{
			$("#productOrderSettingsForm").append("<input type='hidden' name='optionnr' value='"+sOptions+"' />");
			submitProductForm(quantity);
		}
	}
	else
		submitProductForm(quantity);
}

$(document).ready(function(){  
	if($.fn.lightBox)
		$('a.jql').lightBox();
	selectShopCat();
});


function selectShopCat()
{
	$('#shop_cattegories_main').change( function(){
	  var value = 	$(this).val();
	  
	  $.ajax({
		  url: Rfp.links.fm+'action/ff/catShop',
		  data:{id:value},
		  dataType: 'json',
		  success: function(data) {
			  $('#shop_cattegories_sub').html('').append("<option value='0'>Brak</option>");
			  $.each(data, function(key,value){
				  $('#shop_cattegories_sub').append("<option value='"+value['id']+"'>"+value['rfptitle']+"</option>"); 
			  } )
		  }
		});
	  
	  
	});
	
}

function addToBasket(d){
	var f=document.productOrderSettingsForm,
		c=f.mainquantity.value;
	if(c>d){
		alert("Dostępna ilość produktu to "+d+" szt.");
	}else{
		f.typ.value='m';
		f.submit();
	}
}

function addToBasketO(d){
	var f=document.productOrderSettingsForm,
		c=f.mainquantity.value,
		ch=[],
		sum=0;
	$(".optionCheck:checked").each(function(){
		var v=$(this).attr('val'),
			q=$("#quant"+v).val();
		if(q=="")q=1;
			sum+=parseInt(q);});
	if(sum>d){
		alert("Dostępna ilość produktu to "+d+" szt.");
	}else{
		f.typ.value='o';
		f.submit();
	}
}

function ffregister(f){
	var info = "",
		email = f.email.value,
		emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	if(!emailPattern.test(f.email.value))
		info = "Należy podać poprawny adres email.\n";
	if(f.password.value.length<6)
		info += "Hasło powinno składać się z minimum 6 znaków.\n";
	if(f.password_repeat.value!=f.password.value)
		info += "Pole 'Hasło' i 'Powtórzenie hasła' nie są jednakowe.\n";
	if(!f.check.checked)
		info += "Należy zaakceptować warunki regulaminu.\n";
	if(info=="")
		f.submit();
	else
		alert( info );
}

function sendClientMail(f,s){
	var info ="",
		err = 0;
	if( f.subject.value=="" || f.email.value=="" || f.content.value=="")
		alert( "Proszę wypełnić wszystkie pola." );
	else
		if( s==1 && (f.name.value=="" || f.street.value=="" || f.city.value=="") )
			alert( "Proszę wypełnić wszystkie pola." );
		else
			f.submit();
}

function sendMPMail(f){
	var info ="",
		err = 0;
	if( f.city.value=="" || f.phone.value=="" || f.email.value=="" || f.content.value=="")
		alert( "Proszę wypełnić wszystkie pola." );
	else
		f.submit();
}

function profileSave(f){
	var info ="",
		err = 0;
	if( f.publish.checked && ( f.name.value=="" || f.street.value=="" || f.city.value=="" || f.postcode.value=="" || f.maincat.value==0 || f.nip.value=="" || f.phone1.value=="" ) ){
		alert( "Aby możliwe było opublikowanie wizytówki\nnależy wypełnić pola oznaczone gwiazdką (*)." );
	}else{
		if( !checkNIP(f.nip.value) ){
			alert( "Proszę wprowadzić poprawny numer NIP" );
		}else{
			f.submit();
		}
	}
}

function productSave(f){
	var info ="",
		err = 0;
	if( f.publish.checked && ( f.title.value=="" || f.realization.value=="" || f.amount.value=="" || f.left.value=="" || f.price.value=="" ) ){
		alert( "Aby możliwa była publikacja produktu\nnależy wypełnić pola oznaczone gwiazdką (*)." );
	}else{
		f.submit();
	}
}

function checkNIP(value){
	var verificator_nip = new Array(6,5,7,2,3,4,5,6,7); var nip = value.replace(/[\ \-]/gi, ''); 
	if (nip.length != 10)  { return false; } else  {
	var n = 0;
	for (var i=0; i<9; i++) {	n += nip[i] * verificator_nip[i]; }
	n %= 11;
	if (n != nip[9]) { return false; }
	}
	return true;
}

function sendOrder(f,url){
	var info ="",
		err = 0,
		name = $("#buyname").val(),
		street = $("#buystreet").val(),
		postcode = $("#buypostcode").val(),
		city = $("#buycity").val(),
		phone = $("#buyphone").val(),
		email = $("#buyemail").val();
		
	if(name=="")
		err++;
	if(street=="")
		err++;
	if(city=="")
		err++;
	if(email=="" || phone=="")
		err++;
	if( err>0 ){		
		info="Proszę wypełnić wymagane pola oznaczone gwiazdką.";
		alert( info );
	}else{
		f.bname.value=name;
		f.bstreet.value=street;
		f.bpostcode.value=postcode;
		f.bcity.value=city;
		f.bemail.value=email;
		f.bphone.value=phone;
		f.action=url;
		f.submit();
	}
}
/*
$('.custItem').ready(function($) {
	
			$.each( $('[gg]'),function(index,value){
				
				var icon = $(value).attr('icon');
				var nr = $(index).attr('gg');
				var link = "http://status.gadu-gadu.pl/users/status.asp?id=8334&styl=2";
				$.get("http://status.gadu-gadu.pl/users/status.asp",{ id: "8334", styl: "2" } , function(data) {
					 console.log(data);

				})
				$.ajax({ url: "http://status.gadu-gadu.pl/users/status.asp?id=8334&styl=2",type:'GET', success: function( data ){
					alert('asd');
		        }});				
//				$.ajax({ url: "localhost/wordpress",global:true, success: function(){
//						alert('asd');
//			      }});
				
			} )
	
});
*/



