﻿
var Register = function($){
	/**
	 * private methods and variables
	 */
	var priv = {
	    
	};
	
	/**
	 * public methods
	 */
	return {	
		OnReady	: function(){
		 },
		 
		 //return available days for selected month and year
         DateSelectChange : function(month,year,day){
                var options="";
                var i=0;
                var maxDays=0;
                var leapYear = 0;
                if((year*1)%4 == 0) leapYear = 1;
                
                //feburary 28 days 
                if(month == 2 && leapYear == 0) maxDays = 29;
                
                //feburary 29 days
                else if(month == 2 && leapYear == 1) maxDays = 30;
                
                //any other month
                else
                {
                    //months that has 30 days
                    if(month == 4 || month == 6 || month == 9 || month == 11 ) maxDays = 31;
                
                    //months that has 31 days
                    else maxDays = 32;
                }
                
                options = options + "<option value='"+messages.ABBREVIATION_DAY+"'>"+messages.ABBREVIATION_DAY+"</option>";
                var selected;
                for (i=1; i<maxDays; i++)
                {
                    if(i==day) selected = "selected='selected'";
                    else selected = "";
                    if(i<10)
                        options = options + "<option "+selected+" value='0"+i+"'>0"+i+"</option>";
                    else
                        options = options + "<option "+selected+" value='"+i+"'>"+i+"</option>";
                }
                    
                return options;
         },
            
         //check email validation
         CheckMailValidation : function(email){
                var Error = "";
                //firstcheck if a mail id has entered
                if(email=="")
                {
                    Error = messages.VALIDATOR_EMAIL_ERROR_MESSAGE;
                }
                //check if mail id is valid
                else{
                    var emailRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                    if(!emailRegExp.test(email))
                    {
                         Error = messages.VALIDATOR_EMAIL_REGULAR_ERROR_MESSAGE;
                    }
                }
                    
                return Error;
         },
         
         //check login inputs validation
         CheckLoginValidation : function(email,password,pwdBox){
                
                var Error = "";
                //firstcheck if a mail id has entered
                if(email=="")
                {
                    Error = messages.VALIDATOR_EMAIL_ERROR_MESSAGE;
                }
                //check if mail id is valid
                else{
                    var emailRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                    if(!emailRegExp.test(email))
                    {
                         Error = messages.VALIDATOR_EMAIL_REGULAR_ERROR_MESSAGE;
                    }
                    else if(password=="") {
                        Error = messages.VALIDATOR_REQUIRED_PWD_MESSAGE;
                        pwdBox.css("background","#f99");
                    }
                    else pwdBox.css("background","#fff");
                }
                    
                return Error;
         },
         //check login inputs validation
         CheckLoginValidationInLogin : function(email,password,pwdBox,mailBox){
                pwdBox.css("background","#fff");
                mailBox.css("background","#fff");
                var Error = "";
                //firstcheck if a mail id has entered
                if(email=="")
                {
                    Error = messages.VALIDATOR_EMAIL_ERROR_MESSAGE;
                    mailBox.css("background","#f99");
                }
                //check if mail id is valid
                else{
                    var emailRegExp = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
                    if(!emailRegExp.test(email))
                    {
                         Error = messages.VALIDATOR_EMAIL_REGULAR_ERROR_MESSAGE;
                         mailBox.css("background","#f99");
                    }
                    else if(password=="") {
                        Error = messages.VALIDATOR_REQUIRED_PWD_MESSAGE;
                        pwdBox.css("background","#f99");
                    }
                    else{
                        pwdBox.css("background","#fff");
                        mailBox.css("background","#fff");
                    }
                }
                    
                return Error;
         },
         
         //check email existence
         CheckMailExistence : function(email){
                var mailExists = -2;
                
                $.ajax({
                    type: "POST",
                    url:  resources.path_prefix + "/DataProvider/Register.ashx",
                    data: "email="+email+"&job=1", //job=1 --> CheckEmail 
                    async: false,
                    success: function(data){
                        mailExists = data;
                    },
                    error: function(data){
                    }
                });     
                    
                return mailExists;
         },
         //open the register form for the new participant
         OpenRegisterForm : function(header,text){
                $("div#Login").hide();
                $("div#RegisterNewText").show();
                $("div#RegisterNewForm1").show();
                $("div#RegisterNewForm2").show();
                $("span#toStep2").show();
                $("#RegisterNewText").html("<h4>"+header+"</h4>"+"<p>"+text+"</p>");
         },
         //send request to server for the partcipant login from register page
         ParticipantLogin : function(mail,password){
                var LoginRes="";
                $.ajax({
                   type: "POST",
                   url:  resources.path_prefix + "/DataProvider/Register.ashx",
                   data: "email="+mail+"&password="+password+"&job=5", //job=5 --> Login
                   async: false,
                   success: function(data){
                        LoginRes=data;
                   },
                   error: function(data){
                   }
               });   
               return LoginRes;  
         },
         //send request to server for the partcipant login from login page
         ParticipantLogin2 : function(mail,password){
                var LoginRes="";
                $.ajax({
                   type: "POST",
                   url:  resources.path_prefix + "/DataProvider/Register.ashx",
                   data: "email="+mail+"&password="+password+"&is_login="+true+"&job=5", //job=5 --> Login
                   async: false,
                   success: function(data){
                        LoginRes=data;
                   },
                   error: function(data){
                   }
               });   
               return LoginRes;  
         },
         //send request to server for requesting password in register page
         RequestPassword : function(mail,password){
                $("#forgotPwd").show();
                $.ajax({
                   type: "POST",
                   url:  resources.path_prefix + "/DataProvider/Register.ashx",
                   data: "email="+mail+"&job=6", //job=6 --> RequestPassword
                   success: function(data){
                        $("#forgotPwd").fadeOut(2500);
                        if(data.length>1) // a sentence returned as a response and its about validatin of mail id
                        {
                            $("#mailError").html(data);
                        }
                        else if(data=='1')//new password sent
                        {
                            $(".pwdForget").html("<span class='msg-success'>"+messages.USERNAME_AND_PASSWORD_SENT+"</span");
                        }
                        else //email couldnt found or an error occured
                        {
                             $("#mailError").html(messages.EMAIL_UNKNOWN);
                        }
                   },
                   error: function(data){
                   }
               });   
         },
         //send request to server for requesting password in login page
         RequestPassword2 : function(mail,password){
                $("#forgotPwd").show();
                $.ajax({
                   type: "POST",
                   url:  resources.path_prefix + "/DataProvider/Register.ashx",
                   data: "email="+mail+"&is_login="+false+"&job=6", //job=6 --> RequestPassword
                   success: function(data){
                        $("#forgotPwd").fadeOut(2500);
                        if(data.length>1) // a sentence returned as a response and its about validatin of mail id
                        {
                            $("#mailError").html(data);
                        }
                        else if(data=='1')//new password sent
                        {
                            $(".pwdForget").html("<div style='clear:both'></div><div class='msg-success-home'>"+messages.USERNAME_AND_PASSWORD_SENT+"</div");
                        }
                        else //email couldnt found or an error occured
                        {
                             $("#mailError").html(messages.EMAIL_UNKNOWN);
                        }
                   },
                   error: function(data){
                   }
               });   
         },
         //keep the checkbox of rules checked status for server side checking
         AgreeStatements : function(isChecked){
                $.ajax({
                   type: "POST",
                   url:  resources.path_prefix + "/DataProvider/Register.ashx",
                   data: "is_checked="+isChecked+"&job=7", //job=7 --> AgreementControl
                   async: false,
                   success: function(data){
                   },
                   error: function(data){
                   }
               });   
         },
         //send request to server for checking the validation of form inputs
         ParticipantRegisterStep1 : function(firstname,middlename,lastname,day,month,year,gender,address,housenr,
                                            additive,zipcode,city,phone,mobile,club,coach,clubInfo){
                var errorMessages="";
                $.ajax({
                   type: "POST",
                   url:  resources.path_prefix + "/DataProvider/Register.ashx",
                   data: "team_name=team name"+
                   "&email=aaa@aa.com"+
                   "&first_name="+firstname+
                   "&middle_name="+middlename+
                   "&last_name="+lastname+
                   "&birth_date="+day+"-"+month+"-"+year+
                   "&gender="+gender+
                   "&address="+address+
                   "&house_nr="+housenr+
                   "&additive="+additive+
                   "&zip_code="+zipcode+
                   "&city="+city+
                   "&phone="+phone+
                   "&mobile_phone="+mobile+
                   "&club_id="+club+
                   "&coach_id="+coach+
                   "&club_info="+clubInfo+
                   "&job=4", //job=4 --> ValidateForm
                   async: false,
                   success: function(data){
                       errorMessages = data;
                   },
                   error: function(data){
                   }
               });  
               return errorMessages;  
         },
         //check form validation
         CheckFormValidation : function(errorList){
                var errors=0;
                var mobileError="";
                var tlfError="";
                if(errorList.length>0){
                    while(errorList.indexOf('#')>=0){
                        errorList=errorList.substring(errorList.indexOf('#')+1);
                        var field = errorList.substring(0,errorList.indexOf('#'));
                        errorList=errorList.substring(errorList.indexOf('#')+1); 
                        var error = errorList.substring(0,errorList.indexOf('#'));
                        var id;
                        var exField=field;
                        if(field=="mobile") mobileError=error;
                        else if(field=="phone") tlfError=error;
                        if(field=="mobile" && $("li#phone").attr("class")!="error") field="phone";
                        if(error.length > 0) {
                            id = "li#"+field;
                            $(id).attr("class","error");
                            id = "li#"+field+" p";
                            $(id).html(error);
                            errors = errors + 1;
                            if(exField=="mobile"){ $("input#TB_Tlf").css("background","#fff"); $("input#TB_Mobile").css("background","#f99");}
                            else if(exField=="phone"){ $("input#TB_Tlf").css("background","#f99"); $("input#TB_Mobile").css("background","#fff");}
                        }
                        else{
                            id = "li#"+field;
                            $(id).attr("class","");
                            id = "li#"+field+" p";
                            $(id).html("");
                        }
                        if(mobileError.length>1 && tlfError.length>1){
                            $("input#TB_Tlf").css("background","#f99"); 
                            $("input#TB_Mobile").css("background","#f99");
                        }
                        else if(mobileError.length<1 && tlfError.length<1){
                            $("input#TB_Tlf").css("background","#fff"); 
                            $("input#TB_Mobile").css("background","#fff");
                        }
                    }
                    
                }
                return errors;
         },
         
         //open step 3
         OpenStep2 : function(){
               $("div#Login").hide();
               $("div#RegisterNewText").hide();
               $("div#RegisterNewForm1").hide();
               $("div#RegisterNewForm2").hide();
               $("span#toStep2").hide();
               $("div#Step1Content").hide();
               $("div#Step1").attr("class","step")
               $("div#Step2").attr("class","step opened")
               $("div#Step2Content").show();
         },
         
         //check team name validation
         CheckTeamNameValidation : function(teamname){
               var validError="";
               if(teamname=="")
                    validError = messages.VALIDATOR_TEAM_NAME_ERROR_MESSAGE
               else{
                   var teamNameRegExp = /^([a-zA-Z0-9\s\-ÁÂÃÄÅÆÇÈËÉÊÌÏÍÎÑÒÓÔÕÖØÙÚÛÜÝÞßàáâãäåæçèéëêìíîïñòóôõöøùúûüýÿþð]){3,20}$/;
                   if(!teamNameRegExp.test(teamname))
                        validError = messages.VALIDATOR_TEAM_NAME_REGULAR_ERROR_MESSAGE;
               }
               return validError;
         },
         
         //check team name validation
         CheckTeamNameExistence : function(teamname,mail){
               $.ajax({
                   type: "POST",
                   url:  resources.path_prefix + "/DataProvider/Register.ashx",
                   data: "team_name="+teamname+"&email="+mail+"&job=2", //job=2 --> CheckTeamName
                   async: false,
                   success: function(data){
                       if(data=="False"){
                           $(".TeamNameNotAvailable").hide();
                           $(".TeamNameAvailable").show();
                           $("#teamError").html("");
                           $(".TeamNameCheck").val("1")
                           if($("#SelectedCoach").val()>0){
                               $("#toStep3").css("background","url(../images/button-l.gif) no-repeat");
                               $("#toStep3 a").css("cursor","pointer");
                               $("#toStep3 a").css("background","url(../images/button-r-blue.gif) no-repeat 100% 0");
                               $("#toStep3 a").removeClass("inactive");
                           }
                       }
                       else{
                           $(".TeamNameAvailable").hide();
                           $(".TeamNameNotAvailable").show();
                           $("#teamError").html(messages.SELECT_ANOTHER_TEAM_NAME);
                           $(".TeamNameCheck").val("-1")
                       }
                   },
                   error: function(data){
                   }
               });     
         },
         CheckPasswordValidation : function(pwd1,pwd2,pwd3){
                $(".box ul li").attr("class","")
                $("#oldPassword p").html("");
                $("#newPassword1 p").html("");
                $("#newPassword2 p").html("");
                
                var Error = 1;
                //first check if current pwd has entered
                if(pwd1=="")
                {
                    Error = 0;
                    $("#oldPassword p").html(messages.VALIDATOR_REQUIRED_CURRENT_PWD_MESSAGE);
                    $("#oldPassword").attr("class","error");
                }
                //check if current pwd is valid
                else if(pwd1!="" && (pwd1.length<5 || pwd1.length>20))
                {
                    Error = 0;
                    $("#oldPassword p").html(messages.VALIDATOR_PWD_ERROR_MESSAGE);
                    $("#oldPassword").attr("class","error");
                }
                //check if first pwd has entered
                else if(pwd2=="")
                {
                    Error = 0;
                    $("#newPassword1 p").html(messages.VALIDATOR_REQUIRED_PWD_MESSAGE);
                    $("#newPassword1").attr("class","error");
                }
                //check if first pwd is valid
                else if(pwd2!="" && (pwd2.length<5 || pwd2.length>20))
                {
                    Error = 0;
                    $("#newPassword1 p").html(messages.VALIDATOR_PWD_ERROR_MESSAGE);
                    $("#newPassword1").attr("class","error");
                }
                ///check if repeated pwd has entered
                else if(pwd3=="")
                {
                    Error = 0;
                    $("#newPassword2 p").html(messages.VALIDATOR_REQUIRED_REPEATED_PWD_MESSAGE);
                    $("#newPassword2").attr("class","error");
                }
                //check if pwd and repeated pwd is equal
                else if(pwd3!="" && pwd3!=pwd2)
                {
                    Error = 0;
                    $("#newPassword2 p").html(messages.PASSWORDS_DOES_NOT_MATCH);
                    $("#newPassword2").attr("class","error");
                }
                    
                return Error;
         },
         //check if all required steps has done for registration
         CheckRegisterAvailability : function(){
                 var availability = "";
                //check if team name availability has checked
                if($(".TeamNameCheck").val()==0)
                   availability = messages.CHECK_TEAM_NAME_AVAILABILITY;

                //check if team name is valid
                if($(".TeamNameCheck").val()==-1)
                   availability = messages.CHECK_TEAM_NAME_AVAILABILITY;

                //check if assistant coachahs selected
                if($(".SelectedCoach").val()==0)
                   availability = messages.CHOOSE_ASSISTANT_COACH;
                  
                return availability;
         },
         
         //send request to server for the partcipant login
         ParticipantRegister : function(teamname,email,firstname,middlename,lastname,day,month,year,gender,address,housenr,
                                            additive,zipcode,city,phone,mobile,partner_subscription,banknumber,club,coach,item, 
                                            clubInfo,eredivisieInfo){
                var retVal=""; 
                $("#toStep3").hide();
                $("#registerPart").show();
                $.ajax({
                   type: "POST",
                   url:  resources.path_prefix + "/DataProvider/Register.ashx",
                   data: "team_name="+teamname+
                   "&email="+email+
                   "&first_name="+firstname+
                   "&middle_name="+middlename+
                   "&last_name="+lastname+
                   "&birth_date="+day+"-"+month+"-"+year+
                   "&gender="+gender+
                   "&address="+address+
                   "&house_nr="+housenr+
                   "&additive="+additive+
                   "&zip_code="+zipcode+
                   "&city="+city+
                   "&phone="+phone+
                   "&mobile_phone="+mobile+
                   "&partner_subscription="+partner_subscription+
                   "&banknumber="+banknumber+
                   "&club_id="+club+
                   "&coach_id="+coach+
                   "&club_info="+clubInfo+
                   "&eredivisie_info="+eredivisieInfo+
                   "&job=3", //job=3 --> Register
                   success: function(data){
                        retVal=data;
                       $("div#Login").hide();
                       $("div#RegisterNewText").hide();
                       $("div#RegisterNewForm1").hide();
                       $("div#RegisterNewForm2").hide();
                       $("span#toStep2").hide();
                       $("div#Step2Content").hide();
                       $(".TeamNameAvailable").hide();
                       $(".TeamNameNotAvailable").hide();
                       
                       $("#registerPart").fadeOut(2500);
                       $("#toStep3").fadeIn(2500);
                       
                       if(data>0){ //register succesfull
                           $("div#Step3content").show();
                           $("div.step2ResRegister").hide();
                           $(".TeamNameNotAvailable").hide();
                           $("div#Step2").attr("class","step"),
                           $("div#Step3").attr("class","step opened");
                           $("input#TB_Mail2").val(email);
                           $("input#TB_Mail2").attr("readonly","readonly");
                           item.focus(); 
                       }
                       else if(data==-1){ //email exists
                           $("div#Step3content").hide();
                           $("div#Step1Content").show();
                           $("div#Step2").attr("class","step");
                           $("div#Step1").attr("class","step opened")
                           $(".TeamNameNotAvailable").hide();
                           $("#mailError").html(messages.E_MAIL_ALREADY_EXISTS);
                       }
                       else if(data==-2){ //teamname exists
                           $("div#Step2Content").show();
                           $(".TeamNameNotAvailable").show();
                           $("div#Step1Content").hide();
                           $("#teamError").html(messages.TEAM_NAME_TAKEN);
                       }
                       else if(data==-3){ // both team name and email exists
                           $("div#Step3content").hide();
                           $(".TeamNameNotAvailable").hide();
                           $("div#Step1Content").show();
                           $("#mailError").html(messages.NOT_UNIQUE);
                       }
                       else if(data==-4){ // a validation error
                           $("div#Step3content").hide();
                           $(".TeamNameNotAvailable").hide();
                           $("div#Step1Content").show();
                           $("#mailError").html(messages.REGISTRATION_ERROR);
                       }
                       else if(data==-5){ //session timeout for partner_league_id /***temp solution***/
                            window.open(location.reload(true));
                       }
                       else if(data==-6){
                            window.location.href=resources.path_prefix + "/Team/TeamSelect.aspx";
                       }
                       else{ // unknown error
                           $("div#Step3content").hide();
                           $(".TeamNameNotAvailable").hide();
                           $("div#Step1Content").show();
                           $("#mailError").html(messages.REGISTRATION_ERROR);
                       }
                   },
                   error: function(data){
                   }
               });  
               return retVal;
         },
         
         CheckPopup : function(email,pwd){
                var popup;
                
                $.ajax({
                    type: "POST",
                    url:  resources.path_prefix + "/DataProvider/Register.ashx",
                   data: "email="+email+"&password="+pwd+"&job=8", //job=8 --> Check popup
                    async: false,
                    success: function(data){
                        popup = data;
                    },
                    error: function(data){
                    }
                });     
                    
                return popup;
         },
         
         ChangeText : function(){
            $(".dBox").html("Dat is jammer, Profcoaches kunnen Eredivisie Live eigenlijk niet missen. Bestel nu, ontvang Eredivisie Live 2 maanden gratis en speel mee in de exclusieve Eredivisie Live subleague met kans op extra prijzen.");
            $(".dBox").css("font-size","9px");
            
            $(".btn").html("<div style='margin:0 0 0 88px'><input type='button' class='middle' onclick='javascript:Register.OpenOrderWindow()' value='Bestel nu' /></div>");
         },
         
         OpenOrderWindow: function(){
                window.open('http://bestellen.eredivisielive.nl/promotion/besteldirect.aspx?Promotion=aanbieding&AffID=1656');
         },
         
         NewSubleagueRequest: function(){
            $(".dBox").html("Top! Nieuw binnen Profcoach is dat je als abonnee mee mag spelen in de exclusieve Eredivisie Live subleague met kans op extra prijzen."+
                            "Wil je hierin meespelen tegen je collega abonnees?");
            $(".dBox").css("font-size","10px");
            $(".dBox").css("font-weight","bold");
            
            $(".left").html("<input class='little' type='button' id='BTNyes' value='"+messages.YES+"' onclick='javascript:Register.JoinEredivisieLeagueStep1(1)'/>");
            $(".right").html("<input class='little' type='button' id='BTNno' value='"+messages.NO+"' onclick='javascript:Register.JoinEredivisieLeagueStep1(2)'/>");
         },
  
        JoinEredivisieLeagueStep1: function(res){
            $(".btn").html("");
            $(".dBox").html("<span class='loaderimg' style='margin:16px 110px; width:16px; height:16px'>&nbsp;&nbsp;<img style='width:16px; height:16px' src='../Images/ajax-loader.gif' alt='' /></span>");
            $(".participantChoice").val("1");
            var ret = Register.JoinEredivisieLeagueStep2(res);
            $(".dBox").css("font-size","13px");
            $(".dBox").css("font-weight","bold");
            if(res == 2){
                $(".dBox").html("Dat is jammer, wil je straks alsnog meespelen dan kan je dat aanpassen binnen je profiel.");
            }
            if(res == 1){
                $(".dBox").html("Je speelt nu mee tegen andere abonnees, heel veel succes!");
            }
        },
  
        JoinEredivisieLeagueStep2: function(res){
            var ret = 0;
            var email = $(".participantMail").val();
            var pwd = $(".participantPwd").val();
            $.ajax({
                type: "POST",
                url:  resources.path_prefix + "/DataProvider/Register.ashx",
               data: "email="+email+"&password="+pwd+"&isJoin="+res+"&job=9", //job=9 --> JoinLeague
                async: false,
                success: function(data){
                    ret = 1;
                },
                error: function(data){
                }
            }); 
            return ret;
        }
        
	}
	
}(jQuery);

