﻿function gId(id) { return document.getElementById(id); }

//function hideView(isVisible, idToHideView) {
//    if ($get(idToHideView))
//        $get(idToHideView).style.display = test ? 'block' : 'none';
//}

//String Tools
function trim(str) {
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}
//Debug Tools
function viewObject(obj) {
	addToDebug('start');
	try {
		addToDebug('<b>' + obj._name + '</b>:');
	}
	catch (err) { }
	for (var toto in obj) {
		try {
			addToDebug(toto);  //+ ': ' + eval(obj._name + '.' + toto) + '<br/>';
		}
		catch (err) {
			//addToDebug(toto); //+ ': erreur contenu: ' + err.message + '<br/>';
		}
	}

}
function addToDebug(str) {
	document.body.innerHTML += str + '<br/>';
}
function rwDebug(str) {
	document.body.innerHTML += str + '<br/>';
}
//View Tools
function hideView(idToHideView, idHandle) {
	if (document.getElementById(idToHideView)) {
		document.getElementById(idToHideView).style.display = document.getElementById(idToHideView).style.display == 'none' ? '' : 'none';
		if (document.getElementById(idHandle))
			document.getElementById(idHandle).className = 'selected';
	}
}

function viewIndex(startName, indexToView, countMax, hidRef) {
	document.getElementById(hidRef).value = indexToView;
	for (var p = 1; p <= countMax; p++) {
		if (document.getElementById(startName + p))
			document.getElementById(startName + p).style.display = 'none';
	}
	document.getElementById(startName + indexToView).style.display = 'block';
}

function hvIndex(startName, indexToView, oSource) {
	//alert(document.body.getAttribute(startName+'IndexSov'));
	if (document.body.getAttribute(startName + 'IndexSov'))
		document.getElementById(startName + document.body.getAttribute(startName + 'IndexSov')).style.display = 'none';
	document.getElementById(startName + indexToView).style.display = 'block';
	document.body.setAttribute(startName + 'IndexSov', indexToView);
	if (oSource) {
		if (document.body.getAttribute(startName + 'HandleNameSov')) {
			document.getElementById(document.body.getAttribute(startName + 'HandleNameSov')).className =
				document.body.getAttribute(startName + 'HandleNameClassNameSov');
		}
		document.body.setAttribute(startName + 'HandleNameSov', oSource.id);
		document.body.setAttribute(startName + 'HandleNameClassNameSov', oSource.className);
		oSource.className = 'selected';
	}
}

/* Ajax Extension                   */
// Erreur
// This is the failed callback function.
function FailedCallback(error) {
	var stackTrace = error.get_stackTrace();
	var message = error.get_message();
	var statusCode = error.get_statusCode();
	var exceptionType = error.get_exceptionType();
	var timedout = error.get_timedOut();

	// Display the error.    
	return "Stack Trace: " + stackTrace + "<br/>" +
        "Service Error: " + message + "<br/>" +
        "Status Code: " + statusCode + "<br/>" +
        "Exception Type: " + exceptionType + "<br/>" +
        "Timedout: " + timedout;
}



//Objet Onglet
function tabObject(aHandleStartName, aContentStartName, hIdStoreObject) {

	this.classNameOver = 'selected';
	this.classNameSelected = 'selected';
	this.handleStartName = aHandleStartName;
	this.contentStartName = aContentStartName;

	this.getSelectedIndex = function () {
		if (document.getElementById(hIdStoreObject))
			return document.getElementById(hIdStoreObject).value;
		else
			return undefined;
	}
	this.setSelectedIndex = function (val) {
		if (document.getElementById(hIdStoreObject))
			document.getElementById(hIdStoreObject).value = val;
		return val;
	}

	//this.selectedIndex = undefined;
	this.overTab = function (indexTab) {
		//Cache la selection
		if (this.getSelectedIndex() != undefined) {
			this.hideTab(this.getSelectedIndex());
		}
		this.viewTab(indexTab)
	}

	this.outTab = function (indexTab) {
		this.hideTab(indexTab)
		//View la selection
		if (this.getSelectedIndex() != undefined)
			this.viewTab(this.getSelectedIndex());
	}

	this.viewTab = function (indexTab) {
		if (document.getElementById(aHandleStartName + indexTab)) {
			if (!document.getElementById(aHandleStartName + indexTab).classNameSov && !document.getElementById(aHandleStartName + indexTab).classNameSov == '') {
				document.getElementById(aHandleStartName + indexTab).classNameSov = document.getElementById(aHandleStartName + indexTab).className;
			}
			document.getElementById(aHandleStartName + indexTab).className = this.classNameOver;
		}
		if (document.getElementById(aContentStartName + indexTab)) {

			document.getElementById(aContentStartName + indexTab).style.display = 'block';
		}

	}

	this.hideTab = function (indexTab) {
		if (document.getElementById(aHandleStartName + indexTab)) {
			if (document.getElementById(aHandleStartName + indexTab).classNameSov)
				document.getElementById(aHandleStartName + indexTab).className = document.getElementById(aHandleStartName + indexTab).classNameSov;
			else
				document.getElementById(aHandleStartName + indexTab).className = '';

			if (document.getElementById(aContentStartName + indexTab))
				document.getElementById(aContentStartName + indexTab).style.display = 'none';
		}
		if (document.getElementById(aContentStartName + indexTab))
			document.getElementById(aContentStartName + indexTab).style.display = 'none';

	}

	this.selectTab = function (indexTab) {
		if (this.getSelectedIndex() != undefined)
			this.hideTab(this.getSelectedIndex());
		this.setSelectedIndex(indexTab);
		this.viewTab(indexTab);
	}


	//Init
	if (this.getSelectedIndex() != '')
		this.selectTab(this.getSelectedIndex());
	else
		this.selectTab(0);
}


