﻿var paging_total = 0;
//////////////////////////////////////////////////////////////////////////////////////////////////
//create the ajax obj
function createRequestObject() {
    var req;
    if(window.XMLHttpRequest){
        // Firefox, Safari, Opera...
        req = new XMLHttpRequest();
    } else if(window.ActiveXObject) {
        // Internet Explorer 5+
        req = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
        // There is an error creating the object,
        // just as an old browser is being used.
        alert('Problem creating the XMLHttpRequest object');
    }
    return req;
}
// Make the XMLHttpRequest object
var http = createRequestObject();
//////////////////////////////////////////////////////////////////////////////////////////////////
//send the ajax request
function sendRequestJobSearch() {
    
    //show loading
    $('r_').innerHTML = '<img src="SiteImages/loading.gif" /> Loading...';

    var page = document.getElementById('page').value;
    var criteria = document.getElementById('criteria').value;
    var dir = document.getElementById('dir').value;
    var reqFrom = document.getElementById('ctl00_content_reqFrom').value;
    
    var jTitle = document.getElementById('job_title').value;
    var jCategory = document.getElementById('ctl00_content_cmb_JobRole').value;
    var jLocation = document.getElementById('ctl00_content_cmb_Country').value;
    
    //var params = '?SortDirection='+dir+'&SortCriteria='+criteria+'&PageNo='+page+'&FilterBy='+filterby;
    var params = '?t='+jTitle+'&j='+jCategory+'&c='+jLocation+'&SortDirection='+dir+'&SortCriteria='+criteria+'&PageNo='+page+'&ac=true' + '&reqFrom=' + reqFrom;
    
    http.open( 'get', 'JobSearchCode.aspx' + params , true );
    http.onreadystatechange = function (){ handleResponseJobSearch() };
    http.send( null );

}
//////////////////////////////////////////////////////////////////////////////////////////////////
function handleResponseJobSearch(){

    if(http.readyState == 4 && http.status == 200){
        
        //response is JSON-enabled object
        start = http.responseText;
        try{
        if( start == 'no' ){
            response = '';
        }else{
            response = eval('('+start+')');
        }
                
        if( response != '' ){
        
            var count = response.PagesCount;
            if( count > 0 ) {
            
                //change fetch_type value to anything other than 'recent'
                //create pages count [paging]
                //repeat results box n times
                //var fetchType = document.getElementById( 'fetch_type' );
                //fetchType.value = 'done';

                //clear the [loading] identifier
                document.getElementById('r_').innerHTML = '';
                
                //show sorting
                document.getElementById('sorting_criteria').style.display = 'block';
                writeHeader();
                //build the result DIVs
                for( i=0 ; i<response.jobs.length ; i++ ){
                
                    //needle
                    var g = response.jobs[i];
                    
                    //prepare items to be populated
                    job = g.jobid;
                    date = g.dateposted;
                    title = g.title;
                    if(title=='') title = '--';
                    desc = g.description;
                    company = g.company;
                    if(company=='') company = '--';
                    Location = g.location;
                    details_id = '';
                    
                    //unit structure
                    unit( job , date , title , desc , company , Location , details_id , i );
                          
                }
                
                //build paging
                var counterLength = response.PagesCount;
                var pagingDiv = document.getElementById('paging');
                pagingDiv.style.display = 'block';
                //clear the paging area
                pagingDiv.innerHTML = '';
                
                if( counterLength > 1 ){
                    for( j=0 ; j<counterLength ; j++ ){
                        paging( j+1 );
                    }
                                
                    //store num of pages to use it when clearing the highlight effect
                    paging_total = j;
                    
                    //highlight the first link
                    var p = document.getElementById('page');
                    if( p.value == 1 ){
                        document.getElementById('1').className = 'auctionsLink_over';
                    }else{
                        document.getElementById( p.value ).className = 'auctionsLink_over';
                    }
                }
                
                //# of results
                var resultsLength = response.NumberOfResults;
                document.getElementById('num_of_results').innerHTML = resultsLength;
                
            //write no results
            }else{
                document.getElementById('r_').innerHTML = 'No results found.';
                document.getElementById('sorting_criteria').style.display = 'none';
            }
        
        }else{
            document.getElementById('r_').innerHTML = 'No results found.';
            document.getElementById('num_of_results').innerHTML = 0;
        }
        }catch(e){
            alert('Error: [' + e.name + '] in the received results!');
            $('r_').innerHTML = 'Please select another option..'; 
        }
    
    
    }
    
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function resetPaging(){
    //reset all highlights
    for( i=1 ; i<=paging_total ; i++ ){
        document.getElementById( i ).className = 'auctionsLink';
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//when clicking the search button
function search(){
    //reset to defaults, then, send call
    document.getElementById('page').value = 1;
    document.getElementById('criteria').value = 'SubmissionDate';
    document.getElementById('dir').value = 'down';
    
    if( document.getElementById('paging').style.display == 'block' ){
        resetPaging();
        document.getElementById('1').className = 'auctionsLink_over';
    }
    document.getElementById('r').innerHTML = 'Loading...';
    sendRequestJobSearch();
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//create paging after search
function paging( linkID ){
    document.getElementById('paging').innerHTML += '<a id="'+linkID+'" href="#" class="auctionsLink" onclick="pagingGo(this.id);return false;">'+linkID+'</a> ';
}
//////////////////////////////////////////////////////////////////////////////////////////////////
//when click on paging item
function pagingGo( pageID ){
    //show loading
    $('r_').innerHTML = '<img src="SiteImages/loading.gif" /> Loading...';
    //change [page] value, then, send call
    document.getElementById('page').value = pageID;
    //reset paging
    resetPaging();
    //highlight the clicked value
    document.getElementById(pageID).className = 'auctionsLink_over';
    //send the call
    sendRequestJobSearch();
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function writeHeader(){
    document.getElementById('r_').innerHTML = '<!--Single unit--><div style="width:97%; height:30px; background-color:#F7F7F7;"><!--Header--><div class="job_search_date_header">Date</div><div class="job_search_job_title_header">Job Title</div><div class="job_search_company_header">Company</div><div class="job_search_location_header">Location</div><div class="job_search_details_header">Details</div></div>';
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function unit( job , date , title , desc , company , Location , details_id , i ){

    divColor = '';
    document.getElementById('r_').innerHTML += '<!--Body--><div style="width:97%; height: auto; overflow:visible; border-bottom:1px solid #03AF03;"><!--Header--><div class="job_search_date_results">'+date+'</div><div class="job_search_job_title_results" onclick="showHide(\'innerDiv'+i+'\');">'+title+'<div class="job_desc" id="innerDiv'+i+'">'+desc+'</div></div><div class="job_search_company_results">'+company+'</div><div class="job_search_location_results">'+Location+'</div><div class="job_search_details_results"><a href="JobDetails.aspx?JobID='+ job +'">More Details</a></div></div><!--End single unit--><div style="clear:left;"></div><!--End single unit-->';

}
//////////////////////////////////////////////////////////////////////////////////////////////////
function sort( val ){
    
    //change [criteria] value, change [dir] value, then, send call
    var c = document.getElementById('criteria');
    var d = document.getElementById('dir');
    //var f = document.getElementById('filterby');
    
    if( d.value == 'up' ) d.value = 'down';
    else d.value = 'up';
   
    c.value = val;
    
    //show loading
    $('r_').innerHTML = '<img src="SiteImages/loading.gif" /> Loading...';
    
    sendRequestJobSearch();
}
//////////////////////////////////////////////////////////////////////////////////////////////////
function showHide( id ){
    if($(id).style.display == 'none' || $(id).style.display == '') $(id).style.display = 'block';
    else $(id).style.display = 'none';
}
//////////////////////////////////////////////////////////////////////////////////////////////////
