function toggleVisibility(id)
{
	var o = document.getElementById(id);
	if(o.style.visibility=='hidden')
		o.style.visibility='visible';
	else
		o.style.visibility='hidden';
}

/*
 *
 */
 function Person()
{
	this.name = 'name';
}

function Administrator()
{
	this.inheritFrom = Person
	this.inheritFrom()
}
/*
 * Class Slot
 * @param date, am or pm
 */
function Slot(the_date,am_pm)
{
	this.the_date = the_date;
	this.am_pm = am_pm;
	this.dateObj = Date.parseString(this.the_date,'y-MM-dd');

	this.owned_by_me = false;
	this.owned_by_other = false;
	this.locked = false;	
	this.spontaneous = false
	this.base = false;
	this.toggle = false;
	
	
	function getJavaScriptId()
	{
		return "id" + this.dateObj.format('yMMdd') + this.am_pm;
	}
	function nextSlot()
	{
		var n = new Slot('2000-01-01','am');
		return n;
	}
	
	function setToggle()
	{
		this.toggle = (!this.toggle);
		this.updateDisplay()
	}
	
	function setBase()
	{
		this.user_id = 0;
		this.base = true;
		this.updateDisplay()
	}
	
	function setLock()
	{
		this.locked = true;
		this.updateDisplay();
	}

	function incrementSlot()
	{
		if(this.am_pm == "am")
		{
			this.am_pm = "pm";
		}
		else
		{
			this.am_pm = "am";
			this.dateObj.add('d',1);
			this.the_date = this.dateObj.format("y-MM-dd");
		}
		var n = new Slot(this.the_date,this.am_pm);
		return n;
	}

	function setUserId(id)
	{
		this.user_id = id;
		if(this.user_id==auth_user_id)
		{
			this.owned_by_me = true
		} else if (this.user_id>0){
			this.owned_by_other = true
		}
		this.updateDisplay();
	}
	
	function updateDisplay()
	{
		id = this.getJavaScriptId();
		if(document.getElementById(id))
		{
			document.getElementById(id).className = this.makeClassName();
			document.getElementById(id).slotObj = this;
			document.getElementById(id).style.borderColor = ((this.toggle==true)?'#000':'#FFF');
			document.getElementById(id).onclick = handleClick;
		}
		return true;		
	}

	function equals(inSlot)
	{
		return (
			(inSlot.the_date == this.the_date)
			&&
			(inSlot.am_pm == this.am_pm)
			);
	}
	function txtDisplay()
	{
		document.write( this.the_date );
		document.write( " -- " );
		document.write( this.am_pm );
		document.write( " -- " );
		document.write( this.getJavaScriptId() );
		document.write( " -- " );
		document.write( this.dateObj );		
		document.write( "<br>" );
	}
	function infoDisplay()
	{
		out = ""
		out = this.the_date + "  / "
		out += this.am_pm + "\n";
		if (this.isOwnedByMe())
			out += "you own this slot" + "\n"
		if (this.isLocked())
			out	+= "this slot is locked" + "\n"		
		if (this.isOwnedByOther())
			out	+= "this slot is owned by user " + this.user_id + "\n"		
		if (this.isBase())
			out	+= "this slot is a base day. it may not be booked or traded." + "\n"
		//out += "current class: " + this.makeClassName() + "\n"
		return out;

	}
	function alertInfoDisplay()
	{
		alert(this.infoDisplay());
	}


	
	function isDay()
	{
		if(this.am_pm=="day")
			return true;
		else
			return false;

	}
	function isOwnedByMe()
	{
		return this.owned_by_me;
	}
	function isOwnedByOther()
	{
		return this.owned_by_other;
	}
	function isLocked()
	{
		return this.locked;	
	}
	function isSpontaneous()
	{
		return this.spontaneous;
	}
	function isBase()
	{
		return this.base;	
	}
	
	function makeClassName()
	{
		/*
		slot day
		slot daymine
		slot dayother
		slot dayotherlock
		slot daysystemlock
		slot dayspontaneous
		slot daysystembase
		slot eve
		slot evemine
		slot eveother
		slot eveotherlock
		slot evesystemlock
		slot evespontaneous
		slot evesystembase
		*/

		out = '';
		if(this.am_pm == "am")
			out += 'day';
		else
			out += 'eve';

		if(this.isOwnedByMe())
			out += 'mine';
		else if(this.isOwnedByOther())
			out += 'other';
		else
			out += 'system';
		
		if(this.isLocked())
			out += 'lock';

		if(this.isSpontaneous())
			out += 'spontaneous';
			
		if(this.isBase())
			out += 'base';
		
		return out;

	}
	
	function isClickable()
	{
		if(this.isBase() || this.isLocked())
			return false;
		else
			return true;
	}
	

	
	this.getJavaScriptId = getJavaScriptId;
	this.nextSlot = nextSlot;
	this.incrementSlot = incrementSlot;
	this.equals = equals;
	this.txtDisplay = txtDisplay;
	this.setLock = setLock;

	this.isClickable = isClickable;
	this.isDay = isDay;
	this.isOwnedByMe = isOwnedByMe;
	this.isOwnedByOther = isOwnedByOther;
	this.isLocked = isLocked;
	this.isSpontaneous = isSpontaneous;
	this.isBase = isBase;
	this.makeClassName = makeClassName;
	this.infoDisplay = infoDisplay;
	this.alertInfoDisplay = alertInfoDisplay;
	this.setBase = setBase;
	
	this.updateDisplay = updateDisplay;
	this.setUserId = setUserId;
	this.setToggle = setToggle;





}






