// fermer le popUp
function closePopUp() {
	window.close();
}

// fonction qui retourne l'usager au login
function sessionClosed() {
	top.window.location.href = '/logoutAction.do';
}

// pour faire un popUp
function popup(sPageAndVar, sNameWin, sParam) {
	if (sNameWin == null)
		sNameWin = "PopUp";
	if (sParam == null)
		sParam = "width=10,height=10,top=100,left=50,resizable=no,scrollbars=no,status=no,menubar=no";
	var popDownload = open(sPageAndVar, sNameWin, sParam);
	popDownload.focus();
}

function goToTSX(sLangTSX) {
	var sPageTSX = "http://tmx.quotemedia.com/quote.php?qm_symbol=gcl&locale="
			+ sLangTSX;
	var sParamTSX = "width="
			+ (screen.width - 100)
			+ ",height="
			+ (screen.height - 50)
			+ ",top=0,left=50,resizable=yes,scrollbars=yes,status=yes,menubar=no";
	var popDownload = open(sPageTSX, "TSX", sParamTSX);
	popDownload.focus();
}

function showHideDoc() {
	if (window.document.all.docBox.style.display == 'none')
		window.document.all.docBox.style.display = 'block';
	else
		window.document.all.docBox.style.display = 'none';
}

function Action(quoi) {
	document.formulaire.sQuoifaire.value = quoi;
	document.formulaire.submit();
}

function ActionConfirm(quoi) {
	if (quoi == 'Delete')
		if (confirm("Attention ! \n\n Êtes-vous certain de vouloir supprimer cet enregistrement ?"))
			Action(quoi);
		else if (quoi == 'Update')
			if (confirm("Attention ! \n\n Êtes-vous certain de vouloir mettre à jour cet enregistrement ?"))
				Action(quoi);
			else if (quoi == 'Save')
				if (confirm("Attention ! \n\n Êtes-vous certain de vouloir créer cet enregistrement ?"))
					Action(quoi);
				else
					alert("Aucun choix valide d'opération !");
}

function formatCurrency(nValue) {
	var nDecimalLength = -1;
	nValue = (Math.round(nValue * 100) / 100).toString();
	var nLenght = nValue.length;
	for (i = 0; i < nLenght; i++) {
		if (nValue.charAt(i) == ".") {
			nDecimalLength = (nLenght - (i + 1))
			break;
		}
	}
	if (nDecimalLength < 0)
		return nValue + ".00";
	else if (nDecimalLength == 0)
		return nValue + "00";
	else if (nDecimalLength == 1)
		return nValue + "0";
	else
		(nDecimalLength > 1)
	return nValue;
}

// onabort="imageTemp(this);" onError="imageTemp(this);"
function imageTemp(sThis) {
	sThis.src = "images/spacer.gif";
	return true;
}

// onclick="findItemInSelect(document.formulaire.sSearch.value,
// document.formulaire.selectObj, false);"
function findItemInSelect(varSearch, objSelect, bMultiple) {
	varSearch = varSearch.toUpperCase();

	if (varSearch.length > 0) {
		for (i = 0; i < objSelect.length; i++) {
			objSelect.options[i].selected = false;
			if (objSelect.options[i].text.toUpperCase().indexOf(varSearch) != -1) {
				if (bMultiple) {
					if (confirm(objSelect.options[i].text))
						objSelect.options[i].selected = true;
				} else {
					objSelect.options[i].selected = true;
					if (confirm(objSelect.options[i].text))
						i = objSelect.length;
				}
			}
		}
	} else {
		alert("Saisir quelquechose avant de lancer la recherche !");
	}
}

var sCC_01_fr = "Attention !\n\nVotre description ne peut contenir plus de ";
var sCC_02_fr = " caractères.\nIl y a actuellement ";
var sCC_03_fr = " caratères.";
var sCC_01_en = "Warning\n\nA maximum of ";
var sCC_02_en = " characters is allowed.  Your field count more than  ";
var sCC_03_en = ".";
// OnBlur="CompterCaracteres(this, 250,'<%=sLang%>');"
function CompterCaracteres(qui, maxi, sLang) {
	var var_chaine = qui.value.length;
	if (var_chaine > maxi) {
		if (sLang == "en")
			alert(sCC_01_en + maxi + sCC_02_en + var_chaine + sCC_03_en);
		else
			alert(sCC_01_fr + maxi + sCC_02_fr + var_chaine + sCC_03_fr);
		qui.focus();
		return false;
	}
	return true;
}

// onblur=' replace(this.value,",",".",this,"0");'
function replace(string, text, by, sThis, defaut) {
	var strLength = string.length, txtLength = text.length;
	if ((strLength == 0) || (txtLength == 0)) {
		sThis.value = defaut;
		return string;
	}

	var i = string.indexOf(text);
	if ((!i) && (text != string.substring(0, txtLength)))
		return string;
	if (i == -1)
		return string;

	var newstr = string.substring(0, i) + by;

	if (i + txtLength < strLength)
		newstr += replace(string.substring(i + txtLength, strLength), text, by,
				sThis);
	sThis.value = newstr;
	return newstr;
}

var sID_01_fr = "Ce Champs ne peut contenir autre chose que les valeurs suivantes : ";
var sID_01_en = "In this field, you can only used these characters : ";
// onblur="isDigit(this.value,this,'0123456789.','<%=sLang%>');"
function isDigit(laString, sThis, mask, sLang) {
	var permis = mask;
	if (sThis.value == "")
		sThis.value = 0;
	for ( var i = 0; i < laString.length; i++) {
		tt = laString.substring(i, i + 1)
		if (permis.indexOf(tt) == -1) {
			if (sLang == "en")
				alert(sID_01_en + mask);
			else
				alert(sID_01_fr + mask);
			sThis.value = 0;
			sThis.select();
			return false;
		}
	}
	return true;
}

