﻿// this is martin's code copy and pasted over here 
// with the add load listener copy and pasted from another of martins apps to make it work at his direciton.

addLoadListener(setDisplay);
addLoadListener(assignJSRoutines);
addLoadListener(setLoc);

var xmlHttp; 
var selLocKey;
var selCatKey; 
var strServerName;
var autoLoadDescription;

function addLoadListener(fn)
{
  if (typeof window.addEventListener != 'undefined')
  {
    window.addEventListener('load', fn, false);
  }
  else if (typeof document.addEventListener != 'undefined')
  {
    document.addEventListener('load', fn, false);
  }
  else if (typeof window.attachEvent != 'undefined')
  {
    window.attachEvent('onload', fn);
  }
  else
  {
    var oldfn = window.onload;
    if (typeof window.onload != 'function')
    {
     window.onload = fn;
    }
    else
    {
     window.onload = function()
      {
        oldfn();
        fn();
      };
    }
  }
}


function setDisplay () {
 var JSMessage = document.getElementById("JavascriptMessage");
     JSMessage.className = "hide";
 var pageContent = document.getElementById("Joblistings");
     pageContent.className = "show";
}
 
function assignJSRoutines() {
   var jDisplay = document.getElementById("jlist");
   jDisplay.onchange = getSelectedJob;
   var jCat = document.getElementById("category");
   jCat.onchange = getCatJobList;
   var jBranch = document.getElementById("Locations");
   jBranch.onchange = getLocationJobList;
} 
                                                                            
function createXMLHttpRequest() {                                             
    if (window.ActiveXObject) {                                               
        xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");                     
    }                                                                         
    else if (window.XMLHttpRequest) {                                         
        xmlHttp = new XMLHttpRequest();                                       
    }                                                                         
} 
                                                                            
function setLoc() { 
    selectedLocCode = "XX";
    selectedCatCode = "XX";
	var timeStamp = new Date();
    createXMLHttpRequest(); 
    var params = "Category=HWeb&Service=HRSJob-Listing&Ajax=yes&type=init&time=" + timeStamp;                                                  
    xmlHttp.onreadystatechange = initialPageLoad;                         
    xmlHttp.open("GET", "http://" + strDomain + "/guest/default.asp?" + params);                 
    xmlHttp.send(null);                                                       
} 

function initialPageLoad(){
 selLocKey = 'XX';
 selCatKey = 'XX';                                                
 if(xmlHttp.readyState == 4) {                                                
   if(xmlHttp.status == 200) {
     var returnMessage = xmlHttp.responseText;
     if (returnMessage.indexOf("<ServerUnavailable>Yes</ServerUnavailable>") >= 0) {
        serverUnavailable();
     }
     else if (!(returnMessage.indexOf('<?xml version="1.0" encoding="UTF-8" ?>') ==  0)) {
        serverUnavailable();
     }
     else {
     var xmlDoc = xmlHttp.responseXML;
     var locations = xmlDoc.getElementsByTagName("LocationList")[0];
     setLocations(locations); 
     var categories = xmlDoc.getElementsByTagName("CategoryList")[0];
     setCategories(categories); 
     var locations = xmlDoc.getElementsByTagName("JobList")[0];
     setJobList(locations); 
     var pageDate = document.getElementById("Udate");
     var lastupdate = xmlDoc.getElementsByTagName("UpdateDate");
     var txt = document.createTextNode(lastupdate[0].firstChild.nodeValue);
     pageDate.appendChild(txt);
     if (autoLoadDescription == 'yes') {
        document.getElementById("jlist").selectedIndex = 0;
        getSelectedJob();
     }
     }
  }                                                                         
 }                                                                            
}

