var popupMailStatus = 0;
var popupOfertaPStatus = 0;

var windowWidthC = document.documentElement.clientWidth;
var windowHeightC = document.documentElement.clientHeight;

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 = '';
	 else if (type == 'radio')
	   this.checked = false;
	 else if (tag == 'select')
	   this.selectedIndex = -1;
	  });
};

function loadPopupMail(){
	if(popupMailStatus==0){
		$("#backMailToFriend").css({
			"opacity": "0.7"
		});
		$("#backMailToFriend").fadeIn("slow");
		$("#popupMailToFriend").fadeIn("slow");
		popupMailStatus = 1;
	}
}

function loadPopupOfertaP(){
	if(popupOfertaPStatus==0){
		$("#backOfertaP").css({
			"opacity": "0.7"
		});
		$("#backOfertaP").fadeIn("slow");
		$("#popupOfertaPersonala").fadeIn("slow");
		popupOfertaPStatus = 1;
	}
}

function disablePopupMail(){
	if(popupMailStatus==1){
		$("#backMailToFriend").fadeOut("slow");
		$("#popupMailToFriend").fadeOut("slow");
		popupMailStatus = 0;
	}
}

function disablePopupOfertaP(){
	if(popupOfertaPStatus==1){
		$("#backOfertaP").fadeOut("slow");
		$("#popupOfertaPersonala").fadeOut("slow");
		popupOfertaPStatus = 0;
	}
}

function centerPopupMail(){
	$("#popupMailToFriend").center();
	$("#backMailToFriend").css({
		"height": windowHeightC
	});
}
function centerPopupOfertaP(){
	$("#popupOfertaPersonala").center();
	$("#backOfertaP").css({
		"height": windowHeightC
	});
}

