//=========================================================
// :::::::::::::::  FONCTIONS GÉNÉRIQUES :::::::::::::::::
// // copyright nurun inc 2002 :::::::::::::::::::::::::::
//=========================================================

var myTmp = "";
var myTmpDeb = "";
var myTmpFin = "";
var chStr = "";
var chStrDeb = "";
var chStrFin = "";

function newChangeLangue(langid,lastid,strutsPage) {
var template = document.location.href;
var isSecur = 0;
var fin = "";
//Execption to directory /internet.ca/
var idx = template.lastIndexOf("/services/internet.ca");
if(idx != -1){
	debut  =  template.slice(0,idx+21);
	tmp    =  template.slice(idx+21,template.length);
	fin    =  langid+tmp.slice(4,tmp.length);
	document.location.href = (debut + fin);
	return;
}
//others case
langParam = ( langid == "/fr/" ) ? "FRENCH" : "ENGLISH";

var myDate = new Date();
var myLog = myDate;


if(template.lastIndexOf("/services/secur") == -1)
{
		isSecur = 0;
		debut  =  template.slice(0,template.lastIndexOf("/services/")+9);
		tmp    =  template.slice((template.lastIndexOf("/services/")+9),template.length);
}else {
		isSecur = 6;
		debut  =  template.slice(0,template.lastIndexOf("/services/secur/")+15);
		tmp    =  template.slice((template.lastIndexOf("/services/secur/")+15),template.length);
}


// Si pas de forward.do
if(template.lastIndexOf("Forward.do?to=") == -1){
		// S'il y a une page struts et que ce n'est pas une action, on l'utilise
		if(strutsPage != "" && strutsPage.lastIndexOf(".do") == -1) {
			if(strutsPage.lastIndexOf("/secur/") == -1) {
				tmp = strutsPage;
			}
			else {

				tmp = strutsPage.slice((strutsPage.lastIndexOf("/secur/") + 6),strutsPage.length);
			}
		}

		debut  += "/Forward.do?to=";
		if(isSecur == 6) {
			debut += "/secur";
		}

		if (!tmp.lastIndexOf(lastid)) {
			if (tmp.lastIndexOf(lastid) == 0){
				fin = langid + tmp.slice((tmp.lastIndexOf(lastid)+4),template.length) + "&lang=" + langParam;
			} else {
				fin = langid + tmp.slice((tmp.lastIndexOf(lastid)+4),template.length) + "&lang=" + langParam;
			}
		} else {
			if (tmp.lastIndexOf(lastid) == -1) {
				if (tmp.slice(1,template.length).indexOf("lang=FRENCH") != -1 || tmp.slice(1,template.length).indexOf("lang=FRENCH") != -1 )
				{
					chStr = tmp.slice(1,template.length);
					changeParam(chStr);
					if(myTmp.substring(0,1)== "/"){
						fin = myTmp;
					} else {
						fin = "/" + myTmp;
					}
				} else {
					fin = tmp.slice(1,template.length) + "&lang=" + langParam;
				}
			} else {
				if (tmp.slice(1,template.length).indexOf("lang=FRENCH") != -1 || tmp.slice(1,template.length).indexOf("lang=FRENCH") != -1 )
				{
					chStr = tmp.slice(1,template.length);
					changeParam(chStr);
					if(myTmp.substring(0,1)== "/"){
						fin = myTmp
					} else {
						fin = "/" + myTmp
					}
				}else {
					chStr = tmp.slice(1,template.length);
					changeParam(chStr);
					if(myTmp.substring(0,1)== "/"){
						fin = myTmp + "&lang=" + langParam;
					} else {
						fin = "/" + myTmp + "&lang=" + langParam;
					}
				}
			}
		}
} else {

		fin = "/Forward.do?to=";
		if(isSecur == 6) {
			fin += "/secur";
		}
		if (tmp.lastIndexOf(lastid) == -1)
		{
			fin += (tmp.slice(15+isSecur,tmp.lastIndexOf("&lang=")+6)) + langParam;
		} else {
		chStr = tmp.slice(1,template.length);
					changeParam(chStr);
					if(myTmp.substring(0,1)== "/"){
						fin = myTmp;
					} else {
						fin = "/" + myTmp;
					}
		}
}

	if(fin.substring(0,1)== "/"){
		fin = fin;
	} else {
		fin = "/" + fin;
	}

if (fin.indexOf("ShortcutLogin.do")!=-1)
	{
		chStrDeb = debut;
		chStrFin = fin;
		changeDeb(chStrDeb);
		changeFin(chStrFin);
		debut = myTmpDeb;
		if(myTmpFin.substring(0,1)== "/"){
			fin = myTmpFin;
		} else {
			fin = "/" + myTmpFin;
		}
	}

if (fin.indexOf("en/")!=-1 && fin.indexOf("FRENCH")!=-1)
	{
		var re = new RegExp ('lang=FRENCH', 'gi') ;
		var newstr = fin.replace(re, 'lang=ENGLISH') ;
		fin = newstr;
	}

if (fin.indexOf("fr/")!=-1 && fin.indexOf("ENGLISH")!=-1)
	{
		var re = new RegExp ('lang=ENGLISH', 'gi') ;
		var newstr = fin.replace(re, 'lang=FRENCH') ;
		fin = newstr;
	}

if (fin.indexOf("votre_compte/7.jsp")!=-1)
	{
		var re = new RegExp ('/secur', 'gi') ;
		var newstr = debut.replace(re, '') ;
		debut = newstr;
	}

	document.location.href = (debut + fin);

}

