
var ResultPrecision = 2;

/**********************************
/* IE 5.0 does not support the Array object
/* concept, this is the workaround.
/**********************************/

if (typeof Array.prototype.push == "undefined") {
  Array.prototype.push = function(str) {
    this[this.length] = str;
  }
}

function GomioRound( pDouble )
{
//alert ("in: " + pDouble);
	arrResult = new String( pDouble ).split(".");
	strResult = new String();
	if( arrResult.length == 1 )
	{
		strResult = arrResult[0] + ".00";
	}
	if( arrResult.length == 2 )
	{
		if (arrResult[1].length == 1) arrResult[1] = arrResult[1]+"0";
		if (arrResult[1].length > 2)
		{
			rest = parseInt(arrResult[1].substr(2,1));
			rounded = parseInt(arrResult[1].substr(0,2));
//alert ("round: " + rounded + " " + rest);
			if ( rest > 5) //rundungs fehler
				rounded += 1;
			arrResult[1] = rounded;
		}
		strResult = arrResult[0] + "." + arrResult[1];
	}
//alert ("out: " + strResult);
	return strResult;
}

function ConvertYYYYMMDDToDate( iYYYYMMDD )
{
	sYYYYMMDD = new String(iYYYYMMDD);
	if( sYYYYMMDD.length != 8 )
		alert("Script Error -> Please inform the administrator");

	Year  = sYYYYMMDD.substr( 0, 4);
	Month = sYYYYMMDD.substr( 4, 2);
	Day   = sYYYYMMDD.substr( 6, 2);

	return new Date( Year, Month - 1, Day );
}


/**********************************
/* class RoomType
/*
/* Remark:
/*   DdlId = DropDownList Id
/**********************************/
function RoomType( DdlId ,HostelId ,RoomTypeId ,Date ,Price, Beds)
{
//alert (DdlId + "-" + HostelId + "-"  +RoomTypeId + "-"  +Date + "-"  +Price + "-"  +Beds);
	//fields
	this.DdlId = DdlId;
	this.HostelId = HostelId;
	this.RoomTypeId = RoomTypeId;
	this.Date = rtConvertYYYYMMDDToDate( Date );
	this.Price = Price;
	this.Beds = Beds;

	//methods
	this._ConvertYYYYMMDDToDate = rtConvertYYYYMMDDToDate;
	this.GetBedCount = crGetBedCount;
}

function rtConvertYYYYMMDDToDate( iYYYYMMDD )
{
	return ConvertYYYYMMDDToDate( iYYYYMMDD )
}


/**********************************
/* class RoomTypes
/**********************************/
function RoomTypes()
{
  //fields
  this._arrRoomType = new Array();

	//methods
	this.Count = 0;
	this.Add = rtsAdd;
	this.GetDdlId_ByRoomTypeId = rtsGetDdlId_ByRoomTypeId;
	this.GetItem = rtsGetItem;
}

function rtsAdd( oRoomType )
{
	this._arrRoomType.push( oRoomType );
	this.Count++;
}

function rtsGetDdlId_ByRoomTypeId( RoomTypeId, YYYYMMDDDate )
{
	for( var i = 0; i < this._arrRoomType.length; i++ )
	{
		if (
					this._arrRoomType[i].RoomTypeId == RoomTypeId &&
					ConvertDateToYYYYMMDD( this._arrRoomType[i].Date ) == YYYYMMDDDate
				)
		{ return i; }
	}
}

function rtsGetItem( Index )
{
	return this._arrRoomType[Index];
}


/**********************************
/* class ChoosenRooms
/**********************************/
function ChoosenRooms()
{
  //fields
  this._arrRoomType = new Array();
  this.SelectedBedCount;

  //methods
  this.Add = crAdd;
	this.CalculateMaxBeds = crCalculateMaxBeds;
	this.CalculateTotalBeds = crCalculateTotalBeds;
	this.CalculateTotalPrice = crCalculateTotalPrice;
	this.CalculateAveragePrice = crCalculateAveragePrice;
	this.GetBedCount = crGetBedCount;
	this.Serialise = crSerialise;
}


function crAdd( oRoomType, SelectedBedCount )
{
	var Index = GetIndex( oRoomType.DdlId, this._arrRoomType );

	if( ! DoesRoomTypeExist(Index) ){
		oRoomType.SelectedBedCount = SelectedBedCount;
		this._arrRoomType.push( oRoomType );
	}else{
		this._arrRoomType[Index].SelectedBedCount = SelectedBedCount;
	}
}

