/* FONCTIONS POUR FORMULAIRE DE RÉSERVATIONS DES ENTÊTES DES PAGES */

function popUp(url,vwidth,vheight,scroll,name)
{
	var w = vwidth;
	var h = vheight;
	var x = (screen.width - w)/2;
	var y = (screen.height - h)/2;
	var s = scroll;
	var n = name;

	if (!name) {
		n = "remote";
	}
	
if ((s == 0) || (s == "") || (s == null)) {
	s = "no"
}
else {
	s = "yes"
}
	
popupWin = window.open(url, n,  'menubar=no,toolbar=no,location=no,directories=no,status=no,scrollbars='+s+',resizable=no,width='+w+',height='+h+',left='+x+',top='+y+'')
}

//Affichage des cartes de localisation des gares de VIA
/*	function StationMap(coords, addy) {
		var page = "/planner/en_plan_gare_popup.html?" + coords
//		/php/data2vars/getvars.php?action=read&lng=en&id=datamaps&code=ALDR
		var params = "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,screenX=10,screenY=10,width=500,height=550";
		window.open(page,'WinOpen',params) ;
	}*/
	
	//Affichage des cartes de localisation des gares de VIA
	function StationMap(coords, addy) {
		var page = coords
//		/php/data2vars/getvars.php?action=read&lng=en&id=datamaps&code=ALDR
		var params = "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,screenX=10,screenY=10,width=500,height=565";
		window.open(page,'WinOpen',params) ;
	}


// Pour faire popper une fenêtre
function PopWin(theURL,winName,features) {
	window.open(theURL,winName,features);
}

// Bug de Netscape pour le BR des années
function br(){
	if(document.layers){
		document.write("<BR>")
	}
}

function modifon(bouton){
	bouton.style.background='#C0C0C0';
	bouton.style.color='#333333';
	//this.style.background='#333333';this.style.color='#FFCC00';
}

function modifoff(bouton){
	//this.style.background='#C0C0C0';this.style.color='#333333';
	bouton.style.background='#000000';
	bouton.style.color='#FFCC00';
}


//mouseover script
<!--
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];}
}



/* FONCTIONS POUR FORMULAIRE DE RÉSERVATIONS DES ENTÊTES DES PAGES */

/* Pour sélectionner la date d'aujourd'hui dans le bon menu */
/*
function selectDate(){
	var sel_aujo = new Date();
	var sel_demain = new Date();
	sel_demain.setTime(sel_aujo.getTime() + 86400000);

//	document.f.departureDay.selectedIndex = sel_demain.getDate() - 1;
//	document.f.departureMonth.selectedIndex = sel_demain.getMonth() + 1;
	
	//document.f.returnDay.selectedIndex = sel_demain.getDate();
	//document.f.returnMonth.selectedIndex = sel_demain.getMonth() + 1;	

}
*/

/* Pour générer les OPTIONS des mois SUIVANT le mois courant */
function genererMois(champ) {
	var time = new Date() ;
	var month = time.getMonth() ;
	var date = month - 12 ;
	var realJavaScriptMonth = month ;
	var future = month + 0 ;
	month = month + 1 ;
	document.writeln ("<SELECT NAME=\"" + champ + "\" CLASS=\"ViaReserForm\">");
	do {
		month = date ;
		if (month >= 12)
		{
			month = month - 12;
		}
		if (month < 0)
		{
			month = month + 12;
		}
		date++;
		var dateName ="";
		switch (month)
		{
			case 0:
			dateName = "janvier";
			break;
			case 1:
			dateName = "février";
			break;
			case 2:
			dateName = "mars";
			break;
			case 3:
			dateName = "avril";
			break;
			case 4:
			dateName = "mai";
			break;
			case 5:
			dateName = "juin";
			break;
			case 6:
			dateName = "juillet";
			break;
			case 7:
			dateName = "août";
			break;
			case 8:
			dateName = "septembre";
			break;
			case 9:
			dateName = "octobre";
			break;
			case 10:
			dateName = "novembre";
			break;
			case 11:
			dateName = "décembre";
			break
		}
		month++ ;
		realJavaScriptMonth++ ;
		document.write ("<OPTION VALUE=\"" + realJavaScriptMonth + "\">" + dateName + "") ;
		realJavaScriptMonth++ ;
	}
	while (date < future)
	document.write ("</SELECT>");
}



/* Pour calculer l'annee de depart selon le mois choisi */
function makeArray() {
	this[0] = makeArray.arguments.length ;
	for (i = 0; i<makeArray.arguments.length; i++)
	this[i+1] = makeArray.arguments[i] ;
}

var accumulate    = new makeArray( 0,31,59,90,120,151,181,212,243,273,304,334) ;
var accumulateLY  = new makeArray( 0,31,60,91,121,152,182,213,244,274,305,335) ;

/* Pour calculer annees bissextiles */ 
function LeapYear(year) {
	if ((year/4)   != Math.floor(year/4))   return false;
	if ((year/100) != Math.floor(year/100)) return true;
	if ((year/400) != Math.floor(year/400)) return false;
	return true;
}