function getCatJobList() {
   var SelectedCategory = document.getElementById("category");
   selCatKey = SelectedCategory.value;
   var SelectedLocation = document.getElementById("Locations");
   selLocKey = SelectedLocation.value;
   var timeStamp = new Date();
   createXMLHttpRequest(); 
   var params = "Category=HWeb&Service=HRSJobList&Ajax=yes&func=chgcat&selCat= " + selCatKey + "&selLoc= " + selLocKey + "&time=" + timeStamp;                                                  
   xmlHttp.onreadystatechange = displayNewCatDetail;                           
   xmlHttp.open("GET", "http://" + strDomain + "/guest/default.asp?" + params);                 
   xmlHttp.send(null);
}

function displayNewCatDetail() {
 if(xmlHttp.readyState == 4) {                                                
   if(xmlHttp.status == 200) {
     var returnMessage = xmlHttp.responseText;
     checkForTimeOut(returnMessage) ;
     var xmlDoc = xmlHttp.responseXML;
     deleteElements("jlist");
     var locations = xmlDoc.getElementsByTagName("JobList")[0];
     setJobList(locations); 
     deleteElements("Locations");
     var locations = xmlDoc.getElementsByTagName("LocationList")[0];
     setLocations(locations);
     clearHTMLPage();
     if (autoLoadDescription == 'yes') {
        document.getElementById("jlist").selectedIndex = 0;
        getSelectedJob();
     }
   }                                                                         
 }                                                                                 
}
function getLocationJobList() {
   var SelectedLocation = document.getElementById("Locations");
   selLocKey = SelectedLocation.value;
   var SelectedCategory = document.getElementById("category");
   selCatKey = SelectedCategory.value;
   var timeStamp = new Date();
   createXMLHttpRequest(); 
   var params = "Category=HWeb&Service=HRSJobList&Ajax=yes&func=chgbr&selLoc= " + selLocKey + "&selCat= " + selCatKey + "&time=" + timeStamp;                                                  
   xmlHttp.onreadystatechange = displayNewLocDetail;                           
   xmlHttp.open("GET", "http://" + strDomain + "/guest/default.asp?" + params);                 
   xmlHttp.send(null);
}
function displayNewLocDetail() {
 if(xmlHttp.readyState == 4) {                                                
   if(xmlHttp.status == 200) {
     var returnMessage = xmlHttp.responseText;
     checkForTimeOut(returnMessage);
     var xmlDoc = xmlHttp.responseXML;
     deleteElements("jlist");
     var locations = xmlDoc.getElementsByTagName("JobList")[0];
     setJobList(locations); 
     deleteElements("category");
     var catList = xmlDoc.getElementsByTagName("CategoryList")[0];
     setCategories(catList); 
     clearHTMLPage();
     if (autoLoadDescription == 'yes') {
        document.getElementById("jlist").selectedIndex = 0;
        getSelectedJob();
     }
   }                                                                         
 }                                                                                 
}

function getSelectedJob() {
  var SelectedJob = document.getElementById("jlist");
  var selJobKey = SelectedJob.value;
  var selJobTitle = (SelectedJob[SelectedJob.selectedIndex].text);
  if (selJobKey == '') {
    alert ("Please select a job opening");
  }
  else {
    var timeStamp = new Date();
    createXMLHttpRequest(); 
    var params = "Category=HWeb&Service=HRSJobList&Ajax=yes&func=joblist&key= " + selJobKey + "&time=" + timeStamp;                                                  
    xmlHttp.onreadystatechange = displayJobDetail;                           
    xmlHttp.open("GET", "http://" + strDomain + "/guest/default.asp?" + params);                 
    xmlHttp.send(null);
    deleteElements("selJobName");
    var JobTitle = document.getElementById("selJobName");
	var boldTag = document.createElement("strong");
    var txt = document.createTextNode(selJobTitle);
    boldTag.appendChild(txt); 
    JobTitle.appendChild(boldTag);                                                    
  }
  return false;
}

