	var defaultInDivTime = "All day";
	var defaultInDivRate = "Rate not available";
	var defaultInDivValAll = "Must be select an event!";
	
	var defaultInValMSG01 = "Exceeds maximum of tickets allowed for the event";
	var defaultInValMSG02 = "Exceeds maximun of tickets allowed for that hour";
	var defaultInValMSG03 = "Exceeds maximum of available tickets allowed";
	var defaultInValMSG04 = "Must be selected at least a ticket";
	
	var maxTicketsConfiguration = 0;

	function hAvail(da,hv){
		this.day = da;
		this.hourAvail = hv;	
	}
	
	function price(ip,ir,pr,pe,np,mit,mat,tx){
			this.idPrice = ip;
			this.idRate = ir;
			this.price = pr;
			this.priceSpecial = pe;						
			this.namePrice = np;
			this.maxTPrice = mat;
			this.minTPrice = mit;
			this.tax = tx;
	}

	function rate(ir,ie,db,de,tm,ds,ps,th,ta,hv, fh){
		this.idRate = ir;
		this.idEvent = ie;
		this.dBegin = db;
		this.dEnd = de;
		this.time = tm;
		this.DaysSpecial = ds;
		this.prices = ps;
		this.availByEvent = ta;	
		this.ticketsByHour = th;		
		/* Arreglo de dias, hora, disponibilidadporhora */
		this.availByHour = hv;
		this.FreeHour = fh;  //para la hora libre..
	}
	
	function initialize(txtPriceCache,objTimesToRender,objPricesToRender,txtRateCache,txtAvailCache,txtFreeHourCache){
		document.getElementById(objTimesToRender).innerHTML =  "<B>" + defaultInDivTime + "</B>";
		document.getElementById(objPricesToRender).innerHTML = "<P class='Validators'>" + defaultInDivRate + "</P>";		
		
		var cRate = document.getElementById(txtRateCache);
		cRate.value = "";
		var txtPrice = document.getElementById(txtPriceCache);
		txtPrice.value = "";
		var txtAvail = document.getElementById(txtAvailCache);
		txtAvail.value = "";	
		
		var txtHours = document.getElementById(txtFreeHourCache);
		txtHours.value = "";	
		
			
	}
	
	function setRates(ddlDay,txtPriceCache,aRates,objTimesToRender,objPricesToRender,txtRateCache,txtAvailCache,txtFreeHourCache){
		initialize(txtPriceCache,objTimesToRender,objPricesToRender,txtRateCache,txtAvailCache,txtFreeHourCache);
		var aTimes = new Array();		
		aTimes = getTimeRates(ddlDay,aRates)
		if (aRates.length > 0 && aTimes.length > 0){
			var ddlTimeName = 'ddlTimes_' + aRates[0].idEvent;
			renderTimes(ddlTimeName,txtPriceCache,aTimes,objTimesToRender,ddlDay,aRates,objPricesToRender,txtRateCache,txtAvailCache);
			var ddlTime = document.getElementById(ddlTimeName);			
			renderPrices(ddlDay,txtPriceCache,ddlTime.options[ddlTime.selectedIndex].value,aRates,objPricesToRender,objTimesToRender,txtRateCache,txtAvailCache,txtFreeHourCache);								
		}
		else{			
				
			renderPrices(ddlDay,txtPriceCache,'*****',aRates,objPricesToRender,objTimesToRender,txtRateCache,txtAvailCache,txtFreeHourCache);			
		}
	}
	
	function getTimeRates(ddlDay,aRates){
		var sDay = new Date(document.getElementById(ddlDay).value);
		var aTimes = new Array();
		for(var i=0;i<aRates.length;i++){
			if(sDay>=aRates[i].dBegin && sDay<=aRates[i].dEnd){
				var rt = aRates[i];
				if(rt.time != '*****'){
					if (rt.ticketsByHour > 0){
					 /*Consideramos si existe el dia actual en la lista de disponibilidad */
						for(j=0; j < rt.availByHour.length;j++)
							if(rt.availByHour[j].day.toString() == sDay.toString()){
								aTimes.push(rt.time);		
								break;
							}										
					}else
						/*En este caso no consideramos la disponibilidad por hora*/								
						aTimes.push(rt.time);						
				}				
			}
		}
		return aTimes;
	}

	function renderTimes(ddlName,txtPriceCache,aTimes,objTimesToRender,ddlDay,aRates,objPricesToRender,txtRateCache,txtAvailCache){
		document.getElementById(objTimesToRender).innerHTML =  "<B>" + defaultInDivTime + "</B>";
		var cmbTimes = "<SELECT id='" + ddlName + "' onChange=" + "renderPrices('" + ddlDay + "','" + txtPriceCache + "',this.value,aRates_" + aRates[0].idEvent + ",'" + objPricesToRender + "','" +  objTimesToRender + "','" + txtRateCache + "','" + txtAvailCache + "')" + " onmousewheel='return false;'>";
		for(var i=0;i<aTimes.length;i++){
			cmbTimes = cmbTimes + "<OPTION value='" + aTimes[i] + "'>" + aTimes[i] + "</OPTION>";
		}
		cmbTimes = cmbTimes + "</SELECT>";
		document.getElementById(objTimesToRender).innerHTML = cmbTimes;
		/* ocultamos los validadores por el caso de que esten visibles */
		hide_validators(aRfvSelPrices);
	}
			
			
	function setFreeHour(txtFreeHourCache, value){		
		var cFreeHour = document.getElementById(txtFreeHourCache);
		txtFreeHourCache.value = value;	
	}
	
	function renderPrices(ddlDay,txtPriceCache,sTime,aRates,objPricesToRender,objTimesToRender,txtRateCache,txtAvailCache,txtFreeHourCache){
		document.getElementById(objPricesToRender).innerHTML = "<P class='Validators'>" + defaultInDivRate + "</P>";
		
		var cRate = document.getElementById(txtRateCache);
		cRate.value = "";
		var txtPrice = document.getElementById(txtPriceCache);
		txtPrice.value = "";
		var txtAvail =  document.getElementById(txtAvailCache);
		txtAvail.value = "0,0"; //Event Avail, TicketsByHour
		
		
		var ddlDaySel = document.getElementById(ddlDay);		
		var sDay = new Date(ddlDaySel.value);
		
		var tblPrices = "<TABLE cellSpacing='0' cellPadding='1' width='100%' border='0'>";
		for(var i=0;i<aRates.length;i++){			
			if(sDay>=aRates[i].dBegin && sDay<=aRates[i].dEnd && sTime==aRates[i].time){
				/*Verificamos si existen freehours y si el time es ***** */				
				if (aRates[i].time == '*****'){ //(aRates[i].FreeHour.length > 0 && aRates[i].time == '*****'){
					/*Obtener el cache*/	
					
					var cFreeHour = document.getElementById(txtFreeHourCache);
					cFreeHour.value = "";
					var cmbID = 'FreeH_' + aRates[i].idRate;
					var cmbFreeHour = "<SELECT id='" + cmbID + "' onChange='setFreeHour(" + cFreeHour.id + ",this.value);'>";
					
					var s=0;
					for (s=0; s<aRates[i].FreeHour.length; s++)
					{
						cmbFreeHour += "<OPTION VALUE='" + aRates[i].FreeHour[s] + "'>" + aRates[i].FreeHour[s] + "</OPTION>";
					}

					cmbFreeHour += "</SELECT>";  
					document.getElementById(objTimesToRender).innerHTML = cmbFreeHour;
					
					var c = document.getElementById(cmbID);
					if (c){
						if (c.options.length>0){
							cFreeHour.value = c.options[c.selectedIndex].value;
						}					
					}
				}
				
							
				var aPrices = aRates[i].prices;
				cRate.value = aRates[i].idRate;
				/*AKY CAMBIAMOS PARA QUE SE OBTENGA LA DISPONIBILIDAD SEGUN EL DIA SELECIONADO ticketsByHour DEL ARREGLO DE DIAS EN LAS TARIFAS */
				var ticketsxhour = aRates[i].ticketsByHour;
				for(var j=0; j < aRates[i].availByHour.length;j++)
					if(aRates[i].availByHour[j].day.toString() == sDay.toString()){
						ticketsxhour = aRates[i].availByHour[j].hourAvail;
						break;
					}						
				txtAvail.value = aRates[i].availByEvent[ddlDaySel.selectedIndex] + ',' +  ticketsxhour;
						
				for(var j=0;j<aPrices.length;j++){
					tblPrices = tblPrices + "<TR><TD>";
					tblPrices = tblPrices + aPrices[j].namePrice;				
					tblPrices = tblPrices + "</TD><TD align=right>";
					if (aRates[i].DaysSpecial.charAt(sDay.getDay()) == '1')
					{
						var p = parseFloat(aPrices[j].priceSpecial);
						var porc = parseFloat(eval(aPrices[j].tax / 100));
						var total = eval((p * porc) + p);
						tblPrices = tblPrices + formatCurrency(total);							
					}
					else{
						var p = parseFloat(aPrices[j].price);
						var porc = parseFloat(eval(aPrices[j].tax / 100));
						var total = eval((p * porc) + p);
						tblPrices = tblPrices + formatCurrency(total);	
					}
					
					tblPrices = tblPrices + "</TD><TD align=right>";					
					
					tblPrices = tblPrices + "<SELECT name='Quantity_" + aPrices[j].idPrice + "' onchange=addPriceCache('" + txtPriceCache + "'," + aPrices[j].idPrice + ",this.value,true) onclick=addPriceCache('" + txtPriceCache + "'," + aPrices[j].idPrice + ",this.value,true) onmousewheel='return false;' style='width:38px;'>";
					for(var k = aPrices[j].minTPrice; k <= aPrices[j].maxTPrice; k++)
						tblPrices = tblPrices + "<OPTION value=" + k + ">" + k + "</OPTION>";
					tblPrices = tblPrices + "</SELECT>";
					tblPrices = tblPrices + "</TD></TR>";
				}				
			}			
		}			
		tblPrices = tblPrices + "</TABLE>";															
		document.getElementById(objPricesToRender).innerHTML=tblPrices;				
		setAllPriceCache(aTxtSelPrices);

		/* ocultamos los validadores por el caso de que esten visibles */
		hide_validators(aRfvSelPrices);
	}
	
	function formatCurrency(num) {
		num = num.toString().replace(/\$|\,/g,'');
		if(isNaN(num))
		num = "0";
		sign = (num == (num = Math.abs(num)));
		num = Math.floor(num*100+0.50000000001);
		cents = num%100;
		num = Math.floor(num/100).toString();
		if(cents<10)
		cents = "0" + cents;
		for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
		num = num.substring(0,num.length-(4*i+3))+','+
		num.substring(num.length-(4*i+3));
		return (((sign)?'':'-') + '$' + num + '.' + cents);
	}
	
	
	function setAllPriceCache(aText){
		for(i=1;i<aText.length;i++){
			var txtPrice = document.getElementById(aText[i]);
			var parentElem = ((txtPrice.parentElement)?txtPrice.parentElement:txtPrice.parentNode);					
			var cmbPrices = parentElem.getElementsByTagName("select");
						
			if(cmbPrices&&cmbPrices.length>0){
				for(var k=0;k<cmbPrices.length;k++){
					if(cmbPrices[k].name.indexOf('Quantity_') != -1 && cmbPrices[k].options.length>0 && cmbPrices[k].options[cmbPrices[k].selectedIndex].value>0){
						/*Ponemos en el price cache los datos de la cantidad y el id precio*/
						//alert()
						addPriceCache(txtPrice.id,cmbPrices[k].name.split('_')[1],cmbPrices[k].options[cmbPrices[k].selectedIndex].value, false);						
					}
				}
			}
		}
	} 
	
	function addPriceCache(txtCache, idPrice, Quantity, preSelect){
		
		var status = 'Add';
		var txtPrice = document.getElementById(txtCache);
		
		//alert(txtPrice.getAttribute('CRadio'));
		if (preSelect){
			var radios = document.getElementsByTagName("INPUT");
			if (radios.length > 0)
			{
				var i=0;
				for (i =0; i <= radios.length-1; i++)
				{
					if (radios[i].type == 'radio')
					{
						if (radios[i].id.indexOf("UcComplete") >= 0)
						{
							if (radios[i].id != txtPrice.getAttribute('CRadio'))
							{
								radios[i].checked = false;
							}
							else
							{
								radios[i].checked = true;
							}
						}
					}
				}
			}			
		}
		
		
		
		var sPrices = new String(txtPrice.value);
		var aSelPrices = new Array();
		if(sPrices!=""){		
			aSelPrices = sPrices.split(",");
			for(var i=0;i<aSelPrices.length;i++){
				var aSelPrice = new Array();
				aSelPrice = aSelPrices[i].toString().split("/");
				if(aSelPrice[0] == idPrice){
					if(Quantity==0){
						aSelPrices.splice(i,1);
						status = 'Remove';
					}else{
						aSelPrice[1] = Quantity;
						aSelPrices[i] = aSelPrice.join("/");
						status = 'Modify';
						continue;					
					}
				}			
			}			
			validate_selectionFields_in(aTxtAvailables,aTxtSelPrices,aRfvSelPrices);
		}
		
		if(status == 'Add' && Quantity > 0){	
			aSelPrices.push(idPrice + "/" + Quantity);
		}		
		txtPrice.value = aSelPrices.join(",");		
		
		/* Auto-seleccion del evento */
		if(preSelect)
			for(var j=1;j<aTxtSelPrices.length;j++){
				if(txtPrice.id == aTxtSelPrices[j]){
					var chkRequired = document.getElementById(aChkRequired[j]);				
					if(!chkRequired.disabled)
						if(txtPrice.value != "")
							chkRequired.checked = true;
						else
							chkRequired.checked = false;
					break;
				}					
			}		
	}
	
	function getPriceCache(aText){
		for(i=1;i<aText.length;i++){
			var txtPrice = document.getElementById(aText[i]);
			var sPrices = new String(txtPrice.value);			
			var parentElem = ((txtPrice.parentElement)?txtPrice.parentElement:txtPrice.parentNode);					
			var cmbPrices = parentElem.getElementsByTagName("select");
						
			if(cmbPrices&&cmbPrices.length>0){
				if(sPrices!=""){		
					var aSelPrices = new Array();
					aSelPrices = sPrices.split(",");
					for(var j=0;j<aSelPrices.length;j++){
						var aSelPrice = new Array();
						aSelPrice = aSelPrices[j].toString().split("/");
						for(var k=0;k<cmbPrices.length;k++)
							if(cmbPrices[k].name == 'Quantity_' + aSelPrice[0]){
								/* Seleccionamos el valor actual */
								for(var l=0;l<cmbPrices[k].length;l++){
									if(cmbPrices[k].options[l].value == aSelPrice[1])
										cmbPrices[k].selectedIndex = l;
								}
							}																					
					}
					
				}else{
					for(var k=0;k<cmbPrices.length;k++)
						cmbPrices[k].selectedIndex=0;
				}	
			}
			else{
				txtPrice.value="";
			}			
		}
	}

