﻿// JScript File

function PerformRecentSearch(intLocationID, strLocation, intRadius, 
                             strSearchText, intMinSalary, intMaxSalary) {

    var strQuerystring;
    var strTextInQuery;
    var strLocationInQuery;
    
    if(strSearchText == '[no keywords]')
    {
        strSearchText = '';
    }
    
    //Get search parameters
    strLocation = encodeURIComponent(trimAll(strLocation).replace('/', ' ').replace('*', ' ').replace('%', ' '));

    //Build up the new querystring
    strQuerystring = strBrand + "Jobs/";

    //Sort out salary and location fields
    if(intMinSalary < 0)
    {
        intMinSalary = 0;
    }
    
    if(intMaxSalary < 0)
    {
        intMaxSalary = 0;
    }
    
    if(intLocationID < 0)
    {
        intLocationID = 0;
    }
    
    if(intRadius < 0)
    {
        intRadius = 0;
    }
    
    if (isValidForPath(strSearchText)) {
        strQuerystring += strSearchText + ".aspx";
        strTextInQuery = "";
    }
    else
    {
        strTextInQuery = "&searchtext=" + strSearchText;
    }
    
    strLocationInQuery = "&location=" + strLocation;
    
    strQuerystring += "?minsalary=" + intMinSalary + "&maxsalary=" + intMaxSalary + "&radius=" + intRadius + strTextInQuery + strLocationInQuery;
    strQuerystring += '&locationid=' + intLocationID;

    window.location = strQuerystring;

}

function searchJobs(objSearchID, strLocationID, strNewLocationName, strNewSearchText) {

    var strQuerystring;
    var strTextInQuery;
    var strLocationInQuery;
    var strLocation;
    var strSearchText;
    var strTab = '';
    var ddl_SalaryRange = document.getElementById(objSearchID + "_ddlSalaryRange");
    var hih_LocationID = document.getElementById(objSearchID + "_hihLocationID");
   //var chk_includeJobBoards = document.getElementById(objSearchID + "_chkIncludeBoards");
    var hih_Tab = document.getElementById(objSearchID + "_hihTab");
    
    //Get search parameters
    if (strNewSearchText != null && strNewSearchText.length > 0) {
        strSearchText = strNewSearchText;
    } else {
        strSearchText = document.getElementById(objSearchID + "_txtSearchText").value;
    }

    try {
        if (strSearchText == eval('strDef' + objSearchID + '_txtSearchText')) {
            strSearchText = '';
        }
    } catch(err) {}

    strSearchText = encodeURIComponent(trimAll(strSearchText).replace('/', ' ').replace('*', ' ').replace('%', ' ').replace('&', ' ').replace(/\.+/g, '.'));
    
    if (strNewLocationName != null && strNewLocationName.length > 0) {
        strLocation = strNewLocationName;
    } else {
        strLocation = document.getElementById(objSearchID + "_txtLocation").value;
    }

    try {
        if (strLocation == eval('strDef' + objSearchID + '_txtLocation')) {
            strLocation = '';
        }
    } catch(err) {}

    strLocation = encodeURIComponent(trimAll(strLocation).replace('/', ' ').replace('*', ' ').replace('%', ' ').replace('&', ' ').replace(/\.+/g, '.'));

    var intMinSalary = ddl_SalaryRange.options[ddl_SalaryRange.selectedIndex].value.split(":")[0];
    var intMaxSalary = ddl_SalaryRange.options[ddl_SalaryRange.selectedIndex].value.split(":")[1];
    
    var intRadius = document.getElementById(objSearchID + "_wctlRadiusSlider_ctl00_hitTextValue").value;
    

    //Build up the new querystring
    strQuerystring = strBrand + "Jobs/";
    
    strLocationInQuery = "";
    if (strLocation.length > 0) {
        if (isValidForPath(document.getElementById(objSearchID + "_txtLocation").value)) {
            strQuerystring += strLocation + "/";
        } else {
            strLocationInQuery = "&location=" + strLocation;
        }
    }
    
    strTextInQuery = "";
    if (strSearchText.length > 0) {
        if (isValidForPath(document.getElementById(objSearchID + "_txtSearchText").value)) {
            strQuerystring += strSearchText + ".aspx";
        } else {
            strTextInQuery = "&searchtext=" + strSearchText;
        }
    }
    
    if (hih_Tab.value > 0) {
        strTab = "&tab=" + hih_Tab.value;
    }
    
    strQuerystring += "?minsalary=" + intMinSalary + "&maxsalary=" + intMaxSalary + "&radius=" + intRadius + strTextInQuery + strLocationInQuery + strTab;
    
    if (strLocationID != null && strLocationID.length > 0) {
        hih_LocationID.value = strLocationID;
    } else {
        strLocationID = hih_LocationID.value;
        //strLocationID = 0;
    } //TODO: set/get hih in wctlSearch1
    strQuerystring += '&locationid=' + strLocationID;
    //alert(strQuerystring);
    window.location = strQuerystring;
}

