(function() {
   // Work around http://dev.jqueryui.com/ticket/4186
   var oldSetData = $.ui.resizable.prototype._setData;
   $.ui.resizable.prototype._setData = function(key, value) {
      oldSetData.apply(this, arguments);
      if (key === "aspectRatio") {
         this._aspectRatio = !!value;
      }
   };

   // Fix for nested draggables
   // http://dev.jqueryui.com/ticket/4333
   $.extend($.ui.draggable.prototype, (function (orig) {
     return {
       _mouseCapture: function (event) {
         var result = orig.call(this, event);
         if (result && $.browser.msie) event.stopPropagation();
         return result;
       }
     };
   })($.ui.draggable.prototype["_mouseCapture"]));

   $.extend($.fn.disableTextSelect = function() {
      return this.each(function(){
         if($.browser.mozilla){//Firefox
            $(this).css('MozUserSelect','none');
         }else if($.browser.msie){//IE
            $(this).bind('selectstart',function(){return false;});
         }else{//Opera, etc.
            $(this).mousedown(function(){return false;});
         }
      });
   });

   $.preLoadImages = function() {
      var args_len = arguments.length;
      for (var i = args_len; i--;) {
         var cacheImage = document.createElement('img');
         cacheImage.src = arguments[i];
         FLUIDIMAGECACHE.push(cacheImage);
      }
   }

})();


