﻿var EventEditor = function(){this.ctor.apply(this, arguments);};Object.SetProperties(EventEditor.prototype, 
{
   ctor:function(eManager){
      this.EManager=eManager;
   },
   EManager:null,
   Render:function(body, eventInfo, params, lightBox)
   {
      var styles = this.EManager.Styles.Editor;
      var labels = this.EManager.Labels.Editor;
      var datePickerStyles = this.EManager.Styles.DatePicker;
      var div, fDiv, lDiv, rDiv;
      
      if(!params.IsNew){
         var divClose = Object.NewTagInto(body, "div", {style:{width:"100%", clear:'both', styleFloat:'left', cssFloat:'left'}});
         Object.NewTagInto(divClose, "a", styles.GetStyle("Link"), {href:"javascript:void(0)", style:{styleFloat:'right', cssFloat:'right'}, innerHTML:labels.Close, onclick:function(lb){lb.Hide();}.Bind({}, lightBox)});
         
         Object.SetProperties(body, styles.GetStyle("Container"), {style:{height:'auto'}});
      }
      
      // Render Main Div
      div  = Object.NewTagInto(body, "div", {style:{width:'100%', height:'100%'}, innerHTML:''});
      
      // Render Header
      fDiv = Object.NewTagInto(div, "div", {style:{width:'100%', styleFloat:'left', cssFloat:'left', marginBottom:'5px'}});
      Object.NewTagInto(fDiv, "span", styles.GetStyle("Header"), {innerHTML:params.HeaderTitle, style:{}});
      
      var comlblStyle = {style:{width:'20%', styleFloat:'left', cssFloat:'left'}};
      var comCtlStyle = {style:{width:'80%', styleFloat:'left', cssFloat:'left'}};
      
      // Render Title
      fDiv = Object.NewTagInto(div, "div", {style:{width:'100%', styleFloat:'left', cssFloat:'left', marginBottom:'5px'}});
      lDiv = Object.NewTagInto(fDiv, "div", comlblStyle);
      var lblTitle = Object.NewTagInto(lDiv, "span", styles.GetStyle("Label"), {innerHTML:labels.Title});
      rDiv = Object.NewTagInto(fDiv, "div", comCtlStyle);
      var txtTitle = Object.NewTagInto(rDiv, "input", styles.GetStyle("Text"), {type:'text', value:eventInfo.Title, style:{width:'100%'}});
      var valTitle = Object.NewTagInto(rDiv, "span", styles.GetStyle("Validate"), {innerHTML:labels.Required, style:{color:'red', display:'none'}});

      // Render URL
      fDiv = Object.NewTagInto(div, "div", {style:{width:'100%', styleFloat:'left', cssFloat:'left', marginBottom:'5px'}});
      lDiv = Object.NewTagInto(fDiv, "div", comlblStyle);
      var lblTitle = Object.NewTagInto(lDiv, "span", styles.GetStyle("Label"), {innerHTML:labels.URL});
      rDiv = Object.NewTagInto(fDiv, "div", comCtlStyle);
      var txtURL = Object.NewTagInto(rDiv, "input", styles.GetStyle("Text"), {type:'text', value:eventInfo.URL, style:{width:'100%'}});
      var valURL = Object.NewTagInto(rDiv, "span", styles.GetStyle("Validate"), {innerHTML:labels.InvalidURL, style:{color:'red', display:'none'}});
      
      // Render Description
      fDiv = Object.NewTagInto(div, "div", {style:{width:'100%', styleFloat:'left', cssFloat:'left', marginBottom:'5px'}});
      lDiv = Object.NewTagInto(fDiv, "div", comlblStyle);
      Object.NewTagInto(lDiv, "span", styles.GetStyle("Label"), {innerHTML:labels.Description});
      rDiv = Object.NewTagInto(fDiv, "div", comCtlStyle);
      var txtDescription = Object.NewTagInto(rDiv, "textarea", styles.GetStyle("TextArea"), {innerHTML:eventInfo.Description, style:{width:'100%'}});
      
      // Render Venue
      fDiv = Object.NewTagInto(div, "div", {style:{width:'100%', styleFloat:'left', cssFloat:'left', marginBottom:'5px'}});
      lDiv = Object.NewTagInto(fDiv, "div", comlblStyle);
      Object.NewTagInto(lDiv, "span", styles.GetStyle("Label"), {innerHTML:labels.Venue});
      rDiv = Object.NewTagInto(fDiv, "div", comCtlStyle);
      var txtVenue = Object.NewTagInto(rDiv, "input", styles.GetStyle("Text"), {type:'text', value:eventInfo.Venue, style:{width:'100%'}});

      // Render Time
      fDiv = Object.NewTagInto(div, "div", {style:{width:'100%', styleFloat:'left', cssFloat:'left', marginBottom:'5px'}});
      lDiv = Object.NewTagInto(fDiv, "div", comlblStyle);
      Object.NewTagInto(lDiv, "span", styles.GetStyle("Label"), {innerHTML:labels.Time});
      rDiv = Object.NewTagInto(fDiv, "div", comCtlStyle);
      var txtTime = Object.NewTagInto(rDiv, "input", styles.GetStyle("Text"), {type:'text', value:eventInfo.EventTime, style:{width:'100%'}});

      // Event Type 
      fDiv = Object.NewTagInto(div, "div", {style:{width:'100%', styleFloat:'left', cssFloat:'left', marginBottom:'5px'}});
      lDiv = Object.NewTagInto(fDiv, "div", comlblStyle);
      Object.NewTagInto(lDiv, "span", styles.GetStyle("Label"), {innerHTML:labels.EventType});
      rDiv = Object.NewTagInto(fDiv, "div", comCtlStyle);
      var selType = Object.NewTagInto(rDiv, "select", styles.GetStyle("Select"), {style:{}});
      for(var i=0; i<EventTypes.length; i++)Object.NewTagInto(selType, "option", {innerHTML:EventTypes[i], value:i+1, selected:eventInfo.EventType==i+1});

      // Render From Date
      fDiv = Object.NewTagInto(div, "div", {style:{width:'100%', styleFloat:'left', cssFloat:'left', marginBottom:'5px'}});
      lDiv = Object.NewTagInto(fDiv, "div", comlblStyle);
      Object.NewTagInto(lDiv, "span", styles.GetStyle("Label"), {innerHTML:labels.From});
      rDiv = Object.NewTagInto(fDiv, "div", comCtlStyle);
      var txtFrom = Object.NewTagInto(rDiv, "input", styles.GetStyle("ShortText"), {type:'text', value:eventInfo.From.toEString(), readOnly:true, style:{width:'170px'}}); 
      var btnFrom = Object.NewTagInto(rDiv, "input", styles.GetStyle("Button"), {type:'button', value:'...', style:{width:'20px'}});
      var valFrom = Object.NewTagInto(rDiv, "span", styles.GetStyle("Validate"), {innerHTML:labels.Required, style:{color:'red', display:'none'}});

      // Render To Date
      fDiv = Object.NewTagInto(div, "div", {style:{width:'100%', styleFloat:'left', cssFloat:'left', marginBottom:'5px'}});
      lDiv = Object.NewTagInto(fDiv, "div", comlblStyle);
      Object.NewTagInto(lDiv, "span", styles.GetStyle("Label"), {innerHTML:labels.To});
      rDiv = Object.NewTagInto(fDiv, "div", comCtlStyle);
      var txtTo = Object.NewTagInto(rDiv, "input", styles.GetStyle("ShortText"), {type:'text', value:eventInfo.To.toEString(), readOnly:true, style:{width:'170px'}}); 
      var btnTo = Object.NewTagInto(rDiv, "input", styles.GetStyle("Button"), {type:'button', value:'...', style:{width:'20px'}}); 
      var valTo = Object.NewTagInto(rDiv, "span", styles.GetStyle("Validate"), {innerHTML:labels.Required, style:{color:'red', display:'none'}}); 
      var valFromTo = Object.NewTagInto(rDiv, "span", styles.GetStyle("Validate"), {innerHTML:labels.DateMismatch, style:{color:'red', display:'none'}}); 


      //Object.SetProperties(btnTo, {onclick:function(to){DatePicker(new Date(to.value), {target:this, months:labels.Months, monthsHeader:labels.Months, days:labels.Days}, function(d){ d.setHours(24,0,0,0);d.setMilliseconds(d.getMilliseconds()-1);this.value = d.toEString();}.Bind(txtTo));}.Bind(btnTo, txtTo)});
      Object.SetProperties(btnTo, {onclick:function(to){DatePicker(new Date(to.value), Object.Union(datePickerStyles,{target:this, months:labels.Months, monthsHeader:labels.Months, days:labels.Days}), function(d){ this.value = d.toEString();}.Bind(txtTo));}.Bind(btnTo, txtTo)});

      //Object.SetProperties(btnFrom, {onclick:function(txt){DatePicker(new Date(txt.value), {target:this, months:labels.Months, monthsHeader:labels.Months, days:labels.Days}, function(d){d.setHours(0,0,0,0); this.value = d.toEString();}.Bind(txtFrom));}.Bind(btnFrom,txtFrom)})
      //Object.SetProperties(btnFrom, {onclick:function(txt){DatePicker(new Date(txt.value), Object.Union(datePickerStyles, {target:this, months:labels.Months, monthsHeader:labels.Months, days:labels.Days}), function(d){this.value = d.toEString();}.Bind(txtFrom));}.Bind(btnFrom,txtFrom)}); 
      Object.SetProperties(btnFrom, {onclick:function(txtFrom, txtTo){DatePicker(new Date(txtFrom.value), Object.Union(datePickerStyles, {target:this, months:labels.Months, monthsHeader:labels.Months, days:labels.Days}), function(txtTo, d){this.value = d.toEString(); var td=new Date(txtTo.value);if(td<d){d.setDate(d.getDate()+1);txtTo.value=d.toEString();}}.Bind(txtFrom, txtTo));}.Bind(btnFrom,txtFrom, txtTo)}); 

      
      // Render Horizontal Line
      fDiv = Object.NewTagInto(div, "div", {style:{width:'100%', styleFloat:'left', cssFloat:'left', marginBottom:'5px'}});
      Object.NewTagInto(fDiv, "hr", styles.GetStyle("Separator"), {style:{width:'100%'}});
      
      
      // Render button
      fDiv = Object.NewTagInto(div, "div", {style:{width:'100%', styleFloat:'left', cssFloat:'left', marginBottom:'5px', textAlign:'right'}});
      var btnSave = Object.NewTagInto(fDiv, "input", styles.GetStyle("Button"), {type:'button', value:labels.Save, style:{}, 
        onclick:function(title, url, desc, venue, time, type, from, to, vTitle, vURL, vFrom, vTo, vFromTo, eventInfo, manager, labels){
          
          Object.SetProperties(vTitle.style, {display:'none'});
          Object.SetProperties(vFrom.style, {display:'none'});
          Object.SetProperties(vTo.style, {display:'none'});
          Object.SetProperties(vFromTo.style, {display:'none'});
          Object.SetProperties(vURL.style, {display:'none'});
          
          
          if(title.value.Trim()==""){Object.SetProperties(vTitle.style,{display:'block'}); return;}
          if(from.value.Trim()==""){Object.SetProperties(vFrom.style,{display:'block'}); return;}
          if(to.value.Trim()==""){Object.SetProperties(vTo.style,{display:'block'}); return;}
          if(new Date(from.value) > new Date(to.value)){Object.SetProperties(vFromTo.style,{display:'block'}); return;}
          if(url.value.Trim()!=""){
            var URL_REGEXPR = /^http(s)?:\/\/\S+$/i; 
            if(!URL_REGEXPR.test(url.value)){Object.SetProperties(vURL.style,{display:'block'}); return;}
          }
          
          with(eventInfo){
            Title = title.value;
            URL = url.value;
            Description = desc.value;
            Venue = venue.value;
            EventType = type.value;
            From = new Date(from.value);
            To = new Date(to.value);
            EventTime = time.value;
          }
          
          manager.SaveEvent(eventInfo, 
            function(manager, labels, success){
              if(success){
                setTimeout(manager.EventCalendar.TabContainer.FocusToTab.Bind(manager.EventCalendar.TabContainer, "Year"), 1000*1);
                //manager.EventCalendar.TabContainer.FocusToTab("Month")
              }
              else{
                alert(labels.SaveFailed);
              }
            }.Bind({}, manager, labels)
          );          
        }.Bind({}, txtTitle, txtURL, txtDescription, txtVenue, txtTime, selType, txtFrom, txtTo, valTitle, valURL, valFrom, valTo, valFromTo, eventInfo, this.EManager, labels)
      });
      
      if(lightBox || lightBox!=null)lightBox.Show();
      //var btnCancel = Object.NewTagInto(fDiv, "input", styles.GetStyle("Button"), {type:'button', value:labels.Cancel, style:{}});
   }
   
});