//
// rates2010.js: code to calculate journey price
//

function init_language()
{
	var tab_languages;
	
	raz_liste(document.forms[0].language);	// clear all

	if(document.f1.Programme.value == "Sport+Language")
	{
		tab_languages = lang_sport;
		tab_programmes = tab_sports;
	}
	else if(document.f1.Programme.value == "Culture+Language")
	{
		tab_languages = lang_culture;
		tab_programmes = tab_culture;
	}
	else if(document.f1.Programme.value == "Language and Leisure")
	{
		tab_languages = lang_leisure;
		tab_programmes = tab_leisure;
	}
	else
	{
		tab_languages = lang_std;
		for(i=0;i<tab_languages.length;i++)
		{
			lang = tab_languages[i];
			opttemp= new Option(lang);
			document.forms[0].language.options[document.forms[0].language.length]=opttemp;
			document.forms[0].language.options[document.forms[0].language.length-1].value=lang;
		}
		return;
	}
	ptype = document.f1.ProgType.value;	// chosen program
	count = 0;
	if(ptype.length != 0)
	{
		for(i=0;i<tab_languages.length;i++)
		{
			lang = tab_languages[i];
			found = false;
			for(j=0;j<tab_programmes.length;j++)
			{
				languages = tab_programmes[j][6];
				if(tab_programmes[j][2] == ptype && languages.indexOf(lang) != -1)
				{
					found = true;
					break;
				}
			}
			if(found == true) // only show languages offered
			{
				opttemp= new Option(lang);
				document.forms[0].language.options[document.forms[0].language.length]=opttemp;
				document.forms[0].language.options[document.forms[0].language.length-1].value=lang;
				count++;
			}
		}
		// if only one choice, select it
		/*
		if(count == 1)
		{
			document.f1.language.options[1].selected = true;
			init_country(document.f1.language.options[1].value);	
		}
		*/
	}
}

function init_country(langue) 
{
	//alert(langue);
	count=0;
	special = false;
	raz_liste(document.forms[0].country);
	raz_liste(document.forms[0].Region1);
	raz_liste(document.forms[0].Region2);
	raz_liste(document.forms[0].Region3);

	if(document.f1.Programme.value == "Sport+Language")
	{
		ptypea = tab_sports;
		selected = document.f1.ProgType.value;
		special = true;
	}
	else if(document.f1.Programme.value == "Culture+Language")
	{
		ptypea = tab_culture;
		selected = document.f1.ProgType.value;
		special = true;
	}
	else if(document.f1.Programme.value == "Language and Leisure")
	{
		ptypea = tab_leisure;
		selected = document.f1.ProgType.value;
		special = true;
	}
	else
		ptypea = tab_pays;
		
	var paystmp="";
	for(i=0;i<ptypea.length;i++) 
	{
		lang = ptypea[i][6];
		if(paystmp!=ptypea[i][0] && lang.indexOf(langue) != -1) 
		{
			if(special == true && ptypea[i][2] != selected)
				continue;
			paystmp=ptypea[i][0];
			opttemp= new Option(paystmp);
			document.forms[0].country.options[document.forms[0].country.length]=opttemp;
			document.forms[0].country.options[document.forms[0].country.length-1].value=paystmp;
			count++;
		}	
	}
	// if only one choice, select it
	if(count == 1)
	{
		document.f1.country.options[1].selected = true;
		init_region(document.f1.country.value)	
	}
}
function init_region(pays)
{
	var rgtmp="";
	var count =0;
	var langue=document.forms[0].language.value;
	raz_liste(document.forms[0].Location);
	document.forms[0].groupe_pays.value='';
	if(document.f1.Programme.value == "Sport+Language")
	{
		ptypea = tab_sports;
		selected = document.f1.ProgType.value;
		special = true;
	}
	else if(document.f1.Programme.value == "Culture+Language")
	{
		ptypea = tab_culture;
		selected = document.f1.ProgType.value;
		special = true;
	}
	else if(document.f1.Programme.value == "Language and Leisure")
	{
		ptypea = tab_leisure;
		selected = document.f1.ProgType.value;
		special = true;
	}
	else
		ptypea = tab_pays;
		
	for(i=0;i<ptypea.length;i++) 
	{
		//alert(tab_pays[i][0]+ ":langue="+langue+ " 6="+tab_pays[i][6]+" 7="+tab_pays[i][7]);
		lang = ptypea[i][6];
		if(pays==ptypea[i][0] && rgtmp!=ptypea[i][1] && lang.indexOf(langue) != -1) 
		{
			if(special == true && ptypea[i][2] != selected)
				continue;
			rgtmp=eval('"'+ptypea[i][1]+'"');
			opttemp= new Option(rgtmp);
			document.forms[0].Region1.options[document.forms[0].Region1.length]=opttemp;
			document.forms[0].Region1.options[document.forms[0].Region1.length-1].value=rgtmp;			
			opttemp= new Option(rgtmp);
			document.forms[0].Region2.options[document.forms[0].Region2.length]=opttemp;
			document.forms[0].Region2.options[document.forms[0].Region2.length-1].value=rgtmp;
			opttemp= new Option(rgtmp);
			document.forms[0].Region3.options[document.forms[0].Region3.length]=opttemp;
			document.forms[0].Region3.options[document.forms[0].Region3.length-1].value=rgtmp;
			document.forms[0].groupe_pays.value=tab_pays[i][4];
			document.forms[0].devise.value=ptypea[i][3];
			count++;
			// set displayed currency
			switch(document.forms[0].devise.value) 
			{
				case '1':
					document.forms[0].Currency.value='GBP';
					document.forms[0].devise_temp.value='GBP';
					break;
				case '2':
					document.forms[0].Currency.value='USD';
					document.forms[0].devise_temp.value='USD';
					break;
				case '3':
					document.forms[0].Currency.value='EUR';
					document.forms[0].devise_temp.value='EUR';
					break;
			}
		}	
	}
	if(count == 1)
	{
		document.f1.Region1.options[1].selected = true;
		init_location(document.f1.Region1.value);
	}
}