var DUCATI = {
   jQuery : $,
   
   settings : {
      darkButtonOn : {
         'background-color' : '#555555',
         'cursor': 'pointer'
      },
      darkButton : {
         'background-color' : '#333333'
      }
   },

   init : function () {
      this.initializeLayout();

      var splashImages = $('.splashImage').size();
      if (splashImages > 0)
         initializeSplashImages(splashImages);
   },

   preloadHoverImages : function() {
      var preload = new Array();
      $(".imgHover").each(function() {
         s = $(this).attr("src").replace(/\.(.+)$/i, "on.$1");
         preload.push(s)
      });
      var img = document.createElement('img');
      $(img).bind('load', function() {
         if(preload[0]) {
            this.src = preload.shift();
         }
      }).trigger('load');
   },

   imageHoverActivate : function (objectName) {
      var s = $(objectName).attr("src").replace(/\.(.+)$/i, "on.$1");
      $(objectName).attr("src", s);
   },

   imageHoverDeactivate : function (objectName) {
      var s = $(objectName).attr("src").replace(/on\.(.+)$/i, ".$1");
      $(objectName).attr("src", s);
   },

   initializeLayout : function() {
      var DUCATI = this,
         $ = this.jQuery,
         settings = this.settings;

      $('.splashImageDot').unbind().mouseenter(function() {
         if ($('#' + this.id + ' img').attr('src').indexOf("dotred") >= 0)
            return;

         $(this).css('cursor', 'pointer');
         $('#' + this.id + ' img').attr('src', "/images/dotwhite.gif");
      }).mouseleave(function() {
         if ($('#' + this.id + ' img').attr('src').indexOf("dotred") < 0)
            $('#' + this.id + ' img').attr('src', "/images/dotgray.gif");

         $(this).css('cursor', '');
      }).click(function() {
         var id = this.id.replace(/splashImageDot/,'');

         if (id == SPLASHIMAGECURRENT)
            return;

         SPLASHIMAGEMANUALSELECT = true;
         splashImagesLoad(id);
         return false;
      });

      $('.splashImage').unbind().mouseenter(function() {
         $(this).css('cursor', 'pointer');
      }).mouseleave(function() {
         $(this).css('cursor', '');
      }).click(function() {
         SPLASHIMAGEMANUALSELECT = true;
         next = SPLASHIMAGECURRENT + 1;

         if (next > SPLASHIMAGES)
            next = 1;

         splashImagesLoad(next);
         return false;
      });

      $('.notificationIcon').unbind().mouseenter(function() {
         $(this).css('cursor', 'pointer');
      }).mouseleave(function() {
         $(this).css('cursor', '');
      }).click(function() {
         if ($('.notificationPopup').is(":hidden")) {
            $('.notificationPopup').show();

            // Mark them as read
            var imgsrc = $(this).attr("src");

            // That means we need to set this as read
            if (imgsrc.indexOf("off.gif") < 0) {
               $(this).attr("src", "/images/notificationoff.gif");

               var salt = getSalt();
               // Set as read
               $.get('/ajax_notification.php', 
                  { salt : salt, c : "load" }, function(data) {
                  var json = evalJSON(data);


               });

            }
         } else
            $('.notificationPopup').hide();
      });

      $('.notificationClose').unbind().mouseenter(function() {
         $(this).css('cursor', 'pointer');
         $(this).css('background-color', '#f10000');
      }).mouseleave(function() {
         $(this).css('cursor', '');
         $(this).css('background-color', '#d30000');
      }).click(function() {
         $('.notificationPopup').hide();
      });



      $('.navigationArrow').unbind().mouseenter(function() {
         $(this).css('cursor', 'pointer');
      }).mouseleave(function() {
         $(this).css('cursor', '');
      }).click(function() {
         var id = this.id.split(",");
         var type = id[0];
         var goto = id[1];

         var salt = getSalt();

         setLoadingSmall('block' + type + 'Content');

         $.get('/ajax_navigation.php', { salt : salt, type : type, goto : goto 
            }, function(data) {
            var json = evalJSON(data);

            $('#block' + type + 'Content').html(unescape(json.content));
            $('#navigationArrow' + type + "L").html(json.navleft);
            $('#navigationArrow' + type + "R").html(json.navright);

            DUCATI.initializeLayout();

            // adjust the hash
            document.location.hash = '/index.php?' + json.type + '=' + goto;
         });
      });

      $('.forumResultRow').unbind().mouseenter(function() {
         $(this).css('background-color', '#fffddc');
         $(this).css('cursor', 'pointer');
         
         var offsets = $(this).offset();
         var width = 200;
         var areaWidth = $(this).width();

         var frameOffset = $('.framework').offset();

         var content = $('#threadData' + this.id).html();
         popupShow(offsets.top - 168, offsets.left - frameOffset.left + areaWidth + 10, 
            width, content, "left");
      }).mouseleave(function() {
         $(this).css('background-color', '');
         popupHide();
      }).click(function() {
         var link = $('a', this).attr('href');
         location.href = link;
      });
      
      $('.menuLink a').unbind().mouseenter(function() {
         $(this).parent().css('background-color', '#444444');
         $('.menuLinkArrow',this).show();
      }).mouseleave(function() {
         $(this).parent().css('background-color', '#363636');
         $('.menuLinkArrow',this).hide();
      })

      $('.loginToggle').unbind().disableTextSelect().mouseenter(function() {
         $(this).css('cursor', 'pointer');
         $(this).css('text-decoration', 'underline');
      }).mouseleave(function() {
         $(this).css('text-decoration', 'none');
      }).click(function() {
         if ($('#userLogin').is(':hidden')) {
            var togglePositionR = $('#framework').width() - 
               ($(this).offset().left - $('#framework').offset().left) - 2;
            var togglePositionT = $(this).offset().top - $('#framework').offset().top;

            $('#userLogin').css('right',togglePositionR).show();
         } else
            $('#userLogin').hide();
      });

      $('input[type=text]','#userLogin').unbind().keyup(function(e) {
         if (e.keyCode == 13)
            $('#userLoginForm').submit();
      });

      $('input[type=text],input[type=password]','#userLogin').unbind().focus(function(e) {
         $(this).val('');
      });

      $('.userPanelToggleButtonOn').unbind().mouseenter(function() {
         $(this).css('cursor','pointer');
         $(this).css('background-color','#bbbbbb');
      }).mouseleave(function() {
         $(this).css('background-color','#aaaaaa');
      }).click(function() {
         var id = this.id;

         var salt = getSalt();
         $('#value',this).html("...");

         $.get('/ajax_profile.php', { salt : salt, c : "toggleoff", field : id 
            }, function(data) {
            var json = evalJSON(data);

            if (check(json)) {
               $('#value','#' + id).html("Off");
               $('#' + id).removeClass('userPanelToggleButtonOn').addClass('userPanelToggleButtonOff').css('background-color','#dedede');
               DUCATI.initializeLayout();
            } else
               confirm("An error occured. Refresh and try again");
         });
      });


      $('.userPanelToggleButtonOff').unbind().mouseenter(function() {
         $(this).css('cursor','pointer');
         $(this).css('background-color','#e5e5e5');
      }).mouseleave(function() {
         $(this).css('background-color','#dedede');
      }).click(function() {
         var id = this.id;

         var salt = getSalt();
         $('#value',this).html("...");

         $.get('/ajax_profile.php', { salt : salt, c : "toggleon", field : id
            }, function(data) {

            var json = evalJSON(data);

            if (check(json)) {
               $('#value','#' + id).html("On");
               $('#' + id).removeClass('userPanelToggleButtonOff').addClass('userPanelToggleButtonOn').css('background-color','#aaaaaa');
               DUCATI.initializeLayout();
            } else
               confirm("An error occured. Refresh and try again");
         });
      });

      $('.reinstateActivate').unbind().mouseenter(function() {
         $(this).css('text-decoration', "underline");
      }).mouseleave(function() {
         $(this).css('text-decoration', "");
      }).click(function() {
         $('#reinstateBlock').show();
      });
   },

   initializeRegistry : function() {


      $('.uploadifyButtonUpload,.uploadifyButtonCancel,.registryButtonGo,.registryButtonStop'
         ).unbind().mouseenter(function() {
         $(this).css('cursor','pointer');
      }).mouseleave(function() {
         $(this).css('cursor','');
      });

      $('.uploadifyButtonUpload,.registryButtonGo').mouseenter(function() {
         $(this).css('background-color', '#77bf70');
      }).mouseleave(function() {
         $(this).css('background-color', '#b6edb1');
      });

      $('.uploadifyButtonCancel,.registryButtonStop').mouseenter(function() {
         $(this).css('background-color', '#b77373');
      }).mouseleave(function() {
         $(this).css('background-color', '#cdadad');
      });


      $('.registryShortThumbL,.registryShortThumbR').unbind().mouseenter(function() {
         $(this).css("background-color", "#c5c5c5");
         $(this).css("cursor", "pointer");
      }).mouseleave(function() {
         $(this).css("background-color", "#d5d5d5");
      }).click(function() {
         var salt = getSalt();
         var id = this.id.split("_");
         var profileid = id[1];
         var index = 1;

         // Figure out current index
         if ($(this).hasClass("registryShortThumbL"))
            index = parseInt($("#registryShortThumbIndex_" + profileid).html()) - 1;
         else
            index = parseInt($("#registryShortThumbIndex_" + profileid).html()) + 1;

         setLoadingSmall('registryShortImage_' + profileid);

         $.get('/ajax_registry.php', { salt : salt, profileid : profileid,
            index : index, c : "loadthumbnail" }, function(data) {

            var json = evalJSON(data);

            if (check(json)) {
               $('#registryShortImage_' + profileid).html(unescape(json.response));
               $('#registryShortThumbIndex_' + profileid).html(json.current);
            } 
         });


      }).disableTextSelect();

      $('img','.registryLongThumbnails').unbind().mouseenter(function() {
         $(this).css("border-color", "#b30000");
         $(this).css("cursor", "pointer");
      }).mouseleave(function() {
         $(this).css("border-color", "#dddddd");
         $(this).css("cursor", "");
      }).click(function() {
         var src = $(this).attr("src").replace(/_mini.jpg/g, ".jpg");

         $('#registryLongPhoto img').attr("src", src);
      }).disableTextSelect();

      $('img', '#registryLongPhoto').unbind().mouseenter(function() {
         $(this).css("cursor", "pointer");
      }).mouseleave(function() {
         $(this).css("cursor", "");
      }).click(function() {
         // Go through the thumbnails and figure out which one we are on
         // Save the first one, and then if we find it do the one after
         var first = $('img','.registryLongThumbnails').first();
         var seeking = $(this).attr("src").replace(/.jpg/g, "") + "_mini.jpg";

         var found = false;
         var set = false;

         $('img','.registryLongThumbnails').each(function() {
            if (found && !set) {
               var src = $(this).attr("src").replace(/_mini.jpg/g, ".jpg");
               $('img','#registryLongPhoto').attr("src", src);
               set = true;
            }

            if (set)
               return;

            if ($(this).attr('src').indexOf(seeking) >= 0)
               found = true;
         });

         if (!set) {
            var src = first.attr("src").replace(/_mini.jpg/g, ".jpg");
            $('img','#registryLongPhoto').attr("src", src);

         }
      }).disableTextSelect();



      $('.registryManageThumb').mouseenter(function(e) {
         $(this).css('cursor','move');
      }).mouseleave(function() {
         $(this).css('cursor','');
      });

      $('.registryManageThumbDelete').unbind().mouseenter(function() {
         $(this).css('cursor','pointer');
         $(this).css('color', '#ffffff');
         $(this).css('background-color','#b30000');
      }).mouseleave(function() {
         $(this).css('cursor','');
         $(this).css('color', '#b30000');
         $(this).css('background-color','#aaaaaa');
      }).click(function() {
         if (!confirm("Are you sure you wish to delete this photo?"))
            return;

         var salt = getSalt();

         var id = this.id.split("_");
         var profileid = id[1];
         var photoid = id[2];

         var parent = $('#thumb_' + profileid + "_" + photoid);

         $.get('/ajax_manage.php', { profileid : profileid, salt : salt,
            photoid : photoid, c : "deletephoto" }, function(data) {

            var json = evalJSON(data);

            if (check(json)) {
               parent.animate({opacity:0.0},600,function() {
                  parent.remove();
               });
            } else {
               confirm("An error occured while deleting. Refresh and try again");
            }
         });
      });



      $('#registryManageThumbs').sortable({
         opacity: 0.6,
         cursor: 'move',
         update: function(event, ui) {
            var id = ui.item.attr("id").split("_");
            var profileid = id[1];
            var photoid = id[2];

            var count = 0;
            var sortorder = 0;
            $('li', this).each(function() {
               count++;

               if (this.id == ui.item.attr("id"))
                  sortorder = count;
            });

            var salt = getSalt();

            $.get("/ajax_manage.php", { salt : salt, c : 'resortphoto',
               sortorder : sortorder, profileid : profileid, photoid : photoid },
               function(data) {

               var json = evalJSON(data);

               if (!check(json))
                  confirm("An error occured while trying to move this photo. Refresh and try again");
            });
         }
      });
   },

   initializeForum : function() {
      $('.forumAddToFAQ').unbind().mouseenter(function() {
         $(this).css('cursor', 'pointer');
         $(this).css('text-decoration', 'underline');
      }).mouseleave(function() {
         $(this).css('text-decoration', 'none');
      }).click(function() {
         if (confirm("Suggest this thread to the site FAQ?")) {
            faqSuggest(this.id);
         }
         return false;
      });


      var forumButtons = [
            "logout", "join", "enlarge", "reply", "newthread",
            "reply", "reply_small", "forward", "edit", "quote",
            "quickreply"

         ];

      // Button hover?
      $('img').unbind().mouseenter(function() {
         if ($(this).attr('src').indexOf("images/buttons") < 0)
            return;

         // Just obtain the name
         var name = $(this).attr('src').replace(/images\/buttons\//g, "");
         name = name.replace(/.gif/g, "");

         if ($.inArray(name, forumButtons) < 0)
            return;

         $(this).attr('src', $(this).attr('src').replace(/.gif/g, "_on.gif"));
      }).mouseleave(function() {
         if ($(this).attr('src').indexOf("images/buttons") < 0)
            return;

         var name = $(this).attr('src').replace(/images\/buttons\//g, "");
         name = name.replace(/_on.gif/g, "");

         if ($.inArray(name, forumButtons) < 0)
            return;

         $(this).attr('src', $(this).attr('src').replace(/_on.gif/g, ".gif"));
      });

   },

   initializeGallery : function() {
      $('.galleryListFrame').unbind().mouseenter(function() {
         $('.galleryListFrameBody', this).css('opacity', 0.8);
      }).mouseleave(function() {
         $('.galleryListFrameBody', this).css('opacity', 0.5);
      });

      $('.uploadifyButtonUpload,.uploadifyButtonCancel,.galleryButtonGo,.galleryButtonStop'
         ).unbind().mouseenter(function() {
         $(this).css('cursor','pointer');
      }).mouseleave(function() {
         $(this).css('cursor','');
      });

      $('.uploadifyButtonUpload,.galleryButtonGo').mouseenter(function() {
         $(this).css('background-color', '#77bf70');
      }).mouseleave(function() {
         $(this).css('background-color', '#b6edb1');
      });

      $('.uploadifyButtonCancel,.galleryButtonStop').mouseenter(function() {
         $(this).css('background-color', '#b77373');
      }).mouseleave(function() {
         $(this).css('background-color', '#cdadad');
      });

      $('input,textarea','.galleryManageCaptionRow').unbind().focus(function(e) {
         $('.galleryManageCaptionHighlight',$(this).parent()).show();
      }).blur(function(e) {
         $('.galleryManageCaptionHighlight',$(this).parent()).hide();
      }).click(function() {
         if ($(this).attr("type") == 'checkbox') {
            if ($(this).is(':checked')) {
               var id = $(this).attr("id");
               // Undo all of the other ones
               $('input[type=checkbox]', '.galleryManageCaptionRow').each(function(e) {
                  if (id != $(this).attr("id"))
                     $(this).attr("checked", false);
               });
            }
         }
      });

      $('.galleryOrganizeThumb').mouseenter(function(e) {
         $(this).css('cursor','move');
      }).mouseleave(function() {
         $(this).css('cursor','');
      });

/*
      $('.galleryButtonGo,.galleryButtonStop').unbind.mouseenter(function() {
         $(this).css('cursor','pointer');
      }).mouseleave(function() {
         $(this).css('cursor','');
      });
*/

      $('.galleryOrganizeThumbDelete').unbind().mouseenter(function() {
         $(this).css('cursor','pointer');
         $(this).css('color', '#ffffff');
         $(this).css('background-color','#b30000');
      }).mouseleave(function() {
         $(this).css('cursor','');
         $(this).css('color', '#b30000');
         $(this).css('background-color','#aaaaaa');
      }).click(function() {
         if (!confirm("Are you sure you wish to delete this photo?"))
            return;

         var salt = getSalt();

         var id = this.id.split("_");
         var albumid = id[1];
         var photoid = id[2];

         var parent = $('#thumb_' + albumid + "_" + photoid);

         $.get('/ajax_organize.php', { albumid : albumid, salt : salt,
            photoid : photoid, c : "deletephoto" }, function(data) {

            var json = evalJSON(data);

            if (check(json)) {
               parent.animate({opacity:0.0},600,function() {
                  parent.remove();
               });
            } else {
               confirm("An error occured while deleting. Refresh and try again");
            }
         });
      });

      $('#galleryOrganizeAlbums').sortable({
         opacity: 0.6,
         cursor: 'move',
         update: function(event, ui) {
            var id = ui.item.attr("id").split("_");
            var albumid = id[1];
            var childid = id[2];

            var count = 0;
            var sortorder = 0;
            $('li', this).each(function() {
               count++;

               if (this.id == ui.item.attr("id"))
                  sortorder = count;
            });

            var salt = getSalt();

            $.get("/ajax_organize.php", { salt : salt, c : 'resortalbum',
               sortorder : sortorder, albumid : albumid, childid : childid }, 
               function(data) {

               var json = evalJSON(data);

               if (!check(json))
                  confirm("An error occured while trying to move this album. Refresh and try again");
            });
         }
      });


      $('#galleryOrganizeThumbs').sortable({
         opacity: 0.6,
         cursor: 'move',
         update: function(event, ui) {
            var id = ui.item.attr("id").split("_");
            var albumid = id[1];
            var photoid = id[2];

            var count = 0;
            var sortorder = 0;
            $('li', this).each(function() {
               count++;

               if (this.id == ui.item.attr("id"))
                  sortorder = count;
            });

            var salt = getSalt();

            $.get("/ajax_organize.php", { salt : salt, c : 'resortphoto',
               sortorder : sortorder, albumid : albumid, photoid : photoid }, 
               function(data) {

               var json = evalJSON(data);

               if (!check(json))
                  confirm("An error occured while trying to move this photo. Refresh and try again");
            });
         }
      });

      $('.galleryPhotoCommentDelete').unbind().mouseenter(function() {
         $(this).css('cursor','pointer');
      }).mouseleave(function() {
         $(this).css('cursor','');
      }).click(function() {
         var id = this.id.split("_");
         var photoid = id[1];
         var commentid = id[2];

         if (!confirm("Are you sure you wish to delete this comment?"))
            return;

         var salt = getSalt();

         $.get("/ajax_comments.php", { salt : salt, c : 'delete',
            commentid : commentid, photoid : photoid }, 
            function(data) {

            confirm(data);
            var json = evalJSON(data);

            if (check(json)) {
               $('#galleryPhotoComment_' + commentid).animate({opacity : 0.0}, 500, function() {
                  $(this).remove();
               });
            } else
               confirm("An error occured while trying to move this photo. Refresh and try again");
         });
      });

      $('.galleryCaptionEdit').unbind().mouseenter(function() {
         $(this).css("background-color", "#e5e5e5");
         $(this).css("cursor","pointer");
      }).mouseleave(function() {
         $(this).css("background-color", "");
         $(this).css("cursor","");
      }).click(function() {
         $('.galleryPhotoTitle').hide();
         $('.galleryPhotoDescription').hide();
         $('.galleryPhotoCaption').show();
      });
   }
};

$(document).ready(function() {
   DUCATI.init();

});

function zeroPad(num, count) {
   var numZeropad = num + '';

   while(numZeropad.length < count)
      numZeropad = "0" + numZeropad;

   return numZeropad;
}

function disableSelect(objectName) {
   if ($.browser.mozilla){//Firefox
      $('#' + objectName).css('MozUserSelect','none');
   } else if($.browser.msie){//IE
       $('#' + objectName).bind('selectstart',function(){return false;});
   }
}

function removeApos(string) {
   return string.replace(/'/g, "");
}

function loading() {
   show("loading");
}

function unloading() {
   hide("loading");
}

function getSalt() {
   return Math.floor(Math.random()*1000);
}

function evalJSON(responseText) {
   var json = eval('(' + responseText + ')');

   return json;
}

function check(json) {
   var check = parseInt(json.check);
   if (check == 0)
      return false;

   return true;
}

function fadeRefresh(objectName, objectContent) {
   $('#' + objectName).animate({
      opacity: 0
   },function() {
      $(this).html(objectContent);

      DUCATI.initializeLayout();

      $(this).animate({
         opacity: 100
      });
   });
}

function fadeOut(objectName) {
   $('#' + objectName).animate({
      opacity: 0
   }, 2000);
}

function fadeIn(objectName) {
   $('#' + objectName).animate({
      opacity: 100
   }, 2000);
}

function trim(str, chars) {
   return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
   chars = chars || "\\s";
   return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
   chars = chars || "\\s";
   return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function setLoading(objectName) {
   $('#' + objectName).html('<center><img src="/images/contentloading.gif"></center>');
}

function setLoadingSmall(objectName) {
   $('#' + objectName).html('<center><img src="/images/loading.gif"></center>');
}

//----------------------------
// Login
function loginCancel() {
   $('#userLogin').hide();
}

//----------------------------
// Reinstate
function reinstateCancel() {
   $('#reinstateBlock').hide();
}

function reinstateSend() {
   var salt = getSalt();

   var query = trim($("#reinstateQuery",'#reinstateBlock').val());

   if (query != "i will not include links in my first few posts") {
      confirm("Please type in the confirmation sentence correctly");
      return;
   }

   var contents = trim($("#reinstateContents").val());

   if (contents.length < 5) {
      confirm("You can type a better description about the contents of your first post...");
      return;
   }

   $.get('/ajax_reinstateuser.php', { salt : salt,
      reason : contents }, function(data) {

      var json = evalJSON(data);

      if (check(json)) {
         confirm("Your user was reinstated. Please remember, outside links are not recommended." +
            "If you are attempting to publish photos, start a ducati.org gallery or use " +
            "the attachment feature");
      } else {
         confirm(json.response);
      }
   });


}

//----------------------------
// Contact form
function contactFormSubmit() {
   var error = false;
   var values = "";
   $("input[name='submit']",'.contactFormBlock').attr("disabled", true);
   $('input,textarea','.contactFormBlock').each(function() {
      if (values.length > 0)
         values += ",";

      var value = trim($(this).val());

      if (value.length <= 0)
         error = true;

      values += $(this).attr("name") + "=" + escape(value);
   });

   if (error) {
      confirm("All fields are required for valid submission");
      $("input[name='submit']",'.contactFormBlock').attr("disabled", false);
      return false;
   }

   $("input[name='submit']",'.contactFormBlock').val("submitting...");

   var salt = getSalt();

   $.post('/ajax_contactform.php', { salt : salt, values : values }, function(data) {
      var json = evalJSON(data);

      if (check(json)) {
         $('.contactFormBlock').html("<strong>" + json.response + "</strong>");
      } else {
         confirm(json.response);
         $("input[name='submit']",'.contactFormBlock').val("submit").attr("disabled", false);
      }
   });

}


//----------------------------
// Splash images
var SPLASHIMAGES = 0;
var SPLASHIMAGECURRENT = 1;
var SPLASHIMAGEMANUALSELECT = false;
var SPLASHIMAGESLOADING = false;

function initializeSplashImages(splashCount) {
   SPLASHIMAGES = splashCount;

   $('#splashImageCaption1').animate({left: 10},600,function() {});

   if (splashCount > 1)
      setTimeout("splashImagesNext()", 7000);
}

function splashImagesNext() {
   if (!SPLASHIMAGES || SPLASHIMAGEMANUALSELECT)
      return;

   next = SPLASHIMAGECURRENT + 1;

   if (next > SPLASHIMAGES)
      next = 1;

   splashImagesLoad(next);
}

function splashImagesLoad(next) {
   if (SPLASHIMAGESLOADING)
      return;

   SPLASHIMAGESLOADING = true;
   var current = 'splashImage' + SPLASHIMAGECURRENT;
   var currentCaption = 'splashImageCaption' + SPLASHIMAGECURRENT;
   var currentDot = 'splashImageDot' + SPLASHIMAGECURRENT;

   SPLASHIMAGECURRENT = next;
   var next = 'splashImage' + SPLASHIMAGECURRENT;
   var nextCaption = 'splashImageCaption' + SPLASHIMAGECURRENT;
   var nextDot = 'splashImageDot' + SPLASHIMAGECURRENT;


   // Update the dot
   $('#' + currentDot + ' img').attr('src', "/images/dotgray.gif");
   $('#' + nextDot + ' img').attr('src', "/images/dotred.gif");

   // Show the next one
   $('#' + next).css('opacity',1.0).show();

   // Remove the old caption
   $('#' + currentCaption).animate({left: -500}, 600, function() {

      // Fade out the current
      $('#' + current).animate({ opacity: 0.0 }, 400, function() {

         // Strip current of the class
         $('#' + current).removeClass('active lastActive').hide();

         $('#' + next).addClass('active');

         // Place in the new caption
         $('#' + nextCaption).animate({left: 10},600, function() {
            SPLASHIMAGESLOADING = false;

            if (SPLASHIMAGES && !SPLASHIMAGEMANUALSELECT)
               setTimeout("splashImagesNext()", 6000);

         });


      });
   });
}

//----------------------------
// Popup dialogue
function popupShow(top, left, width, content, arrowdir) {
   $('#popupBox').unbind();
   $('#popupBox').css('top', top + 'px');
   $('#popupBox').css('left', left + 'px');
   $('#popupBox').css('width', width + 'px');
   $('#popupBox').html(content);

   if (arrowdir == 'left' || arrowdir == 'right')
      var arrow = "LR";
   else
      var arrow = "TB";

   $('#popupBoxArrow' + arrow).css('top', (top + 2) + 'px');
   $('#popupBoxArrow' + arrow).css('left', (left - 5) + 'px');

   $('#popupBoxArrow' + arrow).html('<img src="/images/popupboxarrow' + arrowdir + '.gif">');
   $('#popupBoxArrow' + arrow).show();
   $('#popupBox').show();
}

function popupHide() {
   $('#popupBox').hide();
   $('#popupBoxArrowLR').hide();
   $('#popupBoxArrowTB').hide();
}

function md5(str) {
   var xl;
 
   var RotateLeft = function(lValue, iShiftBits) {
      return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits));
   };
 
   var AddUnsigned = function(lX,lY) {
      var lX4,lY4,lX8,lY8,lResult;
      lX8 = (lX & 0x80000000);
      lY8 = (lY & 0x80000000);
      lX4 = (lX & 0x40000000);
      lY4 = (lY & 0x40000000);
      lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF);
      if (lX4 & lY4) {
         return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
      }
      if (lX4 | lY4) {
         if (lResult & 0x40000000) {
            return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
         } else {
            return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
         }
      } else {
         return (lResult ^ lX8 ^ lY8);
      }
   };
 
   var F = function(x,y,z) { return (x & y) | ((~x) & z); };
   var G = function(x,y,z) { return (x & z) | (y & (~z)); };
   var H = function(x,y,z) { return (x ^ y ^ z); };
   var I = function(x,y,z) { return (y ^ (x | (~z))); };
 
   var FF = function(a,b,c,d,x,s,ac) {
      a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));
      return AddUnsigned(RotateLeft(a, s), b);
   };
 
   var GG = function(a,b,c,d,x,s,ac) {
      a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));
      return AddUnsigned(RotateLeft(a, s), b);
   };
 
   var HH = function(a,b,c,d,x,s,ac) {
      a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));
      return AddUnsigned(RotateLeft(a, s), b);
   };
 
   var II = function(a,b,c,d,x,s,ac) {
      a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));
      return AddUnsigned(RotateLeft(a, s), b);
   };
 
   var ConvertToWordArray = function(str) {
      var lWordCount;
      var lMessageLength = str.length;
      var lNumberOfWords_temp1=lMessageLength + 8;
      var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64;
      var lNumberOfWords = (lNumberOfWords_temp2+1)*16;
      var lWordArray=Array(lNumberOfWords-1);
      var lBytePosition = 0;
      var lByteCount = 0;
      while ( lByteCount < lMessageLength ) {
         lWordCount = (lByteCount-(lByteCount % 4))/4;
         lBytePosition = (lByteCount % 4)*8;
         lWordArray[lWordCount] = (lWordArray[lWordCount] | (str.charCodeAt(lByteCount)<<lBytePosition));
         lByteCount++;
      }
      lWordCount = (lByteCount-(lByteCount % 4))/4;
      lBytePosition = (lByteCount % 4)*8;
      lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80<<lBytePosition);
      lWordArray[lNumberOfWords-2] = lMessageLength<<3;
      lWordArray[lNumberOfWords-1] = lMessageLength>>>29;
      return lWordArray;
   };
 
   var WordToHex = function(lValue) {
      var WordToHexValue="",WordToHexValue_temp="",lByte,lCount;
      for (lCount = 0;lCount<=3;lCount++) {
         lByte = (lValue>>>(lCount*8)) & 255;
         WordToHexValue_temp = "0" + lByte.toString(16);
         WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2);
      }
      return WordToHexValue;
   };
 
   var x=Array();
   var k,AA,BB,CC,DD,a,b,c,d;
   var S11=7, S12=12, S13=17, S14=22;
   var S21=5, S22=9 , S23=14, S24=20;
   var S31=4, S32=11, S33=16, S34=23;
   var S41=6, S42=10, S43=15, S44=21;
 
   str = utf8_encode(str);
   x = ConvertToWordArray(str);
   a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;
   
   xl = x.length;
   for (k=0;k<xl;k+=16) {
      AA=a; BB=b; CC=c; DD=d;
      a=FF(a,b,c,d,x[k+0], S11,0xD76AA478);
      d=FF(d,a,b,c,x[k+1], S12,0xE8C7B756);
      c=FF(c,d,a,b,x[k+2], S13,0x242070DB);
      b=FF(b,c,d,a,x[k+3], S14,0xC1BDCEEE);
      a=FF(a,b,c,d,x[k+4], S11,0xF57C0FAF);
      d=FF(d,a,b,c,x[k+5], S12,0x4787C62A);
      c=FF(c,d,a,b,x[k+6], S13,0xA8304613);
      b=FF(b,c,d,a,x[k+7], S14,0xFD469501);
      a=FF(a,b,c,d,x[k+8], S11,0x698098D8);
      d=FF(d,a,b,c,x[k+9], S12,0x8B44F7AF);
      c=FF(c,d,a,b,x[k+10],S13,0xFFFF5BB1);
      b=FF(b,c,d,a,x[k+11],S14,0x895CD7BE);
      a=FF(a,b,c,d,x[k+12],S11,0x6B901122);
      d=FF(d,a,b,c,x[k+13],S12,0xFD987193);
      c=FF(c,d,a,b,x[k+14],S13,0xA679438E);
      b=FF(b,c,d,a,x[k+15],S14,0x49B40821);
      a=GG(a,b,c,d,x[k+1], S21,0xF61E2562);
      d=GG(d,a,b,c,x[k+6], S22,0xC040B340);
      c=GG(c,d,a,b,x[k+11],S23,0x265E5A51);
      b=GG(b,c,d,a,x[k+0], S24,0xE9B6C7AA);
      a=GG(a,b,c,d,x[k+5], S21,0xD62F105D);
      d=GG(d,a,b,c,x[k+10],S22,0x2441453);
      c=GG(c,d,a,b,x[k+15],S23,0xD8A1E681);
      b=GG(b,c,d,a,x[k+4], S24,0xE7D3FBC8);
      a=GG(a,b,c,d,x[k+9], S21,0x21E1CDE6);
      d=GG(d,a,b,c,x[k+14],S22,0xC33707D6);
      c=GG(c,d,a,b,x[k+3], S23,0xF4D50D87);
      b=GG(b,c,d,a,x[k+8], S24,0x455A14ED);
      a=GG(a,b,c,d,x[k+13],S21,0xA9E3E905);
      d=GG(d,a,b,c,x[k+2], S22,0xFCEFA3F8);
      c=GG(c,d,a,b,x[k+7], S23,0x676F02D9);
      b=GG(b,c,d,a,x[k+12],S24,0x8D2A4C8A);
      a=HH(a,b,c,d,x[k+5], S31,0xFFFA3942);
      d=HH(d,a,b,c,x[k+8], S32,0x8771F681);
      c=HH(c,d,a,b,x[k+11],S33,0x6D9D6122);
      b=HH(b,c,d,a,x[k+14],S34,0xFDE5380C);
      a=HH(a,b,c,d,x[k+1], S31,0xA4BEEA44);
      d=HH(d,a,b,c,x[k+4], S32,0x4BDECFA9);
      c=HH(c,d,a,b,x[k+7], S33,0xF6BB4B60);
      b=HH(b,c,d,a,x[k+10],S34,0xBEBFBC70);
      a=HH(a,b,c,d,x[k+13],S31,0x289B7EC6);
      d=HH(d,a,b,c,x[k+0], S32,0xEAA127FA);
      c=HH(c,d,a,b,x[k+3], S33,0xD4EF3085);
      b=HH(b,c,d,a,x[k+6], S34,0x4881D05);
      a=HH(a,b,c,d,x[k+9], S31,0xD9D4D039);
      d=HH(d,a,b,c,x[k+12],S32,0xE6DB99E5);
      c=HH(c,d,a,b,x[k+15],S33,0x1FA27CF8);
      b=HH(b,c,d,a,x[k+2], S34,0xC4AC5665);
      a=II(a,b,c,d,x[k+0], S41,0xF4292244);
      d=II(d,a,b,c,x[k+7], S42,0x432AFF97);
      c=II(c,d,a,b,x[k+14],S43,0xAB9423A7);
      b=II(b,c,d,a,x[k+5], S44,0xFC93A039);
      a=II(a,b,c,d,x[k+12],S41,0x655B59C3);
      d=II(d,a,b,c,x[k+3], S42,0x8F0CCC92);
      c=II(c,d,a,b,x[k+10],S43,0xFFEFF47D);
      b=II(b,c,d,a,x[k+1], S44,0x85845DD1);
      a=II(a,b,c,d,x[k+8], S41,0x6FA87E4F);
      d=II(d,a,b,c,x[k+15],S42,0xFE2CE6E0);
      c=II(c,d,a,b,x[k+6], S43,0xA3014314);
      b=II(b,c,d,a,x[k+13],S44,0x4E0811A1);
      a=II(a,b,c,d,x[k+4], S41,0xF7537E82);
      d=II(d,a,b,c,x[k+11],S42,0xBD3AF235);
      c=II(c,d,a,b,x[k+2], S43,0x2AD7D2BB);
      b=II(b,c,d,a,x[k+9], S44,0xEB86D391);
      a=AddUnsigned(a,AA);
      b=AddUnsigned(b,BB);
      c=AddUnsigned(c,CC);
      d=AddUnsigned(d,DD);
   }
 
   var temp = WordToHex(a)+WordToHex(b)+WordToHex(c)+WordToHex(d);
 
   return temp.toLowerCase();
}

