function showForm(form){
    ShowOverlay("#dvAddToFavorites", "&Page=AddToFavorites&theAction=Overlay", "", "#Name")
}

function sharePhoto(id){
    aUrl="&theAction=Overlay&Page=SharePhoto&UserId="+$('#UserId').val()+"&AlbumId="+$('#AlbumId').val()+"&ImageId="+id,
    ShowOverlay("#dvSharePhoto", aUrl, "#ActionForm");
}

function editFavorite(lFavoriteId, lFavoriteFolderId,lPageNr){
    ShowOverlay("#dvEditFavorite", "&Page=EditFavorite&theAction=Overlay&FavoriteFolderId=" + lFavoriteFolderId + "&FavoriteId=" + lFavoriteId + "&PageNr="+lPageNr, "", "#Name")
}
function redirecFavorite(lFavoriteId, lFavoriteFolderId,lPageNr, lUrlFavorite)
{    
   aUrl = "&Page=EditFavorite&theAction=FavoriteRedirect&FavoriteFolderId=" + lFavoriteFolderId + "&FavoriteId=" + lFavoriteId + "&PageNr="+lPageNr;
        $.ajax({
            type: "GET",
            url: './index.php5',
            data: aUrl + '&' + Math.random(),    
            beforeSend: function (XMLHttpRequest) {
            },
            success: function(msg){
               if($.trim(msg)!="")
               {
                    ShowOverlay("#dvEditFavorite", "&Page=EditFavorite&theAction=OverlayErrorUrl&FavoriteFolderId=" + lFavoriteFolderId + "&FavoriteId=" + lFavoriteId + "&PageNr="+lPageNr, "", "#Name");
               }
               else
               {
                 window.location.href=lUrlFavorite; 
               }
                    
            }
        });  
}                         

function createFolder(){
    aUrl = "&theAction=Overlay&Page=CreateFolder";
    ShowOverlay("#dvCreateFolder", aUrl, "#ActionForm", "#FolderName");
}

function renameFolder(){
     aUrl = $("#ActionForm").serialize() + "&theAction=Overlay&Page=RenameFolder";
     ShowOverlay("#dvRenameFolder", aUrl, "#ActionForm", "#FolderName");      
}

function messagePhoto() {
    aUrl = '&theAction=OverlayPhoto&Page=AccountSettings';
    $('#formView').css({left:"0px", top:"0px"});
    $('#formView').show(0);
    ShowOverlay('#dvMessagePhoto', aUrl, '#formView');
}
function messagePhotoAlbum() {
    aUrl = '&theAction=OverlayPhoto&Page=Album';
    $('#formView').css({left:"0px", top:"0px"});
    $('#formView').show(0);
    ShowOverlay('#dvMessagePhoto', aUrl, '#formView');
}
function messageContest() {
    aUrl = '&theAction=OverlayContest&Page=Contest';
    $('#formView').css({left:"0px", top:"0px"});
    $('#formView').show(0);
    ShowOverlay('#dvMessageContest', aUrl, '#formView');
}
function inviteContest(lCurrentDay, lEventType)
{
   HideContent('#dvShowContest')
   lCurrentDay = lCurrentDay || '';
   lEventType = lEventType || '';
   aUrl = '&theAction=Overlay_Contacts&Page=InviteFriend&CurrentDay=' + lCurrentDay + '&EventType=' + lEventType;
   ShowOverlay('#dvSearchcontacs', aUrl);
}
function messagePhotoAlbumPanel() {
    aUrl = '&theAction=OverlayPhoto&Page=AlbumPanel';
    $('#formView').css({left:"0px", top:"0px"});
    $('#formView').show(0);
    ShowOverlay('#dvMessagePhoto', aUrl, '#formView');
}
function editEvent(id) {
    aUrl = '&theAction=Overlay&Page=NewEditEventAgenda&EventId='+id;
    ShowOverlay('#dvAddNewEvent', aUrl);
}

