function submit_form(form_id) {
    if (form_id == undefined || form_id == null) return false;
    $('form#' + form_id).submit();
    return true;
}

function load_link(href) {
    window.location = href;
    return true;
}


//Agency Opt Out toggle Terms
//////////////////////////////////////////////////////////////////////////////////////
function toggleAgencyOptOutLegal() {
    $('select#Form_AgencyOptOut_OptOut').change(

    function () {
        if ($('select#Form_AgencyOptOut_OptOut option:selected').val() == 'Opt Out') {
            $('span#Form_AgencyOptOut_OptOutLegalYes').attr('class', 'OptOutLegalShow');
            $('span#Form_AgencyOptOut_OptOutLegalNo').attr('class', 'OptOutLegalHide');
        } else if ($('select#Form_AgencyOptOut_OptOut option:selected').val() == 'Opt In') {
            $('span#Form_AgencyOptOut_OptOutLegalYes').attr('class', 'OptOutLegalHide');
            $('span#Form_AgencyOptOut_OptOutLegalNo').attr('class', 'OptOutLegalShow');
        }
        else {
            $('span#Form_AgencyOptOut_OptOutLegalYes').attr('class', 'OptOutLegalHide');
            $('span#Form_AgencyOptOut_OptOutLegalNo').attr('class', 'OptOutLegalHide');
        }
    });
}



//Solution for IE6 submit button - Hover
//Tell the browser to add a class when hovered over
function over(event) {
var x = 'btnOver';
$(this).addClass(x);
}
//tell the browser remove the class when
//going outside the object area
function out(event) {
$(this).removeClass('btnOver');
}


//This function adds a class at the end of each sector list so you can style the last item with
//the rounded corners background image
function lastListItem(){
	$("#sector_nav li:last").each(function(){
    $(this).addClass("lastListItem");
	});
	}









