/*********************************************************************************************\
*       COPYRIGHT © 2010 ENVISION INFORMATION TECHNOLOGIES, LLC.    ALL RIGHTS RESERVED       *
*       DISTRIBUTION, UNAUTHORIZED USE AND MODIFICATION IS STRICTLY PROHIBITED                *
*       ENVISION IT, MADISON, WI    http://www.envisionitllc.com   info@envisionitllc.com     *
\*********************************************************************************************/

function CalendarJumpTo() {
  var goto_date = document.getElementById('calendar_goto').value.split('/');

  var filter_url = '';
  if (!document.getElementById('calendar_show_event_type_1').checked) {
    filter_url += '&filter1=1';
  }
  if (!document.getElementById('calendar_show_event_type_2').checked) {
    filter_url += '&filter2=1';
  }
  
  window.location = '?month=' + goto_date[0] + '&year=' + goto_date[1] + filter_url;
}

function Calendar_FilterEvents(type, previous_url, next_url) {
  var display = 'none';
  if (document.getElementById('calendar_show_event_type_' + type).checked)
    display = 'block';
  
  var table_rows = document.getElementById('calendar_table').rows;
  for (row_index = 0; row_index < table_rows.length; row_index++) {
    var row_cells = table_rows[row_index].cells;
    for (cell_index = 0; cell_index < row_cells.length; cell_index++) {
      var cell_children = row_cells[cell_index].childNodes;
      for (div_index = 0; div_index < cell_children.length; div_index++) {
        if (cell_children[div_index].className == ('calendar_event_type_' + type)) {
          cell_children[div_index].style.display = display;
        }
      }
    }
  }
}

