﻿var strURLPrefix = '';

function searchJobs(objSearchID, strLocationID, strNewLocationName, strNewSearchText) {

    var strQuerystring;
    var strTextInQuery;
    var strLocationInQuery;
    var strLocation;
    var strSearchText;
    var intJobType;
    var ddl_SalaryRange = document.getElementById(objSearchID + "_ddlSalaryRange");
    var hih_LocationID = document.getElementById(objSearchID + "_hihLocationID");
    var div_message = document.getElementById(objSearchID + "_divMessage");
   //var chk_includeJobBoards = document.getElementById(objSearchID + "_chkIncludeBoards");

    var ddl_SortBy;
    var sels = document.getElementsByTagName("select");

    for (i = 0; i < sels.length; i++) {
        if (sels[i].id && sels[i].id.indexOf('ddlSortOptions') > 0) {
            ddl_SortBy = sels[i];
        }
    }

    //Get search parameters
    if (strNewSearchText != null && strNewSearchText.length > 0) {
        strSearchText = strNewSearchText.replace(/%22/g, "\"");
    } else {
        strSearchText = document.getElementById(objSearchID + "_txtKeywords").value;
    }
    strSearchText = strSearchText.replace(/\bit\b/g, "IT");
    strSearchText = encodeURIComponent(trimAll(strSearchText).replace('#', ' ').replace('/', ' ').replace('*', ' ').replace('%', ' ').replace('&', ' ').replace(/\.+/g, '.'));
    
    if (document.getElementById(objSearchID + "_chkPartTime").checked) {
        intJobType = 2;
    } else {
        intJobType = 0;
    }
    
    if (strNewLocationName != null && strNewLocationName.length > 0) {
        strLocation = strNewLocationName;
    } else {
        strLocation = document.getElementById(objSearchID + "_txtLocation").value;
    }
    strLocation = encodeURIComponent(trimAll(strLocation).replace('#', ' ').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;
    
    var strSortBy = "";
    if (ddl_SortBy) {
        strSortBy = "&sort=" + ddl_SortBy.options[ddl_SortBy.selectedIndex].value;
    }

    //Build up the new querystring
    strQuerystring = strURLPrefix + "/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 ((strNewSearchText == null && isValidForPath(document.getElementById(objSearchID + "_txtKeywords").value)) || (strNewSearchText != null && strNewSearchText.length > 0 && isValidForPath(strNewSearchText.replace(/%22/g, "\"")))) {
            strQuerystring += strSearchText + ".aspx";
        } else {
            strTextInQuery = "&searchtext=" + strSearchText;
        }
    }

    //var strGoogleQuery = "&q=" + strSearchText + "%20jobs";

    strQuerystring += "?minsalary=" + intMinSalary + "&maxsalary=" + intMaxSalary + "&radius=" + intRadius + strTextInQuery + strLocationInQuery + strSortBy + "&jobtype=" + intJobType;  //+ strGoogleQuery;
    
    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);
    if (strSearchText.length == 0 && strLocation.length == 0) {
        div_message.innerHTML = 'Please enter Keywords and/or Location';
        div_message.style.display = 'block';
    } else {
        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;
	
	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 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 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, strDefault) {
//    var txt_keywords = document.getElementById(strID);
//    if (txt_keywords.value == strDefault) {
//        txt_keywords.value = '';
//    }
}