function changeSearchText(objSearchID, strNewText) {
    searchJobs(objSearchID, null, null, strNewText);
}

function isValidForPath(strString) {
    var intChar;
    for (i = 0; i < strString.length; i++) {
        intChar = strString.charCodeAt(i);
        for (j = 0; j < arrInvalid.length; j++) {
            if (arrInvalid[j] == intChar) {
                return false;
            }
        }
    }
    return true;
}

function submitenter(myfield,e) {

	var keycode;
	var btn_Search;
	var strSearchControlID = myfield.parentNode.id;
	
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;

	if (keycode == 13) {
		searchJobs(strSearchControlID);
		return false;
	} else {
		return true;
	}
}

function toggleOtherMatches(strContainerID) {
    var div_OtherMatchLinks;
    div_OtherMatchLinks = document.getElementById(strContainerID.replace('_spanOtherMatches', '_divOtherMatchLinks'));
    if (div_OtherMatchLinks.className == 'otherMatchLinks hidden') {
        div_OtherMatchLinks.className = 'otherMatchLinks';
    } else {
        div_OtherMatchLinks.className = 'otherMatchLinks hidden';
    }
}

function resetLocationID(objSearchID) {
    var hih_LocationID = document.getElementById(objSearchID + "_hihLocationID");
    hih_LocationID.value = 0;
}

function viewDetails(intJobID, strURL, blnHighlighted, blnRedirect) {

    //var strFullURL = strBrand + "viewjobdetails.aspx?jobid=" + intJobID + "&url=" + encodeURIComponent(strURL) + "&highlighted=" + blnHighlighted;

    //Change to force clicks through jobsno1.co.uk - revert to line above for old behaviour
    var strFullURL = "http://www.jobsno1.co.uk/viewjobdetails.aspx?jobid=" + intJobID + "&url=" + encodeURIComponent(strURL) + "&highlighted=" + blnHighlighted;
    
    if (blnRedirect == 'True') {
        top.location = strFullURL;
    } else {
        window.open(strFullURL);
    }

}