function init_location(region)
{
	var lctmp="";
	var langue=document.forms[0].language.value;
	for(i=0;i<tab_pays.length;i++)
	{ 
		lang = tab_pays[i][6];
		if(region==tab_pays[i][1] && lctmp!=tab_pays[i][2] && lang.indexOf(langue) != -1) 
		{
			lctmp=eval('"'+tab_pays[i][2]+'"');
			opttemp= new Option(lctmp);
			document.forms[0].Location.options[document.forms[0].Location.length]=opttemp;
			document.forms[0].Location.options[document.forms[0].Location.length-1].value=lctmp+'||'+i;
			document.forms[0].groupe_pays.value=tab_pays[i][4];
		}	
	}
}

// initialize program types according to Programme choice
function init_progtype(prog)
{
	var ptypea;
	if(prog == "Sport+Language")
		ptypea = tab_sports;
	else if(prog == 'Culture+Language')
		ptypea = tab_culture;
	else
		ptypea = tab_leisure;
	raz_liste(document.f1.ProgType);
	raz_liste(document.f1.language);
	tarifs_init();
	previous = "";		// avoid duplication
	for(i=0;i<ptypea.length;i++)
	{
			current = ptypea[i][2];
			if(current == previous)
				continue;
			lctmp=eval('"'+ptypea[i][2]+'"');
			opttemp= new Option(lctmp);
			document.forms[0].ProgType.options[document.forms[0].ProgType.length]=opttemp;
			document.forms[0].ProgType.options[document.forms[0].ProgType.length-1].value=lctmp;
			document.forms[0].groupe_pays.value=ptypea[i][4];
			previous = current;
	}
}

function raz_liste(nl)
{
	nl.innerHTML='';
	nl.options[0]=new Option("-------");
	nl.options[0].value='';
}

function setOptionState(obj,disabled,checked)
{
	obj.disabled = disabled;
	if(checked != -1)
		obj.checked = checked;
}

