function start() {
	//start
	
	if(jskomShow&&$('komunikat')) {
		jskomAppear();
		$('komunikat').observe('click',jskomFadeOut);
	}
	
	var txts = $$('[prod_il]');
	txts.each(function(inputEl) {
		inputEl.observe('blur',updateSum);
		inputEl.observe('keyup',updateSum);
	});
}
window.onload = start;

function checkProdukty() {
	var txts = $$('[prod_il]');
	var il = 0;
	txts.each(function(inputEl) {
		if($F(inputEl)>0) il+=$F(inputEl);
	});
	if(il<=0) {
		alert('Prosimy wpisać ilości przy wybranych modelach');
		return false;
	}
	return true;
}

function updateSum(evt) {
	var txts = $$('[prod_il]');
	var id = '';
	var cenaEl;
	var il;
	var cena;
	var podsuma = 0;
	var suma = 0;
	txts.each(function(inputEl) {
		podsuma = 0;
		id=parseInt(inputEl.id.substr(6));
		cenaEl = $('cena_'+id);
		il = inputEl.value;
		cena = cenaEl.value;
		if(il>0&&cena>0) {
			podsuma=il*cena;
			suma+=il*cena;
		}
		$('podsuma_'+id).update(kwota_format(podsuma));
		if(podsuma>0) {
			$('podsuma_'+id).removeClassName('szary');
			$('podsuma_zl_'+id).removeClassName('szary');
		}
		else {
			$('podsuma_'+id).addClassName('szary');
			$('podsuma_zl_'+id).addClassName('szary');
		}
	});
	$('suma').update(kwota_format(suma)+'zł');
}

function kwota_format(kwota) {
	if(kwota<=0) return 0;
	var temp = kwota.toFixed(2).split('.');
	return temp.join(',');
}

function setText(oTextInput) {
	if(oTextInput.value=='') oTextInput.value = oTextInput.defaultValue;
}
function clearText(oTextInput) {
	if(oTextInput.value==oTextInput.defaultValue) oTextInput.value = '';
}

function isEmail(address) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   return(reg.test(address));
}

function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

function selectedRadioValue(oForm, sRadioName) {
	var retval = '';
	for(var i=0;i<oForm.elements.length;i++) {
		if(oForm.elements[i].type!='radio'||oForm.elements[i].name!=sRadioName) continue;
		if(oForm.elements[i].checked) retval = oForm.elements[i].value;
	}
	return retval;
}

function showKom(kom,kom_status) {
	if(kom_status=='error') {
		$('komunikat').setStyle({backgroundColor:'#E1E6FA', border: '5px solid #183152', color:'#183152'});
		$('kom_img').src = 'images/ico_kom_error.png';
	}
	else if(kom_status=='info') {
		$('komunikat').setStyle({backgroundColor:'#E1E6FA', border: '5px solid #183152', color:'#183152'});
		$('kom_img').src = 'images/ico_kom_info.png';
	}
	else {
		$('komunikat').setStyle({backgroundColor:'#E1E6FA', border: '5px solid #183152', color:'#183152'});
		$('kom_img').src = 'images/ico_kom_ok.png';
	}
	$('kom_text').update(kom);
	jskomAppear();
}
var jskomShow=false;
var jskomZostaw=true;
var peJskom;
function jskomAppear() {
	try{
		//Effect.Appear('jskom');
		$('komunikat').show();
	}
	catch (exc) {
		$('komunikat').show();
	}
	if(!jskomZostaw) peJskom = new PeriodicalExecuter(function(peJskom) {
			jskomFadeOut();
		}, 4);
}
function jskomFadeOut() {
	try{
		Effect.Fade('komunikat');
		}
	catch (exc) {
		$('komunikat').hide();
	}
	//$('komunikat').update('');
	if(peJskom) peJskom.stop();
}

