function onAffiliateCodeChange() {
	var badColor = '#f8dede';
	var goodColor = '#ffffff';
	var regEx;

	// URL String parts
	var serverDomain = document.domain;
	var swfURL = "http://" + serverDomain + "/f/embed_bestof.swf?";
	var serverUrl_prefix = '<embed src="';
	var serverUrl_suffix = '" width="400" height="345" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>';
	// Control variables
	var affiliateCodeCtl = document.getElementById("affiliateCode");
	var sampleDivCtl = document.getElementById("dvSample");
	var channelCtl = document.getElementById("channel");
	var timeFrameCtl = document.getElementById("timeframe");
	var searchKeywordsCtl = document.getElementById("searchKeywords");

	// Form validation
	regEx = regexTests['onlyLettersAndNumbersEmpty'];
	regEx = new RegExp(regEx.substr(1, regEx.length-2));
	value = searchKeywordsCtl.value;

	if (((!value.match(regEx)) || (value.length < 2) || (value.length > 60)) && (value.length != 0)) {
		alert('The keyword length should be between 2 and 60 chars, and include only letters and numbers.');
		searchKeywordsCtl.style.backgroundColor = badColor;
		return;
	} else {
		searchKeywordsCtl.style.backgroundColor = goodColor;
	}
	// Finished string
	if (channelCtl.value != null && channelCtl.value != "") {
		swfURL += "&" + channelCtl.value;
	} else {
		swfURL += "&" + "channelID=1";
	}
	if (searchKeywordsCtl.value != null && searchKeywordsCtl.value != "")
		swfURL += "&" + "searchKeywords=" + escape(searchKeywordsCtl.value);
	if (timeFrameCtl.value != null && timeFrameCtl.value != "")
		swfURL += "&" + timeFrameCtl.value;

	var serverUrl = serverUrl_prefix + swfURL;

	serverUrlDisplay = serverUrl + serverUrl_suffix;
	serverUrl += serverUrl_suffix;

	affiliateCodeCtl.value = serverUrl.valueOf();

	var so = new SWFObject(swfURL, "fp2Obj", "400", "345", "8");
	if (serverDomain != "www.metacafe.com") {
		so.addParam("flashvars", "altServerURL=http://"+document.domain);
	}
	so.addVariable("isSite","true");
	so.addParam("wmode", "transparent");
	so.addParam("AllowScriptAccess", "always");
	so.addParam("quality", "high");
	so.write("dvSample");

	//sampleDivCtl.innerHTML = serverUrlDisplay.valueOf();
}

// Copy strClipText to the clipboard in Explorer and FireFox
function copyStringToClipboard(strClipText){
	if (window.clipboardData) {
		window.clipboardData.setData("Text", strClipText);
	} else if (window.netscape) {
		var gClipboardHelper = Components.classes["@mozilla.org/widget/clipboardhelper;1"].getService(Components.interfaces.nsIClipboardHelper);
		gClipboardHelper.copyString(strClipText);
	}
}

// Copy the string from the textarea to the clipboard
function onAffiliateCodeCopy() {
	var affiliateCodeCtl = document.getElementById("affiliateCode");

	affiliateCodeCtl.select();
	copyStringToClipboard(affiliateCodeCtl.value);
}

// Generate the string, and copy it to the clipboard, and don't submit.
function onAffiliateCodeSubmit() {
	onAffiliateCodeCopy();
	return false;
}