function changeParam() {
				var template =chStr;
				var strText = template;
				if (template.indexOf("fr/")!=-1)
				{
					var re = new RegExp ('fr/', 'gi') ;
					var newstr = strText.replace(re, 'en/') ;
					myTmp = newstr;
					return;
				}
				if (template.indexOf("en/")!=-1)
				{
					var re = new RegExp ('en/', 'gi') ;
					var newstr = strText.replace(re, 'fr/') ;
					myTmp = newstr;
					return;
				}
				if (template.indexOf("lang=FRENCH")!=-1)
				{
					var re = new RegExp ('lang=FRENCH', 'gi') ;
					var newstr = strText.replace(re, 'lang=ENGLISH') ;
					myTmp = newstr;
					return;
				}
				if (template.indexOf("lang=ENGLISH")!=-1)
				{
					var re = new RegExp ('lang=ENGLISH', 'gi') ;
					var newstr = strText.replace(re, 'lang=FRENCH') ;
					myTmp = newstr;
					return;
				}
}

function chopLang() {
				var template =chStr;
				var strText = template;

				if (template.indexOf("&lang=FRENCH")!=-1)
				{
					var re = new RegExp ('&lang=FRENCH', 'gi') ;
					var newstr = strText.replace(re, '') ;
					myTmp = newstr;
					return;
				}
				if (template.indexOf("&lang=ENGLISH")!=-1)
				{
					var re = new RegExp ('&lang=ENGLISH', 'gi') ;
					var newstr = strText.replace(re, '') ;
					myTmp = newstr;
					return;
				}
}

function changeDeb() {
				var template =chStrDeb;
				var strText = template;

				if (template.indexOf("/secur")!=-1)
				{
					var re = new RegExp ('/secur', 'gi') ;
					var newstr = strText.replace(re, '') ;
					myTmpDeb = newstr;
					return;
				}
}

function changeFin() {
				var template =chStrFin;
				var strText = template;

				if (template.indexOf("lang=ENGLISH")!=-1)
				{
					var re = new RegExp ('ShortcutLogin.do', 'gi') ;
					var newstr = strText.replace(re, 'en/votre_compte/7.jsp') ;
					myTmpFin = newstr;
					return;
				}

				if (template.indexOf("lang=FRENCH")!=-1)
				{
					var re = new RegExp ('ShortcutLogin.do', 'gi') ;
					var newstr = strText.replace(re, 'fr/votre_compte/7.jsp') ;
					myTmpFin = newstr;
					return;
				}
}


