/* 
   all scripts written or modified 
	 from the original code by Richard Naud
	 copyrights 2003 J.R.Perreault & fils	 
*/

function swapReducer( IDreducer, which )
{
   if(document.images)
	 {
     if( which == 'TRUE')
        document.reducer.src= '../files/pics/reducers/'+IDreducer+'.jpg';
     else
  	 		document.reducer.src='../files/pics/blank.gif';
	 }
}

function swapInline( IDreducer, which )
{
   if(document.images)
	 {
     if( which == 'TRUE')
        document.inline.src= '../files/pics/reducers/'+IDreducer+'.jpg';
     else
  	 		document.inline.src='../files/pics/blank.gif';
	 }
}

function swapMotor( IDmotor, which )
{
   if(document.images)
	 {
     if( which == 'TRUE')
        document.motor.src= '../files/pics/motors/'+IDmotor+'.jpg';
     else
  	 		document.motor.src='../files/pics/blank.gif';
	 }
}
function swapMedium( IDmotor, which )
{
   if(document.images)
	 {
     if( which == 'TRUE')
        document.medium.src= '../files/pics/motors/'+IDmotor+'.jpg';
     else
  	 		document.medium.src='../files/pics/blank.gif';
	 }
}

/*
   function to load images while the welcome page
	 is displayed so the navigation is smoother after
*/
function preLoadImages()
{
  if(document.images)
	{
	  var image0 = new Image();
	  var image1 = new Image();
		var image2 = new Image();
		var image3 = new Image();
		var image4 = new Image();
		var image5 = new Image();
		var image6 = new Image();
		var image7 = new Image();
		var image8 = new Image();
		var image9 = new Image();
		var image10 = new Image();
		var image11 = new Image();
		
		image0.src = "../files/pics/corner.gif";
		image1.src = "../files/pics/gradient_left.gif";
		image2.src = "../files/pics/gradient_top.gif";
		image3.src = "../files/pics/left_line.gif";
		image4.src = "../files/pics/left_line_grad_top.gif";
		image5.src = "../files/pics/left_line_grad_bottom.gif";
		image6.src = "../files/pics/logo_general.gif";
		image7.src = "../files/pics/top_line.gif";
		image8.src = "../files/pics/top_line_grad_left.gif";
		image9.src = "../files/pics/top_line_grad_right.gif";
		image10.src = "../files/pics/anim_fr.gif";
		image11.src = "../files/pics/anim_en.gif";
	}
}

// function to set the window.status message
// function setStatusMessage( winSource )
// {
//   var msgString;
// 	var mySource = winSource;
//   if( mySource == 'index' )
//     msgString='www.jrperreault.com - choose your language - choisissez votre language';
// 	else if( mySource == 'fr' )
// 	   msgString='J.R.Perreault & fils -  Service 24 heures au 1-800-463-0400';
// 	else if( mySource == 'en' )
// 	   msgString='J.R.Perreault & sons -  24 hours service at 1-800-463-0400';
// 	// display the msgString in Loop
// 	window.status = msgString;
// 	var myLoop = window.setTimeout("setStatusMessage('"+mySource+"')",100);
// }

// function to pop-up the SlideShow in the desired language
function slideShow( lang )
{
  window.open('../files/present_' + lang + '.pps','slide','scrollbars=no,resizable=yes,toolbar=no,menubar=no,status=no,location=no,left=0,top=0,width='+ (screen.width-10) +',height='+ (screen.height-50));
}

/*
/* --------------------------------------------------------------
   cookie setting code:
	 - reads if there is a cookie already set
	   redirect to the language set according to this cookie.
	 - do not redirect if there is no cookie already set.
	 - pop-up the cookie dialog page to let the user choose
	   if he wants a cookie recording his language on his puter
		 and then write the cookie and save it.
---------------------------------------------------------------- */

