var silly_files_filenames = new Array("clips/alcoholicsunanimous.mp3", "clips/AlcoholWarning_MMH.mp3", "clips/foxreporterchokes.mp3", "clips/hoemow.mp3", "clips/iquitthisbitch.mp3", "clips/methodent.mp3", "clips/MySpaceTutorial.mp3", "clips/pbushcallssaddam01.mp3", "clips/pbushcallssaddam02.mp3", "clips/Arnold_Calls_Gateway.mp3", "clips/Dr_Phil_Activates_Instant_Credit.mp3", "clips/Eric_Idle_-_FCC_Song.mp3");

var cheesy_files_filenames = new Array("clips/Hamster_MIDI.mp3", "soundfx/halloween/01_scarymusicalroom.mp3", "soundfx/halloween/02_reversepartyroom.mp3", "soundfx/halloween/03_blowup.mp3", "soundfx/halloween/04_bangecho.mp3", "soundfx/halloween/05_boo.mp3", "soundfx/halloween/06_boo.mp3", "soundfx/halloween/07_chainrattle.mp3", "soundfx/halloween/08_chainsawcutting.mp3", "soundfx/halloween/09_deeplaughs.mp3", "soundfx/halloween/10_electricalcurrent.mp3", "soundfx/halloween/11_ghostmoans.mp3", "soundfx/halloween/12_gremblinlaugh.mp3", "soundfx/halloween/13_hauntedhouse.mp3", "soundfx/halloween/14_heavybreathing.mp3", "soundfx/halloween/15_helpme.mp3", "soundfx/halloween/16_laughing.mp3", "soundfx/halloween/17_manscream.mp3", "soundfx/halloween/18_peoplescream.mp3", "soundfx/halloween/19_recordendskip.mp3", "soundfx/halloween/20_slowheartbeat.mp3", "soundfx/halloween/21_spookygraveyard.mp3", "soundfx/halloween/22_spookygraveyard.mp3", "soundfx/halloween/23_thunder.mp3", "soundfx/halloween/24_weirdphonering.mp3", "soundfx/halloween/25_witchlaugh.mp3", "soundfx/halloween/26_womanscream.mp3", "soundfx/halloween/27_thumpsandtapping.mp3", "soundfx/Sports/Vintage_boxing_bell_03.mp3");

var filename_header = "http://myspace.freeze.com/media/audio/";

// counter for total files
var url_files = 1;
var total_files = 0;

function getPlayerValue(player)
{
	var inputs = document.getElementsByTagName('input');
	if (inputs)
	{
		for (var i = 0; i < inputs.length; ++i)
		{
			if (inputs[i].type == "radio" && inputs[i].name == player && inputs[i].checked)
			{
				return inputs[i].value;
			}
		}
	}
}

function add_file()
{
	// increase number of files
	url_files++;
	
	if (url_files <= 10)
	{
		showElement("dfile" + url_files);
	}
}

function clear_files()
{
	document.getElementById('silly').selectedIndex = -1;
	document.getElementById('cheesy').selectedIndex = -1;
	
	// Clear all the questions
	setValue("file1", "http://");
	
	setValue("file2", "http://");
	hideElement("dfile2");
	
	setValue("file3", "http://");
	hideElement("dfile3");
	
	setValue("file4", "http://");
	hideElement("dfile4");
	
	setValue("file5", "http://");
	hideElement("dfile5");
	
	setValue("file6", "http://");
	hideElement("dfile6");
	
	setValue("file7", "http://");
	hideElement("dfile7");
	
	setValue("file8", "http://");
	hideElement("dfile8");
	
	setValue("file9", "http://");
	hideElement("dfile9");
	
	setValue("file10", "http://");
	hideElement("dfile10");
	
	url_files = 1;
}

function generate_code()
{
	var error = "N";
	var autostart;
	var cheesy_select = document.getElementById("cheesy");
	var silly_select = document.getElementById("silly");
	var select_error = document.getElementById("select_error");
	var file_string = '';
	var xml_list = "playlistSource=VARS&";
	
	// Validate autostart
	if (document.getElementById("autostart").checked == true)
	{
		autostart = 'autostart=Y"';
	}
	else
	{
		autostart = 'autostart=N"';
	}
	
	// Init slideshow code
	player_string = '<div><embed src="http://myspace.freeze.com/mediaplayers/mlmp' + getPlayerValue('skins') + '.swf?mediaType=MP3&';
	
	// First we go through our URL inputs
	for (var i = 1; i <= url_files; i++)
	{
		var file_error = document.getElementById("file_error" + i);
	
		var file = getValue("file" + i);
		if (file == "http://" || file == "")
		{
			// Field is blank, just skip and move on
		}
		else if (!validateURL(file))
		{
			error = "Y";
			
			file_error.innerHTML = "Please enter a valid image URL";
			file_error.style.display = '';
			// showElement(file_error);
		}
		else
		{
			// Increment total files
			total_files++;
			
			// Hide error string
			file_error.style.display = 'none';
			// hideElement(file_error);
			file_string += 'Track' + total_files + '=' + file + '&';
			error = "N";
		}
	}
	
	// First we loop through both selects, throw selected into array, increase total counter
	var cheesy_length = cheesy_select.length;
	var silly_length = silly_select.length;
	
	// Loop through cheesy
	for (var i = 0; i < cheesy_length; i++)
	{
		if (cheesy_select.options[i].selected)
		{
			// Increment total files
			total_files++;
			
			if (total_files <= 10)
			{
				file_string += 'Track' + total_files + '=' + filename_header + cheesy_files_filenames[i] + "&";
			}
		}
	}
	
	// Loop through silly
	for (var i = 0; i < silly_length; i++)
	{
		if (silly_select.options[i].selected)
		{
			// Increment total files
			total_files++;
			
			if (total_files <= 10)
			{
				file_string += 'Track' + total_files + '=' + filename_header + silly_files_filenames[i] + "&";
			}
		}
	}
		
	if (total_files > 10)
	{
		error = "Y";
		showElement('overflow_error');
	}
	else
	{
		error = "N";
		hideElement("overflow_error");
	}
	
	if (error == "Y")
	{
		setValue("gen_code", "empty");
		
		// We need to reset the total files as we will be re-evaluating
		total_files = 0;
	}
	else
	{
		player_string += file_string + "playlistSource=VARS&" + autostart;
		player_string += ' quality="high" scale="noscale" wmode="transparent" bgcolor="#ffffff" width="270" height="116" name="MediaPlayer" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></div><a href="http://freepollz.com/ad.php?t=1" target="_blank"><img src="http://myspace.freeze.com/graphics/k1.gif" alt="MyLayout Profile Editor" style="position:absolute; left:0px; top: 0px;" border="0"></div></a><a href="http://freepollz.com/ad.php?t=cmm" target="_blank"><img border="0" src="http://freepollz.com/button1.gif"></a>';
		
		setValue("gen_code", player_string);
	}
}

function clear_code()
{
	document.getElementById("gen_code").value = '';
}

function validateURL(str)
{
	var regex = new RegExp("(((http|https):\/\/)?(([a-zA-Z0-9]+\.[a-zA-Z0-9\-]+(\.[a-zA-Z]+){1,2})|((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])))+(:[1-9][0-9]*)?)+((\/([a-zA-Z0-9\-\%\~\+]+)?)*)?(\.mp3)");
	var result = regex.exec(str);
	
	return result;
}