$(function() {
	// Fade out "success", "error" and "warning" messages after 5 seconds
	
	//$(".success, .error, .warning").each(
	setDefaultFocus();

	// Fade out "success" messages after 5 seconds
	$(".success").each(function() {
		$(this).delay(3000).fadeOut(2000);
	});
	
	// Hijack the click event of each "View" link 
	$("a.view").not('[popup]').click(function(e) {
		
		// Get the element to show
		var div = $(this).attr("showElement");
		
		if ($(this).text() == "View") {

			// Hide all visible records
			$("." + $(this).attr("type")).hide();
			
			// Change the text of all "view" links to "View"
			// and remove the "selected" class if it exists
			$("a.view").each(
				function() {
					$(this)
						.text("View")
						.closest("tr")
						.removeClass("selected");
				}
			);
			
			// Show the element
			$("#" + div).fadeIn("fast");
			
			// and change the text of the link to "Hide" and set the class of the row to "selected"
			$(this)
				.text("Hide")
				.closest("tr")
				.addClass("selected");
			
		} else {
			
			// Hide the element
			$("#" + div).fadeOut("fast");
			
			// and change the text of the link back to "View" and remove the "selected" class from the row
			$(this)
				.text("View")
				.closest("tr")
				.removeClass("selected");
		}
			
		// Prevent the link from being followed
		
		e.preventDefault();
	});

	// Bind the 'click' event of all 'View', 'Add', 'Edit' and 'Report' links to the associated hidden 'add', 'edit' and 'report' panel
	$('a.view[popup], a.add[popup], a.edit[popup], a.report[popup]').click(function(e) {
	
		$('body').append('<div id="popup-overlay"></div>').fadeIn('fast');
	
		var _hiddenPanel = $('div#' + $(this).attr('popup'));
		
		_hiddenPanel.center().fadeIn('fast');

		var elemId = $(this).attr('elemId');
		

		setDefaultFocus();
		
		if(elemId > 0) {
			var elemType = $(this).attr('elemType');
			// dynamically load the contents
			loadAdminElement(elemId,elemType);
		}
		// Stop the browser from following the link
		e.preventDefault();
	});
	
	// Bind the 'click' event of the 'Cancel' button on the form to close the form
	$('a.cancel.button, span.close').click(function(e) {
		
		// Stop the form from submitting
		e.preventDefault();
			
		closePopup();
			
		return false;
	});
	
	// finally, if the user hits the 'esc' key, close the popup also
	$(document).keyup(function(e) {
		if (e.keyCode === 0 || e.keyCode === 27) {
			closePopup();
		}
	});

	$('input[type=submit]').click(function() {
		$(this).closest('form').validate();
	});
	
	$('form').submit(function() {
		$(this).validate();
	});
	
	// Validate all forms
	$("form").each(function() {
		$(this).validate();
	});
	
	// Allow the popups to be dragged around by the header
	$('.popup-container .popup-inner').draggable({handle: '.header'});

	// Change the cursor to a pointer on hover of any cell or row that contains an "onclick" attribute
	$('tr[onclick], td[onclick]').hover(
		function() {
			$(this).css('cursor', 'pointer');
		},
		function() {
			$(this).css('cursor', 'default');
		}
	);
	
	// Apply the jQuery UI Tabs plugin to any element with a "tabs" class
	$(".tabs").tabs();
	
	// Apply the jQuery UI DatePicker plugin to any element with a "datepicker" class
	$(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' });
	
});

function loadPopUp(panelId,elemId,elemType) {

	$('body').append('<div id="popup-overlay"></div>').fadeIn('fast');
	
		var _hiddenPanel = $('div#' + panelId);
		
		_hiddenPanel.center().fadeIn('fast');

		setDefaultFocus();
		
		if(elemId > 0) {
			// dynamically load the contents
			loadAdminElement(elemId,elemType);
		}
		// Stop the browser from following the link
		// e.preventDefault();
}

function setDefaultFocus() {
	
	// Automatically set the focus to the first visible input field on the current form
	var _visibleInputs = $(":input:visible[type!=hidden]").not("input[type=submit]");
	
	for (var i = 0; i < _visibleInputs.length; i++) {
		_input = _visibleInputs.eq(i);
		if (!_input.hasClass("datepicker") && !_input.hasClass("no-focus")) {
			_input.focus();
			break;
		}
	}

	// Now check if we need to set the focus to the field with the id as specified in the form's 'focus' attribute instead,
	// as that should override the focus set above
//	$('form[focus]:visible').each(function() {
//		var _input = $(':input[id='+$(this).attr('focus')+'], :input[name='+$(this).attr('focus')+']');
//		if (_input.length > 0)
//			_input.eq(0).select();
//	});
	
}
function checkAll(field)
{

for (i = 0; i < field.length; i++)
	field[i].checked = true ;
}

function setCheckedValue(radioObj, newValue) {
	
	if(!radioObj)
		return;
	
	var radioLength = radioObj.length;

	if(radioLength == undefined) {	
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}


function uncheckAll(field)
{
for (i = 0; i < field.length; i++)
	field[i].checked = false ;
}

function popUpMsg(text) {

	if(text.length>0) {
		alert(text);
	}
}

function DoNav(theUrl)
  {
  document.location.href = theUrl;
  }

function DoNavLinkNav(theUrl) {

	document.location.href = theUrl;
}
function ChangeColorZeb(tableRow, highLight, rtnCol) {


    if (highLight)
    {
      tableRow.style.backgroundColor = '#96CEFF';
      tableRow.style.cursor = 'pointer';
    
		//tableRow.className = 'adminhoverRow';
			
    }
    else
    {
      tableRow.style.backgroundColor = rtnCol;
      tableRow.style.cursor = 'default';
	  //tableRow.className = 'adminRow';
    }
  }

  function DoNav(theUrl)
  {
  document.location.href = theUrl;
  }
  
  
  function closeClubSearch() {
	
	document.getElementById('clubSearch').style.display = "none";
	
	//alert("Please leave the name field blank. Then populate the form below adding in the category and the club above")
	
	}

	function selectEvent(box, num, price, epId) {
		
		var name = 'epList[' + num +']';
		var elm = document.getElementById(name); 
		elm.style.display = box.checked? "":"none"; 
		
		document.getElementById('entryPackage.id').value = epId;
	
		
		if(elm.style.display == "none") {
			updateTotal("minus",price);
		} else {
			updateTotal("plus",price);
		}
		
	} 
	function updateTotal(up,price) {
		
		var total = parseFloat(document.getElementById('order.orderTotal').value);
		
		if(up=="plus") {
			if(total > 0) {
				total = total + price;
			} else {
				total = price;
			}
		} else {
			total = total - price;
		}
		document.getElementById('order.orderTotal').value = total;
		
		document.getElementById('totalPrice').innerHTML  = '<strong>£' + total + '</strong>';
	}
function saveEntrantClub() {

	var dropdownIndex = document.getElementById('clubs_found').selectedIndex;
	var dropdownValue = document.getElementById('clubs_found')[dropdownIndex].value;
	var dropdownText = document.getElementById('clubs_found')[dropdownIndex].text;
	
	//hide drop down
	document.getElementById('clubSearch').style.display = "none";
	
	if(document.getElementById('clubLookup.id') == null) {
		
		document.getElementById('clubName').value = dropdownText;
		document.getElementById('entry.club.id').value = dropdownValue;
		
	} else {
		// update hidden field with id
		document.getElementById('clubLookup.id').value = dropdownValue;
		// put text in drop down
		document.getElementById('clubLookup.name').value = dropdownText
	}
}

function saveAdminEntrantClub() {

	var dropdownIndex = document.getElementById('clubs_found').selectedIndex;
	var dropdownValue = document.getElementById('clubs_found')[dropdownIndex].value;
	var dropdownText = document.getElementById('clubs_found')[dropdownIndex].text;

	//hide drop down
	document.getElementById('clubSearchAdmin').style.display = "none";
	
	if(document.getElementById('clubLookup.id') == null) {
		
		document.getElementById('clubName').value = dropdownText;
		document.getElementById('entry.club.id').value = dropdownValue;
		
	} else {
		// update hidden field with id
		document.getElementById('clubLookup.id').value = dropdownValue;
		// put text in drop down
		document.getElementById('clubLookup.name').value = dropdownText
	}
}


function ajaxClubAdminList(field,updatediv) {
	
	//get search string
	var search = document.getElementById(field).value;
	
	search = search.replace("'","_");
		
		document.getElementById('clubSearchAdmin').style.display = "block";
		
		var params = "search=" + search;
		
		var myAjax = new Ajax.Updater(
	    			"clubSearchAdmin", 
	    			"ajax/club?mode=club&tem=admin&", 
	    			{method: "get", onFailure: notifyFailure, 
	    			parameters: "mode=club&tem=admin&search=" + search });
	    
      

}

function processContact(originalRequest) {
	
	eval(originalRequest.responseText);	
	document.getElementById('clubSearch').style.display = "none";
}
	
function notifyFailure(originalRequest)
{
  alert("Failed AJAX Request.")
    
}

function adminLoadNavItem(navId) {
	
	$j.getScript("ajaxLookup.htm?mode=nav&navId="+ navId, function(){
  		
	});
	
	
}

function adminLoadNewNavItem(siteId, sectionId, parentId, navId, level) {
	
	document.getElementById('navigationEdit.title').value =  ''; 
	document.getElementById('pageTitle').value =  '';
	document.getElementById('navigationEdit.page.id').value = '';
	document.getElementById('navigationEdit.order').value = '';
	document.getElementById('navigationEdit.url').value = '';
	
	document.getElementById('navigationEdit.level').value = level;
	document.getElementById('navigationEdit.navId').value = navId;
	document.getElementById('navigationEdit.parent.id').value = parentId;
	document.getElementById('navigationEdit.section.id').value = sectionId;
	document.getElementById('navigationEdit.site.id').value = siteId;
	alert("New Link Created - Please populate the form");
}

function addPageToNav() {
		
		
		if(document.getElementById('navigationEdit.navId') == null) {
			alert("Please Select a Navigation Link on the Left Hand Side First");
		} else {
		
			// set hidden values
			var pageId = document.getElementById("pageList");
			document.getElementById("navigationEdit.page.id").value = pageId.options[pageId.selectedIndex].value;
			document.getElementById("pageTitle").value = pageId.options[pageId.selectedIndex].text;
		
		}

}

function ajaxEntryList(field,updatediv) {
	
	//get search string
	var search = document.getElementById(field).value;
	
	search = search.replace("'","_");
		
		document.getElementById('entrySearch').style.display = "block";
		
		var params = "search=" + search;
		
		var myAjax = new Ajax.Updater(
	    			"entrySearch", 
	    			"ajaxSearch.htm", 
	    			{method: "get", onFailure: notifyFailure, 
	    			parameters: "mode=ent&search=" + search });

}

function ajaxEntrantSearch() {
	
	//get search string
	var lastname = document.getElementById('entry.entrant.lastname').value;
	var firstname = document.getElementById('entry.entrant.firstname').value;
	//var dob = document.getElementById('entry.entrant.dob').value;
	
	lastname = lastname.replace("'","_");
	firstname = firstname.replace("'","_");
		
	document.getElementById('entrySearch').style.display = "block";
		
		var myAjax = new Ajax.Updater(
	    			"entrySearch", 
	    			"ajaxSearch.htm", 
	    			{method: "get", onFailure: notifyFailure, 
	    			parameters: "mode=entfull&lastname=" + lastname + "&firstname=" + firstname });

}

function getAdminEntrant(entId) {

	
	//lookup entrant
	var url="ajaxSearch.htm";
    
    	//send to the database
    
    	var params = "mode=entrant&entId=" + entId;
    
		var myAjax = new Ajax.Request(
        				url, 
        				{method: 'get', parameters: params, 
        				asynchronous: true, onSuccess: processEntrant,onFailure: notifyFailure});
}

function getEntrant() {

	var dropdownIndex = document.getElementById('entrants_found').selectedIndex;
	var dropdownValue = document.getElementById('entrants_found')[dropdownIndex].value;
	var dropdownText = document.getElementById('entrants_found')[dropdownIndex].text;
	
	//lookup entrant
	var url="ajaxSearch.htm";
    
    	//send to the database
    
    	var params = "mode=entrant&entId=" + dropdownValue;
    
		var myAjax = new Ajax.Request(
        				url, 
        				{method: 'get', parameters: params, 
        				asynchronous: true, onSuccess: processEntrant,onFailure: notifyFailure});
}
function closeEntrantSearch() {

	document.getElementById('entrySearch').style.display = "none";
}
function closeClubSearch() {

	document.getElementById('clubSearch').style.display = "none";
}
function closeClubSearchAdmin() {

	document.getElementById('clubSearchAdmin').style.display = "none";
}
function processEntrant(originalRequest) {
	
	eval(originalRequest.responseText);	
	document.getElementById('entrySearch').style.display = "none";
}

function ajaxClubListSet(field,updatediv,clubSet) {
	
	//get search string
	var search = document.getElementById(field).value;

	search = search.replace("'","_");
		
		document.getElementById('clubSearch').style.display = "block";
		$("#clubLookup\\.id").val(0);
	
		var params = "search=" + search+"&clubSet="+clubSet;
		var div = '#'+updatediv;
		
		var url = "/ps/ajax/club?"+ params;

		if(search.length >= 1 ) {	
			$(div).html(geturl(url)); 
			$(div).show(1000);
		} else {
			$(div).hide(500);
		}

		
	//		var myAjax = new Ajax.Updater(
	  //  			"clubSearch", 
	  //  			"/ss/ajax/club", 
	   // 			{method: "get", onFailure: notifyFailure, 
	   // 			parameters: params });

}

function ajaxClubList(field,updatediv) {
	
	//get search string
	var search = document.getElementById(field).value;
	
	search = search.replace("'","_");
		
		document.getElementById('clubSearch').style.display = "block";
		
		var params = "search=" + search;
		
		var myAjax = new Ajax.Updater(
	    			"clubSearch", 
	    			"/ss/ajax/club", 
	    			{method: "get", onFailure: notifyFailure, 
	    			parameters: "search=" + search });

}

function updateTotals(index) {

	var lowEntryMarkup =  parseFloat(document.getElementById("lowEntryMarkup").value);
	var highEntryMarkup =  parseFloat(document.getElementById("highEntryMarkup").value);
	var lowEntryMarkupThreshold =  parseFloat(document.getElementById("lowEntryMarkupThreshold").value);
	var element = "epList[" + index + "].price";
	var markup = "epList[" + index + "].markup";
	var total = "epList[" + index + "].totalPrice";
	
	var value = parseFloat(document.getElementById(element).value);
	if(value >= lowEntryMarkupThreshold) {
		document.getElementById(markup).value = highEntryMarkup;
		var newTotal = value+highEntryMarkup;
		document.getElementById(total).value = newTotal;
	} else {
		document.getElementById(markup).value = lowEntryMarkup;
		var newTotal = value+lowEntryMarkup;
		document.getElementById(total).value = newTotal;
	}

}

function updateMarkup(index) {

	var element = "epList[" + index + "].price";
	var markup = "epList[" + index + "].markup";
	var total = "epList[" + index + "].totalPrice";

	var value = parseFloat(document.getElementById(element).value);
	varNewMarkup = parseFloat(document.getElementById(markup).value);
	var newTotal = value+varNewMarkup;
	document.getElementById(total).value = newTotal;
}

function setSelectedIndex(s, v) {

    for ( var i = 0; i < s.options.length; i++ ) {

        if ( s.options[i].value == v ) {
            s.options[i].selected = true;
            return;
        }
    }
}

function setDate(field, component, hiddenField) {
	
	var updateval = document.getElementById(field).value;
	var currentdate = document.getElementById(hiddenField).value;
	
	var mydate = new Array();
	mydate = currentdate.split('/');
	
	if(mydate.length != 3) {
		mydate = ['01','01','1900'];
	}
	
	if (component=='d') {
		mydate[0] = updateval;
	} else if (component=='m') {
		mydate[1] = updateval;
	} else if (component=='y') {
		mydate[2] = updateval;
	}

	document.getElementById(hiddenField).value = mydate[0] + '/' + mydate[1] + '/' + mydate[2];
	
}

function validateEventSearchForm() {

 if(document.getElementById("searchInfo.dateFrom").value == 'dd/mm/yyyy') {
 	document.getElementById("searchInfo.dateFrom").value = '';
 }
 if(document.getElementById("searchInfo.dateTo").value == 'dd/mm/yyyy') {
 	document.getElementById("searchInfo.dateTo").value = '';
 }
 
}


function copyValidate(field) {
	
	var newEntrant = "entrant." + field;
	var extEntrant = "validationInfo.existingEntrant." + field;
		
	if(field == 'dob') {
		
		var newday = "dob.day";
		var exday = "dob2.day";
		
		// day
		var dropdownIndex = document.getElementById(newday).selectedIndex;
		var dropdownValue = document.getElementById(newday)[dropdownIndex].value;
		
		setSelectedIndex(document.getElementById(exday),dropdownValue);
		
		// month
		var newmth = "dob.mth";
		var exdmth = "dob2.mth";
		dropdownIndex = document.getElementById(newmth).selectedIndex;
		dropdownValue = document.getElementById(newmth)[dropdownIndex].value;
		
		setSelectedIndex(document.getElementById(exdmth),dropdownValue);
		
		// year
		var newyr = "dob.yr";
		var exyr = "dob2.yr";
		dropdownIndex = document.getElementById(newyr).selectedIndex;
		dropdownValue = document.getElementById(newyr)[dropdownIndex].value;
		
		setSelectedIndex(document.getElementById(exyr),dropdownValue);
		
		// hidden fields
		document.getElementById(extEntrant).value = document.getElementById(newEntrant).value;
		
	} else if (field == 'gender') {
		
		len = document.getElementById(newEntrant).length;
		alert(len)
		 for (i = 0; i <len; i++) {
			if (document.getElementById(newEntrant)[i].checked) {
				chosen = document.getElementById(newEntrant)[i].value
			}
		}
		
		len = document.getElementById(extEntrant).length;
		
		for (i = 0; i <len; i++) {
			if (document.getElementById(extEntrant)[i].value == chosen) {
				document.getElementById(newEntrant)[i].checked = true;
			}
		}
		
	} else {
		
			
		
		document.getElementById(extEntrant).value = document.getElementById(newEntrant).value;
		
	} 
}

function validate_email(field,alerttxt)
{
with (field)
  {
  apos=value.indexOf("@");
  dotpos=value.lastIndexOf(".");
  if (apos<1||dotpos-apos<2)
    {alert(alerttxt);return false;}
  else {return true;}
  }
}

function validate_required(field,alerttxt)
{
with (field)
  {
  if (value==null||value=="")
    {
    alert(alerttxt);return false;
    }
  else
    {
    return true;
    }
  }
}

function validate_create_form(thisform)
{
with (thisform)
  {
  if (validate_required(message,"Please enter an email address")==false)
  {message.focus();return false;}
  
  if (validate_email(message,"Please enter a valid email address")==false)
  {message.focus();return false;}
  }
}


function addProductToOrder(box, count,price) {

	var name = 'productItemList[' + count +']';
	var elm = document.getElementById(name); 
	elm.style.display = box.checked? "":"none"; 
		
		
		if(elm.style.display == "none") {
			updateTotal("minus",price);
		} else {
			updateTotal("plus",price);
		}
		
}

function addProditemItemToOrder(dropDown, price) {

	var dropdownIndex = dropDown.selectedIndex;
	var dropdownValue = dropDown[dropdownIndex].value;
	
	
	if(dropdownValue == 0) {
		updateTotal("minus",price);
	} else {
		updateTotal("plus",price);
	}
	
}

function removeEvent(id) {

	//lookup entrant
	var url="removeEvent";
    
    	//send to the database
    
    	var params = "id=" + id;
    
		var myAjax = new Ajax.Request(
        				url, 
        				{method: 'get', parameters: params, 
        				asynchronous: true, onSuccess: processRemoveEvent,onFailure: notifyFailure});

}
function processRemoveEvent(originalRequest) {
	
	eval(originalRequest.responseText);	
}

function addProductToCart(itemId) {

	document.getElementById("product.id").value=itemId;
	document.form.submit();
}

function addProductItemToCart(itemId) {
	
	var param = document.getElementById("productId_" + itemId);
	var myParam = param.options[param.selectedIndex].value;
	
	if(myParam==0) {
		alert("No Item Selected");
		return
	}
	
	document.getElementById("productItem.id").value=myParam;
	document.form.submit();
}

function geturl(addr) {
var r = $.ajax({
 type: 'GET',
 url: addr,
 async: false
}).responseText;

return r;
}



function ajaxMemberList(field,updatediv) {
	//get search string
	
	var search = $('#memberName').val()
	
	search = search.replace("'","_");
	
	if(search.length >= 3 ) {
		
	
		var url = "/ps/ajaxContactSearch?mode=mem&search=" + search;
		
		$('#memberSearch').html(geturl(url));
		$('#memberSearch').show(1000);
	} else {
		$('#memberSearch').hide(1000);
	}
	/*
	var search = document.getelementbyid(field).value;
	
	search = search.replace("'","_");
	
	document.getelementbyid('membersearch').style.display = "block";
		
	var myajax = new ajax.updater(
	    			"membersearch", 
	    			"/ss/ajaxcontactsearch", 
	    			{method: "get", onfailure: notifyfailure, 
	    			parameters: "mode=mem&search=" + search });
	    
  */
}

function saveGroupMember() {

	// get the value of the record selected
	var dropdownValue = $('#members_found').val();
	var groupId = $('#group\\.id').val(); 

	var url = "/ps/ajaxGroupMembers?list&groupId=" + groupId + "&contactId="+dropdownValue;

	//hide drop down
	$('#memberSearch').hide(1000);
	$('#memberName').val("");

	$('#groupMembers').html(geturl(url));

//	var myAjax = new Ajax.Updater(
//	    			"groupMembers", 
//	    			"/ss/ajaxGroupMembers", 
//	    			{method: "get", onFailure: notifyFailure, 
//	    			parameters: "mode=list&groupId=" + groupId + "&contactId="+dropdownValue });
	    
  
}


function ajaxEntrySearch(mode) {
	//get search string

	var search = $('#contactName').val()
	
	search = search.replace("'","_");

	if(search.length >= 0 ) {
		
	
		var url = "/ps/ajaxContactSearch?mode=" + mode + "&pageSize=100&search=" + search;

		$('#contactSearch').html(geturl(url));
		$('#contactSearch').show(1000);
	} else {
		$('#contactSearch').hide(1000);
	}
}

function loadContactEntry(mode) {

	// get the value of the record selected
	var dropdownValue = $('#contact_found').val();

	var url = "/ps/ajaxLoadContact?list&mode="+ mode + "&contactId="+dropdownValue;

	$.getScript(url);

}

function readyToSendEmail() {
	
	confirm("Please confirm you wish to send this email");
	document.getElementById("sendMail").value= "Yes";
	
}

function addEmailParam() {
	
	var param = document.getElementById("paramList");
	
	var inst = tinyMCE.getInstanceById('email.message');
	
	var text = inst.getContent();
	
	var myParam = param.options[param.selectedIndex].value;
	
	text = text + "$"+ myParam;
	
	// Do you ajax call here
    inst.setContent(text);

}
function addLetterParam() {

	var param = document.getElementById("paramList");
	var text = document.getElementById("email.message").value;
	var myParam = param.options[param.selectedIndex].value;
	text = text + "$"+ myParam;
	
	document.getElementById("email.message").value = text;
	
}

function loadAdminElement(elemId, elemType) {

	var url = "/ps/ajaxLoadElement?elemId="+ elemId + "&elemType="+elemType;

	$.getScript(url);
}

function removeTeamEntry(id, contactId) {

	// clear the team row
	$('#teamEntry\\.teamEntries\\['+id+'\\]\\.entrant\\.firstname').val('');
	$('#teamEntry\\.teamEntries\\['+id+'\\]\\.entrant\\.lastname').val('');
	$('#teamEntry\\.teamEntries\\['+id+'\\]\\.entrant\\.dob').val('');
	$('#teamEntry\\.teamEntries\\['+id+'\\]\\.entrant\\.gender').val('');
	$('#dob'+id+'\\.day').val('');
	$('#dob'+id+'\\.mth').val('');
	$('#dob'+id+'\\.yr').val('');
	
	// remove from used contacts
	var contactIds = $('#contactIds').val();
	contactIds.replace(contactId + ',');
    $('#contactIds').val(contactIds);
	
	// blank the delete option
	$('#options' + id).html('');

}

function closePopup() {
	
	// Fade out and then remove the overlay
	$('div#popup-overlay').fadeOut(
		'fast',
		function() {
			$(this).remove();
		}
	);
	
//	$('div#edit_clinic')
//		.fadeOut('fast')
//		.find('form')[0]
//		.reset();
	
	// Fade out the popup
	$('div.popup-container:visible').fadeOut('fast').find('form')[0].reset();

}

function keepSessionAlive() {
	$.get("keepSessionAlive");
}

// Custom jQuery method for centering an element on screen
jQuery.fn.center = function() {
    this.css("position","fixed");
    this.css("top", ( ( $(window).height() - this.outerHeight() ) / 2 ) + "px");
    this.css("left", ( ( $(window).width() - this.outerWidth() ) / 2 ) + "px");
    return this;
}

// Custom jQuery method for centering an element within a containing element
jQuery.fn.centerInContainer = function (container) {

	// Check if the specified container already has a "#" at the start
	if (container.indexOf('#') != 0) container = '#' + container;
	
	// Specify the position as "fixed" so that we can position it wherever we want on screen
    this.css("position","fixed");
    
    // Now calculate the top and left of the element that will place it in the center of the container
    this.css("top", ( ( $(container).outerHeight() - this.outerHeight() ) / 2 ) + ( $(container).position().top + $(container).scrollTop() ) + "px");
    this.css("left", ( ( $(container).outerWidth() - this.outerWidth() ) / 2 ) + ( $(container).position().left + $(container).scrollLeft() ) + "px");
    
    return this;
}


