var currentWindow = "";
var CONST_CHEKOUT = "checkoutCountry";
var CONST_CHEKIN  = "checkinCountry";

//var initialise au document rady de la first step
var JS_PATH = "";

function setJsPath(path){
	JS_PATH=path;
}

Waiting = function(){
	this.id = "waiting";
	this.container = getObj(this.id);
	this.timer = new Array();
	this.text = ""+this.container.innerHTML;
}

Waiting.prototype.start = function() {
	this.timer[this.timer.length] = setInterval("getWaiting().load()", 200);
	this.container.innerHTML = this.text;
	this.container.style.display = "block";
	getObj("checkoutLocation").disabled = true;
	getObj("checkinLocation").disabled = true;
	getObj("checkoutLocationRange").disabled = true;
	getObj("checkinLocationRange").disabled = true;
	getObj("checkoutLocationDetail").style.display = "none";
	getObj("checkinLocationDetail").style.display = "none";
}

Waiting.prototype.load = function() {
	if(this.container.innerHTML.length<35)
		this.container.innerHTML += ".";
	else
		this.container.innerHTML = this.text;
}

Waiting.prototype.startSubmit = function() {
	this.container.style.display = "block";
	this.timer = setInterval("getWaiting().load()", 200);
}

Waiting.prototype.stop = function(){
	for(var count=0;count<this.timer.length;count++)
		clearInterval(this.timer[count]);
	this.container.style.display = "none";
	getObj("checkoutLocation").disabled = false;
	getObj("checkinLocation").disabled = false;
	getObj("checkoutLocationRange").disabled = false;
	getObj("checkinLocationRange").disabled = false;
}

var WAITING_INSTANCE = null;
function getWaiting() {
	if(WAITING_INSTANCE==null)
		WAITING_INSTANCE = new Waiting();
	return WAITING_INSTANCE;
}

Stations = function(id){
	this.url = "../booking/ajaxStations.jsp";
	this.listener = null;
	this.id = id;
	this.stationCode = null;
}

Stations.prototype.getListener = function(){
	return getObj(this.id.replace("Country","Location"));
}

Stations.prototype.receive = function(result){

	if(result.indexOf("<!--SESSIONEXPIRED-->")!=-1)
		return;
		
	this.getListener().parentNode.innerHTML = result;

	if(supportedNavigator())
		setStationSelect(this.getListener(), null);	
	getWaiting().stop();	
	
	if(this.stationCode!=null){
		setStation(this.stationCode,this.getListener().id);
		//setValue(this.getListener().id, this.stationCode);	
		//seeStationDetail(getObj(this.id.replace("Country","Location")));
	}
	
	getObj(this.getListener().name).options[getObj(this.getListener().name).selectedIndex].value="";
	
	if(currentWindow!="")
	{
		currentWindow.close();
		currentWindow = "";
	}
}

Stations.prototype.getValues = function(stationCode){
	getObj("errorMessages").style.display = "none";
	this.stationCode = stationCode;	
	getObj(this.getListener().id+"Range").value = CONST_CHOOSE;
	if(getSelectValue(this.id)=="")
		return;
	var parameter = "pkid="+getSelectValue(this.id)+"&component="+this.getListener().id;	
	getWaiting().start();
	sendData(this,HTTP_GET,this.url,parameter);
	if(this.id==CONST_CHEKOUT)
	{
		getObj(CONST_CHEKIN).selectedIndex = getObj(this.id).selectedIndex;
		myCheckInStations.getValues(stationCode);
	}	
}

Stations.prototype.setStation = function(obj){
	getObj("errorMessages").style.display = "none";

	if(obj.id.indexOf("checkin")!=-1)
		return;

	if(getObj(this.id).selectedIndex==getObj(CONST_CHEKIN).selectedIndex){
		myCheckInStations.getListener().selectedIndex = this.getListener().selectedIndex;
		getObj(myCheckInStations.getListener().id+"Range").value = getSelectHtml(this.getListener().id);
	}
}

var myCheckOutStations = new Stations(CONST_CHEKOUT);
var myCheckInStations = new Stations(CONST_CHEKIN);

/*** selet replace managemenr **/

