$(document).ready(function() {
	$("#menu-top li a:eq(3)").css("background", "none")
	$("#sacladire, #sacontinut").css("color", "#ccc")
	$("#sacladire").focus(function(){
		if($(this).val() == "" || $(this).val() == "euro"){
			$(this).val("")
			$(this).css("color", "#000")
		}
	})
	$("#sacladire").blur(function(){
		if($(this).val() == "" || $(this).val() == "euro"){
			$(this).val("euro")
			$(this).css("color", "#ccc")
		}
	})
	
	$("#sacontinut").focus(function(){
		if($(this).val() == "" || $(this).val() == "euro"){
			$(this).val("")
			$(this).css("color", "#000")
		}
	})
	$("#sacontinut").blur(function(){
		if($(this).val() == "" || $(this).val() == "euro"){
			$(this).val("euro")
			$(this).css("color", "#ccc")
		}
	})
	function isInteger(s) {
	  return (s.toString().search(/^-?[0-9]+$/) == 0);
	}
	$("#contactForm").submit(function(){
		if($("#contactForm input[name='nume']").val() == ""){
			alert("Introduceti numele")
			return false;
		}
		if($("#contactForm input[name='prenume']").val() == ""){
			alert("Introduceti prenumele")
			return false;
		}
		if($("#contactForm input[name='telefon']").val() == ""){
			alert("Introduceti telefonul")
			return false;
		}
		if($("#contactForm input[name='email']").val() == ""){
			alert("Introduceti email-ul")
			return false;
		}
		if($("#contactForm input[name='mesaj']").val() == ""){
			alert("Introduceti mesajul")
			return false;
		}
	})
	$("#calcul").live("submit", function(){
		if($("#contactSubmit").val() == "Calculeaza"){
			
			if($("#sacladire").val() < 20000){
				alert("Suma asigurata nu poate fi mai mica de 20.000 euro")
				return false  
			}
			if($("#sacladire").val() > 200000){
				alert("Suma asigurata nu poate fi mai mare de 200.000 euro")
				return false  
			}
			if($("#sacladire").val() == "" || $("#sacladire").val() == "euro"){
				$("#sacladire").focus()
				alert("Introduceti suma asigurata cladire")
				return false;
			}
			if(isInteger($("#sacladire").val())){
				return true;
			}else{
				$("#sacladire").focus()
				alert("Suma asigurata cladire invalida")
				return false;
			}
		}
	})
	$("#calcul2 li").click(function(){
		$("input[name='ales']").val($(this).attr("soc"))
		$("input[name='suma']").val($(this).find(".pret").html())
		$("#calcul2").submit()
	})
	$("#alta_adresa").live("click", function(){
		$("#altaAdresaDetalii").show("slow")
		$("input[name='altaadresa']").val("da")
	})
	$(".lastForm").submit(function(){
		if($(".lastForm input[name='judet']").val() == ""){
			$(".lastForm input[name='judet']").focus()
			alert("Introduceti judetul")
			return false
		}
		if($(".lastForm input[name='localitate']").val() == ""){
			$(".lastForm input[name='localitate']").focus()
			alert("Introduceti localitatea")
			return false
		}
		if($(".lastForm input[name='strada']").val() == ""){
			$(".lastForm input[name='strada']").focus()
			alert("Introduceti strada")
			return false
		}
		if($(".lastForm input[name='nr']").val() == ""){
			$(".lastForm input[name='nr']").focus()
			alert("Introduceti numarul strazii")
			return false
		}
		if($(".lastForm input[name='nume']").val() == ""){
			$(".lastForm input[name='nume']").focus()
			alert("Introduceti numele")
			return false
		}
		if($(".lastForm input[name='prenume']").val() == ""){
			$(".lastForm input[name='prenume']").focus()
			alert("Introduceti prenumele")
			return false
		}
		if($(".lastForm input[name='cnp']").val() == ""){
			$(".lastForm input[name='cnp']").focus()
			alert("Introduceti CNP-ul")
			return false
		}
		if($(".lastForm input[name='cnp']").val().length != 13 ){
			$(".lastForm input[name='cnp']").focus()
			alert("CNP-ul trebuie sa aiba 13 cifre")
			return false
		}
		if(isInteger($(".lastForm input[name='cnp']").val())){
		}else{
			$(".lastForm input[name='cnp']").focus()
			alert("CNP-ul trebuie sa contina numai cifre")
			return false;
		}
		if(validCNP($(".lastForm input[name='cnp']").val()) == 0){
			alert("CNP-ul introdus nu este valid")
			return false;
		}
		if($(".lastForm input[name='tel']").val() == ""){
			$(".lastForm input[name='tel']").focus()
			alert("Introduceti numarul de telefon")
			return false
		}
		if(isInteger($(".lastForm input[name='tel']").val())){
		}else{
			$(".lastForm input[name='tel']").focus()
			alert("Introduceti numarul de telefon din cifre")
			return false
		}  
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if(reg.test($(".lastForm input[name='email']").val()) == false) {
			$(".lastForm input[name='email']").focus()
			alert("Introduceti adresa de email")
			return false
		}
	})
	function validCNP(cnp) {
    key = str_split(cnp);
    if (key[0] >= 1 && key[0] <= 9) {
        switch (key[0]) {
            case 1: case 2: default: an = 19; break;
            case 3: case 4: an = 18; break;
            case 5: case 6: an = 20; break;
        }
        if (checkdate(key[3] + key[4], key[5] + key[6], an + key[1] + key[2])) {
            testkey = str_split(279146358279);
            rez = 0;
            for (i = 0; i < 12; i++) {
                rez += testkey[i] * key[i];
            }
            if (key[12] == rez % 11)
                return 1;
            else if (rez % 11 == 10 && key[12] == 1)
                return 1;
            else
                return 0;
        }
        else
            return 0;
    }
    else
        return 0;
}

	function checkdate(m, d, y) {
		return m > 0 && m < 13 && y > 0 && y < 32768 && d > 0 && d <= (new Date(y, m, 0)).getDate();
	}
	function str_split(string, split_length) {
		if (string === undefined || !string.toString || split_length < 1) {
			return false;
		}
		return string.toString().match(new RegExp('.{1,' + (split_length || '1') + '}', 'g'));
	}
	function IsNumeric(n) {
		return !isNaN(parseFloat(n)) && isFinite(n);
	}
	$.Form = $("#trimite")
	$("#alte_menu li").live("click", function(){
		if($(this).parent().hasClass("click")){
			$(this).parent().removeClass("click")
			$(this).parent().html(fullForm)
			$.Form.hide()
			$("#pre_form_txt").hide()
		}else{
			$(this).parent().addClass("click")
			fullForm = $(this).parent().html()
			selectat = $(this).html()
			$("#alte_menu").html("<li>"+selectat+"</li>")
			$("#asig_sel").html(selectat)
			$.Form.show()
			$("#chestie").val($(this).attr("data_id"))
			$("#pre_form_txt").show()
		}
	})	
	$("#dob, #data").datepicker()
	$("#expirare").submit(function(){
		$("#mesaj").hide()
		params = $(this).serialize()
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		
		var errors = new Array()
		if($("#data").val() == ""){
			errors[0] = "Va rugam introduceti data expirarii"
		}
		if($("#tel").val() == ""){
			errors[1] = "Va rugam introduceti numarul de telefon"
		}
		if($("#email").val() == ""){
			errors[2] = "Va rugam introduceti adresa de email"
		}else
		if(reg.test($("#email").val()) == false) {
				errors[2] = "Adresa de email este invalida";
		}
		if(errors.length != 0){
			$("#mesaj").show().html("")
			for(i=0; i<errors.length; i++){
				if(typeof(errors[i]) != "undefined")
					$("#mesaj").addClass("eroare").html($("#mesaj").html()+errors[i]+"<br />")
			}
			return false
		}
		$.ajax({
			url: 'includes/processes/expirare.php',
			type: 'POST',
			dataType: 'json',
			data: params,
			success: function(msg){
				if(msg[0] == 0){
					$("#mesaj").show().addClass("eroare").html(msg[1])
				}else
				if(msg[0] == 1){
					$("#mesaj").show().addClass("success").html(msg[1])
					$("#data, #tel, #email").val("")
				}else
				if(msg[0] == 2){
					$("#mesaj").show().addClass("eroare").html(msg[1])
					$("#data, #tel, #email").val("")
				}
			}
		})
		return false
	})
	$("#judet").change(function(){
		$.ajax({
			url: "includes/ajax.php",
			type: "POST",
			data: "judet="+$(this).val(),
			success: function(msg){
				$("#localitate").html(msg)
			}
		})
	})
	$("input[type='radio'][name='tip']").click(function(){
		if($(this).val() == "persoana juridica"){
			$("#fieldpj").show()
		}else{
			$("#fieldpj").hide()
		}
	})
})
i = 0;
	function blinkMe() {
		i++;
		if(i%2) {
			$("#exp_sus").css("visibility", "visible")
			$(".asigRight").show()
		} else {
			$("#exp_sus").css("visibility", "hidden")
			$(".asigRight").hide()
			}
			var x = setTimeout("blinkMe()", 600);
	}