function URLEncode(url) //Function to encode URL.
{
    // The Javascript escape and unescape functions do not correspond
    // with what browsers actually do...
    var SAFECHARS = "0123456789" + // Numeric
    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + // Alphabetic
"abcdefghijklmnopqrstuvwxyz" +
"-_.!~*'()"; // RFC2396 Mark characters
var HEX = "0123456789ABCDEF";

var plaintext = url;
var encoded = "";
for (var i = 0; i < plaintext.length; i++ ) {
var ch = plaintext.charAt(i);
if (ch == " ") {
encoded += "+"; // x-www-urlencoded, rather than %20
} else if (SAFECHARS.indexOf(ch) != -1) {
encoded += ch;
} else {
var charCode = ch.charCodeAt(0);
if (charCode > 255) {
alert( "Unicode Character '"
+ ch
+ "' cannot be encoded using standard URL encoding.\n" +
"(URL encoding only supports 8-bit characters.)\n" +
"A space (+) will be substituted." );
encoded += "+";
} else {
encoded += "%";
encoded += HEX.charAt((charCode >> 4) & 0xF);
encoded += HEX.charAt(charCode & 0xF);
}
}
}

return encoded;
};


 

function URLDecode(url) //function decode URL
{
// Replace + with ' '
// Replace %xx with equivalent character
// Put [ERROR] in output if %xx is invalid.
var HEXCHARS = "0123456789ABCDEFabcdef";
var encoded = url;
var plaintext = "";
var i = 0;
while (i < encoded.length) {
var ch = encoded.charAt(i);
if (ch == "+") {
plaintext += " ";
i++;
} else if (ch == "%") {
if (i < (encoded.length-2)
&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
plaintext += unescape( encoded.substr(i,3) );
i += 3;
} else {
alert( 'Bad escape combination near ...' + encoded.substr(i) );
plaintext += "%[ERROR]";
i++;
}
} else {
plaintext += ch;
i++;
}
} // while

return plaintext;
}; 

function AjaxPromoBanner (user_id) {
    aUrl = '&theAction=Overlay&Page=PromoBanner&UserId='+user_id;
    ShowOverlay('#dvPromoBanner', aUrl);
}

function AjaxAddToFavorites(){
    if ( $("#ActionFavoriteForm").length > 0 ) {
        $.ajax({
            type: "POST",
            url: './index.php5',
            data: $("#ActionFavoriteForm").serialize() + '&theAction=Save&PageUrl=' + URLEncode(window.location.href),
            beforeSend: function (XMLHttpRequest) {
                AjaxLoading('#dvPageContent');
            },
            success: function(msg){
                HideContent('dvAddToFavorites');
                if(!AjaxShowSystemMessage(msg))
                    $('#dvFavoritePanel').html($.trim(msg));
                
            }
        });
    }
}

function AjaxEditFavorite(lPageNr){
    if ( $("#ActionFavoriteForm").length > 0 ) {
        $.ajax({
            type: "POST",
            url: './index.php5',
            data: $("#ActionFavoriteForm").serialize() + '&theAction=Save&PageNr='+lPageNr,
            beforeSend: function (XMLHttpRequest) {
                AjaxLoading('#dvEditFavoriteContent');
            },
            success: function(msg){
                HideContent('dvEditFavorite');
                if(!AjaxShowSystemMessage(msg))
                    $('#dvFavoritePanel').html($.trim(msg));
                
            }
        });
    }
}


function AjaxCreateFolder(lForm){
    if ( $(lForm).length > 0 ) {
        $.ajax({
            type: "POST",
            url: "./index.php5",
            data: $(lForm).serialize(),
            beforeSend: function (XMLHttpRequest) {
                AjaxLoading("#dvCreateFolderContent");
            },
            success: function(msg){
                AjaxShowSystemMessage(msg);
                if(!AjaxShowSystemMessage(msg)){
                    $("#FolderList").replaceWith(msg);
                    $(".hdButton").show(0);
                }
                HideContent("#dvCreateFolder");
            }
        });
    }
}

function AjaxEditPhotoField(lElementId, lFunction) {
    if ( $("#ActionForm").length > 0 ) {
        $.ajax({
            type: "POST",
            url: './index.php5',
            data: $("#ActionForm").serialize() + '&Page=Photo&theAction='+lFunction,
            beforeSend: function (XMLHttpRequest) {
            },
            success: function(msg){
                if(!AjaxShowSystemMessage(msg))
                    $(lElementId).html(msg);
                  
            }
        });
    }
}