function setStation(code, id) {
	setValue(id,code);
	getObj(id+"Range").value = getSelectHtml(id);
	getObj(id+"Range").title = getSelectHtml(id);
	getObj(id+"Detail").style.display = "none";
	if(id.indexOf("checkout")!=-1)
		myCheckOutStations.setStation(getObj(id));
	else
		myCheckInStations.setStation(getObj(id));
		
	seeStationDetail(getObj(id));
}

var objTextRange = null;
var isPassedByTextRange = false;
function setTextRange(obj,e) {

	var code;
	if (!e) var e = window.event;
	if (e.keyCode) code = e.keyCode;
	else if (e.which) code = e.which;
	
	
	var frameSet = (obj.id.indexOf("checkout")!=-1)?window.frames.checkoutLocationDetail:window.frames.checkinLocationDetail;	
	var id = obj.id.replace("Range", "");
	if(code==27) //arrow down
	{
		getObj(id+"Detail").style.display = "none";
		return;
	}
	if(code==40) //arrow down
	{
		frameSet.setNextSibling();
		return;
	}	

	if(code==38) //arrow down
	{
		frameSet.setPreviousSibling();
		return;
	}	
	
	if(code==13) //enter
	{
		frameSet.selectCurrentNode();
		return;
	}		
	isPassedByTextRange = true;
	getObj(id+"Detail").style.display = "block";
	setStationSelect(getObj(id), obj.value);
}

function setStationSelect(component, text) {	
	var objIframe = null;
	var options = component.options;
	var iFrame = getObj(component.id+"Detail");
	var textLength = (text==null)?0:text.length;
	

	iFrame = getObj(component.id + "Detail");
	if(iFrame == undefined || iFrame == null)
		objIframe = document;
	else if (iFrame.contentDocument)
		objIframe = iFrame.contentDocument;
	else if (iFrame.contentWindow)
		objIframe = iFrame.contentWindow.document;
	else if (iFrame.document)
		objIframe = iFrame.document;
			

	var optionsDisplayed = 0;
	var firstOptionDisplayed = null;
	if(objIframe.getElementById("container") == undefined || objIframe.getElementById("container") == null){
		return;
	}
	var tbody = objIframe.getElementById("container").getElementsByTagName("tbody")[0];
	
	while(tbody.firstChild)	{
		tbody.removeChild(tbody.firstChild);
	}
	for(var count=0;count<options.length;count++){
		var isDisplay = true;
		var textToDisplay = options[count].text;
		var textNode = objIframe.createElement("nobr");
		if(text != null && text.replace(/^\s+|\s+$/g, '')!='') {
			isDisplay = (options[count].text.toUpperCase().indexOf(text.toUpperCase()) != -1);			
			if(isDisplay) {		
				textToDisplay = options[count].text.substring(indexString+textLength);				
				
				var indexString = options[count].text.toUpperCase().indexOf(text.toUpperCase())+1;
				textToDisplay = options[count].text.substring(0, indexString-1);		
				textNode.appendChild(objIframe.createTextNode(textToDisplay));
				
				var strongNode = objIframe.createElement("strong");
				var uNode = objIframe.createElement("u");
				textToDisplay = options[count].text.substring(indexString-1, indexString+textLength);
				uNode.appendChild(objIframe.createTextNode(textToDisplay));
				strongNode.appendChild(uNode);
				textNode.appendChild(strongNode);	
							
				textToDisplay= options[count].text.substring(indexString+textLength);	
				textNode.appendChild(objIframe.createTextNode(textToDisplay));			
			}
		}
		else{
			textNode.appendChild(objIframe.createTextNode(textToDisplay));
		}
		if(isDisplay && textToDisplay != CONST_CHOOSE) {
			var col = objIframe.createElement("TR"); 
			col.onmouseover = function() {
				this.className="stationOver";
			}
			col.onmouseout = function() {
				this.className="stationOut";
			}
			var cell = objIframe.createElement("TD"); 
			cell.onclick = function() {
				window.parent.setStation(options[this.firstChild.value].value, component.id);
			};
			

			var indexNODE = objIframe.createElement("input");
			indexNODE.type = "hidden";
			indexNODE.value = count;
			cell.appendChild(indexNODE);
			cell.appendChild(textNode);
			col.appendChild(cell);
			tbody.appendChild(col);
			optionsDisplayed++;
		}
	}
	
	var frameSet = (component.id.indexOf("checkout")!=-1)?window.frames.checkoutLocationDetail:window.frames.checkinLocationDetail;
		frameSet.clearData();

	if(navigator.appName == "Netscape"){
		iFrame.style.height = 300;
		iFrame.scrolling = "yes";
	}
	else{

		if(optionsDisplayed == 0) {
			iFrame.style.display = "none";
			iFrame.scrolling = "no";
		}
		else if(optionsDisplayed<10 ) {
			iFrame.style.height = (optionsDisplayed * 15)+5;
			iFrame.scrolling = "no";
		}
		else {
			iFrame.style.height = (10 * 15) + 5;
			iFrame.scrolling = "yes";
		}
	}
}