//Objet Onglet
function accordionObject(aHandleStartName, aContentStartName, hIdStoreObject, aAllowViewHide) {

	this.classNameOver = 'selected';
	this.classNameSelected = 'selected';
	this.handleStartName = aHandleStartName;
	this.contentStartName = aContentStartName;

	//Option
	this.allowViewHide = aAllowViewHide;

	this.getSelectedIndex = function () {
		if (document.getElementById(hIdStoreObject))
			return document.getElementById(hIdStoreObject).value;
		else
			return undefined;
	}
	this.setSelectedIndex = function (val) {
		if (document.getElementById(hIdStoreObject))
			document.getElementById(hIdStoreObject).value = val;
		return val;
	}

	//this.selectedIndex = undefined;
	this.overTab = function (indexTab) {
		//Cache la selection
		if (this.getSelectedIndex() != undefined) {
			this.hideTab(this.getSelectedIndex());
		}
		this.viewTab(indexTab)
	}

	this.outTab = function (indexTab) {
		this.hideTab(indexTab)
		//View la selection
		if (this.getSelectedIndex() != undefined)
			this.viewTab(this.getSelectedIndex());
	}

	this.viewTab = function (indexTab) {
		if (document.getElementById(aHandleStartName + indexTab)) {
			if (!document.getElementById(aHandleStartName + indexTab).classNameSov && !document.getElementById(aHandleStartName + indexTab).classNameSov == '') {
				document.getElementById(aHandleStartName + indexTab).classNameSov = document.getElementById(aHandleStartName + indexTab).className;
			}
			document.getElementById(aHandleStartName + indexTab).className = this.classNameOver;
			document.getElementById(aContentStartName + indexTab).style.display = 'block';
		}
	}

	this.hideTab = function (indexTab) {
		if (document.getElementById(aHandleStartName + indexTab)) {
			if (document.getElementById(aHandleStartName + indexTab).classNameSov)
				document.getElementById(aHandleStartName + indexTab).className = document.getElementById(aHandleStartName + indexTab).classNameSov;
			else
				document.getElementById(aHandleStartName + indexTab).className = '';

			if (document.getElementById(aContentStartName + indexTab))
				document.getElementById(aContentStartName + indexTab).style.display = 'none';
		}
	}

	this.selectTab = function (indexTab) {
		if (this.getSelectedIndex() != undefined && this.getSelectedIndex() != indexTab)
			this.hideTab(this.getSelectedIndex());

		this.setSelectedIndex(indexTab);
		//Affiche ou Cache
		if (gId(aContentStartName + indexTab) && this.allowViewHide) {
			//if (document.getElementById(aContentStartName + indexTab).style.display == 'none') {
			this.viewTab(indexTab);
			//} else {
			//   this.hideTab(indexTab);
			//}
		}
		else
			this.viewTab(indexTab);
	}


	//Init
	if (this.getSelectedIndex() != '')
		this.selectTab(this.getSelectedIndex());
	else
		this.selectTab(0);
}

/****** View on Checked ******/
//Objet Onglet
function viewOnCheckedObject(aCheckBoxId, aContentId) {

	this.checkBoxId = aCheckBoxId;
	this.contentId = aContentId;

	this.checkbox = document.getElementById(checkBoxId);
	this.content = document.getElementById(contentId);

	if (checkbox && content) {

		checkbox.onchange = function () {
			content.style.display = checkbox.checked ? '' : 'none';
		}
	}
}

/****** *************** ******/


/* Flash */
function getFlashMovieObject(movieName) {
	if (document.embeds[movieName]) {
		return document.embeds[movieName];
	}
	if (window.document[movieName]) {
		return window.document[movieName];
	}
	if (navigator.appName.indexOf("Microsoft Internet") == -1) {
		if (document.embeds && document.embeds[movieName])
			return document.embeds[movieName];
	}
	else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	{
		return document.getElementById(movieName);
	}
}


/* Cookies */
function EcrireCookie(nom, valeur) {
	var argv = EcrireCookie.arguments;
	var argc = EcrireCookie.arguments.length;
	var expires = (argc > 2) ? argv[2] : null;
	var path = (argc > 3) ? argv[3] : null;
	var domain = (argc > 4) ? argv[4] : null;
	var secure = (argc > 5) ? argv[5] : false;
	document.cookie = nom + "=" + escape(valeur) +
	((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
	((path == null) ? "" : ("; path=" + path)) +
	((domain == null) ? "" : ("; domain=" + domain)) +
	((secure == true) ? "; secure" : "");
}

function getCookieVal(offset) {
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1) endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
function LireCookie(nom) {
	var arg = nom + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen) {
		var j = i + alen;
		if (document.cookie.substring(i, j) == arg) return getCookieVal(j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}
