//Globals for incremetal form validations
i = 0;
j = 0;
FormCount = 0;

//Global variables for domain mappings
var DomainPrefix = "http://www.superchoix.com/images/";
var imgDomainPrefix = "http://www.superchoix.com/media/";

/* DREAMWEAVER MOUSEOVERS */
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

/* ADD BOOKMARK */
function addbookmark(url,title)
{
	if (document.all) 
		window.external.AddFavorite(url,title);
}

/* PRINT WINDOW */
function printWindow(){
	bV = parseInt(navigator.appVersion)
	if (bV >= 4) window.print()
}

/* QUICKNAV ONCHANGE SELECT */
function getLoc(page) { if (page != "") { self.location=page; } }

/* LOAD PAGE (transparent "password" switch to dir)*/
function loadpage()
{
	var page = "/extranet/" + document.frm.pwd.value + "/";
	if ((document.frm.pwd.value != " ") && (document.frm.pwd.value != ""))
	{
		document.location.href = page;
	}
}

/* POPUP WINDOWS */
  var popWin = null    // use this when referring to pop-up window
  var winCount = 0
  var winName = "popWin"

  function FullPop(winURL)
  {
	window.open(winURL, '', 'fullscreen=yes, scrollbars=1');
  }

  function LargePop(winURL)
  {
    winWidth = 700;
	winHeight = 300;
	winFeatures = 'menubar=0,location=0,resizable=1,scrollbars=1,status=0,titlebar=0,toolbar=0,directories=0';
	winLeft = 20;
	winTop = 20;
    var d_winLeft = 20  // default, pixels from screen left to window left
    var d_winTop = 20   // default, pixels from screen top to window top
    winName = "popWin" + winCount++ //unique name for each pop-up window
    closePopWin()		   // close any previously opened pop-up window
    winFeatures += getLocation(winWidth, winHeight, d_winLeft, d_winTop)
    popWin = window.open(winURL, winName, "width=" + winWidth + ",height=" + winHeight + winFeatures)
  }

  function MedPop(winURL)
  {
    winWidth = 500;
	winHeight = 300;
	winFeatures = 'menubar=0,location=0,resizable=1,scrollbars=1,status=0,titlebar=1,toolbar=0,directories=0';
	winLeft = 20;
	winTop = 20;
    var d_winLeft = 20  // default, pixels from screen left to window left
    var d_winTop = 20   // default, pixels from screen top to window top
    winName = "popWin" + winCount++ //unique name for each pop-up window
    closePopWin()		   // close any previously opened pop-up window
    winFeatures += getLocation(winWidth, winHeight, d_winLeft, d_winTop)
    popWin = window.open(winURL, winName, "width=" + winWidth + ",height=" + winHeight + winFeatures)
  }

  function SmallPop(winURL)
  {
    winWidth = 300;
	winHeight = 300;
	winFeatures = 'menubar=0,location=0,resizable=1,scrollbars=1,status=0,titlebar=1,toolbar=0,directories=0';
	winLeft = 20;
	winTop = 20;
    var d_winLeft = 20  // default, pixels from screen left to window left
    var d_winTop = 20   // default, pixels from screen top to window top
    winName = "popWin" + winCount++ //unique name for each pop-up window
    closePopWin()		   // close any previously opened pop-up window
    winFeatures += getLocation(winWidth, winHeight, d_winLeft, d_winTop)
    popWin = window.open(winURL, winName, "width=" + winWidth + ",height=" + winHeight + winFeatures)
  }

  function closePopWin()
  {    // close pop-up window if it is open
    if (navigator.appName != "Microsoft Internet Explorer" || parseInt(navigator.appVersion) >=4) // do not close if early IE
    if (popWin != null) if(!popWin.closed) popWin.close()
  }

  function getLocation(winWidth, winHeight, winLeft, winTop)
  {
    var winLocation = ""
    if (winLeft < 0)
     winLeft = screen.width - winWidth + winLeft
    if (winTop < 0)
     winTop = screen.height - winHeight + winTop
    if (winTop == "cen")
     winTop = (screen.height - winHeight)/2 - 20
    if (winLeft == "cen")
     winLeft = (screen.width - winWidth)/2
    if (winLeft>0 & winTop>0)
     winLocation =  ",screenX=" + winLeft + ",left=" + winLeft + ",screenY=" + winTop + ",top=" + winTop
    else
    winLocation = ""
    return winLocation
  }

/* NUMBERING == PAD */
function ajouteZero(s) // adds zeros to digits under 10
{
 return((s<10)?"0":"") + s
}

/* DIAPORAMA */
var nimages = 9;
var placeholders = 3;
var mesImages = new Array();
var position = 0;