function displayLocationDetail(obj) {
	
	obj.value = "";
	var id = obj.id.replace("Range", "Detail");
	if(document.getElementById("checkoutCountry").selectedIndex==0){
		getObj(id).style.display = "none";
	}
	else{
		getObj(id).style.display = "block";
		if(isPassedByTextRange) {
			isPassedByTextRange = false;
			setStationSelect(getObj(id.replace("Detail","")), null);
		}
	}
}

//open popup window
function openPopup(url, from){
	if(from==SEE_CHECKOUT) {
		var month = parseInt(getSelectValue("reservationcheckoutmonthyear").split("_")[0],10);
		var year = parseInt(getSelectValue("reservationcheckoutmonthyear").split("_")[1],10);
		
		if(month<10)
			month = "0" + month;
		var day = getObj('reservationcheckoutday').value;
		if(day<10)
			day = "0" + day;
		var chekoutDate = month+day+year;
		currentWindow = window.open(url+"&date="+chekoutDate,'','toolbar=0, location=0, directories=0, status=0, scrollbars=1, resizable=1, copyhistory=0, menuBar=0, width=400, height=400');
	} else {
		var month = parseInt(getSelectValue("reservationcheckinmonthyear").split("_")[0],10);
		var year = parseInt(getSelectValue("reservationcheckinmonthyear").split("_")[1],10);

		if(month<10)
			month = "0" + month;
		var day = getObj('reservationcheckinday').value;
		if(day<10)
			day = "0" + day;
		var chekinDate = month+day+year;
		currentWindow = window.open(url+"&date="+chekinDate,'','toolbar=0, location=0, directories=0, status=0, scrollbars=1, resizable=1, copyhistory=0, menuBar=0, width=400, height=400');
	}
}

function openSearchNearest(url) {
		var month = parseInt(getSelectValue("reservationcheckoutmonthyear").split("_")[0],10);
		var year = parseInt(getSelectValue("reservationcheckoutmonthyear").split("_")[1],10);
		if(month < 10)
			month = "0" + month;
		var day = getObj('reservationcheckoutday').value;
		if(getObj('reservationcheckoutday').value.length == 1)
			day = "0" + day;
		var chekoutDate = month+day+year;
		var countryCode = getSelectValue("checkoutCountry");
		var countryOfResidenceId = getSelectValue("countryOfResidence");
		window.self.location.href = url + "?date="+ chekoutDate + "&countryCode=" + countryCode + "&countryOfResidenceId=" + countryOfResidenceId;		
}


//submit this form
function submitForm(){
	getWaiting().startSubmit();
	var checkoutMonth = parseInt(getSelectValue("reservationcheckoutmonthyear").split("_")[0],10);
	var checkinMonth = parseInt(getSelectValue("reservationcheckinmonthyear").split("_")[0],10);
	
	setValue("checkoutMonth",checkoutMonth);
	setValue("checkoutYear",getSelectValue("reservationcheckoutmonthyear").split("_")[1]);
	setValue("checkinMonth",checkinMonth);
	setValue("checkinYear",getSelectValue("reservationcheckinmonthyear").split("_")[1]);
//Bug TRAC#67 QC#10230 Correction temporaire sur la gestion du bouton précedent à cause d'un défaut dans la liste deroulante AJAX
// avec utilisation du bouton précedent
//	getObj("stationCodeCheckout").value = getSelectValue(CONST_CHEKOUT.replace("Country","Location"));
//	getObj("stationCodeCheckin").value = getSelectValue(CONST_CHEKIN.replace("Country","Location"));

	// defect #11032 - see #360	
	getObj("checkoutLocationRange").disabled = "true";
	getObj("checkinLocationRange").disabled = "true";

	return true;
	
}

