function getImageName() {
	var min=0, max = 24;
	var images = new Array();

	images [0] = 'beach-stones-01h.jpg';
	images [1] = 'boat-the-sea-01h.jpg';
	images [2] = 'boat-beach-01h.jpg';
	images [3] = 'boat-lake-01h.jpg';
	images [4] = 'corn-field-01h.jpg';
	images [5] = 'cotton-field-01h.jpg';
	images [6] = 'dark-forest-01h.jpg';
	images [7] = 'forest-lake-01h.jpg';
 	images [8] = 'mountain-snow-01h.jpg';
	images [9] = 'mountain-trees-01h.jpg';
	images [10] = 'path-to-lake-01h.jpg';
	images [11] = 'roses-field-01h.jpg';
	images [12] = 'sea-coast-01h.jpg';
	images [13] = 'snow-mountain-lake-01h.jpg';
	images [14] = 'sun-beach-afternoon-h01.jpg';
	images [15] = 'forest-lakex-01h.jpg';
	images [16] = 'green-lake-01h.jpg';
    images [17] = 'margaritas-field-01h.jpg';
	images [18] = 'mountain-lake-01h.jpg';
	images [19] = 'mountain-snow-dark-01h.jpg';
	images [20] = 'mountains-field-01h.jpg';
	images [21] = 'P6045007-01h.jpg';
	images [22] = 'snow-marca01h.jpg';
	images [23] = 'sea-01h.jpg';
	
	var index = Math.floor(Math.random()*max + min);

	var image = "url(images/header/" + images[index]+")";
	//var image = "url(images/header/" + images[22]+")";
	//alert(image + " " + index);
	var header = document.getElementById("header");
	header.style.backgroundImage=image;
//	alert(image);
	//header.style.height="192px";
	//header.style.width ="960px";
}
 
function play(aSoundCode, aSoundFile, aPlaysCode, aUsesCode) {
	// var playCode  = "play" + aSoundCode;
	var currentSound = soundManager.getSoundById(aSoundCode);
	//alert(aPlaysCode +" "+ aUsesCode);
	if (currentSound==null) {
		soundManager.stopAll();
		var playButtons = getElementByClass("playButtons");
		//alert(playButtons);
	//	for (i=0; i<playButtons.length; i++) {
			playButtons.src="images/icons/Play Green Button.png";
	//	}	

		changeStateToStop(aPlaysCode);
		if (!soundManager.canPlayURL(aSoundFile)) {
			alert("Sound manager cannot play " + aSoundFile);
			return;
		}
		var soundObject = soundManager.createSound({
			 id:aSoundCode,
			 url:aSoundFile,
			 onfinish: function() {
				changeStateToPlay(aPlaysCode);
				this.destruct();
			  },
			 onstop: function() {
				changeStateToPlay(aPlaysCode);					 
				this.destruct();
			  }
		});
		soundObject.play();
		//alert("Debe cambiar imagen aqui");
		changeStateToStop(aPlaysCode);
		setTimeout("updateData('updatePlays.action', '"+ aSoundCode +"', '#" +aUsesCode +"')", 2000);
		//updateData("updatePlays.action", aSoundCode);
	} else {
		currentSound.destruct();
		changeStateToPlay(aPlaysCode);
	}	

}

function download(aSongCode, aDownloadButton, aDownloadCounter) {
	setTimeout("updateData('updateDownloads.action', '#" + aDownloadCounter +"', '"+ aSongCode +"')", 2000);
}

var globalIndividualPay = false;
function updateDownloads(event, aSoundCode, aIdUsesCode, aIdDownloadsCode) {
   // alert(globalIndividualPay);
	if (event.button==2)   {
/*    alert("You clicked the right mouse button!");  */
	  setTimeout("updateData('updateDownloads.action', '"+ aSoundCode +"', '#"+ aIdDownloadsCode +"')", 500);
	}
	else {
		if ( !globalIndividualPay ) {
			globalIndividualPay  = true;
			setTimeout("updateData('updatePlays.action', '"+ aSoundCode +"', '#"+ aIdUsesCode +"')", 1000);
		}	
		//alert("You clicked the left mouse button!");	  
	}
	
}


function changeStateToPlay(aPlayCode) {
	var imageStatus = document.getElementById(aPlayCode);
	imageStatus.src="images/icons/Play Green Button.png";
	imageStatus.style.border="0";
}
function changeStateToStop(aPlayCode) {
	var imageStatus = document.getElementById(aPlayCode);
	imageStatus.src="images/icons/Stop-Green-Button.png";
	imageStatus.style.border="1px solid green";
}

function getElementByClass(theClass) {
	var allHTMLTags=document.getElementsByTagName("img");
	var classCollection = new Array();
	for (i=0; i<allHTMLTags.length; i++) {
		if (allHTMLTags[i].className==theClass) {
			classCollection[i] = allHTMLTags[i];
		}
	}
	return classCollection;
}