for (i =0; i <nimages; i++)
{
	mesImages[i] = new Image(200,129);
	// mesImages[i].src = "images/a_splash_"+ ajouteZero(i) +".jpg";
}


function dynAfficheImage(i)
{
	document.pic.src = mesImages[i].src;
}

function diaporama()
{
	position  = (position + 1) % nimages;
	dynAfficheImage(position);
	setTimeout("diaporama()",3000);
}

function diapoSuivante(position)
{
	position = (position+1)%nimages;
	dynAfficheImage(position);
}

function diapoPrecedente()
{
	position = (position -1 + nimages)%nimages;
	dynAfficheImage(position);
}

/* VALIDATE EMAIL ADRESSES IN FORMS */
function ValidFormMail()
{
	if ((document.FormMail.realname.value != "") &&
		(document.FormMail.email.value != "") &&
		(document.FormMail.message.value != "") &&
		(document.FormMail.Telephone_bureau.value != "") &&
		(document.FormMail.realname.value != " ") &&
		(document.FormMail.email.value != " ") &&
		(document.FormMail.message.value != " ") &&
		(document.FormMail.Telephone_bureau.value != " ")
	   )
	{
		if ((document.FormMail.email.value.indexOf('.',0) == -1) ||
			(document.FormMail.email.value.indexOf('@',0) == -1)
		)
		{
			alert("Format de courrier ?lectronique non-valide.\nInvalid Email format");
			return false;
		}
		else
		{
			return true;
		}
	}
	else
	{
		sMsg	= "Veuillez remplir les champs obligatoires avant de soumettre.\n"
				+ "Please fill all required fields before submitting";
		alert(sMsg);
		return false;
	}
}

/* ADJUST DATE DROPDOWNS */
function dropDownSelect(select_name,option_name)
{
	select_dropdown = eval ( 'document.FormSoum1.' + select_name );
	for (var i = 0; i<select_dropdown.length; i++)
	{
		if (select_dropdown.options[i].text == option_name)
		{
			select_dropdown.selectedIndex = i;
		}
	}
}

function AdjustDate()
{
	var aMonthName = new Array ("Jan","Fev","Mar","Avr","Mai","Juin","Juil","Ao?t","Sept","Oct","Nov","Dec");
	var aujourdhui =  new Date();
	var iJourNombre = ajouteZero(aujourdhui.getDate());
	var sMois  = aMonthName[aujourdhui.getMonth()];
	var sMois2  = aMonthName[aujourdhui.getMonth()+1];
	var iAnnee  = aujourdhui.getYear();
	dropDownSelect('start_year',iAnnee);
	dropDownSelect('start_month',sMois);
	dropDownSelect('start_day',iJourNombre);
	dropDownSelect('end_year',iAnnee);
	dropDownSelect('end_month',sMois2);
	dropDownSelect('end_day',iJourNombre);
}

function ClearDate()
{
	dropDownSelect('start_year','');
	dropDownSelect('start_month','');
	dropDownSelect('start_day','');
	dropDownSelect('end_year','');
	dropDownSelect('end_month','');
	dropDownSelect('end_day','');
}

function ConcatDate()
{
	sOrigDate = document.FormSoum1.start_year.value + document.FormSoum1.start_month.value + document.FormSoum1.start_day.value;
	document.FormSoum1.O_DATE.value = sOrigDate;
	sDestDate = document.FormSoum1.end_year.value + document.FormSoum1.end_month.value + document.FormSoum1.end_day.value;
	document.FormSoum1.D_DATE.value = sDestDate;
}

/* FORMATING TEXT */
// Alphanumeric & uppercase
function f(o){
  o.value=o.value.toUpperCase().replace(/([^0-9A-Z\' '._-])/g,"");
}
function fNum(o){
  o.value=o.value.replace(/([^0-9\' 'A-Z._-])/g,"0");
}
function g(o){
  if(/[^0-9A-Z]/.test(o.value)){
    // o.value=o.value.toUpperCase().replace(/([^0-9A-Z\.])/g,"");
	o.value=o.value.toUpperCase().replace(/([^0-9A-Z\' '._-])/g,"");
  }
}

// 	Currency
function formatCurrency(strValue)
{
	strValue = strValue.toString().replace(/\$|\,/g,'');
	dblValue = parseFloat(strValue);

	blnSign = (dblValue == (dblValue = Math.abs(dblValue)));
	dblValue = Math.floor(dblValue*100+0.50000000001);
	intCents = dblValue%100;
	strCents = intCents.toString();
	dblValue = Math.floor(dblValue/100).toString();
	if(intCents<10)
		strCents = "0" + strCents;
	for (var i = 0; i < Math.floor((dblValue.length-(1+i))/3); i++)
		dblValue = dblValue.substring(0,dblValue.length-(4*i+3))+','+
		dblValue.substring(dblValue.length-(4*i+3));
	return (((blnSign)?'':'-') + '$' + dblValue + '.' + strCents);
}

