function eventEnableEndTime() {
   var checkbox        = document.getElementById('localevents_noendtime');
   var endtime_hour    = document.getElementById('localevents_endtime_hour');
   var endtime_minute  = document.getElementById('localevents_endtime_minute');   
   var endtime_ampm    = document.getElementById('localevents_endtime_ampm');
   if(!checkbox.checked)
   {
      endtime_hour.disabled = false;
      endtime_minute.disabled = false;
      endtime_ampm.disabled = false;
      return false;
   }
   else
   {
      endtime_hour.disabled = true;
      endtime_minute.disabled = true;
      endtime_ampm.disabled = true; 
      return true;
   }
} 

function eventEnableTime() {
   var checkbox        = document.getElementById('localevents_allday');   
   var endtime_checkbox= document.getElementById('localevents_noendtime');
   var starttime_hour  = document.getElementById('localevents_starttime_hour');
   var starttime_minute= document.getElementById('localevents_starttime_minute');   
   var starttime_ampm  = document.getElementById('localevents_starttime_ampm');
   var endtime_hour    = document.getElementById('localevents_endtime_hour');
   var endtime_minute  = document.getElementById('localevents_endtime_minute');   
   var endtime_ampm    = document.getElementById('localevents_endtime_ampm');
   if(!checkbox.checked)
   {
      if(endtime_checkbox.checked)
      {
         endtime_hour.disabled = true;
         endtime_minute.disabled = true;
         endtime_ampm.disabled = true;
         endtime_checkbox.disabled = false;
      }
      else
      {
         endtime_hour.disabled = false;
         endtime_minute.disabled = false;
         endtime_ampm.disabled = false;
         endtime_checkbox.disabled = false;
         endtime_checkbox.checked  = false; 
      }      
      starttime_hour.disabled = false;
      starttime_minute.disabled = false;
      starttime_ampm.disabled = false;
      return false;
   }
   else
   {      
      starttime_hour.disabled = true;
      starttime_minute.disabled = true;
      starttime_ampm.disabled = true;
      endtime_hour.disabled = true;
      endtime_minute.disabled = true;
      endtime_ampm.disabled = true; 
      endtime_checkbox.disabled = true;
      return true;
   }
   
 }


function eventFormStartup()
{
   eventEnableTime();
}


function eventLocationSwitch(link)
{   
   var location_search_text  = document.getElementById('location_search_text');
   var localevent_locationid = document.getElementById('localevents_locationid');
   var newlocation_container = document.getElementById('newlocation_container');   
   if(location_search_text.disabled==true)
   {
      link.innerHTML = 'Enter new location';
      localevent_locationid.disabled = false;
      location_search_text.disabled  = false;
      location_search_text.style.backgroundColor = '#ffffff';
      newlocation_container.style.display = 'none';
   }
   else   
   {
      link.innerHTML = 'Search existing location';  
      localevent_locationid.disabled = true;
      location_search_text.disabled = true;
      location_search_text.style.backgroundColor = '#aaaaaa';
      newlocation_container.style.display = 'block';      
      
   }  
}

function eventMusicianSwitch(link)
{   
   var musician_search_text   = document.getElementById('musician_search_text');
   var localevents_musicianid = document.getElementById('localevents_musicianid');
   var newmusician_container  = document.getElementById('newmusician_container');   
   if(musician_search_text.disabled==true)
   {
      link.innerHTML = 'Enter new musician';
      localevents_musicianid.disabled = false;
      musician_search_text.disabled  = false;
      musician_search_text.style.backgroundColor = '#ffffff';
      newmusician_container.style.display = 'none';
   }
   else   
   {
      link.innerHTML = 'Search existing musician';  
      localevents_musicianid.disabled = true;
      musician_search_text.disabled = true;
      musician_search_text.style.backgroundColor = '#aaaaaa';
      newmusician_container.style.display = 'block';     
      
   }  
}

function localevents_popup(pageURL,title,w,h) {
   var left = (screen.width/2)-(w/2);
   var top = (screen.height/2)-(h/2);
   var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
   return false;
} 

function chainedFetch(e)
{
	if (typeof(e) == "string") {
		//the id was supplied, get the object reference
		// e = new YAHOO.util.Element(e);
		e = document.getElementById(e);
		if (!e) {
			return true;
		}
	}
	
	var selectBox      = this;
	var subContainerID = this.id+'_sub';
	var subLoaderID    = this.id+'_loader';
	var subContainer   = document.getElementById(subContainerID);
	var subLoader      = document.getElementById(subLoaderID);
	subLoader.style.display  = "inline";
	var subElements    = subContainer.getElementsByTagName('select');
	var subSelectBox   = subElements[0];
	var parentID       = selectBox.options[selectBox.selectedIndex].value;
	var handleSuccess = function(o) 
	{		
		var result = YAHOO.lang.JSON.parse(o.responseText);
		
		subLoader.style.display  = "none";

		if(result.ReplyCode=='200' || result.ReplyCode=='201')
		{	
			localevents_cleanupselect(subSelectBox);
			if(result.ResultSet.totalResultsReturned > 0)
			{
				subContainer.style.display = "block";
				var results = result.ResultSet.Result;
				for(i=0; i<results.length; i++) 
				{ 		
					var option = document.createElement('option');
					option.setAttribute('value',results[i].itemid);
					option.appendChild(document.createTextNode(results[i].title));
					subSelectBox.appendChild(option);
				}
			}
			else
			{
				subContainer.style.display = "none";
			}
			return true;
		}
		else
		{
			if(result.hasOwnProperty('Error'))
			{
				alert(result.Error);
		    }
		    else if(result.ReplyText)
			{
				alert(result.ReplyText);
			}
			return true;
		}
	}
	var handleError = function(o)
	{	
		subLoader.style.display  = "none";	
		alert('Cannot connect to AJAX service.');
	}  
	       
	var callback =
	{
		success: handleSuccess,
		failure: handleError
	}

	YAHOO.util.Connect.asyncRequest('POST',LOCALEVENTS_BBURL+'/localeventsjs.php',callback,'ajax=1&do='+this.id+'&securitytoken='+SECURITYTOKEN+'&parentid='+parentID);
	
	return false;
}

function localevents_cleanupselect(obj)
{  
	var i;
	for (i = obj.length - 1; i>=0; i--) {
		if (obj.options[i]) {
			obj.remove(i);
		}
	}
}
