﻿//      This javascript file handles the create, populate and submit functions
//      for the Kelly Services Global Job Search.
//      It makes calls to a server side ASPX form, which in turn delivers
//      the data needed to dynamically populate the dropdown lists based on location.
//
//      Author: Dean Abraham
//      Date:   November 4, 2008  The day Obama got elected!

//Global XMLHTTP Request object
var XmlHttp;

//Creating and setting the instance of appropriate XMLHTTP Request object to a “XmlHttp” variable
function CreateXmlHttp()
{
    //Creating object of XMLHTTP in IE
    try
    {
        XmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
        try
        {
            XmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        catch (oc)
        {
            XmlHttp = null;
        }
    }
    
    //Creating object of XMLHTTP in Mozilla and Safari
    if (!XmlHttp && typeof XMLHttpRequest != "undefined")
    {
        XmlHttp = new XMLHttpRequest();
    }
}

//Gets called when country combo box selection changes
function InitCountries()
{
    var countryList = document.getElementById("countryList");

    // URL to get data
    var requestUrl = AjaxServerPageName;
    CreateXmlHttp();
    // If browser supports XMLHTTPRequest object
    if (XmlHttp) {
        //Setting the event handler for the response
        XmlHttp.onreadystatechange = HandleResponseInit;
        //Initializes the request object with GET (METHOD of posting), 
        //Request URL and sets the request as asynchronous.
        XmlHttp.open("GET", requestUrl, true);
        //Sends the request to server
        XmlHttp.send(null);
    }
}

//Gets called when country combo box selection changes
function CountryListOnChange()
{
    var countryList = document.getElementById("countryList");
    //Getting the selected country from country combo box.
    if (countryList.selectedIndex == null){countryList.selectedIndex = 0}
    var selectedCountry = countryList.options[countryList.selectedIndex].value;
    
    // URL to get data for a given country
    var requestUrl = AjaxServerPageName + "?SelectedCountry=" + encodeURIComponent(selectedCountry);
    CreateXmlHttp();
    // If browser supports XMLHTTPRequest object
    if (XmlHttp) {
        //Setting the event handler for the response
        XmlHttp.onreadystatechange = HandleResponse;
        //Initializes the request object with GET (METHOD of posting), 
        //Request URL and sets the request as asynchronous.
        XmlHttp.open("GET", requestUrl, true);
        //Sends the request to server
        XmlHttp.send(null);
    }
}

//Called when response comes back from server for page init
function HandleResponseInit()
{
    // To make sure receiving response data from server is completed
    if (XmlHttp.readyState == 4) {
        // To make sure valid response is received from the server, 200 means response received is OK
        if (XmlHttp.status == 200) 
        {
            ClearAndSetCountryList(XmlHttp.responseXML.documentElement);
            
            //Get the Country Querystring, if provided
            var country = getQueryVariable("Country");
            //select the country and hide the selector control
            if(country != null && country != "")
            {                       
                var countryList = document.getElementById("countryList");
                for(i=0;i<countryList.length;i++)
                {
                    if(countryList.options[i].value == unescape(country))
                    {
                        countryList.selectedIndex = i;
                        countryList.className = "hidden";
                    }
                }
            }
            CountryListOnChange();
        }
        else 
        {
            displayError();
            //alert("There was a problem retrieving data from the server.");
        }
    }
}


function ShowJobLink() {
    document.getElementById("jobLink").style.display = "block";
}

function HideJobLink() {
    document.getElementById("jobLink").style.display = "none";
}

function ShowJobLinkapac() {
    document.getElementById("jobLinkapac").style.display = "block";
}

function HideJobLinkapac() {
    document.getElementById("jobLinkapac").style.display = "none";
}


//Called when response comes back from server
function HandleResponse()
{

    var countryList = document.getElementById("countryList");
    //Getting the selected country from country combo box.
    if (countryList.selectedIndex == null){countryList.selectedIndex = 0}
    var selectedCountry = countryList.options[countryList.selectedIndex].value;



    // To make sure receiving response data from server is completed
    if (XmlHttp.readyState == 4) {
        // To make sure valid response is received from the server, 200 means response received is OK
        if (XmlHttp.status == 200) {
            ClearAndSetKeywordLabel(XmlHttp.responseXML.documentElement);
            ClearAndSetSubmitLabel(XmlHttp.responseXML.documentElement);
            ClearAndSetCategoryListItems(XmlHttp.responseXML.documentElement);
            ClearAndSetJobTypeListItems(XmlHttp.responseXML.documentElement);
            ClearAndSetLocationListItems(XmlHttp.responseXML.documentElement);
            ClearAndSetDivisionListItems(XmlHttp.responseXML.documentElement);
            
          
        if ((selectedCountry == 'United States - English')) {
                ShowJobLink();            
            } else {
                HideJobLink();
            }
            
          // if ((selectedCountry == 'China - English')|| (selectedCountry == 'Malaysia - English')|| (selectedCountry == 'Indonesia - English')|| (selectedCountry == 'HongKong - English')|| (selectedCountry == 'Thailand - English')|| (selectedCountry == 'New Zealand - English')) {
           if ((selectedCountry == 'India - English') || (selectedCountry == 'Australia - English') || (selectedCountry == 'China - English') || (selectedCountry == 'Malaysia - English') || (selectedCountry == 'Indonesia - English') || (selectedCountry == 'Singapore - English') || (selectedCountry == 'New Zealand - English')){
                ShowJobLinkapac();            
            } else {
                HideJobLinkapac();
            }
            
            
        }
        else
        {
            displayError();   
            //alert("There was a problem retrieving data from the server.");
        }
    }
}
//Returns the node text value
function GetInnerText(node)
{
    return (node.textContent || node.innerText || node.text);
}


//Clears the and sets the country dropdown list
function ClearAndSetCountryList(countryNodes)
{
var countryList = document.getElementById("countryList");
var countryNodes = countryNodes.getElementsByTagName('country');
for (var count = 0; count < countryNodes.length; count++) {
            textText = GetInnerText(countryNodes[count]);
            textValue = countryNodes[count].getAttribute('value');
            //Populate the textElement variable ONLY if not null.
            //Theoretically, we should only need to put element in One XML node
            if(countryNodes[count].getAttribute('element') != null)
            {
                textElement = countryNodes[count].getAttribute('element');
            }
            optionItem = new Option(textText, textText, false, false);
            countryList.options[countryList.length] = optionItem;
        }
}

//Clears the Keywords label and adds the correct label of currently selected country
function ClearAndSetKeywordLabel(countryNode)
{
    var keywordText = document.getElementById("keywordLBL");
    var KeywordElement = document.getElementById("KeywordElementHF");
    var textElement = countryNode.getAttribute('keywordelement');
    var keywordTextBox = document.getElementById("keywordTextBox");
    var submitAnchor = document.getElementById("submitAnchor");
    
    KeywordElement.setAttribute("value",textElement);
    keywordText.value = countryNode.getAttribute('keywordlabel');
    keywordTextBox.value = countryNode.getAttribute('keywordlabel');
    if(keywordTextBox.value == "null" || keywordTextBox.value == "")
    {
        keywordTextBox.className = 'hidden';
        submitAnchor.className = 'BlocksubmitImage';    
    }
    else
    {
        keywordTextBox.className = 'textbox';
        submitAnchor.className = 'submitImage';    
    }
}

//Clears the submit image and adds the correct images of currently selected country
function ClearAndSetSubmitLabel(countryNode) {
    var submitlocationHF = document.getElementById("SubmitLocationHF");
    var submitAnchor = document.getElementById("submitAnchor");
    var languageHF = document.getElementById("LanguageElementHF");
     
    submitAnchor.innerHTML = countryNode.getAttribute('submitlabel');
    var submitLocation = countryNode.getAttribute('submitlocation');
    var languageCode = countryNode.getAttribute('cLang');
    if (submitLocation != null && submitLocation != "")
    {
        submitlocationHF.setAttribute("value", countryNode.getAttribute('submitlocation'));
    }
    else
    {
        submitAnchor.className = 'hidden';
    }
    
    if (languageCode != null && languageCode != "") {
        languageHF.setAttribute("value", languageCode);
    } else {
    	languageHF.setAttribute("value", "");
    }
}

//Clears the contents of category combo box and adds the categories of currently selected country
function ClearAndSetCategoryListItems(countryNode)
{
    var categoryList = document.getElementById("categoryList");
    //Clears the category combo box contents.
    for (var count = categoryList.options.length - 1; count > -1; count--) {
        categoryList.options[count] = null;
    }
    var categoryNodes = countryNode.getElementsByTagName('category');
    var textText;
    var textValue;
    var textElement;
    var optionItem;
    if (categoryNodes.length > 0)
    {
        //Add new category list to the category combo box.
        for (var count = 0; count < categoryNodes.length; count++) {
            textText = GetInnerText(categoryNodes[count]);
            textValue = categoryNodes[count].getAttribute('value');
            //Populate the textElement variable ONLY if not null.
            //Theoretically, we should only need to put element in One XML node
            if(categoryNodes[count].getAttribute('element') != null)
            {
                textElement = categoryNodes[count].getAttribute('element');
            }
            optionItem = new Option(textText, textValue, false, false);
            categoryList.options[categoryList.length] = optionItem;
        }
        //Put the name of the element in a hidden fields so it can be retrieved later
        //without another call to the server
        if(textElement != null)
        {
            var CategoryElement = document.getElementById("CategoryElementHF");
            CategoryElement.setAttribute("value",textElement);
        }
        categoryList.className = 'dropdown';
    }
    else
    {
        //Hide the Category dropdown, since it is empty.
        categoryList.className = 'hidden';
    }
}

//Clears the contents of location combo box and adds the locations of currently selected country
function ClearAndSetLocationListItems(countryNode)
{
    var locationList = document.getElementById("locationList");
    //Clears the location combo box contents.
    for (var count = locationList.options.length - 1; count > -1; count--) {
        locationList.options[count] = null;
    }
    var locationNodes = countryNode.getElementsByTagName('location');
    var textText;
    var textValue;
    var textElement;
    var optionItem;
    if (locationNodes.length > 0)
    {
        //Add new location list to the location combo box.
        for (var count = 0; count < locationNodes.length; count++) {
            textText = GetInnerText(locationNodes[count]);
            textValue = locationNodes[count].getAttribute('value');
            //Populate the textElement variable ONLY if not null.
            //Theoretically, we should only need to put element in One XML node
            if(locationNodes[count].getAttribute('element') != null)
            {
                textElement = locationNodes[count].getAttribute('element');
            }
            optionItem = new Option(textText, textValue, false, false);
            locationList.options[locationList.length] = optionItem;
        }
        //Put the name of the element in a hidden fields so it can be retrieved later
        //without another call to the server
        if(textElement != null)
        {
            var LocationElement = document.getElementById("LocationElementHF");
            LocationElement.setAttribute("value",textElement);
        }
        locationList.className = 'dropdown';
    }
    else
    {
        //Hide the location list, since it is empty.
        locationList.className = 'hidden';
    }
}

//Clears the contents of jobtype combo box and adds the categories of currently selected country
function ClearAndSetJobTypeListItems(countryNode)
{
    var jobtypeList = document.getElementById("jobtypeList");
    //Clears the jobtype combo box contents.
    for (var count = jobtypeList.options.length - 1; count > -1; count--) {
        jobtypeList.options[count] = null;
    }
    var jobtypeNodes = countryNode.getElementsByTagName('jobtype');
    var textText;
    var textValue;
    var textElement
    var optionItem;
    if(jobtypeNodes.length > 0)
    {
        //Add new jobtype list to the category combo box.
        for (var count = 0; count < jobtypeNodes.length; count++) {
            textText = GetInnerText(jobtypeNodes[count]);
            textValue = jobtypeNodes[count].getAttribute('value');
            //Populate the textElement variable ONLY if not null.
            //Theoretically, we should only need to put element in One XML node
            if(jobtypeNodes[count].getAttribute('element') != null)
            {
                textElement = jobtypeNodes[count].getAttribute('element');
            }
            optionItem = new Option(textText, textValue, false, false);
            jobtypeList.options[jobtypeList.length] = optionItem;
        }
        //Put the name of the element in a hidden fields so it can be retrieved later
        //without another call to the server
        if(textElement != null)
        {
            var JobTypeElement = document.getElementById("JobTypeElementHF");
            JobTypeElement.setAttribute("value",textElement);
        }
        jobtypeList.className = 'dropdown';
    }
    else
    {
        //Hide the jobtype dropdown, since it is empty.
        jobtypeList.className = 'hidden';
    }
    
}

//Clears the contents of division combo box and adds the categories of currently selected country
function ClearAndSetDivisionListItems(countryNode)
{
    var divisionList = document.getElementById("divisionList");
    //Clears the division combo box contents.
    for (var count = divisionList.options.length - 1; count > -1; count--) {
        divisionList.options[count] = null;
    }
    var divisionNodes = countryNode.getElementsByTagName('division');
    var textText;
    var textValue;
    var textElement;
    var optionItem;
    if(divisionNodes.length > 0)
    {
        //Add new division list to the category combo box.
        for (var count = 0; count < divisionNodes.length; count++) {
            textText = GetInnerText(divisionNodes[count]);
            textValue = divisionNodes[count].getAttribute('value');
            //Populate the textElement variable ONLY if not null.
            //Theoretically, we should only need to put element in One XML node
            if(divisionNodes[count].getAttribute('element') != null)
            {
                textElement = divisionNodes[count].getAttribute('element');
            }
            optionItem = new Option(textText, textValue, false, false);
            divisionList.options[divisionList.length] = optionItem;
        }
        //Put the name of the element in a hidden fields so it can be retrieved later
        //without another call to the server
        if(textElement != null)
        {
            var DivisionElement = document.getElementById("DivisionElementHF");
            DivisionElement.setAttribute("value",textElement);
        }        
        divisionList.className = 'dropdown';
    }
    else
    {
        //Hide the division dropdown, since it is empty
        divisionList.className = 'hidden';
    }
}

//Builds elements on the submittal form and submits
function submitForm()
{
    
    //Remove the keyword label, if present
    var keywordText = document.getElementById("keywordLBL");
    var keywordTextBox = document.getElementById("keywordTextBox");
    if(keywordTextBox.value == keywordText.value)
    {
        keywordTextBox.value = "";
    }
    
    //Get the values from the input form
    var keywordText = document.getElementById("keywordTextBox");
    var categoryList = document.getElementById("categoryList");
    var jobtypeList = document.getElementById("jobtypeList");
    var locationList = document.getElementById("locationList");
    var divisionList = document.getElementById("divisionList");
    var SubmitLocationHF = document.getElementById("SubmitLocationHF");

    
    //Get the expected input names
    var keywordElement = document.getElementById("KeywordElementHF");
    var categoryElement = document.getElementById("CategoryElementHF");
    var jobtypeElement = document.getElementById("JobTypeElementHF");
    var locationElement = document.getElementById("LocationElementHF");
    var divisionElement = document.getElementById("DivisionElementHF");
    var languageElement = document.getElementById("LanguageElementHF");
    
    //Create the individual elements for the form  Clang exclusion....what a hack!  hOw embarrasing!
    //if(keywordTextBox.value != "" && keywordElement.value != "cLang")
    //{
	createElement(keywordElement.value, keywordText.value);
    //}
    if(categoryList.length > 0){createElement(categoryElement.value, categoryList.options[categoryList.selectedIndex].value)};
    if(jobtypeList.length > 0){createElement(jobtypeElement.value, jobtypeList.options[jobtypeList.selectedIndex].value)};
    if(locationList.length > 0){createElement(locationElement.value, locationList.options[locationList.selectedIndex].value)};
    if(divisionList.length > 0){createElement(divisionElement.value, divisionList.options[divisionList.selectedIndex].value)};
    if(languageElement.value.length > 0){createElement("cLang", languageElement.value)};
    
    //Set the submittal form action then submit
    document.forms['SubmittalForm'].action = SubmitLocationHF.value;
    document.forms['SubmittalForm'].submit();
}

//Creates individual elements on submittal form
function createElement(name, value)
{
    //Need to check if element already exists
    //and change its value or create it
    var remEl = document.getElementById(name);
    if (remEl === null) 
    {
    	new_element = document.createElement("input");
    	new_element.setAttribute("type", "hidden");
    	new_element.setAttribute("name", name);
    	new_element.setAttribute("id", name);
    	new_element.setAttribute("value", value);
    	document.forms['SubmittalForm'].appendChild(new_element);
    } else {
        remEl.value = value;
    }
}

//Clears the initial textbox value upon entry
function clear_keywords()
{
    var keywordText = document.getElementById("keywordLBL");
    var keywordTextBox = document.getElementById("keywordTextBox");
    if(keywordTextBox.value == keywordText.value)
    {
        keywordTextBox.value = "";
    }
}

//Returns a specific QueryString value
function getQueryVariable(variable)
{
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++)
    {
        var pair = vars[i].split("=");
        if (pair[0] == variable)
        {
            return pair[1];
        }
    }
}

//Hides input values and displays error message
function displayError()
{
    var countryList = document.getElementById("countryList");
    var keywordText = document.getElementById("keywordTextBox");
    var categoryList = document.getElementById("categoryList");
    var jobtypeList = document.getElementById("jobtypeList");
    var locationList = document.getElementById("locationList");
    var divisionList = document.getElementById("divisionList");
    var SubmitLocationHF = document.getElementById("SubmitLocationHF");
    var errorText = document.getElementById("errorTextArea");
    var SubmitAnchor = document.getElementById("submitAnchor");

    countryList.className = "hidden";
    keywordText.className = "hidden";
    categoryList.className = "hidden";
    jobtypeList.className = "hidden";
    locationList.className = "hidden";
    divisionList.className = "hidden";
    SubmitAnchor.className = "hidden";
    errorText.className = "error";
}


<!--
//Server:WWb02
-->
