function showRec(showID, name){
	this.showID = showID;
	this.name = name;
	return this;
}

function ticketRec(amount, price){
	this.amount = amount;
	this.price = price;
	return this;
}

var tickets = new Array();

function dateRec(date, time, maxAmount, hallID){
	this.date = date;
	this.time = time;
	this.maxAmount = maxAmount;
	this.hallID = hallID;
	return this;
}

function hallRec(hallID, name){
	this.hallID = hallID;
	this.name = name;
	return this;
}


function buildShows(selected){
	var index = 0;
	for (var i = 0; i < aP.length; i++){
		var caption = aP[i].name;
		getObj('show').options[i] = new Option(caption, aP[i].showID);
		if (aP[i].showID == selected)
			index = i;
	}
	//getObj('show').selectedIndex = index;
	getObj('show').selectedIndex = curPerfIdx;
}

function buildDates(){
  var curIndex = 0;
  var sIdx = 0;
  var curDate = ''; 
  for (var i = 0; i < aD.length; i++){
  
  	if (curDate != aD[i].date){
		//var caption = aDate[i].date + ' (' + aHalls[aDate[i].hallID].name + ')';
		var caption = aD[i].date;
		getObj('date').options[curIndex] = new Option(caption, i);
		curDate = aD[i].date;
		curIndex++;
	}
	if (i == curDateTimeIdx)
	    sIdx = curIndex - 1;
  }
  //alert('curDateTime: ' + curDateTimeIdx + ' sIdx: ' + sIdx);
  getObj('date').selectedIndex = sIdx;
  buildTime(getObj('date').options[sIdx
  ].text);
}


function buildTime(myDate){
  date = myDate.substring(0,10);
  //empty time-list
  for (var i = getObj('time').length - 1; i > -1; i--){
  	getObj('time').remove(i);
  }
  //build new time-list
  var curIndex = 0;
  var sIdx = 0;  
  for (var i = 0; i < aD.length; i++){

  	if (aD[i].date == date){
		if (aD[i].hallID != curHallID){
			//alert('Jeg vender lige serveren... OK ?');
			curHallID = aD[i].hallID;
		}
	    //getObj('time').options[curIndex] = new Option(aD[i].time, i);
		getObj('time').options[curIndex] = new Option(aD[i].time, aD[i].perfID);
		curIndex++;
	}
	if (i == curDateTimeIdx)
	    sIdx = curIndex - 1;
  }
  //alert('curDateTime: ' + curDateTimeIdx + ' sIdx: ' + sIdx);
  getObj('time').selectedIndex = sIdx;
}


function buildAmount(e){
  //empty time-list
  for (var i = getObj('ticketamount').length - 1; i > -1; i--){
  	getObj('ticketamount').remove(i);
  }
  //build new time-list
  var curIndex = 0;
  for (var i = 1; i <= maxAmount; i++){
		getObj('ticketamount').options[curIndex] = new Option(i, i);
		curIndex++;
  }
  getObj('ticketamount').selectedIndex = (defaultAmount - 1);
}


function buildKind(e){
  //empty time-list
  for (var i = getObj('kind').length - 1; i > -1; i--){
  	getObj('kind').remove(i);
  }
  //build new time-list
  var curIndex = 0;
  for (var i = 0; i < aK.length; i++){
		getObj('kind').options[curIndex] = new Option(aK[i].name, i);
		curIndex++;
  }
  getObj('kind').selectedIndex = 0;
}


function buildAreas(e){
  //empty time-list
  for (var i = getObj('area').length - 1; i > -1; i--){
  	getObj('area').remove(i);
  }
  //build new time-list
  var curIndex = 0;
  for (var i = 0; i < aA.length; i++){
		getObj('area').options[curIndex] = new Option(aA[i].name, i);
		curIndex++;
  }
  getObj('area').selectedIndex = 0;
}


function AreaRec(area, name, color){
	this.area = area;
	this.name = name;
	this.color = color;
	return this;
}

function KindRec(kind, name){
	this.kind = kind;
	this.name = name;
	return this;
}

function PSRec(area, kind, price){
	this.area = area;
	this.kind = kind;
	this.price = price;
	return this;
}

//seat record
function sKR(name, color, colorImg, url){
	this.name = name;
	this.color = color;
	this.colorImg = colorImg
	this.url = url;
}