function showRegistration(eventDateId, eventId, eventName, description, charge, eventDate, startTime, endTime, showImage, showPDF, canRegister)
{
  var ie6 = navigator.userAgent.toLowerCase().indexOf('msie 6') != -1;
  
  var waitBG = document.createElement("div");
  if(ie6)
  {
    waitBG.style.position="absolute";
    waitBG.style.width="100%";
    waitBG.style.height = document.getElementById("centerDiv").clientHeight + "px";
  }
  else
  {
    waitBG.style.width="100%";
    waitBG.style.height="100%";
    waitBG.style.position="fixed";
  }
  waitBG.style.left="0px";
  waitBG.style.top="0px";
  waitBG.id="waitBG";
  waitBG.style.backgroundColor="#000000";
  if (((navigator.userAgent.toLowerCase().indexOf("msie") != -1) && (navigator.userAgent.toLowerCase().indexOf("opera") == -1)))
    waitBG.style.filter = 'alpha(opacity=30)';
  else // for mozilla
    waitBG.style.opacity = 0.3;

  document.body.appendChild(waitBG);

  var container = document.createElement("div");
  container.id="regContainer";
  container.style.backgroundColor="#ffffff";
  if(ie6)
  {
    container.style.position="absolute";
    container.style.left="50%";
    container.style.top="240px";
  }
  else
  {
    container.style.position="absolute";
    container.style.left="50%";
    container.style.top="240px";
  }
  container.style.width="700px";
  //container.style.height="650px";
  container.style.marginLeft="-350px";
  //container.style.marginTop="-325px";
  container.style.paddingTop="0px";
  container.style.paddingLeft="15px";
  container.style.paddingRight="5px";
  //container.style.overflowY="scroll";

  document.body.appendChild(container);
  
  //styling - display information about the event
  var info = document.createElement("div");
  info.style.fontSize="14px";
  info.style.color="#000000";
  var html = '';
  if (showImage == '1')
    html += '<div style="float:right;overflow:hidden;padding:5px;width:285px;"><img src="/events/images/' + eventId + '.jpg" alt="Event Image" /></div>';
  html += '<div style="padding:5px;"><img src="/images/templates/tbc/logo.gif" alt="The Bruce Company" /></div>';
  html += '<h1 class="registrationHeading" style="padding-bottom:0px;">' + eventName + '</h1><div style="clear:left;"></div>';
  html += '<div style="float:left;padding-right:5px;text-align:right;width:80px;" class="bold">Description:</div><div style="float:left;width:300px;">' + description + '</div><div style="clear:left;"></div>';
  html += '<div style="float:left;padding-right:5px;text-align:right;width:80px;" class="bold">Event Date: </div><div style="float:left;width:300px;">' + eventDate + '</div><div style="clear:left;"></div>';
  if (endTime != '')
    endTime = ' - ' + endTime;
  if (startTime != '')
    html += '<div style="float:left;padding-right:5px;text-align:right;width:80px;" class="bold">Time: </div><div style="float:left;width:300px;">' + startTime + endTime + '</div><div style="clear:left;"></div>';
  html += '<div style="float:left;padding-right:5px;text-align:right;width:80px;" class="bold">Charge: </div><div style="float:left;width:300px;">' + charge + '</div><div style="clear:left;"></div>';
  if (showPDF == '1')
    html += '<div style="float:left;padding-right:5px;text-align:right;width:80px;" class="bold">&nbsp; </div><div style="float:left;width:300px;"><a href="/events/pdfs/' + eventId + '.pdf" target="_blank" style="color: #000;font-size:14px;" />Click Here for More Info</a></div><div style="clear:left;"></div>';
  if (canRegister == '1')
    html += '<div style="clear:both;width:90%;height:1px;background-color:#000000;margin:10px 0px 10px 0px;"></div>';
  else
    html += '<div style="clear:both></div>';
  
  info.innerHTML = html;
  
  container.appendChild(info);
  
  if (canRegister == '1') {
    var frm = document.createElement("form");
    frm.method="POST";
    frm.name="frmRegistration";
    frm.onsubmit = function() { if(verifyRegistration()) saveRegistration(); return false; };
  
    frm.appendChild(createInputField("txtFirstName", "First Name:"));
    frm.appendChild(createInputField("txtLastName", "Last Name:"));
    frm.appendChild(createInputField("txtAddress1", "Address Line 1:"));
    frm.appendChild(createInputField("txtAddress2", "Address Line 2:"));
    frm.appendChild(createInputField("txtCity", "City:"));
    frm.appendChild(createInputField("txtState", "State:"));
    frm.appendChild(createInputField("txtZip", "Zip Code:"));
    frm.appendChild(createInputField("txtPhone", "Phone Number:"));
    frm.appendChild(createInputField("txtEmail", "Email Address:"));
    frm.appendChild(createInputField("txtRegistrationCount", "Number Attending:"));
    
    var dateId = document.createElement("input");
    dateId.type="hidden";
    dateId.name="eventdateid";
    dateId.id="eventdateid";
    dateId.value=eventDateId;
    frm.appendChild(dateId);
  
    var sbmtContainer = document.createElement("div");
    sbmtContainer.style.paddingTop="10px";
    sbmtContainer.style.paddingLeft="200px";
    
    var sbmt = document.createElement("input");
    sbmt.type="image";
    sbmt.src="/events/images/register_white.gif";
    sbmtContainer.appendChild(sbmt);
    
    var btnCancel = document.createElement("a");
    //btnCancel.href="#";
    btnCancel.style.paddingLeft="10px";
    btnCancel.onclick = function() { closeRegistration(); return false; }
    
    var btnCancelImage = document.createElement("img");
    btnCancelImage.src="/events/images/register_cancel.gif";
    btnCancelImage.alt="Cancel Registaration";
    btnCancel.appendChild(btnCancelImage);
    sbmtContainer.appendChild(btnCancel);
    
    frm.appendChild(sbmtContainer);    
    
    container.appendChild(frm);
  }
  else {
    var sbmtContainer = document.createElement("div");
    sbmtContainer.style.paddingTop="10px";
    sbmtContainer.style.paddingLeft="80px";
    
    var btnCancel = document.createElement("a");
    //btnCancel.href="#";
    btnCancel.style.paddingLeft="10px";
    btnCancel.onclick = function() { closeRegistration(); return false(); }
    
    var btnCancelImage = document.createElement("img");
    btnCancelImage.src="/events/images/register_close.gif";
    btnCancelImage.alt="Close Info";
    btnCancel.appendChild(btnCancelImage);
    sbmtContainer.appendChild(btnCancel);
    
    container.appendChild(sbmtContainer);
  }
}

