/* ---- funkcje pomocnicze dla formularzy zamówień ---- */
window.addEvent('domready', function(){

	//CuFon (sfir)
	Cufon.set('fontFamily', 'DaxlinePro-Regular').replace('h1')('h2')('h3');
	Cufon.now();

	//JMB patent na title
	$$('.ikonki_materacy img').each(function(el){
		el.set('title', el.get('alt'));
	});

	if ($('zamawiamForm')) {
		$('fveventOn').addEvent('change', function(){$$('.fdisabled').each(function(el) {el.disabled=false;});
		getInvoiceAddress();
		});
		$('fveventOff').addEvent('change', function(){$$('.fdisabled').each(function(el) {el.disabled=true;});});
		$('fillForm').addEvent('click', function(){
			if ($('fveventOn').checked == true) {
				$('fv_nazwa').value = $('ad_firma').value;
				$('fv_ulica').value = $('ad_ulica').value;
				$('fv_numer_domu').value = $('ad_numer_domu').value;
				$('fv_numer_lokalu').value = $('ad_numer_lokalu').value;
				$('fv_kod').value = $('ad_kod').value;
				$('fv_miasto').value = $('ad_miasto').value;
				$('fv_nip').value = '';
				$('fillForm').value = $('ad_kraj').value;
			}
		});
		if ($('fveventOn').checked == true) {
			$$('.fdisabled').each(function(el) {el.disabled=false;});
		}

		new FormCheck('zamawiamForm');
		var el = $('zamawiamForm');
		el.getElements('input[name=client_address_id]').addEvents({
			'click': function()
			{
				getDeliveryAddress (this.value);
			}
		});
	}

	if ($('loginForm')) {
		new FormCheck('loginForm');
	}

	if ($('chpassForm')) {
		new FormCheck('chpassForm');
	}

	if ($('addressForm')) {
		new FormCheck('addressForm');
	}

	if ($('addNewAddress')) {
		$('addNewAddress').addEvent('click', function(){
			$('addressFormBox').setStyle('display', 'block'); return false;});
	}

	if ($('delAddress')) {
		var addressId = 0;
		var el = $('addressForm');
		el.getElements('input[name=client_address_id]').addEvents({'click': function() {
			addressId = this.value;
		}});
		$('delAddress').addEvent('click', function(){
			if (addressId !=0) {
				$('addressId').value = addressId;
				$('akcja').value = 'delAdress';
				$('addressForm').submit();

			} else {
				alert ('Proszę zaznaczyć adres do usunięcia');
			}
			return false;});
	}

	if ($('updateCart')) {
		$('updateCart').addEvent('click', function(){
			$('cartForm').submit();
		});
	}

	if ($('editAddress')) {
		var addressId = 0;
		var el = $('addressForm');
		el.getElements('input[name=client_address_id]').addEvents({'click': function() {
			addressId = this.value;
		}});
		$('editAddress').addEvent('click', function(){
			if (addressId !=0) {
				getDeliveryAddress (addressId);
				$('addressId').value = addressId;
				$('akcja').value = 'updateAdress';
				$('addressFormBox').setStyle('display', 'block');
			} else {
				alert ('Proszę zaznaczyć adres do edycji');
			}
			return false;});
	}

	if ($('loginFormHeader')) {
		new FormCheck('loginFormHeader');
		var lfh = $('emailHeadForm');
		var phf = $('passwordHeadForm');
		if (Browser.Engine.trident != true) phf.value == '' ? 'Twoje hasło...' : phf.setProperties({ type: 'text'});
		lfh.addEvents({
			'focus': function() { lfh.value = (lfh.value == 'Twój login...' ? '' : lfh.value);  },
			'blur': function()  { lfh.value = (lfh.value == '' ? 'Twój login...' : lfh.value);  }
		});
		phf.addEvents({
			'focus': function() { phf.value = (phf.value == 'Twoje hasło...' ? '' : phf.value); phf.setProperties({ type: 'password'}); },
			'blur': function()  { phf.value = (phf.value == '' ? 'Twoje hasło...' : phf.value); if (phf.value == 'Twoje hasło...' && Browser.Engine.trident != true) phf.setProperties({ type: 'text'}); }
		});

	}

	if ($('registerForm')) {
		new FormCheck('registerForm');
	}

	function sekunda(oldcolor) {
		var shf = $('smallSearch');
		shf.focus();
		shf.set('style', 'color:'+oldcolor);
	}

	if ($('smallSearch')) {
		var shf = $('smallSearch');
		shf.addEvents({
			'focus': function() { shf.value = (shf.value == 'Szukany produkt...' ? '' : shf.value);  },
			'blur': function()  { shf.value = (shf.value == '' ? 'Szukany produkt...' : shf.value);  }
		});
		var ssf = $('smallSearchForm');

		ssf.addEvents({
			'submit': function() { if (shf.value == 'Szukany produkt...') {

				var oldcolor = shf.getStyle('color');
				shf.set('style', 'color:red');
				setTimeout(function() {shf.focus();shf.set('style', 'color:'+oldcolor);},1000);


				return false;
			} else {
				$('smallSearchForm').submit();
			}
			}
		});

	}
	if ($('validate_form')) {
		new FormCheck('validate_form');
	}

	if ($('newsletter_form')) {
		new FormCheck('newsletter_form');
	}
	if ($('newsletter_unsubscribe_form')) {
		new FormCheck('newsletter_unsubscribe_form');

	}
	if ($('newsletter_miniform')) {
		new FormCheck('newsletter_miniform');
	}


	if ($('opis_produktu') || $('galeriaBox')) {
		//new QuickBox();
	}

	if ($('slider')) {
		var nS2 = new noobSlide({
			box: $('slider'),
			items: [0,1,2],
			interval: 4000,
			autoPlay: false,
			//			size: 881,
			size:535,
			fxOptions: {
				duration: 1000,
				transition: Fx.Transitions.Cubic.easeOut,
				wait: false
			},

			addButtons: {
				previous: $('addbkwd'),
				next: $('addfwd')
			}
		});
	}

	if ($('pulldown1')) {
		generatePollDown('pulldown1');
	}

});

