// JavaScript Document
$.fx.speeds._default = 400;
$(function(){
	/*slider header*/
	$('#header_slider').innerfade({
			speed: 'normal',
			timeout: 4000,
			type: 'sequence',
			containerheight: '337px'
	});
	/*end slider header*/
	
	/*prettyPhoto*/
	$("a[rel^='prettyPhoto']").prettyPhoto();
	/*end prettyPhoto*/	
	
	/*solicita oferta*/
		$("#dialog").dialog("destroy");
		var valoare = $("#valoare"),
			nume = $("#nume"),
			prenume = $("#prenume"),
			companie = $("#companie"),
			judet = $("#judet"),
			telefon = $("#telefon"),							
			allFields = $([]).add(valoare).add(nume).add(prenume).add(companie).add(telefon),		
			tips = $(".validateTips");

		function updateTips(t) {
			tips
				.text(t)
				.addClass('ui-state-highlight');
			setTimeout(function() {
				tips.removeClass('ui-state-highlight', 1500);
			}, 500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Nr. de caractele al campului " + n + " trebuie sa fie intre "+min+" si "+max+".");
				return false;
			} else {
				return true;
			}
		}
		
		function errorLogin(text){			
			updateTips(text);
			return false;
		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}
		}
		
		$('#trimisOK').dialog({
			autoOpen: false,
			height: 150,
			width: 250,
			modal: true,
			resizable: false,
			buttons: {
				Inchide: function() {
					$(this).dialog('close');
				}				
			}
		});
		
		$('#trimisBuyOK').dialog({
			autoOpen: false,
			height: 150,
			width: 250,
			modal: true,
			resizable: false,
			buttons: {
				Inchide: function() {
					$(this).dialog('close');
				}				
			}
		});
		
		$("#solicita_oferta").dialog({
			autoOpen: false,
			height: 400,
			width: 450,
			modal: true,
			resizable: false,
			buttons: {
				'Trimite': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');					
					
					bValid = bValid && checkLength(nume,"nume",2,250);
					bValid = bValid && checkLength(prenume,"prenume",2,250);
					bValid = bValid && checkLength(companie,"companie",2,250);
					bValid = bValid && checkLength(telefon,"telefon",7,50);
										
					if (bValid) {
						/*ajax login*/	
						var 		
							nume_bid = $("#nume").val(),
							prenume_bid = $("#prenume").val(),
							compania_bid = $("#companie").val(),
							telefon_bid = $("#telefon").val(),														
							email_bid = $("#email").val(),
							intrebari_bid = $("#intrebari").val(),
							produs_nume=$("#produs_nume").val();						
						$.ajax({
						   type: "POST",
						   url: "ajax.php",
						   data: 'produs_nume='+produs_nume+'&nume='+nume_bid+'&prenume='+prenume_bid+'&compania='+compania_bid+'&telefon='+telefon_bid+'&email='+email_bid+'&intrebari='+intrebari_bid
						 });

							$(this).dialog('close');
							$('#trimisOK').dialog('open');
							return false;
						/*end ajax login*/
					}
				},
				Anuleaza: function() {
					$(this).dialog('close');
				}				
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		$('#solicita_oferta_trigger').click(function() {
			$('#solicita_oferta').dialog('open');
			return false;
		});
		/*END solicita oferta*/
});
