var text_fields_id = ["#contest_text", "#contest_name", "#contest_surname", "#contest_mail", "#contest_tel", "#contest_codfisc", "#contest_address", "#contest_cap", "#contest_prov"];

var contest_lang;

var contest_lang_text = {
        "it" : "Post to the wall your original way of life! Descrivici la tua Originalosophy. Lasciaci un messaggio nello spazio qui sotto. Spiegaci con parole tue quando ti senti Originals. Posta fino a un massimo di tre messaggi. <br /> Ai più Originals tra voi regaleremo un viaggio a New York e altri fantastici premi!",
        "en" : "POST TO THE WALL YOUR ORIGINAL WAY OF LIFE! Describe your Originalosophy for us. Leave a message in the space below. Tell us in your own words when you feel Originals. The best phrases will be posted on this websiteâwall!"
};

var contest_lang_textarea_base = {
        "it" : "SONO ORIGINALS QUANDO...",
        "en" : "I'M ORIGINALS WHEN..."
};

var contest_lang_name = {
	"it" : "NOME",
	"en" : "NAME"
};

var contest_lang_surname = {
	"it" : "COGNOME",
	"en" : "SURNAME"
};

var contest_lang_mailinglist = {
        "it" : "Acconsento al trattamento dei miei dati personali ai fini di invio di materiale informativo e pubblicitario.",
        "en" : "I allow for the processing of my personal information for the purpose of receiving information and advertisements."
};

var contest_lang_confirm = {
        "it" : "Accetto il <a href=\"regolamento.pdf\" target=\"_blank\" style=\"color: #c1002b;\">regolamento</a>.",
        "en" : "Pursuant to Legislative Decree no. 196 of 30.06.2003, please be advised that your personal details will be processed by us in accordance with the law, using criteria and systems designed to guarantee security and confidentiality, for the purpose of sending you promotional and advertising material. We may transmit your details to third parties for the same purpose. Under the terms of art. 7 of the aforementioned Legislative Decree, you are entitled to oppose the processing of your details and ask that they be updated, rectified or removed."
};

var contest_lang_submit = {
	"it" : "PARTECIPA",
	"en" : "POST"
};

var contest_lang_agree_error = {
	"it" : "Per partecipare al concorso, è necessario accettare il regolamento. Clicca qui per il tuo assenso.",
	"en" : "You must agree to the personal data processing!"
};

var contest_lang_mail_error = {
	"it" : "Inserire un indirizzo di mail valido!",
	"en" : "Insert a valid email address!"
};

var contest_lang_data_error = {
	"it" : "Tutti i campi sono obbligatori!",
	"en" : "All form fields are required!"
};

var wall_lang_confirm_str = {
        "it" : "Grazie per aver partecipato. Controlla la tua mail per confermare l'invio. Se non ricevi nessun messaggio, prova a controllare nello spam.",
        "en" : "Thank you for participating. Check your e-mail to confirm the message has been sent.  If you do not receive the e-mail, please check your spam folder."
};

function open_contest_popup() {
        $("#contest_text").val(contest_lang_textarea_base[contest_lang]);
        $("#contest_popup_box").css({"display" : "block"});
}

function hide_popup_error() {
        $("input:checkbox[name=contest_confirm[]]").css({"outline" : "none"});

        $(".ui-selectmenu").css({"background" : "#fff"});

        for(var i = 0;i < text_fields_id.length;i++) {
                $(text_fields_id[i]).css({"background" : "#fff"});
        }

        $("#contest_popup_error").html("");
}

function set_field_error(field, error_str) {
        if($(field).attr("name") == "contest_confirm[]")
                $(field).css({"outline" : "3px solid #f00"});
        else
                $(field).css({"background" : "#f00"});

        $("#contest_popup_error").html(error_str);
}

function unset_fields() {
	$("#contest_popup input:text, #contest_popup textarea").val("");

        $("#contest_birth_date_day").selectmenu("value", "empty");
        $("#contest_birth_date_month").selectmenu("value", "empty");
        $("#contest_birth_date_year").selectmenu("value", "empty");

	$("#contest_popup input:checkbox")
		.attr("checked", false)
}

function set_contest_lang(lang) {
	contest_lang = lang;

	$.each($(".contest_lang"), function(key, text_field) {
		var lang_val = $(text_field).attr("lang");

		if(lang == lang_val)
			$(text_field).css({"color" : "#c1002b"});
		else
			$(text_field).css({"color" : "#fff"});
	});

	if(contest_lang == "it") {
		$("#contest_popup_logo_box, #contest_popup_birth_date_box, #contest_popup_tel_box, #contest_popup_codfisc_box, #contest_popup_address_box, #contest_popup_cap_box, #contest_popup_prov_box").css({"display" : "block"});
		$("#contest_popup_text_box").css({"top" : "130px"});
		$("#contest_popup_name_box, #contest_popup_surname_box, #contest_popup_mail_box").css({"top" : "332px"});
		$("#contest_popup_confirm_box").css({"top" : "500px"});
		$("#contest_popup_mailinglist_box").css({"top" : "520px"});
		$("#contest_popup_submit, #contest_popup_error").css({"top" : "574px"});
		$("#contest_popup").css({"height" : "620px"});
	}
	else {
		$("#contest_popup_logo_box, #contest_popup_birth_date_box, #contest_popup_tel_box, #contest_popup_codfisc_box, #contest_popup_address_box, #contest_popup_cap_box, #contest_popup_prov_box").css({"display" : "none"});
		$("#contest_popup_name_box, #contest_popup_surname_box, #contest_popup_mail_box").css({"top" : "252px"});
		$("#contest_popup_text_box").css({"top" : "50px"});
		$("#contest_popup_confirm_box").css({"top" : "305px"});
		$("#contest_popup_mailinglist_box").css({"top" : "385px"});
		$("#contest_popup_submit, #contest_popup_error").css({"top" : "410px"});
		$("#contest_popup").css({"height" : "445px"});
	}

	$("#contest_text").val(contest_lang_textarea_base[lang]);
	$("#contest_popup_text_str").html(contest_lang_text[lang]);
	$("#contest_popup_name_str").html(contest_lang_name[lang]);
	$("#contest_popup_surname_str").html(contest_lang_surname[lang]);
	$("#contest_popup_mailinglist_text").html(contest_lang_mailinglist[lang]);
	$("#contest_popup_confirm_text").html(contest_lang_confirm[lang]);
	$("#contest_popup_submit").html(contest_lang_submit[lang]);
}