function AjaxEditPhotoFieldOnFocus(lElementId, lFunction, lTextFieldId) {
    if ( $("#ActionForm").length > 0 ) {
        $.ajax({
            type: "POST",
            url: './index.php5',
            data: $("#ActionForm").serialize() + '&Page=Photo&theAction='+lFunction,
            beforeSend: function (XMLHttpRequest) {
            },
            success: function(msg){
                if(!AjaxShowSystemMessage(msg))
                    $(lElementId).html(msg);
                    if(lTextFieldId!= "")
                    {  
                       document.getElementById(lTextFieldId).focus();
                    }
            }
        });
    }
}

function AjaxDownloadMail(lForm){
    if ( $(lForm).length > 0 ) {
        $.ajax({
            type: "POST",
            url: "./index.php5",
            data: $(lForm).serialize()+"&Page=ExportMessage&theAction=Download&Option=1",
            beforeSend: function (XMLHttpRequest) {
                AjaxLoading("#dvExportMessage");
            },
            success: function(msg){
            HideContent('dvExportMessage'); 
                AjaxShowSystemMessage(msg);
                if(!AjaxShowSystemMessage(msg)){
                    $("#ViewMessage").replaceWith(msg);
                    
                    //$(".hdButton").show(0);
                }
                
            }   //se agrego el ajax para que aparesca mensaje de confimacion. se cambio el boton aceptar por un submit
        });
    }
}

function AjaxNextPhoto(){
    if ( $("#ActionForm").length > 0 ) {
        $.ajax({
            type: "POST",
            url: './index.php5',
            data: $("#ActionForm").serialize() + '&Page=PhotoPager&theAction=NextImage',
            beforeSend: function (XMLHttpRequest) {
            },
            success: function(msg){
                AjaxShowSystemMessage(msg);
                if(!AjaxShowSystemMessage(msg))
                    $('#PhotoPager').html(msg);
            }
        });
    }
}

function AjaxPreviousPhoto(){
    if ( $("#ActionForm").length > 0 ) {
        $.ajax({
            type: "POST",
            url: './index.php5',
            data: $("#ActionForm").serialize() + '&Page=PhotoPager&theAction=PreviousImage',
            beforeSend: function (XMLHttpRequest) {
            },
            success: function(msg){
                AjaxShowSystemMessage(msg);
                if(!AjaxShowSystemMessage(msg))
                    $('#PhotoPager').html(msg);
            }
        });
    }
}

function AjaxRenameFolder(lForm){
    if ( $(lForm).length > 0 ) {
        $.ajax({
            type: "POST",
            url: "./index.php5",
            data: $(lForm).serialize(),
            beforeSend: function (XMLHttpRequest) {
                AjaxLoading("#dvRenameFolderContent");
            },
            success: function(msg){
                AjaxShowSystemMessage(msg);
                if(!AjaxShowSystemMessage(msg))
                    $("#FolderList").replaceWith(msg);
                HideContent("#dvRenameFolder");
            }
        });
    }
}

function AjaxBlockUser(unblocktext,message){
    message = message || ''; 

    if ( $("#ActionForm").length > 0 ) {
        $.ajax({
            type: "GET",
            url: './index.php5',
            data: $("#ActionForm").serialize() + '&Page=BlockUser&theAction=Block',
            beforeSend: function (XMLHttpRequest) {
                AjaxLoading('#dvPageContent');
            },
            success: function(msg){
                $('#tdUnblockUser').html('<a class="profile_button01" onfocus="this.blur()" href="javascript:UnblockUser(\''+message+'\')">' + unblocktext + '</a>');
                AjaxShowSystemMessage(msg);
                HideContent('dvBlockUser');
            }
        });
    }
}

function closeForm(){
      $('#formView').html('');
      $('#fullView').html('');

      $('#formView').hide("slow");
      $('#fullView').hide("fast");
      
      clearTimeout(iStayTopLeft);
}
function closeActionForm(){
      document.location.href='index.php5?Page=MessagePanel';
}

function OnReport(aItemId, theAction){
    aItemId = aItemId || '';
    theAction = theAction || 'Report';
    
    $("#ActionForm #theAction").val(theAction);
    aUrl = $("#ActionForm").serialize() + '&ReplyTo=' + aItemId;

    $("#formView").css({left:"0px", top:"0px"});
    $("#formView").show(0);
    ShowOverlay('#dvReportItem', aUrl, '#formView');
}

