﻿// JScript File
function NewWindow(mypage, myname, w, h, scroll) {

//    if (window.showModalDialog) {
//        var winl = (screen.width - w) / 2;
//        var wint = (screen.height - h) / 2;
//        winprops = 'dialogHeight:'+h+'px; dialogWidth:'+w+'px; dialogTop:'+wint+';dialogLeft:'+winl+';scroll: '+scroll+';resizable: no;'
//        win = window.showModalDialog(mypage, myname, winprops)
//    } else {
        var winl = (screen.width - w) / 2;
        var wint = (screen.height - h) / 2;
        winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
        win = window.open(mypage, myname, winprops)
        if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
//    }

}

function setCheckedValue(radioObj, newValue) {
    if(!radioObj)
        return;
    var radioLength = radioObj.length;
    if(radioLength == undefined) {
        radioObj.checked = (radioObj.value == newValue.toString());
        return;
    }
    for(var i = 0; i < radioLength; i++) {
        radioObj[i].checked = false;
        if(radioObj[i].value == newValue.toString()) {
	        radioObj[i].checked = true;
        }
    }
}

function teclaEsc(e) { 
    var code;
    if (!e) var e = window.event; 
        
    if (e.keyCode)
        code = e.keyCode; 
    else if (e.which)
        code = e.which; 
            
    if(code == 27)
        window.close();
}

 
//conta caracters do textarea
function contaCaracteres(campo, contador, total){
	if(campo.value.length > total){
		campo.value = campo.value.substring(0, total);
	}else{
		document.getElementById(contador).innerHTML = total - campo.value.length;
	}
}

//popup centralizado
function abrirPopupCenter(pagina, largura, altura) {
   // Definindo meio da tela
   var esquerda = (screen.width - largura)/2;
   var topo = (screen.height - altura)/2;

   // Abre a nova janela 
   window.open(pagina,'Seila','height=' + altura + ', width=' + largura + ', top=' + topo + ', left=' + esquerda); 
}

//fechar janela
function fecharJanela(){
    window.close();
}

//voltar
function voltar(){
    history.go(-1);
}

//popup
function abrePopup(url, name, altura, largura, top, left){
	window.open(url, name, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, top='+top+', left='+left+', width='+largura+', height='+altura);
}

//preview do upload
function preview(campo) {
    field = campo.value;
    image = document.getElementById( 'previewIMG' );
    border = document.getElementById( 'previewBorder' );
    path = 'file://'+ field;
    path = path.replace(/\\/, '/'); // Fix Windows paths
    image.src = path;  

    image.style.display = 'block';
    
    obj=document.createElement("img")
    obj.src=path 

    //alert(obj.width + "x" + obj.height);
    window.setTimeout("previewRedimensiona(obj)", 300);
    
    
}

function previewRedimensiona(obj){
    //alert(obj.width + "x" + obj.height);
    if(obj.width > obj.height)
    {
        //paisagem
        if(obj.width > 300){
            image.style.width = "300px";
            image.style.height = "250px";
            border.style.width = "300px";
            border.style.height = "250px";
        }else{
            image.style.width = obj.width;
            image.style.height = obj.height;
            border.style.width = obj.width;
            border.style.height = obj.height;
        }
    }
    else
    {
        //retrato
        if(obj.width > 250){
            image.style.width = "250px";
            image.style.height = "300px";
            border.style.width = "250px";
            border.style.height = "300px";
        }else{
            image.style.width = obj.width;
            image.style.height = obj.height;
            border.style.width = obj.width;
            border.style.height = obj.height;
        }
        
    }
}
