


if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}


var invalidURIchars = '$&+,/:;=?@\\#{}|^~[]` "<>';

function needURLEncode(ss) {

  for (var i= 0; i<ss.length; i++) {
    if (invalidURIchars.indexOf(ss.charAt(i))>-1)
      return true;
  }
  return false;
}


var lastShownHover = null;

function showHover(hoverId)
{
    //hide the previous hover
    if (lastShownHover != null)
    {
        lastShownHover.style.display ="none";
    }
    var currentHover = document.getElementById(hoverId);

    currentHover.style.display ="block";
    lastShownHover = currentHover;
}

function gotoUrl(targeturl) {
    window.location=targeturl;
    return false;
}
function submitSEOSearchForm() {
        //var seo_list_url
        //var qlcRegex
        //var qlcRegexName
        // var searchFormUrlPostfix
       // var predefinedRegexes = {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"};
    var searches = [];
    var keyword =  document.getElementById("searchKeyword").value;


    //there is keyword, sort by relevant
    var defaultPostfix =searchFormUrlPostfixRelevantSort;
    if (keyword != skillDefault && keyword != '') {
        keyword = keyword.replace("[\\-/]", " ");
        if (needURLEncode(keyword))
            keyword = encodeURIComponent(keyword);

        searches.push("q");
    } else // no keywords, sort by date
        defaultPostfix = searchFormUrlPostfix;

    var searchLocationEle = document.getElementById("searchLocation");
    if (searchLocationEle != null && searchLocationEle != 'undefined') {
        var location = searchLocationEle.value;

        if (location != locationDefault && location != '') {
            location = location.replace("[\\-/]", " ");
            if (needURLEncode(location))
            location = encodeURIComponent(location);
            searches.push("l");
        }
    }
    var searchSourceEle = document.getElementById("searchSources");
    if (searchSourceEle != null && searchSourceEle != 'undefined') {
        var sources = searchSourceEle.value;
        if (sources != 'All' && sources != '')
            searches.push("i");
    }

    var category = document.getElementById("searchCategory").value;
    if (needURLEncode(category))
    category = encodeURIComponent(category);
    if (category != 'All' && category != '')
        searches.push("c");


    if (searches.length == 0) {
        window.location= (seo_list_url + alljobdisplayVar + "-" + defaultPostfix + alljobtagVar).replace(/\s+/g, '-') ;

        return false;
    }

     var regexName = null;
     var regexValue = null;
     for(var key in predefinedRegexes){
         if (key == 'undefined') continue;

          if (key.toString().length == searches.length) {
              var kIndex = 0;
              for (kIndex = 0; kIndex < key.toString().length; kIndex++) {
                  var aChar = key.toString().charAt(kIndex);
                  if (searches.indexOf("" + aChar, 0) == -1)
                      break;
              }
              if (kIndex == key.toString().length) {
                  regexName = key;
                  regexValue = predefinedRegexes[regexName];
                  break;
              }

          }
      }
      if (regexName == null || regexValue == null) {
          return submitSearchForm();
      }

        var returnedUrl = seo_list_url;
        var wildCast = "(.*)";
        var wildCastLength = wildCast.length;
        var matchedIndex = -1;
        var index = 0;

        // we can replace all "(.*)" with (%s) and do a format instead of the following code
        do {
             var newMachedIndex = regexValue.indexOf(wildCast , matchedIndex + 1);
             if (newMachedIndex > -1) {
                  returnedUrl = returnedUrl + regexValue.substring(matchedIndex + (matchedIndex == -1 ? 1 : wildCastLength ), newMachedIndex);
                  var searchVal = null;
                  if (regexName.charAt(index) == 'q')
                      searchVal =keyword;
                  else if (regexName.charAt(index) == 'l')
                      searchVal = location;
                  else if (regexName.charAt(index) == 'c')
                       searchVal = category;
                  else if (regexName.charAt(index) == 'i')
                      searchVal = sources;
                  returnedUrl = returnedUrl + searchVal;
                   index++;
             } else {
                 returnedUrl =returnedUrl + regexValue.toString().substring(matchedIndex +  wildCastLength, regexValue.toString().length);
             }
             matchedIndex = newMachedIndex;
        }
        while(matchedIndex != -1);

        window.location =  (returnedUrl + "-" + defaultPostfix + regexName).toString().replace(/\s+/g, '-').replace(/%20/g, "+") ;
        //window.location = returnedUrl;
        return false;
}


