// String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g,''); }

function TB_alert(title, message)
{
	TB_show(title, message, 'innerHTML');
}

function TB_url(url, title)
{
	return TB_show(title, url);
}

function loadUrl(url, div, CssClass, width, height, inherit, id, postBody)
{
	if (window.ie7 && !window.loaded)
	{
		window.addEvent('load', function() {
			_loadUrl(url , div , CssClass , width , height, inherit, id, postBody);
			window.loaded = true;
		});
	}
	else
		_loadUrl(url , div , CssClass , width , height, inherit, id, postBody);
}

function _loadUrl(url, div , CssClass , width , height, inherit, id, postBody) {
	if (!id) {
		var id = 'divAjax';
		var zindex = '999';
	}
	if ($(id)) {
		$(id).remove();
		var zindex = '9999';
	}
	if (id == 'album' && $('divAjax'))
		div = 'divAjax';
		
	var zindex = '9999';
	var content = new Element('div');
	var container  = new Element('div').setProperty('id',id).setStyle('z-index',zindex);		
	
	if (CssClass) {
		container.addClass(CssClass);
	}
	
	if (width) {
		container.setStyle('width',width+'px');
		content.setStyle('width',width+'px');
	}
	
	if (height) {
		container.setStyle('height',height+'px');
		content.setStyle('height',height+'px');
	}
	content.injectInside(container);
	
	if (!div || !$(div)) 
		div = document.body;

	position = $(div).getPosition();
	size = $(div).getSize();
	position.x = position.x - (width / 2);
	position.y = position.y - (height / 2);
	if (position.y < 0)
		position.y = 100;
		
	$(container).setStyles({
		position: 'absolute'
	});
	
	if (div && $(div) && div != window.document.body)	
	{
		container.injectBefore(div);
	}
	else
	{
		if ($('main'))
			container.injectBefore($('main'));
		else if ($('container'))
			container.injectBefore($('container'));
		else 
		{
			container.injectInside(document.body);
			container.setStyles({
				top:'100px',
				left:'100px'
			});
		}
		scroll(container);
	}
	
	if (!window.lastZIndex)
		window.lastZIndex =10000;
	else
		window.lastZIndex+=1;
		
	container.setStyle('z-index', window.lastZIndex);
	
	var menu = new Element('div')
	.setProperty('id','ajax_menu')
	.injectBefore(content);
	
	var move = new Element('img').setProperty('src','/thm/images/ajax_drag.gif')
	.setProperty('alt','')
	.setStyle('cursor','move')
	.injectInside(menu);
	var localThis = this;
	hideSelects();
	var close = new Element('img').setProperty('src','/thm/images/ajax_close.gif')
	.injectInside(menu)
	.addEvent('click', function (e) {
		/* Restore our poor selects */
		unhideSelects();
		$(id).remove();
	});
	$(id).makeDraggable({handle: move});
	content.setHTML('<br><br><center>Loading <img src="/thm/images/loading.gif" /> </center>');
	if (postBody) 
	{
		new Ajax(url, {
			evalScripts:true,
			update:content,
			data: postBody,
			method: 'POST'})
			.request();
	}
	else
	{
		new Ajax(url, {
			evalScripts:true,
			update:content,
			method: 'get'})
			.request();
	}
	if (inherit != true)  { scroll($('divAjax')); }
}

function hideSelects(except)
{
/* Hide selects for IE */
	if(window.ie) {
		var selectsToHide = document.getElementsByTagName("select");
		window.selectsHidden = new Array();
		for (i=0;i<selectsToHide.length;i++)
		{
			if (selectsToHide[i] == except)
				continue;
			if (selectsToHide[i].style.display != 'none')
			{
				window.selectsHidden.push(selectsToHide[i]);
				selectsToHide[i].style.visibility = 'hidden';
			}
		}
	}	
}

function unhideSelects()
{
	/* Hide selects for IE */
	if (window.ie && window.selectsHidden && window.selectsHidden.length) {
		for (i=0;i<window.selectsHidden.length;i++)
		{
			window.selectsHidden[i].style.visibility = 'visible';
		}
	}	
}

function unloadUrl()
{
	unhideSelects();
	$('divAjax').remove();
}

function sendForm (form,update,redirect) {
	var form = $(form);
	var update = (!update)? form.getProperty('id') : update;
	var query = form.toQueryString();
	var url = form.getProperty('action') + '/method/ajax/1';
	var Options = {
		update: update,
		method: 'POST',
		postBody: query,
		onComplete: function () {
			if (this.response.text == 'redirect') {
				window.location.href= redirect;
			}
		},
		evalScripts:true,
		asynchronous: true
	};
	new Ajax(url, Options).request();
}

