// requires jQuery and colorFlash extension

//Bind Form Submission Function
$(function() {
	$('.dform').each(function() {
		var tid = this.id;
		$('.submit_button',this).click(function() { contactSubmit(tid); });
	});
});

function contactSubmit(formID) {

	//Generic
	var jQ = $;
	var query = '';
	var amp = '';
	
	var submitURL = "http://"+document.location.hostname+"/contact_submit.php"; // may need to be changed per site.
	
	var responseType = 0; // 0 = "default" - show error messages in a response container
	
	if(jQ("#"+formID+"_response").length > 0) {
		var responseDiv = jQ("#"+formID+"_response");
	
		responseDiv.hide();
		responseDiv.html('');
	}
	else {
		responseType = 1; // 1 = on error: animate erroneous inputs, success: hide inputs within formID and insert success message.
	}

	jQ("input,textarea","#"+formID).each(function() {
		if(this.value != this.defaultValue || this.checked || $(this).is("[type=hidden]")) {

			query += amp+this.name+'='+this.value.replace('&','%26'); // hackish urlencoding
			amp = '&';
		}
	});

	$.ajax({
		type:'POST',
		url:submitURL,
		data:query,
		dataType:'xml',
		complete:function(xh,s) {
			var jQ = $;
			xd = xh.responseXML;
			
			if(responseType == 0) {
				defaultResponder(xd,formID,responseDiv);
			}
			else if(responseType == 1) {
				flashyResponder(xd,formID);
			}
		}
	});
}

function resetForm(formID) {
	/* Reset the form upon successful submission */
	$('input, textarea','#' + formID).each(function() {
		this.value = this.defaultValue;
		if(this.checked) this.checked = false;
//		if(!$(this).hasClass("def")) {
//			this.className += " def";
//		}
		$(this).removeClass("mod");
	});
}

function defaultResponder(xd,formID,responseDiv) {
	jQ = $;
	if(jQ("success",xd).length > 0) {
		responseDiv.html("<span class=\"success\">"+ jQ("message",xd).text() +"</span>");

		/* Reset the form upon successful submission */
		resetForm(formID);
	}
	else {
		var plrl = (jQ("error",xd).length > 1) ? 's' : '';
		var errout = '<ul>Your inquiry could not be submitted for the following reason'+plrl+':';
		var i;
		for(i=0; i < jQ("error",xd).length; i++) {
			errout += "<li>"+jQ("error:eq("+i+")",xd).text()+"</li>";
		}
		errout += "</ul>";

		responseDiv.html(errout);
	}
	if(responseDiv.hasClass("contactform")) {
		responseDiv.append('<img class="address_top" src="../images/bluebox_top.png"/><img class="address_bot" src="../images/bluebox_bottom.png"/>');
	}
	responseDiv.fadeIn('fast');
}

function flashyResponder(xd,formID) {
	jQ = $;
	if(jQ("success",xd).length > 0) {
		jQ("#"+formID+" input,#"+formID+" .hide").hide("fast",function() {
			if(jQ("#"+formID+" .success").length == 0) {
				var pn = jQ("#formbody"); //jQ("#"+formID+" input").parent(":eq(0)");
				pn.append("<div class=\"success\" style=\"display:none;\">"+ jQ("message",xd).text() +"</div>");
				pn.children().add(".submit_button").not(".success").hide("fast", function() {
					jQ("#"+formID+" .success").show("fast");
				});
			}
		});
		resetForm(formID);
	}
	else {
		for(i=0; i < jQ("error",xd).length; i++) {
			jQ("error",xd).each(function() {
				jQ("#"+formID+" input[@name="+jQ(this).attr("name")+"]").colorFlash({ speed: "fast" });
			});
		}
	}
}

function resetSFForm(obj) {
	var dform = $(obj).parents(".dform");
	$(".success",dform).remove();
	$("input",dform).not("[type=hidden]").show("fast");
}