function crCalculateMaxBeds()
{
	var i = 0;
	var j = 0;
	var Count = 0;
	var Length = this._arrRoomType.length;
	for( i = 0; i < Length; i++ )
	{
		var LastCount = this._arrRoomType[i].Beds * this._arrRoomType[i].SelectedBedCount;
		var LastDate = ConvertDateToYYYYMMDD(this._arrRoomType[i].Date);
		for( j = 0; j < Length; j++ )
		{
			if(j==i || LastDate != ConvertDateToYYYYMMDD(this._arrRoomType[j].Date)) continue;
			LastCount = LastCount + this._arrRoomType[j].Beds * this._arrRoomType[j].SelectedBedCount;
		}
		if (Count < LastCount) Count = LastCount;
	}
//alert (Total );
	return Count.toString();
}

function crCalculateTotalBeds()
{
	var Count = 0;
	for( i = 0; i < this._arrRoomType.length; i++ )
	{
		Count = Count + this._arrRoomType[i].Beds * this._arrRoomType[i].SelectedBedCount;
	}
//alert (Total );
	return Count.toString();
}

function crCalculateTotalPrice()
{
	var Total = 0.00;
	for( i = 0; i < this._arrRoomType.length; i++ )
	{
		Total = Total + this._arrRoomType[i].Price * this._arrRoomType[i].SelectedBedCount;
	}
//alert (Total );
	return GomioRound( Total.toString() );
}

function crCalculateAveragePrice()
{
	var BedCount = 0;
	var TotalPrice = 0;

	for( i = 0; i < this._arrRoomType.length; i++ ){
		BedCount   = BedCount + this._arrRoomType[i].SelectedBedCount;
		TotalPrice = TotalPrice + this._arrRoomType[i].Price * this._arrRoomType[i].SelectedBedCount;
	}

	var Result = TotalPrice/BedCount;

	if( isNaN(Result) )
		return 0.00;
	else
		return GomioRound( Result.toString() );
}

function crGetBedCount()
{
	var BedCount = 0;
	for( i = 0; i < this._arrRoomType.length; i++ )
	{
		BedCount   = BedCount + this._arrRoomType[i].SelectedBedCount;
	}
	return BedCount;
}

/* Serialised: RoomTypeId, Date, BedCount; "...","...","..." */
function crSerialise()
{
	var Result = "";
	for( i = 0; i < this._arrRoomType.length; i++ )
	{
		if ( this._arrRoomType[i].SelectedBedCount > 0 )
		{
			var Hostel = "";
			Hostel = Hostel + this._arrRoomType[i].HostelId;
			Hostel = Hostel + ",";
			Hostel = Hostel + this._arrRoomType[i].RoomTypeId;
			Hostel = Hostel + ",";
			Hostel = Hostel + ConvertDateToYYYYMMDD( this._arrRoomType[i].Date );
			Hostel = Hostel + ",";
			Hostel = Hostel + this._arrRoomType[i].SelectedBedCount;
			Hostel = Hostel + ";";
			Result = Result + Hostel;
		}
	}

	if ( Result.length > 0 )
		Result = Result.substring( 0, Result.length - 1 );

	return Result;
}

function ConvertDateToYYYYMMDD( oDate )
{
	var Year = oDate.getFullYear();
	var Month = PadLeft( ( parseInt(oDate.getMonth()) + 1 ), "0", 2  );
	var Day = PadLeft( oDate.getDate(), "0", 2 ) ;

	return Year +""+ Month +""+ Day;
}

function PadLeft( Value, Character, Count )
{
	Value = new String(Value);

	CharactersToAdd = "";
	CharacterCountToAdd = Count - Value.length;
	for ( j = 0; j < CharacterCountToAdd; j++ )
	{
		CharactersToAdd = CharactersToAdd + Character;
	}
	return CharactersToAdd + Value;
}

/**********************************/

function DoesRoomTypeExist( Index )
{
	if( Index >= 0 )
		return true;
	return false;
}

function GetIndex( RoomTypeDdlId, arrRoomTypes )
{
	for( i = 0; i < arrRoomTypes.length ;i++ )
	{
		if ( RoomTypeDdlId == arrRoomTypes[i].DdlId  )
			return i;
	}
	return -1;
}

function ReCalculate()
{
	var Total = 0;
	for( i = 0; i < oRoomTypes.length ;i++ )
	{
		var SelectedBedCount = document.getElementById(oRoomTypes[i].DdlId).Value;
		if (SelectedBedCount > 0)
			Total = Total + oRoomTypes[i].Price * SelectedBedCount;
	}

	return GomioRound( Total.toString() );
}

function BookNow( HostelId, CrpType )
{
	if ( oChoosenRooms.GetBedCount() == 0 )
	{
		alert("Please choose at least one bed.");
		return;
	}
	document.getElementById( "hhdHostelId" ).value = HostelId;
	document.getElementById( "hhdChoosenBedsSerialised" ).value = oChoosenRooms.Serialise();
	if(CrpType == "Hostel"){
		document.frmBookNow.action = "HostelBooking.aspx?HostelId=" + HostelId + "&Step=4"
	}else{
		document.frmBookNow.action = "HostelBooking.aspx?GroupId=" + HostelId + "&Step=4"
	}
	document.frmBookNow.submit();
}