function displayJobDetail(){
 var reqNode;
 if(xmlHttp.readyState == 4) {                                                
   if(xmlHttp.status == 200) {
     var returnMessage = xmlHttp.responseText;
     checkForTimeOut(returnMessage);
     var xmlDoc = xmlHttp.responseXML;
     deleteElements("jresponsibilities");
     var Reqoutputarea = document.getElementById("jresponsibilities");
     var jreqNode = xmlDoc.getElementsByTagName("JobReq");   
     for (var i = 0; i < jreqNode.length; i++) {
       var liItemTag = document.createElement("li");
       var txt = document.createTextNode(jreqNode[i].firstChild.nodeValue);
       liItemTag.appendChild(txt); 
       Reqoutputarea.appendChild(liItemTag);
     }
     deleteElements("jqualifications");
     var Qualoutputarea = document.getElementById("jqualifications");
     var jQualNode = xmlDoc.getElementsByTagName("JobQual");
     for (var i = 0; i < jQualNode.length; i++) {
       var liItemTag = document.createElement("li");
       var txt = document.createTextNode(jQualNode[i].firstChild.nodeValue);
       liItemTag.appendChild(txt); 
       Qualoutputarea.appendChild(liItemTag);
     }
     deleteElements("contactinfo");
     var ContactInfoarea = document.getElementById("contactinfo");
     var pItemTag = document.createElement("p");
     var strongTag = document.createElement("strong")
     var txt = document.createTextNode("Send Resume to:");
     pItemTag.appendChild(strongTag);
     strongTag.appendChild(txt);
     ContactInfoarea.appendChild(pItemTag); 
     var ulItemTag = document.createElement("ul");
     ContactInfoarea.appendChild(ulItemTag); 
     ulItemTag.style.listStyle = "none";
     var contactNode = xmlDoc.getElementsByTagName("address1");
     if (contactNode[0].firstChild.nodeValue != "empty") {
       var liItemTag = document.createElement("li");
       var txt = document.createTextNode(contactNode[0].firstChild.nodeValue);
       liItemTag.appendChild(txt); 
       ulItemTag.appendChild(liItemTag);
     }
     var contactNode = xmlDoc.getElementsByTagName("address2");
     if (contactNode[0].firstChild.nodeValue != "empty") {
       var liItemTag = document.createElement("li");
       var txt = document.createTextNode(contactNode[0].firstChild.nodeValue);
       liItemTag.appendChild(txt); 
       ulItemTag.appendChild(liItemTag);
     }
     var contactNode = xmlDoc.getElementsByTagName("address3");
     if (contactNode[0].firstChild.nodeValue != "empty") {
       var liItemTag = document.createElement("li");
       var txt = document.createTextNode(contactNode[0].firstChild.nodeValue);
       liItemTag.appendChild(txt); 
       ulItemTag.appendChild(liItemTag);
     }
     var contactNode = xmlDoc.getElementsByTagName("address4");
     if (contactNode[0].firstChild.nodeValue != "empty") {
       var liItemTag = document.createElement("li");
       var txt = document.createTextNode(contactNode[0].firstChild.nodeValue);
       liItemTag.appendChild(txt); 
       ulItemTag.appendChild(liItemTag);
     }
     var contactNode = xmlDoc.getElementsByTagName("cCity");
     if (contactNode[0].firstChild.nodeValue != "empty") {
       var liItemTag = document.createElement("li");
       var txt = document.createTextNode(contactNode[0].firstChild.nodeValue);
       liItemTag.appendChild(txt); 
       ulItemTag.appendChild(liItemTag);
     }
     var contactNode = xmlDoc.getElementsByTagName("cFax");
     if (contactNode[0].firstChild.nodeValue != "empty") {
       var liItemTag = document.createElement("li");
       var txt = document.createTextNode(contactNode[0].firstChild.nodeValue);
       liItemTag.appendChild(txt); 
       ulItemTag.appendChild(liItemTag);
     }
     var contactNode = xmlDoc.getElementsByTagName("cEmail");
     if (contactNode[0].firstChild.nodeValue != "empty") {
       var liItemTag = document.createElement("li");
       var aItemTag = document.createElement("a");
       aItemTag.className = "about";
       aItemTag.setAttribute("href","mailto:" + contactNode[0].firstChild.nodeValue);
       var txt = document.createTextNode(contactNode[0].firstChild.nodeValue);
       aItemTag.appendChild(txt); 
       liItemTag.appendChild(aItemTag); 
       ulItemTag.appendChild(liItemTag);
     }
   }                                                                         
 }                                                                                 
}
     
