var fn = {  }

fn.ext = '/update/';
fn.media = 'media/';



fn.check_form = function(elem)
{
	var elem_id = $(elem).attr('id');
	
	var errors = "";
	
	$("form[id='"+$(elem).attr('id')+"'] input[accept='true'], form[id='"+$(elem).attr('id')+"'] textarea[accept='true']").each(function(){
		$(this).removeClass('error');
		var mask = $(this).attr('mask');
		
		switch(mask)
		{
		case "email":
			if(!fn.is_email($(this).val())) 
			{
				errors += $(this).attr('alt')+"\n";
				$(this).addClass('error');
				$(this).focus(function(){$(this).removeClass('error');});
			}	
		break; 
		default:
			if($(this).val().length < 1) 
			{
				errors += $(this).attr('alt')+"\n";
				$(this).addClass('error');
				$(this).focus(function(){$(this).removeClass('error');});
			}	
		break;
		}
		
	});
	
	
	if(errors.length < 1) return true;
	alert("Please amend the following before continuing:\n"+errors);
	
	return false;
}

fn.is_email = function(email)
{
	//var email = email.value;
	//var email = document.forms[target].elements[field].value;
	var atSym = email.indexOf('@');
	var dot = email.lastIndexOf('.');
	var space = email.indexOf(' ');
	var len = email.length;
	if (atSym < 1 || dot < atSym || len - dot <= 2 || space != -1) {
		return false;
	}
	else { 
		return true; 
	}
}

fn.encode = function(str) {
		var result = "";
		for (i = 0; i < str.length; i++) {
			if (str.charAt(i) == " ") {
				result += "+";
			}
			else {
				result += str.charAt(i);
			}
		}
		return escape(result);
}

fn.decode = function(str) {
		var result = str.replace(/\+/g, " ");
		return unescape(result);
}

fn.mem = function(mem)
{
	$('div#mem').fadeIn();
	$('div#mem').html(mem);
	//fn.flash('div#mem');
	setTimeout("$('div#mem').fadeOut()", 3000);	
}

fn.flash = function(elem)
{
	$(elem).fadeIn(100).fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);	
}

fn.confirm = function(conf, url)
{
	if(confirm(conf)) location.href = url;
}

fn.random = function()
{
	var random_no = Math.floor(Math.random()*1000000);
	return random_no;
}


$(document).ready(function(){
	
});


