//variables page inscription
var verif_mail = 0;
var verif_name = 0;
var verif_surname = 0;
var verif_civility = 0;
var verif_empty = 0;
var birth = 0;
var travel = 0;
var verif_postal = 0;
var well_img = "<img src='http://static.p-comme-performance.com/overlay.prisma-performance.com/jeu_geo2/static/images/formulaireImages/valid_icon.png' width='20' />";
var wrong_img = "<img src='http://static.p-comme-performance.com/overlay.prisma-performance.com/jeu_geo2/static/images/formulaireImages/wrong_icon.png' width='20' />";

//variables page resultat
var i = 5;
var actual_height = 0;


function verif(email) { // vérif validité email par REGEXP
   var reg = /^[a-z0-9._-]+@[a-z0-9.-]{2,}[.][a-z]{2,3}$/
   //var reg = /^\\w[\\w+\.\-]*@[\\w\-]+\.\\w[\\w+\.\-]*\\w$/
   return (reg.exec(email)!=null)
}

function verif_birth(){
    birthday = $('#birthday').val();
    birthmonth = $('#birthmonth').val();
    birthyear = $('#birthyear').val();
    if(birthday !='NULL' && birthmonth != 'NULL' && birthyear !='NULL'){
	$("#birth_verif").html(well_img);
	birth = 2;
    }
    else{
	$("#birth_verif").html(wrong_img);
	birth = 0;
    }
}

//this func makes all the verification of the input on subscribe page
function verif_form(input,event){
    if(event!=true){
		switch (input){
	
		    case 'civility':
			checked_civility = $('input[type=radio][name=civ]:checked').length;
			if(checked_civility==1){$("#civility_verif").html(well_img);verif_civility = 2;}
			else{$("#civility_verif").html(wrong_img);verif_civility = 0}
		    break;

                    case 'optin':
                        checked_optin = $('input[type=radio][name=optin]:checked').length;
                        if(checked_optin==1){$("#optin_verif").html(well_img);verif_optin = 2;}
                        else{$("#optin_verif").html(wrong_img);verif_optin = 0}
                    break;

		    case 'name' :
			name = $("#input_name").attr('value');
			if(name !=''){$("#name_verif").html(well_img);verif_name = 2;}
			else{$("#name_verif").html(wrong_img);verif_name = 0}
		    break;
	
		    case 'surname' :
			surname = $("#input_surname").attr('value');
			if(surname !=''){$("#surname_verif").html(well_img);verif_surname = 2;}
			else{$("#surname_verif").html(wrong_img);verif_surname = 0}
		    break;
	
		    case 'mail' :
			mail = $("#input_mail").attr('value');
			if(mail !='' && verif(mail)){$("#mail_verif").html(well_img);verif_mail = 2;}
			else{$("#mail_verif").html(wrong_img);verif_mail = 0}
		    break;
	
		    case 'birth' :
			verif_birth();
		    break;

             	    case 'travel-mode':
                        checked_travel = $('input[type=radio][name=travel-mode]:checked').length;
			if(checked_travel==1){$("#travel-verif").html(well_img);travel = 2;}
			else{$("#travel-verif").html(wrong_img);travel = 0}
                    break;
		    
		    case 'travel-mail':
		    	clientMail = $("#input_mail").attr('value');
	    		clientFriendMail = $("#travel-with").attr("value");	
	    		if(clientMail!=clientFriendMail){$("#mail_verif").html(well_img);verif_mail = 2;}
				else{$("#mail_verif").html(wrong_img);verif_mail = 0}
	    		break;
		    
		    case 'postalCode' :
			postal = $("#postal_code").attr("value");
			var reg = new RegExp("[0-9]{5}");
			if(reg.test(postal)){$("#postal_verif").html(well_img);verif_postal = 2;}
			else{$("#postal_verif").html(wrong_img);verif_postal = 0}
		    break;
		}
   	}
   	else{
   		verif_form('civility');
		verif_form('optin');
   		verif_form('name');
   		verif_form('surname');
		verif_form('mail');
   		verif_form('birth');
   		verif_form('postalCode');
   		verif_form('travel-mode');
   		verif_form('travel-mail');	
   	}
    
}

//removing input for the godfather(result) page
function removeInput(nbr){
    $("#row_"+nbr).remove();
    i--;
    actual_height = parseInt($("#detail_container").css('height').substr(0,3));
    $("#remove_case_"+i).html('<img src="./static/images/gagnantImages/supprimer.png" onclick="removeInput('+i+')" id="remove_input" />');
    $("#detail_container").css('height', (actual_height-28)+'px');
    $("#textbox").css('height', (actual_height-28)+'px');
}

