/**
* Browser check
*/
var ie4 = (document.all) ? true : false;
var validBrowser = (document.getElementById) ? true : false;

/**
* Visar ett div lager.
*/
function showLayer(layerName)
{
	if (document.getElementById(layerName))
	{		
		document.getElementById(layerName).style.display = "block";
	}
}

//

function hideLayer(layerName)
{
	if (document.getElementById(layerName))
	{
		document.getElementById(layerName).style.display = "none";
	}
}

//

function isLayerVisible(layerName)
{
	if (document.getElementById(layerName).style.display == "block")
		return true;
	else
		return false;
}

function updateDropDown()
{
	var objValue = document.getElementById("countrylist").value;
	var countrycodeValue = document.getElementById("countrycode").value;
		
	arrcountry=objValue.split(';');
	arrcountrycode=countrycodeValue.split(';');
	
	var stationlistValue = document.getElementById("stationlist").value;
	var stationcodeValue = document.getElementById("stationcode").value;
		
	arrstation=stationlistValue.split(';');
	arrstationcode=stationcodeValue.split(';');

	
	
	for(x=0;x<arrcountry.length;x++)
		{
			var w = arrcountry[x];

			if(x==0)
			{
				document.getElementById('pickup_country_code').options[x] = new Option(arrcountry[x], arrcountrycode[x], true, false);
			}
			else
			{
				document.getElementById('pickup_country_code').options[x] = new Option(arrcountry[x], arrcountrycode[x], false, false);
			}
			
		}

	
	
	
	for(x=0;x<arrstation.length;x++)
		{
			var w = arrstation[x];
			
			if(x==0)
			{
				document.getElementById('pickup_station_code').options[x] = new Option(arrstation[x], arrstationcode[x], true, false);
			}
			else
			{
				document.getElementById('pickup_station_code').options[x] = new Option(arrstation[x], arrstationcode[x], false, false);
			}
			
		}
}

/**
* Används vid post i componenter for att visa progressbar.
*/
function submitWithProgress()
{
	self.scrollTo(0, 0);
	showLayer("progress");
	return true;
}

/**
* Shows the progress bar when loading on the Confirm page. Also disables the Book- and Back-button
*/
function submitWithProgressConfirm()
{
	document.getElementById('book_button').style.display='none';
	document.getElementById('back_button').style.display='none';
	self.scrollTo(0, 0);
	showLayer("progress");	
	return true;
}

/***
 * This function should only be called from rentalcar.com on step 1.
 * This is to make sure that we have actually selected something in the word wheel.
 */
function submitFormStep1(formId_)
{
	topItem = document.getElementById('autocomplete_item_0');
	if(topItem != null && topItem.className == 'autocomplete_item_highlighted')
	{
		FO_SetValueToHiddenInput(topItem.innerHTML);
	}
	submitForm(formId_);
}

function submitForm(formId_)
{
	form = document.getElementById(formId_);
	form.submit();
}

function showLoader(loaderHTML_)
{
	loaderHTML = loaderHTML_.replace(/DELIMIT/g, '"');
	progressDiv = document.getElementById('progress');
	progressDiv.innerHTML = loaderHTML;
	progressDiv.style.display = 'block';
}

/**
* Används tillsammans med writeJSToggleButton i GlobalJs.
*/
function toggleDiv(divId, showButtonHtml, hideButtonHtml, firstTime)
{
	if (validBrowser)
	{
		if (isLayerVisible("firstDiv"+divId) || firstTime)
		{
			newHTML = '&nbsp;<a href="javascript:toggleDiv(\''+divId+'\', \''+showButtonHtml+'\', \''+hideButtonHtml+'\', false)">'+showButtonHtml+'</a>';

			hideLayer("firstDiv"+divId);
			showLayer("secondDiv"+divId);
		}
		else
		{
			newHTML = '&nbsp;<a href="javascript:toggleDiv(\''+divId+'\', \''+showButtonHtml+'\', \''+hideButtonHtml+'\', false)">'+hideButtonHtml+'</a>';

			showLayer("firstDiv"+divId);
			hideLayer("secondDiv"+divId);
		}
		document.getElementById("buttonDiv"+divId).innerHTML = newHTML;
	}
}

