//append it after submit
var popupClone = null;

jQuery(document).ready(function(){
    //show popup login form
	jQuery("a[rel='message']").bind('click', function() {
         //binds submit event on the cloned form element
        submitFormEvent();

        // Nyro models deletes the div after it's used. That's why we need to clone and append it again after showing this DIV
		var popupDiv = jQuery("#popupContent").clone();

		jQuery("#popupContent").show();
		jQuery.nyroModalManual({content: jQuery("#popupContent"), minWidth: 600, minHeight: 600, autoSizable: false, galleryLinks: '', width: 600, height: 500});

		//append it now - when original is deleted
		popupClone = popupDiv;

        return false;
	});

    jQuery.fn.nyroModal.settings.endRemove = function(elts, settings){
        //append popup div at the closing of nyromodal
        jQuery("#body").append(popupClone);
    };

     	//here you can alter data before output!
	jQuery.fn.nyroModal.settings.endFillContent = function(elts, settings) {
        if (typeof elts.contentWrapper[0].children[0].children[1] != undefined && elts.contentWrapper[0].children[0].children[1].nodeName == 'DIV'){
			var descDiv = elts.contentWrapper[0].children[0].children[1];
			descDiv.style.overflow = "hidden";
		}
	}
});

function submitFormEvent(){
     //submiting form
    jQuery("form").submit(function() {
        
        if (jQuery("#senderData").val() == '') {
            alert("Vpišite vaše podatke.");
            jQuery("#senderData").focus();
            return false;
        }

        if (jQuery("#senderPhone").val() == '') {
            alert("Vpišite vaš telefon.");
            jQuery("#senderPhone").focus();
            return false;
        }
        
        var email = jQuery("#senderEmail").val()
        
        if (email == '') {
            alert("Vpišite vašo e-pošto.");
            jQuery("#senderEmail").focus();
            return false;
        } else {
            if (checkEmail(email) == 0) {
                alert("Vpišite pravilno e-pošto.");
                jQuery("#senderEmail").focus();
                return false;
            }
        }
        
        if (jQuery("#senderMsg").val() == '') {
            alert("Vpišite vaše sporočilo.");
            jQuery("#senderMsg").focus();
            return false;
        }        
                
        //ajax form posting
        jQuery.post("index.php?r=sendMsg", jQuery("form").serialize());

        //disable text areas
        jQuery("textarea").each(function(){
           jQuery(this).attr("disabled", "disabled");
        });
        
        jQuery("input").each(function(){
           jQuery(this).attr("disabled", "disabled");
        });        
        
        //disable text areas
        jQuery(".submitBtns").hide();
        jQuery(".closeBtn").show();

        return false;
    });
    
    function checkEmail(email) {
        var result = jQuery.ajax({url : "index.php?r=validation&email=" + email, async: false}).responseText;
        return result;
    }
}