var img;
var galFotos = [];
var aktFoto = 0;
function setGal(fot,fotos) {
	if(typeof fotos=='object') galFotos = fotos;
	else galFotos = [];
	$('bigFotoContainer').show();
	aktFoto = 0;
	if(galFotos.length>0) {
		for(var i=0;i<galFotos.length;i++) {
			if(galFotos[i]==fot) aktFoto = i;
		}
	}
	pokazFotoGal(fot);
}
function pokazFotoGal(fot) {
	$('bigFotoImg').update('<img src="images/czekaj_pl.gif" border="0" />')
	img = new Element('img');
	img.id = 'theimage';
	img.observe('load', showimg);
	//document.body.appendChild(img);
	img.src = fot;
	//$('bigFotoImg').update('<img src="'+fot+'" border="0" />';
	$('bigFotoImg').show();
	fotoNav();
	//$('main').hide();	
}
function pokazFoto(fot) {
	galFotos = [];
	$('bigFotoImg').update('<img src="images/czekaj_pl.gif" border="0" />')
	img = new Element('img');
	img.id = 'theimage';
	img.observe('load', showimg);
	//document.body.appendChild(img);
	img.src = fot;
	//$('bigFotoImg').update('<img src="'+fot+'" border="0" />';
	$('bigFotoImg').show();
	$('bigFotoContainer').show();
	//$('main').hide();
	aktFoto = 0;
	fotoNav();
}
function showimg(evt) {
	$('bigFotoImg').hide();
	$('bigFotoImg').update(img);
	$('bigFotoImg').show();
	var dim = document.viewport.getDimensions();
	if(img.width>dim.width||img.height>dim.height-160) {
		var new_size = image_small_maker(img.width, img.height, dim.width, dim.height-160)
		img.setStyle({'width':new_size[0]+'px','height':new_size[1]+'px'});
	}
}
function wywalFoto() {
	$('bigFotoContainer').hide();
	$('bigFotoImg').hide();
	//$('main').show();
}
function fotoNav() {
	if(aktFoto>0) $('bigFotoPrev').show();
	else $('bigFotoPrev').hide();
	if(aktFoto<galFotos.length-1) $('bigFotoNext').show();
	else $('bigFotoNext').hide();
}
function prevFoto() {
	if(aktFoto<=0) return;
	aktFoto--;
	pokazFotoGal(galFotos[aktFoto]);
}
function nextFoto() {
	if(aktFoto>galFotos.length-1) return;
	aktFoto++;
	pokazFotoGal(galFotos[aktFoto]);
}
function image_small_maker(orig_width, orig_height, max_width, max_height) {
	var xRatio = max_width / orig_width; 
	var yRatio = max_height / orig_height; 
	var new_width = 0;
	var new_height = 0;
	if ( (orig_width <= max_width) && (orig_height <= max_height) ) { 
	 new_width = orig_width; 
	 new_height = orig_height; 
	} 
	else if ((xRatio * orig_height) < max_height) { 
	 new_height = Math.ceil(xRatio * orig_height); 
	 new_width = max_width; 
	} 
	else { 
		new_width = Math.ceil(yRatio * orig_width); 
		new_height = max_height; 
		}
	var output = [];
	output.push(Math.round(new_width));
	output.push(Math.round(new_height));
	return output;
}

function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function setCookie(c_name,value,expiredays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    {
    c_start=c_start + c_name.length+1;
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    }
  }
return "";
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}
var kc_tab= [];
function toggle_kat_cont(kci,kkci) {
	if (kc_tab[kci]==1) {
		$(kci).hide();
		$('kat_cont_arr_d_'+kkci).show();
		$('kat_cont_arr_u_'+kkci).hide();		
		kc_tab[kci]=0;
	}	
	else {
		$(kci).show();
		$('kat_cont_arr_u_'+kkci).show();
		$('kat_cont_arr_d_'+kkci).hide();		
		kc_tab[kci]=1;
	}	
}
var polecane_tab=[];
var aktPolecaneId=1;
function polecaneDeclare(pid) {
	polecane_tab.push(pid);
}
function setPolecane(pid) {
	
	if (aktPolecaneId!=pid) {
		for(var i=0;i<polecane_tab.length;i++) {
			$('polecane_'+polecane_tab[i]).hide();
			$('polecane_tool_'+polecane_tab[i]).removeClassName('button_active');
			$('polecane_tool_'+polecane_tab[i]).addClassName('button');
		}
		aktPolecaneId=pid;
		$('polecane_'+pid).show();
		$('polecane_tool_'+pid).removeClassName('button');
		$('polecane_tool_'+pid).addClassName('button_active');
	}		
}