function sendForm_iframed(form,update)
{
	form = $(form);
	update = $(update);
	iframe = new Element('iframe');
	$(iframe).setProperty('src', 'about:blank').setProperty('name', '__dummySubmitTarget').setStyle('display', 'none');
	iframe.injectInside(document.body);
	form.target = iframe.name;
	form.submit();
	(function() {
	iframe.addEvent('domready', function() {
		update.innerHTML = iframe.contentWindow.document.body.innerHTML;
		iframe.remove();
	});
	}).delay(1000);
	return false;
}

function DeleteItem (item) {
	if (confirm('Do you want delete this post?')) {
		var url = '/administration/ajax/deleteitem/id/'+item;
		var Options = {
			update: 'myposts',
			method: 'GET',
			evalScripts:true,
			asynchronous: true
		};
		new Ajax(url, Options).request();
		$('item'+item).setStyle('background','#DFAB99');
	}
}

function selectAvatar(avatar) {
	$('avatar').value = avatar;
	$('avatar_image').setProperty('src' , 'thm/images/avatars/'+avatar);
	//$('divAjax').setStyle('display','none');
	unloadUrl();
}

function selectOption(obj,t) {
	var img = $(obj).getElements('img');
	$('weblog_template').value = t;
	$('divAjax').setStyle('display','none');
	$('weblog_template_image').setProperty('src',img.getProperty('src'));
}
function check_ajax(id,table) {
	if (id.value.length > 1) {
		if (id.name == 'user_name')
		{
			if (!/^[a-z0-9]+$/i.test(id.value))
			{
				alert("Please use only letters (a-z) or numbers (0-9) in your Login name. No spaces or other characters are allowed.");
				id.value = "";
				id.focus();
				return false;
			}
		}
		if ($('img'+id.name)) $('img'+id.name).remove();
		url = 'default/ajax/check';
		if (table == 'web_log')
		url = '/administration/ajax/check';
		var img = new Element('img').setProperty('src','thm/images/loading.gif')
		.addClass('input_load')
		.injectAfter(id);
		var update = new Element('div');

		var query = "&key=" + id.name + '&value='+ id.value;
		var Options = {
			update: update,
			method: 'POST',
			postBody: query,
			onComplete: function () {
				// invalid
				if (this.response.text.length > 2) {
					id.value = '';
					alert(this.response.text);
					id.focus();
				} else {
					if (!$('img' + id.name))
					{
						var imgvalid = new Element('img').setProperties({'src':'thm/images/checkblue.gif'
						, 'id' : 'img'+id.name })
						.addClass('input_ok')
						.injectAfter($(id));
					}
				}
				img.remove();
			},
			evalScripts:true,
			asynchronous: true
		};
		new Ajax(url, Options).request();
	}
}
function check_not_ajax(id,table)
{
	if (id.name == 'password' && id.name != 'repeat-password') 
	{
		if (id.value.length < 3)
		{
			if ($('img'+id.name))
				$('img'+id.name).remove();
			alert('Password too short.');
			id.value = '';
			return false;
		}		
	}
	else if (id.name == 'repeat-password')
	{
		if (!$('pass_word').value || $('pass_word').value != id.value)
		{
			if ($('img'+id.name))
			{
				$('img'+id.name).remove();
			}
			alert('Passwords do not match.');			
			$('pass_word').value = id.value = '';
			$('pass_word').focus();						
			return false;
		}
	}
	if (!$('img'+id.name))
	{
		var imgvalid = new Element('img').setProperties({'src':'thm/images/checkblue.gif', 'id' : 'img'+id.name })
		.addClass('input_ok')
		.injectAfter($(id));
	}
	return true;
}
function generateCodeForButton(model, url)
{
	// MODEL 1
	code_88x1_html		= '<a title="Visit my weight loss blog" alt="Visit my weight loss blog" href="'+ url +'"><img border="0" src="http://www.extrapounds.com/images/button_links/88x1.jpg"></a>';
	code_140x1_html		= '<a title="Visit my weight loss blog" alt="Visit my weight loss blog" href="'+ url +'"><img border="0" src="http://www.extrapounds.com/images/button_links/140x1.jpg"></a>';
	code_88x1_bbcode	= '[url='+ url +'] [img]http://www.extrapounds.com/images/button_links/88x1.jpg[/img][/url]';
	code_140x1_bbcode	= '[url='+ url +'] [img]http://www.extrapounds.com/images/button_links/140x1.jpg[/img][/url]';
	code_88x1_ezcode	= '[link='+ url +'][image noborder]http://www.extrapounds.com/images/button_links/88x1.jpg[/image][/link]';
	code_140x1_ezcode	= '[link='+ url +'][image noborder]http://www.extrapounds.com/images/button_links/140x1.jpg[/image][/link]';
	// MODEL 2
	code_88x2_html		= '<a title="Visit my weight loss blog" alt="Visit my weight loss blog" href="'+ url +'"><img border="0" src="http://www.extrapounds.com/images/button_links/88x2.jpg"></a>';
	code_140x2_html		= '<a title="Visit my weight loss blog" alt="Visit my weight loss blog" href="'+ url +'"><img border="0" src="http://www.extrapounds.com/images/button_links/140x2.jpg"></a>';
	code_88x2_bbcode	= '[url='+ url +'] [img]http://www.extrapounds.com/images/button_links/88x2.jpg[/img][/url]';
	code_140x2_bbcode	= '[url='+ url +'] [img]http://www.extrapounds.com/images/button_links/140x2.jpg[/img][/url]';
	code_88x2_ezcode	= '[link='+ url +'][image noborder]http://www.extrapounds.com/images/button_links/88x2.jpg[/image][/link]';
	code_140x2_ezcode	= '[link='+ url +'][image noborder]http://www.extrapounds.com/images/button_links/140x2.jpg[/image][/link]';
	// MODEL 3
	code_88x3_html		= '<a title="Visit my weight loss blog" alt="Visit my weight loss blog" href="'+ url +'"><img border="0" src="http://www.extrapounds.com/images/button_links/88x3.jpg"></a>';
	code_140x3_html		= '<a title="Visit my weight loss blog" alt="Visit my weight loss blog" href="'+ url +'"><img border="0" src="http://www.extrapounds.com/images/button_links/140x3.jpg"></a>';
	code_88x3_bbcode	= '[url='+ url +'] [img]http://www.extrapounds.com/images/button_links/88x3.jpg[/img][/url]';
	code_140x3_bbcode	= '[url='+ url +'] [img]http://www.extrapounds.com/images/button_links/140x3.jpg[/img][/url]';
	code_88x3_ezcode	= '[link='+ url +'][image noborder]http://www.extrapounds.com/images/button_links/88x3.jpg[/image][/link]';
	code_140x3_ezcode	= '[link='+ url +'][image noborder]http://www.extrapounds.com/images/button_links/140x3.jpg[/image][/link]';
	// MODEL 4
	code_88x4_html		= '<a title="Visit my weight loss blog" alt="Visit my weight loss blog" href="'+ url +'"><img border="0" src="http://www.extrapounds.com/images/button_links/88x4.jpg"></a>';
	code_140x4_html		= '<a title="Visit my weight loss blog" alt="Visit my weight loss blog" href="'+ url +'"><img border="0" src="http://www.extrapounds.com/images/button_links/140x4.jpg"></a>';
	code_88x4_bbcode	= '[url='+ url +'] [img]http://www.extrapounds.com/images/button_links/88x4.jpg[/img][/url]';
	code_140x4_bbcode	= '[url='+ url +'] [img]http://www.extrapounds.com/images/button_links/140x4.jpg[/img][/url]';
	code_88x4_ezcode	= '[link='+ url +'][image noborder]http://www.extrapounds.com/images/button_links/88x4.jpg[/image][/link]';
	code_140x4_ezcode	= '[link='+ url +'][image noborder]http://www.extrapounds.com/images/button_links/140x4.jpg[/image][/link]';

	textAreaObject_bbcode = $('blogLinkbbCode');
	textAreaObject_bbcode.value = eval('code_'+model+'_bbcode');

	textAreaObject_htmlcode = $('blogLinkHtmlCode');
	textAreaObject_htmlcode.value = eval('code_'+model+'_html');

	textAreaObject_ezcode = $('blogLinkezCode');
	textAreaObject_ezcode.value = eval('code_'+model+'_ezcode');
}