function utf8_encode(string) {
   string = (string+'').replace(/\r\n/g, "\n").replace(/\r/g, "\n");
 
   var utftext = "";
   var start, end;
   var stringl = 0;
 
   start = end = 0;
   stringl = string.length;
   for (var n = 0; n < stringl; n++) {
      var c1 = string.charCodeAt(n);
      var enc = null;
 
      if (c1 < 128) {
         end++;
      } else if((c1 > 127) && (c1 < 2048)) {
         enc = String.fromCharCode((c1 >> 6) | 192) + String.fromCharCode((c1 & 63) | 128);
      } else {
         enc = String.fromCharCode((c1 >> 12) | 224) + String.fromCharCode(((c1 >> 6) & 63) | 128) + String.fromCharCode((c1 & 63) | 128);
      }
      if (enc != null) {
         if (end > start) {
            utftext += string.substring(start, end);
         }
         utftext += enc;
         start = end = n+1;
      }
   }
 
   if (end > start) {
      utftext += string.substring(start, string.length);
   }
 
   return utftext;
} 



//----------------------------
// Gallery
function galleryCaptionSave(albumid) {
   var values = "";

   $('textarea','#galleryManageCaptionBlock').each(function() {
      if (values.length > 0)
         values += "&";

      values += $(this).attr("id") + "=" + escape($(this).val());
   });

   var numCovers = $('input[type=checkbox]:checked', '.galleryManageCaptionRow').length;

   if (numCovers > 1) {
      confirm("Only one album cover should be checked. Error");
      return;
   } else if (numCovers == 1) {
      var obj = $('input[type=checkbox]:checked', '.galleryManageCaptionRow');

      values += "&" + obj.attr("id") + "=1";

   }

   var salt = getSalt();
   $("input[type='button']",'#galleryManageCaptionBlock').attr("disabled", true);

   $.post('/ajax_captionhandler.php', { albumid : albumid, salt : salt,
      values : values }, function(data) {

      var json = evalJSON(data);

      if (check(json)) {
         // Go to the album
         confirm("Saved");
         location.href = json.albumurl;
      } else {
         confirm(json.response);
         $("input[type='button']",'#galleryManageCaptionBlock').attr("disabled", false);
      }
   });

}