// set various possibilities depending on programme. options are reset when changing programme
function action_prg()
{
	tarifs_init();
	if(document.f1.Programme.value == 'Home Tuition')
	{
		setOptionState(document.f1.Private_facilities,false,false);
		//setOptionState(document.f1.Supplement_activities,false,false);
		setOptionState(document.f1.Cocooning,false,false);
		setOptionState(document.f1.VIP,false,false);
		setOptionState(document.f1.business_lang,false,false);
		setOptionState(document.f1.Exam_preparation,false,false);
		setOptionState(document.f1.Five_Star_Home,false,false);
		document.f1.Lessons.disabled = false;
		document.f1.ProgType.disabled = true;
	}
	else if (document.f1.Programme.value == 'Sport+Language')
	{
		setOptionState(document.f1.Private_facilities,false,false);
		//setOptionState(document.f1.Supplement_activities,false,false);
		setOptionState(document.f1.Cocooning,false,false);
		setOptionState(document.f1.VIP,false,false);
		setOptionState(document.f1.business_lang,false,false);
		setOptionState(document.f1.Exam_preparation,false,false);
		setOptionState(document.f1.Five_Star_Home,false,false);
		document.f1.Lessons.disabled = true;
		document.f1.ProgType.disabled = false;
		init_progtype('Sport+Language');
	}
	else if (document.f1.Programme.value == 'Culture+Language')
	{
		setOptionState(document.f1.Private_facilities,false,false);
		//setOptionState(document.f1.Supplement_activities,false,false);
		setOptionState(document.f1.Cocooning,false,false);
		setOptionState(document.f1.VIP,false,false);
		setOptionState(document.f1.business_lang,false,false);
		setOptionState(document.f1.Exam_preparation,false,false);
		setOptionState(document.f1.Five_Star_Home,false,false);
		document.f1.Lessons.disabled = true;
		document.f1.ProgType.disabled = false;
		init_progtype('Culture+Language');
	}
	else if (document.f1.Programme.value == 'Language and Leisure')
	{
		setOptionState(document.f1.Private_facilities,false,false);
		//setOptionState(document.f1.Supplement_activities,false,false);
		setOptionState(document.f1.Cocooning,false,false);
		setOptionState(document.f1.VIP,false,false);
		setOptionState(document.f1.business_lang,false,false);
		setOptionState(document.f1.Exam_preparation,false,false);
		setOptionState(document.f1.Five_Star_Home,false,false);
		document.f1.Lessons.disabled = true;
		document.f1.ProgType.disabled = false;
		init_progtype('Language and Leisure');
	}
	else if (document.f1.Programme.value == 'Lessons only')
	{
		setOptionState(document.f1.Private_facilities,true,false);
		//setOptionState(document.f1.Supplement_activities,true,false);
		setOptionState(document.f1.Cocooning,true,false);
		setOptionState(document.f1.VIP,true,false);
		setOptionState(document.f1.business_lang,false,false);
		setOptionState(document.f1.Exam_preparation,false,false);
		setOptionState(document.f1.Five_Star_Home,true,false);
		document.f1.Lessons.disabled = false;
		document.f1.ProgType.disabled = true;
	}
	else // accomodations only 
	{
		setOptionState(document.f1.Private_facilities,false,false);
		//setOptionState(document.f1.Supplement_activities,false,false);
		setOptionState(document.f1.Cocooning,false,false);
		setOptionState(document.f1.VIP,false,false);
		setOptionState(document.f1.business_lang,true,false);
		setOptionState(document.f1.Exam_preparation,true,false);
		setOptionState(document.f1.Five_Star_Home,false,false);
		document.f1.Lessons.value=0;
		document.f1.Lessons.disabled = true;
		document.f1.ProgType.disabled = true;
	}
}

// called when choosing a sport/culture programme
function action_progtype()
{
	//alert("action_progtype:"+  document.f1.ProgType.value);
	tarifs_init();
	//init_language();
}

// update flags and price according to flags 
function actionFlags() 
{
	if(	document.f1.Five_Star_Home.checked == true ||
		document.f1.VIP.checked == true)
		setOptionState(document.f1.Private_facilities,true,false);
	else
		setOptionState(document.f1.Private_facilities,false,-1);
	if(document.f1.VIP.checked == true)
		setOptionState(document.f1.Cocooning,true,false);
	else
		setOptionState(document.f1.Cocooning,false,-1);
	if(document.f1.Accompaniant_old.value == '')
		document.f1.Accompaniant.checked = false;
	else
		document.f1.Accompaniant.checked = true;
	// vip and five star are somewhat mutually exclusive		
	if(document.f1.Five_Star_Home.checked == true)
		setOptionState(document.f1.VIP,true,false);
	else
		setOptionState(document.f1.VIP,false,-1);
		
	if(document.f1.VIP.checked == true)
		setOptionState(document.f1.Five_Star_Home,true,false);
	else
		setOptionState(document.f1.Five_Star_Home,false,-1);
			
	if(validate(false) == true)
		calcul_tarif(document.forms[0].devise.value);		
}

