// JavaScript Document
//send email using jquery
$(function() {  
    $('#submit_button').click(function() {  
		// validate
		if ($('#taxa_transport').val()==0) {
			alert ('Nu ai selectat modalitatea de livrare!');
			$('#taxa_transport').focus().select();
			return false;
		}
		if ($('#order_nume').val()=='') {
			alert ('Nu ai precizat numele!');
			$('#order_nume').focus().select();
			return false;
		}
		if ($('#order_prenume').val()=='') {
			alert ('Nu ai precizat prenumele!');
			$('#order_prenume').focus().select();
			return false;
		}
		if ($('#order_phone').val()=='') {
			alert ('Nu ai precizat numarul de telefon!');
			$('#order_phone').focus().select();
			return false;
		} else {
			var filter=/^[0-9]+$/;
			if (filter.test($('#order_phone').val())) {
			} else {
				alert('Numarul de telefon nu este valid!');
				$('#order_phone').focus().select();
				return false;
			}
		}
		if ($('#order_email').val()=='') {
			alert ('Nu ai precizat adresa de e-mail!');
			$('#order_email').focus().select();
			return false;
		} else {
			var filter=/^.+@.+\..{2,3}$/;/^[0-9]+$/
			if (filter.test($('#order_email').val())) {
			} else {
			//if ($('#order_email').match(filter)==false) {
				alert ('Adresa de email nu este valida');
				$('#order_email').focus().select();
				return false;
			}
		}
		if ($('#order_str').val()=='') {
			alert ('Nu ai precizat strada!');
			$('#order_str').focus().select();
			return false;
		}
		if ($('#order_no').val()=='') {
			alert ('Nu ai precizat numarul de strada!');
			$('#order_no').focus().select();
			return false;
		}
		if ($('#order_loc').val()=='') {
			alert ('Nu ai precizat localitatea!');
			$('#order_loc').focus().select();
			return false;
		}
		if ($('#order_jud').val()=='') {
			alert ('Nu ai precizat judetul!');
			$('#order_jud').focus().select();
			return false;
		}
		if ($('#order_cp').val()=='') {
			alert ('Nu ai precizat codul postal!');
			$('#order_cp').focus().select();
			return false;
		}  else {
			var filter=/^[0-9]+$/;
			if (filter.test($('#order_cp').val())) {
				if ($('#order_cp').val().length==6) {
				} else {
					alert('Codul postal trebuie sa fie din 6 cifre!');
					$('#order_cp').focus().select();
					return false;
				}
			} else {
				alert('Codul postal nu este valid!');
				$('#order_cp').focus().select();
				return false;
			}
		}
		
		//and process form here  
		
		
		var name = $("#order_name").val();  
		var phone = $("#order_phone").val();  
		var email = $("#order_email").val();  
		var str = $("#order_str").val();
		var no = $("#order_no").val();
		var bloc = $("#order_bloc").val();
		var apt = $("#order_apt").val();
		var loc = $("#order_loc").val();
		var jud = $("#order_jud").val();
		var cp = $("#order_cp").val();
		
		
		
		
		var dataOrder = 'name='+name+'&phone='+phone+'&email='+email+'&strada='+str+'&numar='+no+'&bloc='+bloc+'&apartament='+apt+'&localitate='+loc+'&judet='+jud+'&cod_postal='+cp;
		//alert (dataOrder); //exit();
		
		
		//send form [dataOrder variable] to process file
		/*$.ajax({  
		  type: "POST",  
		  url: "procesare.php",  
		  data: dataOrder,  
		  success: function() {  
			/*
			$('#contact_form').html("<div id='message'></div>");  
			$('#message').html("<h2>Contact Form Submitted!</h2>")  
			.append("<p>We will be in touch soon.</p>")  
			.hide()  
			.fadeIn(1500, function() {  
			  $('#message').append("<img id='checkmark' src='images/check.png' />");  
			});
			*/
		/*  }  
		});  
		return false; */

	});
});  