function galleryCaptionCancel(albumURL) {
   location.href = albumURL;
}

function galleryOrganizeSave(albumid) {
   var values = "";

   $('textarea','#galleryOrganizeInformation').each(function() {
      if (values.length > 0)
         values += "&";

      values += $(this).attr("id") + "=" + escape($(this).val());
   });

   var salt = getSalt();
   $("input[type='button']",'#galleryOrganizeInformation').attr("disabled", true);

   $.get('/ajax_organize.php', { albumid : albumid, salt : salt,
      c : "saveinformation", values : values }, function(data) {

      var json = evalJSON(data);

      if (check(json)) {
         // Go to the album
         confirm("Saved");
         $("input[type='button']",'#galleryOrganizeInformation').attr("disabled", false);
      } else {
         confirm("An error occured while saving. Refresh and try again");
      }
   });

}

function galleryOrganizeDelete(albumid) {
   if (!confirm("Are you sure you wish to delete this album and all of its contents?"))
      return;

   var salt = getSalt();
   $("input[type='button']",'#galleryOrganizeDelete').attr("disabled", true);

   $.get('/ajax_organize.php', { albumid : albumid, salt : salt,
      c : "deletealbum" }, function(data) {

      var json = evalJSON(data);

      if (check(json)) {
         location.href = json.redirect;
      } else {
         confirm("An error occured while deleting. Refresh and try again");
      }
   });
}

