/******************************
Global JS variable
******************************/

var Ws_bNS = (navigator.vendor == ('Netscape6') || navigator.product == ('Gecko'));
var Ws_bSafari = (navigator.userAgent.indexOf('Safari')!=-1)?true:false;
// var Ws_bIE6 = (navigator.userAgent.indexOf('MSIE 6')!=-1)?true:false;
// on prend de l'avance en testant IE 7
var Ws_bIE7 = (navigator.userAgent.indexOf('MSIE 7')!=-1)?true:false;
// IE > 6 ou moz... on active le mode quick.
// var Ws_bQuick = (Ws_bNS || Ws_bIE6 || Ws_bIE7);
// var Ws_bQuick = false;

var sleepStopDate;
var sleepWait;

/******************************
Global JS function
******************************/

function getObj(obj)
{
	if (document.all)
		return document.all[obj];
	else
		return document.getElementById(obj);
}

window.tCachedObj = [];
function getCObj(obj)
{
	if (window.tCachedObj[obj])
	{
		return window.tCachedObj[obj];
	}
	else if (document.all)
	{
		window.tCachedObj[obj] = document.all[obj];
		return window.tCachedObj[obj];
	}
	else
	{
		window.tCachedObj[obj] = document.getElementById(obj);
		return window.tCachedObj[obj];
	}
}

function debug(obj)
{
// cette methode permet d'afficher le contenu d'un objet javascript
	alert('debuggage');
	var tmp, c;
	tmp = name + '\n\n';
	c = 0;
	for (l in obj)
	{
		if (typeof(obj[l]) != 'function')
		{
			var data = '' + obj[l];
			if (data.length > 80)
				data = data.substr(0, 80) + '...';
			tmp += (l + '=' + data + '\n');
			c++;
		}
		if (c == 25)
		{
			alert(tmp);
			tmp = name + '\n\n';
			c = 0;
		}
	}
	alert(tmp);
}

function ajax(fichier)
{
	var xhr_object;

	if(window.XMLHttpRequest)
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject)
		xhr_object = new ActiveXObject('Microsoft.XMLHTTP');
	else
		return(false);

	xhr_object.open('GET', fichier, false);
	xhr_object.send(null);
	if(xhr_object.readyState == 4)
		return(xhr_object.responseText);
	else
		return(false);
}

function ajaxEx(fichier, parameters, method, callback)
{
	var xhr_object;

	if(window.XMLHttpRequest)
		xhr_object = new XMLHttpRequest();
	else if(window.ActiveXObject)
		xhr_object = new ActiveXObject('Microsoft.XMLHTTP');
	else
		return(false);

	if ((callback) && (callback != ''))
	{
		xhr_object.onreadystatechange = function()
		{
			if (xhr_object.readyState == 4)
			{
				if (xhr_object.status == 200)
				{
					var txtResponse = xhr_object.responseText;
					var xmlResponse = xhr_object.responseXML;
					window.tAjaxRes = txtResponse;
					eval(callback);
				}
				else
				{
					var txtResponse = "{result:false, Error:'http error : " + xhr_object.status + "', http_error:" + xhr_object.status + "}";
					window.tAjaxRes = txtResponse;
					eval(callback);
				}
			}
/*
			else
			{
				alert('g' + xhr_object.status);
			}
*/
		}
	}

	if (method == 'POST')
	{
		if ((callback) && (callback != ''))
			xhr_object.open('POST', fichier, true);
		else
			xhr_object.open('POST', fichier, false);
      xhr_object.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
//      xhr_object.setRequestHeader('Content-length', parameters.length);
//      xhr_object.setRequestHeader('Connection', 'close');
      xhr_object.send(parameters);
	}
	else
	{
		if ((callback) && (callback != ''))
			xhr_object.open('GET', fichier + '?' + parameters, true);
		else
			xhr_object.open('GET', fichier + '?' + parameters, false);

		xhr_object.send(null);
	}

	if ((callback) && (callback != ''))
		return true;
	else if(xhr_object.readyState == 4)
	{
		if (xhr_object.status != 200)
			return ("{result:false, Error:'http error : " + xhr_object.status + "', http_error:" + xhr_object.status + "}");
		else
			return(xhr_object.responseText);
	}
	else
		return(false);
}