// transfer requested
function transfer_req()
{
	if(validate(false) == true)
		calcul_tarif(document.forms[0].devise.value);		
}

function tarifs_init_location(idreg) 
{
	raz_liste(document.forms[0].Location);
	init_location(idreg);
}

function tarifs_init_region(id)
{
	raz_liste(document.forms[0].Region1);
	raz_liste(document.forms[0].Region2);
	raz_liste(document.forms[0].Region3);
	init_region(id);
}
function tarifs_init() 
{
	init_language();
	raz_liste(document.forms[0].country);
	raz_liste(document.forms[0].Region1);
	raz_liste(document.forms[0].Region2);
	raz_liste(document.forms[0].Region3);
}
function submit_form()
{
	if(calcul_tarif(document.forms[0].devise.value)) 
	{
		temp_location=document.forms[0].Location.value.split('||');
		document.forms[0].location_transfer.value=temp_location[0];
		return(true);
	} 
	else 
		return(false);
}
function full_amount(etat) 
{
	today = new Date();
	arr = toDate(document.forms[0].Date_Arr.value);
	days = (arr.getTime() - today.getTime())/(1000*3600*24);
	//alert("days="+days);
	if(etat || days < 30)
		document.forms[0].inputamount.value=document.forms[0].inputamount_temp.value;
	else switch(document.forms[0].devise.value) 
	{
		case '1':document.forms[0].inputamount.value=100;break;
		case '2':document.forms[0].inputamount.value=150;break;
		case '3':document.forms[0].inputamount.value=150;break;
	}
}	

function indaterange(r1,r2,arr,dep)
{
	if(r2 < r1)
		r2 += 10000;
	if(dep < arr)
		dep += 10000;
	if(arr  < r1) return (dep >= r1)? true:false;
	if(arr <= r2) return (dep <= r2)? true:false;
	return false;
}

// compute total hours: num is tt.aa tt: hours lessons, aa: hours activities
function totalhours(num)
{
		n = parseFloat(num);
		tt = parseInt(num);	// hours tuition
		aa = Math.round(100*(n-tt));
		//alert(num+" "+tt+" "+aa);
		return tt+aa;
}

// compute hours activities. in 2009, these are charged. num is tt.aa tt: hours lessons, aa: hours activities
function hoursactivities(num)
{
		n = parseFloat(num);
		tt = parseInt(num);	// hours tuition
		aa = Math.round(100*(n-tt));
		return aa;	// could be 0
}

// validate that all required inputs are set
function validate(aflag)
{
	var lessons_only = false;
	var board_only = false;
	var special = false;
	// validate arrival date
	if(document.f1.Date_Arr.value == '')
	{
		if(aflag)
		{
			alert(messages[4]);
			document.f1.Date_Arr.focus();
		}
		return false;
	}
	// validate departure date
	if(document.f1.Date_Dep.value == '')
	{
		if(aflag)
		{
			alert(messages[5]);
			document.f1.Date_Dep.focus();
		}
		return false;
	}
	// validate programme
	if(document.f1.Programme.value == '')
	{
		if(aflag)
		{
			alert(messages[2]);
			document.f1.Programme.focus();
		}
		return false;
	}
	// determine program type
	if(document.f1.Programme.value == 'Sport+Language' ||
		document.f1.Programme.value == 'Culture+Language'
		|| document.f1.Programme.value == 'Language and Leisure')
		special = true;
	// preset some usefull flags.
	if(document.f1.Programme.value == 'Lessons only')
		lessons_only = true;
	if(document.f1.Programme.value == 'B+B accommodation only' 
		|| document.f1.Programme.value == 'H/B accommodation only'
		|| document.f1.Programme.value == 'F/B accommodation only'
		)
		board_only = true;	

	// validate lessons
	if(special == false)
	{
		if(document.f1.Lessons.value == '0' && board_only == false)
		{
			if(aflag)
			{
				alert(messages[3]);
				document.f1.Lessons.focus();
			}
			return false;
		}
	}
	else if(document.f1.ProgType.value == '')
	{
		if(aflag)
		{
				alert(messages[9]);
				document.f1.ProgType.focus();
		}
		return false;
	}
	// validate language selection
	if(document.f1.language.value == '')
	{
		if(aflag)
		{
			alert(messages[0]);
			document.f1.language.focus();
		}
		return false;
	}
	// validate country selection
	if(document.f1.country.value == '')
	{
		if(aflag)
		{
			alert(messages[1]);
			document.f1.country.focus();
		}
		return false;
	}
	// validate region1 selection
	if(document.forms[0].Region1.value == '')
	{
		if(aflag)
		{
			alert(messages[7]);
			document.f1.Region1.focus();
		}
		return false;
	}	
	
	// Edinburgh Festival must book 15 hours lessons and 10 hours activities
	
	if(document.forms[0].Region1.value == "Edinburgh" && indaterange(0815,0905,arr,dep) && document.forms[0].Lessons.value != '15.10')
	
	{
		if(aflag)
		{
			alert("Due to the Edinburgh Festival, between August 15th and Sept 5th you must choose 15 hours lessons and 10 hours activities");
			document.f1.Lessons.focus();
		}
		return false;
	}	
	
	
	
	
	
	
	// check transfer
	if(document.f1.transfer[3].checked == false)	//'not_required'
	{
		if(document.f1.Location.value == '')
		{
			if(aflag)
			{
				alert(messages[6]);
				document.f1.Location.focus();
			}
			return false;
		}
	}
	return true;
}

