﻿var IE = navigator.appName.indexOf("Microsoft") != -1;
var Open;

function PersonalizePrivate(ID, HasMenu)
{
    var prefix = 'PersonalizeOrg';
    var pageID = window.name;
    if(pageID.indexOf(prefix) >= 0)
        pageID = prefix + (parseInt(pageID.substr(prefix.length)) + 1);
    else
        pageID = prefix + '0';
    window.open('/Content/User/OrganizationUpload.aspx?menu=' + (HasMenu ? '1' : '0') + '&ID='+ ID, pageID, 'width=600,height=400,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes');
}

function PersonalizeMenu(ID)
{
    PersonalizePrivate(ID, true);
}

function Personalize(ID)
{
    PersonalizePrivate(ID, false);
}

// Loopa o listbox passado e retorna os valores do mesmo em um array
function GetListBoxOptions(ItemToLoop)
{
    var ReturnValue = null;
    
    if(ItemToLoop != null && ItemToLoop.options.length > 0) // se o item a ser loopado existir
    {
        ReturnValue = new Array();
        for(i=0;i<ItemToLoop.options.length;i++)
            ReturnValue[i] = ItemToLoop.options[i].value;
    }
    
    return ReturnValue;
}

// Procura o componente passado(id) no valor do hidden passado e retorna o componente encontrado
function GetObjectFromHiddenID(HiddenToSearch)
{
    var HiddenValue = document.getElementById(HiddenToSearch).value;
    return document.getElementById(HiddenValue);
}

// Limpa os itens do listbox
function ClearListBox(Item)
{
    if(Item != null)
    {
        for(i=0;i<Item.options.length;i++)
            Item[i] = null;
    }
}

// abre o popup de busca de media]
// FlashObject: EmbedID do flash da página que chamou o popup
function OpenSearchMediaPopup(FlashObjectID, TargetLanguageID, NativeLanguageID)
{
    Open = window.open('/Content/MediaElement/MediaList.aspx?TargetLanguage=' + TargetLanguageID + '&NativeLanguage=' + NativeLanguageID + '&FlashObjectID=' + FlashObjectID, 'SearchMedia','width=720,height=750,toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=no');
    Open.focus();
}

// Função para máscara de entrada de dados.
function formatar(mascara, documento)
{
  var i = documento.value.length;
  var saida = mascara.substring(0,1);
  var texto = mascara.substring(i)
  
  if (texto.substring(0,1) != saida)
  {
	documento.value += texto.substring(0,1);
  }
  
}

// Abre uma popup centralizada na tela.
function OpenCentralized(url, title, width, height, toolbar, directories, status, menubar, scrollbars, resizable) 
{
   // Definindo meio da tela
   var left = 0; //(screen.width - width)/2;
   var top = 0; //(screen.height - height)/2;
   //debugger;
   try
   {
        left = parseInt( (window.screen.availWidth / 2) - (width / 2) );
        top = parseInt( (window.screen.availHeight / 2) - (height / 2) );
        if(left < 0) left = (screen.width - width)/2;
        if(top < 0) top = (screen.height - height)/2;
   }
   catch(e)
   {
        left = (screen.width - width)/2;
        top = (screen.height - height)/2;
   }

   // Abre a nova janela
   return window.open(url, title,'height=' + height + ', width=' + width + ', top=' + top + ', left=' + left + ', toolbar=' + 
      GetPropertieValue(toolbar) + ', directories=' + GetPropertieValue(directories) + ', status=' + GetPropertieValue(status) + 
      ', menubar=' + GetPropertieValue(menubar) + ', scrollbars=' + GetPropertieValue(scrollbars) + ', resizable=' + 
      GetPropertieValue(resizable) + '');
 }

// Checa o valor da propriedade e retorna 'yes' ou 'no' 
function GetPropertieValue (propertie){
    if(propertie == true) 
        return 'yes';
    return 'no';   
 }
 
// Reynaldo Ferrer, 2008/06/13
// Abre em popup a pagina visualizadora de contentComponents, sejam eles tcc ou cc
// Ex: ShowContentComponent1(20, 0) -- chamada de CC id 20
// Ex: ShowContentComponent1(540, 1) -- chamada de TCC id 540
function ShowContentComponent1(ccId, isTcc)
{
    var url = "/ContentComponentViewer/ContentComponentView.aspx?ID=" + ccId.toString() + "&IsTCC=" + isTcc.toString();
    OpenCentralized(url, 'viewContentComponent1', 780, 750, false, false, false, false, true, true);
    return false;
    //OpenCentralized("/ContentComponentViewer/ContentComponent
}

