ITEMS_LENGTH = 82;
TREATMENTS_LENGTH = 20;
ACTIVITIES_LENGTH = 20;
function bindQuoteWH()
{
var items = $('personsNb');
Event.observe($('personsNb'), 'change', quoteFormManageWH);
var items = Form.getInputs('quote','radio','personsAndRooms'); 
for (var index = 0; index < items.length; ++index) 
{
Event.observe(items[index], 'click', quoteFormManageWH); 
}                             
Event.observe($('quote'), 'submit', quoteFormManageWH,false);     
} 
function bindQuoteWOH()
{
var items = $('personsNb');
Event.observe($('personsNb'), 'change', quoteFormManageWOH);  
Event.observe($('quote'), 'submit', quoteFormManageWOH,false);                      
}
function bindStep1()
{
var items1 = Form.getInputs('step1','checkbox');
for (var index = 0; index < items1.length; ++index) 
{
Event.observe(items1[index], 'click', step1ProtectionPriceManage);
}
var items2 = Form.getInputs('step1','radio');
items = items1.concat(items2);                     
for (var index = 0; index < items.length; ++index) 
{
Event.observe(items[index], 'click', step1NetPriceManage);
} 
Event.observe($('step1'), 'submit', step1FormManage,false);           
}      
function quoteFormManageWOH(e)
{
e = e || window.event;
var element = Event.element(e);
if(element == $('personsNb'))
{
personsNbManageWOH(element);
}
if('quote' == element.id)
{
var isChecked = checkForm(e);
if(isChecked)
{
Event.stop(e);  
switchToAJAXBook('step1');   
}
else
{
return;
} 
}  
} 
function quoteFormManageWH(e)
{
e = e || window.event;
var element = Event.element(e);
if(element == $('personsNb'))
{
personsNbManageWH(element);
}
if('oneTwinRoom' == element.id || 'twoSingleRooms' == element.id || 'oneSingleRoom' == element.id)
{
personsAndRoomsManage(element);
}
if('quote' == element.id)
{
var isChecked = checkForm(e);
if(isChecked)
{
Event.stop(e);  
switchToAJAXBook('step1');   
}
else
{
return;
}
}                
}
function secureField(strip,stripTags,stripScripts,inspect,field,fieldLengthMin,fieldLengthMax)
{
var fieldType = field.type;
var value = $F(field);
if(strip)
value = value.strip();
if(stripTags)
value = value.stripTags();
if(stripScripts)
value = value.stripScripts();
if(fieldLengthMin)
{
if(value.length < fieldLengthMin)
return false;
}
if(fieldLengthMax)
{
if(value.length > fieldLengthMax){
value = value.truncate(fieldLengthMax);
}
} 
return value;
} 
function step1FormManage(e)
{
var isChecked = checkForm(e);
var field = '';
if(!isChecked)
{
Event.stop(e);
return;   
}
var formNode = $('step1');
formNode.disable('formNode');
var personsNb = $F('step1PersonsNb');
var p0Title = '';
var p1Title = '';
var p0FirstName = '';
var p1FirstName = '';
var p0LastName = '';
var p1LastName = '';
var value = false;
var items = Form.getInputs('step1','text');
for (var index = 0; index < items.length; ++index) 
{
field = secureField(true,true,false,false,items[index],NAME_LENGTH_MIN,NAME_LENGTH_MAX);
if(!field)
{
return;
}       
else
{
switch(index)
{
case 0:
p0LastName = field;
break;
case 1:
p0FirstName = field;
break;
case 2:
p1LastName = field;
break;
case 3:
p1FirstName = field;
break;
default:
return;
} 
}
}
items = $('title0').options;
for (var index = 0; index < items.length; ++index) 
{
if(items[index].selected)
p0Title=items[index].value;
}
if('2' == personsNb)
{
items = $('title1').options;      
for (var index = 0; index < items.length; ++index) 
{
if(items[index].selected)
p1Title=items[index].value;
}
}
var selectedCuresId = [];             
var selectedCuresPrice = [];         
var p0ActivitiesId = [];             
var p0ActivitiesPrice = [];             
var p1ActivitiesId = [];             
var p1ActivitiesPrice = [];       
var p0TreatmentsId = [];             
var p0TreatmentsPrice = [];             
var p1TreatmentsId = [];             
var p1TreatmentsPrice = [];     
var p1CureId = '';             
var p1CurePrice = '';            
var p0CureId = '';             
var p0CurePrice = '';            
var net = $('net').innerHTML;
net = net.gsub(/&nbsp;.*/,'');
items = Form.getInputs('step1','radio');    
var p0ProtectionType = false;
var p1ProtectionType = false;
var p0ProtectionPrice = false;
var p1ProtectionPrice = false;
for (var index = 0; index < items.length; ++index) 
{
var id = items[index].id;
tmp = id.substr(0, 2);
{
if('a0'==tmp)
{
if(items[index].checked)          
{
p0ProtectionType = id.substr(14,1);
p0ProtectionPrice = items[index].value;
}
continue;
}
else if('a1' == tmp)
{
if(items[index].checked)          
{
p1ProtectionType = id.substr(14,1);
p1ProtectionPrice = items[index].value;
}
continue;
}
else
{
if(items[index].checked)
{
if('p0' == tmp)
{
p0CureId = id;
p0CurePrice = items[index].value;
}
else if('p1' == tmp)
{
p1CureId = id;
p1CurePrice = items[index].value;
}
}
}
}           
}
items = Form.getInputs('step1','checkbox');        
for (var index = 0; index < items.length; ++index) 
{
if(items[index].checked)
{
var id = items[index].id;          
var tmp1 = id.substr(0, 10);
if('p0Activity' == tmp1)
{
var name = [items[index].name];       
p0ActivitiesId = p0ActivitiesId.concat(name);    
var value = [items[index].value];       
p0ActivitiesPrice = p0ActivitiesPrice.concat(value);      
} 
else if('p1Activity' == tmp1)
{
var name = [items[index].name];       
p1ActivitiesId = p1ActivitiesId.concat(name);    
var value = [items[index].value];       
p1ActivitiesPrice = p1ActivitiesPrice.concat(value);      
}
else if('p0Treatmen' == tmp1)
{
var name = [items[index].name];       
p0TreatmentsId = p0TreatmentsId.concat(name);    
var value = [items[index].value];       
p0TreatmentsPrice = p0TreatmentsPrice.concat(value);      
}
else if ('p1Treatmen' == tmp1)
{
var name = [items[index].name];       
p1TreatmentsId = p1TreatmentsId.concat(name);    
var value = [items[index].value];       
p1TreatmentsPrice = p1TreatmentsPrice.concat(value);      
}
}
}
var data = {
'stepOrder':'',
'p0':'',
'p1':'',
'net':'' 
};
data['stepOrder'] = '0';
data['p0'] = {
'protectionType':'',
'protectionPrice':'',
'title':'',
'firstName':'',
'lastName':'',
'cureId':'',
'curePrice':'',
'treatmentsId':'',
'treatmentsPrice':'',
'activitiesId':'',
'activitiesPrice':''
};
var UTF8_tool = function (){}
UTF8_tool.encode = function (string)
{
var string = string.replace(/\r\n/g,"\n");;
var utftext = "";
for (var n = 0; n < string.length; n++)
{
var c = string.charCodeAt(n);
if (c < 128) 
{
utftext += String.fromCharCode(c);
}
else if((c > 127) && (c < 2048))
{
utftext += String.fromCharCode((c >> 6) | 192);
utftext += String.fromCharCode((c & 63) | 128);
}
else
{
utftext += String.fromCharCode((c >> 12) | 224);
utftext += String.fromCharCode(((c >> 6) & 63) | 128);
utftext += String.fromCharCode((c & 63) | 128);
}
}
return utftext;
};
data['p0']['protectionType'] = p0ProtectionType;  
data['p0']['protectionPrice'] = p0ProtectionPrice; 
data['p0']['title'] = p0Title; 
data['p0']['firstName'] = UTF8_tool.encode(p0FirstName);
data['p0']['lastName'] =  UTF8_tool.encode(p0LastName);
data['p0']['cureId'] =  p0CureId;
data['p0']['curePrice'] =  p0CurePrice;     
data['p0']['treatmentsId'] =  p0TreatmentsId;
data['p0']['treatmentsPrice'] =  p0TreatmentsPrice;     
data['p0']['activitiesId'] =  p0ActivitiesId;
data['p0']['activitiesPrice'] =  p0ActivitiesPrice;     
data['p1'] = {
'protectionType':'',
'protectionPrice':'',
'title':'',
'firstName':'',
'lastName':'',
'cureId':'',
'curePrice':'',
'treatmentsId':'',
'treatmentsPrice':'',
'activitiesId':'',
'activitiesPrice':''
};
data['p1']['protectionType'] = p1ProtectionType;  
data['p1']['protectionPrice'] = p1ProtectionPrice; 
data['p1']['title'] = p1Title; 
data['p1']['firstName'] = UTF8_tool.encode(p1FirstName);  
data['p1']['lastName'] =  UTF8_tool.encode(p1LastName);
data['p1']['cureId'] =  p1CureId;
data['p1']['curePrice'] =  p1CurePrice;     
data['p1']['treatmentsId'] =  p1TreatmentsId;
data['p1']['treatmentsPrice'] =  p1TreatmentsPrice;     
data['p1']['activitiesId'] =  p1ActivitiesId;
data['p1']['activitiesPrice'] =  p1ActivitiesPrice;     
data['net'] = net;         
var tmp = $('data');
var tmp1 = $H(data).toJSON();
tmp.value = tmp1;
formNode.enable(formNode);  
}
function step1ProtectionPriceManage(e)
{
var element = Event.element(e);
var tableRadio = [];  
var tableCheckbox = [];  
var tableP0 = [];  
var tableP1 = []; 
var net0 = 0;
var net1 = 0;
var landCat = $F('landCat');
var countryCat = $F('countryCat');
var protectionPrices = [];
var items = Form.getInputs('step1','radio');    
for (var index = 0; index < items.length; ++index) 
{
if(items[index].checked)
{
tableRadio.push(items[index].id);
}
}
items = Form.getInputs('step1','checkbox');    
for (var index = 0; index < items.length; ++index) 
{
if(items[index].checked)
{
tableCheckbox.push(items[index].id);      
}
}
var table = tableRadio.concat(tableCheckbox);
for (var index = 0; index < table.length; ++index) 
{
var tmp = table[index];
tmp = tmp.substr(0, 2);
if('p0' == tmp)
{
tableP0.push(table[index]);
}
else if('p1' == tmp)
{
tableP1.push(table[index]);
}   
}
tableP0.each(function(n){ net0 += parseFloat($(n).value); });         
protectionPrices = getProtectionPrices(landCat, countryCat, net0);
for(var index = 0; index < protectionPrices.length; ++index)
{
var node = $('a0_protection_'+index);
node.value = protectionPrices[index];
node = $('txt_a0_protection_'+index);  
node.innerHTML = protectionPrices[index] + '&nbsp;&euro;';        
}
tableP1.each(function(n){ net1 += parseFloat($(n).value); });         
protectionPrices = getProtectionPrices(landCat, countryCat, net1);
for(var index = 0; index < protectionPrices.length; ++index)
{
var node = $('a1_protection_'+index);
node.value = protectionPrices[index];
node = $('txt_a1_protection_'+index);        
node.innerHTML = protectionPrices[index] + '&nbsp;&euro;';           
}
}         
function getIsLongHaul(landCat,countryCat)
{
var destinationEuropeCat = this.destinationEuropeCat; 
var destinationAfricaCat = this.destinationAfricaCat;              
if(!landCat)
return false;
if(!countryCat)
return false;
switch(landCat)
{
case destinationEuropeCat:
return false;
break;
case destinationAfricaCat:
switch(countryCat)
{
case destinationMoroccoCat: 
return false;
break;
case destinationTunisiaCat:
return false;
break;
default:
return true;
}
break;
default :
return true;
}
}
function getProtectionPrices(landCat, countryCat, travelPrice)     
{
if(!landCat)
return false;
if(!countryCat)
return false;
if(!travelPrice)
return false;
var travelPrice = parseFloat(travelPrice);
if(travelPrice <= 0)
return false;
protectionPrices = []; 
var isLongHaul = getIsLongHaul(landCat,countryCat);
if(travelPrice < 400)
{    
price = 14;
} 
if(travelPrice >= 400)
{
price = travelPrice*(3.4/100);
if(price < 18)
price = 18; 
}
protectionPrices[0] = price;
if(travelPrice < 400)
{    
price = 16;
} 
if(travelPrice >= 400 && travelPrice < 5000)
{
if(isLongHaul)
{
price = 46;
}
else
{
price = 25;
}   
}
if(travelPrice >= 5000)
{
price = travelPrice*(3.8/100);            
}
protectionPrices[1] = price;              
if(travelPrice < 400)
{    
price = 18;
} 
if(travelPrice >= 400 && travelPrice < 5000)
{
if(isLongHaul)
{
price = 60;
}
else
{
price = 38;
}   
}
if(travelPrice >= 5000)
{
price = travelPrice*(4.8/100);            
}
protectionPrices[2] = price; 
protectionPrices[3] = 20;      
protectionPrices[4] = 0;
for (var index = 0; index < protectionPrices.length; ++index) 
{
protectionPrices[index] = Math.round(protectionPrices[index]);    
}
return protectionPrices;         
}
function step1NetPriceManage(e)
{
var element = Event.element(e);
var tableRadio = [];
var pricesRadio = [];
var tableCheckbox = [];
var pricesCheckbox = [];
var table = [];
var net = 0;
step1ProtectionPriceManage(e);
var items = Form.getInputs('step1','radio');    
for (var index = 0; index < items.length; ++index) 
{
if(items[index].checked)
{
var name = items[index].name;
var value = items[index].value;
tableRadio[name] = value;
pricesRadio[index] = parseFloat(value);  
}
}
var items = Form.getInputs('step1','checkbox');    
for (var index = 0; index < items.length; ++index) 
{
if(items[index].checked)
{
var name = items[index].name;
var value = items[index].value;
tableCheckbox[name] = value;
pricesCheckbox[index] = parseFloat(value);
}
}
var prices = pricesRadio.concat(pricesCheckbox);
prices.each(function(n){ 
if(undefined == n)
{

}
else
{
net += n; 
}
});           
$('net').innerHTML = net + '&nbsp;&euro;';
}
function switchToAJAXBook(step)
{
switch(step)
{
case 'step1':
var formNode = $('quote');
formNode.disable('formNode');
var personsNb = $F('personsNb'); 
var date = $F(DATE_ID);
var travel = $F('travel');
if($('bookingWHProcess')) 
{
var oneSingleRoom = $('oneSingleRoom');
var oneTwinRoom = $('oneTwinRoom');
var twoSingleRooms = $('twoSingleRooms');
var hasNoCurist = $('hasNoCurist').checked;         
if(oneSingleRoom.checked)
{
var personsAndRooms = 0;
var firstPersonClassRoom = $F('onePersonClass');
var secondPersonClassRoom = false;
var firstPersonPension = $F('onePersonPension');
var secondPersonPension = false;
}
else if(oneTwinRoom.checked)
{
var personsAndRooms = 1; 
var firstPersonClassRoom = $F('firstPersonOneRoomClass');
var secondPersonClassRoom = $F('firstPersonOneRoomClass');
var firstPersonPension = $F('firstPersonOneRoomPension'); 
var secondPersonPension = $F('secondPersonOneRoomPension');                   
}
else if(twoSingleRooms.checked)
{
var personsAndRooms = 2;
var firstPersonClassRoom = $F('firstPersonTwoRoomsClass');
var secondPersonClassRoom = $F('secondPersonTwoRoomsClass');
var firstPersonPension = $F('firstPersonTwoRoomsPension'); 
var secondPersonPension = $F('secondPersonTwoRoomsPension');   
}
var data = $H({
'formvars[step]' : step,
'formvars[personsNb]' : personsNb,
'formvars[date]' : date,
'formvars[personsAndRooms]': personsAndRooms,
'formvars[hasNoCurist]': hasNoCurist,
'formvars[travel]': travel,
'formvars[firstPersonClassRoom]': firstPersonClassRoom,
'formvars[firstPersonPension]': firstPersonPension,
'formvars[secondPersonClassRoom]': secondPersonClassRoom,
'formvars[secondPersonPension]': secondPersonPension
});
}
else if($('bookingWOHProcess'))
{
if(1 == personsNb)
{
var firstPersonPension = $F('onePersonPension');   
var secondPersonPension = false;                 
}
else if(2 == personsNb)
{
var firstPersonPension = $F('firstPersonTwoRoomsPension');       
var secondPersonPension = $F('secondPersonTwoRoomsPension');         
}
var data = $H({
'formvars[step]' : step,
'formvars[personsNb]' : personsNb,
'formvars[date]' : date,
'formvars[travel]': travel,
'formvars[firstPersonPension]': firstPersonPension,
'formvars[secondPersonPension]': secondPersonPension         
});
}
new Ajax.Updater({ success : 'update'}, URLBase + "?module=BookAjax",
{
method: 'post',
postBody: data.toQueryString(),
insertion: undefined,
onComplete: function() 
{
decorateLabels(); 
formNode.enable(formNode);
bindStep1(); 
new Effect.Parallel([
new Effect.Pulsate('update', { sync: true }),
new Effect.BlindDown('update', { sync: true })
], { duration: 2 });                
}     
});
break;
}
}
function personsAndRoomsManage(elt)
{
var oneSingleRoom = $('oneSingleRoom');
var oneTwinRoom = $('oneTwinRoom');
var twoSingleRooms = $('twoSingleRooms');
var personsAndRooms = Form.getInputs('quote','radio','personsAndRooms');                       
for (var index = 0; index < personsAndRooms.length; ++index) 
{
if(personsAndRooms[index].id == elt.id)
{
personsAndRooms[index].checked == true;
}
else
{
personsAndRooms[index].checked == false;
}
}          
switch(elt.id)
{
case 'oneTwinRoom':
setHidden($('onePersonClassPension'));
setVisible($('twoPersonsOneRoomClassPension'));
setHidden($('twoPersonsTwoRoomsClassPension'));
break;
case 'oneSingleRoom':
setVisible($('onePersonClassPension'));
setHidden($('twoPersonsOneRoomClassPension'));
setHidden($('twoPersonsTwoRoomsClassPension'));
break;
case 'twoSingleRooms':
setHidden($('onePersonClassPension'));
setHidden($('twoPersonsOneRoomClassPension'));
setVisible($('twoPersonsTwoRoomsClassPension'));
break;
default:
break;
}
}
function setVisible(elt)
{
if(Element.hasClassName(elt, 'hidden'))
{
Element.removeClassName(elt, 'hidden'); 
if(!Element.hasClassName(elt, 'visible'))
{
Element.addClassName(elt, 'visible'); 
}  
}
}
function setHidden(elt)
{
if(Element.hasClassName(elt, 'visible'))
{
Element.removeClassName(elt, 'visible'); 
if(!Element.hasClassName(elt, 'hidden'))
{
Element.addClassName(elt, 'hidden'); 
}  
}
}
function personsNbManageWOH(personsNb)
{
var onePersonClassPension = $('onePersonClassPension');
var twoPersonsTwoRoomsClassPension = $('twoPersonsTwoRoomsClassPension');
switch(Field.getValue(personsNb))
{
case '2':
setHidden(onePersonClassPension);
setVisible(twoPersonsTwoRoomsClassPension);   
Form.reset('quote');
break;
case '1':
setHidden(twoPersonsTwoRoomsClassPension);       
setVisible(onePersonClassPension);
break;
}  
}
function personsNbManageWH(personsNb)
{
var onePersonRoom = $('onePersonRoom');
var twoPersonsRooms = $('twoPersonsRooms'); 
var onePersonClassPension = $('onePersonClassPension');
var twoPersonsOneRoomClassPension = $('twoPersonsOneRoomClassPension');
var twoPersonsTwoRoomsClassPension = $('twoPersonsTwoRoomsClassPension');
var oneSingleRoom = $('oneSingleRoom');
var oneTwinRoom = $('oneTwinRoom');
var twoSingleRooms = $('twoSingleRooms');
var personsAndRooms = Form.getInputs('quote','radio','personsAndRooms');
var hasNotCuristManage = $('hasNotCuristManage');
switch(Field.getValue(personsNb))
{
case '2':
setHidden(onePersonRoom);
setVisible(twoPersonsRooms);
setHidden(onePersonClassPension);
setVisible(twoPersonsOneRoomClassPension);
setHidden(twoPersonsTwoRoomsClassPension);  
setVisible(hasNotCuristManage);  
Form.reset('quote');
personsAndRooms[0].value = '0';
personsAndRooms[1].value = '1';
personsAndRooms[1].checked = true;
personsAndRooms[2].value = '0'; 
break;
case '1':
$('hasNoCurist').checked = false;
setVisible(onePersonRoom);
setHidden(twoPersonsRooms);
setHidden(hasNotCuristManage); 
setHidden(twoPersonsOneRoomClassPension);            
setHidden(twoPersonsTwoRoomsClassPension);       
setVisible(onePersonClassPension);
personsAndRooms[0].value = '1'; 
personsAndRooms[0].checked = true;
personsAndRooms[1].value = '0';
personsAndRooms[2].value = '0';  
break;
}  
}
function init()
{
if($('quote'))
{
if($('bookingWHProcess'))
{
bindQuoteWH();
}
else if($('bookingWOHProcess'))
{
bindQuoteWOH();
}
}
}
Event.observe(window, 'load', init);