// function to write cookies
function setTheCookie( thislang, from_lang )
{
    var expiredate = new Date();
		var chosen_lang = thislang;
		
		if(chosen_lang != '')
		{
  	  expiredate.setTime( expiredate.getTime() + (60 * 24 * 60 * 60 * 1000) );	//set expiration date to 60 days from now
      var this_cookie = "jrperreault=" + escape("setlang:" + chosen_lang )+ ';path=/;expires=' + expiredate;
      document.cookie = this_cookie;
		}
		else
		{
		  var this_cookie = "jrperreault=" + escape("setlang:" + chosen_lang )+ ";path=/";
			document.cookie = this_cookie;
		}
		
		// alert to the user what was done and will happens next times
    if( thislang == 'fr')
		{
		  window.alert('Lors de vos visites, pour les 60 prochains jours,\nvous arriverez directement sur cette page.');
			window.close();
		}
		else if( thislang == 'en')
		{ 
		  window.alert('When you\'ll visit, in the next 60 days,\nyou\'ll come directly to this page.');
			window.close();
		}
		else if( thislang == 'nopref' && from_lang == 'fr')
		{
		  window.alert('Nous ne vous demanderons pas d\'enregistrer\nde préférence de langage pour 60 jours.'); 
		  window.close();
		}
		else if( thislang == 'nopref' && from_lang == 'en')
		{
		  window.alert('We won\'t ask you to save any language\n preference for 60 days.'); 
		  window.close();
		}
}
// function to read the cookie
function getTheCookie()
{
  if(document.cookie)
	{
    var this_cookie = document.cookie;							 // refer to the document cookie object
    var this_cookie = unescape(this_cookie);				 // take out any special chars
    var splitted_cookie = this_cookie.split(':');		 // split the cookie paired with :
    var chosen_lang = splitted_cookie[1];						 // take the second part of the splitted value
		if(chosen_lang == 'en' || chosen_lang == 'fr')
		{
      var redir_page = chosen_lang +'/';							 // set the page to redirect to based on the language
		  window.setTimeout(redirLang(redir_page),2000); //set a 2 seconds timeout and call the redir function after this time
    }
		else
		{
		  return chosen_lang;
		}
	}
	else { return 'no_cookie'}
}

// function to pop-up the cookie setting dialog window
function setPrefLang( thislang )
{
	  var no_cookie_set = getTheCookie();
		if( no_cookie_set == 'no_cookie' || no_cookie_set == '')
		{ 
	    window.open( thislang +'/cookie.html','preflang','scrollbars=no,resizable=no,toolbar=no,menubar=no,status=no,location=no,left=100,top=50,width=500,height=500' );
    }
}

// function to redirect according to the cookie's language value
function redirLang( open_page )
{
  window.clearTimeout();		 			// clear the window timeout
	window.location = open_page;		// open the redirected page
}

// function to clear the cookie
function clearTheCookie( thislang )
{
  if( thislang == 'fr')
	{
    var usr_ans = window.confirm('Désirez-vous réellement effacer votre préférence de language?\nSi oui, vous arriverez sur la page d\'accueil qui offre \nle choix de language lors de la prochaine visite.');
		if(usr_ans == true)
		{
		  setTheCookie('','fr');
		}
	}
	else if( thislang == 'en')
	{
	  var usr_ans = window.confirm('Do you really want to clear you language preference?\nIf so, you\'ll get to the welcome page offering a language \nselection the next time you visit the site.');
		if(usr_ans == true)
		{
		  setTheCookie('','en');
		}
	}
}
/* ------------  end of the cookies code -------------------*/

/*
/* -------------------------------------

  functions for the reducers output ranges 
	originally written by ?
	re-written by Richard Naud
	richard-naud@sympatico.ca
	
-------------------------------------------*/

// function to pop-up the reducer calculation page
function popGearboxTest()
{
  window.open('calcul.html','gearbox','scrollbars=yes,resizable=no,toolbar=no,menubar=no,status=no,location=no,left=0,top=0,width=700,height=590');
}

// function to print the results
function startPrinting()
{
  if(document.calcul_gearbox.txtSortie.value!="" && document.calcul_gearbox.txtSize.value!="")
	{
	  window.print();
	}
}

// angle reducer radio button click
function rdoAngle_onclick()
{
  document.calcul_gearbox.rdoInline.checked=false;
	document.calcul_gearbox.rdoAngle.checked=true;
	document.calcul_gearbox.txtFacteur.value=1.25;
	//document.calcul_gearbox.txtError.value="";
	if(document.calcul_gearbox.selRatio.value>=65 && document.calcul_gearbox.rdoInline.checked==false)
	{
	  window.alert('Ratio trop grand pour les autres valeurs.\n Maximum : 60\nLe formulaire sera réinitialisé.');
	  document.calcul_gearbox.cmdClear.click();
		document.calcul_gearbox.rdoAngle.focus();
		//document.calcul_gearbox.txtError.value="Ratio trop grand(max. 60)"
	}
  /*if(document.calcul_gearbox.txtError.value != "")
	{
	  document.calcul_gearbox.cmdCalcul.disabled=true
	}
  else
	{
	  document.calcul_gearbox.cmdCalcul.disabled=false
	}*/
}