// Reynaldo Ferrer, 2008/06/27
// Abre em popup a pagina visualizadora de contentComponents, sejam eles tcc ou cc
// Ex: EditContentComponent1(20, false) -- chamada de CC id 20
// Ex: EditContentComponent1(540, true) -- chamada de TCC id 540
function EditContentComponent1(ccId, isTcc)
{
    ///Content/ContentComponent/ContentComponent.aspx?ContentComponentID=460&IndTCC=True
    var url = "/Content/ContentComponent/ContentComponent2.aspx?ContentComponentID=" + ccId.toString() + "&IndTCC=" + isTcc.toString();
    var _width = parseInt( window.screen.availWidth * 0.93 );
    var _height = parseInt( window.screen.availHeight * 0.93 );
    OpenCentralized(url, 'editContentComponent1', _width, _height, false, false, false, false, true, true);
    return false;
    //OpenCentralized("/ContentComponentViewer/ContentComponent
}

function CreateContentComponent1()
{
     ///Content/ContentComponent/ContentComponent.aspx?ContentComponentID=460&IndTCC=True
    var url = "/Content/ContentComponent/ContentComponent2.aspx"; //?ContentComponentID=" + ccId.toString() + "&IndTCC=" + isTcc.toString();
    var _width = parseInt( window.screen.availWidth * 0.93 );
    var _height = parseInt( window.screen.availHeight * 0.93 );
    OpenCentralized(url, 'editContentComponent1', _width, _height, false, false, false, false, true, true);
    return false;
    //OpenCentralized("/ContentComponentViewer/ContentComponent
}

// Reynaldo Ferrer, 2008/06/13
// abre em popup a pagina visualizadora de lesson
function ShowLessonNavigate(encodedLessonId)
{
    //Distribution/PrimitiveLesson.aspx?ID
    //OpenCentralized(url, title, width, height, toolbar, directories, status, menubar, scrollbars, resizable) 
    var url = "/Distribution/PrimitiveLesson.aspx?ID=" + encodedLessonId.toString();
    return OpenCentralized(url, 'viewPrimitiveLesson', 785, 755, false, false, false, false, (!!('scrollbars')), true);
}

function NewWin(page, name, Width, Height) 
{
   window.open(page,name,'height=' + Height + ',width=' + Width + ',toolbar=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no ,modal=yes');
} 

// Função responsável por obter a posição vertical das janelas de popup
function GetPopupY( e )
{
    var positionY;
    
    if ( !e )
    {
        e = window.event;
    }
    
    if ( e.clientY )
    {
        positionY = document.body.scrollTop + document.documentElement.scrollTop;
    }
    else
    {
        positionY = document.body.scrollTop + document.documentElement.scrollTop + window.pageYOffset;
    }
    
    return ( positionY );
}

// Função responsável por centralizar um popup na página
function SetPopupPosition( popupId, popupWidth )
{
    var panel = $get( popupId );
    var panelWidth = parseInt( popupWidth );
    var top = (document.documentElement.scrollTop + window.screen.availHeight / 4);
    var left = ((window.screen.availWidth - panelWidth) / 2) + 20;
    panel.style.top = top;
    panel.style.left = left;
}

function ShowDivPopup(itemId)
{
    var panel = $get(itemId);
    var width = panel.style.width;
    var height = panel.style.height;
    
    width = width.toString().replace("px", "");
    height = height.toString().replace("px", "");
    
    var top = (document.documentElement.scrollTop + window.screen.availHeight / 4);
    var left = ((window.screen.availWidth - width) / 2) + 20;
   
    panel.style.display = 'block';
    panel.style.top = top;
    panel.style.left = left;
}

function HideDivPopup(itemId)
{
    var panel = $get(itemId);
    panel.style.display = 'none';
}

function TrimText( textToTrim )
{
    var trimText = new String( textToTrim );
    return ( trimText.trim( ) );
}

function ShowItem(id)
{
    document.getElementById(id).style.visibility = "visible";
    return true;
}

function HideItem(id)
{
    document.getElementById(id).style.visibility = "hidden";
    return true;
}

function ClientDecode(html)
{
    html = html.replace(/&gt;/g,'>');
    html = html.replace(/&gtx;/g,'&gt;');
    html = html.replace(/&lt;/g,'<');
    html = html.replace(/&ltx;/g,'&lt;');
    html = html.replace(/&amp;/g,'&');
    html = html.replace(/&ampx;/g,'&amp;');
    html = html.replace(/&quot;/g,'"');
    html = html.replace(/&quotx;/g,'&quot;');
    
    return html;
}

function StripHTML(text) 
{
    var re = /(<([^>]+)>)/gi;
    text = text.replace(re, "");        
    return text;
}