function DeleteFriend (friend, $page) {
	if (!$page)
		$page = 0;
	if (confirm('Do you want to remove this friend from the list?')) {
		var url = '/administration/ajax/delFriend/id/'+friend+'?page=' + $page;
		var Options = {
			update: 'myfriends',
			method: 'GET',
			evalScripts:true,
			asynchronous: true
		};
		new Ajax(url, Options).request();
	}
}

function SaveFriendNote(friend, note, banned)
{
	if (window.ie)
		note = escape(note);
		var url = '/administration/ajax/saveFriendNote/id/'+friend + '?note=' + note;
		if (banned)
			url += '&banned=true';
			
		var Options = {
			onComplete: function() { $( (banned?'blocked_note_':'friends_note_')+friend+'_save').addClass('hide') },
			method: 'GET',
			evalScripts:true,
			asynchronous: true
		};
		new Ajax(url, Options).request();
}

function validateURL(txtURL)
{
	lengthValue = (txtURL).trim();
	
	if(lengthValue.length != 0) {
		var j = new RegExp();
		j.compile("^[A-Za-z]+://[A-Za-z0-9-]+\.[A-Za-z0-9]+");
				
		if (!j.test(lengthValue)) {
			alert('Please enter valid URL');
			return false;
		}
		return true;
	}
}
function deleteMp3(mp3) {
 if (confirm("Do you want Remove this mp3?")) {
	$('mp3-' + mp3).remove();
 }
}
function addMp3 (link, title) {

	if (validateURL(link)) {
		if (link.test("\.mp3", "i")) {
			$('divmp3').innerHTML = "<img src='/thm/images/loading.gif'> checking mp3 ... ";
			var url = '/administration/weblog/addmp3/ajax/1/?mp3='+link+'&title='+title;
			var Options = {
				update: 'divmp3',
				method: 'GET',
				evalScripts:true,
				asynchronous: true
			};
			new Ajax(url, Options).request();
		} else {
			alert('Only mp3 links');			
		}
	}
	/*if (confirm('Do you want to remove this friend from the list?')) {
		var url = '/administration/ajax/delFriend/id/'+friend;
		var Options = {
			update: 'friend'+friend,
			method: 'GET',
			evalScripts:true,
			asynchronous: true
		};
		
		new Ajax(url, Options).request();
		$('friend'+friend).setStyle('background','red');
		(function () { $('friend'+friend).remove(); }).delay(1000)
		
	}*/

}
function DeleteBan (user, $page) {

	if (confirm('Do you want delete this Banned?')) {
		var url = 'administration/ajax/delBan/id/'+user+'?page='+$page;
		var Options = {
			update: 'blockfriends',
			method: 'GET',
			evalScripts:true,
			asynchronous: true
		};
		new Ajax(url, Options).request();
	}
}
function check_values()
{
	from_unit_object	= $('from_unit');
	to_unit_object		= $('to_unit');
	value_object		= $('value');
	from_unit			= from_unit_object.options[from_unit_object.selectedIndex].value;
	to_unit				= to_unit_object.options[to_unit_object.selectedIndex].value;

// CHECKING values conditions
	if(from_unit_object.options[from_unit_object.selectedIndex].value == 'st')
	{
		value_1_object = $('value_1');
		if(value_1_object.value > 14)
		{
			alert('Incorrect value in pounds field. Please enter a value between 0-14.');
			value_1_object.focus();
			value_1_object.select();
			return false;
		}
	}
	if(isNaN(value_object.value))
	{
		alert('Please use numerical characters only (0-9).');
		value_object.focus();
		value_object.select();
		return false;
	}
	if((value_object.value).replace(/^\s*|\s*$/g,"") == '')
	{
		alert('Please enter the value you wish to convert.');
		value_object.focus();
		value_object.select();
		return false;
	}
	if(from_unit == 'st' && isNaN(value_1_object.value))
	{
		alert('Please use numerical characters only (0-9).');
		value_1_object.focus();
		value_1_object.select();
		return false;
	}
// CHECKING unit conditions
	if(from_unit == '' || to_unit == '')
	{
		alert('You have to select units to convert.');
		return false;
	}
	return true;
}
function update_fields(key, target_object)
{
	var value = ($('value').value).replace(/^\s*|\s*$/g,"");
	if(key=='st')
	{
		target_object.innerHTML = '<label style="width: 60px;">Value:</label><input type="text" class="small" name="value" id="value" value="' + value + '"> st. &nbsp;&nbsp; <input  class="small" type="text" name="value_1" id="value_1" value=""> lb.';
	}
	else if (key == 'ft')
	{
		target_object.innerHTML = '<label style="width: 60px;">Value:</label><input type="text" class="small" name="value" id="value" value="' + value + '"> ft. &nbsp;&nbsp; <input  class="small" onchange="this.value=parseFloat(this.value)%12" type="text" name="value_1" id="value_1" value=""> in.';
	}
	else
	{
		target_object.innerHTML = '<label style="width: 60px;">Value:</label><input type="text" class="small" name="value" id="value" value="' + value + '"> ' + key;
	}
}
function clean_result()
{
	var result_div = $('result');
	if (result_div && result_div.innerHTML)
		result_div.innerHTML = '&nbsp;';
}
function show(obj,div) { 
				if (!obj || !div)
					return;

				if ($(obj).hasClass('active')) 
					return;
					
				if (window.autocompleter)
					window.autocompleter.fade(window.autocompleter.update_hidden);
					
				$$('div.show').each(function (el) {
					el.removeClass('show');
					/*
					var ef = new Fx.Style(el, 'opacity', {
						duration: 500,
	  					transition: Fx.Transitions.quartInOut
					})
					ef.start(1,0);
					*/
					el.setStyle('display','none');
				});
				
				$$('a.active').each(function (el) {
					el.removeClass('active');
				});
				
				$(obj).addClass('active');
				$(div).addClass('show');
				$(div).setStyle('display','block');
				var ef = new Fx.Style(div, 'opacity', {
						duration: 500,
	  					transition: Fx.Transitions.quartInOut
				})
				ef.start(0,1);
			}
