$(document).ready(function(){
	
	$("a[rel^='prettyPhoto']").prettyPhoto();
	
	/*
	// float offers
	var qr = $('#query-result');
	if(qr.length) {
		$(window).scroll(function(e){
			resultsTop	= Math.round(qr.offset().top);
			scrollTop 	= $(this).scrollTop();
			if(resultsTop - scrollTop < 0) {
				qr.stop(true,true).animate({paddingTop:Math.abs(resultsTop-scrollTop)});
			} else {
				qr.stop(true,true).animate({paddingTop:0});
			}
			//console.log(resultsTop - scrollTop);
		});
	}
	*/	
		
	// replace some select tags
	$('#subheader form div.select select, #daune-form div.select select').css({opacity:'0.001'}).change(function(){
		$(this).parent().find('.replace span.value').html($(this).find('option:selected').text());
	});
	$('#daune-form div.select select').change(function(){
		if($('#daune-form div.select select option:selected').val()!=""){
			location.href = $('#daune-form div.select select option:selected').val();
		}
	});
	$('#daune-form div.select select').parent().find('.replace span.value').html($('#daune-form div.select select').find('option:selected').text());
	
	/* Abonarea la nl */
	$('#side_newsletter_email').click(function(){
		$(this).val('');
	});
	
	$('#newsletter_email').click(function(){
		if($('#side_newsletter_email').val()!=""){
			$.ajax({
				url: url_base+"ajax/newsletter.php",
				cache: false,
				type: "POST",
			    data: ({			    	
			    	email: $('#side_newsletter_email').val()
			    }),
			    async:"true",
				success: function(data) {
					$('#nlmsg').html(data);
				}
			});
		}else{
			alert('Scrie adresa ta de email!');
		}
	});
	
	
	// side equal to content
	
	sh = $('#content .side').height();
	ch = $('#content').height();
	if(sh < ch) {
		$('#content .side').css("min-height",ch);
	}
	
	$('div.field.datepicker input[type=text]').css({cursor:'pointer'}).datepicker({
		dateFormat:'yy-mm-dd',
	});
	
	$('#DataPolita').css({cursor:'pointer'}).datepicker({
		dateFormat:'yy-mm-dd',
		minDate:1
	});
	$('#DataLivrare').css({cursor:'pointer'}).datepicker({
		dateFormat:'yy-mm-dd',
		minDate:0
	});
	$('#Permis').css({cursor:'pointer'}).datepicker({
		dateFormat:'yy-mm-dd',
		maxDate:-1,
		changeYear:true,
		changeMonth:true,
		yearRange:'1930:+'
	});
	$('#memento-date').css({cursor:'pointer'}).datepicker({
		dateFormat:'yy-mm-dd',
		minDate:1
	});
	

	
	// faq
	$('.faq .content').hide();
	$('.faq h3').toggle(function(){
		$(this).addClass('active').parent().find('.content').slideDown();
	},function(){
		$(this).removeClass('active').parent().find('.content').slideUp();
	}).eq(0).click();
	
	
	
	// onload show tips below title (on form side)
	$('.side.form .sys-info').hide();
	$('.side.form .sys-info.tip').slideDown();
	
	// home make selects invisible
	$('#subheader select, #daune select').css({opacity:.01});
	
	// adjust inner side's height
	var side = $('.inner #content .side:not(.form)');
	var content = $('#content');
	//if(side.height < content.height())
	if(side.height() < content.height()) side.height(content.height());
	
	
	$('.asirom').show();
	
	$('#moreCalc').click(function(){
		$(this).hide();
		$(this).parent().find('span').show();
	});
	
	
/**
 * Compare js code
 */	
	// some form precessing
	// show optional fields (for auto2)
	$('#opt-auto2').change(function(e){
		if($(this).is(':checked')) {
			$(this).parent().parent().find('.optional').slideDown().find('input,select').attr('disabled',false);
		} else {
			$(this).parent().parent().find('.optional').slideUp().find('input,select').attr('disabled',true);
		}
	});
	
	
	// ajax call at trigger
	//ajaxTriggers = $('#calc-step-1 .field input.trigger, #calc-step-1 .field select.trigger');
	ajaxTriggers = $('#calc-step-1 #TipRCA, #calc-step-1 #TipPersoana, #calc-step-1 #CapCil'); 
	
	if(useSave==1){
		$('#offerRecalc').click(function(){
			if($("#acc-term").is(':checked')){
				if($("#Nume").val()!="" && $("#Telefon").val()!="") {
					if(!validate_phone($("#Telefon").val())){
						$ACTsave = 0;
						alert("Numar de telefon invalid");
					}else if(!validate_email($("#Email").val())){
						$ACTsave = 0;
						alert("Adresa de email invalida");
					}else{
						$ACTsave = 1;
						ajaxTriggers.eq(0).change();
					}
				}else{
					alert("Pentru a putea recalcula te rugam sa completezi formularul cu \n - numele tau sau al firmei \n - numarul tau de telefon!");
				}
			}else{
				alert("Trebuie sa fii de acord cu termenii si conditiile site-ului!");
			}
		});
	}else{
		$('#offerRecalc').click(function(){
			$ACTsave = 0;
			ajaxTriggers.eq(0).change();
		});
	}
		
	$nrTotal = asigs.length;
	var ajax = new Array();
	var ajaxStarter;
	
	$('#Bugetar').click(function(){
		if($(this).is(':checked')){
			$('#Bugetar2').attr("checked", "");
		}
	});
	$('#Bugetar2').click(function(){
		if($(this).is(':checked')){
			$('#Bugetar').attr("checked", "");
		}
	});

	$('#Copil').click(function(){
		if($(this).is(':checked')){
			$('#Copil2').attr("checked", "");
		}
	});
	$('#Copil2').click(function(){
		if($(this).is(':checked')){
			$('#Copil').attr("checked", "");
		}
	});
	
	$('#Student').click(function(){
		if($(this).is(':checked')){
			$('#Somer').attr("checked", "");
		}
	});
	$('#Somer').click(function(){
		if($(this).is(':checked')){
			$('#Student').attr("checked", "");
		}
	});
	
	
	$('#CapCil').change(function(){
		if($('#TipRCA option:selected').val()=="1"){
			switch($("#CapCil option:selected").val()){
				case '1199':{
					$("#PutMaxima").val("30");
				}break;
				case '1399':{
					$("#PutMaxima").val("50");
				}break;
				case '1599':{
					$("#PutMaxima").val("60");
				}break;
				case '1799':{
					$("#PutMaxima").val("75");
				}break;
				case '1999':{
					$("#PutMaxima").val("100");
				}break;
				case '2001':{
					$("#PutMaxima").val("120");
				}break;
			}
		}else if($('#TipRCA option:selected').val()=="2"){
			switch($("#CapCil option:selected").val()){
				case '49':{
					$("#PutMaxima").val("5");
				}break;
				case '249':{
					$("#PutMaxima").val("25");
				}break;
				case '749':{
					$("#PutMaxima").val("50");
				}break;
				case '999':{
					$("#PutMaxima").val("90");
				}break;
			}
		}else if($('#TipRCA option:selected').val()=="3"){
			switch($("#CapCil option:selected").val()){
				case '1':{
					$("#PutMaxima").val("30");
				}break;
				case '2':{
					$("#PutMaxima").val("55");
				}break;
			}
		}else if($('#TipRCA option:selected').val()=="4"){
			$("#PutMaxima").val("100");
		}else if($('#TipRCA option:selected').val()=="5"){
			$("#PutMaxima").val("0");
		}else if($('#TipRCA option:selected').val()=="6"){
			$("#PutMaxima").val("200");
		}
	});
	
	$('#CapCil').change(function(){
		if($('#TipRCA option:selected').val()=="1"){
			switch($("#CapCil option:selected").val()){
				case '1199':{
					$("#Capacitate").val("1199");
				}break;
				case '1399':{
					$("#Capacitate").val("1399");
				}break;
				case '1599':{
					$("#Capacitate").val("1599");
				}break;
				case '1799':{
					$("#Capacitate").val("1799");
				}break;
				case '1999':{
					$("#Capacitate").val("1999");
				}break;
				case '2001':{
					$("#Capacitate").val("2001");
				}break;
			}
		}else if($('#TipRCA option:selected').val()=="2"){
			switch($("#CapCil option:selected").val()){
				case '49':{
					$("#Capacitate").val("49");
				}break;
				case '249':{
					$("#Capacitate").val("249");
				}break;
				case '749':{
					$("#Capacitate").val("749");
				}break;
				case '999':{
					$("#Capacitate").val("1000");
				}break;
			}
		}else if($('#TipRCA option:selected').val()=="3"){
			switch($("#CapCil option:selected").val()){
				case '1':{
					$("#Capacitate").val("1999");
				}break;
				case '2':{
					$("#Capacitate").val("2499");
				}break;
			}
		}else if($('#TipRCA option:selected').val()=="4"){
			$("#Capacitate").val("2999");
		}else if($('#TipRCA option:selected').val()=="5"){
			$("#Capacitate").val("0");
		}else if($('#TipRCA option:selected').val()=="6"){
			$("#Capacitate").val("3999");
		}
	});
	
	
	ajaxTriggers.change(function(){
			
		// var used to test when minimum inputs are filled/selected to generate a valid query
		$test = true;
		$ACTsave = 0;
		
		if($("#CapCil option:selected").val()=="") {
			$test = false;
			alert("Alege capacitatea cilindrica!");
		}
		if($("#PutMaxima").val()=="") {
			$test = false;
			alert("Scrie puterea maxima!");
		}
		if($("#Sasiu").val().length>0 && $("#Sasiu").val().length<8) {
			$test = false;
			alert("Serie de sasiu invalida!");
		}
		if($("#CNP").val().length>0){
			if($("#TipPersoana").val()==5 || $("#TipPersoana").val()==6){
				if(!IsNumeric($("#CNP").val()) && ($("#CNP").val().length < 7 || $("#CNP").val().length > 10)){
					$test = false;
					$("#CNP").val('');
					alert("CUI invalid! Fara 'RO' si maxim 8 cifre!");
				}
			}else{
				if(validareCNP($("#CNP").val())==0) {
					$test = false;
					$("#CNP").val('');
					alert("CNP invalid!");
				}
			}
		}
		
		if($("#Nume").val()!="" && $("#Telefon").val()!="") {
			if(!validate_phone($("#Telefon").val())){
				$ACTsave = 0;
			}else{
				$ACTsave = 1;
			}
		}
		
		if($test) {
			
			//abort request-uri curente
			for($i=1;$i<$nrTotal;$i++){
				if(ajax[$i]){
					ajax[$i].abort();
				}
			}
			if(ajaxStarter){
				ajaxStarter.abort();
			}
			
			setTimeout("abortAjax()",30000);
			
			$('#offset1, #offset2').empty();
			$('#fldst1, #fldst2').hide();
			
			// efective post request
			$('.side.form .sys-info').not('.loading').not('.asirom').slideUp().parent().find('.sys-info.loading').slideDown(100,function(){
				$('#query-result').slideUp(0, function(){
					$('#query-result').hide().slideDown(400,function(){
						
						$respNr = $raspWS = $offExist = 0;
						
						if($('#Bugetar').is(':checked')){
							$_Bugetar = 1;
						}else if($('#Bugetar2').is(':checked')){
							$_Bugetar = 2;
						}else{
							$_Bugetar = 0;
						}
						
						if($('#Copil').is(':checked')){
							$_Copil = 1;
						}else if($('#Copil2').is(':checked')){
							$_Copil = 2;
						}else{
							$_Copil = 0;
						}
						
						if($('#Student').is(':checked')){
							$_Student = 1;
						}else if($('#Somer').is(':checked')){
							$_Student = 2;
						}else{
							$_Student = 0;
						}
						
						//save actualizari
						if($ACTsave==1){
							$.ajax({
								url: url_base+"ajax/ws/save.php",
								cache: false,
								type: "POST",
							    data: ({			    	
							    	TipPersoana: $("#TipPersoana option:selected").val(),
							    	Activity: $("#Activity option:selected").val(),
							    	TipRCA: $("#TipRCA option:selected").val(),
							    	CapCil: $("#CapCil option:selected").val(),
							    	Capacitate: $("#Capacitate").val(),
							    	Perioada: $("input[name=Perioada]:checked").val(),
							    	CNP: $("#CNP").val(),
							    	Sasiu: $("#Sasiu").val(),
							    	PutMaxima: $("#PutMaxima").val(),
							    	NrInmatriculare: $("#NrInmatriculare").val(),
							    	DataPolita: $("#DataPolita").val(),
							    	Daune: $("#Daune").val(),
							    	Localitate: $("#Localitate option:selected").val(),
							    	MarcaAuto: $("#MarcaAuto option:selected").val(),
							    	Nume: $("#Nume").val(),
							    	Permis: $("#Permis").val(),
							    	Bugetar: $_Bugetar,
							    	Copil: $_Copil,
							    	Student: $_Student,
							    	Email: $("#Email").val(),
							    	Telefon: $("#Telefon").val()
							    }),
							    async:"true",
								success: function(data) {
									$_lastID = data;
								}
							});
						}
						
						//ask WS :)
						ajaxStarter = $.ajax({
							url: url_base+"ajax/ws/offer/omniasig.php",
							cache: false,
							type: "POST",
							timeout: 20000,
						    data: ({			    	
						    	TipPersoana: $("#TipPersoana option:selected").val(),
						    	Activity: $("#Activity option:selected").val(),
						    	TipRCA: $("#TipRCA option:selected").val(),
						    	CapCil: $("#CapCil option:selected").val(),
						    	Capacitate: $("#Capacitate").val(),
						    	Perioada: $("input[name=Perioada]:checked").val(),
						    	CNP: $("#CNP").val(),
						    	Sasiu: $("#Sasiu").val(),
						    	PutMaxima: $("#PutMaxima").val(),
						    	NrInmatriculare: $("#NrInmatriculare").val(),
						    	DataPolita: $("#DataPolita").val(),
						    	Daune: $("#Daune").val(),
						    	Localitate: $("#Localitate option:selected").val(),
						    	MarcaAuto: $("#MarcaAuto option:selected").val(),
						    	Nume: $("#Nume").val(),
						    	Permis: $("#Permis").val(),
						    	Bugetar: $_Bugetar,
						    	Copil: $_Copil,
						    	Student: $_Student,
						    	Email: $("#Email").val(),
						    	Telefon: $("#Telefon").val()
						    }),
						    async:"true",
							success: function(data) {
						
								$('#siteTerms').show();
						
								for($i=1;$i<$nrTotal;$i++){
									ajax[$i] = $.ajax({
										url: url_base+"ajax/ws/offer/"+ asigs[$i] +".php",
										cache: false,
										type: "POST",
										timeout: 20000,
									    data: ({			    	
									    	TipPersoana: $("#TipPersoana option:selected").val(),
									    	Activity: $("#Activity option:selected").val(),
									    	TipRCA: $("#TipRCA option:selected").val(),
									    	CapCil: $("#CapCil option:selected").val(),
									    	Capacitate: $("#Capacitate").val(),
									    	Perioada: $("input[name=Perioada]:checked").val(),
									    	CNP: $("#CNP").val(),
									    	Sasiu: $("#Sasiu").val(),
									    	PutMaxima: $("#PutMaxima").val(),
									    	NrInmatriculare: $("#NrInmatriculare").val(),
									    	DataPolita: $("#DataPolita").val(),
									    	Daune: $("#Daune").val(),
									    	Localitate: $("#Localitate option:selected").val(),
									    	MarcaAuto: $("#MarcaAuto option:selected").val(),
									    	Nume: $("#Nume").val(),
									    	Permis: $("#Permis").val(),
									    	Bugetar: $_Bugetar,
									    	Copil: $_Copil,
									    	Student: $_Student,
									    	Email: $("#Email").val(),
									    	Telefon: $("#Telefon").val()
									    }),
									    async:"true",
										success: function(data) {
											
											$raspWS++;
											
											newdata = $(data);
											newdata.find('span.counter').html($respNr);
											$pret = newdata.attr('pret');
											$newID = newdata.attr('asig');
											$offExist = 0;
											
											$('#offset1 li.offer').each(function(){
												if($(this).attr('asig') == $newID){
													$offExist = 1;
												}
											});
											
											if($pret=="0"){
												newdata.css('display','none');
												$offExist = 1;
											}
											
											if($offExist == 0){
												
												$respNr++;
											
												if($respNr!=1){
			
													$size = $('#offset1 li.offer').size();
													
													for($k=0;$k<$size;$k++){
														if(parseFloat($pret) <= parseFloat($('#offset1 li.offer:eq('+$k+')').attr('pret'))){
															$('#offset1 li.offer:eq('+$k+')').before(newdata);
															break;
														}else{
															$('#offset1').append(newdata);
														}
													}
													
													if($respNr>2){
														$('#offset1 li.offer').removeClass('first').removeClass('second');
														//$('#offset1 li.offer:eq(0)').addClass('first');
														//$('#offset1 li.offer:eq(1)').addClass('second');
													}
													
													$cnt=1;
													$('#offset1 li.offer').each(function(){
														if($(this).css('display')!="none"){
															$(this).find('span.counter').html($cnt);
															$cnt++;
														}
													});	
													
													if($respNr>7){
														
														$('#fldst2').show();
														
														$sizeSec = $('#offset2 li.offer').size();
														
														if($sizeSec==0){
															$('#offset2').append($('#offset1 li.offer:eq(7)').clone());
														}else{
															
															$pretNew = $('#offset1 li.offer:eq(7)').attr('pret');
															$idAsig = $('#offset1 li.offer:eq(7)').attr('asig');
																
															for($j=0;$j<$sizeSec;$j++){
																if(parseFloat($pretNew) <= parseFloat($('#offset2 li.offer:eq('+$j+')').attr('pret'))){
																	$('#offset2 li.offer:eq('+$j+')').before($('#offset1 li.offer:eq(7)').clone());
																	break;
																}else{
																	if($('#off'+$idAsig).length == 0){
																		$('#offset2').append($('#offset1 li.offer:eq(7)').clone());
																	}
																}
															}
															
														}
														
														$('#offset1 li.offer:eq(7)').remove();
														
														$cnt=8;
														$('#offset2 li.offer').each(function(){
															if($(this).css('display')!="none"){
																$(this).find('span.counter').html($cnt);
																$cnt++;
															}
														});
														
													}
			
												}else{
													$('#fldst1').show();
													$('#offset1').append(newdata);
													
													$cnt=1;
													$('#offset1 li.offer').each(function(){
														if($(this).css('display')!="none"){
															$(this).find('span.counter').html($cnt);
															$cnt++;
														}
													});	
												}
												
											}
											
											if($raspWS==$nrTotal-1){
												$('.side.form .sys-info.loading').slideUp(100);
											}
											
											
											// prettyPhoto
											oferte = $('#comp-oferte .offer');
											$("a[rel^='prettyPhoto']").prettyPhoto();
											oferte.find('.back-side .info a, .back-side .btn a').attr('target','_blank').click(function(e){
												e.stopPropagation();
											});
											
											// single check
											cartButtonPressed = 0;
											oferte.unbind('click').bind('click',function(e){
												e.preventDefault();
												e.stopPropagation();
												$('#AsigID').val($(this).attr('asig'));
												$('#AsigPret').val($(this).attr('pretAsig'));
												$('#idOferta').val($(this).attr('idoffer'));
												$('#Discount').val($(this).attr('discount'));
												$('#PretOrig').val($(this).attr('pretAsig'));
												
												if($(this).hasClass('current')) {
													//$(this).removeClass('current').find('input[type=checkbox]').attr('checked',false);
		
												} else {
													oferte.each(function(i){
														$(this).removeClass('current').find('input[type=checkbox]').attr('checked',false);
													});
													
													thisoff = $(this);
													
													$(this).addClass('current').find('input[type=checkbox]').attr('checked',true);
												}
												if(cartButtonPressed==1) {
													$('#comp-oferte a.order_btn').click();
												}
												cartButtonPressed = 0;
												//alert(cartButtonPressed);
											});
											
											oferte.find('.front-side .btn').unbind('click').bind('click',function(e){
												cartButtonPressed = 1;
											});
											
											$('.offers .help').attr('title','').mousemove(function(e){
												//$('title').html(tooltip.height());
												tooltip.css({top:e.pageY-tooltip.height()-5,left:e.pageX-20});
												
											}).mouseover(function(e){
												$(this).css({zIndex:12});
												ttcontent = $(this).find('.tooltip');
												tooltip.find('.c').html(ttcontent.html()).parent().stop(true,true).fadeIn();
												if(ttcontent.find('img').length > 0) {
													tooltip.addClass('withimg');
													//$('title').html('are img');
												} else {
													//$('title').html('nu are img');
													
												}
											}).mouseleave(function(e){
												$(this).css({zIndex:'auto'});
												tooltip.stop(true,true).fadeOut().removeClass('withimg');
											});
											
										}
									});
									
									$('#comp-oferte a.details_btn').unbind('click').click(function(e){
										if(!oferte.eq(0).hasClass('back')) {
											oferte.addClass('back').find('.front-side').slideUp();
											$(this).html('înapoi la preţuri');
										} else {
											oferte.removeClass('back').find('.front-side').slideDown();
											$(this).html('vezi detaliile ofertei');
										}
										
									});
									
									if(useIframe == 1){
										$("a").each(function(){
											if($(this).attr("id")!="offerRecalc"){
												$(this).attr("target","_blank");
											}
										});
									}
									
								}
						
						
					
							}
						});	
						
						
					});

				});
			});
			
		} else {
			$('.side.form .sys-info:not(.tip)').slideUp().parent().find('.sys-info.tip').slideDown(200);
		}
	}).eq(0).change();
	
	$('#comp-oferte a.order_btn').click(function(){		
		$gasit = $stopCom = 0;
		oferte = $('#comp-oferte .offer');
		oferte.each(function(i){
			if($(this).find('input[type=checkbox]').attr('checked')==true){
				$gasit = 1;
			}
		});
		
		if($gasit == 1){
				if($("#Nume").val() == "" && $("#Telefon").val() == "") {
					$stopCom = 1;
					alert("Scrie numele tau sau al firmei! \nScrie numarul tau de telefon!");
				}else if($("#Nume").val() == ""){
					$stopCom = 1;
					alert("Scrie numele tau sau al firmei!");
				}else if($("#Telefon").val() == "") {
					$stopCom = 1;
					alert("Scrie numarul tau de telefon!");
				} else if($('#CNP').val() != ''){
					if($("#TipPersoana option:selected").val()<=4){
						if(!validareCNP($("#CNP").val())){
							$stopCom = 1;
							alert("CNP invalid!");
						}
					}else{
						if(!IsNumeric($("#CNP").val()) && ($("#CNP").val().length < 7 || $("#CNP").val().length > 10)){
							$stopCom = 1;
							alert("CUI invalid! Fara 'RO' si maxim 8 cifre!");
						}
					}
				}else if(!$("#acc-term").is(':checked')){
					$stopCom = 1;
					if(confirm("Sunteti de acord cu termenii si conditiile site-ului?")){
						$("#acc-term").attr('checked', true);
						$stopCom = 0;
					}
				}
			
			if($stopCom == 0){
				$('#calc-step-1').submit();
			}
		}else{
			alert("Alege un RCA!");
		}
	});
	
	$('.print-offers').click(function(e){
		e.preventDefault();
		
			url = getOffersUrl();
			url += "&print";
			
			window.open(url,'Print','scrollbars=yes,resizable=yes,location=no,width=860,height=680');
		
	});
	
	
	
	$('#btm11').click(function(e){
		e.preventDefault();
		$('.field.tomail').slideUp();
		$('.field.tomail.tm11').slideDown();
		
	});
	
	$('#btm12').click(function(e){
		e.preventDefault();
		$('.field.tomail').slideUp();
		$('.field.tomail.tm12').slideDown();
		
	});
	$('#tm11-send').click(function(e){
		$.post(url_base+"popup.php", {
			url:getOffersUrl(),
			mail:true,
			toemail:$('#tm11-toemail').val(),
			fromemail:$('#tm11-fromemail').val(),
			name: $('#tm11-name').val(),
			r: getRCAParam(),
			o: getOffersParam()}, function(data){
			//alert(data);
			$('#tm11-toemail,#tm11-fromemail,#tm11-name').removeClass('error');
			if(data=='01'){
				msg = 'Adresa de email a prietenului tău este invalidă!'
				$('#tm11-toemail').addClass("error").val(msg);
				alert(msg);
				return false;
			}
			if(data=='02'){
				msg = 'Adresa ta de email este invalidă!'
				$('#tm11-fromemail').addClass("error").val(msg);
				alert(msg);
				return false;
			}
			if(data=='03'){
				msg = 'Numele tău este incomplet sau invalid!'
				$('#tm11-name').addClass("error").val(msg);
				alert(msg);
				return false;
			}
			if(data=='1'){
				$('.field.tomail.tm11').slideUp();
				$('.field.tm11.msg').html('Lista cu oferte a fost trimisă!').addClass('success').slideDown();
				$('#tm11-toemail,#tm11-fromemail,#tm11-name').val('');
				return true;
			}else{
				$('.field.tomail.tm11').slideUp();
				$('.field.tm11.msg').html('A intervenit o eroare de sistem! Vă rugăm să reveniţi mai târziu!').addClass('error').slideDown();
				return false;
			}
		})
	});
	$('#tm12-send').click(function(e){
		$.post(url_base+"popup.php", {
			url:getOffersUrl(),
			mail:true,
			toemail:$('#tm12-toemail').val(),
			r: getRCAParam(),
			o: getOffersParam()}, function(data){
			//alert(data);
			$('#tm12-toemail').removeClass('error');
			if(data=='01'){
				msg = 'Adresa ta de email este invalidă!'
				$('#tm12-toemail').addClass("error").val(msg);
				alert(msg);
				return false;
			}
			if(data==1){
				$('.field.tomail.tm12').slideUp();
				$('.field.tm1.msg').html('Lista cu oferte a fost trimisă!').addClass('success').slideDown();
				$('#tm11-toemail').val('');
			}else{
				$('.field.tomail.tm11').slideUp();
				$('.field.tm1.msg').html('A intervenit o eroare de sistem! Vă rugăm să reveniţi mai târziu!').addClass('error').slideDown();
			}
		})
	});
	
	//informatii obligatorii
	
	$("#infor_obl").click(function(e){
		$(".politatohide").hide();	
		$(".info_client").show();		
		$("#comand").removeClass('active');
		$(this).addClass("active");
	});	
	
	$("#comand").click(function(e){
		$(".politatohide").show();
		$(".info_client").hide();
		$(this).addClass('active');
		$("#infor_obl").removeClass("active");		
	});
});






