var searchRequestObj = null;
var search_max_results = 30;
var indexUrl = 'index.php';
$(document).ready(function(){
getIndexUrl();
});
function getIndexUrl(){
indexUrl = $('#index_page').size() ? $('#index_page').val() : indexUrl;
}
function getSessionID(pbSIDOnly){
var ccsid;
if( typeof(pbSIDOnly) != 'undefined' && pbSIDOnly ){
ccsid = typeof(SID) != 'undefined' && SID.length > 0 ? SID.split('=')[1] : '';
}
else {
ccsid = typeof(SID) != 'undefined' && SID.length > 0 ? '?' + SID + '&' : '?';
}
return ccsid;
}
function searchOccupations(pEvt, poInput){
var disregard_keys = [13, 27, 33, 34, 37, 38, 39, 40];
var keyPressed = pEvt.keyCode;
if( in_array(keyPressed, disregard_keys) ){
return false;
}
var $input = $(poInput);
var $results = $('#searchResults');
$input.addClass("ac_loading").addClass("user_input");
$results.hide().empty();
if( $.browser.safari ){
var width = $('#occupationInput').parent().width() - 3; //1px border on each side (both inputs are the same width)
$results.width(width);
$results.css('top', '-2px');
}
var width = $('#occupationInput').get(0).offsetWidth - 2; //1px border on each side (both inputs are the same width)
$results.width(width);
setTimeout(function(){
var term = $.trim($input.val());
if( term != '' ){
term = term.toLowerCase();
// if we're in the middle of a search, kill it (but make sure IE doesn't tweak out)
if( searchRequestObj != null ){
try {
searchRequestObj.aborted = 1; // set a variable on the object to know if we aborted or not (so we don't show the oops message on every abort as they type)
searchRequestObj.abort();
} catch(err){}
}
searchRequestObj = $.ajax({
url: indexUrl,
data: {
search: 1,
q: term,
limit: search_max_results
},
success: function(response, status, xhr){
var aborted = typeof(xhr) != 'undefined' && xhr.aborted;
if( response != '' ){
var parsed = parse(response);
$input.removeClass("ac_loading");
fillList(term, parsed, $results);
}
else if( !aborted ){ // we didn't find anything (and didn't abort the request)
var neutered_term = term.replace(/'/g, '').replace(/"/g, '').replace(//g, ">"); //prevent basic xss
var not_found = "
Oops...
We couldn't find any occupations that matched " + '"' + neutered_term + '". Please check the spelling and try again.
';
var $div = $("").addClass('oldOccupation').html( not_found );
$results.html( $div );
$input.removeClass("ac_loading");
}
if( !aborted ){
$results.show(0, function(){ triggerResize(); });
$results.scrollTop(0); //set the scroll position back to the top, just to be sure
if( $.browser.safari ){
var height = 0;
$results.children('div.oldOccupation').each(function(){
height += $(this).get(0).offsetHeight;
if( height >= 500 ){
return;
}
});
var new_height = height >= 500 ? 500 : height;
$results.height(new_height);
triggerResize();
}
}
}
});
}
else {
$input.removeClass("ac_loading");
$results.hide(0, function(){ triggerResize(); }).empty();
}
}, 250);
}
function parse(data){
var parsed = [];
var rows = data.split("\n");
for(var i = 0; i < rows.length; i++){
var row = $.trim(rows[i]);
if( row ){
row = row.split("~|~");
parsed[parsed.length] = {
data: row,
value: row[0]
};
}
}
return parsed;
}
function fillList(term, data, $results){
$results.empty();
var bNewOcc = $results.is('#newSearchResults');
var bAcOver = false;
for(var i = 0; i < search_max_results; i++){
if( !data[i] ){
continue;
}
var formatted = formatItem(data[i].data, term, i);
if( formatted === false ){
continue;
}
var $div = $("").addClass('oldOccupation').html(formatted);
// TODO: setup a check so that there is ALWAYS one selected on ENTER
$div.mouseover(function(){ $(this).addClass('ac_over').siblings('div.oldOccupation').removeClass('ac_over'); });
//$div.mouseout(function(){ $(this).removeClass('ac_over'); });
$div.click(function(){ selectOccupation(this, bNewOcc); });
if( !bAcOver ){
$div.addClass('ac_over');
bAcOver = true;
}
$results.append($div);
}
}
function formatItem(data, term, count){
term = term.split(' ').join('|');
//we want to bold search terms in every piece of data returned
var oRegex = new RegExp("(" + term + ")", "gi");
var strInput = '';
var strTitle = '';
var strAlternativeTitles = data[2] == '' ? '' : '
Alternative titles: ' + data[2].replace(oRegex, "$1") + '
';
var strDescription = '
' + data[3].replace(oRegex, "$1") + '
';
return '
' + strInput + strTitle + '
' + strAlternativeTitles + strDescription;
}
//Basic function that checks if the value is in the array, similar to php's in_array() (used by jquery.alphanumeric.js)
function in_array(pstrNeedle, paHaystack){
var i;
var len = paHaystack.length;
for(i = 0; i < len; i++){
if( pstrNeedle === paHaystack[i] ){
return true;
}
}
return false;
}
function selectOldOcc(poDiv){
$(poDiv).addClass('selected').siblings('div.oldOccupation').removeClass('selected');
var $input = $(poDiv).children('h3:first').children('input:first');
var $label = $input.siblings('label');
$('#occupationInput').val($label.text());
$('#searchResults').hide();
$('#selectedOccID').val($input.val());
$('#selectedOccName').val($label.text());
}
// setup all the valid search types
var aTypes = ['search', 'hotjob', 'topprogram', 'similar'];
var aSubTypes = ['change-zip', 'change-wage', 'change-radius'];
// merge them all into a single array
var aAllTypes = [];
$(aTypes).each(function(){
var curType = this.toString();
aAllTypes.push(curType);
aAllTypes.push(curType + '|viewsimilar');
$(aSubTypes).each(function(){
aAllTypes.push(curType + ':' + this);
aAllTypes.push(curType + '|viewsimilar:' + this);
});
});
function setSearchType(pstrType, pbRemoveSubType){
// validate the search type
var strType = in_array(pstrType, aAllTypes) ? pstrType : 'error'; // default to error if we don't know
// if we want to remove the sub type
if( typeof(pbRemoveSubType) != 'undefined' && pbRemoveSubType ){
if( strType.indexOf(':') != -1 ){
strType = strType.split(':')[0];
}
}
// set the search type value
$('#searchType').val(strType);
}
function getSearchType(){
return $('#searchType').size() ? $('#searchType').val().toLowerCase() : '';
}
function removeSearchSubType(){
var searchType = getSearchType();
setSearchType(searchType, true);
}
/*
function reselectOccupation(){
var $div = $('#searchResults').children('div.selected');
var $input = $div.children('h3:first').children('input:first');
var $label = $input.siblings('label');
$('#selectedOccID').val($input.val());
$('#selectedOccName').val($label.text());
}
*/
function selectOccupation(poDiv){
var $div = $(poDiv);
$div.addClass('selected').siblings('div.oldOccupation').removeClass('selected');
var $input = $div.children('h3:first').children('input:first');
var $label = $input.siblings('label');
var $search = $('#occupationInput');
var strOccTitle = $label.text();
var strOccID = $input.val();
var searchTerm = $search.is('.user_input') ? $search.val() : '';
$search.val(strOccTitle).removeClass("user_input");
$('#searchResults').hide();
// clear out the last fromOcc value for stats
setSearchType('search');
$('#fromOcc').val('');
// set the global version
$('#selectedOccID').val(strOccID);
$('#selectedOccName').val(strOccTitle);
nextStep(2, searchTerm); // record the search term
}
function navigate(poLI){
var $li = $(poLI);
// if it's not the currently active one, do something
if( !$li.is('.active') ){
var step = parseInt($li.attr('id').split('_')[1], 10);
var curStep = parseInt($li.siblings('li.active').attr('id').split('_')[1], 10);
if( $li.is('.disabled') ){
checkOcc();
}
else {
$li.addClass('active').siblings().removeClass('active');
if( step < curStep ){
previousStep(step);
}
else if( step > curStep ){
nextStep(step);
}
}
}
}
function openPopup(strLocation, strID, height, width, bNotResizable){
strID = strID ? strID : 'default'; //Default ID
height = height ? height : 500; //Default height
width = width ? width : 400; //Default width
bResizable = bNotResizable ? 0 : 1;
var newWin;
newWin = window.open(strLocation, strID, 'resizable=' + bResizable + ',scrollbars=auto,width=' + width + ',height=' + height + ',locationbar=no,toolbar=no');
newWin.focus();
}
$(function(){
$('label.over-apply').labelOver('over-apply');
});
function startOver(){
if( confirm('Are you sure you want to clear everything and start over?') ){
window.location.reload(); // so we don't clear everything for metrix learning people
}
}
function checkZIPInput(){
if( $('#zipInput').size() || $('#similarZipInput').size() ){
var $input = $('#similarZipInput').size() && $('#similarZipInput').is(':visible') ? $('#similarZipInput') : $('#zipInput')
var zip = $input.val();
var data = {task: 'verifyzipinput', zip: zip};
var SID = getSessionID(true);
if( SID.length > 0 ){
$.extend(data, {ccsid: SID});
}
var xhrZIP = $.ajax({
url: indexUrl,
async: false,
type: 'POST',
data: data
});
if( xhrZIP.responseText != 'VALID' ){
var message = 'Please enter a valid ';
switch(xhrZIP.responseText){
case 'county':
message += 'ZIP code in your county.';
break;
case 'state':
message += 'ZIP code in your state.';
break;
case 'US':
message += 'US ZIP code.';
break;
}
$input.select();
alert(message);
return false;
}
}
// made it here so it's good
return true;
}
function checkName(){
if($.trim($('#nameInput').val()) != ''){
return true;
}
else {
$('#nameInput').val('').blur();
alert('Please enter your name.');
return false;
}
}
function checkOcc(){
if( $.trim($('#selectedOccName').val()) == '' || $.trim($('#occupationInput').val()) != $.trim($('#selectedOccName').val()) ){
if( $('#selectedOccID').val() != '' ){
return true;
}
else {
$('#occupationInput').val('').blur();
alert('Please select a valid occupation before trying to move on to the next step.');
return false;
}
}
else {
return true;
}
}
function addIndeedPageHit(){
var name = $('#nameInput').val();
var userID = $('#idInput').val();
var occID = $('#selectedOccID').val(); //TODO: validate this...if they don't have one, go back to the correct page and make them pick one
var miles = $('#milesSelect').val();
var zip = $('#zipInput').size() ? $('#zipInput').val() : 0;
var wages = $('#wagesInput').val();
$.post(indexUrl, {indeedpagehit: 1, name: name, userid: userID, occid: occID, miles: miles, zip: zip, wages: wages});
return true;
}
function logout(){
if( confirm('Are you sure you want to log out?') ){
$.post('index.php', {logout: 1}, function(){
var url = window.location.href;
window.location.replace(url.replace(window.location.search, ''));
});
}
}