function scroll(div) {
	if (!div)
		return false;
	win = window;
	if (!win.$(div) && div.document.parentWindow)
		win = div.document.window;
	if (!win.$(div))
		return false;
  var winScroller = new Fx.Scroll(window);
  winScroller.toElement(div);
}

function autocomplete(input) {
	return false;
}

function pm_delete_sent($page)
{
	if (!$page)
		$page = 0;
	var list = $('list_pm_sent').getElements('input[type$=checkbox]');
	var out = new Array();
	for (i=0;i<list.length;i++)
	{
		if (list[i].value && list[i].checked)
		{
			out.push(parseInt(list[i].value));
		}
	}
	str_list = out.join(',');
	var url = '/administration/privatemessages/sent/ajax/1/?page='+$page+'&action=delete_sent&id='+str_list;
	var Options = {
		update: 'ajaxupdate_sent',
		method: 'GET',
		evalScripts:true,
		asynchronous: true
	};
	$('ajaxupdate_sent').innerHTML = 'Loading... <img src="/thm/images/qm_loading.gif">';
	new Ajax(url, Options).request();	
}

function posts_show_page($page)
{
	if (!$page)
		$page = 0;
		var url = '/administration/weblog/refreshposts?page='+$page;
	var Options = {
		update: 'myposts',
		method: 'GET',
		evalScripts:true,
		asynchronous: true
	};
	$('myposts').innerHTML = 'Loading messages... <img src="/thm/images/qm_loading.gif">';
	new Ajax(url, Options).request();
}