function AjaxReportItem(Form){
    if ( $(Form).length > 0 ) {
        $.ajax({
            type: "POST",
            url: './index.php5',
            data: $(Form).serialize(),
            beforeSend: function (XMLHttpRequest) {
                AjaxLoading('#dvReportItem');
                HideSystemMessage();
            },
            success: function(msg){
                HideContent('dvReportItem');
                AjaxShowSystemMessage(msg);
            }
        });
    }

}

// It's used into BlogMessageView
function textCounter(field, maxlimit) {     

    if (field.value.length > maxlimit) // if too long...trim it!
        field.value = field.value.substring(0, maxlimit);
        
    var strComment = field.value || "";
    strComment = cutWord(strComment,35);
    if ( $("#tdPreviewComment").length > 0 )
        $('#tdPreviewComment').html(strComment);
    $("#Comment").focus();
}

// It's used into MessagePanel
function toFolder(inMemberFolderId){
    var theForm = document.getElementById("ActionForm");
    theForm.Page.value = "MessagePanel";
    theForm.MemberFolderId.value = inMemberFolderId;
    theForm.theAction.value = "Load";
    theForm.submit();
}

function toMessage(inMessageId, inPage, inAction){
    var theForm = document.getElementById("ActionForm");
    theForm.Page.value = inPage;
    theForm.MessageId.value = inMessageId;
    theForm.theAction.value = inAction;
    theForm.submit();            
}
function OnChooseAll(checked){
    var i=0;
    for (i=0; i < document.ActionForm.elements.length; i++)
        if (document.ActionForm.elements[i].type.toLowerCase() == 'checkbox')
            document.ActionForm.elements[i].checked = checked;
}

function OnChooseAllNoRead(checked){
    var i=0;
    for (i=0; i < document.ActionForm.elements.length; i++)
      //  if (document.ActionForm.elements[i].type.toLowerCase() == 'checkbox')
            document.ActionForm.elements[i].checked = checked;
}

// It's used into Agenda 
function toAgendaFriendSelector(){
    var theForm = document.getElementById("ActionForm");
    theForm.Page.value = "AgendaFriendSelector";
    theForm.theAction.value = "Load";
    theForm.submit();
}
function OnDeleteEvent(inEventId, msg){
    var isConfirmed = true;

    inConfirmation = msg;
    if (inConfirmation.length)
        isConfirmed = confirm(inConfirmation);

    if (isConfirmed){
        var theForm = document.getElementById("ActionForm");
        theForm.Page.value = "AgendaEventDetail";
        theForm.theAction.value = "Delete";
        theForm.EventId.value = inEventId;
        theForm.submit();
    }
}

function showEmbeddedFriendSelector(){                
    if ( $("#tdFriends").length > 0 ) {
        $("#tdContent").hide(0);
        $("#tdFriends").show(0);
    }else{
        $.ajax({
            type: "GET",
            url: './index.php5',
            data: '&theAction=Overlay&Page=AgendaFriendSelector&NoBorder=1',
            success: function(msg){
                $("#tdContent").hide(0);
                $("#tdContent").after('<td background="Images/style00/dark-frame05.png" id="tdFriends">' + msg + '</td>');
            }
        });
    }
}

