var SERVER = "auctiontrax.com";
var defaultWidth = 750;

function getXMLURL(id) {
	var basic_url = "http://" + SERVER + "/flash/slideshow/" + id + "/list.xml";
	return basic_url.escapeHTML();
}

function generateFlash(id) {
	if (validate_form()) {
		var flash_width_elements = $A(document.getElementsByName("flash_width"));
		var flash_width_element = flash_width_elements.find(
			function(option){
				if (option.checked == true) return option;
			}
		);

		var width = flash_width_element.value;
		var height = getHeight(width);

		var flashTemplate =
			/*
			new Template(
				'<div align="center">' +
				'<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="750" height="170" id="main" align="middle">' +
				'<param name="allowScriptAccess" value="sameDomain" />' +
				'<param name="movie" value="http://#{server}/flash/slideshow/slideshow.swf" />' +
				'<param name="quality" value="high" />' +
				'<param name="bgcolor" value="#ffffff" />' +
				'<param name="params" value="#ffffff" />' +
				'<param name="FlashVars" value="url=#{url}" />' +
				'<embed FlashVars="url=#{url}" src="http://#{server}/flash/slideshow/slideshow.swf" quality="high" bgcolor="#ffffff" width="750" height="170" name="main" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />' +
				'</object>' +
				'</div>'
			);
			*/
			new Template(
				'<div align="center">' +
				'<object width="' + width + '" height="' + height + '" align="middle">' +
				'<param name="movie" value="http://#{server}/flash/slideshow/' + id + '.swf" />' +
				'<embed src="http://#{server}/flash/slideshow/' + id + '.swf" width="' + width + '" height="' + height + '" align="middle" type="application/x-shockwave-flash" />' +
				'</object>' +
				'</div>'
			);
		var show = {url: getXMLURL(id), server: SERVER};
		$("flashPreview").style.visibility = 'visible';
		$("flashText").value = flashTemplate.evaluate(show);
		$("flashText").select();
	}
}

function validate_form() {
	if ($("PrimarySellerID").value == "" && $("QueryKeywords").value == "") {
		alert("Please specify Primary eBay Seller ID or eBay KeyWord search criteria");
		return false;
	}

	var site_elements = $A(document.getElementsByName("data[site][]"));
	var site_element = site_elements.find(
		function(option){
			if (option.checked == true) return option;
		}
	);
	if (!site_element || !site_element.value) {
		alert("Please specify Site ID");
		return false;
	}

	var border_color_elements = $A(document.getElementsByName("border_color"));
	var border_color_element = border_color_elements.find(
		function(option){
			if (option.checked == true) return option;
		}
	);
	if (!border_color_element || !border_color_element.value) {
		alert("Please specify Border Color");
		return false;
	}
	return true;
}

function getHeight(value) {
	var result = (value * 170)/750;
	return Math.round(result);
}

function supportedSitesCheck(checked) {
	var site_elements = $A(document.getElementsByName("data[site][]"));
	site_elements.find(
		function(element){
			element.checked = checked;
		}
	);
}