function posts_delete_selected($page)
{
	if ($page)
		$page = 0;
	var list = $('list_posts').getElements('input[type$=checkbox]');
	var out = new Array();
	for (i=0;i<list.length;i++)
	{
		if (list[i].value && !isNaN(parseInt(list[i].value)) && list[i].checked)
		{
			out.push(parseInt(list[i].value));
		}
	}
	if (!out.length)
	{
		alert('Please select at least one message.');
		return false;
	}
	str_list = out.join(',');
	var url = '/administration/ajax/deleteitem?id='+str_list;
	var Options = {
		update: 'myposts',
		method: 'GET',
		evalScripts:true,
		asynchronous: true
	};
	$('myposts').innerHTML = 'Deleting messages... <img src="/thm/images/qm_loading.gif">';
	new Ajax(url, Options).request();	
}

function pm_sent(page)
{
	var url = '/administration/privatemessages/sent/ajax/1/?page='+page;
	var Options = {
		update: 'ajaxupdate_sent',
		method: 'GET',
		evalScripts:true,
		asynchronous: true
	};
	$('ajaxupdate_sent').innerHTML = 'Loading... <img src="/thm/images/qm_loading.gif">';
	new Ajax(url, Options).request();	
}

function pm_action(action, id, page) {
	if (!page)
		page = 0;
		
	div = 'ajaxupdate';
	if (!$(div) && page.id)
	{
		div = page.id;
		page = 'administration';
	}
	if (action == 'reload' || div != 'ajaxupdate')
	{
		$(div).innerHTML = 'Loading... <img src="/thm/images/qm_loading.gif">';
		var url = '/administration/privatemessages/action/ajax/1/?page='+page;
		if (div == 'privatemessages_ajaxupdate')
		{
			// Deleting private messages from personalized home page
			url += '&id=' + id + '&action=1';
			page=0;
		}
		var Options = {
			update: div,
			method: 'GET',
			evalScripts:true,
			asynchronous: true,
			onComplete: function() {
				if ($('divAjax')) 
					$('divAjax').remove(); 
				if ($('privatemessages_ajaxupdate')) 
					scroll($('privatemessages_ajaxupdate')); 
			}
		};
		new Ajax(url, Options).request();
		return;
	}
	
	var listado = '';
	if (id == undefined) {
	var list = $('list_pm').getElements('input[type$=checkbox]');
	if (action == 1) {
		if (confirm('Are you sure you want to delete selected messages')) {
			var cof = 1;	
		}
	} else {
			var cof = 1;
	}
	if (cof == 1) {
	list.each( function (el) {
						if (el.value > 0) {
						 if (el.checked) {
							 listado = listado + el.value + ',';
							 switch (action) {
								 	case '3':
										
											($('td1'+el.value)).setStyle('font-weight','bold');
											($('td2'+el.value)).setStyle('font-weight','bold');
											($('td3'+el.value)).setStyle('font-weight','bold');										
											
									break;
									case '2':
										
											($('td1'+el.value)).setStyle('font-weight','normal');
											($('td2'+el.value)).setStyle('font-weight','normal');
											($('td3'+el.value)).setStyle('font-weight','normal');										
										
									break;
									
									case '1':
										
											$('item'+el.value).remove();
										
									break;
							 }
	                       }
						 }
						 //el.setProperty('checked',obj.getProperty('checked'));						 
						 });
	}
	} else {
		listado = id + ",";	
	}
	if (listado.length > 0) {
			var url = '/administration/privatemessages/action/ajax/1/?action='+action+'&id='+listado
			var Options = {
				update: 'ajaxupdate',
				method: 'GET',
				evalScripts:true,
				asynchronous: true,
				onComplete: function() { if ($('divAjax')) $('divAjax').remove(); }
			};
			$('inboxActions').value = '';
			$('ajaxupdate').innerHTML = 'Loading... <img src="/thm/images/qm_loading.gif">';
			new Ajax(url, Options).request();
	} else {
		alert('Please select at least one message.');	
	}
	
}
function selectALL(obj, listObj) {
	if (!listObj)
		listObj = $('list_pm');
	var list = listObj.getElements('input[type$=checkbox]');
	list.each( function (el) {
		el.setProperty('checked',obj.getProperty('checked'));					
		
		if(el.fireEvent)
		{
			el.fireEvent('onchange');
		}
		//On Gecko based browsers
		if(document.createEvent)
		{
			var evt = document.createEvent('HTMLEvents');
			if(evt.initEvent)
			{
				evt.initEvent('change', true, true);
			}
			if(el.dispatchEvent)
			{
				el.dispatchEvent(evt);
			}
		}
	});
}

