﻿
function AbrirFormAlterar(servidor, caminho) {
    var currentTime = new Date()
    caminho = servidor + "/_layouts/hsl/formserverPortalMedico.aspx?XmlLocation=" + caminho + "&OpenIn=Browser&data=" + currentTime

    if (ValidarAppleMobile()) {
        return window.open(caminho, "", 'width=820,height=600, scrollbars=yes,resizable=yes,top=0');
    }
    else {
        return window.showModalDialog(caminho, "", 'dialogWidth:820px; dialogHeight:600px; center:1; dialogHide:0; edge:sunken; help:0; resizable:0; scroll:1; status:1; unadorned:1;');
    }
}

function AbrirFormNovo(servidor, caminho) {
    var currentTime = new Date()
    caminho = servidor + "/_layouts/hsl/formserverPortalMedico.aspx?XsnLocation=" + caminho + "&OpenIn=Browser&DefaultItemOpen=1&data=" + currentTime

    if (ValidarAppleMobile()) {
        return window.open(caminho, "", 'width=820,height=600, scrollbars=yes,resizable=yes,top=0');
    }
    else {
        return window.showModalDialog(caminho, "", 'dialogWidth:820px; dialogHeight:600px; center:1; dialogHide:0; edge:sunken; help:0; resizable:0; scroll:1; status:1; unadorned:1;');
    }
}

//alert('teste');

function ShowModal(_URL, _width, _height, _scroll) {

    //_scroll=1
    if (navigator.appVersion.indexOf("Windows NT 5.1") > 0) {
        _height += 6
    }

    if (window.XMLHttpRequest) {
        //Para IE7
        _height -= 50
    }

    
    if (ValidarAppleMobile()) {
        return window.open(_URL, "", 'width=' + _width + ',height=' + _height + ', scrollbars=' + _scroll + ',resizable=yes,top=0');
    }
    else {
        return window.showModalDialog(_URL, window, 'dialogWidth:' + _width + 'px; dialogHeight:' + _height + 'px; center:1; dialogHide:0; edge:sunken; help:0; resizable:0; scroll:' + _scroll + '; status:1; unadorned:1;');
    }
}

function RetornarModal(valores) {

    parametros = new Array();
    parametros = valores.split(";");
    
    parent.window.returnValue = parametros;
   
    parent.close();
}

function RetornarModalMobile(valores, campos) {
    
    parametros = new Array();
    parametros = valores.split(";");
   
    arrayCampos = new Array();
    arrayCampos = campos.split(";");
       
    if (ValidarAppleMobile()) {
    
        if (parametros != null) {
            for (i = 0; i < parametros.length; i++) {
                try {
                    eval("window.opener.document.getElementById('" + arrayCampos[i] + "').value = '" + parametros[i] + "'");
                }
                catch (e) {
                }
            }
        }
    }
    else {
        parent.window.returnValue = parametros;
    }
    
    parent.close();
}

function TratarRetornoModal(retornoModal, campos) {
    
    arrayCampos = new Array();
    arrayCampos = campos.split(";");

    if (retornoModal != null) {
        for (i = 0; i < retornoModal.length; i++) {
            try {

                eval("document.getElementById('" + arrayCampos[i] + "').value = '" + retornoModal[i] + "'");
            }
            catch (e) {
            }
        }
    }
}


function AbrirJanela(caminho, nomeJanela) {
 
    leftVal = (screen.width - 800) / 2;
    topVal = ((screen.height - 500) / 2) - 10;

    window.open(caminho, nomeJanela, 'width=820, height=600, scrollbars=yes,resizable=yes, top=' + topVal + ', left=' + leftVal)
}

function AbrirJanelaCustom(caminho, nomeJanela, pW, pH) {
    leftVal = (screen.width - pW) / 2;
    topVal = ((screen.height - pH) / 2) - 10;

    window.open(caminho, nomeJanela, 'width=' + pW + ', height=' + pH + ', scrollbars=yes,resizable=yes, top=' + topVal + ', left=' + leftVal)
}