//display the link "see station detail"
var SEE_CHECKOUT = "seeCheckoutStation";
var SEE_CHECKIN  = "seeCheckinStation";
var stationProvider = "";
function seeStationDetail(obj){
	var selectedValue = getSelectValue(obj.id);
	if(selectedValue=="") return;
	id = (obj.id=="checkoutLocation")?SEE_CHECKOUT:SEE_CHECKIN;
	stationProvider = id;
	getObj(id).style.display = "block";
	getObj(id).childNodes[0].title = selectedValue;
	if(id==SEE_CHECKOUT)
	{
		getObj(SEE_CHECKIN).style.display = "block";
		getObj(SEE_CHECKIN).childNodes[0].title = selectedValue;
	}
	
}


//information from "choose country of residence"
function callSetCountryOfResidence(value,name){
	setValue("countryOfResidence",value);
	getObj("countryName").innerHTML = name;
}

// Set when browser sniffer implementation does not retrieve the country
function setCountryLocation(){
	if(getSelectValue("countryOfResidence")=="")
		setText("countryOfResidence", "FRANCE");
	else
		getObj("countryName").innerHTML = getSelectHtml("countryOfResidence");
}

//Information from search nearest location
// @stationCode : stationCode
// @countryCode : countryCode
function setLocation(stationCode, countryCode)
{
	setValue("checkoutCountry", countryCode);
	setValue("checkinCountry", countryCode);
	myCheckOutStations.getValues(stationCode);
	myCheckInStations.getValues(stationCode);
}


var myEuropcarForm = "";

function initialize(){	
	myEuropcarForm = new EuropcarForm();
	setCountryLocation();
	//setTimeout(initWithWaitingTimeOut,10);
	//setTimeout(initWithWaitingTimeIn,10);
}

function initWithWaitingTimeOut(){
	if(getObj("stationCodeCheckout").value!="")
		myCheckOutStations.getValues(getObj("stationCodeCheckout").value);
		
	if(supportedNavigator()) 
		setStationSelect(getObj("checkoutLocation"),null);
	else
	{
		getObj("checkoutLocation").parentNode.style.display = "block";
		getObj("checkinLocation").parentNode.style.display = "block";
	}
	
	if(getObj("checkoutLocation").options.length>1)  {
		getObj("checkoutLocationRange").value = getSelectHtml("checkoutLocation");
		seeStationDetail(getObj("checkoutLocation"));
	}
}
function initWithWaitingTimeIn(){
	if(getObj("stationCodeCheckin").value!="") 
		myCheckInStations.getValues(getObj("stationCodeCheckin").value);
		
	if(supportedNavigator()) 
		setStationSelect(getObj("checkinLocation"),null);
	else
	{
		getObj("checkoutLocationRange").style.display = "none";
		getObj("checkinLocationRange").style.display = "none";
	}
	if(getObj("checkinLocation").options.length>1)  {
		getObj("checkinLocationRange").value = getSelectHtml("checkinLocation");
		seeStationDetail(getObj("checkinLocation"));
	}
}

function clearInput(obj){
	obj.value="";
}

function setPassword(id,pwd) {
	getObj("driverId").value = id;
	getObj("password").value = pwd;
}


//get the browser cookie support
var cookieEnabled = true;
if (getCookie("TestCookie") == null)
{
	setCookie("TestCookie",1);
	cookieEnabled = (getCookie("TestCookie") != null )? true : false;
	deleteCookie("TestCookie");
}

if (!cookieEnabled)
{
	alert("To use our website, you must accept First Party/internal cookies in your browser Privacy/Security options");	
} 

function setCookie(name, value, expires, path, domain, secure)
{
	document.cookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}


function getCookie(name)
{
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1)
	{
		begin = dc.indexOf(prefix);
		if (begin != 0) 
		{
			return null;
		}
	}
	else
	{
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1)
	{
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}
 
function deleteCookie(name, path, domain)
{
	if (getCookie(name))
	{
	document.cookie = name + "=" +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}