function toggleVisibility(obj, wha)
{
	if(!obj) return;
	if (!wha) wha = 'block';
		
	if (obj.style.display == 'none')
		obj.style.display = wha;
	else
		obj.style.display = 'none';
}
function deleteWeight(w) {
	if (confirm('Do you want delete this Date/Weight?')) {
	var url = '/administration/weightlog/delete/ajax/1?id='+w;
			var Options = {
				method: 'GET',
				evalScripts:true,
				asynchronous: true,
				update: 'ajaxupdate'
			};
			new Ajax(url, Options).request();
	}
}

function graphWeight(type, user) {

	var src = "/images/index/graph/width/180/height/180/user_id/"+user+"/graph_type/"+type;
	/*
	var url = '/administration/weightlog/change/ajax/1?ago='+w;
			var Options = {
				update: 'ajaxupdate',
				method: 'GET',
				evalScripts:true,
				asynchronous: true
			};
			
	//new Ajax(url, Options).request();
	*/
	$('graph').setProperty('src',src);
	
}
function showGraph(obj) {
  if (obj.checked) {
	  var opt = $('option').value;
  } else {
 	  var opt = 0;
  }
  
  	var url = '/administration/weightlog/change/ajax/1?graph_type='+opt;
			var Options = {
				update: 'ajaxupdate',
				method: 'GET',
				evalScripts:true,
				asynchronous: true
			};
			
	new Ajax(url, Options).request();
	
}
function editBodyLog(date, Neck, Biceps, Forearm, Chest, Waist, Hips, Thigh, Calf) {
	$('date').value = date;
	$('Neck').value = Neck;
	$('Biceps').value = Biceps;
	$('Forearm').value = Forearm;
	$('Chest').value = Chest;
	$('Waist').value = Waist;
	$('Hips').value = Hips;
	$('Thigh').value = Thigh;
	$('Calf').value = Calf;
	scroll($('content'));
}
function deleteBodylog(w) {
	if (confirm('Do you want delete this Date/Body Log?')) {
	var url = '/administration/bodylog/delete/ajax/1?id='+w;
			var Options = {
				update: 'ajaxupdate',
				method: 'GET',
				evalScripts:true,
				asynchronous: true
			};
			
			new Ajax(url, Options).request();
			$('b'+w).remove();
	}
}
function convertWeight(conversion, value)
{
	switch (conversion)
	{
		case 'mg st':
		{
			$stones_pounds = [];
			$pounds = value/(1000 * 0.4535);
			$stones_pounds[0] = parseInt(Math.floor($pounds / 14));
			$stones_pounds[1] = parseFloat((Math.round($pounds % 14 * 100))/100);
			if (isNaN($stones_pounds[0])) $stones_pounds[0] = 0;
			if (isNaN($stones_pounds[1])) $stones_pounds[1] = 0;
			
			return $stones_pounds;
		}
		case 'mg lb':
		{
			$return = Math.round(((value/0.4535)/1000)*100) / 100;
			break;
		}
		case 'mg kg':
		{
			$return = Math.round(value) / 1000;
			break;			
		}
		case 'st mg':
		{
			// Value will be coming in two
			$stones = parseInt(value[0]); $pounds = parseFloat(value[1]) % 14;
			$return = parseInt(Math.round( Math.floor( ($stones*14 + $pounds) * 453.5 ) ));
			break;			
		}
		case 'kg mg':
		{
			value = value.replace(/[,]/g, '.');
			$return = parseInt(parseFloat(value) * 1000);
			break;
		}
		case 'lb mg':
		{
			$return = parseInt(Math.round((value*0.4535*1000)*100) / 100);
			break;			
		}
	}
	// alert('Return: ' + $return);
	return isNaN($return) ? 0 : $return;
}
function showWeightInput(type, value, realWeightInput)
{
	value = parseInt(value);
	switch(type)
	{
		case 'kg':
			inputValue = convertWeight('mg kg', value);
			return '<input class="small" type="text" name="weight_kg" value="'+ inputValue +'" onchange="$(\''+ realWeightInput.id +'\').value=convertWeight(\'kg mg\', this.value)"> kilograms';
		case 'st':
			inputValue = convertWeight('mg st', value);
			id = realWeightInput.id;
			return ('<div style="display:block"><input style="display:inline" class="small" id="'+ id +'_helper1" type="text" name="weight_st1" value="'+ inputValue[0] +'" onchange="$(\''+ realWeightInput.id +'\').value=convertWeight(\'st mg\', [this.value,$(\''+ realWeightInput.id + '_helper2\').value])"> stones <input style="display:inline" class="validate-stone-pounds small" id="'+ id +'_helper2" type="text" name="weight_st2" value="'+ inputValue[1] +'" onchange="$(\''+ realWeightInput.id +'\').value=convertWeight(\'st mg\', [$(\''+ realWeightInput.id + '_helper1\').value,this.value])"> pounds</div>');
		case 'lb':
			inputValue = convertWeight('mg lb', value);		
			return ('<input class="small" type="text" name="weight_lb" value="'+ inputValue +'" onchange="$(\''+ realWeightInput.id +'\').value=convertWeight(\'lb mg\', this.value)"> pounds');
	}
}