function getOffersUrl() {
	oferte = $('li.offer');
	var url = "";
	url = url_base+'popup/?';
	url += "r="+$('#TipRCA option:selected').val();
	url += ","+$('#Activity option:selected').val();
	url += ","+$('#TipPersoana option:selected').val();
	url += ","+$('#MarcaAuto option:selected').val();
	url += ","+$('#CapCil option:selected').val();
	url += ","+$('#Localitate option:selected').val();
	url += ","+$('#DataPolita').val();
	url += ","+$('input[name=Perioada]:checked').val();
	if(oferte.length) {
		url += "&o="; 
		oferte.each(function(i){
			url += $(this).attr('asig')+","+$(this).attr('pret')+","+$(this).attr('pretAsig');
			if(i!=oferte.length-1) url+=";";
			
		});
		return url;
	} else return false;
}

function getRCAParam() {
	oferte = $('li.offer');
	var url = "";
	url += ""+$('#TipRCA option:selected').val();
	url += ","+$('#Activity option:selected').val();
	url += ","+$('#TipPersoana option:selected').val();
	url += ","+$('#MarcaAuto option:selected').val();
	url += ","+$('#CapCil option:selected').val();
	url += ","+$('#Localitate option:selected').val();
	url += ","+$('#DataPolita').val();
	url += ","+$('input[name=Perioada]:checked').val();
	return url;
}

