

function slideDataContainer(dontFocusAfterLoading)
{
	if($('dataContainer').style.display == 'none')
		new Effect.BlindDown('dataContainer', { duration: 0.4, queue: 'end'});
	else
		new Effect.BlindUp('dataContainer', { duration: 0.4, queue: 'end' });
}

function slideReservationEditContainer()
{
	if($('changeStatusContainer').style.display != 'none')
		new Effect.BlindUp('changeStatusContainer', { duration: 0.4, queue: 'end' });
	
	if($('dataContainer').style.display == 'none')
		new Effect.BlindDown('dataContainer', { duration: 0.4, queue: 'end' }); 
	else
		new Effect.BlindUp('dataContainer', { duration: 0.4, queue: 'end' });
}

function slideStatusContainer()
{
	if($('dataContainer').style.display != 'none')
		new Effect.BlindUp('dataContainer', { duration: 0.4, queue: 'end' });
	
	if($('changeStatusContainer').style.display == 'none')
		new Effect.BlindDown('changeStatusContainer', { duration: 0.4, queue: 'end' });
	else
		new Effect.BlindUp('changeStatusContainer', { duration: 0.4, queue: 'end' });	
}

function userFormSubmit(edit)
{
	var isValid = true;

	if(!Form.Element.present('contact_name'))
	{
		Shake('contact_name');
		isValid = false;
	}
	
	if(!Form.Element.present('company_name'))
	{
		Shake('company_name');
		isValid = false;
	}
	
	if(!Form.Element.present('email'))
	{
		Shake('email');
		isValid = false;
	}
	
	if(!Form.Element.present('login'))
	{
		Shake('login');
		isValid = false;
	}
	
	if(!edit)
		if(!Form.Element.present('passwd'))
		{
			Shake('passwd');
			isValid = false;
		}
	
	if(isValid)
		$('dataForm').submit();	
	else
		$('formWarning').update('Please, fill all the required fields.');
}

function adminUserFormSubmit(edit)
{
	var isValid = true;

	if(!Form.Element.present('name'))
	{
		Shake('name');
		isValid = false;
	}
	
	if(!Form.Element.present('email'))
	{
		Shake('email');
		isValid = false;
	}
	
	if(!Form.Element.present('login'))
	{
		Shake('login');
		isValid = false;
	}
	
	if(!edit)
		if(!Form.Element.present('passwd'))
		{
			Shake('passwd');
			isValid = false;
		}
	
	if(isValid)
		$('dataFormAdmin').submit();	
	else
		$('formWarning').update('Please, fill all the required fields.');
}

function adminNewUserFormSubmit()
{
	var isValid = true;

	if(!Form.Element.present('admin_name'))
	{
		Shake('name');
		isValid = false;
	}
	
	if(!Form.Element.present('admin_email'))
	{
		Shake('email');
		isValid = false;
	}
	
	if(!Form.Element.present('admin_login'))
	{
		Shake('login');
		isValid = false;
	}
	
	if(!Form.Element.present('admin_passwd'))
	{
		Shake('passwd');
		isValid = false;
	}
	
	if(isValid)
		$('dataFormAdmin').submit();	
	else
		$('formWarning').update('Please, fill all the required fields.');
}


function accountFormSubmit()
{
	var isValid = true;

	if(!Form.Element.present('contact_name'))
	{
		Shake('contact_name');
		isValid = false;
	}
	
	if(!Form.Element.present('email'))
	{
		Shake('email');
		isValid = false;
	}
	
	if(isValid)
		$('dataForm').submit();	
	else
		$('formWarning').update('Please, fill all the required fields.');
}

function itemFormSubmit()
{
	var isValid = true;

	if(!Form.Element.present('name'))
	{
		Shake('name');
		isValid = false;
	}
	
	if(isValid)
		$('dataForm').submit();
	else
	{
		$('formWarning').update('Please, fill all the required fields.');
	}
}

