/**
 * @author Marceli Janowski <marceli@mjdesign.pl>
 * @version 1.0
 */
$(document).ready(function()
{
	if($('#orderForm').length)
	{
		var price = $('#prince_brutto').val().replace(' zł','');
		var prince_package = $('#prince_brutto_package').val();
		var re = /^[1-9][0-9]?$/;
		
		$('#count').keyup(function()
		{
			var count = $('#count').val();
			if (!re.test(count))
			{
				var count = count.substring(0,[count.length-1]);
				$('#count').val(count);			
			}
			var prince_brutto_count = (price*$('#count').val()).toFixed(2);
			var whole_prince = (parseFloat(prince_brutto_count)+ parseFloat(prince_package)).toFixed(2);
			$('#prince_brutto_count').val(prince_brutto_count+' zł');
			if(count < 1)
				whole_prince = "0";
			$('#prince_brutto_all').val(whole_prince+' zł');
			
		});
		SetReqFields('orderForm');
		//CheckReqFields('orderForm');
		
		var options = 
		{ 
	        target:        'div.orderReady',    // target element(s) to be updated with server response 
	        beforeSubmit:  showRequest,  		// pre-submit callback 
	        success:       showRes,  		// post-submit callback 
	        //other available options: 
	        //url:       url         // override for form's 'action' attribute 
	        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
	        dataType:  'json',        // 'xml', 'script', or 'json' (expected server response type) 
	        clearForm: true       // clear all form fields after successful submit 
	        //resetForm: true        // reset the form after successful submit 
	        // $.ajax options can be used here too, for example: 
	    }; 
 		$('#orderForm').ajaxForm(options);
		
	}
	
	$('#contentBlockSubpage a').each(function()
	{
		var a_href = $(this);
		a_href.click(function()
		{
			switch(a_href.attr('class'))
			{
				case 'contentBoxImgPlus': {
												$('div .boxContentLeft p').css('font-size','12px');
												$('div .boxContentLeft table').css('font-size','12px');
												$('div .boxContentLeft li').css('font-size','12px');
												$('div .boxContentLeft h2').css('font-size','14px');
												break;
										  }
				case 'contentBoxImg':	  {
												$('div .boxContentLeft p').css('font-size','11px');
												$('div .boxContentLeft table').css('font-size','11px');
												$('div .boxContentLeft li').css('font-size','11px');
												$('div .boxContentLeft h2').css('font-size','12px');
												break;
										  }
				case 'contentBoxImgMinus':{
												$('div .boxContentLeft p').css('font-size','10px');
												$('div .boxContentLeft table').css('font-size','10px');
												$('div .boxContentLeft li').css('font-size','10px');
												$('div .boxContentLeft h2').css('font-size','11px');
												break;
										  }
				case 'contentBoxImgPrint':{
												window.print();return false;
												break;
										  }
			}
		})	
	});
});
function CheckReqFields(form)
{
	var val = true;
	var input;
	$('#'+form+' input.req').each(function()
	{
		input = trim($(this).val());
		if(!input.length)
			val = false;
	});
	return val;
}
function SetReqFields(form)
{
	
	$('#'+form+' input.req').each(function()
	{
		$(this).after("<sup>*</sup>");
	});
}
function showRequest(formData, jqForm, options)
{
	
	if (!CheckReqFields('orderForm')) {
		$('#orderReady').html('Proszę o uzupełnienie pól obowiązkowych.').removeClass("orderReady").addClass("orderError").fadeIn('slow');
		return false;
	}
	else 
	{
		var prince_brutto_count = $('#prince_brutto_count').val();
		var prince_brutto_all = $('#prince_brutto_all').val();
		var prince_brutto_package = $('#prince_brutto_package').val();
		
		formData.push({name:'prince_brutto_count',value:prince_brutto_count});
		formData.push({name:'prince_brutto_all',value:prince_brutto_all});
		formData.push({name:'prince_brutto_package',value:prince_brutto_package});
		return true;
	}
}
function showRes()
{
	
	$('#orderReady').html('Zamówienie zostało przyjęte do realizacji. Potwierdzenie zamówienia zostało wysłane na adres e-mail podany w formularzu.').removeClass("orderError").addClass("orderReady").fadeIn('slow');
}
function wybor(element)
{

	 jQuery.facebox({ ajax: '/apteki/'+element+',apteka.html' }) 
} 
function trim(str) 
{
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}