$(document).ready(function(){
    if(alerterror==1){
	$("#errorline").html('<td colspan="3"><div class="errormsg">Le formulaire contient des erreurs</div></td>');
	    $("#errorline").show();
    }
    //verification on load(subscribe)
    if($('input[type=radio][name=civ]:checked').length>0)verif_form('civility');
    if($('input[type=radio][name=optin]:checked').length>0)verif_form('optin');
    if($('#input_name').attr('value')!='')verif_form('name');
    if($('#input_surname').attr('value')!='')verif_form('surname');
    if($('#input_mail').attr('value')!='')verif_form('mail');
    if($('#birthday').attr('value')!='NULL')verif_form('birth');
    if($('#birthmonth').attr('value')!='NULL')verif_form('birth');
    if($('#birthyear').attr('value')!='NULL')verif_form('birth');
    if($('#postal_code ').attr('value')!='')verif_form('postalCode');
    if($('input[type=radio][name=travel-mode]:checked').length>0)verif_form('travel-mode');
    if($('input[type=radio][name=optin]:checked').length>0)verif_form('optin');
    //verification on click(subscribe)
    $('input[type=radio][name=civ]').click(function(){verif_form('civility')});
    $('input[type=radio][name=optin]').click(function(){verif_form('optin')});
    $("#input_name").blur(function(){verif_form('name')});
    $("#input_surname").blur(function(){verif_form('surname')});
    $("#input_mail").blur(function(){verif_form('mail')});
    $("#birthday").change(function(){verif_form('birth')});
    $("#birthmonth").change(function(){verif_form('birth')});
    $("#birthyear").change(function(){verif_form('birth')});
    $("#postal_code").blur(function(){verif_form('postalCode')});
    $("#travel-with").change(function(){verif_form('travel-mail')});
    $('input[type=radio][name=travel-mode]').click(function(){verif_form('travel-mode')});
    //displaying the page win or lost
    if(winlost==1){
	$("#textbox-win").show();
	$("#textbox-lost").hide();
	$("#win-or-lost-header").css("background-image",'url(http://static.p-comme-performance.com/overlay.prisma-performance.com/jeu_geo2/static/images/gagnantImages/header-win.jpg)');
    }
    else if(winlost==2){
	$("#textbox-lost").show();
	$("#textbox-win").hide();
	$("#win-or-lost-header").css("background-image",'url(http://static.p-comme-performance.com/overlay.prisma-performance.com/jeu_geo2/static/images/gagnantImages/header-lost.jpg)');
    }

    //adding an input to the godfather(result) page
    $("#more_input").click(function(){
	actual_height = parseInt($("#detail_container").css('height').substr(0,3));
	$("#detail_container").css('height', (actual_height+28)+'px');
	$("#textbox").css('height', (actual_height+28)+'px');
	$("#remove_case_"+i).html('');
	i++;
	$("#row_"+(i-1)).after('<tr id="row_'+i+'"><td>e-mail '+i+'</td><td><input type="text" class="parrain-input-adress" name="mail_'+i+'" /></td><td id="remove_case_'+i+'"><img src="./static/images/gagnantImages/supprimer.png" onclick="removeInput('+i+')" id="remove_input" /></td></tr>');

    });

    //events submit on the subscribe button
    $("#submitForm").click(function(){
    verif_form('',true);
	if(verif_mail==2){
	  //$("#optinInput").attr('value',1);
	  document.insc.submit();
	  $("#errorline").html('');
	}
	else{
	    $("#errorline").html('<td colspan="3"><div class="errormsg">Le formulaire contient des erreurs</div></td>');
	    $("#errorline").show();
	}
    });
/*
    $("#participateonlygame").click(function(){
	 verif_form('',true);
	if(verif_mail==2){
	  $("#optinInput").attr('value',0);
	  document.insc.submit();
	  $("#errorline").html('');
	}
	else{
	    $("#errorline").html('<td colspan="3"><div class="errormsg">Le formulaire contient des erreurs</div></td>');
	    $("#errorline").show();
	}
    });
*/
    $("#travel-friend").click(function(){
        $("#travel-row").show();
    });
    $("#travel-alone").click(function(){
        $("#travel-row").hide();
    });


    $("#inscriremesamis").click(function(){
	document.godfather.submit();
    });
});