function getDeliveryAddress (id) {
	var jsonRequest = new Request.JSON({url: appUrl + "shop/jsonGetDeliveryAddress", onSuccess: function(dataObj){
		var address = dataObj[0];
		for(var p in address){
			if ($('ad_'+p))	$('ad_'+p).value = address[p];
		}

	}}).get({'addressId': id});
}

function getInvoiceAddress () {
	var jsonRequest = new Request.JSON({url: appUrl + "jsonGetInvoiceAddress", onSuccess: function(dataObj){
		var address = dataObj;
		for(var p in address){
			if ($('fv_'+p))	$('fv_'+p).value = address[p];
		}

	}}).get({});
}


/* ---- funkcje pomocnicze do validatora formularza ---- */
function kodpocztowyCheck(el){
	if (!el.value.test(/^[0-9]{2}\-[0-9]{3}$/)) {
		el.errors.push("Podaj prawidłowy kod pocztowy (np. 80-180)");
		return false;
	} else {
		return true;
	}
}

function nipCheck(el){
	if (!el.match( /^[0-9]{3}-[0-9]{2}-[0-9]{2}-[0-9]{3}$/ )) {
		return false;
	}
	var ar = el.replace(/-/g,'');
	var arg = el.replace(/-/g,'');
	var coefficients = "657234567";
	var sum=0;
	var index=8;
	for (index=8; index>=0 ;index--)if( rr==false)
	sum += (parseInt(coefficients.charAt(index)) * parseInt(arg.charAt(index)));
	if ( (sum % 11) == 10 ? false : ((sum % 11) == parseInt(arg.charAt(9)))) return true;
	return false;
}

/* ---- funkcje do debugu skryptów ---- */
function print_r(theObj){
	if(theObj.constructor == Array ||
	theObj.constructor == Object){
		document.write("<ul>")
		for(var p in theObj){
			if(theObj[p].constructor == Array||
			theObj[p].constructor == Object){
				document.write("<li>["+p+"] => "+typeof(theObj)+"</li>");
				document.write("<ul>")
				print_r(theObj[p]);
				document.write("</ul>")
			} else {
				document.write("<li>["+p+"] => "+theObj[p]+"</li>");
			}
		}
		document.write("</ul>")
	}
}

function generatePollDown(obj) {
	$(obj+'_options')._state = 'close';
	$(obj).addEvents({
		'click': function() {
			if ($(obj+'_options')._state == 'open') {
				closePopup(obj);
			} else {
				var wysokosc = window.getScrollHeight();
				var szerokosc = window.getScrollWidth();
				$(obj+'_bg').setStyle('width',szerokosc+'px');
				$(obj+'_bg').setStyle('height',wysokosc+'px');
				$(obj+'_options').setStyle('display','block');
				$(obj+'_options')._state = 'open';
			}
		}
	});
	$(obj+'_bg').addEvents({
		'click': function() {
			closePopup(obj);
		}
	});
}

function closePopup(obj) {
	if ($(obj+'_options')._state == 'open') {
		$(obj+'_options').setStyle('display','none');
		$(obj+'_options')._state = 'close';
		$(obj+'_bg').setStyle('width','0');
		$(obj+'_bg').setStyle('height','0');
	}
}

function addItem(obj,id,value,image) {
	$(obj+'_value').value=id;	
	$(obj+'_text').value=value;	
	$(obj+'_image').src=image;	
	closePopup(obj);
}