

  $(document).ready(function(){

    $("#Title").autocomplete("index.php?option=com_musicadvisor&view=suggestalbumtitle&format=raw", {
		selectFirst: false,
		multiple: false,
		formatResult: formatResult,
		formatItem: formatItem,
		mustMatch: true,
		width: 440
	});

	function formatItem(row) {
		return row[0] + " (<strong>" + row[2] + "</strong>)";
	}

	function formatResult(row) {
		return row[0].replace(/(<.+?>)/gi, '');
	}

	function indexMainVersion(id){
		var options = '';

		//index main version
	    $.get("index.php?option=com_musicadvisor&view=indexmainversion&format=raw",
	      { idalbum: id },

	      function(n) {options = '<option value="' + n + '">Other Version...</option>';});

		jAlert ("Now, you are able to select the version!", 'MusicAdvisor');
		return options;


	}

    $("#Title").result(function(event, data, formatted) {
		var hidden = $("#IdAlbum");
		hidden.val(data[1]);
		 //versions
		    $.getJSON("index.php?option=com_musicadvisor&view=jsonversion&format=json",
		    {idalbum: hidden.val()}, function(j){
		    var options = '';
		      for (var i = 0; i < j.length; i++) {
		        options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
		      }
		       $("#IdReleased").html(options + indexMainVersion(hidden.val()));
	    	});

    });

	//controlla se già scritto una review
    $("#IdReleased").change(function () {

    //disabilita il controllo MyFormat se seleziono 'Other Version'
      	if ($("#IdReleased option:selected").text() == 'Other Version...')
			{$("#MyFormat").attr('disabled', 'disabled');
			 $("#ov").val(1);}
		else
			{$("#MyFormat").removeAttr('disabled'); $("#ov").val(0);}

    		idr = $("select option:selected").val();
    		ov = $("#ov").val();
    		c = $("#comment").val();
    		mv = $("#myversion").val();
		    	//alert("myversion: " + mv + " idreleased: " + idr + " ov: " + ov);
		    if ( idr != "" )
		    {
		    	$.getJSON("index.php?option=com_musicadvisor&view=checkrevrat&format=json",
		    	{IdReleased: idr, c: c, ov: ov}, function(n){

		    		if (n.value == "1" && c == 0 && (mv != idr || ov == 1))
		    		{
			    		$("#checkresult").css({'color': '#FF0000', 'text-align': 'center', 'font': 'Arial', 'font-size': 'x-small'});
			    		$("#checkresult").text("You've already written a review for this version, you are able to write a review for another version of this album!");
		    		}
		    		else if (n.value == "1" && c == 1 && (mv != idr || ov == 1))
		    		{
			    		$("#checkresult").css({'color': '#FF0000', 'text-align': 'center', 'font': 'Arial', 'font-size': 'x-small'});
			    		$("#checkresult").text("You've already rated this version, you are able to rated another version of this album!");
		    		}
		    		else
		    		{
			    		$("#checkresult").css({'color': '#626200', 'text-align': 'center', 'font':'inherit', 'font-size':'inherit'});
			    		$("#checkresult").text("..:::::..");
		    		}
		    	 });

		    }

    });

		$('#formrat').bind('submit', function(){

	if (document.getElementById('content').value == '')
						{
							jAlert('Please, first write your comment!!', 'MusicAdvisor');
							return false;
						}
						else if (document.getElementById('Title') != null && document.getElementById('Title').value == '' ){
						 	jAlert ('Please, write album title!!', 'MusicAdvisor');
						 	return false;
						 }
						else if (document.getElementById('IdReleased').value == ''){
						 	jAlert ('Please, select an album version!!', 'MusicAdvisor');
						 	return false;
						 }
						 else if (document.getElementById('rating').innerHTML == '0.0'){
						 	jAlert ('Please, give your rating!!', 'MusicAdvisor');
						 	return false;
						 }
						 else if(document.getElementById('checkresult').innerHTML != '..:::::..'){
						 	jAlert ('You\'ve already rated this version, you are able to rated another version of this album.!', 'MusicAdvisor');
						 	return false;
						 }
						 else
						 {
						 	return true;
						 }


		});

		$('#formrev').bind('submit', function(){

		if( tinyMCE.get('content').getContent()  == '' )
			{
				jAlert('Please, first write your review!!', 'MusicAdvisor');
				return false;
			}
			else if (document.getElementById('Title') != null &&  document.getElementById('Title').value == ''){
			 	jAlert ('Please, write album title!!', 'MusicAdvisor');
			 	return false;
			 }
			else if (document.getElementById('IdReleased').value == ''){
			 	jAlert ('Please, select an album version!!', 'MusicAdvisor');
			 	return false;
			 }
			 else if (document.getElementById('rating').innerHTML == '0.0'){
			 	jAlert ('Please, give your rating!!', 'MusicAdvisor');
			 	return false;
			 }
			 else if(document.getElementById('checkresult').innerHTML != '..:::::..'){
			 	jAlert ('You\'ve already writed a review for this version!', 'MusicAdvisor');
			 	return false;
			 }
			 else
			 {
			 	return true;
			 }


		});



   });



