/**
 * @author bnelson
 */
var programs;

$( document ).ready( function()
{
	$( "#wrapper-content" ).css( { background: "#FFF", borderLeft: "1px solid #B7BABC", borderRight: "1px solid #B7BABC" } );
	$( "#wrapper-menu-main" ).hide();

	$( ".form-item input:text" ).css( { width: "492px" } );
	$( "#_topic, #_proid" ).css( { width: "495px" } );
	$( "#_message" ).css( { width: "492px" } );

	getPrograms();
	
	$(".form-item").append( $("<div></div>").addClass( "form-message" ) );
	
	$(".prompting-select").change( function()
	{
		$(this).find("option[value='prompt']").remove();
	});
	
	$("#submitForm").click( submitForm );
	$("#resetForm").click( resetForm );
	$("#cte_yes").change( function() { $( ".cte-no" ).hide(); $( ".cte-yes" ).show();enableForm(); } );
	$("#cte_no").click( function() { $( ".cte-no" ).show(); $( ".cte-yes" ).hide();enableForm(); } );
});

function enableForm()
{
	$( "#_emailaddress" ).removeAttr( 'disabled' );
	$( "#_lastname" ).removeAttr( 'disabled' );
	$( "#_firstname" ).removeAttr( 'disabled' );
	$( "#_message" ).removeAttr( 'disabled' );
	$( "#_authcode" ).removeAttr( 'disabled' );
	$( "#_proid" ).removeAttr( 'disabled' );
	$( "#_topic" ).removeAttr( 'disabled' );
	$("#submitForm").removeAttr( 'disabled' );
}

function getPrograms()
{
	$.ajax(
	{
		type: "POST",
		dataType: "xml",
		url: "/CTE/services/cte-services.php?cachebust=" + new Date().getTime(),
		data: { method: "GetPrograms", showAll: true, salt: $( "#salt" ).val(), hash: $( "#hash" ).val(), cachebreak: Math.random() },
		success: function( xml )
		{
			programs = xml;
			$( xml ).find( "program" ).each( function()
			{
				if( $(this).find("num").text()*1 < 500 )
				{
					$("<option></option>").attr( { value: $(this).find("id").text(), title: $(this).find("name").text() } ).text( "(ASC) " + $(this).find("name").text() ).appendTo( $("#_proid") );
				} else {
					$("<option></option>").attr( { value: $(this).find("id").text(), title: $(this).find("name").text() } ).text( "(MASTERS) " + $(this).find("name").text() ).appendTo( $("#_proid") );
				}
			});
		},
		error: function( XMLHttpRequest, textStatus, errorThrown )
		{
			alert( "ERROR: [contactform|getPrograms] " + textStatus + ": " + errorThrown );
		}
	} );
}

function resetForm()
{
	window.close();
}

function submitForm()
{
	validate( "email", $( "#_emailaddress" ) );
	validate( "string", $( "#_message" ) );
	if( $("[name=ctemember]:radio:checked").val() == "yes" )
	{
		validate( "string", $( "#_authcode" ) );
	} else if( $("[name=ctemember]:radio:checked").val() == "no" )
	{
		validate( "string", $( "#_lastname" ) );
		validate( "string", $( "#_firstname" ) );
		$( "#_authcode" ).val( "" );
	}
	if( $("#page .invalid").size() > 0 )
	{
		return;
	}
	
	$.ajax(
	{
		type: "POST",
		dataType: "xml",
		url: "/CTE/services/cte-services.php?cachebust=" + new Date().getTime(),
		data: { method: "SendContact", salt: $( "#salt" ).val(), hash: $( "#hash" ).val(), 
				firstname: $( "#_firstname" ).val(), lastname: $( "#_lastname" ).val(), 
				email: $( "#_emailaddress" ).val(), proid: $( "#_proid" ).val(),
				topic: $( "#_topic" ).val(), message: $( "#_message" ).val(), authcode: $( "#_authcode" ).val(),
				type: window.location.hash },
		success: function( xml )
		{
			$.modal.close();
			var type = $( xml ).find( "type:first" ).text();
			if( type == "failure" )
			{
				$.prompt('<strong>' + $( xml ).find( "title:first" ).text() + '</strong> <p>' + $( xml ).find( "body:first" ).text() + '</p>', { prefix: "mp", loaded: function(){ $('#flash-sponsers').hide(); }, callback: function(){ $('#flash-sponsers').show();$( "#submitForm" ).removeAttr( "disabled" ); } } );
			} else if( type == "success" )
				$.prompt('<strong>' + $( xml ).find( "title:first" ).text() + '</strong> <p>' + $( xml ).find( "body:first" ).text() + '</p>', { prefix: "mp", loaded: function(){ $('#flash-sponsers').hide(); }, callback: resetForm } );
		},
		error: function( XMLHttpRequest, textStatus, errorThrown )
		{
			alert( "ERROR: [contactform|submitForm] " + textStatus + ": " + errorThrown );
		}
	});
	
	$( "#submitForm" ).attr("disabled","disabled");
}

function validate( type, object )
{
	if( type == "string" )
	{
		if( StringValidator( $(object).val() ) )
		{
			$(object).removeClass("invalid").parent().find(".form-message").text("");
		} else {
			$(object).addClass( "invalid" ).parent().find( ".form-message" ).text("Required field.");
		}
	}
	
	if( type == "email" )
	{
		if( EmailValidator( $(object).val() ) )
		{
			$(object).removeClass( "invalid" ).parent().find( ".form-message" ).text("");
		} else {
			$(object).addClass( "invalid" ).parent().find( ".form-message" ).text("Invalid email address.");
		}
	}
}

function EmailValidator( e )
{
	var emailReg = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i;
	var regex = new RegExp( emailReg );
	return regex.test( e );
}

function StringValidator( s )
{
	return ( s.replace( /\s+/g, '' ) != "" );
}