function calcul_tarif(devise) 
{
	var lessons_only = false;
	var board_only = false;
	var special = false;
	var fnumweeks;
	
	if(validate(true) == false)
		return false;

	// note: exact number of days is used to calculate option prices.
	numweeks = eval(document.f1.numweeks.value);
	fnumweeks = numweeks + eval(document.f1.extranights.value/7);
	montant_temp = 0;
	
	//alert(fnumweeks);
	// preset some usefull flags.
	if(document.f1.Programme.value == 'Lessons only')
		lessons_only = true;
	if(document.f1.Programme.value == 'B+B accommodation only' 
		|| document.f1.Programme.value == 'H/B accommodation only'
		|| document.f1.Programme.value == 'F/B accommodation only'
		)
		board_only = true;	

	if(document.f1.Programme.value == 'Sport+Language')
	{
		ptypea = tab_sports;
		special = true;
	}
	else if(document.f1.Programme.value == 'Culture+Language')
	{
		ptypea = tab_culture;
		special = true;
	}
	else if(document.f1.Programme.value == 'Language and Leisure')
	{
		ptypea = tab_leisure;
		special = true;
	}
		
	if(special == false)
	{
		hours = totalhours(document.f1.Lessons.value);
	}
	
	// determine group the choice of country/region belongs to
	var group=document.forms[0].groupe_pays.value;
	// note: special cases: english in spain, and chinese/japanese in USA!!!!

	
	//alert("group="+group);
	

	//alert("special="+special);
	if(board_only == true)
	{
		if(document.f1.Programme.value == 'B+B accommodation only')
			montant_temp += bed_breakfast[group] * fnumweeks;
		else if(document.f1.Programme.value == 'H/B accommodation only')
			montant_temp += half_board[group] * fnumweeks;
		else if(document.f1.Programme.value == 'F/B accommodation only')
			montant_temp += full_board[group] * fnumweeks;	
	}
	else
	{
		coef_e=1;
		if(special == false)
			eval ('montant_temp=std_'+hours+'h[group];');	// price for given hours
		else	// must determine the programme.
		{
			montant_temp = 0;
			ptype = document.f1.ProgType.value;
			for(i=0;i<ptypea.length;i++)
			{
				if(ptypea[i][2] == ptype && ptypea[i][0] == document.f1.country.value
					&& ptypea[i][1] == document.f1.Region1.value)
				{
						montant_temp = ptypea[i][5];
						break;
				}
			}
			if(montant_temp == 0)
			{
				alert("Can't calculate...");
				return;
			}
		}
		montant_temp *= numweeks;
		//alert("here:group="+group+" devise="+devise+" hours="+hours);
		//return false;
		if(document.f1.business_lang.checked) switch(devise) 
		{
			case '1': montant_temp += 100*fnumweeks;break;
			case '2': 
			case '3': montant_temp += 150*fnumweeks;break;
		}
		if(document.f1.Five_Star_Home.checked || document.f1.VIP.checked) 
		{
			coef_e=2;
			montant_temp *= 2;
		}
		// add extra nights
		//if(document.forms[0].extranights.checked) 
		montant_temp += coef_e*std_extra[group]*document.f1.extranights.value;
		if(document.forms[0].Private_facilities.checked && 
			document.f1.Five_Star_Home.checked == false && 
			
			document.f1.VIP.checked == false) 
		{
			if(std_private[group]=='N/A') 
			{
				
				alert("One of the options you chose is not available. Please unclick it.(Private bathroom)"); 
				return(false);
			}
			montant_temp += std_private[group]*fnumweeks;
		}
		if(document.forms[0].Accompaniant.checked) switch(document.forms[0].Accompaniant_old.value) 
		{
			case '0-4' : montant_temp += 0.25*full_board[group]*fnumweeks;break;
			case '5-11' : montant_temp += 0.5*full_board[group]*fnumweeks;break;
			case '12 and over' : montant_temp += 0.8*full_board[group]*fnumweeks;break;
		}	
	}
	
	// option Cocooning
	if(document.forms[0].Cocooning.checked && document.f1.VIP.checked == false) switch(devise) 
	{
		case '1':montant_temp +=100*fnumweeks;break;
		case '2':montant_temp +=150*fnumweeks;break;
		case '3':montant_temp +=150*fnumweeks;break;
	}
		
	// option Exam_preparation
	if(document.forms[0].Exam_preparation.checked) switch(devise) 
	{
		case '1':montant_temp += 50*fnumweeks;break;
		case '2':montant_temp += 75*fnumweeks;break;
		case '3':montant_temp += 75*fnumweeks;break;
	}
	
	
	
	// apply reduction (5% for 3rd week, and 10% for 4th and over)
		
	{
		var v3=0;
		var v4=0;
		over = numweeks-2;
		if(over >= 1)
		{
			v4 = fnumweeks-3;	// float weeks over 3
			v3 = 1;
		}
		else if(over >= 0)
		{
			v4 = 0;
			v3 = fnumweeks-2;	// float weeks over 2
		}
		ppweek = montant_temp/numweeks;	// average price per week
		reduc = (0.05*v3 + 0.10*v4)*ppweek;
		montant_temp -= reduc;
	}
	// option Supplement_activities: this is done automatically
	if(document.f1.Programme.value == "Home Tuition")
	{
	/*
	$result['L'] = array(5 => 65,10 => 130,15 => 195);
	$result['D'] = array(5 => 90,10 => 180,15 => 270);
	$result['E'] = array(5 => 75,10 => 150,15 => 225);
	*/

		hact = hoursactivities(document.f1.Lessons.value);
		if(hact != 0) switch(devise) // note: should come from a data table...
		{
			case '1': // pounds
				if(hact == 5)
					montant_temp += 65*fnumweeks;
				else if(hact == 10)
					montant_temp += 130*fnumweeks;
				else if(hact == 15)
					montant_temp += 195*fnumweeks;
				break;
			case '2': // dollars
				if(hact == 5)
					montant_temp += 90*fnumweeks;
				else if(hact == 10)
					montant_temp += 180*fnumweeks;
				else if(hact == 15)
					montant_temp += 270*fnumweeks;
				break;
			case '3':// euros
				if(hact == 5)
					montant_temp += 75*fnumweeks;
				else if(hact == 10)
					montant_temp += 150*fnumweeks;
				else if(hact == 15)
					montant_temp += 225*fnumweeks;
				break;
		}			
		//alert(hact);
	}
	/*
	if(document.forms[0].Supplement_activities.checked) switch(devise) 
	{
		case '1':montant_temp += 60*fnumweeks;break;
		case '2':montant_temp += 90*fnumweeks;break;
		case '3':montant_temp += 90*fnumweeks;break;
	}
	*/
	
	// option Lessons_only
	if(lessons_only == true) switch(devise) 
	{
		case '1':montant_temp -= 150;break;
		case '2':montant_temp -= 200;break;
		case '3':montant_temp -= 200;break;
	}
		
	// 20% de réduction pour Two Student
	if(document.forms[0].Two_Student.checked) 
	{
		coef_transfert = 0.5;
		coef_chrismas = 0.8;
		montant_temp = 0.8*montant_temp;
	}
	else 
	{
		coef_transfert = 1;
		coef_chrismas = 1;
	}
	
	// transfert
	var temp_transfer=0;
	if(document.forms[0].transfer[0].checked) 
		temp_transfer=1;
	else if(document.forms[0].transfer[1].checked) 
		temp_transfer=1;
	else if(document.forms[0].transfer[2].checked) 
		temp_transfer=2;
	else if(document.forms[0].transfer[3].checked) 
		temp_transfer=0;
	if(temp_transfer!=0) 
	{
		if(document.f1.Location.value=='')
		{
			alert(messages[6]);
			document.f1.Location.focus();
			document.forms[0].inputamount.value='';	
			return(false);
		}
		temp_index=document.forms[0].Location.value.split('||');
		//temp_col=eval('2+'+devise);
		temp_col=5;	// single currency
		montant_temp+=coef_transfert*temp_transfer*tab_pays[temp_index[1]][temp_col];
	}	
	if(document.f1.Christmas_New_Year.checked == true) switch(devise)
	{
		case '1':montant_temp += coef_chrismas*95;break;
		case '2':montant_temp += coef_chrismas*145;break;
		case '3':montant_temp += coef_chrismas*145;break;
	}
	
		
	
	// arrondi
	montant_temp=Math.floor(montant_temp*1000)/1000;
	montant_temp=Math.round(montant_temp*100)/100;
	document.forms[0].inputamount_temp.value=montant_temp;
	full_amount(document.forms[0].pay_the_full_amount.checked);
	return(true);
}