function setLocations(locNode) {
  var branchNode;
  var branch;
  var branchCodeNode;
  var branchCode;
  var currentLoc = null;
  var locList = document.getElementById("Locations");
  var option = null;
  var brLoc = locNode.getElementsByTagName("location");
  for (var i = 0; i < brLoc.length; i++) {
     currentLoc = brLoc[i];
     branchNode = currentLoc.firstChild;
     branch = branchNode.firstChild.nodeValue;
     branchCodeNode = currentLoc.lastChild;
     branchCode = branchCodeNode.firstChild.nodeValue;
     option = document.createElement("option");
     option.setAttribute("value",branchCode);
     if (branch[0] == "*") {
      option.className = "highlight";
    }
     if (selLocKey == branchCode) {
       option.setAttribute("selected","selected");
     }
     option.appendChild(document.createTextNode(branch));
     locList.appendChild(option);
  }
}

function setCategories(catNode) {
  var catNode;
  var cat;
  var catCodeNode;
  var catCode;
  var currentCat = null;
  var catList = document.getElementById("category");
  var option = null;
  var catLoc = catNode.getElementsByTagName("JCategory");
  for (var i = 0; i < catLoc.length; i++) {
     currentCat = catLoc[i];
     catNode = currentCat.firstChild;
     cat = catNode.firstChild.nodeValue;
     catCodeNode = currentCat.lastChild;
     catCode = catCodeNode.firstChild.nodeValue;
     option = document.createElement("option");
     option.setAttribute("value",catCode);
     if (cat[0] == "*") {
       option.className = "highlight";
     }
     if (selCatKey == catCode) {
       option.setAttribute("selected","selected");
     }
     option.appendChild(document.createTextNode(cat));
     catList.appendChild(option);
  }
}
 
function setJobList(JobNode){
  var titleNode;
  var title;
  var keyNode;
  var key;
  var JList = document.getElementById("jlist");
  var currentJob = null;
  var option = null;
  var Jobs = JobNode.getElementsByTagName("JobSumm");
  autoLoadDescription = 'No';
  for (var i = 0; i < Jobs.length; i++) {
     autoLoadDescription = 'yes';
     currentJob = Jobs[i];
     titleNode = currentJob.firstChild;
     title = titleNode.firstChild.nodeValue;
     keyNode = currentJob.lastChild;
     key = keyNode.firstChild.nodeValue;
     option = document.createElement("option");
     option.setAttribute("value",key);
     option.appendChild(document.createTextNode(title));
     JList.appendChild(option);
  }
}

function clearHTMLPage() {
     deleteElements("jresponsibilities");
     deleteElements("jqualifications");
     deleteElements("contactinfo");
     deleteElements("selJobName");
     var JobTitle = document.getElementById("selJobName");
	 var boldTag = document.createElement("strong");
     var txt = document.createTextNode("No job selected");
     boldTag.appendChild(txt); 
     JobTitle.appendChild(boldTag);                                                    
}

function serverUnavailable() {
 var JSMessage = document.getElementById("ServerUnavailableMessage");
     JSMessage.className = "show";
 var pageContent = document.getElementById("Joblistings");
     pageContent.className = "hide";
}
                                                                             
function deleteElements(elementName) {
  var thisElement = document.getElementById(elementName);
    while (thisElement.childNodes.length > 0) {
       thisElement.removeChild(thisElement.childNodes[0]);
    }
}    

function checkForTimeOut (returnMessage) {
     if (returnMessage.indexOf("<ServerTimedOut>Yes</ServerTimedOut>") >= 0) {
	   alert("Current Job Openings has timed out and will need to reload.");
	   location.replace("Job_Openings.aspx");
	 }
     return true;
}     
