function popup(file, title, width, height) {
	window.open(file, title, 'toolbar=no,location=no,titlebar=no,scrollbars=no,resizable=yes,status=no,menubar=no,width=' + width + ',height=' + height);
}

var $j = jQuery.noConflict();

function stripslashes(str) { 
	return (str+'').replace(/\0/g, '0').replace(/\\([\\'"])/g, '$1');
}

function getFlexApp(appName) {
	if (navigator.appName.indexOf("Microsoft") !=-1) {
		return window[appName];
	} else {
		return document[appName];
	}
}


function clearfield(layer_ref) {
	if (!layer_ref._haschanged) {
		layer_ref.value= ''; 
		layer_ref._haschanged=true;
	}
}

function onReturn(e, callback, parameter) { //e is event object passed from function invocation	
	var characterCode;// literal character code will be stored in this variable

	if (e && e.which) //if which property of event object is supported (NN4)
		characterCode = e.which;
	else //character code is contained in IE's keyCode property
		characterCode = event.keyCode;
		
	if (characterCode == 13) { 
		callback(parameter);
		return false;
	} 
	return true;
}


function register()
{
	if ($j('#register').val()=='' || $j('#register').val()=='Name' ||
		$j('#passRegister').val()=='' || $j('#passRegister').val()=='Password' ||
		$j('#passVerify').val()=='' || $j('#passVerify').val()=='Verify Password' ||
		$j('#email').val()=='' || $j('#email').val()=='Email')  
	{
		alert('Fill out your user name, password and email to register');
		return;
	}
	if ($j('#passRegister').val()!=$j('#passVerify').val()) 
	{
		alert('Your password doesn\'t match your password verification.  Make sure they are the same password.');
		return;
	}
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	if (!filter.test($j('#email').val()))
	{
		alert('Please enter a valid email address.  If you lose your password, it will be sent to this address.');
		return;
	}
	$j('#register_button').attr('class','button_disabled');
	
	$j.ajax({url: "authenticate.php", 
		dataType: "text",
		type: "POST",
		data: {action: 'register', login: $j('#register').val(), password: $j('#passRegister').val(), email: $j('#email').val(), invitecode: $j('#invitecode').length > 0 ? $j('#invitecode').val() : ''},
		error: function() {
			alert("Registration failed");
			$j('#register_button').attr('class','button');
		},
		success: function(response) {
			if (response=='OK') {
				$j('#register_button').attr('class','button');
				$j('#register_panel').hide();
				$j('#login_text').hide();
				$j('#logout_text').show();
				$j('#profile').show();
				$j(document).trigger("login", [$j('#register').val()]);
			} else {
				alert('Registration failed\r\n' + response);
				$j('#register_button').attr('class','button');
			}			 
		}		
	});
}


function login()
{
	if ($j('#login').val()=='' || $j('#login').val()=='Name' ||
		$j('#pass').val()=='' || $j('#pass').val()=='Password') 
	{
		alert('Fill out your user name and password to login');
		return;
	}
	
	$j('#login_button').attr('class','button_disabled');
	
	$j.ajax({url: "authenticate.php", 
		dataType: "text",
		type: "POST",
		data: {action: 'login', login: $j('#login').val(), password: $j('#pass').val()},
		error: function() {
			alert("Login failed");
			$j('#login_button').attr('class', 'button');
		},
		success: function(response) {
			if (response=='OK') {
				$j('#login_button').attr('class', 'button');
				$j('#login_panel').hide();
				$j('#login_text').hide();
				$j('#logout_text').show();
				$j('#profile').show();
				$j(document).trigger("login", [$j('#login').val()]);
			} else {
				alert('Login failed\r\n' + response);
				$j('#login_button').attr('class', 'button');
			}			 
		}		
	});
}

function logout()
{
	$j('#logout_text').attr({opacity: 0.5, cursor: 'default'});
	
	$j.ajax({url: "authenticate.php", 
			dataType: "text",
			type: "POST",
			data: {action: 'logout'},
			error: function() {
				alert("Logout failed");
				$j('#logout_text').attr({opacity: 1, cursor: 'pointer'});
			},
			success: function(response) { 
				if (response=='OK') {
					$j('#logout_text').attr({opacity: 1, cursor: 'pointer'});
					$j('#logout_text').hide();
					$j('#login_text').show();
					$j('#profile').hide();
					$j(document).trigger("logout");
				} else {
					alert('Logout failed\r\n' + response);
					$j('#logout_text').attr({opacity: 1, cursor: 'pointer'});
				}				 
			}			
	});	
}

function forgotpassword()
{
	if ($j('#login').val()=='' || $j('#login').val()=='Name') 
	{
		alert('Fill out your user name to get your password');
		return;
	}
	//$j('#logout_text').attr({opacity: 0.5, cursor: 'default'});
	
	$j.ajax({url: "authenticate.php", 
			dataType: "text",
			type: "POST",
			data: {action: 'forgot', name: $j('#login').val()},
			error: function() {
				alert("Forgot password failed");
				//$j('#logout_text').attr({opacity: 1, cursor: 'pointer'});
			},
			success: function(response) { 
				if (response=='OK') {
					alert('Your password has been emailed to the email on your account.');
					//$j('#logout_text').attr({opacity: 1, cursor: 'pointer'});
					//$j('#logout_text').hide();
					//$j('#login_text').show();
					//$j('#profile').hide();
					//$j(document).trigger("logout");
				} else {
					alert('Forgot password failed\r\n' + response);
					//$j('#logout_text').attr({opacity: 1, cursor: 'pointer'});
				}				 
			}			
	});	
}
