
/*--- MyProjectList Sort --------------------*/
function OnChangeSortType(dropdown) {

    var myindex  = dropdown.selectedIndex
    var SelValue = dropdown.options[myindex].value
    
    top.location.href = SelValue;
    
    return true; 
}

/*--- List Detail Sort --------------------*/
function onChangeSortTypeSubmit(dropdown) {
    document.registry.submit(); 
}

/*--- Check all functionality for check box --------------------*/


/*--- TODO: this  type="checkbox" onClick="checkUncheckAll(this);" class="form_checkbox" name="checkAll" value="" />
      should  not be  on the jsp, add event using jquery and after use Util.js
  ---*/
function checkUncheckAll(e) {

     var theForm = e.form, x = 0;
     for(x=0; x<theForm.length;x++){
         if(theForm[x].type == 'checkbox' && theForm[x].name != 'checkall' &&  theForm[x].name != 'isPrivate'){
            theForm[x].checked = e.checked;
         }
     }
}



/*--- Assumes that jquery.js  and Util.js are available  ----*/



/*------------- WishList -------------------------------*/

// Adding events  to  elements  the jquery way.
 
// $(document).ready lauches BEFORE onload() event for the page
// it's a jquery function that executes  when the dom is ready to be  manipulated.
// It does not wait for big images.
// Perhaps  we need a better naming convention so we 
// do not have  name space name  collisions

$(document).ready (
  function() {
       $("#toggleListBtn1").click( function(event) {  
           toggleListScroller(event);
           return  false
       });
       
       $("#addItemMainFormFloating").submit(function(event) {
           var registryIdSelected = $("input[@name=registryId]:checked").val();
           var newList = registryIdSelected == "new_list"; 
    
           if (newList && jQuery.trim(($("#new_wishlist_name").val())).length == 0) {
               $("#errorMessage").html("Please specify a name for the new list.");
               $("#importantMessageBox").show();
               return false; 
           }
           
           return true;
       });
       
       $("#cancelLink").click( function(event) {  
            toggleListScroller(event);
            return  false
       });
      
       $("#createNewListBtnFloating").click(function (){ doCreateNewListRequest();return  false;});
  
       //turn on javascript menu for sortBy functionality [My ProjectList page]
       $("#sortByMenuJS").css("display","block");

       if (Util.isIE7P || Util.isIE) { 
           document.getElementById("sortByMenuJS").name="sortBy";
       } else {
           $("#sortByMenuJS").attr("name", "sortBy");
       }
 
  }
);

function toggleListScroller(event) {
    $("#importantMessageBox").hide();
 
    var offset = $('#toggleListBtn1').offset();
   
    var box =  $("#listOfWishlistFloating");

    // 22 is  the height of the button, we want to the box right below
    box.css("top", offset.top + 22);
    box.css("left", offset.left);
       
 
    box.toggle();
    
}

function addItemToListRequest() {
    // Obtain the selected registryId from the radio buttons selected
    
    var formData = $("#addItemMainFormFloating").serialize();
    
    jQuery.ajax({
         data: formData,
         
         dataType: 'html',                     
         
         error: function (xhr, textStatus, errorThrown) {
            //xhr.status == 302 will happen when log in is required(or the page did move)
            if(xhr.status == 302) {
                window.location = xhr.getResponseHeader("Location");
            } else {
                $("#errorMessage").html(xhr.status + " - " + xhr.statusText);
                $("#importantMessageBox").toggle();
            }                
         },
          
         success: function(data, textStatus) {    
            if (newList) {
                document.location.reload();
            } else {        
                $('#listOfWishlistFloating').toggle();
            }                
         },
         
         type: 'POST',
         url: '/wishlistAddItemsAjax'
    });

}
