function isEmail(str) {
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	return (filter.test(str) ? true : false);
}

$(document).ready(function() {
	$("div.roundedTop").corner("10px top");
	$("dl#testimoniale dd").corner("10px");
	$("dl#testimoniales dd").corner("10px");
	
	if($('#contactForm')) {
		$("button").mouseover(function(){$(this).addClass('over')});
		$("button").mouseout(function(){$(this).removeClass('over')});
	}
	
	
	$('#contactForm').submit(function() {
		var vnume = $('#contactForm input[@name=nume]').val();
		if (vnume == '')
		{
			alert('Va rugam sa va completati numele!');
			return false;
		}
		
		var vemail = $('#contactForm input[@name=email]').val();
		if (vemail == '' || !isEmail(vemail))
		{
			alert('Va rugam sa va completati emailul!');
			return false;
		}
		
		var vloc = $('#contactForm input[@name=localitate]').val();
		if (vloc == '')
		{
			alert('Va rugam sa completati localitatea!');
			return false;
		}
		
		var vmodel = $('#contactForm select[@name=model]').val();
		if (vmodel == 'curent')
		{
			if (document.getElementById('modele_container').innerHTML.indexOf('hidden') == -1)
			{
				alert('Va rugam sa selectati cel putin un model pe care-l doriti! De asemenea, puteti specifica si metrajul aproximativ.');
				return false;
			}
		}
		else if (vmodel == 'special')
		{
			var vdesc = $('#contactForm textarea[@name=model_descriere]').val();
			if (vdesc == '')
			{
				alert('Va rugam sa descrieti modelul dorit!');
				return false;
			}
		}
		
		var vmesaj = $('#contactForm input[@name=mesaj]').val();
		if (vmesaj == '')
		{
			alert('Va rugam sa scrieti un mesaj!');
			return false;
		}

		alert('Va multumim, mesajul dumneavoastra a fost trimis');
		return true;
	});
	
});

function schimbaModel()
{
	var model = document.getElementById('model').value;
	var model_special = document.getElementById('model_special');
	var modele_container_big = document.getElementById('modele_container_big');
	
	if (model == 'curent')
	{
		model_special.style.display = 'none';
		modele_container_big.style.display = '';
	}
	else if (model == 'special')
	{
		model_special.style.display = '';
		modele_container_big.style.display = 'none';
	}
}

function adaugaModel()
{
	var model_select = document.getElementById('model_select');
	if (model_select.selectedIndex == 0)
		return;
		
	var container = document.getElementById('modele_container');
	var id = rand(100000);
	container.innerHTML += '<p id="m'+id+'"><input type="hidden" name="modele[]" id="mh'+id+'" value="' + model_select.value + '" />' + model_select.value + ' <input type="text" name="modele_metraj[]" value="" class="mic" onmouseover="return overlib(\'&lt;p style=&quot;background: #fff; padding: 5px; border: solid 1px #c7c7c7;&quot;&gt;&lt;img src=&quot;images/info.gif&quot; /&gt;Specificati metrajul aproximativ&lt;/p&gt;\')" onmouseout="return nd();"/> ml <input type="text" name="modele_nr[]" value="0" class="mic" onmouseover="return overlib(\'&lt;p style=&quot;background: #fff; padding: 5px; border: solid 1px #c7c7c7;&quot;&gt;&lt;img src=&quot;images/info.gif&quot; /&gt;Specificati numarul de coturi sau unghiuri pe care le doriti&lt;/p&gt;\')" onmouseout="return nd();" /> nr de <select name="modele_tip[]" class="mic" onmouseover="return overlib(\'&lt;p style=&quot;background: #fff; padding: 5px; border: solid 1px #c7c7c7;&quot;&gt;&lt;img src=&quot;images/info.gif&quot; /&gt;Specificati daca doriti unghiuri (90 de grade) sau coturi (rotunjite) &lt;/p&gt;\')" onmouseout="return nd();"><option>unghiuri</option><option>coturi</option></select> <a href="#" onclick="return stergeModel(\''+id+'\');"><img src="images/delete.png" alt="" /></a></p>';
	
	model_select.options[model_select.selectedIndex].disabled = true;
	
	model_select.selectedIndex = 0;
}

function stergeModel(id)
{
	if (!confirm('Sunteti sigur ca doriti sa stergeti acest model ?')) return false;
	
	var container = document.getElementById('modele_container');
	var remove = document.getElementById('m' + id);
	var hidden_value = document.getElementById('mh' + id).value;
	var model_select = document.getElementById('model_select');
	
	for (var i = 0; i<model_select.options.length; i++)
		if (model_select.options[i].value == hidden_value)
			model_select.options[i].disabled = false;
	
	container.removeChild(remove);
	
	return false;
}

function rand(n)
{
	return Math.floor(Math.random()*n + 1);
}