function galleryOrganizeCreateSubalbum(albumid) {
   var salt = getSalt();
   var title = $('textarea','#galleryOrganizeSubalbum').val();

   $("input[type='button']",'#galleryOrganizeSubalbum'
      ).val("Creating...").attr("disabled", true);

   $.get('/ajax_organize.php', { albumid : albumid, salt : salt,
      title : title, c : "createsubalbum" }, function(data) {

      var json = evalJSON(data);

      if (check(json)) {
         $('#galleryOrganizeAlbums').html(json.content);
      } else {
         confirm("An error occured while creating album. Refresh and try again");
      }
      $("input[type='button']",'#galleryOrganizeSubalbum'
         ).val("Create album").attr("disabled", false);
   });
}

function gallerySetSelect(photoid) {
   var salt = getSalt();

   $.get('/ajax_adm.php', { photoid : photoid, salt : salt,
      c : "setselect" }, function(data) {

      var json = evalJSON(data);

      if (check(json)) {
         confirm(json.response);
      } else {
         confirm("An error occured while setting this. Refresh and try again");
      }
   });
}

function galleryStart() {
   var salt = getSalt();

   $.get('/ajax_start.php', { salt : salt }, function(data) {
      var json = evalJSON(data);

      if (check(json)) {
         location.href = json.response;
      } else {
         confirm("An error occured while attempting to create your album. Refresh and try again");
      }
   });
}