// Postal code
function fcodepostal(o)
{
	if (o.value.length >= 6)
	{
		var t = o.value.toUpperCase().replace(/([^0-9A-Z\' '._-])/g,"");
		if (isPostCode(t))
		{
			o.value = o.value.substring(0,3) + ' ' + o.value.substring(3,o.value.length);
		}
		o.value = o.value.toUpperCase().replace(/([^0-9A-Z\' '._-])/g,"");
	}
	else
	{
		var sMsg = "Code postal non conforme";
		alert(sMsg);
	}
}

function isPostCode(entry)
{
	strlen = entry.length; if (strlen != 6) {return false;}
	entry=entry.toUpperCase();
	if ('ABCEHJKLMNPRSTVXY'.indexOf(entry.charAt(0)) < 0) {return false;}
	if ('0123456789'.indexOf(entry.charAt(1)) < 0) {return false;}
	if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(2)) < 0) {return false;}
	if ('0123456789'.indexOf(entry.charAt(3)) < 0) {return false;}
	if ('ABCDEFGHJKLMNPQRSTUVWXYZ'.indexOf(entry.charAt(4)) < 0) {return false;}
	if ('0123456789'.indexOf(entry.charAt(5)) < 0) {return false;}
	return true;
}

function ftelephone(o)
{
	// o.value=o.value.toUpperCase().replace(/([^0-9A-Z\.])/g,"");
	o.value=o.value.toUpperCase().replace(/([^0-9\' '._])/g,"-");
}





/* GET PARAMS FOR MUTLIPLE STEP FORM DATA GATHERING */
function showElements(theForm) {
	str = "Form Elements of form " + theForm.name + " <br>\n";
	for (var FormCount = 0; FormCount < theForm.length; FormCount++)
	{
		str += theForm.elements[FormCount].name + ": " + theForm.elements[FormCount].value + " <br>\n"
	}
	return(str);
}

function getFormParams(theForm)
{
	sMsg = '';
	var sUrl = document.location.search;
	if (sUrl)
	{
		iUrlCount = 0;
		var iLastCharPos = sUrl.indexOf("&submit");
		var sFullString = sUrl.substring("?",iLastCharPos);
		var sGetString = sFullString.substring(1,iLastCharPos);
		var aUrl = sGetString.split("&");
		var iUrlLength = aUrl.length;
		for (iUrlCount =0; iUrlCount < iUrlLength; iUrlCount++)
		{
			var sParamAndValue = aUrl[iUrlCount];
			var sSep = sParamAndValue.indexOf("=");
			var sFieldName = sParamAndValue.substring(0,sSep);
			var sFieldValue = sParamAndValue.substring(sSep+1);

			sMsg += "\n sFieldName: " + sFieldName;
			sMsg += "\n sFieldValue: " + sFieldValue;

			for (j = 0; j < theForm.length; j++)
			{
				Item = theForm.elements[j];
				ItemType = (Item.type);
				if (ItemType == 'hidden')
				{

					ItemName = (Item.name);
					if (ItemName == sFieldName)
					{
						sMsg += "\n ItemName: " + ItemName;
						theForm.elements[j].value = sFieldValue;
					}
				}
			}
		}
		// alert(sMsg);
	}
}

function getLocationParams(sExtract)
{
	var sFieldValue = '';
	var sFieldName = '';
	var sParamAndValue  = '';
	var sSep  = '';
	var sUrl = document.location.search;
	if (sUrl)
	{
		iUrlCount = 0;
		var iLastCharPos = sUrl.length+1;
		var sFullString = sUrl.substring("?",iLastCharPos);
		var sGetString = sFullString.substring(1,iLastCharPos);
		var aUrl = sGetString.split("&");
		var iUrlLength = aUrl.length;
		for (iUrlCount =0; iUrlCount < iUrlLength; iUrlCount++)
		{
			sParamAndValue = aUrl[iUrlCount];
			sSep = sParamAndValue.indexOf("=");
			sFieldName = sParamAndValue.substring(0,sSep);
			sFieldValue = sParamAndValue.substring(sSep+1);
			if (sFieldName == sExtract)
			{
				return sFieldValue; // + "?3407a3c1c111937a153fdbc976553664";
			}
		}
	}
}


// EXPANDING TEXT BLOCKS

content = new Array ();