function ajaxParallele(url, callback, numObject, uid)
{
	var key = getUI();
	var oFrame = getObj('excel_frame');
	if (!oFrame)
	{
		var body = document.body;
		var oFrame = addtag(body, 'IFRAME', 'uploadtarget', ['id', 'excel_frame'], '');
		oFrame.style.left = '100px';
	}
	oFrame.alt = callback;
	oFrame.numObject = numObject;
	oFrame.uid = uid;
	oFrame.src = url;
	oFrame.onload = cbAjaxParalle;
/*
	var ui = getShortUI();
	var body = document.body;
	var id = 'IFR_' + ui;
	var ifr = addtag(body, 'IFRAME', 'uploadtarget', ['id', id], '');
	ifr.style.left = '100px';
	ifr.src = 'http://th.extranet2.ufp-international.fr' + url;
	ifr.onload = cbAjaxParalleFF;
*/
}

function cbAjaxParalle()
{
	alert(this.alt);
	alert(this.numObject);
	alert(this.uid);
	dataRes = ajaxEx('/ws_userstuff/cache/' + this.id, '', 'GET', '');
}


function ajaxEscape(s)
{
	s = s.replace(/%/g, '%25');
	s = s.replace(/\\\\/g, '%5c');
	s = s.replace(/ /g, '%20');
	s = s.replace(/\+/g, '%2b');
	s = s.replace(/\?/g, '%3f');
	s = s.replace(/\&/g, '%26');
	return s;
}

function ajaxEval(dataRes)
{
	try { var oResult = eval('(' + dataRes + ')'); }
	catch (e) { var oResult = {result:false, Error:'PHP error'}; }
	return oResult;
}

function addtext(src, text)
{
	var txt = document.createTextNode(text);
	src.appendChild(txt);
	return (src);
}

function addtag(src, tag, classname, attribut, text)
{
	var obj = document.createElement(tag);
	var i;
	if (classname)
		obj.className = classname;

	for (i = 0 ; i < attribut.length ; i+=2)
	{
		var att = attribut[i];
		var val = attribut[i + 1];
		obj.setAttribute(att, val);
	}

	if (text)
	{
		var txt = document.createTextNode(text);
		obj.appendChild(txt);
	}

	src.appendChild(obj);
	return (obj);
}

function addOption(src, value, text)
{
	if (Ws_bNS)
	{
		var obj  = new Option(text, value);
		src.options[src.length] = obj;
	}
	else
	{
		var obj = document.createElement('OPTION');
		obj.text=text;
		obj.value=value;
		src.add(obj);
	}
	return (obj);
}

function inserttag(src, chld, tag, classname, attribut, text)
{
	var obj = document.createElement(tag);
	var i;
	if (classname)
		obj.className = classname;

	for (i = 0 ; i < attribut.length ; i+=2)
	{
		var att = attribut[i];
		var val = attribut[i + 1];
		obj.setAttribute(att, val);
	}

	if (text)
	{
		var txt = document.createTextNode(text);
		obj.appendChild(txt);
	}

	src.insertBefore(obj, chld);
	return (obj);
}

function searchtag(obhtml, tag, tO)
{
	var kids, i;

	if (obhtml.tagName == tag)
		tO[tO.length] = obhtml;

	kids = obhtml.childNodes;
	for (i = 0; i < kids.length; i++)
		searchtag(kids[i], tag, tO);
}

function fireEvent(obj, event)
{
	if (document.createEventObject)
	{
		var evt = document.createEventObject();
		return obj.fireEvent('on'+event,evt)
	}
	else
	{
		var evt = document.createEvent("HTMLEvents");
		evt.initEvent(event, true, true ); // event type,bubbling,cancelable
		return !obj.dispatchEvent(evt);
	}
}

function getAbsoluteOffset(obj, use_scroll, dbg)
{
	var oCoord = {x:0, y:0};

	oCoord.x = obj.offsetLeft;
	oCoord.y = obj.offsetTop;
	oCoord.w = obj.scrollWidth;
	oCoord.h = obj.scrollHeight;
	oCoord.wview = obj.offsetWidth;
	oCoord.hview = obj.offsetHeight;

	var parent = obj.offsetParent;
	while (parent)
	{
		oCoord.x += parent.offsetLeft;
		oCoord.y += parent.offsetTop;

		if (dbg == 1)
		      alert(parent.id + ':' + parent.offsetTop + '--' + oCoord.y);

		parent = parent.offsetParent;
	}

	parent = obj.parentNode;
	while (parent)
	{
		if ((use_scroll == true) && (parent.nodeName == 'DIV'))
			oCoord.y -= parent.scrollTop;
		parent = parent.parentNode;
	}
	return oCoord;
}