function getOffersParam(){
	oferte = $('li.offer');
	var url = "";
	if(oferte.length) {
		url += ""; 
		oferte.each(function(i){
			url += $(this).attr('asig')+","+$(this).attr('pret')+","+$(this).attr('pretAsig');
			if(i!=oferte.length-1) url+=";";
		});
	}
	return url;
}

function validareCNP(cnp){
	if(cnp.length != 13){
		return 0;
	}else{
		if(IsNumeric(cnp) == 0){
			return 0;
		}else{
			return 1;
		}
	}
}

function IsNumeric(sText) {
   ValidChars = "0123456789";
   IsNumber = 1;
   Char = '';
   
   for (i = 0; i < sText.length && IsNumber == true; i++) { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) {
         IsNumber = 0;
      }
   }
   
   return IsNumber;
}

/*
function validareCNP($cnp){
	if($cnp.length!=13){
		return 0;
	}else{
		if($cnp[0]!=1 && $cnp[0]!=2){
			return 0;
		}else{
			$sum = $cnp[0] * 2 + $cnp[1] * 7 + $cnp[2] * 9 + $cnp[3] * 1 + $cnp[4] * 4 + $cnp[5] * 6 + $cnp[6] * 3 + $cnp[7] * 5 + $cnp[8] * 8 + $cnp[9] * 2 + $cnp[10] * 7 + $cnp[11] * 9;
			$vf = $sum % 11;
			
			if($vf<10){
				if($vf == $cnp[12]){
					return 1;
				}else{
					return 0;
				}
			}else if($vf == 10){
				if($cnp[12] == 1){
					return 1;
				}else{
					return 0;
				}
			}else{
				return 0;
			}
		}
	}
}
*/