function checkDefaultBlur(strID, strDefault) {
//    var txt_keywords = document.getElementById(strID);
//    if (txt_keywords.value == '') {
//        txt_keywords.value = strDefault;
//    }
}



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 toggleBigSearch(size) {

    if (size) {

        if (size == "big") {
            document.getElementById('RadiusBox').style.display = "block";
            document.getElementById('SalaryBox').style.display = "block";
            document.getElementById('OptionsBig').style.display = "none";
            document.getElementById('OptionsSmall').style.display = "block";
            Set_Cookie("atb-searchctl", "big_un", 365, "/");
        } else {
            document.getElementById('RadiusBox').style.display = "none";
            document.getElementById('SalaryBox').style.display = "none";
            document.getElementById('OptionsBig').style.display = "block";
            document.getElementById('OptionsSmall').style.display = "none";
            Set_Cookie("atb-searchctl", "little_un", 365, "/");
        }
    } else {
    //If not specified, do it based on cookie, or make it small if no cookie.
        var strCookie = Get_Cookie("atb-searchctl");

        if (strCookie) {
            if (strCookie == "big_un") {
                toggleBigSearch("big");
            } else {
                toggleBigSearch("small");
            }
        } else {
            toggleBigSearch("big");
        }
    }    
    
}

function viewDetails(intJobID, strURL, blnHighlighted, blnFeatured) {

    Set_Cookie("RedirectedToJob" + intJobID, "false", 1, "/");

    if (blnFeatured) {
        window.open("/viewjobdetails.aspx?jobid=" + intJobID + "&url=" + encodeURIComponent(strURL) + "&returnto=" + encodeURIComponent(location.href) + "&highlighted=" + blnHighlighted + "&featured=" + blnFeatured);
    } else {
        window.open("/viewjobdetails.aspx?jobid=" + intJobID + "&url=" + encodeURIComponent(strURL) + "&returnto=" + encodeURIComponent(location.href) + "&highlighted=" + blnHighlighted);
    }

}

function Set_Cookie(name, value, expires, path, domain, secure) {
    // set time, it's in milliseconds
    var today = new Date();
    today.setTime(today.getTime());

    /*
    if the expires variable is set, make the correct 
    expires time, the current script below will set 
    it for x number of days, to make it for hours, 
    delete * 24, for minutes, delete * 60 * 24
    */
    if (expires) {
        expires = expires * 1000 * 60 * 60 * 24;
    }
    var expires_date = new Date(today.getTime() + (expires));

    document.cookie = name + "=" + escape(value) +
((expires) ? ";expires=" + expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : "");
}


// this function gets the cookie, if it exists
function Get_Cookie(name) {

    var start = document.cookie.indexOf(name + "=");
    var len = start + name.length + 1;
    if ((!start) &&
(name != document.cookie.substring(0, name.length))) {
        return null;
    }
    if (start == -1) return null;
    var end = document.cookie.indexOf(";", len);
    if (end == -1) end = document.cookie.length;
    return unescape(document.cookie.substring(len, end));
}


// this deletes the cookie when called
function Delete_Cookie(name, path, domain) {
    if (Get_Cookie(name)) document.cookie = name + "=" +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function visitAd(intID)
{
    window.open('/Visit.aspx?id=' + String(intID));
}

function toggleKeywordsList(link) {
    var list = document.getElementById('SuggestedKeywordsList');
    var popup = document.getElementById('SuggestedKeywordsPopupList');
    var popuplink = document.getElementById('SuggestedMoreLink');

    if (link) {
        if (list) {
            if (list.style.display == "none") {
                list.style.display = "block";
                link.innerHTML = " - ";
                Set_Cookie('atb-keywordslist', 'big', 365, "/");
            }
            else {
                list.style.display = "none";
                popup.style.display = "none";
                popuplink.style.display = "inline";
                link.innerHTML = " + ";
                Set_Cookie('atb-keywordslist', 'small', 365, "/");
            }
        }
    }
    else {
        strCookie = Get_Cookie('atb-keywordslist');
        
        link = document.getElementById('SuggestedKeywordsControlLink');
        
        if (strCookie && strCookie == 'small') {
            list.style.display = "none";
            link.innerHTML = " + ";
            Set_Cookie('atb-keywordslist', 'small', 365, "/");
        }
        else {
            list.style.display = "block";
            link.innerHTML = " - ";
            Set_Cookie('atb-keywordslist', 'big', 365, "/");
        }
    }
}
