//*****Bind print command to print buttons**************************************************//
function printCommand() {
	$('a.print-btn').click(function() {
    window.print()
    return false;
  });	
}
//*****Bind share command to share buttons**************************************************//
function emailCommand() {
	$('a.email-btn').click(function() {
		if($('#tell-friend').is(':hidden')){
			$('#tell-friend').toggle('slow');
		}else{
			$('#tell-friend').fadeOut('slow');
		}
    return false;
  });	
}
//*****Replace align attribute with class**************************************************//
function replaceAlign() {
  if (!document.getElementsByTagName('img')) return false;
  $('img[align="left"]').addClass('left').removeAttr('align');
  $('img[align="right"]').addClass('right').removeAttr('align');
  $('img[align="middle"]').addClass('middle').removeAttr('align');
}
//*****Replace target attribute with class**************************************************//
function replaceTarget() {
  if (!document.getElementsByTagName('a')) return false;
  $('a[target]').addClass('newwindow').removeAttr('target');
}
//*****The following function make it possible to have web standard popups**************************************************//
function strictNewWindow() {
  if (!document.getElementsByTagName('a')) return false;
  $('a.newwindow').click(function() {
    window.open($(this).attr('href'));
    return false;
  });
}
//*****jQuery clear value from july 21st 2009 comment on http://www.joesak.com/2008/11/19/a-jquery-function-to-auto-fill-input-fields-and-clear-them-on-click*****//
function clearDefaultValue() {
  $(':input').focus(function() {
    if($(this).val() == $(this).attr('title')) {
      $(this).val('');
    }
  }).blur(function() {
    if($(this).val() == '') {
      $(this).val($(this).attr('title'));
    }
  });
  $('#btnNext').click(function() {
	  $(':input').each(function (i) {
      if($(this).val() == $(this).attr('title')) {
        $(this).val('');
      }
    });
  });
}
function populateDefaultValues(){
  $(':input').each(function(){
    if($(this).val() == ''){
      $(this).val($(this).attr('title'));      
    }
  });
}
//******nav states**********//
function navStates() {
	$("ul#headerNav > li").mouseover(function(){
		$(this).addClass("iefix");																			
	});
	$("ul#headerNav > li").mouseout(function(){
		$("ul#headerNav li").removeClass("iefix");																			
	});
}
//******risk analysis**********//
function riskAnalysis() {
	$('#risk-analysis-form #lbl-submit input').click(function() {
		var risk = $.fn.autoNumeric.Strip('risk');
		
		var realized = risk - (risk * (.06));
		var realized = realized * 1000000;
		//var realized = realized.toFixed(2);
		$('#realized-hidden').val(realized);
		
		var potential = risk * (.06);
		var potential = potential * 1000000;
		//var potential = potential.toFixed(2);
		$('#potential-hidden').val(potential);
		
		var formatted = $.fn.autoNumeric.Format('realized-hidden', realized);
    $('#realized span').html(formatted);
		var formatted2 = $.fn.autoNumeric.Format('potential-hidden', potential);
		$('#potential span').html(formatted2);
		
		return false;
  });
	$("#risk-analysis-form").bind("submit", function(event) {
		event.preventDefault();
		$('#risk-analysis-form #lbl-submit input').trigger('click');
	});
	function checkForEnter(event) {
		if (event.keyCode == 13) {
			if(('#risk-analysis-form #lbl-submit input[type=text]:focus').length){
				$('#risk-analysis-form #lbl-submit input').click();
			}
		}
	}
	if ($.browser.mozilla) {
		$('#lbl-risk input').keypress(checkForEnter);
	} else {
		$('#lbl-risk input').keydown(checkForEnter);
	}
	
}
//*****Load all functions**************************************************//
$(document).ready(function(){
	printCommand();
	riskAnalysis();
	emailCommand();
  replaceAlign();
  replaceTarget();
  strictNewWindow();
  clearDefaultValue();
	navStates();
});