jQuery().ready(function () {

//Show staff form onload

showStaffForm();

 //Show and hide job details
swap();

//Hide last li
$('ul.pageMenu li:last').addClass("last");






//Solution for IE6 submit button - Hover
$('#Form_JobSearch_Submit, #Form_PostAJob_Submit, #Form_CallBack_Submit, #Form_RegisterAVacancy_Submit, #Form_Olr_Submit, #Form_AgencyWorkersRegulation_Submit').hover(over, out);
$('#Form_Jbe_Unsubscribe, #Form_Jbe_UnsubscribeReason, #Form_Jbe_Register, #Form_Jbe_RegisterButton').hover(over, out);
$('#Form_Olr_Submit, #Form_OlrReg_Submit, #Form_OlrShortlist_Submit, #Form_OlrLogin_Submit, .JobSearch_MoreButton').hover(over, out);
$('#Form_CVA_Submit_Container, #Form_CVA_Submit_Container input, #Form_SCVS_Submit_Container, #Form_SCVS_Submit_Container input, #Form_OlrLogin_Submit_Container ,#Form_OlrLogin_Submit_Container input').hover(over, out);

    //show the print button....
    $('div#content ul.jobOptions li a.print').css('display', 'block');
    $('div#content ul.jobOptions li a.print').click(function () {
        window.print();
        return false;
    });

    // Swap tabs and show job search / staff search form
    //////////////////////////////////////////////////////////////////////////////////////
    
    
    	//The following activates the staff search form after post a jopb has been completed
			
		function showStaffForm(){
		var staffTab = $('ul.tabs li.olr a').attr('class');
		var jobTab = $('ul.tabs li.js a').attr('class');
		
		
		var jobForm = $('form#Form_JobSearch');
		var staffForm = $('form#Form_Olr');
		
						
		
				if 	(staffTab == 'selected'){
						jobForm.hide();
						staffForm.show();
					}
				
				if 	(jobTab == 'selected'){
					staffForm.hide();
					jobForm.show();
					}

				
		}
    
    
    

    	
    		
			//show job search & swap button
			$('ul.tabs li.js a').click(function(e){
			e.preventDefault();
			$('ul.tabs li.olr a').removeClass('selected');
			$(this).addClass('selected');
			$('form#Form_Olr').hide();
			$('form#Form_JobSearch').show();
			});
			
			//show staff search & swap button
			$('ul.tabs li.olr a').click(function(e){
			e.preventDefault();
			$('ul.tabs li.js a').removeClass('selected');
			$(this).addClass('selected');
			$('form#Form_JobSearch').hide();
			$('form#Form_Olr').show();
			});
			
			

    
    
    
    
    
    
    
    
    
    


    // Add IE 6 hover states for submit buttons
    //////////////////////////////////////////////////////////////////////////////////////
    $('#Form_SCVS_Submit_Container input').hover(function () {
        $(this).addClass('hover');
    }, function () {
        $(this).removeClass('hover');
    });

    // Date Picker
    //////////////////////////////////////////////////////////////////////////////////////
    if (typeof datepicker == "function") {
        //add the date picker start date on staff match
        $("#staff_start_date").datepicker({
            duration: "slow",
            dateFormat: "dd/mm/yy"
        });
    }

    // Get the current language of the site from the language menu
    //////////////////////////////////////////////////////////////////////////////////////
    var sSiteLang = $('ul#language_nav li.sel a').attr('href').toLowerCase().replace('/','');
    var sSiteLang = sSiteLang.slice(15,17);

    // don't display the check boxes on the olr use images instead.
    $('table input[@type=checkbox]').each(function () {
        $(this).css('position', 'absolute');
        $(this).css('marginTop', '-999em');
        $(this).css('marginLeft', '-999em');

        $(this).parent('td').append('<img src="/img/lang/'+sSiteLang+'/btn/checkbox-off.gif" class="checkReplace">');
    });

    //check the hidden checkbox on td click & change the image to match
    $('#Form_OlrShortlist td.olr_ats').click(function () {
        if ($(this).attr('class') == 'olr_ats') {
            switch ($(this).children('input').attr('checked')) {
            case true:
                $(this).find("input").attr('checked', '');
                $(this).find("img").attr('src', $(this).find("img").attr('src').replace('-on.gif', '-off.gif'))
                break;

            case false:
                $(this).find("input").attr('checked', 'checked');
                $(this).find("img").attr('src', $(this).find("img").attr('src').replace('-off.gif', '-on.gif'))
                break;
            }
            // add a class to the short listed CV
            if (!$(this).children('input').attr('checked')) {
                $(this).parent().removeClass('addedCV');
            } else {
                $(this).parent().addClass('addedCV');
            }
        }
        else {
            return true;
        }
    });


    //Show and hide text for agency opt out options//
	toggleAgencyOptOutLegal();

    // Preload all mouseover form buttons
    //////////////////////////////////////////////////////////////////////////////////////
    $('form input[@type=image]').each(function () {
        if ($(this).attr('src').indexOf('spacer.gif') < 0) {
            if ($('div#preLoad').attr('id') != 'preLoad') {
                $('#wrapper').after('<div style="display:none;" id="preLoad"></div>');
            }
            $('#preLoad').prepend('<img width="1" height="1" src="' + $(this).attr('src').replace('-off.gif', '-on.gif') + '"/>');
        }
    })

    //swap the form submission button[s] to over on mouseover
    $('form input[@type=image], img.onOff').mouseover(function () {
        if ($(this).attr('src').indexOf('-on.gif') <= 0) {
            var NewSubmit = $(this).attr('src').replace('-off.gif', '-on.gif');
            $(this).attr('src', NewSubmit);
        }
    });

    $('form input[@type=image], img.onOff').mouseout(function () {
        if ($(this).attr('src').indexOf('-off.gif') <= 0) {
            var NewSubmit = $(this).attr('src').replace('-on.gif', '-off.gif');
            $(this).attr('src', NewSubmit);
        }
    });

    //swap the form submission button[s] to active on form submission
    $('form input[@type=image]').click(function () {
        if ($(this).attr('src').indexOf('img/spacer') > 0) {
            return true;
        }

        if ($(this).attr('src').indexOf('-active.gif') > 0) return false;
        var NewSubmit = $(this).attr('src').replace('-on.gif', '-active.gif');
        var NewSubmit = $(this).attr('src').replace('-off.gif', '-active.gif');
        $(this).attr('src', NewSubmit.replace('-on.gif', '-active.gif'));

        if ($(this).attr('class') == 'click2animate' && $(this).parent('a').attr('id') != 'apply_top' && $(this).parent('a').attr('id') != 'apply_bot') {
            setTimeout('load_link("' + $(this).parent('a').attr('href') + '")', 1);
            return false;
        } else {
            setTimeout('submit_form("' + $(this).parents().filter('form').attr('id') + '")', 1);
            return false;
        }
    });


    // Search Result page - style the whole row when mouseover.
    serpHover();

    //Home Page
    toggleFeaturedJobs();

    //Contact Us Office selected
    contactUsSelectedCountry();

	//Adds class to last list sector list item
	lastListItem();

});