// Transforme la chaine de caractère en majuscule
// onblur="toCaps(this);"
function toCaps(sThis) {
	sThis.value = sThis.value.toUpperCase();
}

function addCheckDigitColabor(sCodeColIN) {
	var sCodeColOUT = "";
	var arrDigits = new Array(20);
	var nTempResult = 0;
	var sTempDigit = "";
	if (sCodeColIN != null) {
		var nSize = sCodeColIN.length;
		if (nSize <= 20) {
			sCodeColOUT = sCodeColIN;
			// padder avec des "0" x20
			for ( var j = nSize; j < 20; j++)
				sCodeColOUT = "0" + sCodeColOUT;
			// placer chacun des digits dans le tableau
			for ( var k = 0; k < sCodeColOUT.length; k++)
				arrDigits[k] = ((sCodeColOUT.substring(k, k + 1)) * 1);

			for ( var m = (sCodeColOUT.length - 1); m >= 0; m--) {
				if (m % 2 != 0) {// impaire
					sTempDigit = "" + (arrDigits[m] * 2);
					for ( var n = 0; n < sTempDigit.length; n++)
						nTempResult += ((sTempDigit.substring(n, n + 1)) * 1);
				} else {// paire
					nTempResult += (arrDigits[m] * 1);
				}
			}

			nTempResult = (11 - ((nTempResult % 10) + 1));
			if (nTempResult == 10) {
				nTempResult = 0;
			}
			sCodeColOUT = sCodeColOUT + "" + nTempResult;
		}
	}
	return (sCodeColOUT * 1);
}

// soumettre un formulaire avec la touche ENTER
// onKeyPress="checkEnter(event)"
NS4 = (document.layers) ? true : false;
function checkEnter(event) {
	var code = 0;
	if (NS4)
		code = event.which;
	else
		code = event.keyCode;
	if (code == 13)
		return true;
	else
		return false;
}

// fonction qui retourne le nombre de jour entre deux dates
// DaysBetween2Dates('dBegin' , 'dFinish')
function DaysBetween2Dates(dDate_B, dDate_F) {

	var _d2 = new Date(dDate_B) // date de début
	var _d1 = new Date(dDate_F) // date de fin
	var _delta = 0;
	_delta = _d1.getTime() - _d2.getTime(); // temps en millisecondes
	_delta /= (1000 * 60 * 60 * 24); // Jours
	_delta = Math.round(_delta); // pour arrondir et avoir des dates entières
	// alert(_delta);
	return _delta;
}

// chimes.wav
// notify.wav
// chord.wav
// ding.wav
// <bgsound id=objSound>
function playSound(sWavFile) {
	if (eval("objSound"))
		objSound.src = '/library/' + sWavFile;
}

function replaceSubstring(inputString, fromString, toString) {
	// Goes through the inputString and replaces every occurrence of fromString
	// with toString
	var temp = inputString;
	if (fromString == "") {
		return inputString;
	}
	if (toString.indexOf(fromString) == -1) { // If the string being replaced
		// is not a part of the
		// replacement string (normal
		// situation)
		while (temp.indexOf(fromString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)
					+ fromString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	} else { // String being replaced is part of replacement string (like "+"
		// being replaced with "++") - prevent an infinite loop
		var midStrings = new Array("~", "`", "_", "^", "#");
		var midStringLen = 1;
		var midString = "";
		// Find a string that doesn't exist in the inputString to be used
		// as an "inbetween" string
		while (midString == "") {
			for ( var i = 0; i < midStrings.length; i++) {
				var tempMidString = "";
				for ( var j = 0; j < midStringLen; j++) {
					tempMidString += midStrings[i];
				}
				if (fromString.indexOf(tempMidString) == -1) {
					midString = tempMidString;
					i = midStrings.length + 1;
				}
			}
		} // Keep on going until we build an "inbetween" string that doesn't
		// exist
		// Now go through and do two replaces - first, replace the "fromString"
		// with the "inbetween" string
		while (temp.indexOf(fromString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)
					+ fromString.length, temp.length);
			temp = toTheLeft + midString + toTheRight;
		}
		// Next, replace the "inbetween" string with the "toString"
		while (temp.indexOf(midString) != -1) {
			var toTheLeft = temp.substring(0, temp.indexOf(midString));
			var toTheRight = temp.substring(temp.indexOf(midString)
					+ midString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
	} // Ends the check to see if the string being replaced is part of the
	// replacement string or not
	return temp; // Send the updated string back to the user
} // Ends the "replaceSubstring" function

function SetCookie(name, value, expires) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime(today.getTime());
	/*
	 * if the expires variable is set, make the correct expires time, the
	 * current script below will set it for x number of MINUTES, to make it for
	 * hours, delete * 24, for minutes, delete * 60 * 24
	 */
	if (expires) {
		expires = expires * 1000 * 60;
	}
	var expires_date = new Date(today.getTime() + (expires));

	document.cookie = name + "=\"" + escape(value) + "\""
			+ ((expires) ? ";expires=" + expires_date.toGMTString() : ";")
}

// this function gets the cookie, if it exists
function GetCookie(name) {
	var start = document.cookie.indexOf(name + "=");
	var len = start + name.length + 1;
	if ((!start) && (name != document.cookie.substring(0, name.length))) {
		return null;
	}
	if (start == -1)
		return null;
	var end = document.cookie.indexOf(";", len);
	if (end == -1)
		end = document.cookie.length;
	return unescape(document.cookie.substring(len, end));
}

// this deletes the cookie when called
function DeleteCookie(name) {
	if (GetCookie(name))
		document.cookie = name
				+ "= '' ? ; expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

