﻿/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;


var popupStatusMediaNotOk = 0;

//loading popup with jQuery magic!
function loadMediaNotOkPopup(){
	//loads popup only if it is disabled
	if(popupStatusMediaNotOk==0){
		$("#backgroundPopupMediaNotOk").css({
			"opacity": "0.7"
		});
		$("#backgroundPopupMediaNotOk").fadeIn("slow");
		$("#popupMediaNotOk").fadeIn("slow");
		
		popupStatusMediaNotOk = 1;
	}
}

//disabling popup with jQuery magic!
function disableMediaNotOkPopup(){
    //disables popup only if it is enabled
    
    if (popupStatusMediaNotOk == 1) {
        
		$("#backgroundPopupMediaNotOk").fadeOut("slow");
		$("#popupMediaNotOk").fadeOut("slow");
		popupStatusMediaNotOk = 0;
	}
}

//centering popup
function centerMediaNotOkPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupMediaNotOk").height();
	var popupWidth = $("#popupMediaNotOk").width();
	//centering
	$("#popupMediaNotOk").css({
		"position": "absolute",
		"top": windowHeight / 2 - popupHeight / 2 + $(window).scrollTop() + "px",
		"left": windowWidth / 2 - popupWidth / 2 + $(window).scrollLeft() + "px"
	});
	//only need force for IE6
	
	$("#backgroundPopupMediaNotOk").css({
		"height": windowHeight
	});
	
}

function popUpMediaNotOk() {        
    //centering with css
    centerMediaNotOkPopup();
    //load popup
    loadMediaNotOkPopup();
}

function MediaNotOk() {
    
    var error = false;
    var errorMsg = "";
    if ($("#txtSenderName").val() == "" || $("#txtSenderName").val() == null) {
        errorMsg += "<li>Vul alstublieft naam afzender in</li>";
        error = true;
    }
    if ($("#txtSenderEmail").val() == "" || $("#txtSenderEmail").val() == null) {
        errorMsg += "<li>Vul alstublieft e-mail afzender in</li>";
        error = true;
    }
    else if (echeck($("#txtSenderEmail").val()) == false) {
        errorMsg += "<li>Vul alstublieft e-mail afzender in</li>";
        error = true;
    }
    if ($("#txtMessage").val() == "" || $("#txtMessage").val() == null) {
        errorMsg += "<li>U moet een bericht meesturen</li>"
        error = true;
    }
//    if (error == true) {
//        errorMsg = "<ul style='color:red;'>" + errorMsg + "</ul>";
//        $("#errorMsg").html(errorMsg);
//        return false;
//    }
//    else {
    $("#errorMsg").html("");    
    disableMediaNotOkPopup();
//    return true;
//    }
    return true;
}

function echeck(str) {

    var msgid = document.getElementById("msg");
    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        //alert("Invalid E-mail ID")
        msgid.innerHTML = "Invalid  Email ID";
        return false
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        //alert("Invalid E-mail ID")
        msgid.innerHTML = "Invalid  Email ID";
        return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        //alert("Invalid E-mail ID")
        msgid.innerHTML = "Invalid  Email ID";
        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        //alert("Invalid E-mail ID")
        msgid.innerHTML = "Invalid  Email ID";
        return false
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        //alert("Invalid E-mail ID")
        msgid.innerHTML = "Invalid  Email ID";
        return false
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        //alert("Invalid E-mail ID")
        msgid.innerHTML = "Invalid  Email ID";
        return false
    }

    if (str.indexOf(" ") != -1) {
        //alert("Invalid E-mail ID")
        msgid.innerHTML = "Invalid  Email ID";
        return false
    }

    return true
}
    

//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

    //LOADING POPUP
    //Click the button event!
    $("#MediaNotOkButton").click(function() {
        //centering with css		
        centerMediaNotOkPopup();
        //load popup
        loadMediaNotOkPopup();
    });

    //CLOSING POPUP
    //Click the x event!
    $("#popupMediaNotOkClose").click(function() {        
        disableMediaNotOkPopup();
    });

    //CLOSING POPUP
    //Click the x event!
    $("#btnSubmitMediaNotOk1").click(function() 
    {
        var error = false;
        var errorMsg = "";
        if ($("#txtSenderName").val() == "" || $("#txtSenderName").val() == null) 
        {
            errorMsg += "<li>Vul alstublieft naam afzender in</li>";
            error = true;
        }
        if ($("#txtSenderEmail").val() == "" || $("#txtSenderEmail").val() == null) 
        {
            errorMsg += "<li>Vul alstublieft e-mail afzender in</li>";
            error = true;
        }
        else if (echeck($("#txtSenderEmail").val()) == false) 
        {
            errorMsg += "<li>Vul alstublieft e-mail afzender in</li>";
            error = true;
        }
        if ($("#txtMessage").val() == "" || $("#txtMessage").val() == null) 
        {
            errorMsg += "<li>U moet een bericht meesturen</li>"
            error = true;
        }
        if (error == true) 
        {
            errorMsg = "<ul style='color:red;'>" + errorMsg + "</ul>";
            $("#errorMsg").html(errorMsg);
            return false;
        }
        else 
        {
            $("#errorMsg").html("");
            alert('Bedankt, uw bericht is verstuurd');
            disableMediaNotOkPopup();
            return true;
        }
    });

    //Click out event!
    $("#backgroundPopupMediaNotOk").click(function() {
        disableMediaNotOkPopup();
    });
    //Press Escape event!
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatusMediaNotOk == 1) {
            disableMediaNotOkPopup();
        }
    });

});