function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

var cal1; 
var whenStr='today'; 
function showCalendar(){
	cal1 = new YAHOO.widget.Calendar("cal1","cal1Container"); 
	cal1.render(); 
}
var _rid;
function checkTableAvailabilityForToday(rid){
	_rid=rid;
	var now=new Date();
	if(now.getHours()>=21 && (now.getHours()>21 || now.getMinutes()>=30)){
		now=new Date(now.getTime()+(12*60*60*1000));
		whenStr='tomorrow';
	}
	else if(now.getHours>=17)
		whenStr='tonight';
	//alert('now:'+now);
	var when=(now.getMonth()+1)+'/'+now.getDate()+'/'+now.getFullYear()+' '+(now.getHours())+':'+now.getMinutes();
	var oturl=_baseUri+'/otCheck/tableAvailabilitySmartCheck.jsp?rid='+rid+'&when='+encodeURIComponent(when)+'&rand='+Math.floor((7+Math.random())*100);
	//alert('oturl:'+oturl);
	//$('reservationstatuscontainer').innerHTML='Querying OpenTable.  Please wait..<br/><img src="'+_baseUri+'images/ajax-loader.gif"/>';
	new Ajax.Request(oturl, 
	{
	  method: 'get',
	  onComplete:availCallback
	});
}

function checkTableAvailabilityStatusForToday(){
	var oturl=_baseUri+'/otCheck/tableAvailabilitySmartCheckStatus.jsp?rid='+_rid+'&rand='+Math.floor((7+Math.random())*100);
	new Ajax.Request(oturl, 
	{
	  method: 'get',
	  onComplete:statusCallback
	});
	oturl=_baseUri+'/otCheck/tableAvailabilitySmartCheck.jsp?rid='+_rid+'&rand='+Math.floor((7+Math.random())*100);
	new Ajax.Request(oturl, 
	{
	  method: 'get', 
	  onComplete:availCallback
	});
}
var done=false;
var statusCallback=function(o){
	//alert('rsp:'+o.responseText);	
	if(o.responseText.indexOf('ok')!=-1){
		done=true;
		if(o.responseText.indexOf('notables')==-1)
			$('reservationstatus').innerHTML='Reserve table for 2 '+whenStr;
		else
			$('reservationstatus').innerHTML='Reserve table online';
		$('reservationstatus').style.backgroundImage='none';
		//if(o.responseText.indexOf('notables')!=-1)
		//	$('reservationinfo').innerHTML='Sorry, no open tables '+whenStr;			
	}else if(o.responseText.indexOf('err')!=-1){
		done=true;
		//$('reservationinfo').innerHTML='Sorry, we ran into problems while retrieving open tables.';	
	}
}

var availCallback=function(o){
	//alert('rsp:'+o.responseText);	
	if(o.responseText.trim()!='')
		$('reservationinfo').innerHTML=o.responseText;
	if(!done)
		setTimeout("checkTableAvailabilityStatusForToday()",2000);
}


/**regular search**/
var _rid1;
var otRandom;
var done1=false;
function checkTableAvailability(rid){
	//alert('here1');
	otRandom=Math.floor((2+Math.random())*100);
	done1=false;
	$('resstatus').innerHTML='Checking with our friends at<br/><img src="'+_baseUri+'/images/opentable.jpg"/>';
	$('resinfo').innerHTML='';
	//$('resstatus').style.backgroundImage=_baseUri+'/images/loading_bw.gif';
	//$('resstatus').style.backgroundImage='url("'+_baseUri+'/images/loading_bw.gif"); background-position: bottom right';
	//alert('here2');
	_rid1=rid;
	var reservationDate=$('reservationDate').innerHTML;
	var now=new Date();
	var when=getCheckedValue(document.getElementsByName('when'));
	//alert('when:'+when);
	if(when!='date')
		reservationDate=(now.getMonth()+1)+'/'+now.getDate()+'/'+now.getFullYear()+' '+now.getHours()+':'+now.getMinutes();
	else
		reservationDate=reservationDate + ' ' + now.getHours()+':'+now.getMinutes() 
	var url=_baseUri+'/otCheck/tableAvailability.jsp?reqId='+otRandom+'&rid='+rid+'&when='+when+'&partySize='+$('partySize').value+'&reservationDate='+encodeURIComponent(reservationDate)+'&ld='+$('ld').value+'&rand='+Math.floor((7+Math.random())*100);
	new Ajax.Request(url, 
	{
	  method: 'get', 
	  onComplete:availCallback1
	});
}

function checkTableAvailabilityStatus(){
	var oturl=_baseUri+'/otCheck/tableAvailabilityStatus.jsp?reqId='+otRandom+'&rid='+_rid1+'&rand='+Math.floor((7+Math.random())*100);;
	//alert('checkTableAvailabilityStatusForToday '+otRandom);
	new Ajax.Request(oturl, 
	{
	  method: 'get', 
	  onComplete:statusCallback1
	});
	oturl=_baseUri+'/otCheck/tableAvailability.jsp?reqId='+otRandom+'&rid='+_rid1+'&rand='+Math.floor((7+Math.random())*100);;
	//alert('checkTableAvailabilityStatusForToday '+otRandom);
	new Ajax.Request(oturl, 
	{
	  method: 'get', 
	  onComplete:availCallback1
	});
}

function statusCallback1(o){	
	if(o.responseText.indexOf('ok')!=-1){
		done1=true;
		if(o.responseText.indexOf('notables')==-1)
			$('resstatus').innerHTML='Showing availability<br/><img src="'+_baseUri+'/images/opentable.jpg"/>';
		else
			$('resstatus').innerHTML='<br/><img src="'+_baseUri+'/images/opentable.jpg"/>';
		
		$('resstatus').style.backgroundImage='none';
		if(o.responseText.indexOf('notables')!=-1)
			$('resinfo').innerHTML='<br/>Sorry, we were unable to find any open tables.  Please try another search.';			
	}else if(o.responseText.indexOf('err')!=-1){
		done1=true;
		$('resinfo').innerHTML='<br/>Sorry, there was a problem finding open tables for this restaurant.';	
	}
}

function availCallback1(o){	
	if(o.responseText.trim()!='')
		$('resinfo').innerHTML=o.responseText;
	if(!done1)
		setTimeout("checkTableAvailabilityStatus()",2000);
}