// DriversEd.com Sweepstakes entry form processing

var notSending = true;

$(document).ready(function() {
	var options = {
	    target: '#serverMsg',
	    beforeSubmit: showRequest,
	    success: showResponse,
	    dataType: 'xml',
	    url: '/cgi-bin/parentEmail.pl' };

	$('#tellParents').submit(function() {
		$('#submit img').replaceWith('<img src="/images/btn_sending.gif" width="91" height="22" alt="Button Sending" />');
		$(this).ajaxSubmit(options);
		return false;
	    });
    });

function submitTell() {
    if (notSending) {
	clearErrors();
	$('#serverMsg').html('Sending message...');
	$('#tellParents').submit();
	$('#tellOutput').show(0);
    }
}

function showRequest(formData, jqForm, options) {
    var queryString = $.param(formData);
}

function showResponse(responseText, statusText) {
    $('#submit img').replaceWith('<img src="/images/btn_submit.gif" width="91" height="22" alt="Button Submit" />');
    $('#tellOutput').click(function() { $(this).hide() });

    var status = $('response', responseText).attr('status');
    var msg = $('message', responseText).text();

    if (status == 'ERROR') {
	var attribute = $("field", responseText).each(function() {
		var fieldName = $(this).attr('name');		
		$('#' + fieldName).addClass('err');
		msg += '<br/>' + $(this).text();
	    });
    }
    else {
	pageTracker._trackPageview('/parentemail/success');
	$('#name').val('');
        $('#email').val('');
        $('#parentsEmail').val('');
        $('#message').val('');
        $('#terms').attr("checked", "");
    }

    $('#serverMsg').html(msg);
}

function clearErrors() {
    $('input.err').each(function() { $(this).removeClass('err'); });
    $('#message').removeClass('err');
}