function toggleDivButton(divId, showButtonHtml, hideButtonHtml, firstTime)
{
	if (validBrowser)
	{
		if (firstTime)
		{
			newHTML = '&nbsp;<a href="javascript:toggleDiv(\''+divId+'\', \''+showButtonHtml+'\', \''+hideButtonHtml+'\', false)">'+showButtonHtml+'</a>';
		}
		else
		{
			newHTML = '&nbsp;<a href="javascript:toggleDiv(\''+divId+'\', \''+showButtonHtml+'\', \''+hideButtonHtml+'\', false)">'+hideButtonHtml+'</a>';
		}
		document.getElementById("buttonDiv"+divId).innerHTML = newHTML;
	}
}


/**
* Aktiverar print kommandot som finns under file->print.
*/
function printPage()
{
	if (window.print)
		window.print()
	else
		alert("Din webbläsare stöder inte det här kommandot.");
}

/**
* Sätter focus på första redigerbara controllen på webbsidan.
*/
function setElementFocus()
{
	if (document.forms.length > 0)
	{
		var field;

		if (typeof(document.standard) != 'undefined')
			field = document.standard;
		else
			field = document.forms[0];

		for (i = 0; i < field.length; i++)
		{
			if ((field.elements[i].type == "select-one") || (field.elements[i].type == "text") || (field.elements[i].type == "textarea") || (field.elements[i].type == "checkbox"))
			{
				field.elements[i].focus();
				break;
			}
		}
	}
}

/**
*	Används för att via en anchor kunna göra en post istället för en get.
*
*	Example:
* <code>
* 	<a href="javascript:doPostOnLink(\''.$queryStr.'\', \'\');">
*	</code>
*/
function doPostOnLink(query, target)
{
	var postTarget, postAction, postMethod;

	postTarget = document.forms[0].target;
	postAction = document.forms[0].action;
	postMethod = document.forms[0].method;

	if (query != '')
		document.forms[0].action = postAction+"?"+query;

	if (target != '')
		document.forms[0].target = target;

	document.forms[0].method = 'post';
	document.forms[0].submit();

	document.forms[0].target = postTarget;
	document.forms[0].action = postAction;
	document.forms[0].method = postMethod;
}

/**
* Används för att "disabla" en redigerbar kontroll. Det är inte en riktig
* disable, utan den bara ändrar färg på controllen, så att det ser ut som
* att den inte går att använda.
*/
function setElementEnabled(element, state)
{
	var bcolor;
	var fcolor;

	if (state == "false")
	{
		bcolor='#CCCCCC';
		fcolor='#AAAAAA';
	}
	else
	{
		bcolor='#FFFFFF';
		fcolor='#000000';
	}

	element.style.backgroundColor=bcolor;
	element.style.color=fcolor;
}

/**
* Sätter focus på en controll med ett visst id.
*/
function setIdFocus(id)
{
	document.getElementById(id).focus();
}

/**
* Scrollar till översta postionen på sidan.
*/
function scrollToTop()
{
	self.scrollTo(0, 0);
}

/**
* Handler för click på dateNav
*/
function dateOutboundNavClick(clickedDate)
{
	document.getElementById('date_outbound_nav_clicked_value').value = clickedDate;
	document.forms[0].onSubmit = submitWithProgress();
	document.forms[0].submit();
}

/**
* Handler för click på dateNav
*/
function dateInboundNavClick(clickedDate)
{
	document.getElementById('date_inbound_nav_clicked_value').value = clickedDate;
	document.forms[0].onSubmit = submitWithProgress();
	document.forms[0].submit();
}


/*
* Functions for displaying the correct Credit Card input fields depending on the selected credit card
****/

function toggleExtraCreditCardFields()
{
	list = document.getElementById('card_type_list');
	selectedCreditCard = list.options[list.selectedIndex].value;
	
	if (selectedCreditCard == 'maestro' || selectedCreditCard == 'solo')
	{
		updateCreditCardInputFields(false);
	}
	else
	{
		//'VISA/MASTERCARD/AMEX';
		updateCreditCardInputFields(true);
	}
	
}

function updateCreditCardInputFields(disabled)
{
	fields = ['issue_number_field', 'start_month_field', 'start_year_field']
	
	for (var i in fields)
	{
		document.getElementById(fields[i]).disabled = disabled;
	}
}