/* Pour obtenir le nombre de jours écoulés depuis le 1er janvier */
function getJulian(day,month,year) {
	if (LeapYear(year))
		return (day + accumulateLY[month]);
	else
		return (day + accumulate[month]);
}

/* Calcul des dates et années pour envoyer la bonne année */
function calculerEnvoyer(f) {
	var today = new Date() ;
	var ddjour = parseInt(f.departureDay.value) ;
	var ddmois = parseInt(f.departureMonth.value) ;
	var ddannee = today.getYear() ;
	var dd = getJulian(ddjour,ddmois,ddannee) ;
	var aujourdhui = getJulian(today.getDate(), today.getMonth() + 1, today.getYear() ) ;
	if ((dd > 0) && (dd < aujourdhui)) {
		var zeDate =  (ddannee + 1) ;
		var zeDateString =  zeDate + "" ;
		var dateCroppee = zeDateString.substring(2,4) ;
//		f.dda.value = dateCroppee ;
	} 
	else {
		var zeDate = today.getYear() ;
		var zeDateString =  zeDate + "" ;
		var dateCroppee =  zeDateString.substring(2,4) ;
//		f.dda.value = dateCroppee ;
	}
	return(false) ;
}

	function verifNombre(choix) {
		var totalPass = parseInt(document.f.cat_1.options[document.f.cat_1.options.selectedIndex].value) +
						parseInt(document.f.cat_2.options[document.f.cat_2.options.selectedIndex].value) +
						parseInt(document.f.cat_4.options[document.f.cat_4.options.selectedIndex].value) +
						parseInt(document.f.cat_5.options[document.f.cat_5.options.selectedIndex].value) +
						parseInt(document.f.cat_0.options[document.f.cat_0.options.selectedIndex].value) ;
		if ((choix==3) && (totalPass < 2)) {
			alert("The total number of tickets is " + totalPass + " when the total allowed\nfor double berths is 2.\n");
		}
		else
		if ((choix==4) && (totalPass < 3)) {
			alert("The total number of tickets is " + totalPass + " when the total allowed\nfor double berths is 3.\n");
		}
	}
	
	function Validate (f){
	
	with (f){
	
		if (departureMonth.selectedIndex == 0){
		alert ('Please choose a departure date (month) ...')
		departureMonth.focus();
		return(false);
		}

		if (departureDay.selectedIndex == 0){
		alert ('Please choose a departure date (Day) ...')
		departureDay.focus();
		return(false);
		}
		
		if ((departureStnCd.selectedIndex==0) || (destinationStnCd.selectedIndex==0)){
			alert('Please choose both Departure and Arrival stations ...');
			departureStnCd.focus();
			return(false);
			}	
			
		if ((numAdults.selectedIndex + numSeniors.selectedIndex + numStudents.selectedIndex + numChildren.selectedIndex) > 6) {
			alert('You have exceeded the maximum of 6 passengers.');
			return(false);
			}
			
		if ((numAdults.selectedIndex + numSeniors.selectedIndex + numStudents.selectedIndex + numChildren.selectedIndex) == 0) {
			alert('Please choose at least one passenger.');
			numAdults.focus();
			return(false);		
			}			
			
		if ((numAdults.selectedIndex + numSeniors.selectedIndex + numStudents.selectedIndex) == 0) {
			if (numChildren.selectedIndex > 0) {
				alert('Children must be accompanied by at least one adult.');
				return(false);				
				}				
			}
			
		if ((numAdults.selectedIndex == 0) && (numSeniors.selectedIndex == 0) && (numStudents.selectedIndex > 0) && (numChildren.selectedIndex > 0)){
			var agree = confirm("Children under 8 years of age must be accompanied by their mother or father (the mother or the father can be under 18 years of age) or by an ADULT (must be at least 18 years old).\n\nChildren between eight and eleven years of age can travel alone as long as they are under the watch and supervision of a VIA Rail employee until they are met by their mother or father, or by a designated ADULT (of at least 18 years of age).\n\nClick OK if you agree to these terms.\nClick Cancel to read VIA Rail's conditions.");
			if (agree) {
				numAdults.focus();
				return(true);			
				}
			else {
				popUp('/families/en_fami_enfa_noac.html',800,500,1);
				return false;
				}												
			}							
		}
	}
	
	
// sondage pop-under

// read cookie function
Cookies = {
	read : function(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 w = 500;
var h = 400;
var x = (screen.width - w)/2;
var y = (screen.height - h)/2;		
//var currentURI = ""+parent.document.location;	
var afficherSondage = Cookies.read("sondageVia2");

// ouvre sondage popup si cookie existe pas ET si ont est pas sur askvia.ca
/*
if ((!afficherSondage) && (currentURI.indexOf("askvia") < 0 )) {
	window.open('/en_sondage.html', 'sondage', 'menubar=no,toolbar=no,location=no,directories=no,status=yes,scrollbars=yes,resizable=yes,width='+w+',height='+h+',left='+x+',top='+y+'');
	window.focus();
}
*/

function viewStationInfo(f,selected)
{
	var link = f.xml.options[selected].value ;
	document.location.href = link;
	return true;
}
		

