//Functions for the AlertSubscribeConfirmed UserControl

//Initialize component
$().ready(function(){
    AlertSubscribe.init();
});


AlertSubscribe = {   

    //initialize the component
    init: function() {
        //Hide content job alerts
        $(".SetAlertSubscribe")[0].style["display"] = "none";
        //Override events server-side
        $(".lnkEmailAlerts").unbind().click(AlertSubscribe.onClickToggle);      
        $(".btnSubmit").unbind().click(AlertSubscribe.onClickSubmit);        
        $(".btnYes").unbind().click(AlertSubscribe.onClickYes);
        $(".btnNo").unbind().click(AlertSubscribe.onClickNo); 
        $(".btnSave").unbind().click(AlertSubscribe.onClickRegisteredSubmit);   
    },

    onClickToggle: function(){       
        $(".SetAlertSubscribe").toggle();
        if ($(".SetAlertSubscribe").is(':visible')==false){
            $(".pnlSuccessEmailActivation").hide();
            $(".pnlFailEmailActivation").hide();
            $(".pnlOverrideEmailSubs").hide();
            $(".pnlNewAlert").hide();
        }
    },
    
    //Submit the alert
    onClickSubmit: function() {        
	
        var request =
        {
            email: jQuery.trim($(".AlertSubscribe_txtEmail")[0].value),
            frequency: parseInt($(".AlertSubscribe_ddlFrequency")[0].value, 10),
            filters: AlertSubscribe.getFilters()
        };		
        
             
        if (request.frequency != 0)
        {
            WSK.OffAng.ClientServices.JobAlertServices.CreateJobAlert(request.email,request.frequency,request.filters,offerDestiny,onCreatedJobAlert.bind(this));
            return false;               	
        }   
          else{
            $(".AlertSubscribe_ddlFrequency").focus();
            return true;
          }        
          
    },
    
    //OnClick if the user was registered in email alerts zone.
    onClickRegisteredSubmit: function(){   			
            
        var result = 
        {
			filters: AlertSubscribe.getFilters()
        }      
		WSK.OffAng.ClientServices.JobAlertServices.SaveJobAlertRegistered(result.filters,onJobAlertSavedRegistered.bind(this));
        return false;               	
    },
    
    //Override the existing alert
    onClickYes: function() {
        var request =
        {
            email: jQuery.trim($(".AlertSubscribe_txtEmail")[0].value),
            frequency: parseInt($(".AlertSubscribe_ddlFrequency")[0].value, 10),
            filters: AlertSubscribe.getFilters()
        };	

        var result;        
        WSK.OffAng.ClientServices.JobAlertServices.UpdateJobAlert(request.email,request.frequency,request.filters,offerDestiny,onJobAlertUpdatedRegistered.bind(this));
        return false;
    },

    //Override the existing alert
    onClickNo: function() {
         $(".pnlSuccessEmailActivation").hide();
         $(".pnlFailEmailActivation").hide();
         $(".pnlOverrideEmailSubs").hide();
         $(".pnlNewAlert").hide();
         $(".SetAlertSubscribe").hide();
         $(".AlertSubscribe_txtEmail")[0].value = "";
         $(".AlertSubscribe_ddlFrequency")[0].value = 0;
         return false;         
    },

    //get filters
    getFilters: function() {
        var contractTypes=$(".contractType label input");
        var filtersSearch="";               	                	                
        var j=0;
        
        //Check if exists contract types selected
        for (i=0;i<contractTypes.length;i++){
            if(contractTypes[i].checked){
                if (j==0){
                    filtersSearch="contractTypes=";
                }
                else{
                    filtersSearch+="+";
                }
                filtersSearch+=contractTypes[i].id;
                j++;
            }                   	                   
        }   
        
        //Check if exists any activity domain selected
        if ($(".main")[0].style.length!="0"){
            if (($(".main select").length!=0)&&($(".main select")[0].options[$(".main select")[0].selectedIndex].value!=0)){	                    
                        if(filtersSearch!=""){
                            filtersSearch+="&";
                        }
                        filtersSearch+="activityDomain=" + $(".main select")[0].options[$(".main select")[0].selectedIndex].value;	                    	                                
                if($(".optional select")[0].selectedIndex!=-1){
                    if (($(".optional select").length!=0)&&($(".optional select")[0].options[$(".optional select")[0].selectedIndex].value!=0)){	                    
                        if(filtersSearch!=""){
                            filtersSearch+="&";
                        }
                        filtersSearch+="jobCategory=" + $(".optional select")[0].options[$(".optional select")[0].selectedIndex].value;
                     }
                }	 	                                
            }                   	                                       
        }          	                
        else if (($(".editDomain")[0].innerHTML=="modifier")&&($(".domain label input").length!=0)){	                    
                if($(".domain label input")[0].checked){
                   if(filtersSearch!=""){
                                filtersSearch+="&";
                   }
                   filtersSearch+="activityDomain=" + $(".domain label input")[0].id.replace("AD","");   	                       	                           	                           
                } 
                if($(".category label input").length!=0){
                    if($(".category label input")[0].checked){
                            if(filtersSearch!=""){
                                        filtersSearch+="&";
                            }
                            filtersSearch+="jobCategory=" + $(".category label input")[0].id.replace("JC","");
                    }
                }
       }           	                    	                    	                    	                                  
                   	                	                	                

        //Check if exists any region selected
        if ($(".mainSecond")[0].style.length!="0"){
            if (($(".mainSecond select").length!=0)&&($(".mainSecond select")[0].options[$(".mainSecond select")[0].selectedIndex].value!=0)){	                    
                        if(filtersSearch!=""){
                            filtersSearch+="&";
                        }
                        filtersSearch+="region=" + $(".mainSecond select")[0].options[$(".mainSecond select")[0].selectedIndex].value;	                    	                                
                }
            
            if($(".optionalSecond select")[0].selectedIndex>-1){
                if (($(".optionalSecond select").length!=0)&&($(".optionalSecond select")[0].options[$(".optionalSecond select")[0].selectedIndex].value!=0)){	                    
                    if(filtersSearch!=""){
                        filtersSearch+="&";
                    }
                    filtersSearch+="department=" + $(".optionalSecond select")[0].options[$(".optionalSecond select")[0].selectedIndex].value;;
                }
            }	                    
        }          	                
        else if (($(".editLocation")[0].innerHTML=="modifier")&&($(".region label input").length!=0)){	                    
                if($(".region label input")[0].checked){
                   if(filtersSearch!=""){
                                filtersSearch+="&";
                   }
                   filtersSearch+="region=" + $(".region label input")[0].id.replace("R","");  	                       	                           
                } 	
                if($(".department label input").length!=0){                               	                    	                    
                    if($(".department label input")[0].checked){
                            if(filtersSearch!=""){
                                        filtersSearch+="&";
                            }
                            filtersSearch+="department=" + $(".department label input")[0].id.replace("D","");	                           
                    }	                    	                                  
                }
        }
        
        //Check if exists any publication date selected	
        if ($(".mainThird")[0].style.length!="0"){
            if (($(".mainThird select").length!=0)&&($(".mainThird select")[0].options[$(".mainThird select")[0].selectedIndex].value!=0)){	                    
                        if(filtersSearch!=""){
                            filtersSearch+="&";
                        }
                        if ($(".mainThird select")[0].options[$(".mainThird select")[0].selectedIndex].value == "5"){
                            var aux = $(".datePicker")[0].value.replace("/","-");
                            aux = aux.replace("/","-");
                            filtersSearch+="publicationDate=" + aux;
                        }
                        else{
                            filtersSearch+="publicationDate=" + $(".mainThird select")[0].options[$(".mainThird select")[0].selectedIndex].value; 
                        }        
                }                 
        }          	                
        else if (($(".editPublicationDate")[0].innerHTML=="modifier")&&($(".publicationDate label input").length!=0)){	                    
                if($(".publicationDate label input")[0].checked){
                   if(filtersSearch!=""){
                                filtersSearch+="&";
                   }
                   filtersSearch+="publicationDate=" + $(".publicationDate label input")[0].id.replace("PD","");  	                       	                           
                } 	                               	                    	                    	                        	                    	                                  
        }               	                
        
        
        //Check if exists any keyword selected
        j=0;               	                	                
        var keywords=$(".keyword label input");
        for (i=0;i<keywords.length;i++){	                    
            if(keywords[i].checked){
                if (j==0){
                    if(filtersSearch!=""){
                        filtersSearch+="&";
                    }
                    filtersSearch+="keywords=";
                }
                else{
                    filtersSearch+="+";
                }
                filtersSearch+=keywords[i].value;
                j++;
            }                    	                   
        }
     
        
        return filtersSearch;                                              
    }    
};