function createInputField(inputName, inputLabel)
{
  var container = document.createElement("div");
  container.style.clear = "left";
  container.style.paddingTop="5px";
  
  var lbl = document.createElement("div");
  if(lbl.style.styleFloat != undefined)
    lbl.style.styleFloat = "left";
  else
    lbl.style.cssFloat = "left";
  lbl.style.width="125px";
  lbl.style.textAlign="right";
  lbl.style.paddingRight = "5px";
  lbl.style.color="#000000";
  lbl.innerHTML=inputLabel;
  
  container.appendChild(lbl);
  
  var inpt = document.createElement("input");
  inpt.type="text";
  inpt.name=inputName;
  inpt.id=inputName;
  inpt.style.width="450px";
  
  container.appendChild(inpt);
  
  return container;
}

function verifyRegistration()
{
  if(document.getElementById('txtFirstName').value == "")
    return fail("Please enter your first name");

  if(document.getElementById('txtLastName').value == "")
    return fail("Please enter your last name");

  if(document.getElementById('txtAddress1').value == "")
    return fail("Please enter your address");
    
  if(document.getElementById('txtCity').value == "")
    return fail("Please enter your city");
  
  if(document.getElementById('txtState').value.length != 2)
    return fail("Please enter your two-letter state");
  
  if(document.getElementById('txtPhone').value == "")
    return fail("Please enter your phone number");
  
  if(document.getElementById('txtEmail').value.length < 5)
    return fail("Please enter a valid email address");

  var emailRegEx = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i;
  if(!emailRegEx.test(document.getElementById('txtEmail').value))
    return fail("Please enter a valid email address");
  
  if(document.getElementById('txtRegistrationCount').value == "")
    return fail("Please enter the number attending.");
    
  return true;
}

function saveRegistration()
{
  var params = "firstname=" + escape(document.getElementById('txtFirstName').value) + "&lastname=" + escape(document.getElementById('txtLastName').value) + "&address1=" + escape(document.getElementById('txtAddress1').value) + "&address2=" + escape(document.getElementById('txtAddress2').value) + "&city=" + escape(document.getElementById('txtCity').value) + "&state=" + escape(document.getElementById('txtState').value) + "&zip=" + escape(document.getElementById('txtZip').value) + "&phone=" + escape(document.getElementById('txtPhone').value) + "&email=" + escape(document.getElementById('txtEmail').value) + "&count=" + escape(document.getElementById('txtRegistrationCount').value) + "&eventdateid=" + escape(document.getElementById('eventdateid').value);
  
  var http = createHTTPRequest();

  http.open("POST","/events/registration_save.php", true);  
  http.onreadystatechange=function()
  {
    if(http.readyState == 4)
    {
      var response = http.responseText;
      if(response == "OK")
      {
        alert('Thank you for registering for our event!  We look forward to seeing you at our Middleton Garden Center location.\n\n- The Bruce Company Team');
        closeRegistration();
      }
      else
      {
        alert('There was a problem registering you for this event.  Please ensure that all of the fields have been filled out properly and resubmit your registration.');
      }
    }
  }

  http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  http.setRequestHeader("Content-length", params.length);
  http.setRequestHeader("Connection", "close"); 
  
  http.send(params);  
}

function fail(err)
{
  alert(err);
  return false;
}

function closeRegistration()
{
  document.body.removeChild(document.getElementById('waitBG'));
  document.body.removeChild(document.getElementById('regContainer'));
}
