var popupStatus = 0;
var iddiv;// ="#popupOferta";
function loadPopup(iddiv){
	//loads popup only if it is disabled

	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		
		$(iddiv).fadeIn("slow");
		popupStatus = 1;
	}
}

jQuery.fn.center = function () {
	this.css("position", "absolute");
	this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");
    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");
    return this;
};

$.fn.clearForm = function() {
	  return this.each(function() {
	 var type = this.type, tag = this.tagName.toLowerCase();
	 if (tag == 'form')
	   return $(':input',this).clearForm();
	 if (type == 'text' || type == 'password' || tag == 'textarea')
	   this.value = '';
	  });
};


//disabling popup with jQuery magic!
function disablePopup(iddiv){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$(iddiv).fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(iddiv){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	
	$(iddiv).center();
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){

	//LOADING POPUP
	//Click the button event!
	$("#buttonOferta").click(function(){
		$("#formOferta").clearForm();
		
		centerPopup("#popupOferta");
		//load popup
		loadPopup("#popupOferta");
	});
	
	//Click the button event!
	$("#buttonNewsletter").click(function(){
		
		$("#formNewsletter").clearForm();
	
		centerPopup("#popupNewsletter");
		
		//load popup
		loadPopup("#popupNewsletter");
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupOfertaClose").click(function(){
		disablePopup("#popupOferta");
		
	});
	$("#popupNewsletterClose").click(function(){
		disablePopup("#popupNewsletter");
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}

	});
	$("#poupOferta").draggable({handle: "#antet"});
	$("#poupNewsletter").draggable({handle: "#antet2"});
	
	$("#Trimite").click(function() {
		ecompletat = eSelectat("model", "Va rugam sa alegeti din lista modelul dorit!");
		if (!ecompletat){
			return;
		}

		ecompletat = eObligatoriu("mesaj", "Va rugam sa introduceti detalii suplimentare!");

		if (!ecompletat){
			return;
		}
		
		ecompletat = eObligatoriu("nume", "Va rugam sa completati campul Nume!");
		if (!ecompletat){
			return;
		}
		
		ecompletat = eObligatoriu("prenume", "Va rugam sa completati campul Prenume!");
		if (!ecompletat){
			return;
		}
		
		ecompletat = eObligatoriu2("mail","telefon", "Va rugam sa completati cel putin unul din campurile E-mail sau Telefon!");
		if (!ecompletat){
			return;
		}
		
		ecompletat = eEmail("mail", "Campul Email nu are un format valid!");
		if (!ecompletat){
			return;
		}
		
		ecompletat = eObligatoriu("rezultat", "Va rugam sa completati campul Cod de securitate!");
		if (!ecompletat){
			return;
		}
		
		var j = parseInt(document.getElementById("7j35").value);
		var k = parseInt(document.getElementById("3k51").value);
		var h = parseInt(document.getElementById("9h08").value);
		var g = parseInt(document.getElementById("6g38").value);
		var rezultat = parseInt($("#rezultat").val());
		if (j + k + h - g != rezultat) {
			alert("Introduceti valoarea corecta pentru Codul de securitate!");
			//var tempDiv = document.getElementById('7j35'); 
			//var 7j35_n = Math.floor(Math.random()*(6));
			//tempDiv.innerHTML = 7j35_n;  
			//document.getElementById("7j35").innerHTML = 7j35_n; 
			//tempDiv = document.getElementById('3k51'); 
			//var 3k51_n = Math.floor(Math.random()*(6));
			//tempDiv.innerHTML = 3k51_n;
			//document.getElementById("3k51").value = 3k51_n; 
			//tempDiv = document.getElementById('9h08'); 
			//var 9h08_n = Math.floor(Math.random()*(6) + 5);
			//tempDiv.innerHTML = 9h08_n;
			//tempDiv = document.getElementById('g3'); 
			//var 6g38_n = Math.floor(Math.random()*(6));
			//tempDiv.innerHTML = 6g38_n; 
			//tempDiv = document.getElementById('r1'); 
			//tempVal = 7j35_n + 3k51_n;
			//tempDiv.innerHTML = tempVal;
			//tempDiv = document.getElementById('r2'); 
			//tempVal = 9h08_n - 6g38_n;
			//tempDiv.innerHTML = tempVal;
			$("#rezultat").focus();
			$("#r1, #r2").css("font-weight", "bolder");
			return;
		}
		
		document.oferta.submit();
	});
	$("#TrimiteNewsletter").click(function() {
		ecompletat = eObligatoriu("numeNewsletter", "Va rugam sa completati campul Nume!");
		if (!ecompletat){
			return;
		}
		
		ecompletat = eObligatoriu("prenumeNewsletter", "Va rugam sa completati campul Prenume!");
		if (!ecompletat){
			return;
		}
		
		ecompletat = eObligatoriu("mailNewsletter", "Va rugam sa completati campul Email!");
		if (!ecompletat){
			return;
		}
		
		ecompletat = eEmail("mailNewsletter", "Campul Email nu are un format valid!");
		if (!ecompletat){
			return;
		}
		
		document.formularNewsletter.submit();
	});

});
