function loadstart(){
	$('display').setOpacity(0.5);
	$('loading').show();}

function loadend(){
	$('display').setOpacity(1.0);
	$('loading').hide();}
	
function errorbox(a) {
	Element.toggle('errordiv');
	$('display').setOpacity(0.5);
	document.getElementById('errorcontents').innerHTML=a;
	return false;	
}

function successbox(a) {
	Element.toggle('successdiv');
	$('display').setOpacity(0.5);
	document.getElementById('successcontents').innerHTML=a;
	return false;	
}

function passwordbox() {
	$('password').show();
	$('infoedit').setOpacity(0.5);
	return false;	
}

function edittab(a){
	b=a+"edit";
	if ($(b).style.display=='none') {
		Element.hide('favoritesedit');
		Element.hide('bccedit');
		Element.hide('infoedit');
		Element.hide('reviews');
		Element.hide('coolfacts');
		Element.toggle(b);
	 	$('favorites').removeClassName('editcurrent');
	 	$('bcc').removeClassName('editcurrent');
		if (a!='info'){
			$(a).addClassName('editcurrent');}
	}
	else {
		Element.hide('favoritesedit');
		Element.hide('bccedit');
		Element.hide('infoedit');
		$('favorites').removeClassName('editcurrent');
	 	$('bcc').removeClassName('editcurrent');
		Element.show('reviews');
		Element.show('coolfacts');
	}
}

function canceledit(){
	Element.hide('favoritesedit');
	Element.hide('bccedit');
	Element.hide('infoedit');
	$('favorites').removeClassName('editcurrent');
 	$('bcc').removeClassName('editcurrent');
	Element.show('reviews');
	Element.show('coolfacts');
}

function coolfacts(o,p,u) {
	new Ajax.Updater('coolfactscontent','/profile/coolfacts.php', {method: 'get', parameters: {o:o,p:p,u:u},
		onLoading: function() { $('coolfactscontent').setOpacity(0.5); $('loading').show(); },
		onFailure: function() {errorbox('This page has failed to load.');},
	    onComplete: function() { $('coolfactscontent').setOpacity(1.0); $('loading').hide(); }
    })
}

function reviews(o,p,u) {
	new Ajax.Updater('reviewscontent','/profile/reviews.php', {method: 'get', parameters: {o:o,p:p,u:u},
		onLoading: function() { $('reviewscontent').setOpacity(0.5); $('loading').show(); },
		onFailure: function() {errorbox('This page has failed to load.');},
	    onComplete: function() { $('reviewscontent').setOpacity(1.0); $('loading').hide(); }
    })
}

function expanddest(){
	if ($('destexpanded').style.display=='none') {
		$('destmorelink').hide();
		$('destexpanded').show();
		new Effect.Parallel([
			Effect.BlindUp('myfavoriterestaurants'),
			new Effect.Opacity('myfavoriterestaurants', {from: 1, to: 0})
		]);
	}
	else {
		$('destexpanded').hide();
		$('destmorelink').show();
		new Effect.Parallel([
			Effect.BlindDown('myfavoriterestaurants'),
			new Effect.Opacity('myfavoriterestaurants', {from: 0, to: 1})
		]);
	}
}

function expandrest(){
	if ($('restexpanded').style.display=='none') {
		$('restmorelink').hide();
		$('restexpanded').show();
		Effect.DropOut('myfavoritedestinations');
	}
	else {
		$('restexpanded').hide();
		$('restmorelink').show();
		Effect.Appear('myfavoritedestinations');
	}
}

function check_password(form) {
		var form = $(form);
		var selectn = form['new_password'];
			var n = $(selectn).getValue();
		var selectc = form['confirm_password'];
			var c = $(selectc).getValue();
		
		var pass = n
		if(pass != c) {
			errorbox('Your new password and confirmation do not match. Please re-enter your confirmed password.');
			return false;
		}
		if(pass.length < 6) {
			errorbox('Your password must be at least 6 characters');
			return false;
		}

		var charsets = new Array();
		charsets[0] = 'abcdefghijklmnopqrstuvwxyz';
		charsets[1] = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
		charsets[2] = '1234567890';
		charsets[3] = '';
		for(var i = 32; i < 128; i++)
		{
			var ch = unescape('%'+(i<16 ? '0' : '')+i.toString(16));
			for(var j = 0; j < 3; j++)
				if(charsets[j].indexOf(ch) >= 0)
					ch = '';
			charsets[3] += ch;
		}

		var sets_used = 0;
		
		for(var i = 0; i < pass.length; i++)
		{
			for(var j = 0; j < charsets.length; j++)
			{
				if(charsets[j].indexOf(pass.charAt(i)) >= 0)
				{
					sets_used++;
					charsets[j] = '';
					break;
				}
			}
		}
		
		if(sets_used < 2) {
			errorbox('Password must contain two of the following:<br/>-lowercase letters<br/>-uppercase letters<br/>-numbers<br/>-symbols');
			return false;
		}
		return true;
}

function changepassword(u) {
	var a = 'changepass';
	var form = $('passwordchange');
	var selecto = form['old_password'];
		var o = $(selecto).getValue();
	var selectn = form['new_password'];
		var n = $(selectn).getValue();
	var selectc = form['confirm_password'];
		var c = $(selectc).getValue();

	if (check_password('passwordchange')) {
		new Ajax.Updater('password','/profile/password.php', {method: 'get', parameters: {a:a,o:o,n:n,u:u},
			onLoading: function() {loadstart();},
			onFailure: function() {errorbox('This page has failed to load.');},
		    onComplete: function() { loadend(); }
	    })
	}
}

function closepasswordbox() {
	var a ='refresh';
	$('password').hide();
	$('infoedit').setOpacity(1.0);
	new Ajax.Updater('password','/profile/password.php', {method: 'get', parameters: {a:a}});
}

function refreshfavs(u) {
	new Ajax.Updater('favorites','/profile/favoritestab.php', {method: 'get', parameters: {u:u},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
	    onComplete: function() { loadend(); }
    })
}

function addfavs(t,u){
	var a='add';
	var whichform=t+'add';
	var whichselect='favorite_'+t+'s';
	var whichnotype='no'+t;
	var form = $(whichform);
	var select = form[whichselect];
	var i = $(select).getValue();
	new Ajax.Updater(t,'/profile/favorites.php', {
		method: 'get',
		parameters: {a:a,t:t,i:i,u:u},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
		onComplete: function() {loadend();$(whichnotype).hide();},
		insertion: Insertion.Bottom
	})
	refreshfavs(u);
}

function removefavs(t,i,u){
	var a='remove';
	var whichli=t+i;
	new Ajax.Request('/profile/favorites.php', {
		method: 'get',
		parameters: {a:a,t:t,i:i,u:u},
		onLoading: function() {loadstart();},
		onFailure: function() {errorbox('This page has failed to load.');},
		onComplete: function() {
			loadend();
			$(whichli).remove();}
	})
	refreshfavs(u);
}