// inline reducer radio button click
function rdoInline_onclick()
{
  document.calcul_gearbox.rdoAngle.checked=false;
  document.calcul_gearbox.rdoInline.checked=true;
  if(document.calcul_gearbox.selRatio.value==5)
	{
	  document.calcul_gearbox.txtFacteur.value=2.5
	}
  else
	{
	  document.calcul_gearbox.txtFacteur.value=1.7
	}
  //document.calcul_gearbox.txtError.value="";
  if(document.calcul_gearbox.selRatio.value>=175 && document.calcul_gearbox.rdoInline.checked==true)
	{
	  window.alert('Ratio trop grand pour les autres valeurs.\n Maximum : 150\nLe formulaire sera réinitialisé.');
	  document.calcul_gearbox.cmdClear.click();
		document.calcul_gearbox.rdoAngle.focus();
		//document.calcul_gearbox.txtError.value="Ratio trop grand(max. 150)"
	}
  /*if(document.calcul_gearbox.txtError.value != "")
	{ 
	  document.calcul_gearbox.cmdCalcul.disabled=true
	}
  else
	{ 
	  document.calcul_gearbox.cmdCalcul.disabled=false
	}*/
}

// ratio selection dropdown click
function selRatio_onclick()
{
  if(document.calcul_gearbox.rdoInline.checked==true)
  {
    if (document.calcul_gearbox.selRatio.value==5)
		{
		  document.calcul_gearbox.txtFacteur.value=2.5
		}
 		else
		{
		  document.calcul_gearbox.txtFacteur.value=1.7
		}
	}
  //document.calcul_gearbox.txtError.value="";
  if(document.calcul_gearbox.selRatio.value>=65 && document.calcul_gearbox.rdoInline.checked==false)
	{
	  window.alert('Ratio trop grand pour les autres valeurs.\n Maximum : 60\nLe formulaire sera réinitialisé.');
	  document.calcul_gearbox.cmdClear.click();
		document.calcul_gearbox.rdoAngle.focus();
		//document.calcul_gearbox.txtError.value="Ratio trop grand(max. 60)"
	}
  document.calcul_gearbox.RPMsortie.value=Math.round(document.calcul_gearbox.txtRPM.value/document.calcul_gearbox.selRatio.value);
  /*if(document.calcul_gearbox.txtError.value != "")
	{
	  document.calcul_gearbox.cmdCalcul.disabled=true
	}
  else
	{
	  document.calcul_gearbox.cmdCalcul.disabled=false
	}*/
}

// motor input RPM text changed
function txtRPM_onchange()
{
  document.calcul_gearbox.RPMsortie.value=Math.round(document.calcul_gearbox.txtRPM.value/document.calcul_gearbox.selRatio.value);
  /*if(document.calcul_gearbox.txtError.value != "")
	{
	  document.calcul_gearbox.cmdCalcul.disabled=true
	}
  else
	{
	  document.calcul_gearbox.cmdCalcul.disabled=false
	}*/
}

