function returnObjById( id ) 
{ 
    if (document.getElementById) 
        var returnVar = document.getElementById(id); 
    else if (document.all) 
        var returnVar = document.all[id]; 
    else if (document.layers) 
        var returnVar = document.layers[id]; 
    return returnVar; 
}

function hilight(elemID, colorToSet) {
	var ver = navigator.appVersion;
	if (ver.indexOf("MSIE") == -1) {
	returnObjById(elemID).style.setProperty('color', colorToSet, null);
	} else {
		returnObjById(elemID).style.color = colorToSet;
	}
}

function dehilight(elemID) {
	var ver = navigator.appVersion;
	if (ver.indexOf("MSIE") == -1) {	
		returnObjById(elemID).style.removeProperty('color');
	} else {
		returnObjById(elemID).style.color = '';
	}
}

function onFocusValue(value, constValue) {
	return 'DEPRECATED! USE onFocusHandler';	
	if(value.replace(/\s+$/,"") == constValue) {
		return '';
	} else {
		return value;
	}
}

function onBlurValue(value, constValue) {
	return 'DEPRECATED! USE onBlurHandler';	
	if(value.replace(/\s+$/,"") == '') {
		return constValue;
	} else {
		return value;
	}
}

function onFocusHandler(id, constValue) {
	var input = $('#'+id);
	input.removeClass("hint");
	if(input.get(0).value.replace(/\s+$/,"") == constValue) {
		input.get(0).value= '';
	} 
}

function onBlurHandler(id, constValue) {
	var input = $('#'+id);
	if( input.get(0).value.replace(/\s+$/,"") == '' ){
		input.addClass("hint");		
		input.get(0).value = constValue;
	}
}



function focusPokus( ){
	$(' input[hint] ').each( function( key, el ){
		el.onfocus= function( ){
			this.className= ( this.className || '' ).replace( /\bhint\b/, '' );
			this.value= this.value.replace( this.getAttribute( 'hint' ), '' );
		}
		el.onblur= function( ){
			if( !this.value.trim() ){
				this.value= this.getAttribute( 'hint' );
				this.className= ( this.className || '' ) + ' hint';
			}
		}
		el.onblur();
	});
}


function formKeyUpHandler(event) {
	if(event.keyCode == 13) {
		return true;
	}
	return false;
}

function swapImage(destImage, element) {
	element.src = destImage;
}

function preloadImages(extra_image_url) {
    if (document.images)
    {      
      if (extra_image_url == null) {
		extra_image_url = new Array();  
	  }
	  
	  // set image url
      var common_image_url = new Array();
      common_image_url.push("/v3/gfx/header-logbox-button-login-on.png");
      common_image_url.push("/v3/gfx/header-logbox-button-signup-on.png");
      common_image_url.push("/v3/gfx/nav-2ndlev-jobs-on.png");
      common_image_url.push("/v3/gfx/nav-2ndlev-companies-on.png");
      common_image_url.push("/v3/gfx/nav-2ndlev-agencies-on.png");
      common_image_url.push("/v3/gfx/nav-2ndlev-recruiters-on.png");
	  common_image_url.push('/v3/gfx/searchbox-button-searchjobs-on.png');
      common_image_url.push('/v3/gfx/arrow_right_blue.gif');
      common_image_url.push('/v3/gfx/arrow_down_red.gif');
	  
      var image_url = new Array();
	  image_url = common_image_url.concat(extra_image_url);

       var i = 0;
       for(i=0; i<image_url.length; i++) {
	     if (image_url[i]) {
         preload_image_object = new Image();
         preload_image_object.src = image_url[i];
	       }
	   }
    }
}