/*
 * Calendar Class
 */
function Calendar(startSlot,endSlot)
{
	this.startSlot = startSlot;
	this.endSlot = endSlot;
	this.slots = new Array();
	
	/* these get initialized in the constructor */
	this.dayCount = 0;
	this.weekCount = 0;

		this.startSlot = new Slot(this.startSlot.the_date,this.startSlot.am_pm);
		this.endSlot   = new Slot(this.endSlot.the_date  ,this.endSlot.am_pm);

		i = 0;
		var theNext = new Slot(startSlot.the_date,startSlot.am_pm);
		this.slots[i] = theNext;
		while (!theNext.equals(this.endSlot))
		{
			this.slots[i] = theNext.incrementSlot();
			this.slots[i].updateDisplay();
			i++;
		} 

	/*
	 * @description Constructor
	 * @param Slot slot1
	 * @param Slot slot2
	 * @returns Calendar object
	 */

	function numberOfSlots()
	{
		return this.slots.length;
	}
	
	function numberOfDays()
	{
		return ((numberOfDays / 2)+0.5).parseInt();
	}
	
	function numberOfWeeks()
	{
		return (numberOfSlots() / 7);
	}
	
	function getSlotById(id)
	{
		for(i=0;i<this.slots.length;i++)
		{
			if(this.slots[i].getJavaScriptId()== id)
				return this.slots[i];
		}
//		alert('This slot was not found on this calendar')
		return false;
	}
	
	function displayCalendar()
	{
		document.write("Calendar");
		document.writeln("");
		document.write("Start Slot: ");
		this.startSlot.txtDisplay();
		document.write("End Slot:   ");
		this.endSlot.txtDisplay();
		document.writeln("");
		for(i=0;i<this.slots.length;i++)
		{
			document.write(i) + " :: ";
			this.slots[i].txtDisplay();
			document.write(this.slots[i].makeClassName());
		}

		document.writeln("There are " + this.numberOfSlots() + " slots");

	}

	/*
	 * Calendar functions
	 */
	this.numberOfSlots = numberOfSlots
	this.numberOfDays = numberOfDays
	this.numnerOfWeeks = numberOfWeeks
	this.getSlotById = getSlotById
	this.displayCalendar = displayCalendar
}


function handleClick()
{
//	alert(this.id);
	if(document.getElementById(this.id))
	{
		slot_instance = document.getElementById(this.id);

		slot_instance.slotObj.setToggle();

		if(!slot_instance.slotObj.isClickable())
		{
			//alert('You do not have access to this slot')
			slot_instance.slotObj.setToggle();
		}
		else if(slot_instance.slotObj.owned_by_other == true)
		{
			if(slot_instance.slotObj.toggle==true)
				document.forms['actionFormTrade'].dates.value+=slot_instance.slotObj.the_date+" "+slot_instance.slotObj.am_pm+"\n"
				
		}
		else
		{
			if(slot_instance.slotObj.toggle==true)
				document.forms['actionFormBook'].dates.value+=slot_instance.slotObj.the_date+" "+slot_instance.slotObj.am_pm+"\n";
		}
		alert(slot_instance.slotObj.infoDisplay())

		

			
	}

/*
	if(!this.slotObj.isClickable())
		alert('You do not have access to this slot')
	else
		alert(this.slotObj.infoDisplay())

	document.forms['actionFormBook'].dates.value+=this.slotObj.the_date+" "+this.slotObj.am_pm+"\n"
*/	

//	alert(this.slotObj.the_date)
//	alert(this.slotObj.infoDisplay())
	
}



/*
3/1 am - 1
3/1 pm - 2
3/2 am - 3
3/2 pm - 4
3/3 am - 5 
3/3 pm - 6
3/4 am - 7
3/4 pm - 8
*/ 


function loadLock(js_id,status_type_id,user_id,user_id,boat_id,lock_am_or_pm)
{
	if (document.getElementById(js_id))
	{
		if(lock_am_or_pm = 'am')
			document.getElementById(js_id).className='dayOtherLock';
		else
			document.getElementById(js_id).className='eveOtherLock';
	}	
}

function loadDate(js_id,status_type_id,user_id,boat_id,slot_am_or_pm)
{
	if (document.getElementById(js_id))
	{

		obj = document.getElementById(js_id);

		if(slot_am_or_pm=='am')
		{
			if(status_type_id == '2')
				obj.className = 'dayBase'
			else if(status_type_id == '1')
			{
				if(user_id == auth_user_id)
					obj.className = 'dayMine'
				else
					obj.className = 'dayOther'
			}
			else
				obj.className = 'day'
		
		} else if (slot_am_or_pm=='pm')
		{
			if(status_type_id == '2')
				obj.className = 'eveBase'
			else if(status_type_id == '1')
			{
				if(user_id == auth_user_id)
					obj.className = 'eveMine'
				else
					obj.className = 'eveOther'
			}
			else
				obj.className = 'eve'
		}

	}	
}


function validateAction()
{
	return true;
}