function previewGraph(button, type) {
    var temp = button.form.action;
    button.form.action = "/graph?preview=" + type;
    button.form.target = '_blank';
    button.form.submit();
    button.form.action = temp;
    button.form.target = '';

    return false;
}

function clearForm(form) {
    for (el = 0; el < form.elements.length; el++) {
        if (form.elements[el].type == 'text') {
            form.elements[el].value = '';
        }
    }
}

function check_username(username, error_field) {
    $('#username_errors').load('/request.php', {action: 'check_username', username: username}, function(responseText, status, response) {
        if (responseText) {
            $('#username_errors_row').show();
        } else {
            $('#username_errors_row').hide();
        }
    });
}

function lookup_postcode(postcode) {
    $.getJSON('/request.php', {action: 'lookup_postcode', postcode: postcode},
        function(json) {
            if (json.town) $('#input_town').val(json.town);
            if (json.post_county) $('#input_county').val(json.post_county);
        });
}

$(function(){
    $("p.flash").each(function(){
        doBGFade(this,[255,255,100],[255,255,255],'transparent',75,20,4);
    });
})


function easeInOut(minValue,maxValue,totalSteps,actualStep,powr) {
	var delta = maxValue - minValue;
	var stepp = minValue+(Math.pow(((1 / totalSteps)*actualStep),powr)*delta);
	return Math.ceil(stepp)
}

function doBGFade(elem,startRGB,endRGB,finalColor,steps,intervals,powr) {
	if (elem.bgFadeInt) window.clearInterval(elem.bgFadeInt);
	var actStep = 0;
	elem.bgFadeInt = window.setInterval(
		function() {
			elem.style.backgroundColor = "rgb("+
				easeInOut(startRGB[0],endRGB[0],steps,actStep,powr)+","+
				easeInOut(startRGB[1],endRGB[1],steps,actStep,powr)+","+
				easeInOut(startRGB[2],endRGB[2],steps,actStep,powr)+")";
			actStep++;
			if (actStep > steps) {
			elem.style.backgroundColor = finalColor;
			window.clearInterval(elem.bgFadeInt);
			}
		}
		,intervals)
}