//seat record
function sR(name, x, y, row, col, kind, area){
	this.name = name;
	this.x = x;
	this.y = y;
	this.row = row;
	this.col = col;
	this.kind = kind;
	this.area = area;
}

function isInHallArea(evt){
	var tempX = getMouseX(evt);
	var tempY = getMouseY(evt);
	if (tempX >= hallX & tempX <= (hallX + hallWidth) & tempY >= hallY & tempY <= (hallY + hallHeight)){
		
		return true;
	}
	return false;
}

// --------------------------------- functions ------------------------------------


function numbersOnlyCalculation(event){
	if (isIE)
		Ch = event.keyCode;
	else if (isNS6){
		Ch = event.which;
		//alert(Ch);
		}
	else if (isNS4)
		Ch = event.which;
	else return false;
	if (Ch < 32 | Ch > 47 && Ch < 58){
		var a = getObj('ticketamount').value;
		if (Ch > 47 && Ch < 58){
			a += String.fromCharCode(Ch);
			alert(Ch);
		}
		doCalculation(a);
		return true;
	}
	return false;
}
 


function ArrayDeleteAt(Array, Position){
  if (Array.length > 0){
    for (var i = Position; i < Array.length - 1; i++){
      Array[i] = Array[i + 1];
    }
    Array.length = Array.length - 1;
  }
}

/*
function findPrice(area, kind){
	for (var i = 0; i < aPS.length; i++){
		if ((aPS[i].area == parseInt(area)) && (aPS[i].kind == parseInt(kind))){
			return aPS[i].price;
		}
	}
	return -1;
}
*/
function findPrice(area, kind){
//alert(area + ' - ' + kind);
	if (area > -1 && kind > -1)
	for (var i = 0; i < aPS.length; i++){
//	alert('area: ' + area + ' kind: ' + kind + '\n' +
//	      'aPS[i].area: ' + aPS[i].area + ' aPS[i].kind:' + aPS[i].kind);
		if ((aPS[i].area == parseInt(area)) && (aPS[i].kind == parseInt(kind))){
			return aPS[i].price;
		}
	}
	return -1;
}

function listPrices(){
	var S = new String();
	for (var i = 0; i < aPS.length; i++){
		S += aPS[i].area + ' - ' + aPS[i].kind + ' - ' + aPS[i].price + '\n';
	}
	return S;
}


function setPrice(kind, area){
	getObj('kind').value = kind;
	getObj('area').value = area;
	if (getObj('ticketamount').value == '')
	    getObj('ticketamount').value = '0';
	var a = getObj('ticketamount').value;
	var b = findPrice(area, kind);
	getObj('price').value = parseInt(a) * parseInt(b);
	getObj('ticketamount').focus();
}

function setArea(x, y, area){
	//getObj('area').value = area;
	choosenArea = area;
	//doCalculation();
	moveObjXY('choosenseat', x, y);
}
 
  
function FloatToMoney(amount){
	var PriceStr = '' + amount;
	var idx = PriceStr.indexOf('.');
	if (idx == -1)
		PriceStr = PriceStr + '.00';
	else if (idx == PriceStr.length - 2)
		PriceStr = PriceStr + '0';

	return PriceStr;
}

function doCalculation(){
	var myAmount = 0;
	var myPrice = 0;
	var myTotalAmount = 0;
	var myTotalPrice = 0;
	for (var i = 0; i < aK.length; i++){
		myAmount = parseInt(getObj('p_' + choosenArea + '_' + aK[i].priceID).value);
		//myPrice = findPrice(parseInt(getObj('area').value),  i) * myAmount;
		myPrice = findPrice(parseInt(choosenArea),  aK[i].priceID) * myAmount;
		if (myPrice < 0){
			alert('Der kan ikke købes \"' + aK[i].name + '\" - billetter i det valgte område !');
			myTotalAmount = 0;
			myTotalPrice = 0;
			break;
		}
		myTotalAmount = myTotalAmount + myAmount;
		myTotalPrice = myTotalPrice + myPrice;
	}
	var PriceStr = FloatToMoney(myTotalPrice);
	getObj('allprice').value = PriceStr;
	getObj('alltickets').value = myTotalAmount;
	//getObj('reservetickets').disabled = !myTotalAmount > 0;
	getObj('area').value = aA[getAreaIndex(parseInt(choosenArea))].name;
	//alert(parseInt(choosenArea));
}