function submitSearchForm()
{
    var keyword = document.getElementById("searchKeyword").value;
    if (keyword == skillDefault)
        keyword='';
    else if (needURLEncode(keyword))
        keyword = encodeURIComponent(keyword);


    var location = document.getElementById("searchLocation").value;
    if (location == locationDefault)
        location = '';
    else if(needURLEncode(location))
	location = encodeURIComponent(location);

    var category = document.getElementById("searchCategory").value;
    if(needURLEncode(category))
	category = encodeURIComponent(category);

    var timeVal = getParamVal("t-");
    var pageSize = getParamVal("s-");
    if (pageSize == null)
        pageSize = 12; //default
    var newLocation = "/list/p-0/q-" + keyword + "/l-" + location  + "/s-"+pageSize;
    if (category != "All") {
        newLocation = newLocation +  "/c-" + category;
    }
    if (timeVal != null){
        newLocation = newLocation + "/t-" + timeVal;
    }

    window.location = newLocation + "/x" + getParamVal("x");
    return false;
}


function jobMouseOver(e, jobid) {
    var top = 0, left = 0;
    var myTarget = e;

    while (myTarget != document.body && myTarget != null) {
        top += myTarget.offsetTop;
        left += myTarget.offsetLeft;
        myTarget = myTarget.offsetParent;
    }

    left = left + 15;
    var detailDiv = document.getElementById("detailDiv");

    detailDiv.style.left = left + "px";
    detailDiv.style.top = top + "px";
    detailDiv.style.visibility ="visible";

    var jobDomEle = document.getElementById('fullDesc' + jobid);
    var jobDetailDomEle = document.getElementById('jobDetailContent');
    jobDetailDomEle.innerHTML = jobDomEle.innerHTML;

    var sourcelinkDomEle = document.getElementById('sourcelink' + jobid);
    var applyNowLink = document.getElementById("applyNowLink");
    var fulldetailLink = document.getElementById("fullDetailLink");
    applyNowLink.href = sourcelinkDomEle.href;
    applyNowLink.target ="_blank";
    fulldetailLink.href="/job_" + jobid +".html";
}
function jobMouseOut() {
    var optionPane = document.getElementById("detailDiv");
    optionPane.style.visibility ="hidden";
}

function toggleSavedJobs(anAnchor, linkValue)
{
    if (anAnchor.innerHTML == saveVar)
    {
        anAnchor.innerHTML = removeVar;
        addSavedJob(linkValue);
    }
    else
    {
        anAnchor.innerHTML = saveVar;
        removeSavedJob(linkValue);

        //ok, check if we are in the basket
        var viewMode = gup("view");
        if (viewMode != null && viewMode == "basket")
        {
            var savedJobs = readCookie("savedJobs");
            if (savedJobs != null)
            {
                //check if we still have any save
                gotoSavedJobsUrl();
            }
            else
            {
                gotoAllJobsUrl();
            }
        }


    }

}

function toggleRefinement(h3ID)
{
    var h3Element = document.getElementById(h3ID);
    if (h3Element != null)
    {
        if (h3Element.className != "closed")
            h3Element.className ="closed";
        else
            h3Element.className="";
    }
}

function toggleVisibility(divID)
{

    var divElement = document.getElementById(divID);
    if (divElement.style.display != "block")
        divElement.style.display ="block";
    else
        divElement.style.display="none";
}



function sortByRelevance()
{
    var currentLoc = "" + window.location;
    var newLoc = rewriteURL("d-", "no", false, currentLoc);
    window.location = newLoc;
}

function sortByDate()
{
    var currentLoc = "" + window.location;
    var newLoc = rewriteURL("d-", "yes", false, currentLoc);
    window.location = newLoc;

}
function gotoBasketJobsUrl(newurl) {
    var currentSavedJobs = readCookie("savedJobs");
    if (currentSavedJobs != null)
    {
        window.location = newurl;
    }
    else
    {
        jAlert(nojobMsgVar, basketTitleVar);

    }
}
function gotoSavedJobsUrl()
{
    var currentSavedJobs = readCookie("savedJobs");
    if (currentSavedJobs != null)
    {
        var currentLoc = "" + window.location;
        var newLoc = rewriteURL("v-", "basket", true, currentLoc);
        window.location = newLoc;
    }
    else
    {
        jAlert(nojobMsgVar, basketTitleVar);

    }
}

function getParamVal(param)
{
    var currentLocation = "" + window.location;
    var tokens = currentLocation.split("/");
    var newLocation =tokens[0];
    var hasToken = false;
    for (var i = 1; i < tokens.length; i++)
    {
        var index = tokens[i].indexOf(param);
        if (index  == 0)
        {
            return tokens[i].substring(param.length);
        }

    }
    return null;
}

function gotoAllJobsUrl()
{
    var currentLoc = "" + window.location;
    var newLoc = rewriteURL("v-", "default", true, currentLoc);
    window.location = newLoc;

}

function switchViewStyle(newStyle) {
    var currentLoc = "" + window.location;
    var newLoc = rewriteURL("x-", newStyle, false, currentLoc);
    window.location = newLoc;
    // Store the cookie for viewStyle
    if (newStyle == "classic")
    {
        createCookie("x", "classic", 365);
    }
    else
    {
        createCookie("x", "newspaper", 365);
    }

}