/* No valida evento requerido */
	function validate_selectionFields_in(aObjAvail,aObjPrices,aObjVal){	
		var lblMessage;
		var IsValued = false;
		var exceed = false;
		/* recorremos todos los cache de precio */
		for(var i=1; i < aObjPrices.length; i++){				
			var txtValidate = document.getElementById(aObjPrices[i]);
			var txtAvail = document.getElementById(aObjAvail[i]);
			lblMessage = document.getElementById(aObjVal[i]);
			lblMessage.style.display='none';			
			if(txtValidate.value != ""){
				IsValued = true;
				var result = IsExceedMaxTickets(txtAvail.value,txtValidate.value);
				if(result == 1){							
					lblMessage.innerText = defaultInValMSG01;
					lblMessage.style.display='block';					
					exceed = true;					
				}
				if(result == 2){							
					lblMessage.innerText = defaultInValMSG02;
					lblMessage.style.display='block';					
					exceed = true;					
				}
				if(result == 3){							
					lblMessage.innerText = defaultInValMSG03;
					lblMessage.style.display='block';					
					exceed = true;					
				}
			}
		}		
		if(!exceed && !IsValued){
			alert(defaultInDivValAll);
			return false;
		}		
		if(exceed)
			return false;			
		if(IsValued)
			return true;				
	}
	
	/* Validando evento requerido */
	function validate_selectionFields(aObjAvail,aObjPrices,aObjVal,aObjSelect){	
		var lblMessage;
		var hasSelected = false;
		var hasTicketsSelected = true;
		var IsValued = false;
		var exceed = false;
		/* recorremos todos los cache de precio */
		for(var i=1; i < aObjSelect.length; i++){
			var chkSelected = document.getElementById(aObjSelect[i]);			
			if(chkSelected.checked == true){				
				var txtValidate = document.getElementById(aObjPrices[i]);
				var txtAvail = document.getElementById(aObjAvail[i]);
				
				hasSelected = true; /* Establecemos que se ha seleccionado al menos un evento */
				
				lblMessage = document.getElementById(aObjVal[i]);
				lblMessage.style.display='none';			
				if(txtValidate.value != ""){
					IsValued = true;
					var result = IsExceedMaxTickets(txtAvail.value,txtValidate.value);
					if(result == 1){							
						lblMessage.innerText = defaultInValMSG01;
						lblMessage.style.display='block';					
						exceed = true;					
					}
					if(result == 2){							
						lblMessage.innerText = defaultInValMSG02;
						lblMessage.style.display='block';					
						exceed = true;					
					}
					if(result == 3){							
						lblMessage.innerText = defaultInValMSG03;
						lblMessage.style.display='block';					
						exceed = true;											
					}
				}else{
						lblMessage.innerText = defaultInValMSG04;
						lblMessage.style.display='block';					
						hasTicketsSelected = false;												
					}
			}/*End if chkSelected.checked*/
		}	/*End for*/
		if(!hasSelected){
			alert(defaultInDivValAll);
			return false;
		}	
		if(!hasTicketsSelected){			
			return false;
		}					
		if(!exceed && !IsValued){
			alert(defaultInDivValAll);
			return false;
		}		
		if(exceed)
			return false;			
		if(IsValued)
			return true;				
	}

	
	function IsExceedMaxTickets(sAvail,sPrices){		
		var aSelPrices = new Array();
		var aSelAvail = new Array();
		var tickets = 0;		
		var result = 0;
		if(sPrices!=""){		
			aSelPrices = sPrices.split(",");
			aSelAvail = sAvail.split(",");
			for(var i=0;i<aSelPrices.length;i++){
				var aSelPrice = new Array();
				aSelPrice = aSelPrices[i].toString().split("/");
				tickets += parseInt(aSelPrice[1]);				
			}
			if(tickets > maxTicketsConfiguration)
				result = 1; /* Config */
			else
				if(aSelAvail[1]>0 && aSelAvail[1] < tickets)
					result = 2; /* Por hora */		
				else
					if(aSelAvail[0] < tickets)
						result = 3; /* No hay dispo */
		}
		return result;
	}
	
	function hide_validators(aObjVal){
		for(var i=1;i<aObjVal.length;i++){
			lblMessage = document.getElementById(aObjVal[i]);
			lblMessage.style.display='none';
		}
	}


