﻿/***************************/
//@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 popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup").css({
			"opacity": "0.7"
		});
		$("#backgroundPopup").fadeIn("slow");
		$("#popupContact").fadeIn("slow");

        $("#txtSenderName").val(""); 
        $("#txtSenderEmail").val(""); 
        $("#txtReceiverName").val(""); 
        $("#txtReceiverEmail").val("");
		$("#txtMessage").val(""); 
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup").fadeOut("slow");
		$("#popupContact").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact").height();
	var popupWidth = $("#popupContact").width();
	//centering
	$("#popupContact").css({
		"position": "absolute",
		//"top": ($(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");    this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");    
		"top": (windowHeight-popupHeight)/2 + "px",
		"left": (windowWidth-popupWidth)/2 + "px"
	});
	//only need force for IE6
	
	$("#backgroundPopup").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function() {

    //LOADING POPUP
    //Click the button event!
    $("#button").click(function() {
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });
    //LOADING POPUP
    //Click the button event for media page.
    $(".sendFrenBtnMid").click(function() {
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });
    $(".sendfavoriteBtnMid").click(function() {
        //centering with css
        centerPopup();
        //load popup
        loadPopup();
    });
    //CLOSING POPUP
    //Click the x event!
    $("#popupContactClose").click(function() {
        $("#errorMsg").html("");
        disablePopup();
    });

    //Click out event!
    $("#backgroundPopup").click(function() {
        $("#errorMsg").html("");
        disablePopup();
    });
    //Press Escape event!
    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            $("#errorMsg").html("");
            disablePopup();
        }
    });
    function echeck(str) {
        
        
        var msgid = document.getElementById("errorMsg"); 
        //put the name of the element where you want to show your error messages
        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
    }
    $("#btnSubmit").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 ($("#txtReceiverName").val() == "" || $("#txtReceiverName").val() == null) {
            errorMsg += "<li>Vul alstublieft naam ontvanger in</li>";
            error = true;
        }
        if ($("#txtReceiverEmail").val() == "" || $("#txtReceiverEmail").val() == null) {
            errorMsg += "<li>Vul alstublieft e-mail ontvanger in</li>";
            error = true;
        }
        else if (echeck($("#txtReceiverEmail").val()) == false) {
            errorMsg += "<li>Vul alstublieft e-mail ontvanger 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("");
            disablePopup();
            return true;
        }

    });
});

function GetUrl(evt)
{
    evt = evt || window.event;
    var target = evt.target || evt.srcElement;
    hdnid= document.getElementById("hdnurl");
    hdnid.value=target;
}


