var preloadFlag = false;
function getTheSong(songId, playlistId, attempt)
{
	var c = true;
	var strConfirmMsg = new String("You have already downloaded this song twice.\nif you download it again, you will be charged a credit.\nAre you sure you want to download this song?");
	if (attempt > 1)
	{
		return confirm(strConfirmMsg);
	}
}

function getSong(x,y)
	{
	var theDiv = document.getElementById('div_' + y);
	var theText = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0" width="65" height="20">';
	theText +='<param name="movie" value="flv.swf?songName=http://stream.freemusicdownloadspot.com/' + x + '&Play=1">';
	theText +='<param name="quality" value="high">';
	theText +='<param name="allowScriptAccess" value="sameDomain">';
	theText +='<param name="wmode" value="transparent">';
	theText +='<embed src="flv.swf?songName=http://stream.freemusicdownloadspot.com/' + x + '&Play=1" allowScriptAccess="sameDomain" wmode="transparent"  quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="65" height="20"></embed>';
	theText +='</object>';
	theDiv.innerHTML = theText;
	var theOldDiv = document.getElementById('txtTheDiv').value;
	var theOldLink = document.getElementById('txtTheLink').value;
	if(theOldDiv > 0)
		{
		var theDiv = document.getElementById('div_' + theOldDiv);
		var theText = '<a href="" onclick="getSong(\'' + theOldLink + '\', ' + theOldDiv + ');return false;" ';
		theText +='onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage(\'play_' +  theOldDiv + '\',\'\',\'images/listen_over.gif\',1)"><img src="images/listen.gif" name="play_' + theOldDiv + '" width="53" height="17" border="0" id="play_' + theOldDiv + '" /></a>';
		theDiv.innerHTML = theText;
		}
	document.getElementById('txtTheDiv').value = y;
	document.getElementById('txtTheLink').value = x;
	return false;
	}
	
	function preloadImages() {
			if (document.images) {
				pre_listen_on = newImage("images/listen_on.gif");
				preloadFlag = true;
			}
		}
		
	function checkAll(field){
					
		if(!field)
			{
			alert('There are no remaining songs to select');
			return false;	
			}
	
		for (i = 0; i < field.length; i++)
				field[i].checked = true ;
		}
	
	
		function uncheckAll(field){
			for (i = 0; i < field.length; i++)
				field[i].checked = false ;
		}
		
		function validateForm(){
			var isGood = false;
			var checkCounter = 0;
			for (i = 0; i < document.myform.songCheck.length; i++){	
				if(document.myform.songCheck[i].checked == true){
					 checkCounter += 1;	
				}
			}
			if(checkCounter > 0){
				isGood = true;					
			}			
			if(isGood == false)
				alert("Please select 1 or more songs.");
			else{
				document.myform.submit();
			}
		}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function countCheck(theCheckbox)
	{
	var theCount = 0;
	var di = 0;
	theForm = document.getElementById('myform');
	for(i=0; i<theForm.elements.length; i++)
		{
		if(theForm.elements[i].type == 'checkbox' && theForm.elements[i].checked == true)
			{
			theCount +=1;
			if(theCount > 5)
				{
				if(di == 0)
					{		
					alert('You can not download more than 5 songs at a time.');
					theCheckbox.checked = false;
					theCount -=1;
					di =1;
					}
				theForm.elements[i].checked = false;	
				}
			
			}
		}
	}

function clickDownload(credits)
	{
	var theCount = 0;
	var failed = 0;
	theForm = document.getElementById('myform');
	for(i=0; i<theForm.elements.length; i++)
		{
		if(theForm.elements[i].type == 'checkbox' && theForm.elements[i].checked == true)
			{
			theCount +=1;
			if(theCount >credits)
				{
				failed = 1;	
				}
			}
		}
	if(failed == 0)
		{
		window.open('./retrievingfile.html', 'PostForm', 'width=400,height=180');	
		validateForm();
		}
	else
		{
		alert('You do not have enough credits to download the number of songs you have selected.\nPlease remove a few selections');
		return false;
		}
	
	
	}