$(document).ready( function(){	

	// Toggler
	$(".toggle").next().hide();
	$(".toggle").click(function(){
		if( $(this).next().is(":hidden") ) {
			$(this).next().slideDown('fast');
		} else {
			$(this).next().slideUp('fast');
		}
	});
	$(".toggleall").click(function(){
		if( $(".toggle").next().is(":hidden") ) {
			$(".toggle").next().slideDown('fast');
		} else {
			$(".toggle").next().slideUp('fast');
		}
	});
	
	// Smooth Scroller
	$('a[href*=#]').click(function() {
		if( location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') && location.hostname == this.hostname )
		{
			var $target = $(this.hash);
			$target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');

			if ($target.length) 
			{
				var targetOffset = $target.offset().top;
				$('html,body').animate({scrollTop: targetOffset}, 1000);
		  		return false;
			}
		}
	});
	
	// Email Spam Gaurd
	$('a.nospam').click(function(){
		var href = $(this).attr('href');
		var h_array = href.split('+');

		var new_href;
		new_href = h_array[0] + '@' + rot13(h_array[1]) + h_array[2];
		$(this).attr('href', new_href);
	});
	
	// // Over Labels
	$("#admin label").addClass("compact");
	if( $("#admin #username").val() != "" )
	{
		$("#admin #username").prev().hide();
	}
	if( $("#admin #password").val() != "" )
	{
		$("#admin #password").prev().hide();
	}	
	$("#admin #username").focus( function() { 
		$(this).prev().hide(); 
	});
	$("#admin #username").blur( function() {
		if( $(this).val() == "" )
		{
			$(this).prev().fadeIn();
		}
	});
	$("#admin #password").focus( function() { 
		$(this).prev().hide(); 
	});
	$("#admin #password").blur( function() {
		if( $(this).val() == "" )
		{
			$(this).prev().fadeIn();
		}
	});
	
	// // Date Picker
	$('#inception_date input').datepicker({ 
	    minDate: new Date(2005, 1 - 1, 1),  
	    maxDate: new Date(2015, 12 - 1, 31),  
		firstDay: 1,
		onSelect: function(date) { if(date) $('#inception_date a').text(date); else $('#inception_date a').text('Set Date'); }, 
		showAnim: 'fadeIn', 
        speed: 'fast'
	});
	$('#inception_date a').click(function(){
		$('#inception_date input').datepicker('show', {
			showAnim: 'fadeIn', 
	        speed: 'fast'
		});
	});
	$('#as_of input').datepicker({ 
	    minDate: new Date(2005, 1 - 1, 1),  
	    maxDate: new Date(2015, 12 - 1, 31),  
		firstDay: 1,
		onSelect: function(date) { if(date) $('#as_of a').text(date); else $('#as_of a').text('Set Date'); }, 
		showAnim: 'fadeIn', 
        speed: 'fast'
	});
	$('#as_of a').click(function(){
		$('#as_of input').datepicker('show', {
			showAnim: 'fadeIn', 
	        speed: 'fast'
		});
	});


	// Legal Agreement
	$('.noscript').hide();
	$('.agree input').click(function(){
		var url = $(this).next().next().attr('href');
		if( $(this).is(':checked') == true )
		{
			window.open(url, "PDF", "width=700,height=550,status=no,toolbar=no,menubar= no");
		}
	});
});

var clicked_reset_before;

function ResetPassword()
{	
	$.ajax({
 		type: "GET",
 		dataType: "html",
		url: "/ejf2/includes/scripts/ajax.php",
	  	data: "action=resetpassword",
	  	success: SetNewPassword
	});
}
function SetNewPassword(data)
{
	if( clicked_reset_before == true )
	{
		$('#new_password').attr('value', data);
	} else {
		$('#reset_password').before('<input type="text" name="new_password" id="new_password" value="'+data+'" /> ');
		clicked_reset_before = true;
	}	
}

function rot( t, u, v ) {  return String.fromCharCode( ( ( t - u + v ) % ( v * 2 ) ) + u ); }  function rot13( s ) {  var b = [], c, i = s.length,   a = 'a'.charCodeAt(), z = a + 26,   A = 'A'.charCodeAt(), Z = A + 26;  while(i--) {   c = s.charCodeAt( i );   if( c>=a && c<z ) { b[i] = rot( c, a, 13 ); }   else if( c>=A && c<Z ) { b[i] = rot( c, A, 13 ); }   else { b[i] = s.charAt( i ); }  }  return b.join( '' ); }  function rot5( s ) {  var b = [], c, i = s.length,   a = '0'.charCodeAt(), z = a + 10;  while(i--) {   c = s.charCodeAt( i );   if( c>=a && c<z ) { b[i] = rot( c, a, 5 ); }   else { b[i] = s.charAt( i ); }  }  return b.join( '' ); }  function rot135( s ) {  return rot13( rot5( s ) ); }