function generateTopRunner2(place, texts, links) {
	
	if (document.images)
    {      
      // set image url
      image_url = new Array();
	  image_url[0] = "/v3/gfx/top_runner_bgnd.png";	  	  	
	  image_url[1] = "/v3/gfx/top_runner_left.png";
	  image_url[2] = "/v3/gfx/top_runner_right.png";	  
	  image_url[3] = "/v3/gfx/top_runner_spacer.png";	  


       var i = 0;
       for(i=0; i<=image_url.length; i++) {
	     preload_image_object = new Image();
         preload_image_object.src = image_url[i];
	   }
    }
	var top = '<td class="top_runner2_left"><!-- --></td><td class="top_runner2"><a href="/" title="Home">Home</a></td>';
	for(i = 0; i< texts.length; i++) {
		top = top + '<td class="top_runner2_spacer"><!-- --></td><td class="top_runner2"><a href="' +  links[i] + '" title="' + texts[i] + '">' + texts[i] + '</a></td>';   
	}
	top = top + '<td class="top_runner2_spacer"><!-- --></td>';
	$("#top_runner_image").replaceWith(top);
}
function generateTopRunner(place, texts, links) {
	 if (document.images)
    {      
      // set image url
      image_url = new Array();
	  image_url[0] = "/v3/gfx/top_runner_bgnd.png";	  	  	
	  image_url[1] = "/v3/gfx/top_runner_left.png";
	  image_url[2] = "/v3/gfx/top_runner_right.png";	  
	  image_url[3] = "/v3/gfx/top_runner_spacer.png";	  


       var i = 0;
       for(i=0; i<image_url.length; i++) {
	     preload_image_object = new Image();
         preload_image_object.src = image_url[i];
	   }
    }
	var top = '<table cellpadding="0" cellspacing="0"><tr><td class="top_runner2_left"><!-- --></td><td class="top_runner2"><a href="/" title="Home">Home</a></td>';
	//define pattern to replace (multiple)spaces to single &nbsp; in breadcrumbs
  var re = /\s+/g;
	for(i = 0; i< texts.length; i++) {
		top = top + '<td class="top_runner2_spacer"><!-- --></td><td class="top_runner2"><a href="' +  links[i] + '" title="' + texts[i] + '">' + texts[i].replace(re,"&nbsp;") + '</a></td>';   
	}
	top = top + '<td class="top_runner2_spacer"><!-- --></td><td class="top_runner2">'+place.replace(re,"&nbsp;")+'</td>';   
    top = top + '<td class="top_runner2_right"><!-- --></td></tr></table>';
	document.getElementById("top_runner").innerHTML = top;
}

/*
	Add comas to number
*/
function addCommas(className)
{
	var mass = document.getElementsByTagName('span');
  for (var i=0; 'object' == typeof mass[i]; i++) {
  	var m = mass[i];
    if (m.className == className) {
     	if (m.firstChild && 3 == m.firstChild.nodeType) {
      	nStr = m.innerHTML;
				nStr += '';
				x = nStr.split('.');
				x1 = x[0];
				x2 = x.length > 1 ? '.' + x[1] : '';
				var rgx = /(\d+)(\d{3})/;
				while (rgx.test(x1)) {
					x1 = x1.replace(rgx, '$1' + ',' + '$2');
				}
				m.innerHTML = x1 + x2;
      }
    }
  }
}

function getWindowSize() {
		var myWidth = 0, myHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		return [myWidth, myHeight];
}

function countRows(where) {
    var rows = new Array();
    for (var i=0; i<where.childNodes.length; i++) {
    
        var row = where.childNodes[i];
        if (row.nodeName!='#text') {
            rows.push(row);
        }
    }
    return rows.length;
}


function moveRows(from, to, countLeave) {
    // clean whitespaces between 'tr' tags (for Mozzy)
    var rows = new Array();
    for (var i=0; i<from.childNodes.length; i++) {
        var row = from.childNodes[i];
        if (row.nodeName!='#text') {
            rows.push(row);
        }
    }

    //copying a number of rows to the destination
    var i = 0;
    while (rows.length>countLeave) {
        if (i++) {
            to.insertBefore(rows.pop(), to.childNodes[0]);
        }
        else {
            to.appendChild(rows.pop());
        }
    }
}


function switchTable(o) {
    if (o.style.display == 'none') {
        o.style.display = document.all && !window.opera ? 'block' : 'table';
        return true;
    } else {
        o.style.display = 'none';
        return false;
    }
}

function setMovableBox(source, more, maxlines, button, display, openMessage, closeMessage) {
    var maxLines = maxlines;
    var source = document.getElementById(source);
    var more = document.getElementById(more);
    var button = document.getElementById(button);
    more.style.display = 'none';
    button.style.display = 'none';

    if (countRows(source.childNodes[0]) > maxLines) {
        moveRows(source.childNodes[0], more.childNodes[0], maxLines);
        button.style.display = display;
        button.onclick = function () {
            button.innerHTML = switchTable(more) ? closeMessage : openMessage;
            return false;
        }
    }
}


/* Trims the given text at the given font size to the given length in pixels and puts the result in the object with the given ID.
 * If the text is shorter than the given length, it's put as plain text. Otherwise it's wrapped inside a <html:abbr> tag.
 * Important: <span id="ruler"></span> should be the first tag inside the page <body>.
 * Usage: <td id="some_id"><script type="text/javascript">trim('some text', 50, 'some_id', 12);</script></td>
 * remember to add  <html xmlns:html="http://www.w3.org/1999/xhtml"> - it may be needed
 */