var startTestim = 3;
function showTestimonials(){
	total = $('#testimonials div.item').size();
	if(startTestim == 1){
		$("#parere"+total).hide();
		$("#parere"+(total-1)).hide();
	}else{
		$("#parere"+(startTestim-2)).hide();
		$("#parere"+(startTestim-1)).hide();
	}
	$("#parere"+startTestim).show();
	$("#parere"+(startTestim+1)).show();
	
	if(startTestim<(total-1)){
		startTestim += 2;
	}else{
		startTestim = 1;
	}
}

function dbg(text) {
	//$('title').html(text);
}

function abortAjax(){
	$('.sys-info.loading').slideUp(100);
}



function validate_email(email) {
	if(email!=""){
	    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	    if(reg.test(email) == false) {
	    	return false;
	    }else{
	    	return true;
	    }
	}else{
		return true;
	}
}

function validate_phone(phone) {
	var returnString = "";
    for (i = 0; i < phone.length; i++) {   
        c = phone.charAt(i);
        if (c != " ") returnString += c;
    }
    
    for (i = 0; i < returnString.length; i++) {   
        c = returnString.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    
    if(returnString.length<=9){
    	return false;
    }
    
    c = returnString.charAt(0);
    if (c != "0") {
    	return false;
    }
    
    return true;
}