function showLengthInput(type, value, realLengthInput)
{
	value = parseInt(value);
	switch(type)
	{
		case 'in':
			inputValue = milimeters_to_inches(value);
			return '<input type="text" name="length_in" value="'+ inputValue +'" onchange="$(\''+ realWeightInput.id +'\').value=inches_to_milimeters(this.value)">';
		case 'cm':
			inputValue = Math.round(value / 1000);
			return '<input type="text" name="length_cm" value="'+ inputValue +'" onchange="$(\''+ realWeightInput.id +'\').value=inches_to_milimeters(this.value)">';
		case 'ft': // to implement
			return '';
	}
}

function html_entitity_decode(text)
{
    var div = document.createElement('div');
    var text = document.createTextNode(s);
    div.appendChild(text);
    return div.innerHTML;
}

function album_edit( action, value, name, album_id )
{
	var url = '/administration/photogallery/ajax/1/?edit='+escape(action)+'&value='+escape(value)+'&id='+escape(name);
	if (album_id)
	{
		url += '&album_id=' + album_id;
	}
	var Options = {
		update: 'ajaxupdate',
		method: 'GET',
		evalScripts:true,
		asynchronous: true
	};
	$('ajaxupdate').innerHTML = 'Loading... <img src="/thm/images/qm_loading.gif">';
	new Ajax(url, Options).request();
}