//lightbox
/*$(function() {
	// Use this example, or...
	//$('a[@rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
	// This, or...
	//$('#gallery a').lightBox(); // Select all links in object with gallery ID
	// This, or...
	$('a.lightbox').lightBox(); // Select all links with lightbox class
	// This, or...
	//$('a').lightBox(); // Select all links in the page
	// ... The possibility are many. Use your creative or choose one in the examples above
});*/






// init tooltip
function init_tooltip() {$('.tooltip').tooltip({showURL:false,top:20,left:0,delay:200,track:true});}



// change count event
function cc(){
	var totalcount_f=0,
		//fullprice_f=price_f[price_f.length-1][1],
		fullprice_f=10,
		currentprice_f=0,
		totalprice_f=0,
		totalcount_c=0,
		fullprice_c=13,
		//fullprice_c=price_c[price_c.length-1][1],
		currentprice_c=0,
		totalprice_c=0,
		
		total_colet=0;
		//alert(price_f[price_f.length-1][1]);
		
	
	
	//codu meu pt butoanele de stergere
	$('.delete_btn_f').click(function() {
		var j = $(this).attr('id');
		//alert(j); exit();
       	var text = 0;
		$('#c'+j).val(text);
		$('#c'+j).append(cc());		
	});
	$('.delete_btn_c').click(function() {
		var j = $(this).attr('id');
		//alert(j); exit();
       	var text = 0;
		$('#c'+j).val(text);
		$('#c'+j).append(cc());		
	});
	//end codu meu pt butoane stergere
	
	
	
	$('.count_f').each(function(i){
		$a=$(this).val(count=parseInt($(this).val().replace(/[^0-9]/g,'').replace(/^$/g,'0')));
		totalcount_f+=count;
		
		//var price=count*fullprice_f;
		var price=count*$('#price_f_'+i).val(); //a mea
											 
		
		totalprice_f+=price;
		$(this).parent().next().html(price.toFixed(2)+' RON');
	});
	
	var reducedprice_f=totalcount_f*currentprice_f;
	$('#totalcount_f').html(totalcount_f);
	$('#totalprice_f').html(totalprice_f.toFixed(2)+' RON');
	
	$('.count_c').each(function(i){
		$(this).val(count=parseInt($(this).val().replace(/[^0-9]/g,'').replace(/^$/g,'0')));
		totalcount_c+=count;
		
		//var price=count*fullprice_c;
		var price=count*$('#price_c_'+i).val(); //a mea
		
		totalprice_c+=price;
		$(this).parent().next().html(price.toFixed(2)+' RON');
	});
	
	var reducedprice_c=totalcount_c*currentprice_c;
	$('#totalcount_c').html(totalcount_c);
	$('#totalprice_c').html(totalprice_c.toFixed(2)+' RON');
	
	//var count_promo=Math.min(totalcount_f,totalcount_c);
	//var reducedprice=count_promo*price_promo+(totalcount_f-count_promo)*currentprice_f+(totalcount_c-count_promo)*currentprice_c;
	var totalprice=totalprice_f+totalprice_c;
	
	//ale mele
	var taxare=($('#taxa_transport').val());
	
	total_colet=totalprice+parseFloat(taxare);
	
	//alert(total_colet);
	//end ale mele
	
	$('#totalprice').html(totalprice.toFixed(2)+' RON');
	
	
	//ale mele
	$('#taxa_livrare').html(taxare+' RON');
	$('#totalcolet').html(total_colet.toFixed(2)+' RON');
	//end ale mele
	
	//$('#orderdetails').toggle(totalprice>0);
	//$('#transport_cost').toggle(totalprice>0&&totalcount_f+totalcount_c<300);
	
	
}


// document ready
$(document).ready(function(){init_tooltip();cc();$('.autofocus:first:visible').focus();});
