﻿// -------------------------------------------------------------------------------------------------------------------
// IQdram FileUpLoad Objeto
//
// Na página que for utilizar o objeto, deve ser colocado o bloco de código abaixo:
//
//    <System.Web.Services.WebMethod(True)> _
//    Public Shared Function IQPU_GetEnableUpLoad(ByVal v_st_ret As String)  As String
//        IQPU_GetEnableUpLoad = IQRT_SH_FileUpload(v_st_ret)
//    End Function
//
// Caso seja utilizada imagem para plotagem é necessário a inclusão do arquivo "Handler_IqUpload.ashx"
//
// -------------------------------------------------------------------------------------------------------------------
//
// Declaração das variáveis 
//

var vFrameUpLoadIQ = "";
var vCustomUpLoadIQ = "";
var vFileExtensionsIQ = "";
var vWidthUpLoadIQ = 0;
var vTimeShowUpLoad = false;
var vContadorProgress = 0;
var vContadorTempoUL = 0;
var vContaImgBlank = 0;


var vPanelProgUpLoadIQ = "Panel_ProgressUpLoad";
var vBtnUpLoadIQ = "btnUpload";
var vObjUoLoad = "IQObjUpLoad";
var vLabelTimeUpLoad = "TextBox_TimeViewUpLoad";

var vbaseUrl = window.parent.document.location.protocol + "//" + document.domain + "/";


function initPhotoUpload(vFrame,vObjUL,vSessionVar,vHeight,vWidth,vFileExt,vTimeView)
{
 vFrameUpLoadIQ = vFrame;
 vCustomUpLoadIQ = vObjUL;
           
 var myFrame = document.getElementById(vFrameUpLoadIQ);

 try
  { var btnUpload = myFrame.contentWindow.document.getElementById(vBtnUpLoadIQ); }
 catch (ex) 
  { return; }
  
 myFrame.contentWindow.document.getElementById(vObjUoLoad).style.height = vHeight + "px";
 myFrame.contentWindow.document.getElementById(vObjUoLoad).style.width = vWidth + "px";
 vFileExtensionsIQ =  vFileExt;
 vWidthUpLoadIQ = vWidth;
 vTimeShowUpLoad = vTimeView;
 if (vTimeShowUpLoad == false)
    { myFrame.contentWindow.document.getElementById(vLabelTimeUpLoad).style.display = "none";
    }
               
 myFrame.contentWindow.document.getElementById(vPanelProgUpLoadIQ).style.width = (vWidthUpLoadIQ)+ "px";
 myFrame.style.height = (vHeight + 30 ) + "px";
            
 btnUpload.onclick = function(event)
 
    {  
     var x = 1;
     var a = PageMethods.IQPU_GetEnableUpLoad("vbrowse",iqf_goUpLoad,iqf_nogoUpLoad);
    }

}
    
function iqf_goUpLoad(vret,vparam)

    { 
      if (vret == "DontGo") { return; }
    
      var myFrame = document.getElementById(vFrameUpLoadIQ);
      var vIQObjUp = myFrame.contentWindow.document.getElementById(vObjUoLoad);
      vContadorProgress = 0;
      vContadorTempoUL = 0;
                 
      myFrame.contentWindow.document.getElementById(vPanelProgUpLoadIQ).style.width = "0px";
      myFrame.contentWindow.document.getElementById("Image_ErrorSzUpLoad").style.display = "none"; 
      myFrame.contentWindow.document.getElementById("Image_ErrorUpUpLoad").style.display = "none"; 
      myFrame.contentWindow.document.getElementById("Image_OkUpUpLoad").style.display = "none"; 
              
      if (vIQObjUp.value.length == 0)
         { myFrame.contentWindow.document.getElementById("Image_ErrorUpLoad").style.display = "block";
           vIQObjUp.focus();
           return;
         }
      else
         { myFrame.contentWindow.document.getElementById("Image_ErrorUpLoad").style.display = "none";
         }

      // Validação das extensões permitidas
                
      var vUrlAtiva = vIQObjUp.value.toLowerCase();
      var vRestricao = vFileExtensionsIQ.toLowerCase();

      var vExtensao = "";
      var vpos = 0;
      for (var i = 1; i <= 20; i++)
          { vpos = vRestricao.indexOf(";");
            if (vpos == -1) { vUrlAtiva = ""; break; }
            vExtensao = vRestricao.substr(0,vpos);
            if (vUrlAtiva.indexOf("." + vExtensao) != -1 ) { break; } // Achou Extensão permitida
            vRestricao = vRestricao.replace(vExtensao + ";",""); // Elimina Extensão analisada
          }
                  
      if (vUrlAtiva == "")
         { myFrame.contentWindow.document.getElementById("Image_ErrorUpLoad").style.display = "block";
           vIQObjUp.focus();
           return;
         }
      else
         { myFrame.contentWindow.document.getElementById("Image_ErrorUpLoad").style.display = "none";
         }
                   
      vIntervaloUp = window.setInterval("iqf_ProgressBarUpLoad()",500)
               
      try
        { myFrame.contentWindow.document.getElementById('photoUpload').submit(); } // Faz o upload
      catch (ex) 
        { return; }
    }


function IQUploadComplete(vStatus,vVarSession,vImgClientID,vTrigger)
{
  clearInterval(vIntervaloUp);
  var myFrame = document.getElementById(vFrameUpLoadIQ);
  myFrame.contentWindow.document.getElementById(vPanelProgUpLoadIQ).style.width = vWidthUpLoadIQ + "px";
  if (vStatus == "size")
     { myFrame.contentWindow.document.getElementById("Image_ErrorSzUpLoad").style.display = "block";  }
  if (vStatus == "erro")
     { myFrame.contentWindow.document.getElementById("Image_ErrorUpUpLoad").style.display = "block";  }
  if (vStatus == "ok")
     { 
     if (vVarSession != "" && vImgClientID != "")
        { var vDateNow = new Date();
          document.getElementById(vImgClientID).src = "Handler_IqUpload.ashx?idfile=IQUPLOADIMAGE&idimg=P1&idblank=" + vDateNow + "&idsession=" + vVarSession;
        }
     vContaImgBlank += 1;
     myFrame.contentWindow.document.getElementById("Image_OkUpUpLoad").style.display = "block"; 
     if (vTrigger != "")
        { document.getElementById(vTrigger).click();
        }
     }   
}


function iqf_ProgressBarUpLoad()
{
  vWd = parseInt (vWidthUpLoadIQ * ( vContadorProgress / 100) );

  var myFrame = document.getElementById(vFrameUpLoadIQ);
  try
   { myFrame.contentWindow.document.getElementById(vPanelProgUpLoadIQ).style.width = vWd + "px"; }
  catch (ex)
   { clearInterval(vIntervaloUp); 
     myFrame.src = "iqc_FileUpLoad_NoTouch.aspx" ; 
     // alert("Excedeu o limite ( httpRuntime maxRequestLength ) estabelecido no WebConfig.");
     return; 
   }
   
  if (vTimeShowUpLoad == true)
     { var vTime = parseInt(vContadorTempoUL/2) + " s";
       myFrame.contentWindow.document.getElementById(vLabelTimeUpLoad).value = vTime;
     }
  vContadorProgress += 1;
  vContadorTempoUL += 1;
  if (vContadorProgress > 100) { vContadorProgress = 1; }
}

function iqf_nogoUpLoad(vret,vparam) { var x = vret; }

// --------------------------------------------------- Fim --------------------------------------------------------------------


function iqf_loadpageiframe(vframe)
{
document.getElementById(vframe).src = "iqc_FileUpLoad_NoTouch.aspx";
}

