
/* groupsChecked and groupsUnchecked are expected to be a javascript array encoded as json string */
function shareToGroups( idOfResource, groupsChecked, groupsUnchecked ) {
	jQuery.ajax({
        url: "/AloeView/ajax/shareToGroups",
        type: 'get',  // get is the default type
        data: {
	        resourceId: idOfResource,
	        groupsToShareTo: groupsChecked,
	        groupsToResignFrom: groupsUnchecked
		},
		success: function( data, textStatus, jqXHR ) {
	        var form = document.getElementById("share_to_group_form");
	        form.submit();
		},
		error: function( jqXHR, textStatus, errorThrown ) {
			// do nothing
		}
	});
}
/* setsChecked and setsUnchecked are expected to be a javascript array encoded as json string */
function changeSetAffiliation( idOfResource, setsChecked, setsUnchecked ) {
	jQuery.ajax({
        url: "/AloeView/ajax/changeSetAffiliation",
        type: 'get',  // get is the default type
        data: {
	        resourceId: idOfResource,
            setsToAddTo: setsChecked,
            setsToRemoveFrom: setsUnchecked
		},
		success: function( data, textStatus, jqXHR ) {
	        var form = document.getElementById("add_to_set_form");
	        form.submit();
		},
		error: function( jqXHR, textStatus, errorThrown ) {
			// do nothing
		}
	});
}	

function rateResource( idOfResource, ratingValue, clickEvent ) {
	jQuery.ajax({
        url: "/AloeView/ajax/rateResource",
        type: 'get',  // get is the default type
        data: {
	        resourceId: idOfResource,
            ratingValue: ratingValue
		},
		success: function( data, textStatus, jqXHR ) {
			var response = data;
        	if ( response != "" ) {
              document.getElementById('ratingElement').innerHTML = response;
        	}
        	else {
	        	var errorString = global_aloeTranslations['ratingFailed'];
        		popupAtMouse(errorString,clickEvent,-20,-10);
        	}
		},
		error: function( jqXHR, textStatus, errorThrown ) {
        	var errorString = global_aloeTranslations['ajaxFailed'];
    		popupAtMouse(errorString,clickEvent,-20,-10);
		}
	});
}

function addComment( idOfResource, commentToAdd ) {
	jQuery.ajax({
        url: "/AloeView/ajax/addComment",
        type: 'get',  // get is the default type
        data: {
	        resourceId: idOfResource,
            commentToAdd: commentToAdd
		},
		success: function( data, textStatus, jqXHR ) {
			var response = data;
	        if ( response != "" ) {
	            addNewComment( response );
	            document.getElementById('user_comment').value = '';
	        }
		},
		error: function( jqXHR, textStatus, errorThrown ) {
	        // do nothing
		}
	});
}

function extractCheckboxValues( formId, expectedCheckboxName, currentState ) {

    var toReturn = new Array();
    var form = document.getElementById( formId );
    
    for ( i=0; i<form.elements.length; i++ ) {
        var checkbox = form.elements[i];
        if ( checkbox.name == expectedCheckboxName ) {
            if ( ( currentState && checkbox.checked ) || !( currentState || checkbox.checked ) ) {
            	toReturn.push( checkbox.value );
            }
        }
    }
    return( JSON.stringify( toReturn ) );
}

function extractSetIdsToAddTo() {
	return( extractCheckboxValues( "add_to_set_form", "add_to_set", true ) );
}
function extractSetIdsToRemoveFrom() {
	return( extractCheckboxValues( "add_to_set_form", "add_to_set", false ) );
}