function changelangue(langid,lastid) {
	var template = document.location.href
	lang = ( langid == "/fr/" ) ? "FRENCH" : "ENGLISH";
	debut  =  template.slice(0,template.lastIndexOf("/services/")+9);
	tmp    =  template.slice((template.lastIndexOf("/services/")+9),template.length);
	// Si pas de forward.do
	if(template.lastIndexOf("Forward.do?to=") == -1) {
		debut  += "/Forward.do?to=";
		if (!tmp.lastIndexOf(lastid)) {
			fin = langid + tmp.slice((tmp.lastIndexOf(lastid)+4),template.length) + "&lang=" + lang
		}
		else {
			if (tmp.lastIndexOf(lastid) == -1) {
				fin = tmp.slice(1,template.length) + "&lang=" + lang
				}
			else fin = langid + tmp.slice(1,template.length) + "&lang=" + lang
		}
	}
	else {
		if (tmp.lastIndexOf(lastid) == -1) fin = "/Forward.do?to=" + (tmp.slice(15,tmp.lastIndexOf("&lang=")+6)) + lang
		else fin = "/Forward.do?to=" + langid + (tmp.slice(19,tmp.lastIndexOf("&lang=")+6)) + lang
	}
	document.location.href = (debut + fin);
}

function swap(id,isOn,lang) {
	if (isOn == "on") return;
	state = (document["n" + id].src.indexOf("_on") == -1) ? "_on" : "_off" ;
	document["n" + id].src = "/services/static/" + lang + "/img/nav" +  state + "_" + id  +".gif"
}

function pop(url,winname,w,h,sc){
	// Nous nommons la fenetre courrante pour pouvoir la rediriger
	//     pour rediriger l'opener vers un nouveau url faire comme suit:
	//     <a href="http://www.videotron.com" target="opener" onClick="closeWindow()">Videotron</a>
	self.name = "opener"; // names current window as "opener"
	if (sc==1){w+=16}
	var winleft = (screen.width - w) / 2;
	var wintop = (screen.height - h) / 2;
	pfWin = window.open(url, winname, 'width='+w+',height='+h+',left='+winleft+',top='+wintop+',screenX=0,screenY=0,directories=0,resizable=1,location=0,menubar=0,scrollbars='+sc+',status=0,toolbar=0');
	pfWin.focus();
}

function closeWindow() {
	timer = setTimeout('window.close();', 10);
}

function printMe() {
	window.focus();
	window.print();
}

function goopage(page) {
	opener.location.href=page
	window.close();
}

function checkURL() {
	var idx = document.URL.indexOf('?');
	var truc = document.URL.substring(idx+1, document.URL.length);
	return truc;
}

//Moving from detectflash.jsp
function getCookie(Name)  {
	var search = Name + "="
	if (document.cookie.length > 0) {
	offset = document.cookie.indexOf(search)
		if (offset != -1) {
		offset += search.length
		end = document.cookie.indexOf(";", offset)
			if (end == -1)
            end = document.cookie.length
    		return unescape(document.cookie.substring(offset, end))
		}
	}
}
function setCookieValue(value) {
	var today = new Date()
	var expires = new Date()
	expires.setTime(today.getTime() + 1000*60*60*24*365)
	setCookie("isFlash", value, expires);
}

function setCookie(name, value, expires) {
	document.cookie = name + "=" + escape(value) + ((expires == null) ? "" : ("; expires=" + expires.toGMTString())) + "; path=/";
}



function DetectFlash6() {
	HasFlash = false;
	if (navigator.mimeTypes) {
		if (navigator.mimeTypes.length > 0) {
			for (i = 0 ; i < navigator.plugins.length ; i++ ) {
				if (navigator.plugins[i].name.indexOf('Shockwave Flash') >= 0) {
					//if (navigator.plugins[i].description.indexOf('6.') >= 0) HasFlash = true ;
					if (navigator.plugins[i].description.indexOf('7.') >= 0) HasFlash = true ;
				}
			}
		}
	}

	// write vbscript detection if we're not on mac.
	if((navigator.appVersion.indexOf("MSIE") != -1) && (navigator.appVersion.indexOf("Windows") != -1)){ // don't write vbscript tags on anything but ie win
		document.write('<SCR' + 'IPT LANGUAGE=VBScript\> \n');
		document.write('on error resume next \n');
		//document.write('HasFlash = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.6"))) \n');
		document.write('HasFlash = (IsObject(CreateObject("ShockwaveFlash.ShockwaveFlash.7"))) \n');
		document.write('</SCR' + 'IPT\> \n'); // break up end tag so it doesn't end script
	}
}