// reducer output RPM text changed
function RPMsortie_onchange()
{
  var temp=0;
  //document.calcul_gearbox.txtError.value="";
  if(document.calcul_gearbox.RPMsortie.value!=0 && document.calcul_gearbox.RPMsortie.value!='')
  {
    temp= document.calcul_gearbox.txtRPM.value/document.calcul_gearbox.RPMsortie.value;
		if (temp>=2.5 && temp <7.5) {document.calcul_gearbox.selRatio.value=5}
		if (temp>=7.5 && temp <12.5) {document.calcul_gearbox.selRatio.value=10}
		if (temp>=12.5 && temp <17.5) {document.calcul_gearbox.selRatio.value=15}
		if (temp>=17.5 && temp <25) {document.calcul_gearbox.selRatio.value=20}
		if (temp>=25 && temp <35) {document.calcul_gearbox.selRatio.value=30}
		if (temp>=35 && temp <45) {document.calcul_gearbox.selRatio.value=40}
		if (temp>=45 && temp <55) {document.calcul_gearbox.selRatio.value=50}
		if (temp>=55 && temp <65) {document.calcul_gearbox.selRatio.value=60}
		if (temp>=65 && temp <75 && document.calcul_gearbox.rdoInline.checked ==true){document.calcul_gearbox.selRatio.value=70}
		if (temp>=75 && temp <85 && document.calcul_gearbox.rdoInline.checked==true){document.calcul_gearbox.selRatio.value=80}
		if (temp>=85 && temp <95 && document.calcul_gearbox.rdoInline.checked==true){document.calcul_gearbox.selRatio.value=90}
		if (temp>=95 && temp <105 && document.calcul_gearbox.rdoInline.checked==true){document.calcul_gearbox.selRatio.value=100}
		if (temp>=105 && temp <120 && document.calcul_gearbox.rdoInline.checked==true){document.calcul_gearbox.selRatio.value=110}
		if (temp>=120 && temp <140 && document.calcul_gearbox.rdoInline.checked==true){document.calcul_gearbox.selRatio.value=130}
		if (temp>=140 && temp <170 && document.calcul_gearbox.rdoInline.checked==true){document.calcul_gearbox.selRatio.value=150}
		if (temp<2.5)
		{
		  document.calcul_gearbox.selRatio.value="";
			window.alert('Vitesse de sortie trop grande pour les autres valeurs.\n Ratio minimal : 5\nLe formulaire sera réinitialisé.');
			document.calcul_gearbox.cmdClear.click();
			document.calcul_gearbox.rdoAngle.focus();
			//document.calcul_gearbox.txtError.value="Ratio trop petit(min. 5)"
		}
		if (temp>=170 && document.calcul_gearbox.rdoInline.checked==true)
		{
		  document.calcul_gearbox.selRatio.value="";
			window.alert('Vitesse de sortie trop petite pour les autres valeurs.\n Vitesse minimale : 12 RPM\nLe formulaire sera réinitialisé.');
			document.calcul_gearbox.cmdClear.click();
			document.calcul_gearbox.rdoAngle.focus();
			//document.calcul_gearbox.txtError.value="Ratio trop grand(max. 150)"
		}
		if (temp>=65 && document.calcul_gearbox.rdoInline.checked==false)
		{
		  document.calcul_gearbox.selRatio.value="";
			window.alert('Vitesse de sortie trop petite pour les autres valeurs.\n Vitesse minimale : 30 RPM\nLe formulaire sera réinitialisé.');
			document.calcul_gearbox.cmdClear.click();
			document.calcul_gearbox.rdoAngle.focus();
			//document.calcul_gearbox.txtError.value="Ratio trop grand(max. 60)"
		}
		else //(document.calcul_gearbox.txtError.value=="")
		{
		  document.calcul_gearbox.RPMsortie.value=Math.round(document.calcul_gearbox.txtRPM.value/document.calcul_gearbox.selRatio.value)
		}
  }
	else
	{
	  document.calcul_gearbox.selRatio.value="";
		window.alert('Le RPM doit être supérieur à 0.\nLe formulaire sera réinitialisé.');
		document.calcul_gearbox.cmdClear.click();
		document.calcul_gearbox.rdoAngle.focus();
		//document.calcul_gearbox.txtError.value="RPM doit être supérieur à 0";
	}
  /*if(document.calcul_gearbox.txtError.value != "")
	{
	  document.calcul_gearbox.cmdCalcul.disabled=true
	}
  else
	{
	  document.calcul_gearbox.cmdCalcul.disabled=false
	}*/
}