function reservationFormSubmit()
{
	var isValid = true;

	if(!Form.Element.present('date_needed'))
	{
		Shake('date_needed');
		isValid = false;
	}
	
	if(!Form.Element.present('shipment_address'))
	{
		Shake('shipment_address');
		isValid = false;
	}
	
	if(!Form.Element.present('city'))
	{
		Shake('city');
		isValid = false;
	}
	
	if(!Form.Element.present('state'))
	{
		Shake('state');
		isValid = false;
	}
	
	if(!Form.Element.present('zip'))
	{
		Shake('zip');
		isValid = false;
	}
	
	if(!Form.Element.present('phone_number'))
	{
		Shake('phone_number');
		isValid = false;
	}
	
	if(!Form.Element.present('estimated_return_date'))
	{
		Shake('estimated_return_date');
		isValid = false;
	}
	
	if(!Form.Element.present('requester_name'))
	{
		Shake('requester_name');
		isValid = false;
	}
	
	if(!Form.Element.present('requester_email'))
	{
		Shake('requester_email');
		isValid = false;
	}
	
	if(!Form.Element.present('send_attention_to'))
	{
		Shake('send_attention_to');
		isValid = false;
	}
	
	if(!Form.Element.present('event_name'))
	{
		Shake('event_name');
		isValid = false;
	}
	
	if(!Form.Element.present('onsite_contact_phone'))
	{
		Shake('onsite_contact_phone');
		isValid = false;
	}
	
	if(!Form.Element.present('onsite_contact'))
	{
		Shake('onsite_contact');
		isValid = false;
	}
	
	if($('purchase_order'))
		if(!Form.Element.present('purchase_order'))
		{
			Shake('purchase_order');
			isValid = false;
		}

	if($('region_id'))
		if(!Form.Element.present('region_id'))
		{
			Shake('region_id');
			isValid = false;
		}
	
	if(isValid)
	{
		new Ajax.Request(ajaxUrl, { postBody: 'ajaxAction=verifyItemAvailability&' + $('dataForm').serialize(), onComplete: reservationFormSubmit_callback});
		// $('dataForm').submit();
	}
	else
	{
		$('formWarning').update('Please, fill all the required fields.');
	}
}

function reservationFormSubmit_callback(request)
{
	if(request.responseText == 'false')
	{
		if(confirm("The item you're trying to reserve already has another reservation on overlapping dates. Are you sure you want to go ahead?"))
			$('dataForm').submit();
	}
	else
		$('dataForm').submit();
}

function removeUser(el, userId)
{
	if(!confirm('Are you sure you want to remove this client? All related reservations and items will be deleted too.'))
		return false;
	else
	{
		var theForm   = Builder.build('<form method="post" action="' + el.href + '"><input type="hidden" name="userId" value="' + userId + '" /></form>');
		el.parentNode.appendChild(theForm);
		theForm.submit();
	}
}

function removeAdmin(el, userId)
{
	if(!confirm('Are you sure you want to remove this admin user?'))
		return false;
	else
	{
		var theForm   = Builder.build('<form method="post" action="' + el.href + '"><input type="hidden" name="userId" value="' + userId + '" /></form>');
		el.parentNode.appendChild(theForm);
		theForm.submit();
	}
}

function addFormClient() { $('dataForm').style.display = 'block'; $('dataFormAdmin').style.display = 'none';}
function addFormAdmin() { $('dataFormAdmin').style.display = 'block'; $('dataForm').style.display = 'none';}

function removeItem(el, itemId)
{
	if(!confirm('Are you sure you want to remove this item? All related reservations will be deleted too.'))
		return false;
	else
	{
		var XHTMLContent = '<form method="post" action="' + el.href + '"><input type="hidden" name="itemId" value="' + itemId + '" /></form>';
		var theForm = Builder.build(XHTMLContent);
		el.parentNode.appendChild(theForm);
		theForm.submit();
	}
}

function tableHovers(tbodyId)
{
	var i, size, trs = document.getElementsByTagName('tr');
	for(i = 0, size = trs.length; i < size; i++)
	{
		//alert(trs[i].ancestors);
		// alert('a');
		// if(trs[i].descendantOf(tbodyId))
		// {
		if(isDescendant(trs[i], tbodyId))
		{
			trs[i].onmouseover = trOver;
			trs[i].onmouseout = trOut;
		}
		// }		
	}		
}

function trOver() { this.className = 'hover'; }
function trOut() { this.className = ''; }

/*
 * My replacement for the descendantOf function from Prototype.js (which doesn't seem to work properly on IE)
 */
function isDescendant(element, ancestor) 
{
	// element = $(element), ancestor = $(ancestor);
	ancestor = $(ancestor);
    while (element = element.parentNode)
      if (element == ancestor) return true;
    return false;
}




function showCalendar(container, inputElement)
{
	if($(container).style.display == 'none')
	{
		
		showLoading($(container).parentNode);
		new Ajax.Updater(container, ajaxUrl, { evalScripts: true, parameters: 'ajaxAction=showCalendar&inputElement=' + inputElement, onComplete: function() { new Effect.BlindDown(container, {duration:0.3, queue: 'end'}); hideLoading($(container).parentNode); } });
	}
	else
		new Effect.BlindUp(container, {duration:0.3});
}

function hideCalendars()
{
	
	hideNeededCalendar();
	hideReturnCalendar();
}

function hideNeededCalendar(){ if($('dateNeededCalendar').style.display != 'none') new Effect.BlindUp('dateNeededCalendar', {duration:0.3}); }
function hideReturnCalendar(){ if($('returnDate').style.display != 'none') new Effect.BlindUp('returnDate', {duration:0.3}); }


function showOnly(trClass)
{
	// var trs = $A(document.getElementsByTagName('tr'));
	var trs = $$('tr');
	trs.each( function(tr) {
		
		if(isDescendant(tr, 'hoverable'))
		{
			if(trClass == 'all' || tr.className == trClass)
				tr.style.display = '';
			else
				tr.style.display = 'none';
		}
			
				
	});
}




