function urlencode (str) {
	// http://kevin.vanzonneveld.net
	// +   original by: Philip Peterson
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +      input by: AJ
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Brett Zamir (http://brett-zamir.me)
	// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +      input by: travc
	// +      input by: Brett Zamir (http://brett-zamir.me)
	// +   bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Lars Fischer
	// +      input by: Ratheous
	// +      reimplemented by: Brett Zamir (http://brett-zamir.me)
	// +   bugfixed by: Joris
	// +      reimplemented by: Brett Zamir (http://brett-zamir.me)
	// %          note 1: This reflects PHP 5.3/6.0+ behavior
	// %        note 2: Please be aware that this function expects to encode into UTF-8 encoded strings, as found on
	// %        note 2: pages served as UTF-8
	// *     example 1: urlencode('Kevin van Zonneveld!');
	// *     returns 1: 'Kevin+van+Zonneveld%21'
	// *     example 2: urlencode('http://kevin.vanzonneveld.net/');
	// *     returns 2: 'http%3A%2F%2Fkevin.vanzonneveld.net%2F'
	// *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
	// *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'

	str = (str+'').toString();
	
	// Tilde should be allowed unescaped in future versions of PHP (as reflected below), but if you want to reflect current
	// PHP behavior, you would need to add ".replace(/~/g, '%7E');" to the following.
	return encodeURIComponent(str).replace(/!/g, '%21').replace(/'/g, '%27').replace(/\(/g, '%28').replace(/\)/g, '%29').replace(/\*/g, '%2A').replace(/%20/g, '+');
}

function http_build_query (formdata, numeric_prefix, arg_separator) {
	// Generates a form-encoded query string from an associative array or object.  
	// 
	// version: 911.815
	// discuss at: http://phpjs.org/functions/http_build_query    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Legaev Andrey
	// +   improved by: Michael White (http://getsprink.com)
	// +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// +   improved by: Brett Zamir (http://brett-zamir.me)    // +    revised by: stag019
	// -    depends on: urlencode
	// *     example 1: http_build_query({foo: 'bar', php: 'hypertext processor', baz: 'boom', cow: 'milk'}, '', '&amp;');
	// *     returns 1: 'foo=bar&amp;php=hypertext+processor&amp;baz=boom&amp;cow=milk'
	// *     example 2: http_build_query({'php': 'hypertext processor', 0: 'foo', 1: 'bar', 2: 'baz', 3: 'boom', 'cow': 'milk'}, 'myvar_');    // *     returns 2: 'php=hypertext+processor&myvar_0=foo&myvar_1=bar&myvar_2=baz&myvar_3=boom&cow=milk'
	var value, key, tmp = [];
 
	var _http_build_query_helper = function (key, val, arg_separator) {
		var k, tmp = [];
		if (val === true) {
			val = "1";
		} else if (val === false) {
			val = "0";
		}        
		if (val !== null && typeof(val) === "object") {
			for (k in val) {
				if (val[k] !== null) {
					tmp.push(_http_build_query_helper(key + "[" + k + "]", val[k], arg_separator));
				}            
			}
			return tmp.join(arg_separator);
		} else if (typeof(val) !== "function") {
			return this.urlencode(key) + "=" + this.urlencode(val);
		} else {  
			throw new Error('There was an error processing for http_build_query().');
		}
	};
 
	if (!arg_separator) {
		arg_separator = "&";
	}
	for (key in formdata) {
		value = formdata[key];
		if (numeric_prefix && !isNaN(key)) {
			key = String(numeric_prefix) + key;
		}
		tmp.push(_http_build_query_helper(key, value, arg_separator));
	}
	return tmp.join(arg_separator);
}

function getViewPortHeight()
{
	var height = 0;

	if (window.innerHeight)
	{
		height = window.innerHeight - 18;
	}
	else if ((document.documentElement) && (document.documentElement.clientHeight))
	{
		height = document.documentElement.clientHeight;
	}
	else if ((document.body) && (document.body.clientHeight))
	{
		height = document.body.clientHeight;
	}

	return height;
}

function getJitFrame(jitParam)
{
	document.getElementById('jitwait').style.display='inline';
	var width, height, searchword, jitiframe, src;
	width=(jitParam.boxwidth);
	//height=0;
	searchword=document.getElementById('jitsearchword').value;
	jitiframe=document.getElementById('jitiframe');
	src=jitParam.url + '/partners/cobranding/adlist.php?searchwords=' + searchword + '&' + http_build_query(jitParam) + '&viewport=' + getViewPortHeight();
	jitiframe.width=width;
	if(height)
		jitiframe.height=height;
	jitiframe.src=src;
}

function initJitFrame(jitParam)
{
	var jitiframe,currentHash,height;
	document.getElementById('jitwait').style.display='none';
	jitiframe=document.getElementById('jitiframe');
	currentHash=parent.location.hash;
	if(currentHash.match(/#.*jit(\d+)/))
	{
		var height=RegExp.$1;
		height=parseInt(height)+10;
	}
	else
	{
		height=0;
	}

	if(jitParam.boxheight>0)
	{
		jitiframe.height=jitParam.boxheight+'px';
		jitiframe.overflow='hidden';
	}
	else
	{
		jitiframe.height=height+'px';
		jitiframe.overflow='visible';
	}
	jitiframe.display='block';
}

function writeJitSearchBox(pos,nologo)
{
	document.writeln('<div id="jitsearch' + pos + '" align="left">');
	document.writeln('<input type="text" name="searchword" value="" id="jitsearchword" size="30" onfocus="this.style.color=\'#000\';">&nbsp;<input type="submit" name="search" value="Rechercher" onclick="getJitFrame(jitParam)"><img src="' + jitParam.url + '/images/wait20.gif" alt="recherche en cours..." id="jitwait"/><br/>');
	document.writeln('<span id="jithelp">M&eacute;tier, Lieu, Contrat, ...</span>');
	document.writeln('</div>');
	if(pos=='top' && !nologo)
	{
		document.writeln('<a href="http://www.jobintree.com" target="_blank" id="jitimglink"><img src="' + jitParam.url + '/images/powered_by_jobintree.png" style="margin-right:0px;vertical-align:top" alt="offres emploi"/></a><br/>');
	}
}

function writeJitIframe()
{
	document.writeln('<iframe id="jitiframe" height="0" width="0" scrolling="no" frameborder="0" marginheight="0" marginwidth="0" hspace="0" vspace="0" src="" onload="initJitFrame(jitParam)">');
	document.writeln('</iframe>');
}
function writeJitCss(jitParam)
{
	document.writeln('<style type="text/css">');
	document.writeln('	<!--');
	document.writeln('	#jitsearchbox{padding:10px;width:' + jitParam.boxwidth + 'px;background-color:#' + jitParam.bckgcolor + '; font-size:' + jitParam.fontsize + '; font-familly:' + jitParam.fontname + ';}');
	document.writeln('	#jitsearchtop, #jitsearchbottom {float:left;width:' + (jitParam.boxwidth-100) + 'px;}');
	document.writeln('	#jitwait {margin-left:5px;vertical-align:top}');
	document.writeln('	#jitsearchword {color:#AAA}');
	document.writeln('	#jithelp {color:#AAA;font-size:11px;font-style:italic}');
	document.writeln('	#jitimglink img,#jitimglink:hover img,#jitimglink:visited img {border:none}');
	document.writeln('	-->');
	document.writeln('</style>');
}
function writeJitStartSearchBox()
{
	document.writeln('<div id="jitsearchbox" align="left">');
}
function writeJitEndSearchBox()
{
	document.writeln('</div><br />');
}
//function writeJitTopAdsense(jitParam)
//{
//	document.writeln('<script type="text/javascript">');
//	document.writeln('<!--');
//	document.writeln('google_ad_client = "pub-' + jitParam['adspubid'] + '";');
//	document.writeln('google_alternate_color = "ffffff";');
//	document.writeln('google_ad_width = 600;');
//	document.writeln('google_ad_height = 160;');
//	document.writeln('google_ad_format = "600x160_as";');
//	document.writeln('google_ad_channel ="' + jitParam['adsbanid'] + '";');
//	document.writeln('google_ad_type = "text_image";');
//	document.writeln('google_color_border = "000000";');
//	document.writeln('google_color_bg = "' + jitParam['backgroundcolor'] + '";');
//	document.writeln('google_color_link = "' + jitParam['colorlink'] + '";');
//	document.writeln('google_color_url = "' + jitParam['colorhover'] + '";');
//	document.writeln('google_color_text = "' + jitParam['color'] + '";');
//	document.writeln('//-->');
//	document.writeln('</script>');
//	document.writeln('<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>');
//}


//intialisation url host
//var jitHostUrl=jitParam.host;
var jitHostUrl=location.href.toString().replace(/'/g,'\\\'');
if(jitHostUrl.match(/#[^\/]*$/))
{
	jitHostUrl=jitHostUrl.replace(/#jit\d+/g,'#');
	parent.location=jitHostUrl;
}
if(jitParam.nojitlogo)
{
	var nologo=true;
}
else
{
	var nologo=false;
}

// construction iframe
writeJitCss(jitParam);
writeJitStartSearchBox();
if(jitParam.searchtop=='checked')
{
	writeJitSearchBox('top',nologo);
}
writeJitIframe();
writeJitEndSearchBox();
if(jitParam.searchcheck && jitParam.searchdefault)
{
	jitParam.searchcheck=false;
	document.getElementById('jitsearchword').value=jitParam.searchdefault;
	getJitFrame(jitParam);
}

