// JavaScript Document
function selectedValue(selBox) {
  return selBox.options[selBox.selectedIndex].value;
}
function validateColor(color, name) {
	var isValid = true;
	if (color.length != 6)
		isValid = false;
	if (isValid) {
		for (var i = 0; i < 6; ++i) {
			if ('0123456789ABCDEFabcdef'.indexOf(color.charAt(i)) < 0) {
				isValid = false;
				break;
			}
		}
	}
	if (!isValid) {
		alert('please enter valid color for your ' + name + '.');
	}
	return isValid;
}
function validate(message) {
	var ret = null;
	if (!validateColor(document.createForm.background_color.value, 'background color')) {
		return ret;
	}
	else if (!validateColor(document.createForm.off_color.value, 'background dot color')) {
		return ret;
	}
	else if (!validateColor(document.createForm.text_color.value, 'background text color')) {
		return ret;
	}
	// validate message
	var allowed = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!$%&()-_;\'",./? ';
	var ret = '';
	var isValid = false;
	for (var i = 0; i < message.length; ++i) {
		if (allowed.indexOf(message.charAt(i)) >= 0) {
			ret += message.charAt(i);
			if (message.charAt(i) != ' ')
				isValid = true;
		}
	}
	if (!isValid) {
		alert('Your message is empty or has only invalid characters.');
	}
	return ret;
}
function getFlashVars(message) {
	var flashVars = 'text=' + escape(message) + '&';
	flashVars += 'background_color=' + document.createForm.background_color.value + '&';
	
	flashVars += 'off_color=' + document.createForm.off_color.value + '&';
	flashVars += 'text_color=' + document.createForm.text_color.value + '&';
	var speed = selectedValue(document.createForm.speed) * selectedValue(document.createForm.direction);
	flashVars += 'speed=' + speed + '&';
	return flashVars;
}

function getHtmlFromFlashVars(myFlashVars) {
	var html = '<br /><div style="white-space: nowrap; line-height:0px; vertical-align: baseline"><a href="http://www.stunme.com" target="_blank">' +
		  '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" ' +	
          'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" ' +
		  'width="300" height="60">' +
		  '<param name="movie" value="http://www.stunme.com/flash/digital_text.swf" />' +
		  '<param name="quality" value="high" />' +
		  '<param name="menu" value="false" />' +
		  '<param name="FlashVars" value="' + myFlashVars + '">' +
		  '<embed src="http://www.stunme.com/flash/digital_text.swf" quality="high" menu="false" ' +    
		  'pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" ' +
		  'type="application/x-shockwave-flash" width="300" height="60" ' +
		  'FlashVars="' + myFlashVars + '"></embed></object></a><br />' +
		  '<a href="http://www.stunme.com" target="_top"><img src="http://www.stunme.com/images/stunme_bar_300.gif" alt="Free Widgets" width="300" height="18" border="0" /></a><br /></div>';
	return html;
}
function getCode() {
	var message = validate(document.createForm.text.value);
	if (message == null) {
		return;
	}
	var flashVars = getFlashVars(message);
	document.createForm.embed_code.value = getHtmlFromFlashVars(flashVars);
	document.getElementById('code').style.display = 'block';
	document.getElementById('gigya').src = '/widgets/digital_text/gigya?flash_vars=' + escape(flashVars);
}
function preview() {
	var message = validate(document.createForm.text.value);
	if (message == null) {
		return;
	}
	document.createForm.text.value = message;	
	var flashVars = getFlashVars(message);
	document.getElementById('preview').innerHTML = getHtmlFromFlashVars(flashVars);
}