// It's used into News 
function NewsHandlerTree(node){
    var div = document.getElementById('layer'+node);
    var link = document.getElementById('link'+node);

    if(div.style.display == 'none'){            
        div.style.display = 'block';
        link.innerHTML = '<img src="Images/style00/icon_down00.gif" width="15" height="11" alt="-" border="0">';
    }else{
        div.style.display = 'none';
        link.innerHTML = '<img src="Images/style00/icon_forum01.gif" width="15" height="11" alt="+" border="0">';
    }
}

 
function OnCancelPendingRequest(inUserId,msn){
    var isConfirmed = true;
    isConfirmed = confirm(msn);
    if (isConfirmed){
        var theForm = document.getElementById("ActionForm");
        theForm.theAction.value = "Cancel";
        theForm.UserId.value = inUserId;
        theForm.submit();
    }
}
//It's used into Profile
function commentProfile(aReplyTo){
    aReplyTo = aReplyTo || '';
    aUrl = '&theAction=Overlay&Page=ProfileCommentPanel&UserId=' + $('#UserId').val() + '&ReplyTo=' + aReplyTo;
    $("#ActionForm #ReplyTo").val(aReplyTo);
    ShowOverlay('#dvComment', aUrl, '#ActionForm', '#Comment');
}
function UnblockUser(msg){
    var isConfirmed = true;
    isConfirmed = confirm(msg);
    if (isConfirmed){                                        
        var theForm = document.getElementById("ActionForm");
        theForm.Page.value = "Profile";                
        theForm.theAction.value = "Unblock";
        theForm.PageReturn.disabled=true;
        theForm.ReplyTo.disabled=true;
        theForm.submit();
    }
}
function blockUser() {
    aUrl = '&theAction=Overlay&Page=BlockUser&UserId=' + $('#UserId').val();
    ShowOverlay('#dvBlockUser', aUrl, '#ActionForm');
}
function addFriend() {
    aUrl = '&theAction=Overlay&Page=AddFriendProfile&UserId=' + $('#UserId').val();
    ShowOverlay('#dvAddFriend', aUrl, "", "#Code");
}
        
// It's used into PhotoView
function commentPhoto(aReplyTo){        
    aReplyTo = aReplyTo || '';
    aUrl = '&theAction=Overlay&Page=Photo&AlbumId=' + $('#AlbumId').val() + '&ImageId=' + $('#ImageId').val() + '&ReplyTo=' + aReplyTo;
    $("#ActionForm #ReplyTo").val(aReplyTo);
    ShowOverlay('#dvComment', aUrl, '#ActionForm', '#Comment');
}
function updatePhotoComments(msg, dvOverlay){
    location.reload();
}
function OnDeleteComment(lPhotoComment,msg){
    var isConfirmed = true;
    isConfirmed = confirm(msg);
    if (isConfirmed){
        $("#ActionForm #ReplyTo").val(lPhotoComment);
        AjaxOnActionDeleteMessage("DeleteComment", "#ActionForm", "#dvPhotoComments", updatePhotoComments)
    }
}
function OnDeleteProfile(lPhotoComment,msg){
    var isConfirmed = true;
    isConfirmed = confirm(msg);
    if (isConfirmed){
        $("#ActionForm #ReplyTo").val(lPhotoComment);
        AjaxOnActionDeleteMessage("DeleteComment", "#ActionForm", "#dvPhotoComments", updatePhotoComments)
    }
}

function AddPhotoToFavorites(lStatusId){
    ShowOverlay("#dvAddToFavorites", "&Page=AddToFavorites&theAction=Overlay&StatusId="+lStatusId+"", "", "#Name")
}
function copy(txt) {
    if( window.clipboardData && clipboardData.setData ) {
        clipboardData.setData("Text", txt);
    } else {
        var flashcopier = 'flashcopier';
        if(!document.getElementById(flashcopier)) {
            var divholder = document.createElement('div');
            divholder.id = flashcopier;
            document.body.appendChild(divholder);
        }
        document.getElementById(flashcopier).innerHTML = '';
        var divinfo = '<embed src="/flash/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(txt)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
        document.getElementById(flashcopier).innerHTML = divinfo;
    }
    $("#msg4 td").html("<?=McwTranslation::LABEL_IMAGE_COPIED?>");
    $("#msg4").fadeOut(2000, function() {
        $("#msg4").css("visibility","hidden");
        $("#msg4").css("display","block");
        $("#msg4 td").html("<?=McwTranslation::LABEL_IMAGE_COPY?>");
    });
    
    return false;
}
function moveToPage(inPageNr){
        var theForm = document.getElementById("ActionForm");
        theForm.PageNr.value = inPageNr;
        theForm.theAction.value = "Load";
        theForm.submit();                       
}

function ChangeFolderAlert(){
    ShowOverlay("#dvChangeFolder", "&Page=MessagePanel&theAction=ChangeFolder&MessageSelected=1", "", "")
}

function MoveBlogAlert(){
    ShowOverlay("#dvMoveBlog", "&Page=BlogPanel&theAction=Move&MessageSelected=1", "", "")
}