$(document).ready(function(){
	$(".mailToFriend").click(function(){
		$("#mailtofriend").clearForm();
		$("#succesMessage").hide();
		$("#mailtofriend").fadeIn();
		centerPopupMail();
		loadPopupMail();
	});
	$(".buttonOferta").click(function(){
		$("#ofertapersonala").clearForm();
		$("#succesMessage").hide();
		$("#ofertapersonala").fadeIn();
		centerPopupOfertaP();
		loadPopupOfertaP();
		
		var button_id = this.id;
		var hidden_id = "#hidden_" + button_id.substr(7, button_id.length);
		var hidden_value = $(hidden_id).val();
		$("#codID").val(hidden_value.substr(0, hidden_value.indexOf("_")));
		$("#nume_auto").val(hidden_value.substr(hidden_value.indexOf("_") + 1, hidden_value.length));
		if ($("#codID").val().length > 6)
			$("#codID").val("null");
	});
	
	$("#popupMailClose").click(function(){
		disablePopupMail();
	});
	$("#popupOfertaPClose").click(function(){
		disablePopupOfertaP();
	});
	
	$("#backMailToFriend").click(function(){
		disablePopupMail();
	});
	$("#backOfertaP").click(function(){
		disablePopupOfertaP();
	});
	
	$(document).keypress(function(e){
		if(e.keyCode == 27)
			if (popupMailStatus == 1)
				disablePopupMail();
			else if (popupOfertaPStatus == 1)
				disablePopupOfertaP();
	});
	$("#popupMailToFriend").draggable({handle: "#antetMail"});
	$("#popupOfertaPersonala").draggable({handle: "#antetOfertaP"});
	
	$("#buttonMF").click(function() {
		ecompletat = eObligatoriu("numeMF", "Va rugam sa completati campul Numele dvs.!");
		if (!ecompletat){
			return false;
		}
		
		ecompletat = eObligatoriu("prenumeMF", "Va rugam sa completati campul Prenumele dvs.!");
		if (!ecompletat){
			return false;
		}
		
		ecompletat = eObligatoriu("emailMF", "Va rugam sa completati campul E-mailul dvs.!");
		if (!ecompletat){
			return false;
		}
		
		ecompletat = eEmail("emailMF", "Campul Emailul dvs. nu are un format valid!");
		if (!ecompletat){
			return false;
		}
		
		ecompletat = eObligatoriu("numePMF", "Va rugam sa completati campul Numele prietenului!");
		if (!ecompletat){
			return false;
		}
		
		ecompletat = eObligatoriu("prenumePMF", "Va rugam sa completati campul Prenumele prietenului!");
		if (!ecompletat){
			return false;
		}
		
		ecompletat = eObligatoriu("emailPMF", "Va rugam sa completati campul E-mailul prietenului!");
		if (!ecompletat){
			return false;
		}
		
		ecompletat = eEmail("emailPMF", "Campul Emailul prietenului nu are un format valid!");
		if (!ecompletat){
			return false;
		}
		var nume = $("#numeMF").val();
		var prenume = $("#prenumeMF").val();
		var _mail = $("#emailMF").val();
		var numeP = $("#numePMF").val();
		var prenumeP = $("#prenumePMF").val();
		var _mailP = $("#emailPMF").val();
		var mesaj = $("#mesajMF").val();
		var acord = $("#acordMF:checked").val();
		var dataString = "nume=" + nume + "&prenume=" + prenume + "&email=" + _mail + 
			"&mesaj=" + encodeURIComponent(mesaj) + "&numeP=" + numeP + "&prenumeP=" + prenumeP + 
			"&emailP=" + _mailP + "&acord=" + acord;
		$.ajax({  
			type: "POST",  
			url: "stoc-inscriere.jsp?id=mail_to_friend",
			data: dataString, 
			cache: false,
			success: function(msg) {
		    	if (msg == 1) {
					$('#mailtofriend').fadeOut();
			    	$('#popupMailToFriend').append("<div id='succesMessage'></div>");  
			    	$('#succesMessage').html("<h3>Mesajul dumneavoastra a fost trimis cu succes!</h3>");
			    	$('#succesMessage').fadeIn("slow");
		    	} else if (msg == 0) { // eroare
		    		alert("Ne pare rau. Mesajul dumneavoastra nu a fost trimis cu succes. Verificati daca ati completat corect datele din campurile formularului sau reveniti mai tarziu.");
		    	}
			}
		});
		return false;
	});
	
	$("#buttonOP").click(function() {
		ecompletat = eObligatoriu("numeOP", "Va rugam sa completati campul Nume!");
		if (!ecompletat){
			return false;
		}
		
		ecompletat = eObligatoriu("prenumeOP", "Va rugam sa completati campul Prenume!");
		if (!ecompletat){
			return false;
		}
		
		ecompletat = eObligatoriu("ofertaOP", "Va rugam sa completati campul Oferta dvs.!");
		if (!ecompletat){
			return false;
		}
		
		ecompletat = eObligatoriu2("emailOP","telefonOP", "Va rugam sa completati cel putin unul din campurile E-mail sau Telefon!");
		if (!ecompletat){
			return false;
		}
		
		ecompletat = eEmail("emailOP", "Campul Email nu are un format valid!");
		if (!ecompletat){
			return false;
		}
		
		$("#ofertapersonala").submit();
		
		/*var nume = $("#numeOP").val();
		var prenume = $("#prenumeOP").val();
		var oferta = $("#ofertaOP").val();
		var _mail = $("#emailOP").val();
		var telefon = $("#telefonOP").val();
		var acord = $("#acordOP:checked").val();
		var codId = $("#codID").val();
		var nume_auto = $("#nume_auto").val();
		
		var dataString = "nume=" + nume + "&prenume=" + prenume + "&oferta=" + oferta + "&email=" + _mail + 
			"&telefon=" + telefon + "&acord=" + acord + "&codID=" + codId + "&nume_auto=" + nume_auto;
		$.ajax({  
			type: "POST",
			url: "stoc-inscriere.jsp?id=oferta_personala",  
			data: dataString, 
			cache: false,
			success: function(msg) {
		    	if (msg == 1) {
					$('#ofertapersonala').fadeOut();
			    	$('#popupOfertaPersonala').append("<div id='succesMessage'></div>");  
			    	$('#succesMessage').html("<h3>Mesajul dumneavoastra a fost trimis cu succes!</h3>");
			    	$('#succesMessage').fadeIn("slow");
		    	} else if (msg == 0) { // eroare
		    		alert("Ne pare rau. Mesajul dumneavoastra nu a fost trimis cu succes. Verificati daca ati completat corect datele din campurile formularului sau reveniti mai tarziu.");
		    	}
			}
		});
		return false;*/
	});
});
