﻿// Jscript

$(document).ready(function(){});

var dotnetprefix = "ctl00_ContentPlaceHolder1_";


function ToggleButton(onoff) {
    //alert('hey');
    if (onoff==1) {
        $("#"+dotnetprefix+"btnSubmit").attr("src","images/get-free-ringtones-now.gif");
    } else {
        $("#"+dotnetprefix+"btnSubmit").attr("src","images/get-free-ringtones.gif");
    }
}

function ConfirmBox(url,msg){
	        var conf = confirm(msg + "\n" + url); 
	        if(conf){
		        window.location.href = url; 
	        }
        }


function ImgSrcToggle(id,opensrc,closesrc) {
    var curr = $("#"+id).attr("src");
    
    if (curr==opensrc) {
        $("#"+id).attr("src",closesrc);
        //$("#naviparent").show();
    } else {
        $("#"+id).attr("src",opensrc);
    }
}

function toggleCheckBoxes(startswith,chktotal) {
    var b;
    var sbool = $("#"+ startswith.toString() + 'All').attr("checked");    
    for (b=1;b<=chktotal;b++) {
        if (sbool==true) {
            $("#"+ startswith.toString() + b.toString()).attr("checked",true);
        } else {
            $("#"+ startswith.toString() + b.toString()).attr("checked",false);
        }
    }
}

function toggleAllOption(startswith,index,chktotal) { 
    // should you select all and then deselect one of the other options
    // the select all check box needs to be deselected
    var that;
    var blAllOptionsChecked = true;
    var isItChecked;
    
    for (b=1;b<=chktotal;b++) {
        isItChecked = $("#"+ startswith.toString() + b.toString()).attr("checked");
        if (isItChecked==false) {
            blAllOptionsChecked = false;
        }
    }
    
    that = $("#"+ startswith.toString() + index.toString()).attr("checked");
    
    if (that==false&&blAllOptionsChecked==false) {
        $("#"+ startswith.toString() + 'All').attr("checked",false);
    
    } else if (blAllOptionsChecked==true) {
        $("#"+ startswith.toString() + 'All').attr("checked",true);  
        toggleCheckBoxes(startswith,chktotal);
        
    }
        
}


function toggleFade(objid) {
    var sty = $("#"+objid).attr("style");
    
    if (sty.indexOf('none')>0) {
        $("#"+objid).fadeIn();
    } else {
        $("#"+objid).fadeOut();
    }
    
    //alert($("#"+objid).attr("style"));
    
}

function Highlight(obj) {
    $(obj).attr("style","cursor:hand;background:#cccccc;");
}

function UnHighlight (obj,color) {
    $(obj).attr("style","background:"+color);
}

function AnimateDiv(divid,fromNum,toNum,axis) {
    var currheight = $("#"+divid).height();
    var currwidth = $("#"+divid).width();
    
    //alert(navigator.userAgent);
    
    if (axis=='y') { 
        if (currheight==fromNum) {  // minimize height
            if (navigator.userAgent.indexOf("Firefox")>0) {
                $("#"+divid).attr("style","overflow-y:auto;overflow-x:hidden;height:" + toNum.toString() + "px");
            } else {
                $("#"+divid).animate({height: toNum},800); // minimize 
            }
            
        } else {                    // maximize height
            if (navigator.userAgent.indexOf("Firefox")>0) {
                $("#"+divid).attr("style","overflow-y:auto;overflow-x:hidden;height:" + fromNum.toString() + "px");
                if (divid=="navison") {
                    setTimeout("$('#navison').hide();",1300);
                }
            } else {
                $("#"+divid).animate({height: fromNum},800); 
                if (divid=="navison") {
                    setTimeout("$('#navison').fadeOut();",1300);
                }
            }
        }
    } else { 
        if (currwidth==fromNum) {   // maximize width
            if (navigator.userAgent.indexOf("Firefox")>0) { //
                $("#"+divid).attr("style","overflow-y:auto;overflow-x:hidden;width:" + toNum.toString() + "px");
            } else {
                $("#"+divid).animate({width: toNum},800);   
            }
            
        } else {                    // minimize width
            if (navigator.userAgent.indexOf("Firefox")>0) {
                $("#"+divid).attr("style","overflow-y:auto;overflow-x:hidden;width:" + fromNum.toString() + "px");
                if (divid=="navison") {
                    setTimeout("$('#navison').hide();",1300);
                }
            } else {
                $("#"+divid).animate({width: fromNum},800);
                if (divid=="navison") {
                    setTimeout("$('#navison').hide();",1300);
                }
            }
            
            
        }
    }
}

function LoadFrame(Frame,URL){
	$("#"+Frame).attr("src",URL);
}

function LoadFrameParent(Frame,URL){
	if (navigator.userAgent.indexOf("Firefox")>0){
	    parent.document.getElementById(Frame).src=URL;
	} else {
	    parent.document[Frame].location.href=URL;}
}