//////////////////////////////////////////////////////////////////
// called when returning from calendar function.
// to calculate number of days, and number of weeks.
function DateChanged()
{
    ds = toDate(document.f1.Date_Arr.value);
    de = toDate(document.f1.Date_Dep.value);
    
    // set departure date to starting date if not set
    if(!isNaN(ds) && isNaN(de))
		document.f1.Date_Dep.value = document.f1.Date_Arr.value;
	
    if(isNaN(ds) || isNaN(de))
		numdays = 0;
	else
		numdays = Math.floor( (de.getTime()- ds.getTime())/(1000*60*60*24)); // rounding problem!
    //alert(numdays);
    // readjust departure date to be arrival date
    if(numdays < 0)
    {
 	document.f1.Date_Dep.value = document.f1.Date_Arr.value;
	return;
    }
    //document.f1.Night_Sup.value = numdays;
    // save hidden variables for form
    da = toDate(document.f1.Date_Arr.value);
    document.f1.startday.value = da.getDate();
    document.f1.coursestartmonth.value = da.getMonth()+1;
    document.f1.coursestartyear.value = da.getYear();
    dd = toDate(document.f1.Date_Dep.value);
    document.f1.finishday.value = dd.getDate();
    document.f1.finishmonth.value = dd.getMonth()+1;
    document.f1.finishyear.value = dd.getYear();
	// set Christmas / New Year flag
	arr = 100* parseInt(document.f1.coursestartmonth.value) + parseInt(document.f1.startday.value);
	dep = 100* parseInt(document.f1.finishmonth.value) + parseInt(document.f1.finishday.value);

	//if(document.forms[0].Christmas_New_Year.checked) switch(devise) 
	if(indaterange(1224,1226,arr,dep) || indaterange(1231,0101,arr,dep)) 
		document.f1.Christmas_New_Year.checked = true;
	else
		document.f1.Christmas_New_Year.checked = false;
	// set number of weeks.
	numweeks = Math.floor(numdays/7);
	extranights = numdays - numweeks*7;
	if(extranights > 4)
	{
		extranights = 0;
		numweeks++;
	}
	if(numweeks < 1)
		numweeks = 1;	// minimum
	document.f1.numweeks.value=numweeks;
	document.f1.extranights.value=extranights;
}


// get date from DD-MM-YYYY format
function toDate(s)
{
    mm = s.substring(0,2);
    if(mm.indexOf("0") == 0)
            mm = mm.substring(1);
    day = parseInt(mm);
    mm = s.substring(3,5);
    if(mm.indexOf("0") == 0)
            mm = mm.substring(1);
    month = parseInt(mm)-1;
    year = parseInt(s.substring(6));
    //alert("day="+day+" month="+month+" year="+year);
    return new Date(year,month,day);
}