function galleryPermissionsSave(albumid) {
   var salt = getSalt();
   var values = "";

   $("input[type='checkbox']","#galleryOrganizePermissions").each(function() {
      if (values.length > 0)
         values += "&";

      var value = 0;
      if ($(this).is(":checked"))
         value = 1;

      values += $(this).attr("name") + "=" + value;
   });

   $("input[type='button']",'#galleryOrganizePermissions').attr("disabled", true);

   $.get('/ajax_organize.php', { albumid : albumid, salt : salt,
      values : values, c : "savepermissions" }, function(data) {

      var json = evalJSON(data);
      $("input[type='button']",'#galleryOrganizePermissions').attr("disabled", false);

      if (check(json)) {
         confirm("Permission settings saved");
      } else {
         confirm("An error occured while saving. Refresh and try again");
      }
   });
}

function galleryCommentPost(photoid, photohash) {
   var salt = getSalt();

   var comment = escape($('#galleryCommentText').val());

   $("input[type='button']",'#galleryCommentBlock').attr("disabled", true);

   $.post('/ajax_comments.php', { photoid : photoid, salt : salt,
      hash : photohash, comment : comment, c : "post" }, function(data) {

      var json = evalJSON(data);
      $("input[type='button']",'#galleryCommentBlock').attr("disabled", false);

      if (check(json)) {
         // Insert the comment to the comment area
         $(unescape(json.block)).insertBefore('#galleryCommentBlock').css("opacity", 0.0).animate(
            {opacity: 1}, 500, function() {
            $(this).css("opacity", 1);

            DUCATI.initializeGallery();
         });
      } else {
         confirm("An error occured while posting the comment. Repeat posts are not allowed, along with ones that are far too short");
      }
   });
}

