function submitComment(e)
{
	new Ajax.Request('/include/designs.php?func=submit_comment_json', 
	{
		method : 'post', 
		parameters : $('formMain').serialize(true), 
		onSuccess : function(transport, json) 
		{
			if (json.rc == 0)
			{
				$('design_comments').disabled = true; 
				$('commenter_name').disabled = true; 
				$('message').innerHTML = json.rm; 
			}
			else if ( json.rc == 100 ) 
			{
				location.href = location.href; 
			}
			else
			{
				var messages = ''; 
				
				for (i in json.field_messages)
				{
					messages+= json.field_messages[i];
				} 
				for (i in json.messages)
				{
					messages+= json.messages[i]; 
				}
				modalBoxAlert( messages, 'Uh-oh!' ); 
			}
		}, 
		onFailure : function() {
			modalBoxAlert( 'A communication error occured while trying to submit comment.', 'Uh-oh!' ); 
		}
	}); 
	Event.stop(e); 
}

function submitVote(e)
{
	var form = this; 
	var button = form.childNodes[2];
	var buttonId = button.id; 
	new Ajax.Request('/include/designs.php?func=submit_vote_json', {
		method : 'post', 
		parameters : this.serialize(true), 
		onSuccess : function(transport, json) {
			if (json.rc == 0) {
				button.src = '/i/' + buttonId + '_voted.jpg'; 
				button.stopObserving( 'mouseover' ); 
				button.stopObserving( 'mouseout' );
				$('voteThanks').innerHTML = 'Thank you for your feedback! ';  		
				modalBoxAlert( 'Your vote is in. Thanks for your feedback!' );  
				
			} else {
				var messages = ''; 
				
				for (i in json.field_messages) {
					messages+= json.field_messages[i];
				} 
				for (i in json.messages) {
					messages+= json.messages[i]; 
				}
				modalBoxAlert( messages, 'Uh-oh!' ); 
			}
		}, 
		onFailure : function() {
			modalBoxAlert( 'A communication error occured while trying to submit your notification request, please try again.', 'Uh-oh!' ); 
		}
	}); 

	Event.stop(e); 
}

function tweetRequest( url )
{
	shortenUrl( url, tweet ); 
}

function tweet( url ) 
{
	$( 'status' ).value = 'Check out this ' + $( 'tweetForm' ).design_name.value + ' design @splitreason_com ' + url + ' !'; 
	$( 'tweetForm').submit(); 
}

function submitNotify(e) 
{
	new Ajax.Request( '/include/designs.php?func=notify_json', 
	{
		method : 'post', 
		parameters : this.serialize(true), 
		onSuccess : function(transport, json)
		{
			if (json.rc == 0) { 
				modalBoxAlert( json.rm, 'We\'re good!' ); 
			} else { 
				var messages = ''; 
				for (i in json.messages) {
					messages+= json.messages[i]; 
				}
				modalBoxAlert( messages, 'Uh-oh!' ); 

			}
		}, 
		onFailure : function() { 
			modalBoxAlert( 'A communication error occured while trying to send your vote, please try again.', 'Uh-oh!' ); 
		} 
	}); 
	Event.stop(e); 
}

Event.observe( window, 'load', function() {
	var v = $( 'vote1' ); 
	if ( v != undefined )
	{ 
		$( 'vote1' ).observe( 'mouseover', function() { this.src = '/i/vote1_over.jpg'; }); 
		$( 'vote1' ).observe( 'mouseout', function() { this.src = '/i/vote1.jpg'; }); 
		Event.observe( 'rating1Form', 'submit', submitVote );
	
		$( 'vote2' ).observe( 'mouseover', function() { this.src = '/i/vote2_over.jpg'; }); 
		$( 'vote2' ).observe( 'mouseout', function() { this.src = '/i/vote2.jpg'; }); 
		Event.observe( 'rating2Form', 'submit', submitVote );
		
		$( 'vote3' ).observe( 'mouseover', function() { this.src = '/i/vote3_over.jpg'; }); 
		$( 'vote3' ).observe( 'mouseout', function() { this.src = '/i/vote3.jpg'; }); 
		Event.observe( 'rating3Form', 'submit', submitVote );
		
		$( 'vote4' ).observe( 'mouseover', function() { this.src = '/i/vote4_over.jpg'; }); 
		$( 'vote4' ).observe( 'mouseout', function() { this.src = '/i/vote4.jpg'; }); 
		Event.observe( 'rating4Form', 'submit', submitVote );
		
		$( 'vote5' ).observe( 'mouseover', function() { this.src = '/i/vote5_over.jpg'; }); 
		$( 'vote5' ).observe( 'mouseout', function() { this.src = '/i/vote5.jpg'; }); 
		Event.observe( 'rating5Form', 'submit', submitVote );
	}
	var nf = $( 'notifyform' ); 
	if ( nf != null)
		nf.observe( 'submit', submitNotify ); 
	
	Event.observe( 'formMain', 'submit', submitComment ); 
	var submitCmt = $( 'submitCommentBut' ); 
	submitCmt.observe( 'mouseover', function() { this.src = '/i/leavecmt_big_over.jpg'; });
	submitCmt.observe( 'mouseout', function() { this.src = '/i/leavecmt_big.jpg'; }); 
	var leaveCmt = $( 'leaveCmt' );
	leaveCmt.observe( 'mouseover', function() { this.src = '/i/leavecmt_sml_over.jpg'; }); 
	leaveCmt.observe( 'mouseout', function() { this.src = '/i/leavecmt_sml.jpg'; });  
	listenNavButtons(); 
}); 