var popupStatus = 0;

function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupOferta").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.clearFormOferta = 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(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupOferta").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	
	$("#popupOferta").center();
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//LOADING POPUP
	//Click the button event!
	$("#buttonOferta").click(function(){
		$("#formOferta").clearFormOferta();
		
		centerPopup();
		//load popup
		loadPopup();
	});
				
	//CLOSING POPUP
	//Click the x event!
	$("#popupOfertaClose").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	$("#popupOferta").draggable({handle: "#antet"});
	
	$("#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("sumResult", "Va rugam sa completati campul Cod de securitate!");
		if (!ecompletat){
			return;
		}
		
		var a = parseInt($("#a").text());
		var b = parseInt($("#b").text());
		var sumResult = parseInt($("#sumResult").val());
		if (a + b != sumResult) {
			alert("Introduceti valoarea corecta pentru Codul de securitate!");
			$("#sumResult").focus();
			$("#a, #b").css("font-weight", "bolder");
			return;
		}
		
		document.oferta.submit();
	});

});