function galleryPhotoCaptionCancel(photoid) {
   $('.galleryPhotoCaption').hide();
   $('.galleryPhotoTitle').show();
   $('.galleryPhotoDescription').show();
   $('.galleryPhoto').show();
}

function galleryPhotoCaptionSave(photoid) {
   var title = escape($('#galleryPhotoTitleEdit').val());
   var tags = escape($('#galleryPhotoTagsEdit').val());
   var description = escape($('#galleryPhotoDescriptionEdit').val());

   $('.galleryPhotoCaption').hide();
   $('.galleryPhotoCaptionSaving').show();

   var salt = getSalt();

   $.post('/ajax_photo.php', { photoid : photoid, salt : salt, tags : tags,
      title : title, description : description, c : "updatecaption" }, function(data) {

      var json = evalJSON(data);

      if (check(json)) {
         $('.galleryPhotoCaptionSaving').hide();
         $('.galleryPhotoTitle').html(json.title);
         $('.galleryPhotoDescription').html(json.description);
         $('.galleryPhotoTags').html(json.tags);


         galleryPhotoCaptionCancel(photoid);

         DUCATI.initializeGallery();
      } else {
         confirm("An error occured while saving. Please refresh and try again.");
      }
   });
}

//-----------------------------------------
function registryMapChange(maptype) {
   var salt = getSalt();

   $.get('/ajax_registry.php', { salt : salt, maptype : maptype,
      c : 'mapload' }, function(data) {
      var json = evalJSON(data);

      if (check(json)) {
         $('#registryMapContainer').html(unescape(json.response));
      }
   });
}

