﻿$().ready(function(){
	PublishControl.init();
});

PublishControl = {

	init: function(){
		PublishControl.setBlockUIDefaults();
		PublishControl.onChangeList();
		PublishControl.onChangeElement();
		PublishControl.onClickPublish();		
		PublishControl.onClickOKPublish();		
		PublishControl.onClickInscription();
		PublishControl.onClickCheckBox();
		PublishControl.onClickSend();
		PublishControl.onClickOpenDocument();
		PublishControl.onClickOpenIncident();
		PublishControl.onClickOpenInvoice();
		PublishControl.onClickOpenAnnotation();			
	},
	
	onChangeList: function(){
		$(".publishList select").change(function(){
			if (this.selectedIndex != 0)
			{
				var list = this.options[this.selectedIndex].text;								
				var res = AxpertiaBT.WCorp.Web.ClientServices.PublishControl.GetListItems(list, location.href);
				
				$(".publishElement select").empty();
				$(".publishElement select").append(res.value);
				$(".publishElement select")[0].disabled = false;
			}
			else
			{
				$(".publishElement select")[0].disabled = true;
			}
		});
	},
	
	onChangeElement: function(){
		$(".publishElement select").change(function(){
			if (this.selectedIndex != 0)
			{								
				var res = AxpertiaBT.WCorp.Web.ClientServices.PublishControl.GetListItems("Views",location.href);
				
				$(".publishView select").empty();
				$(".publishView select").append(res.value);
				$(".publishView select")[0].disabled = false;
			}
			else
			{
				$(".publishView select")[0].disabled = true;
			}
		});
	},
	
	setBlockUIDefaults: function() {
        $.blockUI.defaults.css = {
            position:       'absolute',
            width:			'980px',
            top:            '250px', 
            left:           '11%', 
            color:          '#000', 
            border:         '3px solid #aaa',
            backgroundColor:'#fff'
        }
        $.blockUI.defaults.message = "";
        $.blockUI.defaults.centerY = false;
    },
    
    onClickPublish: function() {		
		$(".btnPublish").click(function(){
			if (ValidatePublish()){
				$(".OKProcess").hide();
				PublishControl.show();
				var list = $(".publishList select")[0].options[$(".publishList select")[0].selectedIndex].text;
				var element = $(".publishElement select")[0].options[$(".publishElement select")[0].selectedIndex].value;
				var view = $(".publishView select")[0].options[$(".publishView select")[0].selectedIndex].text;
				
				var res = AxpertiaBT.WCorp.Web.ClientServices.PublishControl.Publish(location.href, list, element, view);			
				$(".process").hide();
				$(".OKProcess").show();				
			}
			else{
				alert(AxpertiaBT.WCorp.Web.ClientServices.PublishControl.GetLocalResource("cvPublish.ErrorMessage", location.href).value);
			}				
			
			return false;		
		});    
    },
    
    onClickOKPublish: function(){
		$(".btnOKProcess").click(function(){
			window.location = location.href.split('?')[0];			
		});
    },
    
    show: function() {				
		$.blockUI({ message: $("#divPanel") });	
	},
	
	hide: function() {		
		$.unblockUI({ message: $("#divPanel") });
	},
	
	onClickInscription: function() {
		if($(".inscription").size()!=0) {	
			 $("#lnkInscription_list").hide();
			 $(".inscriptionothers_list").click(function(){
				 var id = ($(this).attr('id'));
				 var ulID = id +"_list";
				 $("#"+ ulID +":visible").slideUp("normal");
				 $("#"+ ulID +":hidden").slideDown("normal");
				 $(this).toggleClass('selected');
				 
				 if (this.attributes["src"].value == "/images/InscriptionSelected.png")
				 	 $(this).attr("src","/images/InscriptionNotSelected.png");
				 else if (this.attributes["src"].value == "/images/InscriptionNotSelected.png")
				 	 $(this).attr("src","/images/InscriptionSelected.png");
				 	 
				 return false;
			 });
		}	
	},
	
	onClickCheckBox: function() {
		$(".checkbox1 input").click(function(){
			if ($(".checkbox1 input")[0].checked == true)
				$(".checkbox2 input")[0].checked = false;
			else
				$(".checkbox2 input")[0].checked = true;
		});
		
		$(".checkbox2 input").click(function(){
			if ($(".checkbox2 input")[0].checked == true)
				$(".checkbox1 input")[0].checked = false;
			else
				$(".checkbox1 input")[0].checked = true;
		});		
	},
	
	onClickSend: function(){			
		$(".btnSend").click(function(){						
			if (ValidateInscriptionEventForm()){
				$(".fieldButton input").hide();
				$(".fieldButton img").show();
				var email = $(".txtEmail").val();
				var name = $(".txtName").val();
				var lastName = $(".txtLastName").val();
				var info = false;
				
				if ($(".checkbox1 input")[0].checked)
					info = false;
				else
					info = true;					
						
				var res = AxpertiaBT.WCorp.Web.ClientServices.PublishControl.SendEmail(email, name, lastName, info, location.href, "event");
				
				if (res.value)
					alert(AxpertiaBT.WCorp.Web.ClientServices.PublishControl.GetLocalResource("EmailSentSuccessfully", location.href).value);												
				else 
					alert(AxpertiaBT.WCorp.Web.ClientServices.PublishControl.GetLocalResource("EmailSentKO", location.href).value);												
			}
			else{
				alert(AxpertiaBT.WCorp.Web.ClientServices.PublishControl.GetLocalResource("cvEvents.ErrorMessage", location.href).value);
			}	
			$(".fieldButton img").hide();
			$(".fieldButton input").show();
			
			return false;			
		});					
	},
	
	onClickOpenInvoice: function(){			
		$(".ibtnFieldInvoice").click(function(){																			
			var res = AxpertiaBT.WCorp.Web.ClientServices.PublishControl.OpenInvoice(this.title, location.href);
			
			if (res.value)
				window.open("/es-ES/ZonaPrivada/Paginas/Descargas.aspx", null, "width=800,height=600,top=100,left=10,fullscreen=yes", "true");
			else
				alert('El documento adjunto no se encuentra disponible en este momento.');
			
			return false;			
		});	
		
		$(".ibtnFieldInvoicePending").click(function(){																			
			var res = AxpertiaBT.WCorp.Web.ClientServices.PublishControl.OpenInvoice(this.title, location.href);
			
			if (res.value)
				window.open("/es-ES/ZonaPrivada/Paginas/Descargas.aspx", null, "width=800,height=600,top=100,left=10,fullscreen=yes", "true");
			else
				alert('El documento adjunto no se encuentra disponible en este momento.');
				
			return false;			
		});		
	},	
	
	onClickOpenDocument: function(){			
		$(".ibtnFieldDocument").click(function(){																			
			var res = AxpertiaBT.WCorp.Web.ClientServices.PublishControl.OpenDocument(this.title, location.href);
			if (res.value)
				window.open("/es-ES/ZonaPrivada/Paginas/Descargas.aspx", null, "width=800,height=600,top=100,left=10,fullscreen=yes", "true");
			else
				alert('El documento adjunto no se encuentra disponible en este momento.');
			
			return false;			
		});		
	},
	
	onClickOpenIncident: function(){			
		$(".ibtnFieldIncident").click(function(){																			
			var res = AxpertiaBT.WCorp.Web.ClientServices.PublishControl.OpenIncident(this.title.replace("{", "").replace("}", ""), location.href);			
			
			window.open("/es-ES/ZonaPrivada/Paginas/Adjuntos.aspx");						
		
			return false;			
		});		
	},
	
	onClickOpenAnnotation: function(){			
		$(".ibtnFieldAnnotation").click(function(){																			
			var res = AxpertiaBT.WCorp.Web.ClientServices.PublishControl.OpenAnnotation(this.title.replace("{", "").replace("}", ""), location.href);			
			
			window.open("/es-ES/ZonaPrivada/Paginas/Descargas.aspx", null, "fullscreen=yes", "true");			
		
			return false;			
		});		
	}	
}


function ValidatePublish(){	
	if (($(".publishList select")[0].selectedIndex != 0) && ($(".publishElement select")[0].selectedIndex != 0)
		&& ($(".publishView select")[0].selectedIndex != 0)){			
			return true;			
		}
	else{
		return false;
	}
}


function ValidateInscriptionEventForm()
{
	if ($(".txtName").val() != "" && $(".txtLastName").val() != "" && $(".txtPhone").val() != "" && $(".txtEnterprise").val() != "" && $(".txtPosition").val() != "" && $(".txtEmail").val() != "")
	{
		if ($(".field span")[0].style["visibility"] != "visible" && $(".field span")[1].style["visibility"] != "visible" && $(".field span")[2].style["visibility"] != "visible" && $(".field span")[3].style["visibility"] != "visible" && $(".lastField span")[0].style["visibility"] != "visible" && $(".lastField span")[1].style["visibility"] != "visible")
		{
			arguments.IsValid=true;
			return arguments.IsValid;
        }
        else
        {
			arguments.IsValid=false;
			return arguments.IsValid;
        }
	}
	else
	{
        arguments.IsValid=false;
        return arguments.IsValid;
	}
}