function send_data() {

	// Mailinglist

	var mailinglist = false;

        if($("input:checkbox[name='contest_mailinglist[]']").attr("checked") == true) 
		mailinglist = true;

	// Send data

        $.ajax({
        	url: "colmar_sendContest.php?lang=" + contest_lang + "&mailinglist=" + mailinglist,
                type: "POST",
                dataType: "json",
                data : $("#contestForm").serialize(),
                success: function(data) {

        	        // Reset fields

        	        $("#contest_popup_box").css({"display" : "none"});

        	        $.each(text_fields_id, function(key, field) {
        	        	$(field).val("");
        	        });

        	        $("#contest_birth_date_day").selectmenu("value", "empty");
        	        $("#contest_birth_date_month").selectmenu("value", "empty");
        	        $("#contest_birth_date_year").selectmenu("value", "empty");
	
        	        $("#contest_popup input:checkbox").attr("checked", "");

                        // Open confirm popup

                        $("#data_popup_text").html(wall_lang_confirm_str[contest_lang]);
                        open_popup("#data_popup");
		}
	});
}

$(document).ready(function() {
        $("#contest_popup_box_bg").css({"opacity" : 0.3});
        $("#contest_popup_bg").css({"opacity" : 0.8});

        $("#contest_popup_close").mousedown(function() {
                $("#contest_popup_box").css({"display" : "none"});
		unset_fields();
                hide_popup_error();
        });

	$(".contest_lang").mousedown(function() {
		var lang = $(this).attr("lang");
		set_contest_lang(lang);
	});

	set_contest_lang("it");

	$("#contest_popup input:checkbox").css({
        	"position" : "absolute",
        	"width" : "12px",
        	"margin" : "0px",
        	"padding" : "0px"
	});

        $(".contest_birth_date").selectmenu({
                "style" : "dropdown",
                "transferClasses" : true,
                "width" : 85
        });

        $("#contest_popup_box,.ui-selectmenu").mousedown(function() {
                hide_popup_error();
        });

        $("#contest_popup_submit").mousedown(function() {

                // Confirm

                if($("input:checkbox[name='contest_confirm[]']").attr("checked") == "") {
                        set_field_error($("input:checkbox[name='contest_confirm[]']"), contest_lang_agree_error[contest_lang]);
                        return false;
                }

                // Mail

                if(!check_email($("#contest_mail").val())) {
                        set_field_error($("#contest_mail"), contest_lang_mail_error[contest_lang]);
                        return false;
                }

		if(contest_lang == "it") {

			// Cod. fisc.

			if(!check_codfisc($("#contest_codfisc").val())) {
				set_field_error($("#contest_codfisc"), "Inserire un codice fiscale valido!");
				return false;
			}

			// Day

			if($("#contest_birth_date_day").selectmenu("value") == "empty") {
				set_field_error($(".ui-selectmenu").eq(0), "Inserire il giorno di nascita!");
				return false;
			}

			// Month

			if($("#contest_birth_date_month").selectmenu("value") == "empty") {
				set_field_error($(".ui-selectmenu").eq(1), "Inserire il mese di nascita!");
				return false;
			}

			// Year

			if($("#contest_birth_date_year").selectmenu("value") == "empty") {
				set_field_error($(".ui-selectmenu").eq(2), "Inserire l'anno di nascita!");
				return false;
			}

	                // Form fields

        	        for(var i = 0;i < text_fields_id.length;i++) {
                	        if($(text_fields_id[i]).val() == "") {
                	                set_field_error($(text_fields_id[i]), contest_lang_data_error[contest_lang]);
                	                return false;
                	        }
                	}

	                // Send data

			$.ajax({
				url: "colmar_checkContestData.php",
				type: "POST",
				dataType: "json",
				data : $("#contestForm").serialize(),
				success: function(data_error) {
					if(data_error == "true")
						return false;

					send_data();
				}
			});
		}
		else {
               	        if($("#contest_name").val() == "") {
               	                set_field_error($("#contest_name"), contest_lang_data_error[contest_lang]);
               	                return false;
               	        }

               	        if($("#contest_surname").val() == "") {
               	                set_field_error($("#contest_surname"), contest_lang_data_error[contest_lang]);
               	                return false;
               	        }

               	        if($("#contest_mail").val() == "") {
               	                set_field_error($("#contest_mail"), contest_lang_data_error[contest_lang]);
               	                return false;
               	        }

			send_data();
		}
        });
});