function registryLocationChange(abbr, maptype) {
   var salt = getSalt();

   $('#registryResults').html("loading...");

   $.get('/ajax_registry.php', { salt : salt, maptype : maptype,
      abbr : abbr, c : 'getresults' }, function(data) {

      var json = evalJSON(data);

      if (check(json)) {
         $('#registryResults').html(unescape(json.response));

         DUCATI.initializeRegistry();
      }
   });
}

function registryCheckFields() {
   var required = [
         "fullname", "year", "mileage", "odo", "numowners",
         "location_city", "location_country", "from_city",
         "from_country"
      ];


   var hasAllFields = true;
   $('input, textarea','#registryForm').each(function() {
      var value = $(this).val();
      var name = $(this).attr("name");

      if ($.inArray(name, required) >= 0 && value.length == 0) {
         $(this).css('background-color', '#ffd3d3');
         hasAllFields = false;
      } else {
         $(this).css('background-color', '');
      }
   });

   $('select','#registryForm').each(function() {
      var name = $(this).attr("name");
      var value = $(this).val();

      if ($.inArray(name, required) >= 0 && value == '0') {
         $(this).css('background-color', '#ffd3d3');
         hasAllFields = false;
      } else {
         $(this).css('background-color', '');
      }
   });

   // Is the country US? Need state
   var locCountry = $('select[name="location_country"]','#registryForm').val();
   var locState = $('select[name="location_state"]','#registryForm').val();
   if (locCountry == 'US' && locState == '0') {
      $('select[name="location_state"]','#registryForm').css("background-color", "#ffd3d3");
      hasAllFields = false;
   } else
      $('select[name="from_state"]','#registryForm').css("background-color", "");

   var fromCountry = $('select[name="from_country"]','#registryForm').val();
   var fromState = $('select[name="from_state"]','#registryForm').val();
   if (fromCountry == 'US' && fromState == '0') {
      $('select[name="from_state"]','#registryForm').css("background-color", "#ffd3d3");
      hasAllFields = false;
   } else
      $('select[name="from_state"]','#registryForm').css("background-color", "");


   return hasAllFields;
}

function registrySubmit() {
   if (!registryCheckFields()) {
      confirm("All highlighted fields must be entered in");
      return false;
   }

   $("input[type='button']",'#registryForm').val("submitting...");

   var salt = getSalt();
   var values = "";

   $('input,select,textarea','#registryForm').each(function() {
      value = $(this).val();
      if (values.length > 0)
         values += ",";

      values += $(this).attr("name") + "=" + escape(value);
   });

   $.post('/ajax_registry.php', { c : "submit",
      salt : salt, values : values }, function(data) {
      var json = evalJSON(data);

      if (check(json)) {
         location.href = json.redirect;
      } else {
         confirm(json.response);
         $("input[type='button']",'#registryForm').val("Submit and add photos").attr("disabled", false);
      }
   });
}


function registryManageDelete(profileid) {
   if (!confirm("Are you sure you wish to delete this profile and all of its contents?"))
      return;

   var salt = getSalt();
   $("input[type='button']",'#registryManageDelete').attr("disabled", true);

   $.get('/ajax_manage.php', { profileid : profileid, salt : salt,
      c : "deleteprofile" }, function(data) {

      var json = evalJSON(data);

      if (check(json)) {
         location.href = "/";
      } else {
         confirm("An error occured while deleting. Refresh and try again");
      }
   });
}

function registryUpdate(profileid) {
   if (!registryCheckFields()) {
      confirm("All highlighted fields must be entered in");
      return false;
   }

   $("input[type='button']",'#registryForm').val("saving...");

   var salt = getSalt();
   var values = "";

   $('input,select,textarea','#registryForm').each(function() {
      value = $(this).val();
      if (values.length > 0)
         values += ",";

      values += $(this).attr("name") + "=" + escape(value);
   });

   $.post('/ajax_manage.php', { c : "update", profileid : profileid,
      salt : salt, values : values }, function(data) {

      confirm(data);
      var json = evalJSON(data);

      $("input[type='button']",'#registryForm').val("Save changes").attr("disabled", false);
      if (check(json)) {
         confirm("Changes have been saved");
      } else {
         confirm(json.response);
      }
   });
}



