
// Declare vars to record the short and expanded version of position description.
var previewShortDescription = '';
var previewLongDescription = '';

function updatePositionPreview()
{
   // Get the form data to display in the preview.
   var compensation = document.getElementById('CompensationTxt').value;
   var description = document.getElementById('DescriptionTxt').value.replace(/\n/g, '::linebreak::');
   var fullPostingURL = document.getElementById('FullPostingURLTxt').value;
   var positionTitle = document.getElementById('PositionTitleTxt').value;
   var practiceArea = document.getElementById('PracticeAreaTxt').value;
   var schedule = document.getElementById('ScheduleTxt').value;
   var shift = document.getElementById('ShiftTxt').value;
   
   var city = '';
   var facility = '';
   var state = '';
   if (document.getElementById('LocationDdl').selectedIndex > 0)
   {
      var location = document.getElementById('LocationDdl').options[document.getElementById('LocationDdl').selectedIndex].text;
      var cityStateComma = location.lastIndexOf(',', location.length - 5);
      var cityState = location.substr(cityStateComma + 2);
      facility = location.substr(0, cityStateComma);
      city = cityState.substr(0, cityState.lastIndexOf(','));
      state = cityState.substr(cityState.lastIndexOf(',' + 2));
   }
   
   var applyLink;
   var contactInfoLink;
   var employerLink;
   var priorityPosition;
   var priorityResponse;
   if (getCheckBoxSelectedValue(applyLinkCbxId))
   {
      applyLink = '1';
   }
   else
   {
      applyLink = '0';
   }
   if (getCheckBoxSelectedValue(contactInfoLinkCbxId))
   {
      contactInfoLink = '1';
   }
   else
   {
      contactInfoLink = '0';
   }
   if (getCheckBoxSelectedValue(employerLinkCbxId))
   {
      employerLink = '1';
   }
   else
   {
      employerLink = '0';
   }
   if (getCheckBoxSelectedValue(priorityPositionCbxId))
   {
      priorityPosition = '1';
   }
   else
   {
      priorityPosition = '0';
   }
   if (getCheckBoxSelectedValue(priorityResponseCbxId))
   {
      priorityResponse = '1';
   }
   else
   {
      priorityResponse = '0';
   }
   
   var positionXML = "<positions>" +
                        "<preview>1</preview>" +
                        "<orderTypeCTRFacility></orderTypeCTRFacility>" +
                        "<orderTypeCTRPosting></orderTypeCTRPosting>" +
                        "<position>" +
                           "<applyLink>" + applyLink + "</applyLink>" +
                           "<city>" + city.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') + "</city>" +
                           "<compensation>" + compensation.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') + "</compensation>" +
                           "<contactInfoLink>" + contactInfoLink + "</contactInfoLink>" +
                           "<description>" + description.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') + "</description>" +
                           "<employerLink>" + employerLink + "</employerLink>" +
                           "<facility>" + facility.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') + "</facility>" +
                           "<fullPostingURL>" + fullPostingURL.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') + "</fullPostingURL>" +
                           "<itemId></itemId>" +
                           "<positionId>-1</positionId>" +
                           "<positionTitle>" + positionTitle.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') + "</positionTitle>" +
                           "<practiceArea>" + practiceArea.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') + "</practiceArea>" +
                           "<priorityPosition>" + priorityPosition + "</priorityPosition>" +
                           "<priorityResponse>" + priorityResponse + "</priorityResponse>" +
                           "<schedule>" + schedule.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') + "</schedule>" +
                           "<shift>" + shift.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;') + "</shift>" +
                           "<stateAbbr>" + state + "</stateAbbr>" +
                           "<websiteURL>http://www.toobusyworking.com</websiteURL>" +
                        "</position>" +
                     "</positions>";
                     
   var previewHTML = generatePreviewHTML(positionXML);
   
   // Show the preview pane.
   document.getElementById('previewPosition').innerHTML = previewHTML.replace(/::linebreak::/g, '<br />');
   document.getElementById('previewPositionDiv').style.display = 'block';
}

function generatePreviewHTML(positionXML)
{
   if (window.XSLTProcessor)
   {
      // support Mozilla/Gecko based browsers
      var xsltProcessor = new XSLTProcessor();
      xsltProcessor.importStylesheet(xsldocPositionPreview);      
      var parser = new DOMParser();
      var xmlPositionPreview = parser.parseFromString(positionXML, "text/xml");
      var outputXHTML = xsltProcessor.transformToFragment(xmlPositionPreview, document);
      var serializer = new XMLSerializer();
   	return serializer.serializeToString(outputXHTML);
   }
   else if(window.ActiveXObject)
   {
      // support Windows/ActiveX enabled browsers// Load XML 
      var xmlPositionPreview = new ActiveXObject("Microsoft.XMLDOM");
      xmlPositionPreview.async = "false";
      xmlPositionPreview.loadXML(positionXML);
      var outputXHTML = xmlPositionPreview.transformNode(xsldocPositionPreview);
      return outputXHTML;
   }
   else
   {
      return "Sorry, the preview could not be generated.";
   }
}

// On page load, initialise the xsl for the position preview
var xsldocPositionPreviewLoaded = false;
var xsldocPositionPreview;

if (window.XSLTProcessor)
{
   // support Mozilla/Gecko based browsers
   xsldocPositionPreview = document.implementation.createDocument("", "", null);
   xsldocPositionPreview.load('/Lib/PositionsFormat.xsl');
}
else if(window.ActiveXObject)
{
   // support Windows / ActiveX
   xsldocPositionPreview = new ActiveXObject("Microsoft.XMLDOM");
   xsldocPositionPreview.load('/Lib/PositionsFormat.xsl');
}

function showHideDescription(showDescriptionLbl, hideDescriptionLbl)
{
   document.getElementById(hideDescriptionLbl).style.display = 'none';
   document.getElementById(showDescriptionLbl).style.display = 'block';
}