function CalcularIdade(obj) {

    data = obj.value;
    var array_data = data.split("/")
    //se o array nao tem tres partes, a data eh incorreta 
    if (array_data.length != 3)
        return ""

    //comprovo que o ano, mes, dia são corretos 
    var yy
    yy = parseInt(array_data[2]);
    if (isNaN(yy))
        return ""

    var mm
    mm = parseInt(array_data[1]);
    if (isNaN(mm))
        return ""

    var dd
    dd = parseInt(array_data[0]);
    if (isNaN(dd))
        return ""

    thedate = new Date()
    mm2 = thedate.getMonth() + 1
    dd2 = thedate.getDate()
    yy2 = thedate.getYear()

    if (yy2 < 100)
        yy2 = yy2 + 1900

    yourage = yy2 - yy
    if (mm2 < mm)
        yourage = yourage - 1;

    if (mm2 == mm)
        if (dd2 < dd)
        yourage = yourage - 1;

    agestring = yourage;
    return agestring;
}
  
function validaHora(source, arguments) { 

    try
    {
      parData = arguments.Value;
      hrs = (parData.substr(0,2)); 
      min = (parData.substr(3,5));

      situacao = true;

      if (hrs == "" ||
         min == "")
          situacao = false
      else {

          // verifica data e hora 
          if ((hrs < 00) || (hrs > 23) || (min < 00) || (min > 59)) {
              situacao = false;
          }
      }
  }
  catch (e) {
      situacao = false;
  }

   
    arguments.IsValid = situacao;
}

function CalcularHora(HoraInicial, HoraFinal) {

    try {

        horaI = HoraInicial.substring(0, HoraInicial.indexOf(":"));
        minutoI = HoraInicial.substring(HoraInicial.indexOf(":") + 1, HoraInicial.length);

        horaF = HoraFinal.substring(0, HoraFinal.indexOf(":"));
        minutoF = HoraFinal.substring(HoraFinal.indexOf(":") + 1, HoraFinal.length);



        var date1 = new Date(parseInt(2000, 10), parseInt(01, 10), parseInt(01, 10)); //ano,mes,dia
        var date2 = new Date(parseInt(2000, 10), parseInt(01, 10), parseInt(01, 10)); //ano,mes,dia

        date1.setHours(parseInt(horaI, 10));
        date1.setMinutes(parseInt(minutoI, 10));

        date2.setHours(parseInt(horaF, 10));
        date2.setMinutes(parseInt(minutoF, 10));

        var time = date2.getTime() - date1.getTime();
        var horas = (time / 3600000);
        var calculo = "00" + (((time % 3600000) / 60000));
        calculo = calculo.substring(calculo.length - 2, calculo.length);
        horas = "00" + parseInt(horas, 10);

        if (isNaN(calculo) ||
                            isNaN(horas))
            return "";

        return horas.substring(horas.length - 2, horas.length) + ":" + calculo; // mostra o resultado.......................
    }
    catch (e) {
        return "";
    }
}


//FUNO PARA ADICIONAR AOS FAVORITOS
function favoritos() {
    var url = location;
    var title = (document.getElementsByTagName('title').value = document.title);

    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "")
    }
    else if (window.opera && window.print) {
        var mbm = document.createElement('a');
        mbm.setAttribute('rel', 'sidebar');
        mbm.setAttribute('href', url);
        mbm.setAttribute('title', title);
        mbm.click();
    }
    else if (document.all) {
        window.external.AddFavorite(url, title);
    } else (
		alert('Pressione Ctrl + D Para Adicionar aos Favoritos.')
	)
}

function ValidarAppleMobile() {
    var retorno = false;

    //var mobile = (/iphone|ipad|ipod|android|blackberry|mini|windows\sce|palm/i.test(navigator.userAgent.toLowerCase()));

    if (
            navigator.userAgent.toLowerCase().match(/palm/i) ||
            navigator.userAgent.toLowerCase().match(/windows\sce/i) ||
            navigator.userAgent.toLowerCase().match(/mini/i) ||
            navigator.userAgent.toLowerCase().match(/blackberry/i) ||
            navigator.userAgent.toLowerCase().match(/android/i) ||
            navigator.userAgent.toLowerCase().match(/iPad/i) ||
            navigator.userAgent.toLowerCase().match(/iPhone/i) ||
            navigator.userAgent.toLowerCase().match(/iPod/i)
           ) {
        retorno = true;
    }

    return retorno;
}
