String.prototype.trim = function(){ return this.replace(/^\s+|\s+$/g,'') }
var sa=0;
var sn=0;
var tc=0;

function calcST(id, cant, cont){
	cant.value=toInt(cant.value);
	if (!cant.value.trim().length>0){
		cant.value=0;
	}
	new Ajax.Request(
		'/lib/getPrice.php?id=' + id + '&t=' + cont + '&cant=' + cant.value,
		{
			method:'get',
			onSuccess: function(transport){
				var response = transport.responseText || "0";
				$(cont).innerHTML = '$ ' +  response;
				(cont == 'sa') ? sa = response : sn = response; 
				var totMN = parseFloat(sa) + parseFloat(sn);
				$('totMN').innerHTML = '$ ' +   totMN.toFixed(2) ; 
				calcTotDll(totMN);
			},
			onFailure: function(){ 
				alert('Something went wrong...') 
			}
		}
	);	
}


function toInt(n){
	n = parseInt(n);
	if (isNaN(n)) {
            return 0;
	}
	else{
            return n;
	}
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function getTC(){
	new Ajax.Request(
		'/lib/getTC.php',
		{
			method:'get',
			onSuccess: function(transport){
				tc = parseFloat(transport.responseText) || "10";
			},
			onFailure: function(){ 
				alert('Something went wrong...');
				tc="1000";
			}
		}
	);	
}


function calcTotDll(totMN){
	$('totDLL').innerHTML = '$ ' +  (totMN / tc).toFixed(2); 
}

seatsSelected=new Array();
seTotMN=0;

function SelectSeat(obj, id, seatNumber, seatlbl){
	var idx=-1;
	for (i=0; i<seatsSelected.length; i++)
		if (seatsSelected[i].number==seatNumber) idx=i;
	obj.src=(idx>=0)? '/images/butacaG.jpg' : '/images/butacaY.jpg';
	idx=-1;
	new Ajax.Request(
		'/lib/getSeatPrice.php?id=' + id + '&seat=' + seatNumber,
		{
			method:'get',
			onSuccess: function(transport){
				var seatPrice =  parseFloat(transport.responseText) || "0";
				var idx=-1;
				var seats=new Array;
				for (i=0; i<seatsSelected.length; i++)
					if (seatsSelected[i].number==seatNumber) idx=i

				obj.src=(idx>=0)? '/images/butacaG.jpg' : '/images/butacaY.jpg';

				if (idx>=0){
					seTotMN=seTotMN-seatsSelected[idx].price;
					seatsSelected.splice(idx,1);
			
					$('data').deleteRow(idx+1);
					$('totMN').innerHTML='$ ' +   seTotMN.toFixed(2);
					
					if (seatsSelected.length==0){
						myTable = $('data');
						myRow=myTable.insertRow(1);
						myCell=myRow.insertCell(0);
						myCell.innerHTML = '--'; 
						myCell=myRow.insertCell(1);
						myCell.innerHTML = '<div class="pri">$ 0.00</div>';
						myCell=myRow.insertCell(2);
						myCell.innerHTML = '<div class="st pri">$ 0.00</div>';
					}
				}
				else{
					if (seatsSelected.length==0){
						$('data').deleteRow(1);
					}

					seats.number=seatNumber;
					seats.price=seatPrice;
					seats.lbl=seatlbl;

					seatsSelected.push(seats);

					seTotMN=seTotMN+seatPrice;

					myTable = $('data');
					myRow=myTable.insertRow(seatsSelected.length);

					myCell=myRow.insertCell(0);
					myCell.innerHTML = seatlbl; 

					myCell=myRow.insertCell(1);
					myCell.innerHTML = '<div class="pri">$ ' +   seatPrice.toFixed(2) + '</div>';

					myCell=myRow.insertCell(2);
					myCell.innerHTML = '<div class="st pri">$ ' +   seatPrice.toFixed(2) + '</div>';

					$('totMN').innerHTML='$ '+  seTotMN.toFixed(2);
				}

				var vseats=new Array();
				for (i=0; i<seatsSelected.length; i++)
					vseats.push(seatsSelected[i].number);
				$('vseats').value=vseats.join();		
				calcTotDll(seTotMN);
			},
			onFailure: function(){ 
				alert('Something went wrong...') 
	
			}
		}
	);
}

function checkSeats(){
	if (seatsSelected.length>0){
		return true;
	}
	else{
		alert('No ha seleccionado ninguna butaca');
		return false;
	}
}

function checkNumbers(){

 	if ((sa>0) || (sn>0)){
		return true;
	}
	else{
		alert('Especifique una cantidad');
		return false;
	}
}