function onCreatedJobAlert(res) 
  {
        var result=res.value;   
        switch (result)
         {
		    case "OK":							 
			    $(".pnlSuccessEmailActivation")[0].style["display"] = "block";
			    break;
		    case "ERROR_EMAIL_EXISTS":
		        $(".pnlSuccessEmailActivation")[0].style["display"] = "none";
		        $(".pnlFailEmailActivation")[0].style["display"] = "none";
			    $(".pnlOverrideEmailSubs")[0].style["display"] = "block";
			    break;
		    case "ERROR_SENDING_EMAIL":
		    case "ERROR_SAVING_ALERT":
		    case "ERROR_INVALID_EMAIL":
		    case "ERROR_UNKNOWN":
			    $(".pnlFailEmailActivation")[0].style["display"] = "block";
			    break;
        }
  }
 function onJobAlertSavedRegistered(res)
 {
    var result=res.value;
    $(".message").empty();
    $(".message").append("<br/><br/>" + result)
 }    
 
 
 function onJobAlertUpdatedRegistered(res){
        var result=res.value;
        
        if (result == "True")
	    {
	        $(".pnlFailEmailActivation")[0].style["display"] = "none";
		    $(".pnlSuccessEmailActivation")[0].style["display"] = "block";
		    $(".pnlOverrideEmailSubs")[0].style["display"] = "none";						
	    }
	    else
	    {
	        $(".pnlSuccessEmailActivation")[0].style["display"] = "none";
		    $(".pnlFailEmailActivation")[0].style["display"] = "block";
		    $(".pnlOverrideEmailSubs")[0].style["display"] = "none";						
	    }													
    }                
 