function extractGroupIdsToShare() {
	return( extractCheckboxValues( "share_to_group_form", "share_to_group", true ) );
}
function extractGroupIdsToResign() {
	return( extractCheckboxValues( "share_to_group_form", "share_to_group", false ) );
}
		      
              function addNewComment( newComment ) {
                  var newElement = document.createElement( "li" );
                  newElement.innerHTML = newComment;
                  
                  var unsortedList = document.getElementById('commentList');
                  
                  var topmostComment = unsortedList.firstChild;
                  if ( topmostComment == null ) {
                      unsortedList.appendChild( newElement );
                  }
                  else {
                      unsortedList.insertBefore( newElement, topmostComment );
                  }
               }
               
               /* Get value property of next preceding sibling of type Element. If
                  no such sibling exists return empty string */
               function getValueOfNextPrecedingSiblingElement( start ) {
                   var current = start;
                   while( current.previousSibling != null ) {
                       current = current.previousSibling;
                       if ( current.nodeType == 1 /*Element node*/ ) {
                           return( current.value );
                       }
                   }
                   return( "" );
               }
               

               /* Toggles the display of the groups a resource is contained in */
               
               function toggleInGroupsBlock() {
                   var element1 = document.getElementById( 'createInGroupsLongBlock' );
                   var element2 = document.getElementById( 'createInGroupsShortBlock' );
                  if ( element1 != null && element2 != null) {
                       var nextVisibility1 = element1.style.display == "none" ? "block" : "none";
                       var nextVisibility2 = element2.style.display == "none" ? "block" : "none";
                       var nextImage = element1.style.display == "none" ? "../images/functionalIcons/basic/minus.gif" : "../images/functionalIcons/basic/plus.gif";

                           var theImage = document.getElementById( 'plusMinusInGroups' );
                           if ( theImage != null ) {
                           theImage.src = nextImage;
                       }
                       element1.style.display = nextVisibility1;
                       element2.style.display = nextVisibility2;
                   }
               }

               
               /* Toggles the display of the collections a resource is contained in */
               
               function toggleInSetsBlock() {
                   var element1 = document.getElementById( 'createInSetsLongBlock' );
                   var element2 = document.getElementById( 'createInSetsShortBlock' );
                  if ( element1 != null && element2 != null) {
                       var nextVisibility1 = element1.style.display == "none" ? "block" : "none";
                       var nextVisibility2 = element2.style.display == "none" ? "block" : "none";
                       var nextImage = element1.style.display == "none" ? "../images/functionalIcons/basic/minus.gif" : "../images/functionalIcons/basic/plus.gif";

                           var theImage = document.getElementById( 'plusMinusInSets' );
                           if ( theImage != null ) {
                           theImage.src = nextImage;
                       }
                       element1.style.display = nextVisibility1;
                       element2.style.display = nextVisibility2;
                   }
               }

               
               /* Toggles the display of the associated metadata sets for a resource */
               
               function toggleAssociatedMetadataSetsBlock() {
                   var element1 = document.getElementById( 'createAssociatedMetadataSetsLongBlock' );
                   var element2 = document.getElementById( 'createAssociatedMetadataSetsShortBlock' );
                  if ( element1 != null && element2 != null) {
                       var nextVisibility1 = element1.style.display == "none" ? "block" : "none";
                       var nextVisibility2 = element2.style.display == "none" ? "block" : "none";
                       var nextImage = element1.style.display == "none" ? "../images/functionalIcons/basic/minus.gif" : "../images/functionalIcons/basic/plus.gif";

                           var theImage = document.getElementById( 'plusMinusAssociatedMetadataSets' );
                           if ( theImage != null ) {
                           theImage.src = nextImage;
                       }
                       element1.style.display = nextVisibility1;
                       element2.style.display = nextVisibility2;
                   }
               }

               /* Toggles the display of the classifications for a resource */
               
               function toggleResourceClassificationBlock() {
                   var element1 = document.getElementById( 'createResourceClassificationLongBlock' );
                   var element2 = document.getElementById( 'createResourceClassificationShortBlock' );
                  if ( element1 != null && element2 != null) {
                       var nextVisibility1 = element1.style.display == "none" ? "block" : "none";
                       var nextVisibility2 = element2.style.display == "none" ? "block" : "none";
                       var nextImage = element1.style.display == "none" ? "../images/functionalIcons/basic/minus.gif" : "../images/functionalIcons/basic/plus.gif";

                           var theImage = document.getElementById( 'plusMinusResourceClassification' );
                           if ( theImage != null ) {
                           theImage.src = nextImage;
                       }
                       element1.style.display = nextVisibility1;
                       element2.style.display = nextVisibility2;
                   }
               }

               function createQRCode(){
            	   var form = document.getElementById( 'create_qr_code_form' );
            	   
//            	   var actionLink = "/AloeView/ajax/createQRCodeForURI?resourceUri="
            	   var actionLink = "/AloeView/action/createQRCode?resourceId="
            		+ form.qrCodeResourceId.value + "&resourceType="
            		+ form.qrCodeResourceType.value + "&qrCodeType="
            	   	+ getSelectedButtonValue(form.qrCodeType) 
            	   	+ "&qrCodeSize=500";
            	   	
            	   	window.open(actionLink,"_blank");
               }