function restorImg() {
    $('img').each(function () {
        if ($(this).attr('restoreimage') == 'yes') {
            $(this).attr('src', $(this).attr('src').replace('-active.gif', '-on.gif'));
            $(this).attr('restoreimage', '');
        }
    })
}

function GoToMap(sLocation) {
    //get the current url
    if (window.location.href.indexOf('&') < 0) {
        var sUrl = window.location.href + sLocation + '/';
    } else {
        var sUrl = window.location.href + '&option=' + sLocation;
    }
    window.location = sUrl;
}

// Sector Tabs
//////////////////////////////////////////////////////////////////////////////////////


function showSectorList() {
    $('div#filterBySector').css('borderColor', '#938B82');
    $('div#filterBySector ul#FeaturedJobsListNav').removeClass('hideall');
}

function hideSectorList() {
    $('div#filterBySector').css('borderColor', '#FFFFFF');
    $('ul#FeaturedJobsListNav').addClass('hideall');
}

function toggleFeaturedJobs() {
    // Featured Job - filter by sector
    $('div#filterBySector').css('visibility', 'visible');
    $('div#filterBySector').click(function () {
        if ($('ul#FeaturedJobsListNav').attr('class').indexOf('hideall') >= 0) {
            showSectorList();
        } else {
            hideSectorList();
        }
    });

    // Toggle Featured Jobs List
    //////////////////////////////////////////////////////////////////////////////////////
    $('div.colB ul#FeaturedJobsListNav li a').click(function () {
        $('div.colB ul.featuredJobsList:visible').hide();
        $('div.colB ul li.on').removeClass('on');
        if ($(this).attr('id')) {
            var activeTab = $(this).attr('id');
            $(this).parent().addClass('on');
            var active_li = activeTab.replace('FeaturedJobs_', 'FeaturedJobsList_');
            var active_title = activeTab + '_title';
            $('ul#' + active_li).show();
            hideSectorList();
            $('div.colB div.FeaturedJobTitle span.cnr:visible').addClass('hideall');
            $('span.' + active_title).removeClass('hideall');
        }
        return false;
    });
}

function serpHover() {
    $(".tabularResults tbody tr").hover(function () {
        $(this).addClass('activeRowClick');
    }, function () {
        $(this).removeClass('activeRowClick');
    });

    //job search
    $("#JobSearchResults tbody tr").click(function () {
        var sTheLink = '/' + $(this).find(".JobSearch_job_title a").attr('href');
        window.location = sTheLink;
    });

    //staff search open CV window
    $('#Olr_SearchResults td.olr_jt').click(function () {
        window.open('/' + $(this).children('a').attr('href'), 'cand_cv', 'scrollbars=yes,width=700, height=600,resizable=yes');
        return false;
    });
}

function contactUsSelectedCountry() {
    //office selector: add class to the country of its city.
    $('div.colRight ul.pageMenu li ul.office li').each(function () {
        $("div.colRight ul.pageMenu li ul.office li.sel").parent().parent().addClass('selCountry');
    });
}
function swap(){
	$('div#jobDetailsContent a.showJobDetailsLink').click(function(){
			$('#JobDetails_Details_Container').slideToggle();
			$(this).hide();
			$('div#jobDetailsContent a.hideJobDetailsLink').show();
	});
	$('div#jobDetailsContent a.hideJobDetailsLink').click(function(){
			$('#JobDetails_Details_Container').slideToggle();
			$('div#jobDetailsContent a.hideJobDetailsLink').hide();
			$('div#jobDetailsContent a.showJobDetailsLink').show();

	});
	}