function shareTherapy(aSongCode, aShortTitle, aVotesCode, aLink, aLongTitle) {
	//alert("Entrando a shareTherapy: aSongCode= " + aSongCode + "\nshortTitle "+ aShortTitle + "\naVotesCode " + aVotesCode + "\naLink "+ aLink +"\naLongTitle" + aLongTitle);
	$('#theraphyTitle').html( "Sharing: " + aShortTitle );
	$('#link').val( aLink );
	$('#messagex').val( aLongTitle );
	$('#description').val( aLongTitle );
	$('#pSongCode').val( aSongCode );
	$('#pIdShareCode').val( aVotesCode );
	

	$('#formDialog').dialog('open');
//	updateData('updateVotes.action', '#' + aVotesCode, aSongCode);
//	setTimeout("updateData('updateVotes.action', '#" + aVotesCode +"', '"+ aSongCode +"')", 1000);
}

/*-- Ajax dialog --*/
	$(function() {
	
	    var fromName  = $("#fromName"),
	    	fromEmail = $("#fromEmail"),
	    	toEmail   = $("#toEmail"),
	    	message   = $('textarea[name="messagex"]'), 
	    	link      = $("#link"),
	    	description  = $("#description"),   
	    	dialogSum1 = $("#sum1"),
	    	dialogSum2 = $("#sum2"),
	    	clientSum   = $("#clientSum"),
	    	pSongCode = $('#pSongCode'),
	    	pIdShareCode = $('#pIdShareCode'),
	    	allFields = $([]).add(fromName).add(fromEmail).add(toEmail).add(message).add(clientSum).add(pSongCode).add(pIdShareCode),
	    	tips = $("#validateTips");
	    					
	    
		function updateTips(t) {
			tips.text(t).effect("highlight",{},3000);
		}
	
		function checkNotEmpty(o,n) {
			if ( o.val() == null || o.val().length == 0 ) {
				o.addClass('ui-state-error');
				updateTips(n + " must be introduced.");
				o.focus();
				return false;
			} else {
				return true;
			}
		}
	
		function checkRegexp(o,regexp,n) {
			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				o.focus();
				return false;
			} else {
				return true;
			}
		}
		
		function checkLength(o, n, len){
			if ( o.val().length  > 140 ) {
				o.addClass('ui-state-error');
				updateTips(n);
				o.focus();
				return false;			
			} else {
				return true;
			}
		}
		
		function checkSum(o, s1, s2, n){
			var sum1 = parseInt (s1.text());
			var sum2 = parseInt (s2.text());
			var sumRight = sum1 + sum2;
			var  sumClient = parseInt (o.val()); 
			
			if ( isNaN(sumClient) || (sumClient != sumRight) ) {
				//alert("sumRightx " + sumRight + " clientSumx " + sumClient);
				o.addClass('ui-state-error');
				updateTips(n);
				o.focus();
				return false;			
			} else {
				return true;
			}
		}		
		
		$("#formDialog").dialog({
			bgiframe: true,
			autoOpen: false,
			width:  350,
			height: 450,
			modal: true,
			open: function() {
				var sum1 = Math.floor(Math.random()*10 + 1);
				var sum2 = Math.floor(Math.random()*10 + 1);
				var sumTotal = sum1 + sum2;
				$("#sum1").text(sum1);
				$("#sum2").text(sum2);
			},			
			buttons: {
				'Send': function() {
				   var bValid = true;
				   allFields.removeClass('ui-state-error');
	/*					
	fromName  fromEmail toEmail  message link description allFields
	*/
	             if (!checkNotEmpty(fromName,"Your Name")) {
				 	return false;
				 }
	           	 if (!checkNotEmpty(fromEmail,"Your Email must be introduced")){
					return false;
				 }
				 var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
	           	 if (!checkRegexp(fromEmail, pattern, "Your Email must be a valid one")){
					return false;
				 }					
	           	 if (!checkNotEmpty(toEmail,"His/Her Email  must be introduced")){
					return false;
				 }					
	           	 if (!checkRegexp(toEmail, pattern, "His/Her Email must be a valid one")){
				    return false;
				 }                
			     /*     if (!checkLength(message,"Message"," must have Maximum 140 characters")) {
				   return false;
	           	} */
		         if (!checkSum(clientSum, dialogSum1,  dialogSum2,  "Your sum is wrong, try again")) {
					 return false; 
				 }		        	 
	
				if (bValid) {
	               	/*					
					alert(
							"\nfromName : "+ fromName.val() +  
							"\nfromEmail: "+ fromEmail.val() + 
							"\ntoEmail:   "+ toEmail.val()   +
							"\nmessage text:  "+ message.text()   +
							"\nmessage val:   "+ message.val()   +
							"\nmessage htmlx:  "+ message.html()   +
							 $('textarea[name="messagex"]').attr('value') +
							"\nlink:      "+ link.val()      +
							"\ndescription: "+ description.val()
					);
					*/		
				   if (message.val() == "Enter your comment here...") {
					   message.val('');
				   }   
					
				//	alert("pSongCode " + pSongCode + " pIdShareCode " + pIdShareCode );
				   semdMail("jspf/shareTherapy.jsp", fromName.val(), fromEmail.val(), toEmail.val(), message.val(), link.val(), description.val() ); 
				  // updateData("updateVotes.action", pSongCode, "#" + pIdShareCode);
				   updateTips('Your message was submited');
				   setTimeout("updateData('updateVotes.action', '"+ pSongCode.val() +"', '#" +pIdShareCode.val() +"')", 1000);
				   
				   //setTimeout("closeDialog()",1000);
				   alert('Your message has been submited');
				   $(this).dialog('close');		  
				}
					
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
				updateTips('');
			}
		});

	});

	function semdMail(aJSP, aFromName, aFromEmail, aToEmail, aMessage, aLink, aDescription ) {
		//alert("Entring update data " + aJSP +" "+ aID +" "+ aSongCode);
		$("#validateTips").load(aJSP, {
			fromName: aFromName,   
			fromEmail: aFromEmail,  
			toEmail: aToEmail,
			message: aMessage,  
			link:    aLink, 
			description: aDescription 
		});
	}
	
	function countDown(control, maxLen, counter, typeName) {
	       var len = control.value.length;
	       var txt = control.value;
	       var span = document.getElementById(counter);
	       span.style.display = '';
	       span.innerHTML = (maxLen - len) + ' characters remaining';
	       if (len >= (maxLen - 10)) {
	            span.style.color = 'red';
	            if (len > maxLen) {
	                 control.innerHTML = txt.substring(0, maxLen);
	                 span.innerHTML = (maxLen - control.value.length) + ' characters remaining';
	                 alert(typeName + ' text exceeds the maximum allowed!');
	            }
	       } else {
	            span.style.color = '';
	       }
		 }
	/*-- Ajax dialog --*/	
	
	function validateFormMessage() {
		//alert("Entring validateFormContact " + showComments +" "+ origin);
		$('.error').hide();
		$("#result").hide();

		var name = $("#name").val();
		if (name.length == 0) {
			$("#name_error").show();
			$("#name").focus();
			return false;
		}

		var email = $("#email").val();
		if (email.length == 0) {
			$("#email_error").show();
			$("#email").focus();
			return false;
		}

		var email1 = $("#email").val();
		var pattern = /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
		if (!pattern.test(email1)) {
			$("#email_error").show();
			$("#email_error").innerText = "Must provide a valid email";
			$("#email").focus();
			return false;
		}
		
		var message = $("#message").val();
		if (message == null || message.length == 0) {
			$("#message_error").show();
			$("#message").focus();
			return false;
		} else if (message.length > 250) {
			alert("You have " + message.length
					+ " characters, the maximum is 250 ");
			$("#message_error").val("Maximum 250 characters");
			$("#message_error").show();
			$("#message").focus();
			return false;
		}
		//	alert("There are " + message.length + " characters ");

		$("p#result").html("<b>Your message was submited</b>");
		$("p#result").show().fadeOut(3000);

		return true;

	}	


	function updateBookReads(aBookCode, aReadsId) {
	//	alert ("updateBookReads: aBookCode " + aBookCode + " aReadsId " + aReadsId);
	//	setTimeout("updateData('updateBookReads.action', '" + aBookCode  +"', '#"+ aReadsId +"')", 1);
	     updateData('updateBookReads.action', aBookCode  , '#'+ aReadsId );
	}
	 
	function updateBookShares(aBookCode, aSharesId) {
		//alert ("updateBookShares: aBookCode " + aBookCode + " aSharesId " + aSharesId);
		setTimeout("updateData('updateBookShares.action', '" + aBookCode  +"', '#"+ aSharesId +"')", 2000);
		
	}

	function updateBookDownloads(aBookCode, aDownloadsId) {
		//alert ("updateBookDownloads: aBookCode " + aBookCode + " aReadsId " + aDownloadsId);
		setTimeout("updateData('updateBookDownloads.action', '" + aBookCode +"', '#"+ aDownloadsId +"')", 2000);
	}	
	
	function updateData(aJSP, aSoundCode, aIdSoundCode) {
		$(aIdSoundCode).load(aJSP, {
			psoundcode :aSoundCode
		});
		//alert ("updateData: aBookCode " + aSoundCode + " aReadsId " + aIdSoundCode);
	}