function trim(text, length, targetId, fontSize)
{
	if (document.getElementById)
	{
		var rulerSpan = document.getElementById('ruler');
		var tempText = text;
		var tooLong = false;
		
		rulerSpan.style.fontSize = fontSize + 'px';
		rulerSpan.innerHTML = tempText;
		
		while (rulerSpan.offsetWidth > length) {
			tempText = tempText.substring(0, tempText.length - 1);
			rulerSpan.innerHTML = tempText + '...';
			tooLong = true;
		}

		var lastChar = tempText.charAt(tempText.length - 1);
		while (lastChar == ' ' || lastChar == ',' || lastChar == '&' || lastChar == '.') {
			tempText = tempText.substring(0, tempText.length - 1);
			lastChar = tempText.charAt(tempText.length - 1);
		}

		if (tooLong) {
			document.getElementById(targetId).innerHTML = '<html:abbr title="' + text + '">' + tempText + '...</html:abbr>';
		} else {
			document.getElementById(targetId).innerHTML = text;
		}
	}
} 

function imageResize(img, dWidth, dHeight) {
	if (img.width > dWidth || img.height > dHeight) {
		var dRatio = dWidth / dHeight;
		var cRatio = img.width / img.height;

		if (dRatio > cRatio) {
			img.height = dHeight;
		} else {
			img.width = dWidth;
		}
	}
}


var highlighText= function( text, find, before, after ){
	if( !find || !text ) return text;
	if( !before ) before= '<strong class="j-highlight">';
	if( !after ) after= '</strong>';
	var words= find.match( /[\w\-]+/g );
	if( !words ) return text;
	var tRegex = new RegExp( '(<.*?>)|\\b(' + words.join( '|' ) + ')\\b', 'gi' );
	return text.replace( tRegex, function( str, tag, word ){
		if( tag ) return tag;
		else return before + word + after;
	})
}

var Null= function(){};
Null.toString= function(){ return null };
Function.prototype.setProto= function( obj ){
	this.prototype= obj;
	return this;
}
Function.prototype.extend= function( obj ){
	for( var i in obj ) this.prototype[ i ]= obj[ i ];
	return this;
}
Function.prototype.create= function( ){
	return new this( );
}
//for DoubleClick banners
var rndTimestamp= Math.floor(Math.random() * 1000000);

function demo_DoFSCommand(command, args)
{
	if (command == "closeAndOpen")
		closeAndOpen(args);
}

function closeAndOpen(url) {
	var newWindow = window.open(url, '', 'dependent=0, fullscreen=1, location=1, menubar=1, resizable=1, scrollbars=1, status=1, titlebar=1, toolbar=1, directories=1');
	newWindow.opener.close();
}

function loadFrontpageAds() {
	$("#nowhiring-table").html('<tr><td colspan="3" class="nowhiring-header"></td></tr><tr><td colspan="3" id="ad-loader"><img src="/v3/gfx/loader.gif" id="ad-img-loader" /></td></tr>');
	$.ajax({
            url: "/cgi-bin/user.cgi?module=news&task=get_logos_for_frontpage",
            success: function(responseText) {
				$("#nowhiring-table").html(responseText);
            },
            error: function() {
            }
        });	
}

function changeRightmenuBanner() {
	var imgSrc = $("#rightmenu-banner").attr("src");
	var imgNo = parseInt(imgSrc.substr(33).charAt(0));
	
	if(imgNo == 1) {
		$("#rightmenu-banner").attr("src", "/v3/gfx/banner-scyscraper-static-2.jpg")
	} else {
		$("#rightmenu-banner").attr("src", "/v3/gfx/banner-scyscraper-static-1.jpg")
	}
}

function rotateFrontpageAds() {
	var nowHiringAds = new Array();
	var lastAd = parseInt(document.getElementById('nowhiring-ad-last').value);
	
	if((lastAd + 1) > 4) {
		document.getElementById('nowhiring-ad-last').value = 0;
	} else {
		document.getElementById('nowhiring-ad-last').value = (lastAd + 1);
	}
	
	nowHiringAds.push('<a href="/microsites/ellisons_estate_agents_3153279"><img alt="Ellisons" src="/v3/gfx/ad-ellisons.jpg"/></a>');
	nowHiringAds.push('<a href="/microsites/dexia_banque_internationale_a_luxembourg_5024776"><img alt="Dexia" src="/v3/gfx/ad-dexia.png"/></a>');
	nowHiringAds.push('<a href="/microsites/blt_12569"><img alt="BLT" src="/v3/gfx/ad-blt.jpg"/></a>');
	nowHiringAds.push('<a href="/vacancies/director_of_markets_5729.html"><img alt="IMD" src="/v3/gfx/ad-imd.jpg"/></a>');
	nowHiringAds.push('<a href="/vacancies/global_product_manager_fund_administration_5799.html"><img alt="RBC Dexia" src="/v3/gfx/ad-rbc-dexia.jpg"/></a>');
	
	for(var i=0; i<4; i++) {
		lastAd++;
		if(lastAd > 4) {
			lastAd = 0;
		}
		$("#nowhiring-ad-" + i).html(nowHiringAds[lastAd]);
	}
}

