$(document).ready(function(){
	if($('#registration_table').length > 0){
		$('input[name=password]').keyup(function(){check_password_strength('password')});
	}
	if($('#personal_info_table').length > 0){
		$('input[name=new_password]').keyup(function(){check_password_strength('new_password')});
	}
});

function check_confirmation(target_url, text){
	if(confirm(text)){
		window.location = target_url;
	}
}

function edit_file(file_id){
	$.post("/files/show_edit_form/", {"file_id":file_id}, function(data){
		$('#manage_file_'+file_id).html(data);
	})
}

function submit_edit_file(file_id){
	file_title = $('#title_'+file_id).val();
	if(file_title.match(/^\S+.*$/)){
		$.post("/files/edit_file_title/", {"file_id":file_id, "file_title":file_title}, function(data){
			$('#manage_file_'+file_id).html(data);
		})
	}
}

function hide_edit_file(file_id){
	$.post("/files/hide_edit_form/", {"file_id":file_id}, function(data){
		$('#manage_file_'+file_id).html(data);
	})	
}

function admin_edit_file(user_id, file_id){
	$.post("/admin/show_edit_form/", {"user_id":user_id, "file_id":file_id}, function(data){
		$('#manage_file_'+file_id).html(data);
	})
}

function admin_submit_edit_file(user_id, file_id){
	file_title = $('#title_'+file_id).val();
	if(file_title.match(/^\S+.*$/)){
		$.post("/admin/edit_file_title/", {"user_id":user_id, "file_id":file_id, "file_title":file_title}, function(data){
			$('#manage_file_'+file_id).html(data);
		})
	}
}

function admin_hide_edit_file(user_id, file_id){
	$.post("/admin/hide_edit_form/", {"user_id":user_id, "file_id":file_id}, function(data){
		$('#manage_file_'+file_id).html(data);
	})	
}


function toggle_file_drawer(file_type_id){
	$('#files_'+file_type_id).slideToggle('slow');
}

function check_password_strength(field_name){
	test_password($('input[name='+field_name+']').val(), field_name);
}

function test_password(passwd, field_name)
{
	var intScore   = 0
	var strVerdict = "weak"
	var strLog     = ""
	
	// PASSWORD LENGTH
	if (passwd.length<2)                         // length 2 or less
	{
		intScore = (intScore+3)
		strLog   = strLog + "3 points for length (" + passwd.length + ")\n"
	}
	else if (passwd.length>2 && passwd.length<8) // length between 3 and 7
	{
		intScore = (intScore+6)
		strLog   = strLog + "6 points for length (" + passwd.length + ")\n"
	}
	else if (passwd.length>7 && passwd.length<12)// length between 8 and 12
	{
		intScore = (intScore+12)
		strLog   = strLog + "12 points for length (" + passwd.length + ")\n"
	}
	else if (passwd.length>12)                    // length 13 or more
	{
		intScore = (intScore+18)
		strLog   = strLog + "18 point for length (" + passwd.length + ")\n"
	}
	
	
	// LETTERS (Not exactly implemented as dictacted above because of my limited understanding of Regex)
	if (passwd.match(/[a-z]/))                              // [verified] at least one lower case letter
	{
		intScore = (intScore+1)
		strLog   = strLog + "1 point for at least one lower case char\n"
	}
	
	if (passwd.match(/[A-Z]/))                              // [verified] at least one upper case letter
	{
		intScore = (intScore+5)
		strLog   = strLog + "5 points for at least one upper case char\n"
	}
	
	// NUMBERS
	if (passwd.match(/\d+/))                                 // [verified] at least one number
	{
		intScore = (intScore+5)
		strLog   = strLog + "5 points for at least one number\n"
	}
	
	if (passwd.match(/(.*[0-9].*[0-9].*[0-9])/))             // [verified] at least three numbers
	{
		intScore = (intScore+5)
		strLog   = strLog + "5 points for at least three numbers\n"
	}
	
	
	// SPECIAL CHAR
	if (passwd.match(/.[!,@,#,$,%,^,&,*,?,_,~]/))            // [verified] at least one special character
	{
		intScore = (intScore+5)
		strLog   = strLog + "5 points for at least one special char\n"
	}
	
								 // [verified] at least two special characters
	if (passwd.match(/(.*[!,@,#,$,%,^,&,*,?,_,~].*[!,@,#,$,%,^,&,*,?,_,~])/))
	{
		intScore = (intScore+5)
		strLog   = strLog + "5 points for at least two special chars\n"
	}

	
	// COMBOS
	if (passwd.match(/([a-z].*[A-Z])|([A-Z].*[a-z])/))        // [verified] both upper and lower case
	{
		intScore = (intScore+2)
		strLog   = strLog + "2 combo points for upper and lower letters\n"
	}

	if (passwd.match(/([a-zA-Z])/) && passwd.match(/([0-9])/)) // [verified] both letters and numbers
	{
		intScore = (intScore+2)
		strLog   = strLog + "2 combo points for letters and numbers\n"
	}

								// [verified] letters, numbers, and special characters
	if (passwd.match(/([a-zA-Z0-9].*[!,@,#,$,%,^,&,*,?,_,~])|([!,@,#,$,%,^,&,*,?,_,~].*[a-zA-Z0-9])/))
	{
		intScore = (intScore+2)
		strLog   = strLog + "2 combo points for letters, numbers and special chars\n"
	}


	if(intScore < 6)
	{
	   strVerdict = "Very weak";
	   color = "ff2f00";
	}
	else if (intScore > 5 && intScore < 15)
	{
	   strVerdict = "Weak";
	   color = "ff2f00";
	}
	else if (intScore > 14 && intScore < 25)
	{
	   strVerdict = "Mediocre";
	   color = "fcff52";
	}
	else if (intScore > 24 && intScore < 35)
	{
	   strVerdict = "Strong";
	   color = "5dff72";
	}
	else
	{
	   strVerdict = "Stronger";
	   color = "5dff72";
	}
	
	$('div#password_strength').remove();
	offset = $('input[name='+field_name+']').offset();
	$('body').prepend('<div id="password_strength">'+strVerdict+' password</div>');
	$('#password_strength').css('left', (offset.left+230)).css('top', (offset.top-3)).css('background-color', "#"+color);
	
}