$(document).ready(function() {
	$('#q1').focus();	
	//showCallToActions();
	//$('#container').onclick = hideAllTips();
});





/* UI ---------------------------------------- */		

function showCallToActions(){
	if ($('#calcToAFriend').is(":hidden")) {
        $("#calcToAFriend").fadeIn("slow");
    } 
    if ($('#rmi-panel').is(":hidden")) {
        $("#rmi-panel").fadeIn("slow");
    } 
    $("#calcToAFriend").css('visibility', 'visible');
    $("#rmi-panel").css('visibility', 'visible');
    $("#other-calculators").css('border-style', 'none solid solid solid');        
}		
				
function hideLoading() {
	$('#calculator-button').removeAttr('disabled');		
	$('#calculator-button').val(calculateLabel);
	showTotals();
	$('.loader').css('visibility', 'hidden');
}

function showLoading() {
	$('#calculator-button').attr('disabled', 'disabled');
	$('#calculator-button').val(workingLabel);
	
	hideTotals();
	$('.loader').css('visibility', 'visible');		
}

function showValidation() {
	if ($('#validation').is(":hidden")) {
        $("#validation").fadeIn("slow");
    } 
}
	
function hideValidation() {
	if (!$('#validation').is(":hidden")) {
         $("#validation").fadeOut("slow");
    } 
}

function hideTotals() {
	for (var i=0;i<TOTALS.length;i++){
    	$('#'+TOTALS[i]).css('visibility', 'hidden');		
	}
}

function showTotals() {
	for (var i=0;i<TOTALS.length;i++){
    	$('#'+TOTALS[i]).css('visibility', 'visible');		
	}
}

function highlight(question) {
	$('#q'+question+'-question').css('background', 'lightyellow');
	
	if ($('#tip'+question).size() != 0)  {
		showTip(question);			
	} else {
		hideAllTips();	
	}	
}

function lowLight(question) {
	$('#q'+question+'-question').css('background', '#FFF');		
}

function focusOn(question, displayTip){
	$('#q'+question).focus();
	
	if (displayTip == true) {
		showTip(question);		
	} else {
		hideAllTips();	
	}
}

// ------------------------------------------------ TIPS

function showTip(number) {
	// show if not already showing it
	if ($('#tip'+number).css('display') != 'block') {
		hideAllTips();
		$('#tip'+number).fadeIn("slow");
	}		
}

function fadeTip(number){
	$('#tip'+number).fadeOut("slow");
}

function hideAllTips(){
	$('a.info span').css("display", "none");
}

function fadeAllTips(){
	if ($('a.info span').css('display') != 'none') {
		$('a.info span').fadeOut("slow");
	}
}

/* Ajax ---------------------------------------- */		
		
function calculatorSubmit(formId) {		
	if (!validate()) {			
		showValidation();
	} else {
		hideValidation();
		fadeAllTips();
		showLoading();		
		updateTotals();
		$(formId).ajaxSubmit({
			success: requestSuccess,
			timeout: requestTimeout,
			failure: requestFailure
		});
	}
	return false;
}

	
function requestSuccess(content) {		
	hideLoading();	
	showCallToActions();					
	return;
}

function requestFailure(content) {
	hideLoading();
	alert('Failed');
	return;
}

function requestTimeout() {
	hideLoading();
	alert('Timeout');
	return;
}

/* Validation ---------------------------------------- */

function validate(){	
	var isValid = true;
	for (var i=0; i<ANSWERS.length; i++){
    	if ($('#'+ANSWERS[i]).val() == '') {
			isValid = false;
		}
	}
	return isValid;		
}

/* Utility ---------------------------------------- */		
		
function isNavigationKey(e) {
	var keycode
	var numcheck
	var isNavKey = false;
	
	if (window.event) keycode = e.keyCode;
	else if (e) keycode = e.which;
		
	/* Allowed keycodes			
		 9: tab
		16: shift
		17: ctrl
		35: end
		36: home
		37: left
		38: up
		39: right
		40: down	
	*/				
		
			
		if ((keycode >= 35 && keycode <= 40) ||   		
			 keycode == 9 || 
			 keycode == 16 || 
			 keycode == 17) {
				 isNavKey = true;					
		}				
	
	return isNavKey
}		

function chkKey(evt) {
	var mykey, alt, ctrl, shift, str;
	
	if (window.Event) {
		mykey = evt.which;
		alt = (evt.modifiers & Event.ALT_MASK) ? true : false;
		ctrl = (evt.modifiers & Event.CONTROL_MASK) ? true : false;
		shift = (evt.modifiers & Event.SHIFT_MASK) ? true : false;
	} else {
		mykey = event.keyCode;
		alt = event.altKey;
		ctrl = event.ctrlKey;
		shift = event.shiftKey;
	}
	
	if(shift && mykey == 13) {
		alert("Shift-Enter was pressed");
	}
	return true;
}
		
		
function validateNumber(e) {	
	var keycode
	var keychar
	var numcheck
		
	if (window.event) keycode = e.keyCode;
	else if (e) keycode = e.which;
	keychar = String.fromCharCode(keycode)
	numcheck = /\d/
		
	var isOk = false
/*
	var mykey, alt, ctrl, shift, str;
	
	if (window.Event) {
		mykey = e.which;
		alt = (e.modifiers & Event.ALT_MASK) ? true : false;
		ctrl = (e.modifiers & Event.CONTROL_MASK) ? true : false;
		shift = (e.modifiers & Event.SHIFT_MASK) ? true : false;
	} else {
		mykey = e.keyCode;
		alt = e.altKey;
		ctrl = e.ctrlKey;
		shift = e.shiftKey;
	}
	
	console.log(shift && mykey);
	
	if(shift && mykey == 13) {
		alert("Shift-Enter was pressed");
	}
	*/	
	/* Allowed keycodes			
		 8: backsp
		 9: tab
		16: shift
		17: ctrl
		35: end
		36: home
		37: left
		38: up
		39: right
		40: down	
		46: delete			
		96 - 105: 0-9 keypad
			
		110: . keypad
		190: .			
	*/				
	
	if (numcheck.test(keychar) || (keycode >= 35 && keycode <= 40) ||
   								  (keycode >= 96 && keycode <= 105) ||
				  				   keycode == 8 || 
				  				   keycode == 9 || 
				  				   keycode == 16 || 
   				  				   keycode == 17 ||
				  				   keycode == 46 ||
				  				   keycode == 110 || 
				  				   keycode == 190) {
		isOk = true;					
			
	}
	return isOk
}					