function txtBoxFormat(objeto, sMask, evtKeyPress) 
{
    var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
 
    if(document.all) { // Internet Explorer
        nTecla = window.event.keyCode;
    } else if(document.layers) { // Nestcape
        nTecla = evtKeyPress.which;
    } else {
        nTecla = evtKeyPress.which;
        if (nTecla == 8) {
            return true;
        }
    }

    sValue = document.getElementById(objeto).value;
        
    // Limpa todos os caracteres de formatação que
    // já estiverem no campo.
        sValue = sValue.toString().replace( "-", "" );
        sValue = sValue.toString().replace( "-", "" );
        sValue = sValue.toString().replace( ".", "" );
        sValue = sValue.toString().replace( ".", "" );
        sValue = sValue.toString().replace( "/", "" );
        sValue = sValue.toString().replace( "/", "" );
        sValue = sValue.toString().replace( ":", "" );
        sValue = sValue.toString().replace( ":", "" );
        sValue = sValue.toString().replace( "(", "" );
        sValue = sValue.toString().replace( "(", "" );
        sValue = sValue.toString().replace( ")", "" );
        sValue = sValue.toString().replace( ")", "" );
        sValue = sValue.toString().replace( " ", "" );
        sValue = sValue.toString().replace( " ", "" );
        fldLen = sValue.length;
        mskLen = sMask.length;

        i = 0;
        nCount = 0;
        sCod = "";
        mskLen = fldLen;

        while (i <= mskLen) 
        {
          bolMask = ((sMask.charAt(i) == "-") || (sMask.charAt(i) == ".") || (sMask.charAt(i) == "/") || (sMask.charAt(i) == ":"))
          bolMask = bolMask || ((sMask.charAt(i) == "(") || (sMask.charAt(i) == ")") || (sMask.charAt(i) == " "))

          if (bolMask) {
            sCod += sMask.charAt(i);
            mskLen++; }
          else {
            sCod += sValue.charAt(nCount);
            nCount++;
               }
          i++;
        }

        document.getElementById(objeto).value = sCod;

        if (nTecla != 8) { // backspace
          if (sMask.charAt(i-1) == "9") { // apenas números...
            return ((nTecla > 47) && (nTecla < 58)); } 
          else { // qualquer caracter...
            return true;
          } 
        }
        else {
          return true;
        }
}

 function fnValidaNroVirgula(e) 
 {
        var charCode;
        
        if(e && e.which){
                charCode = e.which;
        }
        else if(window.event){
                e = window.event;
                charCode = e.keyCode;
        }
        
        if ((charCode >= 48) && (charCode <= 57)) {
                  return;
        }
        else {
                if ((charCode == 8) || (charCode == 44)) {
                        return;
                }
                else {
                        return false;
                }
        }
}

 function fnValidaNro(e) 
 {
        var charCode;
        
        if(e && e.which){
                charCode = e.which;
        }
        else if(window.event){
                e = window.event;
                charCode = e.keyCode;
        }
        
        if ((charCode >= 48) && (charCode <= 57)) {
                  return;
        }
        else {
            return false;
               
        }
}

function CurrencyFormatted(amount)
{
	var i = parseFloat(amount);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}

function DisplayMessage(ID,text){
		var x = document.getElementById(ID); 
		if(x){
			x.innerHTML=text;	
		}
	}

function  calcTotal(txtqtd,txtvalor,txtdest){
    var q = $("#"+txtqtd).attr("value");
    var c = $("#"+txtvalor).attr("value");
    var t = 0;
    q = q.replace(",",".");
    c = c.replace(",",".");
    t = parseFloat(q) * parseFloat(c);
    t = CurrencyFormatted(t);
    t = t.toString().replace(".",",");
    DisplayMessage(txtdest,t);


}

function calcTotalPedidoCompra() {
    var a = '';
    var i = 0;
    var si = "";
    var f = 0;
    var sf = "";
    var t = 0;
    var st = "";

    $("span").each(function () {
        if (this.id.indexOf("lblValorTotal") > 0) {
            a = this.innerText;
            a = a.replace(",", ".");
            i = parseFloat(i) + parseFloat(a);
        }
    });
    i = CurrencyFormatted(i);
    si = i.toString().replace(".", ",");
    $("#txtValorItens").val(si);

    f = $("#txtFrete").attr("value");
    f = parseFloat(f);
    f = CurrencyFormatted(f);
    sf = f.toString().replace(".", ",");
    $("#txtFrete").val(sf);

    t = parseFloat(i) + parseFloat(f);
    t = CurrencyFormatted(t);
    st = t.toString().replace(".", ",");
    $("#txtValorTotal").val(st);

}

function  calcTotalOrdem(txtqtd,txtvalor,txtmarkup,txtdest){
    var q = $("#"+txtqtd).attr("value");
    var c = $("#"+txtvalor).attr("value");
    var m = $("#"+txtmarkup).attr("value");
    var t = 0;
    alert(q);
    alert(c);
    alert(m);
    q = q.replace(",",".");
    c = c.replace(",",".");
    c = m.replace(",",".");
    t = parseFloat(q) * parseFloat(c) * parseFloat(m);
    t = CurrencyFormatted(t);
    t = t.toString().replace(".",",");
    DisplayMessage(txtdest,t);


}





function AbrirModal(url) {
    jQuery.fn.modalBox({
        directCall: {
            data: '<iframe id="ifDetalhe" frameborder="0" scrolling="auto" height="600px" width="100%" src="' + url + '"></iframe>'
        }
        , setWidthOfModalLayer: 950
    });

}



function FecharModalCONF(msg) {
    if (confirm(msg)) jQuery.fn.modalBox("close");

}

function FecharModalMSG(msg) {
    if (msg != '') alert(msg);
    jQuery.fn.modalBox("close");
    //window.location.reload(true);
    window.location.href = window.location.href;

}

function FecharModalMSG_NoRefresh(msg) {
    if (msg != '') alert(msg);
    jQuery.fn.modalBox("close");
}