content [0] = new Array (
false,
new Array('sub_0_1')
);
content [1] = new Array (
false,
new Array('sub_1_1')
);
content [2] = new Array (
false,
new Array('sub_2_1')
);
content [3] = new Array (
false,
new Array('sub_3_1')
);

isOPERA = (navigator.userAgent.indexOf('Opera') >= 0)? true : false;
isIE    = (document.all && !isOPERA)? true : false;
isDOM   = (document.getElementById && !isIE && !isOPERA)? true : false;

function processTree(id){
	if (content [id][0]){
		for (i = 0; i < content [id][1].length; i++)
			hide (content [id][1][i]);
		content [id][0] = false;
	}else{
		for (i = 0; i < content [id][1].length; i++)
			show (content [id][1][i], 'table-row');
		content [id][0] = true;
	}
	return false;
}

function setVisibility(id,value)
{
	if (isDOM)
		document.getElementById(id).style.visibilty = value;
	else if (isIE)
		document.all[id].style.display = value;
}

function show(id,displayValue){
	if (isDOM)
		document.getElementById(id).style.display = (displayValue)? displayValue : "block";
	else if (isIE)
		document.all[id].style.display = "block";
}

function hide(id){
	if (isDOM)
		document.getElementById(id).style.display = "none";
	else if (isIE)
		document.all[id].style.display = "none";
}

if(isDOM||isIE){
	document.writeln('<style type="text/css">');
	document.writeln('.SubItemRow \{ display: none; \}');
	document.writeln('</style>');
}

function notReady(){
	alert("Sorry, the information you just requested is not yet ready. Please check back later.");
}

// CHOIX SELECT TO SELECT
function ajouter(bTout)
{

	var	f = document.forms[0];
	var optionSource = f.source;
	var optionChoix = f.idDestinataire;
	for (var i =0; i < optionSource.options.length; i++)
	{
		if (optionSource.options[i].selected==true || bTout)
		{
			if(!contient(optionChoix,optionSource.options[i].value))
				optionChoix.options[optionChoix.options.length]=new Option(optionSource.options[i].text,optionSource.options[i].value);
		}

	}
}

function contient(o,v)
{
	for (var i =0; i < o.options.length; i++)
	{
		if (o.options[i].value==v) return true;
	}
	return false;
}

function enlever(bTout)
{
	var	f = document.forms[0];
	var optionChoix = f.idDestinataire;

	for (var i =0; i <optionChoix.options.length; i++)
	{
		if (optionChoix.options[i].selected==true ||  bTout)
		{
			optionChoix.options[i]=null;
			i--;
		}
	}
}
function selectAll()
{
	var	f = document.forms[0];
	var optionChoix = f.idDestinataire;

	for (var i =0; i <optionChoix.options.length; i++)
	{
		optionChoix.options[i].selected=true;
	}
}

// @deprecated view switchAllId
function switchAll(f,grp)
{
	for(j=0; j < f.elements.length; j++)
	{
		name = f.elements[j].name;
		if (name==grp)
		{
			f.elements[j].checked = f[grp+"All"].checked;	
		}
	}
}

function isdefined(variable)
{
	return (typeof(variable) == "undefined")?  false: true;
}

function switchAllId(grp)
{
	var i=0;
	var checked = document.getElementById(grp+'All').checked;
	try{
		while(isdefined(document.getElementById(grp+i)))
		{
			elt = document.getElementById(grp+i).checked = checked;
			i++;		
		}
	}catch(e){}
}

/* STYLE SHEET SWITCHER */

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);


// fonctions pour conversion de montants

function formaterMontant(num)
{
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num))
	num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.floor(num/100).toString();
	if(cents<10)
	cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+' '+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '' + num + ',' + cents);
}

function getMontant(strMontant)
{

	m = replaceAll(strMontant," ","");
	m = replaceAll(m,"$","");
	m = replaceAll(m,",",".");

	return(m);
}

function replaceAll(chaine,a,b)
{
	while(chaine.indexOf(a)!=-1)
	{
		chaine = chaine.replace(a,b);
	}
	return(chaine);
}

function getObjet(id)
{
	if (isDOM)
		return(document.getElementById(id));
	else if (isIE)
		return(document.all[id]);
}

function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	
	if(TRIM_VALUE==""){
		return "";
	}
	else{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;
	
	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;	
	} //End While
	return strTemp;
	
} //End Function

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	
	var v_length = VALUE.length;
	var strTemp = "";
	
	var iTemp = 0;
	
	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}
		else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function


/* FoncCnx */
function changerVisibilite(id){
	var etat = document.getElementById(id).style.display;
	var nouveauEtat = 'none';
	if (etat==nouveauEtat) {
	  nouveauEtat='inline';	
	}
	document.getElementById(id).style.display=nouveauEtat;
}