$(document).ready(function() {

	var flags = $('input[name="flag"]');
	
	if(flags == null) return true; /* to be fixed */
	$(flags).eq(0).val('0');
	$(flags).eq(1).val('1');
	
	
	var newsTitle = $('h1.newsSingleTitle').text();
	
	$(flags).eq(1).parent().parent().find('p.pageTitle').html(newsTitle);

    $('.colleague').click(function(){
        $('#right .tx-friend-pi1').fadeIn('slow');
		$('#right form').next('#contentmessages').html('');
    });
	

    $('.closeSend').click(function() {
        $(this).parent().parent('.tx-friend-pi1').fadeOut('slow');
		$(this).parent().find("form input[name='from']").val('Your Name');
		$(this).parent().find("form input[name='nameto']").val('Your Colleague\'s Name');
		$(this).parent().find("form input[name='emailto']").val('Your Colleague\'s Email Address');
    });

    $('.recommend').click(function(){
        $('#header-right .tx-friend-pi1').fadeIn('slow');
		$('#header-right form').next('#contentmessages').html('');
    });

    $(".submit-recfriend").click(function(e) {
        e.preventDefault();
		form = $(this).parent();
        var from = $(form).find("input#from").val();
        var nameto = $(form).find("input#nameto").val();
        var emailto = $(form).find("input#emailto").val();
        var address = $(form).find("input#address").val();
		var flag = $(form).find("input[name='flag']").val();

        var dataString = 'from='+ from + '&nameto=' + nameto + '&emailto=' + emailto + '&address=' + address + '&flag=' + flag;

        var message;
        
                if((from != " " && from != "Your Name") && (nameto != " " && nameto != "Your Colleague's Name") && (emailto != " " && emailto != "Your Colleague's Email Address"))
                {
                    if(validEmail(emailto))
                    {
                        $.ajax({
                                type: "POST",
                                url: "../../../../typo3conf/ext/friend/pi1/sendmail.php",
                                data: dataString,

                                success: function(response) {
                                    response = eval('('+response+')');
                                    if(response['result'] == 1)
                                    {
                                        message = "<div class='h3Send'>Message sent!</div>";
										
										$(form).find("input[name='from']").val('Your Name');
										$(form).find("input[name='nameto']").val('Your Colleague\'s Name');
										$(form).find("input[name='emailto']").val('Your Colleague\'s Email Address');
										
										setTimeout(function () {
											$(form).parent().parent().parent('.tx-friend-pi1').fadeOut();

											
										}, 1500);
										
										
										
                                    }else{
                                        message = "";
                                        for (N in response['error'])
                                        {
                                          message +=   "<div class='h3Send'>"+response['error'][N]+"</div>"
                                        }
                                        message += "<div class='h3Send'>Message has not been sent!</div>";
                                    }
                                    $(form).next('#contentmessages').html(message)
                                            .hide()
                                            .fadeIn(1500, function() {
                                    });
                                  }
                            });
                    }else{
                        message = "<div class='h3Send'>Email Incorrect!</div>";
                        $(form).next('#contentmessages').html(message)
                            .hide()
                            .fadeIn(1500, function() {
                        });
                    }
                }else{
                    message = "";
                    if (from == " " || from == "Your Name")
                    {
                        message += "<div class='h3Send'>Your Name is Required!</div>";
                    }
                    if (nameto == " " || nameto == "Your Colleague's Name")
                    {

                        message += "<div class='h3Send'>Your Colleague's Name is Required!</div>";
                    }
                    if (emailto == " " || emailto == "Your Colleague's Email Address")
                    {
                        message += "<div class='h3Send'>Your Colleague's Email Address is Required!</div>";
                    }
                    $(form).next('#contentmessages').html(message)
                    .hide()
                    .fadeIn(1500, function() {
                        
                    });
                }
    });

});





// Function for Validate email in JS
function validEmail(mail) {
    invalidChars = " /:,;"

    if (mail == "") { // cannot be empty
        return false
    }
    for (i=0; i<invalidChars.length; i++) { // does it contain any invalid characters?
        badChar = invalidChars.charAt(i)
        if (mail.indexOf(badChar,0) > -1) {
            return false
        }
    }
    atPos = mail.indexOf("@",1) // there must be one "@" symbol
    if (atPos == -1) {
        return false
    }
    if (mail.indexOf("@",atPos+1) != -1) { // and only one "@" symbol
        return false
    }
    periodPos = mail.indexOf(".",atPos)
    if (periodPos == -1) { // and at least one "." after the "@"
        return false
    }
    if (periodPos+3 > mail.length) { // must be at least 2 characters after the "."
        return false
    }
    return true
}