var getzip = "<div><span>Your zip code:&nbsp;&nbsp;</span><input type='text' name='zipselection' value='' onChange='javascript:updatezipcode(this);'/></div>"; 
var javascript_code = "<div style='float: left;width:100%;'><div style='padding-top: 5px;padding-bottom: 5px;'>Copy and paste the following " +
			"javascript code where you would like the StoreCatch widget to be on your site:</div><textarea onclick='javascript:this.select();'>" +
			"<script type='text/javascript' src='http://www.storecatch.com/widgetcontent?number_to_fetch=5&start_zip={{zip_to_use}}" +
			"&container_id=storecatch_1&response_type=javascript'></script></textarea></div>";
			
var blogger_code = "<form method='POST' action='http://www.blogger.com/add-widget'> " +
		"<input type='hidden' name='widget.title' value='Great Local Sales'/>" +
		"<input type='hidden' name='logoUrl' value='http://www.storecatch.com/widgets'/> " +
		"<input type='hidden' name='widget.content' value='&lt;script type=&#39;text/javascript&#39; " +
		"src=&#39;http://www.storecatch.com/widgetcontent?number_to_fetch=5&start_zip={{zip_to_use}}" +
		"&container_id=storecatch_1&response_type=javascript&#39;&gt;&lt;/script&gt;'/><input type='submit' value='click me to install on blogger'/></form>";
var javascript_code_updated = "";
var blogger_code_updated = "";

jQuery(document).ready(function() {
	//setup our event triggers
	$(".widgettype").bind("click",
	  function(element) {
	  	//display our widget code container
	  	var codetype = $(this).attr("id");
	  	displaycode(codetype);
	  	$(".widgetinstallcontainer").css({display: "none"});
	  	$(".widgetinstallcontainer").fadeIn("slow"); 
	  }
	);	
	
});

//This function decides what to display in our widget get code box
function displaycode(codetype){
	
	if(codetype == 'javascript'){
		if(javascript_code_updated.length > 0){
			$("#specificinstallcode").html(javascript_code_updated);
		}else{
			if($("input[name='zipselection']").val().length == 5){
				updatezipcode("input[name='zipselection']");
				$("#specificinstallcode").html(javascript_code_updated);	
			}else{
				alert("Please select a valid zip code first");
				$("input[name='zipselection']").select();
			}
		}
	}else if(codetype == 'blogger'){
		if(blogger_code_updated.length > 0){
			$("#specificinstallcode").html(blogger_code_updated);
		}else{
			if($("input[name='zipselection']").val().length == 5){
				updatezipcode("input[name='zipselection']");
				$("#specificinstallcode").html(javascript_code_updated);	
			}else{
				alert("Please select a valid zip code first");
				$("input[name='zipselection']").select();
			}
		}
	}else{
		$("#specificinstallcode").html("<div>Unsupported code</div>");
	}	
}

//This just takes our zipcode and places it in the generated code block
function updatezipcode(element){
	var zipcode = $(element).val();
	if((zipcode.length >= 1 && zipcode.length < 5) || zipcode.length > 5){
		alert("Please enter valid zipcode (ex: 84096)");
		javascript_code_updated = "";
		blogger_code_updated = "";
		$("input[name='zipselection']").select();
		return;
	}
	javascript_code_updated = javascript_code.replace(/{{zip_to_use}}/ig,$(element).val());	
	blogger_code_updated = blogger_code.replace(/{{zip_to_use}}/ig,$(element).val());	
	$(".widgetinstallcontainer").css({display: "none"});
}
