/* 
 * Main Ajax content
 */

function LoadAjax(subPage, id){
    
    // First build the XMLHttp object
    var xmlHttpReq;
    var pathAdditions = "";
    var goodToGo = false;
    var sendType = "GET";
    var formContent = "";
    var contLeng = 0;
    
    // IE likes to do things differently
    if(navigator.appName == "Microsoft Internet Explorer"){
        
        // IE
        xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
        
    }else{
        
        // FF, Opera...
        xmlHttpReq = new XMLHttpRequest();
        
        if (xmlHttpReq.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            xmlHttpReq.overrideMimeType('text/html');
         }
         
    }
    
    // Check the status
    xmlHttpReq.onreadystatechange = function(){

        // If the ready state is at 4, get any content from the page
        if(xmlHttpReq.readyState == 4){

            // Check if page was found
            if(xmlHttpReq.status == 200){
                
                // Depending on what we need, the reponse text is used differently
                var httpContent = xmlHttpReq.responseText;
                
                // do the switch, note that if no subpage is given, the requested page is still executed
                // if one simply wanted to update the DB for example, you wouldn't need a return value
                switch(subPage){
                    
                    // Get the data for the dropdown
                    case "ddRegionUpdate":
                        
                        // Use the retrieved data
                        updateRegionDropdown(httpContent, id);
                        
                    break;
                    
                    // Get the data of a vacancy
                    case "vacancyData":
                        
                        // Set the values
                        setVacancyData(httpContent);
                            
                    break;
                    
                    // Send a mail via the data from the contact form
                    case "sendContactMail":
                        
                        // Check to see if the mail was succesful
                        alert(httpContent);
                        
                    break;
                    
                    // Get the data of a vacancy
                    case "vacancyTitle":
                        
                        // Set the values
                        setVacancyTitle(httpContent);
                            
                    break;
                }
                
            }else{

                // Send warning
                //alert("Page not found");
            }
        }
    }
    
    // Determine if any additional data needs to be sent
    switch(subPage){
        
        case "ddRegionUpdate":
            
            // Get ID from discipline dropdown
            var dSelIndex = document.getElementById("discipline").selectedIndex;
            var dID = document.getElementById("discipline").options[dSelIndex].value;
            
            // We only continue if the ID is valid (e.g. not 0)
            if(dID != NaN){
                
                // We can load the page now
                goodToGo = true;
            
                // Set ID in path additions
                pathAdditions = dID;
            }
            
        break;
        
        case "vacancyData":
        case "vacancyTitle":
            
            // We only continue if the ID is valid (e.g. not 0)
            if(id != NaN){
                
                // We can load the page now
                goodToGo = true;
            
                // Set ID in path additions
                pathAdditions = id;
            }
            
        break;
        
        case "sendContactMail":
            
            // Check length
            if(id.length > 0){
              
                // We can load the page
                goodToGo = true;
                sendType = "POST";
                formContent = id;
                contLeng = formContent.length;
                
            }
            
        break;
    }
        
    // Open the given page if we are good to go
    if(goodToGo == true){
        
        // Check
        if(pathAdditions.length > 0){
            
            // Add /
            pathAdditions = "/" + pathAdditions;
        }
        
        // Use the page
        //xmlHttpReq.open(sendType, "http://localhost/flextep/ajax/"+subPage+"/"+addRandom()+pathAdditions, true);
        xmlHttpReq.open(sendType, "http://www.website.flextep.com/ajax/"+subPage+"/"+addRandom()+pathAdditions, true);
        
        // Add headers if POST
        if(sendType == "POST"){
            
            // Add headers
            xmlHttpReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlHttpReq.setRequestHeader("Content-length", contLeng);
            xmlHttpReq.setRequestHeader("Connection", "close");
            xmlHttpReq.send(formContent);
            
        }else{
        
            // Send it
            xmlHttpReq.send(null);
        }
        
    }      
}

// Adds seconds since 1970 so the URL is always unique (IE issue)
function addRandom(){
    
    // build secs
    var date = new Date();
    var secs = date.getTime();
    
    // return secs
    return secs;
}

// Takes in and translates the http data for the region dropdown in the left menu
function updateRegionDropdown(encodedResult, regionID){
    
    // The result is encoded, |:| is the separator for a row, |;| for a column/field/value
    var rowArray = encodedResult.split("|:|");
    
    // Check id
    if(regionID == NaN || regionID == "undefined" || regionID <= 0)
    {
        // Set zero
        regionID = 0;
    }
    
    // Loop through the data (we leave the first option as it is)
    var iterator = 1;
    
    // Clear current options in the dropdown
    var regionDropdown = document.getElementById("region");
    ClearDropdown("region", iterator);
    
    // Start looping
    while(iterator < rowArray.length){
        
        // Split by values
        var columnSplit = rowArray[iterator].split("|;|");
        
        // First value is ID, second is description and third is amount
        regionDropdown.options[iterator] = new Option(columnSplit[1]+" ("+columnSplit[2]+")", columnSplit[0]);
        
        // Check for matching selection
        if(regionID > 0)
        {
            // Check it
            if(Number(columnSplit[0]) == Number(regionID))
            {
                // We have a match
                regionDropdown.options[iterator].selected = true;
            }
        }
        
        // Add to iterator
        ++iterator;
    }
}

// Removes all options from a dropdown
function ClearDropdown(theDropdownName, itStart){
    
    // Set it
    var theLength = document.getElementById(theDropdownName).options.length;
    
    // If undefined, set to 0
    if(itStart == "undefined" || itStart == NaN || itStart < 0){
        
        itStart = 0;
    }
    
    // Loop through it all
    for(var ddI = itStart; ddI < theLength; ++ddI){
        
        // Remove 'm all
        document.getElementById(theDropdownName).remove(1);
    }
}

// Set the vacancy data
function setVacancyData(encodedResult){
    
    // Display the divs
    getItem("mainPageBG").style.display = "";
    getItem("mainPage").style.display = "";
    
    // Separate the data
    var valueList = encodedResult.split("|:|");
    
    // Set each value
    getItem("vacancyTitleVal").innerHTML = valueList[0];
    getItem("vacancyCompanyVal").innerHTML = valueList[1];
    getItem("vacancyDescVal").innerHTML = valueList[2];
    getItem("vacancyTasksVal").innerHTML = valueList[3];
    getItem("vacancyReqVal").innerHTML = valueList[4];
    getItem("vacancyCommentsVal").innerHTML = valueList[5];
    getItem("vacancySollicitVal").innerHTML = valueList[6];
    getItem("vacancyCityVal").innerHTML = valueList[7];
}

// Get vacancy title
function setVacancyTitle(titleName){
    
    getItem("app_vacancy_desc").value = titleName;
}
