// Open popup window
function popup(url, width, height, scroll, resize) {
	var winh = height;
	var winw = width;
	var winx = (screen.width / 2) - (winw / 2);
	var winy = (screen.height / 2) - (winh / 2);
	var scrollbar = scroll;
	var resizable = resize;
	var win = window.open(url, '', 'scrollbars=' + scrollbar +',resizable=' + resizable + ',width=' + winw + ',height=' + winh + ',left=' + winx + ',top=' + winy + ',screenX=' + winx + ',screenY=' + winy);
}

function PageInit() {
	initMenuThread();
}
function postSearchQuery(lang) {
	var node = document.getElementById("searchField");
	if ( node != null ) {
		if ( new String( trim( node.value ) ).length > 0 ) {
			window.location = "/" + lang + "/search/index.asp?q=" + replace( node.value, " ", "+" ) + "&t=-300&s=";
		}
	}
}
function getSingleNode( nodeList, nodeId ) {
	for ( var i = 0; i < nodeList.length; i++ ) {
		var node = nodeList[i];
		var attrId = node.attributes['id'];
		if ( ( attrId != null ) && ( attrId.value == 'searchField' ) ) {
			return node;
		}
	}
	return null;
}
function replace( input, oldString, newString ) {
	return ( new String( input ) ).replace( new RegExp( oldString, 'gi' ), newString );
}	

function ltrim ( s ) {
	return s.replace( /^\s*/, "" );
}

function rtrim ( s ) {
	return s.replace( /\s*$/, "" );
}

function trim ( s ) {
	return rtrim( ltrim( s ) );
}

/* Marche juste dans IE */
function detectReturnCode() {
	if ( window.event.keyCode == 13 ) {
		postSearchQuery(lang);
		return false;
	}		
	return true;
}

/* KeyPress handler qui marche dans Mozilla; IE; Opera */
document.onkeyup = handleKeyUp;
function handleKeyUp( e ) {
	if( !e ) {
		var e = window.event;
	}
	key = e.keyCode;
	if ( ( key == 13 ) && searchFieldIsFocused) { 
		postSearchQuery(lang);
	}
}

var searchFieldIsFocused = false;

function searchPhrase() {
	var ret = "";

	if (lang == "fr") {
		ret = "Entrez recherche";
	}
	else if (lang == "en") {
		ret = "Enter search term";
	}
	
	return ret;
}

function sfOnFocus() {
	searchFieldIsFocused = true;
	
	var sp = searchPhrase();
	
	var sf = document.getElementById("searchField");
	if (sf) {
		if (trim(sf.value) == sp) {
			sf.value = "";
		}
	}
	
	return true;
}

function sfOnBlur() {
	searchFieldIsFocused = false;
	
	var sp = searchPhrase();
	
	var sf = document.getElementById("searchField");
	if (sf) {
		if (trim(sf.value) == "") {
			sf.value = sp;
		}
	}
	
	return true;
}

function xWindow(name, w, h, x, y, loc, men, res, scr, sta, too)
{
  var e='',c=',',xf='left=',yf='top='; this.n = name;
  if (document.layers) {xf='screenX='; yf='screenY=';}
  this.f = (w?'width='+w+c:e)+(h?'height='+h+c:e)+(x>=0?xf+x+c:e)+
    (y>=0?yf+y+c:e)+'location='+loc+',menubar='+men+',resizable='+res+
    ',scrollbars='+scr+',status='+sta+',toolbar='+too;
  this.opened = function() {return this.w && !this.w.closed;};
  this.close = function() {if(this.opened()) this.w.close();};
  this.focus = function() {if(this.opened()) this.w.focus();};
  this.load = function(sUrl) {
    if (this.opened()) this.w.location.href = sUrl;
    else this.w = window.open(sUrl,this.n,this.f);
    this.focus();
    return false;
  };
}

function nextMW() {
	var cDiv = document.getElementById("marketingwindow" + mwIds[mwCurrentIndex % mwIds.length]);
	var cNextDiv = document.getElementById("marketingwindow" + mwIds[(mwCurrentIndex + 1) % mwIds.length]);

	if (cDiv && cNextDiv) {
		cDiv.style.display = "none";
		cNextDiv.style.display = "block";
		mwCurrentIndex++;
	}

	return false;
}

function nextSC() {
	var cDiv = document.getElementById("scenario" + scIds[scCurrentIndex % scIds.length]);
	var cNextDiv = document.getElementById("scenario" + scIds[(scCurrentIndex + 1) % scIds.length]);

	if (cDiv && cNextDiv) {
		cDiv.style.display = "none";
		cNextDiv.style.display = "block";
		scCurrentIndex++;
	}

	return false;
}
