// JavaScript Document
var sRootUrl = '';
var arrRowCellsPassengers = null;
var arrRowCellsFlights = null;
var arrRowCellsHotels = null;
var arrRowCellsCars = null;
var arrRowCellsRails = null;

function InitRow (pRowBodyId){
	
	var _arrResult = new Array();
	var _oBodyRows = document.getElementById(pRowBodyId);
	var _oRow = _oBodyRows.rows[0]; 
	var _arrCells = _oRow.cells;
	for(var i = 0; i < _arrCells.length; i++){
		_arrResult[i] = new Object();
		_arrResult[i]['width'] = _arrCells[i].style.width;
		_arrResult[i]['innerHTML'] = _arrCells[i].innerHTML;
	}
	_oBodyRows.deleteRow(0);
	return _arrResult;
}

function ManageRows(pRowBodyId, pCount, pRowCells){
	
	var _oBodyRows = document.getElementById(pRowBodyId);
	var _iNbrRows = _oBodyRows.rows.length;
	var _iRowCount = pCount;
	
	if(!isNaN(_iRowCount)){
		if(_iNbrRows != _iRowCount){
			if(_iRowCount > 0){
				if(_iNbrRows > _iRowCount){
					// we have to remove some rows
					for(var i =(_iNbrRows-1); i > (_iRowCount-1); i--){
						_oBodyRows.deleteRow(i);
					}
				} else {
					// we have to add some rows
					for(var i = _iNbrRows; i < _iRowCount; i++){
						var _oRow = _oBodyRows.insertRow(i);
						for (var j = 0 ; j < pRowCells.length; j++){
							_oCell = _oRow.insertCell(j);
							_oCell.innerHTML = pRowCells[j]['innerHTML'].replace(/__Index/gi, i);
							_oCell.style.width = pRowCells[j]['width'];
						}
					}
				}
			} else {
				for(var i = (_iNbrRows-1); i >= 0; i--){
					_oBodyRows.deleteRow(i);
				}
			} 
		}
	}
}

function InitTravelRequestForm(pRootUrl){
	sRootUrl = pRootUrl;
	arrRowCellsPassengers = InitRow('PassengersListBody');
	arrRowCellsFlights = InitRow('FlightsListBody');
	arrRowCellsHotels = InitRow('HotelsListBody');
	arrRowCellsCars = InitRow('CarsListBody');
	arrRowCellsRails = InitRow('RailsListBody');
}

function ShowHideReservationBlock(pCheckBoxID, pBlockID){
	if(document.getElementById(pCheckBoxID).checked == true){
		ShowBlockElement(pBlockID);
	} else {
		HideBlockElement(pBlockID);
	}
}

function ManagePassengersRows(){
	ManageRows('PassengersListBody', new Number(document.getElementById('PassengersNumber').value), arrRowCellsPassengers);
}

function ManageFlightsRows(){
	var _iCount = 0;
	var _iPassengerNumber = new Number(document.getElementById('FlightPassengersNumber').value);
	var _iTransits = new Number (document.getElementById('FlightTransits').value);
	
	if(!isNaN(_iPassengerNumber)){
		_iCount = 2 * _iPassengerNumber;
		_iCount += (_iPassengerNumber * (_iTransits * 2));
		ManageRows('FlightsListBody', _iCount, arrRowCellsFlights);
	}
}

function ManageHotelsRows(){
	ManageRows('HotelsListBody', new Number(document.getElementById('HotelGuestsNumber').value), arrRowCellsHotels);
}

function ManageCarsRows(){
	ManageRows('CarsListBody', new Number(document.getElementById('CarsNumber').value), arrRowCellsCars);
}

function ManageRailsRows(){
	ManageRows('RailsListBody', new Number(document.getElementById('RailPlacesNumber').value), arrRowCellsRails);
}

function DeleteCustomer(pCustomerID) {
	
	var _bDelete = confirm('Sie sind dabei den Kunde zu löschen. Wollen Sie mit dem löschen fortfahren.');
	
	if(_bDelete){
		if(CreateXMLHttpRequest()){
	    	RequestDeleteCustomer(pCustomerID);
	    }
	}
}

function RequestCodeValidation() {
	
	if(CreateXMLHttpRequest()){
		// Build Url
		var _sUrl = sRootUrl;
		// Build param string to send by get
		_sUrl += '?module=traveltools&page=ajax&action=validateSCode';
		_sUrl += '&SCode=' + document.getElementById('SCode').value;
		
		// Execute the request
		oHttpRequest.onreadystatechange = CodeValidationResponse;
		oHttpRequest.open('get', _sUrl, true);
		oHttpRequest.send(null);
	}	
}

function CodeValidationResponse () {
	
	if (oHttpRequest.readyState == 4) {
		if (oHttpRequest.status == 200) {

			// put response into xmlobject and text object
			var _xmlDoc = oHttpRequest.responseXML;
			var _iValidated = _xmlDoc.documentElement.getElementsByTagName('validated').item(0).firstChild.nodeValue;
			if(_iValidated == '1'){
				document.getElementById('frmTravelRequest').submit();
			} else {
				alert('The security code is wrong. Please provide the correct security code.');
			}
		} else {
			alert('Bei dem Request ist ein Problem aufgetreten.');
		}
	}
}

function SubmitTravelRequest(){
	
	var _sErrorMessage = '';
	
	// Check approval request email
	if(document.getElementById('SendApprovalRequestYes').checked == true){
		if(IsEmail(document.getElementById('SendApprovalEmail').value) == 0){
			_sErrorMessage += 'Please provide an correct approval request email.\n';
		}		
	}
	// Check travel arranger firstname
	if(document.getElementById('TravelArrangerFirstname').value == ''){
		_sErrorMessage += 'Please provide an firstname.\n';
	}
	// Check travel arranger lastname
	if(document.getElementById('TravelArrangerLastname').value == ''){
		_sErrorMessage += 'Please provide an lastname.\n';
	}
	// Check travel arranger email
	if(IsEmail(document.getElementById('TravelArrangerEmail').value) == 0){
		_sErrorMessage += 'Please provide an correct email.\n';
	}
	// Check if we have an error
	if(_sErrorMessage == ''){
		// No more code validation 20-04-2010
		//RequestCodeValidation();
		// So lets submit the form
		document.getElementById('frmTravelRequest').submit();
	} else {
		alert(_sErrorMessage);
	}
}