function album_show($id)
{
	if (window.albumCurrentId == $id)
	{
		show( $('tab_' + $id), 'ajaxupdate');
		return;
	}

	var url = '/administration/photogallery/ajax/1/?album_id='+$id;
	var Options = {
		update: 'ajaxupdate',
		method: 'GET',
		evalScripts:true,
		asynchronous: true
		//onComplete: function() { window.addEvent('domready', function() {  } ); }
	};
	show( $('tab_' + $id), 'ajaxupdate');
	$('ajaxupdate').innerHTML = 'Loading... <img src="/thm/images/qm_loading.gif">';
	new Ajax(url, Options).request();
	window.albumCurrentId = $id;
}

function disableTip(tip, obj)
{
	var url = '/administration/profile/disableTip?tip=' + tip;
	var Options = {
		update: 'ajaxupdate',
		method: 'GET',
		evalScripts:true,
		asynchronous: true,
		onComplete: function() { obj.innerHTML = 'Tip disabled.' }
		//onComplete: function() { window.addEvent('domready', function() {  } ); }
	};
	new Ajax(url, Option).request();
}

function showPage(url, container, $page, $method)
{
	if (!$page)
		$page = '0';
	if (!$method)
		$method = 'get';
		
	if (url.indexOf('?') > 0)
	{
		url = url.replace(/page=[^&]+/, '');
		url += "&page=" + $page;
	}
	else
		url += "?page=" + $page;
		
	var Options = {
		method: $method,
		update: container
	};
	new Ajax(url, Options).request();
	$(container).innerHTML = '<img src="/thm/images/loading.gif">';
}

function paypal_submit (form_id)
{
	var _form = $(form_id);
	( function() { _form.submit(); } ).delay(1);
}

function paypal_submit_gift (form_id)
{
	// Ajax check
	form = $(form_id);
    var Options = {
		method: 'GET',
		evalScripts:true,
		onloading: 1,
		asynchronous: true,
		onComplete: function(data) {
			if (data == '') {
				alert('No such user');
				return false;
			}
			form['custom'].value = data;
			if ($('gift_message').value)
				data += '_' + escape($('gift_message').value);
			form.submit();
		}
    };
    var url = "/administration/ajax/getPaypalCustom?friend="+escape($('displayed_name').value)+"&ptype="+form['item_number'].value;
    new Ajax(url, Options).request();
}

function fixtime()
{
	var trytz = document.cookie.match(/tzo=([^;]+)/);
	if (!(trytz) || !trytz[1] || parseInt(trytz[1]) != new Date().getTimezoneOffset())
	{
		document.cookie = 'tzo=' + parseInt(new Date().getTimezoneOffset());
	}
}

function sendConfirmationEmail(email)
{
	if (!email.prevVal)
		email.prevVal = email.value;

	if(email.value == email.prevVal)
	{
		window.emailChanged = false;
		return false;
	}
		 
	if (!confirm('Before this change is made you must confirm your new email address.  We will send an email to your new address. Click the link in the email message to complete the change.'))
	{
		window.emailChanged = false;
		email.value = email.prevVal;
		return false;
	}

	if (!email.value || !/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(email.value))
	{
		alert('E-mail you entered does not appear to be a valid address.');
		email.value = email.prevVal;
		window.emailChanged = false;
		return false;
	}
	
   var Options = {
		method: 'POST',
		evalScripts:false,
		asynchronous: true,
		data: {email: email.value},
		onComplete: function(data) {
			alert(data);
			if (data.indexOf('No changes made') > -1 || data.indexOf('Error: ') > -1)
				email.value = email.prevVal;
			window.emailChanged = false;
		}
    };
    var url = "/administration/ajax/emailConfirmation";
    new Ajax(url, Options).request();
    return false;
}

// Things to do when it's all loaded
window.addEvent('domready', function()  
{
	fixtime();
});
//-vertical
window.addEvent('domready', function() {
	if (!$('searchoptions'))
		return;
	var mySlide = new Fx.Slide('searchoptions', {mode: 'horizontal'});
	$('toggle').addEvent('click', function(e){
    if ($('toggle').hasClass('active'))
	{
	$('toggle').setHTML('Search Options<img src="thm/images/profile_browse1.gif" />');
	$('toggle').removeClass('active');
	}
	else
	{	$('toggle').setHTML('Hide Options<img src="thm/images/profile_browse2.gif" />');
		$('toggle').addClass('active');
	}
	e = new Event(e);
	mySlide.toggle();
	e.stop();
	});
	mySlide.hide();
	$('searchoptions').removeClass('hidden');
});