function cmdCalcul_onclick()
{
  var size;
  var hp;
  var pertes;
  var hpb;
  var stage;
  var lp;
  var sss;
  if (document.calcul_gearbox.cmdCalcul.disabled==false)
  {
	  if (document.calcul_gearbox.RPMsortie.value==0)
		{
		   window.alert('Le RPM doit être supérieur à 0.\nLe formulaire sera réinitialisé.');
			 document.calcul_gearbox.cmdClear.click();
			 document.calcul_gearbox.rdoAngle.focus();
		}
    else 
		{
  		if (document.calcul_gearbox.rdoAngle.checked==true)
  		{
  			 pertes=Math.pow(document.calcul_gearbox.entryHP.value, -0.225)*Math.pow(document.calcul_gearbox.RPMsortie.value, -0.6)*2.463;
  			 hpb = document.calcul_gearbox.entryHP.value*(1-pertes);
  			 lp = (hpb*5250)/document.calcul_gearbox.RPMsortie.value*12*.96;
  			 hp = document.calcul_gearbox.txtFacteur.value*lp*(1+(0.2*(document.calcul_gearbox.selRatio.value<11)))*(1+(0.25*(document.calcul_gearbox.selRatio.value<8)));
  			 if(hp<=250) {size="34"}
  			 if(hp>250 && hp<=369) {size="40"}
  			 if(hp>369 && hp<=611) {size="50"}
  			 if(hp>611 && hp<=1092) {size="60"}
  			 if(hp>1092 && hp<=1674) {size="70"}
  			 if(hp>1674 && hp<=2088) {size="80"}
  			 if(hp>2088 && hp<=3775) {size="100"}
  			 if(hp>3775 && hp<=5986) {size="120"}
  			 if(hp>5986 && hp<=7325) {size="135"}
  			 if(hp>7325 && hp<=9748) {size="155"}
  			 if(hp>9748 && hp<=13473) {size="175"}
  			 if(hp>13473 && hp<=17251) {size="200"}
  			 if(hp>17251 && hp<=22470) {size="225"}
  			 if(hp>22470 && hp<=27118) {size="250"}
  			 if(hp>27118 && hp<=43308) {size="300"}
  			 if(hp>43308 && hp<=71741) {size="350"}
  			 if(hp>71741)
  			 {
  			   document.calcul_gearbox.txtSize.value="Trop haut"
  			 }
  			 else
  			 {
  					hpb=(Math.round(hpb*100))/100;
  					lp=Math.round(lp);
  					document.calcul_gearbox.txtSize.value=size;
  					if (hpb<0.1 || lp<20)
  					{
  					  document.calcul_gearbox.txtSortie.value="< 0.1HP ou < 20 livres-pouces";
  					}
  					else
  					{
  					  document.calcul_gearbox.txtSortie.value=hpb+" HP, "+lp+" livres-pouces"}
  				 } 
  			 } 
  			 if(document.calcul_gearbox.rdoAngle.checked==false)
  			 {
  			   if(document.calcul_gearbox.selRatio.value==5) {stage=1; sss=""}
  			   if(document.calcul_gearbox.selRatio.value==10 || document.calcul_gearbox.selRatio.value==15) {stage=2; sss="s"}
  			   if(document.calcul_gearbox.selRatio.value>=20) {stage=3; sss="s"}
  			   hpb=(Math.round(document.calcul_gearbox.entryHP.value*98))/100;
  			   lp=Math.round((hpb*5250)/document.calcul_gearbox.RPMsortie.value*12);
  			   hp=document.calcul_gearbox.txtFacteur.value*document.calcul_gearbox.entryHP.value*5250*12/document.calcul_gearbox.RPMsortie.value;
  			   if(stage==1)
  				 {
  				   if(hp>1 && hp<=220) {size="A71"}
  					 if(hp>220 && hp<=440) {size="A80"}
  					 if(hp>440 && hp<=750) {size="A90"}
  					 if(hp>750 && hp<=970) {size="A100"}
  					 if(hp>970 && hp<=2600) {size="A132"}
  					 if(hp>2600) {size="haut"}
  				 }
  			   if(stage==2)
  				 {
  				   if(hp>1 && hp<=800){size="A90"}
  					 if(hp>800 && hp<=1760){size="A115"}
  					 if(hp>1760 && hp<=3100){size="A140"}
  					 if(hp>3100 && hp<=7500){size="A180"}
  					 if(hp>7500 && hp<=14200){size="A225"}
  					 if(hp>14200 && hp<=25000){size="A250"}
  					 if(hp>25000 && hp<=44500){size="A315"}
  					 if(hp>44500 && hp<=71000){size="A375"}
  					 if(hp>71000 && hp<=106000){size="A415"}
  					 if(hp>106000){size="haut"}
  				 }
  			   if(stage==3)
  				 {
  				   if(hp>1 && hp<=1100){size="A90"}
  					 if(hp>1100 && hp<=3000){size="A115"}
  					 if(hp>3000 && hp<=5300){size="A140"}
  					 if(hp>5300 && hp<=9730){size="A180"}
  					 if(hp>9730 && hp<=18580){size="A225"}
  					 if(hp>18580 && hp<=35400){size="A250"}
  					 if(hp>35400 && hp<=70800){size="A315"}
  					 if(hp>70800 && hp<=141000){size="A375"}
  					 if(hp>141000 && hp<=221000){size="A415"}
  					 if(hp>221000){size="haut"}
  				 }
  			   if(size=="haut")
  				 {
  				   document.calcul_gearbox.txtSize.value="Trop haut"
  				 }
  			   else
  				 {
  					 document.calcul_gearbox.txtSize.value=size+" -  "+stage+" étage"+sss;
  					 document.calcul_gearbox.txtSortie.value=hpb+" HP, "+lp+" livres-pouces";
  				 } 
  			 }
			}
   }
}