var sumString=''
function summary(identifier, value) {
	sumString+=identifier;
	sumString+='=';
	sumString+=value;
	sumString+='\n';
}

function gatherFormData(formQuery) {
  	var data = {};
  	sumString='';
		
  	var allInputs = formQuery.find(':input[name]');
  	  	
    // first take data from non-hidden inputs
  	allInputs.not('[type=hidden]').each( function(index) {  		
  		if (jQuery(this).is('input:checkbox')) {
			if (jQuery(this).is(':checked')) {
				data[jQuery(this).attr('name')] = 'true';
				summary('checkbox ' + jQuery(this).attr('name'),'true');
			} else {
				data[jQuery(this).attr('name')] = 'false';
				summary('checkbox ' + jQuery(this).attr('name'),'false');
			}
  		} else if (jQuery(this).is('option')) {
  			data[jQuery(this).attr('name')] = jQuery(this).val();
  			summary('select ' + jQuery(this).attr('name'),jQuery(this).val());
  		} else if (jQuery(this).val()!=jQuery(this).attr('alt')) {
  			data[jQuery(this).attr('name')] = jQuery(this).val();
  			summary('adding ' + jQuery(this).attr('name'),jQuery(this).val());
  		} else {
  			data[jQuery(this).attr('name')] = '';
  			summary('adding ' + jQuery(this).attr('name'),'<empty>');
  		}
  	});
  	
  	// merge data from hidden inputs
  	allInputs.filter('[type=hidden]').each( function(index) {
  		summary('testing for hidden:' + jQuery(this).attr('name'),data[jQuery(this).attr('name')]);
  		if (data[jQuery(this).attr('name')]==undefined) {
  			summary('adding from hidden:' + jQuery(this).attr('name'),jQuery(this).val());
  			data[jQuery(this).attr('name')] = jQuery(this).val();
  		}
  	});
  	  	
  	return $.param(data,true);
}

function submitForm(targetFormQuery, targetUrl) {
	var newAction = targetUrl + '?' + gatherFormData(targetFormQuery);	
	window.location.href = newAction;
  	return false;
}


function submitFormWithAjax(targetToReplaceSelector, targetFormId, targetUrl, correctContentSelector, pleaseWaitSelector, submitButtonSelector, errorMessage) {	
  	var theForm = jQuery("form[id="+targetFormId+']');
  	var data = gatherFormData(theForm);
  	  	
  	var pleaseWaitText = jQuery(pleaseWaitSelector).text();
  	
  	var theFormsSubmitButtons = jQuery(submitButtonSelector);	
 	
  	theFormsSubmitButtons.each(function(index) {
  		var button = jQuery(this);
  	    button.attr('data-orignalLabel',button.text()).attr('disabled', 'disabled').text(pleaseWaitText);
    });
  	
  	jQuery(pleaseWaitSelector).show();
  	
  	jQuery.get(targetUrl, data, function(data, status, xhr) {
  	  var gotErrorPage = !jQuery(data).is(correctContentSelector);
  	  
  	  if (status == "error" || gotErrorPage) {
  	    if (!gotErrorPage) {
  	    	// append status if we are not on the error page
  	    	errorMessage += ' (' + xhr.status + xhr.statusText + ')';
  	    }
  	    jQuery(targetToReplaceSelector).html('<strong><font color="red">' + errorMessage + '</font></strong>');
  	  } else {
  		jQuery(targetToReplaceSelector).html(data);
  	  }
    
  	  jQuery(pleaseWaitSelector).hide();
			  
		var theFormsSubmitButtons = jQuery(submitButtonSelector);	
		
		theFormsSubmitButtons.each(function(index) {
		    jQuery(this).text( jQuery(this).attr('data-orignalLabel')).removeAttr('disabled');
		});
      	  	  
  	  customizeUI();
  	  return false;
  	});
  	
  	return false;  
}
   
//find a city by given plz
//@param targetDomSelector a selector for the dom element that gets the content. gets red if nothing found
//@param sourcePlzSelector the source input where the plz comes from. is reset to alt when nothing is found
//@param notFoundMessage message to Display if nothing is found
function findCityByPLZ(targetDomSelector, sourcePlzSelector, notFoundMessage) {
	var plzQuery = jQuery(sourcePlzSelector);
	var targetDomQuery = jQuery(targetDomSelector);
	jQuery.get('/fansale/ajaxFindCityName.htm', {'plz':plzQuery.val()},
		function(data, status, xhr) {
		  	var gotResult = !jQuery(data).is('span[id=result]:empty');
		  				
			if (!gotResult) {
				if (targetDomQuery.is(':input')) {
					targetDomQuery.val(notFoundMessage).addClass('red');
				} else {
					targetDomQuery.html(notFoundMessage).addClass('red');
				}
				plzQuery.val(plzQuery.attr('alt'));
			} else {
				var city = jQuery.trim(jQuery(data).text());
				targetDomQuery.removeClass('red');
				if (targetDomQuery.is(':input')) {
					targetDomQuery.val(city);					
				} else {
					targetDomQuery.html(city);
				}
			}
		}
	);
}

function suggestArtist(targetDomSelector, sourceDomSelector, notFoundMessage) {
	var artistQuery = jQuery(sourceDomSelector);
	var targetDomQuery = jQuery(targetDomSelector);	
	targetDomQuery.load('/fansale/ajaxSuggestArtist.htm', {'characters':artistQuery.val()},function() {
		if (targetDomQuery.is("select")) {
			targetDomQuery.selectmenu('destroy').selectmenu();
		}
	});
}
