// 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 validateUrl(urlField) {
	var urlString = urlField.value;
	if (urlString != '') {
		if (urlString.indexOf('http://') != 0 ||
			(urlString.indexOf('http://www.stunme.com/images') != 0 &&
             urlString.indexOf('myspacecdn.com') < 0 &&
             urlString.indexOf('hi5.com') < 0 &&
             urlString.indexOf('flickr.com') < 0)) {
			urlField.value = '';
			return false;
		}
	}
	return true;
}
function validateAll(count) {
	var ret = false;
	if (!validateColor(document.createForm.background_color.value, 'paper color')) {
		return ret;
	}

	var hasUrl = false;
	var hasBadUrl = false;
	for (var i = 1; i <= count; ++i) {
		var thisUrl = eval('document.createForm.url'+i).value;
		if (!validateUrl(eval('document.createForm.url'+i))) {
			hasBadUrl = true;
		}
		else if (thisUrl != '') {
			hasUrl = true;
		}
		
	}
	if (hasBadUrl) {
		alert('You entered an invalid image URL. Please make sure you have only URLs from allowed websites listed below.');
		return ret;
	}
	if (!hasUrl) {
		alert('Please enter at least one image URL.');
		return ret;
	}
	return true;
}
function getFlashVars(count) {
	var newFlashVars = 'background_color=' + document.createForm.background_color.value + '&';
	for (var i = 1; i <= count; ++i) {
		var thisUrl = eval('document.createForm.url'+i).value;
		if (thisUrl != '') {
			newFlashVars += 'url' + i + '=' + escape(thisUrl) + '&';
			if (eval('document.createForm.resize'+i).checked)
				newFlashVars += 'resize' + i + '=Y&';
			if (eval('document.createForm.crop'+i).checked)
				newFlashVars += 'crop' + i + '=Y&';
		}
	}
	newFlashVars += 'optimize=' + selectedValue(document.createForm.optimize) + '&';
	newFlashVars += 'autoplay=' + selectedValue(document.createForm.autoplay);
	return newFlashVars;
}
	
function getHtmlFromFlashVars(myFlashVars) {
	var html= 
		  '<br /><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="340">' +
		  '<param name="movie" value="http://www.stunme.com/flash/animated_paint_flash9.swf" />' +
		  '<param name="quality" value="high" />' +
		  '<param name="menu" value="false" />' +
		  '<param name="FlashVars" value="' + myFlashVars + '">' +
		  '<embed src="http://www.stunme.com/flash/animated_paint_flash9.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="340" ' +
		  'FlashVars="' + myFlashVars + '"></embed></object><br />' +
		  '<a href="http://www.stunme.com" target="_top"><img src="http://www.stunme.com/images/stunme_bar_300.gif" alt="Stun Me" width="300" height="18" border="0" /></a><br />';
	return html;
}
function getCode(count) {
	if (validateAll(count)) {	
		var flashVars = getFlashVars(count);
		document.createForm.embed_code.value = getHtmlFromFlashVars(flashVars);
		document.getElementById('code').style.display = 'block';
		document.getElementById('gigya').src = '/widgets/animated_paint/gigya?flash_vars=' + escape(flashVars);
	}
}
function preview(count) {
	if (validateAll(count)) {	
		var flashVars = getFlashVars(count);
		document.getElementById('preview').innerHTML = getHtmlFromFlashVars(flashVars);
	}
}
function updateCropCheckbox(resizeBox, cropBox) {
	if(!resizeBox.checked) {
		cropBox.checked = true;
		cropBox.disabled = true;
	}
	else {
		cropBox.disabled = false;
	}
}