/**
 * This script is included on the detail page of the service.
 * This script displays the calculator on load and handels its event interactions.
 * This script uses the jquery library version 1.3.2
 * @author CTE tourism 2009
 */

var API_URL = 'http://cteapi.ro-studio.net/index.php';
var CODE    = 'CxqrLdly1WAdlsBMuHYwIerInMR5DeED3jjPks4ZBkfMS6iB8h8iyg2PnWr41sY+SQ3EKuZajp7Le6iWytjTMHZCaJIyeLpYgr8oJCSroVNhyNvA4hsT9oH7UR0phehV';

/**
 * cunctructor initializes the datepickers, subservices and events
 */
function cte_search(languagecode,subservices,events)
{
	
	var dts = [];
	
	for ( var i in events )
	{
	    datesplt 	= events[i].split('|');
		dts.push(datesplt[2]);
		dts.push(datesplt[3]);
	} 
	
	$("#datefrom").datepicker({
		 	beforeShowDay: function(thedate) {
		    
			var day 	= thedate.getDate();
			var month 	= thedate.getMonth() + 1;
			var year 	= thedate.getFullYear();
			
			curdate = year+'-'+month+'-'+day;
			//alert($.inArray(curdate, dts));
			
			if($.inArray(curdate, dts)==-1){
				return [true,""];
			}else{
				return [true,"specialDate",'Event'];
			}
			
			
		    //alert(curdate);
		    //var theday    = thedate.getDate();
			//alert(theday);
		    //if( $.inArray(theday,specialDays) == -1 ) return [true,""];
			//return [true, "specialDate"];
			
		  },
		  numberOfMonths: 2
	});
	$("#dateto").datepicker({numberOfMonths: 2});
	
	this.languagecode = languagecode;
	this.subservices = subservices;
	this.events = events;
	this.update();
	
}



cte_search.prototype.delDates = function(){
	
		$('form#searchform #datefrom').val('');
		$('form#searchform #dateto').val('');
		
}

cte_search.prototype.delSubservices = function(){
	
	for ( var i in this.subservices )
	{
	    $('#amount_'+this.subservices[i]).val('');
	} 
	
}

cte_search.prototype.updateDates = function(){
		
		dates = this.events[$('form#searchform #eventid').val()];
		datesplt = dates.split('|');
		
		fromsplt 	= datesplt[0].split("-");
		tosplt 		= datesplt[1].split("-");
		dfromsplt 		= datesplt[2].split("-");
		dtosplt 		= datesplt[3].split("-");
		
		$("#datefrom").datepicker( 'option' , 'minDate' , new Date(fromsplt[0],fromsplt[1]-1,fromsplt[2]) ); 
		$("#datefrom").datepicker( 'option' , 'maxDate' , new Date(dtosplt[0],dtosplt[1]-1,dtosplt[2]) );
		
		$("#dateto").datepicker( 'option' , 'minDate' , new Date(dfromsplt[0],dfromsplt[1]-1,dfromsplt[2]) ); 
		$("#dateto").datepicker( 'option' , 'maxDate' , new Date(tosplt[0],tosplt[1]-1,tosplt[2]) );

}


cte_search.prototype.update = function(){
	
	if(!$('form#searchform #eventid').val()){
		
		this.delDates();
		
		$('form#searchform #datefrom').attr("disabled", true); 
		$('form#searchform #dateto').attr("disabled", true);
		
	}else{
		
		this.delDates();
		this.updateDates();
		
		$('form#searchform #datefrom').attr("disabled", false);
		$('form#searchform #dateto').attr("disabled", false);
		
	}
}



cte_search.prototype.updateevents = function(){
	
	if(!$('form#searchform #eventid').val()){
		
		this.delDates();
		this.delSubservices();
		
		
		$('form#searchform #datefrom').attr("disabled", true); 
		$('form#searchform #dateto').attr("disabled", true);
		
		
	}else{

		this.delDates();
		this.delSubservices();
		this.updateDates();
		
		
		$('form#searchform #datefrom').attr("disabled", false);
		$('form#searchform #dateto').attr("disabled", false);

	}
	
}






  