function rewriteURL(param, newVal, restartPage, currentLocation)
{
    var tokens = currentLocation.split("/");
    var newLocation =tokens[0];
    var hasToken = false;
    for (var i = 1; i < tokens.length; i++)
    {
        if (tokens[i].indexOf("p-") == 0)
        {
            if (restartPage)
                newLocation = newLocation + "/p-0";
            else
                newLocation = newLocation + "/" + tokens[i];
        }
        else if (tokens[i].indexOf(param) == 0)
        {
            newLocation = newLocation + "/" +  param  + newVal;
            hasToken = true;
        }
        else
        {
            newLocation = newLocation + "/" + tokens[i];
        }
    }
    if (!hasToken)
        newLocation = newLocation + "/" +  param  + newVal;

    return  newLocation;

}



function gup( name )
{
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}



function addSavedJob(jobId)
{
    var savedJobs = readCookie("savedJobs");
    if (savedJobs == null)
        savedJobs = "" + jobId;
    else
        savedJobs = unescape(savedJobs) + " " + jobId;

    createCookie("savedJobs", escape(savedJobs), 1);
}

function removeSavedJob(jobId)
{
    var savedJobs = readCookie("savedJobs");
    if (savedJobs == null)
        return;

    savedJobs = unescape(savedJobs).replace(jobId, "");
    savedJobs = savedJobs.replace("  ", " ");
    createCookie("savedJobs", escape(savedJobs), 1);
}



function clearSavedJobs()
{
    eraseCookie("savedJobs");
}

function createCookie(name,value,days)
{
    if (days)
    {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toUTCString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name)
{
    var nameEQ = name + "=";
    var ca = (""+document.cookie).split(';');
    for(var i=0;i < ca.length;i++)
    {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0)
        {
            var cookieVal =  c.substring(nameEQ.length,c.length);
            //ok, ignore empty strings
            if (unescape(cookieVal).replace(/\s/g,"") == "")
                return null;
            else
                return cookieVal;

        }
    }
    return null;
}

function eraseCookie(name)
{
    createCookie(name,"",-1);
}


////////////////////////////////////JQUERY///////////////////////////////////////////////


function applyWaterMark(element, defaultval) {

       if (element.val() == "") {
           element.addClass("watermarkOn").val(defaultval);
       }
       element.focus(function() {

        $(this).filter(function() {

            // We only want this to apply if there's not
            // something actually entered
            return $(this).val() == "" || $(this).val() == defaultval

        }).removeClass("watermarkOn").val("");

    });

    // Define what happens when the textbox loses focus
    // Add the watermark class and default text
    element.blur(function() {

        $(this).filter(function() {

            // We only want this to apply if there's not
            // something actually entered
            return $(this).val() == ""

        }).addClass("watermarkOn").val(defaultval);

    });
}

function getTop(adPanel) {

    var top = 0;
    var myTarget = adPanel;
       while (myTarget != document.body) {
        top += myTarget.offsetTop;
        myTarget = myTarget.offsetParent;
    }
    return top;
}


function padAds2() {
    var ad1 = document.getElementById("adDiv0");
    var ad2 =  document.getElementById("adDiv1");
    var ad3 = document.getElementById("adDiv2");
    var ad4 =  document.getElementById("adDiv3");
    var top1 = 0;
    if (ad1 != null && ad1 != undefined)
    top1 = getTop(ad1);

    var top2 =0;
    if (ad2 != null && ad2 != undefined)
    top2 = getTop(ad2);

    var top3 = 0;
    if (ad3 != null && ad3 != undefined)
    top3 = getTop(ad3);

    var top4 = 0;
    if (ad4 != null && ad4 != undefined)
    top4 = getTop(ad4);

    var top = Math.max(top1, top2, top3, top4);

    //do the padding
    if (ad1 != null && ad1 != undefined)
    ad1.innerHTML = '<div style="vertical-align : middle;border:1px;border-color:#777777;height:' + (top - top1) + 'px; ' + '">Search with jBasket, the best search engine</div>';

    if (ad2 != null && ad2 != undefined)
    ad2.innerHTML = '<div style="vertical-align : middle;border:1px;border-color:#777777;height:' + (top - top2) + 'px; ' + '">Search with jBasket, the best search engine</div>';

    if (ad3 != null && ad3 != undefined)
    ad3.innerHTML = '<div style="vertical-align : middle;border:1px;border-color:#777777;height:' + (top - top3) + 'px; ' + '">Search with jBasket, the best search engine</div>';

    if (ad4 != null && ad4 != undefined)
    ad4.innerHTML = '<div style="vertical-align : middle;border:1px;border-color:#777777;height:' + (top - top4) + 'px; ' + '">Search with jBasket, the best search engine</div>';

}


