// jQuery Dropdown Mailer

/*
	This file is designed to REPLACE existing content with a drop down that allows users 
	to choose a contact method. Then submit a quick Opt In form all using AJAX.
	
	The file works as follows.
	- Include jQuery on the page.  will not work without
	- make a block level element in XHTML and give it the class of "ddReplace"
	- Place the alternate content inside the element for users without JS support
	- Ex:
		<p class="ddReplace"><a href="mailto:info@nacconline.ca">Contact us for more info</a></p>
	

*/
function replaceTarget(target, index) {
	//
	index = index + $(".dd").length;
	$(target).toggleClass("ddReplace").toggleClass("dd");
	$(target).html("");
	//$("<form/>").attr({"id": "ddr_"+index, "method": "post"}).appendTo(target);

	$(target).load("/site/php/ddr.php?index="+index,{},function(){
		$("#ddrTarget_"+index).hide();
		$("#ddrSelect_"+index).change(function(){
			var fn = $(this).val();
			//alert(fn);
			$("#ddrTarget_"+index).fadeOut("fast").load("/site/php/ddr.php?index="+index+"&fn="+fn,{},function(){
				$("#ddrSubmitForm_"+index).submit(function(){
					
					temp = {};
					$("#ddrSubmitForm_"+index+" :input").each(function(inputIndex){
						var tempName = $(this).attr("name");
						var tempVal = $(this).val();
						//temp[inputIndex] = {tempName :  tempVal};
						temp[tempName] = tempVal;
						//temp.tempName.value() = tempVal;
						//alert(temp[inputIndex]);
					}); 
						//alert(temp);
					// !!! Important !!! 
					// always return false to prevent standard browser submit and page navigation 
					$("#ddrTarget_"+index).html("<h3>Sending</h3>").load("/site/php/ddr.php", temp, function(data){
						// after load
						$("#ddrForm_"+index).fadeOut("slow");
					});
					return false; 
				});
			}).fadeIn("slow");
		});
	});
}
function findTargets(selector){
	$(selector).each(function(index){ 
		replaceTarget(this, index);
	});
}
function straightReplace(selector, fn){
	index = $(".dd").length;
	
	$("#ddrTarget_"+index).html("");
	$(selector).load("/site/php/ddr.php?infoAccess=1&index="+index+"&fn="+fn,{},function(){
		$("#ddrSubmitForm_"+index).submit(function(){
			
			temp = {};
			$("#ddrSubmitForm_"+index+" :input").each(function(inputIndex){
				var tempName = $(this).attr("name");
				var tempVal = $(this).val();
				//temp[inputIndex] = {tempName :  tempVal};
				temp[tempName] = tempVal;
				//temp.tempName.value() = tempVal;
				//alert(temp[inputIndex]);
			}); 
				//alert(temp);
			// !!! Important !!! 
			// always return false to prevent standard browser submit and page navigation 
			$("#ddrTarget_"+index).html("<h3>Sending</h3>").load("/site/php/ddr.php", temp, function(data){
				// after load
				$("#ddrForm_"+index).fadeOut("slow");
			});
			return false; 
		});
	});
}
$(document).ready(function(){ 
	findTargets(".ddReplace"); 
	straightReplace(".straightReplace", "periodic");
	$(".ddLink").click(function(){
		$(this).after("<div class='ddReplace'>&nbsp;</div>");
		findTargets(".ddReplace"); 
		return false;
	});
	$(".ddLink_straightReplace").click(function(){
		$(this).after("<div class='straightReplace'>&nbsp;</div>");
		straightReplace(".straightReplace", "periodic");
		return false;
	});
	
});