function trimAll(sString) {
    while (sString.substring(0,1) == ' ')
    {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ')
    {
        sString = sString.substring(0,sString.length-1);
    } 
    return sString;
}

function checkDefaultFocus(strID) {
    var txt_keywords = document.getElementById(strID);
    if (txt_keywords.value == eval('strDef' + strID)) {
        txt_keywords.value = '';
    }
}

function checkDefaultBlur(strID) {
    var txt_keywords = document.getElementById(strID);
    if (txt_keywords.value == '') {
        txt_keywords.value =  eval('strDef' + strID);
    }
}

function selectLocation(intLocationID, strLocationText, strNamingContainer, strDescription) {

    var txt_location = document.getElementById(strNamingContainer + '_txtLocation');
    var hih_locationID = document.getElementById(strNamingContainer + '_hihLocationID');
    var lbl_matchedLocation = document.getElementById(strNamingContainer + '_lblMatchedLocation');
        
    txt_location.value = strLocationText;
    hih_locationID.value = intLocationID;
    lbl_matchedLocation.innerText = 'Selected: ' + strDescription;
    
    lbl_matchedLocation.className = 'matchedLocationGood';
    
}

function MoveSliderFromText(ctrl) {
    var txt_Value;
    var ctl_Knob;
    var ctl_Container;
    var intMargin;
    
    ctl_Knob = document.getElementById(ctrl.id.replace('_hitTextValue','') + '_ctlKnob');
    txt_Value = document.getElementById(ctrl.id.replace('_hitTextValue','') + '_hitTextValue');
    ctl_Container = document.getElementById(ctrl.id.replace('_hitTextValue','') + '_divContainer');
    
    var intValue = txt_Value.value;
    if (intValue < minValue) {
        intValue = minValue;
        txt_Value.value = minValue;
    } else if (intValue > maxValue) {
        intValue = maxValue;
        txt_Value.value = maxValue;
    }
    
    intMargin = Math.round(((intValue * ctl_Container.offsetWidth) / maxValue));
    
    if (intMargin < ctl_Container.offsetWidth) {
        if (intMargin > 1) {
            ctl_Knob.style.marginLeft = intMargin - 7 + 'px';
        } else {
            ctl_Knob.style.marginLeft = '-7px';
        }
    } else {
        ctl_Knob.style.marginLeft = ctl_Container.offsetWidth - 7 + 'px';
    }
}

function reportLocation(intJobID, strLocationText)
{
    window.open("/ContactUsWebUser.aspx?text=" + encodeURIComponent('Inaccurate location report\nJobID:' + intJobID + '\nLocation Text:' + strLocationText), 'LinkReport', 'width=800,height=600,toolbar=no,location=no,menubar=no,status=no,scrollbars=yes');
}

function referto(strWhere)
{
    if (strWhere == 'team') {
        window.open('http://www.jobsatteam.com/');
    } else if (strWhere == 'jt') {
        window.open('http://www.jtrecruit.co.uk/');
    } else if (strWhere == 'dragonfly') {
        window.open('http://www.dragonflyrec.co.uk/');
    }
}

function browseCounty(strBrand, strName, strID)
{
    window.location = strBrand + 'Jobs/' + strName + '/?browsecountyid=' + strID;
}

function browseTown(strBrand, strName, strID)
{
    window.location = strBrand + 'Jobs/' + strName + '/?browsetownid=' + strID;
}


var c_xhrFeatured;
var span_FeaturedJobsAjaxContainer;
function LoadFeaturedJobs(strQueryText, intCountyID, intX, intY, intRadius, intOrganicResults, intAdSize, intTownID, intMinSalary, strMicrositeID)
{

    span_FeaturedJobsAjaxContainer = document.getElementById('spanFeaturedJobsAjaxContainer');
    
    if (span_FeaturedJobsAjaxContainer != null) {
    
        c_xhrFeatured = CreateXmlHttp();

        var strURL;
        var strMicrositeParam;
        
        if (strMicrositeID != undefined) {
            strMicrositeParam = '&microsite=' + strMicrositeID;
        }
        
        strURL = strBrand + 'AjaxFeaturedJobs.aspx?searchtext=' + strQueryText + '&countyid=' + intCountyID + '&x=' + intX + '&y=' + intY + '&radius=' + intRadius + '&organicresults=' + intOrganicResults + '&adsize=' + intAdSize + '&town=' + intTownID + '&minsalary=' + intMinSalary + strMicrositeParam;
        
        MakeAjaxCall(c_xhrFeatured, strURL, FeaturedCallback);
        
    }
   
}

function FeaturedCallback() {
    if(c_xhrFeatured.readyState==4)
    {
        span_FeaturedJobsAjaxContainer.innerHTML = c_xhrFeatured.responseText;
    }
}


function BrowseViewSectors() {
    document.getElementById(strSectorsID).style.display = 'block';
    document.getElementById('BrowseTabSectors').className = 'BrowseTab BrowseTabSelected';
    document.getElementById(strRolesID).style.display = 'none';
    document.getElementById('BrowseTabRoles').className = 'BrowseTab';
    document.getElementById(strCountiesID).style.display = 'none';
    document.getElementById('BrowseTabCounties').className = 'BrowseTab';
}


function BrowseViewRoles() {
    document.getElementById(strSectorsID).style.display = 'none';
    document.getElementById('BrowseTabSectors').className = 'BrowseTab';
    document.getElementById(strRolesID).style.display = 'block';
    document.getElementById('BrowseTabRoles').className = 'BrowseTab BrowseTabSelected';
    document.getElementById(strCountiesID).style.display = 'none';
    document.getElementById('BrowseTabCounties').className = 'BrowseTab';
}


function BrowseViewCounties() {
    document.getElementById(strSectorsID).style.display = 'none';
    document.getElementById('BrowseTabSectors').className = 'BrowseTab';
    document.getElementById(strRolesID).style.display = 'none';
    document.getElementById('BrowseTabRoles').className = 'BrowseTab';
    document.getElementById(strCountiesID).style.display = 'block';
    document.getElementById('BrowseTabCounties').className = 'BrowseTab BrowseTabSelected';
}
