// #################################################
function Sponsor_Description(object) {
	var buf = $('#demo_description');

	$(buf).html($(object).val());
}

// #################################################
function Sponsor_Company(object) {
	$('#demo_link_url').html( $(object).val() );
}

// #################################################
function Sponsor_Website(object) {
	$('#demo_website_url').html( $(object).val() );
}

// #################################################
function CheckMaxLength(object,max_length,id) {
	if(!max_length) {
		max_length = 10;
	}
	var value = $(object).val();

	if(value.length > max_length) {
		value = value.substr(0,max_length);
		$(object).val( value );
	}

	$('#demo_link'+id).html(value);
}

// #################################################
function SponsorAction(action) {
	$('#sponsor_action').val(action);
}

// #################################################
function SponsorApplication_Update() {
	var data = ParseForm('twerq_sponsor_signup');
	$.ajax({
		url:		"/ajax/sponsor_application_update.php",
		type:		"POST",
		data:		data,
		dataType:	"json",
		success:	function(object) {
			$('#sponsor_num_keywords').html(object.num_keywords);
			$('#sponsor_duration').html(object.duration);
			$('#sponsor_discount_percent').html(object.discount_percent);
			$('#sponsor_subtotal').html(object.subtotal);
			$('#sponsor_discount_amount').html(object.discount_amount);
			$('#sponsor_total').html(object.total);
		}
	});
}

// #################################################
function SponsorSignup() {
	// submit action
	return true;
}

// #################################################
function ParseForm(form_id,action_val) {
	var query_string = "action=";

	// allow form_id to be an actual form
	var form;
	if (typeof form_id == 'string')
		form = $('#' + form_id);
	else
		form = $(form_id);
	
	if(action_val == null) {
		query_string += "parse_form";
	} else {
		query_string += action_val;
	}

	$(':input', form).each(function(i) {
		var this_name = $(this).attr('name');
		var this_type = $(this).attr('type');
		var this_val = $(this).val();

		// Is this a checkbox?
		if( this_type == 'checkbox' ) {
			// Yes. Is it checked?
			if( $(this).attr('checked') == true ) {
				query_string += "&"+this_name+"="+encodeURIComponent( this_val );
			} else {
				// Else not checked, don't send a "true" value.
				query_string += "&"+this_name+"=0";
			}
		} else if( this_type == 'radio' ) {
			// Radio button. Is it selected?
			if( $(this).attr('checked') == true ) {
				// Yes.
				query_string += "&"+this_name+"="+encodeURIComponent( this_val );
			} // Else, it's not selected. Don't send it.
		} else {
			// Not a checkbox or radio button.. Send it.
			query_string += "&"+this_name+"="+encodeURIComponent( this_val );
		}
	});

	return query_string;
}

// #################################################
function ExpandCollapseDetails(id) {

	//Find the span we'll be hiding.
	var button = $('#button_'+id);
	var detail = $('#details_'+id);

	//Are we collapsing or expanding the link.
	if(detail.css('display') == "block") {
		$(button).attr({ src: "/images/plus.gif", 'alt': 'Expand', 'title': 'Expand' });

		detail.css('display', 'none');
	} else {
		$(button).attr({ src: "/images/minus.gif", 'alt': 'Collapse', 'title': 'Collapse' });

		detail.css('display', 'block');
	}

	delete detail;
	delete link;
	delete button;
}

// #################################################
function ExpandCollapseAll(obj) {
	if($(obj).attr('class') == 'expand') {
		$(obj).removeClass('expand')
			.addClass('collapse')
			.html('Collapse All');
		$('.details').css('display', 'block');
		$('.details_button').attr({ src: "/images/minus.gif", 'alt': 'Collapse', 'title': 'Collapse' });
	} else {
		$(obj).removeClass('collapse')
			.addClass('expand')
			.html('Expand All');
		$('.details').css('display', 'none');
		$('.details_button').attr({ src: "/images/plus.gif", 'alt': 'Expand', 'title': 'Expand' });
	}		
}

// #################################################
function print_r(obj,id) {
	var buffer = "";
	$.each(obj,
		function(i) {
			buffer = buffer + i + ": " + this + "\n";
		}
	);

	if(id) {
		$('#'+id).html(buffer);
	} else {
		alert(buffer);
	}
}