function getBrowserZone()
{
	var oCoord = {};

	if (document.all)
	{
		if (Ws_bIE7)
		{
			oCoord.x = document.documentElement.clientWidth;
			oCoord.y = document.documentElement.clientHeight;
		}
		else
		{
			oCoord.x = document.body.clientWidth;
			oCoord.y = document.body.clientHeight;
		}
	}
	else
	{
		oCoord.x = window.innerWidth;
		oCoord.y = window.innerHeight;
	}
	return oCoord;
}

function setMaxHeight(obj, minus)
{
	getObj(obj).style.height = (document.documentElement.clientHeight - minus) + 'px';
	return (document.documentElement.clientHeight);

	if (document.all)
	{
		if (Ws_bIE7)
			getObj(obj).style.height = (document.documentElement.clientHeight - minus) + 'px';
		else
			getObj(obj).style.height = (document.body.clientHeight - minus) + 'px';
	}
	else
		getObj(obj).style.height = (window.innerHeight - minus) + 'px';
}

function setMaxWidth(obj, minus)
{
//	minus += 50;
	if (document.all)
	{
//		if (Ws_bIE6)
// IE 6
//			getObj(obj).style.width = (document.documentElement.clientWidth - minus) + 'px';
//		else
			getObj(obj).style.width = (document.body.clientWidth - minus) + 'px';
	}
// mozilla c'est space...
	else
		getObj(obj).style.width = (window.innerWidth - minus) + 'px';
}

function trim(st)
{
	st = ' ' + st;
	return st.replace(/(^\s*)|(\s*$)/g,'');
}

function nl2br(st)
{
	st = '' + st;
	return st.replace(/\\n/g, '<br>');
}

function addslashes(st)
{
	st = '' + st;
	return st.replace(/\'/g, "\\'");
}

function htmlencode(st, nl2br)
{
	if (typeof(st) == 'string')
	{
		st = st.replace(/&/g,'&amp;');
		st = st.replace(/\"/g,'&quot;');
		st = st.replace(/'/g,'&#039;');
		st = st.replace(/</g,'&lt;');
		st = st.replace(/>/g,'&gt;');
		if (nl2br)
			st = st.replace(/\\n/g,'<br />');
	}
	return st;
}

window.ws_lastMD5 = '';
function getUI(key)
{
	var script = document.location.href;
	var str;
	var md5 = window.ws_lastMD5;
	while (md5 == window.ws_lastMD5)
	{
		var d = new Date();
   	str = key + script + '##' + d.getTime();
		md5 = hex_md5(str);
	}

	window.ws_lastMD5 = md5;
	return md5;
}

window.ws_lastGUID = 0;
function getShortUI()
{
	var ui = '00000' + window.ws_lastGUID;
	var nb = ui.length;
	ui = '_ws' + ui.substr(nb - 5, 5) + '_';
	window.ws_lastGUID++;
	return ui;
}

function cancelBubble(ev)
{
	if (ev.stopPropagation)
		ev.stopPropagation();
	ev.cancelBubble = true;

	return false;
}

function sleep(milliSecond)
{
	var now = new Date();
	var startingMSeconds = now.getTime();
	var endMSeconds = startingMSeconds + milliSecond;
	var ms = startingMSeconds;
	while (ms < endMSeconds)
	{
		now = new Date();
		ms = now.getTime();
	}
	return false;
}

function getObjectClass(obj)
{
	if (obj && obj.constructor && obj.constructor.toString)
	{
		var arr = obj.constructor.toString().match(/function\s*(\w+)/);
		if (arr && arr.length == 2)
		{
			return arr[1];
		}
	}
	return undefined;
}

Object.prototype.clone = function() {
  var newObj = (this instanceof Array) ? [] : {};
  for (i in this) {
    if (i == 'clone') continue;
    if (this[i] && typeof this[i] == "object") {
      newObj[i] = this[i].clone();
    } else newObj[i] = this[i]
  } return newObj;
};

