﻿/*************************************************************************************
* Object Description:
* Author: T_Cinq.Mizael
* Date: 06/04/2010
**************************************************************************************
* History
* ------------------------------------------------------------------------------------
* Date        | Author               | Description
* ------------------------------------------------------------------------------------
* 06/04/2010   T_Cinq.Mizael	       Implementação.
* ------------------------------------------------------------------------------------
**************************************************************************************/
var ctrlNamePrefix = "ctl00$cphMain$";
var ctrlIDPrefix = "ctl00_cphMain_";
function clearString(text) { return text.replace(' ', '').replace('.', '').replace('-', '').replace('/', ''); }
/*Realiza um postback ASPNET a partir do javascript*/
function doPostback() { __doPostBack('', ''); }
function formatCEP(ctrl) { if (ctrl.value.length == 5) { ctrl.value += '-'; } }

/*Formata o conteúdo de um campo no formato moeda.*/
function formatCurrency(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
	document.selection.clear();
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode);
    if (strCheck.indexOf(key) == -1) return false;
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);
    aux += key;
    len = aux.length;
    if (len == 0) objTextBox.value = '';
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;
    if (len == 2) objTextBox.value = '0'+ SeparadorDecimal + aux;
    if (len > 2) {
        aux2 = '';
        for (j = 0, i = len - 3; i >= 0; i--) {
            if (j == 3) {
                aux2 += SeparadorMilesimo;
                j = 0;
            }
            aux2 += aux.charAt(i);
            j++;
        }
        objTextBox.value = '';
        len2 = aux2.length;
        for (i = len2 - 1; i >= 0; i--)
        objTextBox.value += aux2.charAt(i);
        objTextBox.value += SeparadorDecimal + aux.substr(len - 2, len);
    }
    return false;
}

function formatCurrencyPop(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
	document.selection.clear();
    var sep = 0;
    var key = '';
    var i = j = 0;
    var len = len2 = 0;
    var strCheck = '0123456789';
    var aux = aux2 = '';
    var whichCode = (window.Event) ? e.which : e.keyCode;
    if (whichCode == 13) return true;
    key = String.fromCharCode(whichCode);
    if (strCheck.indexOf(key) == -1) return false;
    len = objTextBox.value.length;
    for(i = 0; i < len; i++)
        if ((objTextBox.value.charAt(i) != '0') && (objTextBox.value.charAt(i) != SeparadorDecimal)) break;
    aux = '';
    for(; i < len; i++)
        if (strCheck.indexOf(objTextBox.value.charAt(i))!=-1) aux += objTextBox.value.charAt(i);    
    len = aux.length;    
    if (len == 0) objTextBox.value = '0'+ SeparadorDecimal + '0' + aux;	
    if (len == 1) objTextBox.value = '0'+ SeparadorDecimal + aux;	
	if (len >= 2)
	{
		var prefix = aux.substr(0, aux.length - 1);
		var sufix = aux.substr(aux.length - 1, 1);
		if (prefix.length + sufix.length < 9)		
			objTextBox.value = prefix + SeparadorDecimal + sufix;
		else
		{
			prefix = aux.substr(0, aux.length - 2);
			sufix = aux.substr(aux.length - 2, 2);
			objTextBox.value = prefix + SeparadorDecimal + sufix;
		}		
	}	
    return false;
}
/*Formata o conteúdo de um campo no formato moeda.*/
function formatDate(ctrl) { if (ctrl.value.length == 2 || ctrl.value.length == 5) { ctrl.value += '/'; } }
function formatTelefone(ctrl) { if (ctrl.value.length == 2) ctrl.value += ' '; if (ctrl.value.length == 7) ctrl.value += '-'; }
function jFormatTelefone(ctrl) { if (ctrl.val().length == 2) ctrl.val(ctrl.val() + ' '); if (ctrl.val().length == 7) ctrl.val(ctrl.val() + '-'); }
function onlyNumbers(e) { var tecla = (e.which) ? e.which : e.keyCode; if ((tecla > 47 && tecla < 58) || (tecla == 8 || tecla == 9)) return true; else return false; }
function showMoreInformation(c, t) { if ($("#dlgMoreInformation").css("display") == "none") $("#dlgMoreInformation").html(t.val()).show().position({ my: "left top", at: "left bottom", of: c }); else $("#dlgMoreInformation").hide(); return false; }
function showOfDiv(div) { if (!div) { return; } div = typeof div === "string" ? document.getElementById(div) : div; var elms = div.getElementsByTagName("*"); for (var i = 0, maxI = elms.length; i < maxI; ++i) { var elm = elms[i]; switch (elm.type) { case "text": case "textarea": case "button": case "reset": case "submit": case "file": case "hidden": case "password": case "image": case "radio": case "checkbox": case "select-one": case "select-multiple": alert("Type: " + elm.type + "\nName: " + elm.name + "\nId: " + elm.id); } } }
function validateCNPJ(cnpj) { cnpj = clearString(cnpj); var c = cnpj.substr(0, 12); var dv = cnpj.substr(12, 2); var d1 = 0; for (var i = 0; i < 12; i++) { d1 += c.charAt(11 - i) * (2 + (i % 8)); } if (d1 == 0) return false; d1 = 11 - (d1 % 11); if (d1 > 9) d1 = 0; if (dv.charAt(0) != d1) return false; d1 *= 2; for (i = 0; i < 12; i++) { d1 += c.charAt(11 - i) * (2 + ((i + 1) % 8)); } d1 = 11 - (d1 % 11); if (d1 > 9) d1 = 0; if (dv.charAt(1) != d1) return false; return true; }
function validateCPF(cpf) { cpf = clearString(cpf); var c = cpf.substr(0, 9); var dv = cpf.substr(9, 2); var d1 = 0; for (var i = 0; i < 9; i++) { d1 += c.charAt(i) * (10 - i); } if (d1 == 0) return false; d1 = 11 - (d1 % 11); if (d1 > 9) d1 = 0; if (dv.charAt(0) != d1) return false; d1 *= 2; for (i = 0; i < 9; i++) { d1 += c.charAt(i) * (11 - i); } d1 = 11 - (d1 % 11); if (d1 > 9) d1 = 0; if (dv.charAt(1) != d1) return false; return true; }
/*Função para substituir a função alert*/
function jAlert(msg, title, callback) { if (title == null) title = "Aviso"; msg = msg.replace(/\n/g, '<br />'); $("body").append("<div id='dialog-alerts' style='display:none;' title='" + title + "'><p><table><tr><td style='width:10%;'><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span></td><td>" + msg + "</td></tr></table></p></div> "); $(function () { $("#dialog").dialog("destroy"); $("#dialog-alerts").dialog({ resizable: true, minHeight: 200, maxHeight: 600, width:700, minWidth: 500, maxWidth: 800, modal: true/*, show: 'scale', hide: 'clip'*/, buttons: { "OK": function () { if (callback) callback(true); $(this).dialog('close'); } }, close: function () { $("#dialog-alerts").dialog("destroy"); $("#dialog-alerts").remove(); } }); }); return false; }
/*Função para substituir a função confirm*/
function jConfirm(msg, title, callback) { if (title == null) title = "Confirmação"; msg = msg.replace(/\n/g, '<br />'); $("body").append("<div id='dialog-alerts' style='display:none;' title='" + title + "'><p><span class='ui-icon ui-icon-info' style='float:left; margin:0 7px 20px 0;'></span>" + msg + "</p></div> "); $(function () { $("#dialog").dialog("destroy"); $("#dialog-alerts").dialog({ resizable: true, minHeight: 200, maxHeight: 600, width: 700, minWidth: 500, maxWidth: 800, modal: true, /*show: 'scale', hide: 'clip',*/ buttons: { "Sim": function () { if (callback) callback(true); $(this).dialog('close'); }, "Não": function () { if (callback) callback(false); $(this).dialog('close'); } }, close: function () { $("#dialog-alerts").dialog("destroy"); $("#dialog-alerts").remove(); } }); }); return false; }
/*Função para substituir a função prompt*/
function jPrompt(msg, value, title, callback) { if (title == null) title = "Informação"; if (value == null) value = ""; msg = msg.replace(/\n/g, '<br />'); $("body").append("<div id='dialog-alerts' style='display:none;' title='" + title + "'><p><span class='ui-icon ui-icon-help' style='float:left; margin:0 7px 20px 0;'></span>" + msg + "<br /><input id='txtUserInput' type='text' size='30' class='text' /></p></div> "); $("#txtUserInput").val(value); $(function () { $("#dialog").dialog("destroy"); $("#dialog-alerts").dialog({ resizable: true, height: 300, minHeight: 300, maxHeight: 600, width: 500, minWidth: 500, maxWidth: 600, modal: true, show: 'scale', hide: 'clip', buttons: { "OK": function () { $(this).dialog('close'); if (callback) callback($("#txtUserInput").val()); }, "Cancelar": function () { $(this).dialog('close'); if (callback) callback(null); } }, close: function () { $("#dialog-alerts").dialog("destroy"); $("#dialog-alerts").remove(); } }); }); return false; }
/*Exibe ou oculta uma tooltip para o controle informado*/
function jToolTip(target, content, show) { if (!content) return; if (!target) return; if (!$(document.body).is('[role]')) $(document.body).attr('role', 'application'); target.attr("title", ""); var ttId = "tt" + target.attr("id"); if (show) { $("body").append("<div id='" + ttId + "' class='ui-tooltip ui-widget ui-corner-all' style='max-width:400px; padding:10 10 10 10; overflow:hidden;'><div class='ui-widget-content'><div style='max-width:390px; padding: 10 10 10 10;margin: 10 10 10 10;overflow:hidden;'>" + content + "</div></div>"); $("#" + ttId).css({ top: 0, left: 0 }).position({ my: "left top", at: "right button", offset: "0 0", of: target }); $("#" + ttId).attr("aria-hidden", "false"); target.attr("aria-describedby", $("#" + ttId).attr("id")); $("#" + ttId).show("slow"); } else { target.removeAttr("aria-describedby"); $("#" + ttId).attr("aria-hidden", "true"); $("#" + ttId).hide("slow"); $("#" + ttId).remove(); $("body").remove($("#" + ttId)); } }
/*Simula a função Left do VBScript*/
function left(value, length) { var result = ""; for (var cnt = 0; cnt < length - value.length; cnt++) result += "0"; result = result + value; result = result.substring(0, length); return result; }
/* Substitui caracteres especiais para seus equivalentes HTML */
function specialCharsToHTML(str) { str = str.replace(/Á/g, "&Aacute;").replace(/º/g, "&ordm;").replace(/á/g, "&aacute;").replace(/Â/g, "&Acirc;").replace(/â/g, "&acirc;").replace(/À/g, "&Agrave;").replace(/à/g, "&agrave;").replace(/Å/g, "&Aring;").replace(/å/g, "&aring;").replace(/Ã/g, "&Atilde;").replace(/ã/g, "&atilde;").replace(/Ä/g, "&Auml;").replace(/ä/g, "&auml;").replace(/Æ/g, "&AElig;").replace(/æ/g, "&aelig;").replace(/É/g, "&Eacute;").replace(/é/g, "&eacute;").replace(/Ê/g, "&Ecirc;").replace(/ê/g, "&ecirc;").replace(/È/g, "&Egrave;").replace(/è/g, "&egrave;").replace(/Ë/g, "&Euml;").replace(/ë/g, "&euml;").replace(/Ð/g, "&ETH;").replace(/ð/g, "&eth;").replace(/Í/g, "&Iacute;").replace(/í/g, "&iacute;").replace(/Î/g, "&Icirc;").replace(/î/g, "&icirc;").replace(/Ì/g, "&Igrave;").replace(/ì/g, "&igrave;").replace(/Ï/g, "&Iuml;").replace(/ï/g, "&iuml;").replace(/Ó/g, "&Oacute;").replace(/ó/g, "&oacute;").replace(/Ô/g, "&Ocirc;").replace(/ô/g, "&ocirc;").replace(/Ò/g, "&Ograve;").replace(/ò/g, "&ograve;").replace(/Ø/g, "&Oslash;").replace(/ø/g, "&oslash;").replace(/Õ/g, "&Otilde;").replace(/õ/g, "&otilde;").replace(/Ö/g, "&Ouml;").replace(/ö/g, "&ouml;").replace(/Ú/g, "&Uacute;").replace(/ú/g, "&uacute;").replace(/Û/g, "&Ucirc;").replace(/û/g, "&ucirc;").replace(/Ù/g, "&Ugrave;").replace(/ù/g, "&ugrave;").replace(/Ü/g, "&Uuml;").replace(/ü/g, "&uuml;").replace(/Ç/g, "&Ccedil;").replace(/ç/g, "&ccedil;").replace(/Ñ/g, "&Ntilde;").replace(/ñ/g, "&ntilde;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/&/g, "&amp;").replace(/\"/g, "&quot;").replace(/®/g, "&reg;").replace(/©/g, "&copy;").replace(/Ý/g, "&Yacute;").replace(/ý/g, "&yacute;").replace(/Þ/g, "&THORN;").replace(/þ/g, "&thorn;").replace(/ß/g, "&szlig;");	 return str; }	
/*Substitui caracteres HTML pelos seus caracteres equivalentes*/
function HtmlToSpecialChars(str){ str = str.replace(/&amp;/g, "&").replace(/&ordm;/g, "º").replace(/&Aacute;/g, "Á").replace(/&aacute;/g, "á").replace(/&Acirc;/g, "Â").replace(/&acirc;/g, "â").replace(/&Agrave;/g, "À").replace(/&agrave;/g, "à").replace(/&Aring;/g, "Å").replace(/&aring;/g, "å").replace(/&Atilde;/g, "Ã").replace(/&atilde;/g, "ã").replace(/&Auml;/g, "Ä").replace(/&auml;/g, "ä").replace(/&AElig;/g, "Æ").replace(/&aelig;/g, "æ").replace(/&Eacute;/g, "É").replace(/&eacute;/g, "é").replace(/&Ecirc;/g, "Ê").replace(/&ecirc;/g, "ê").replace(/&Egrave;/g, "È").replace(/&egrave;/g, "è").replace(/&Euml;/g, "Ë").replace(/&euml;/g, "ë").replace(/&ETH;/g, "Ð").replace(/&eth;/g, "ð").replace(/&Iacute;/g, "Í").replace(/&iacute;/g, "í").replace(/&Icirc;/g, "Î").replace(/&icirc;/g, "î").replace(/&Igrave;/g, "Ì").replace(/&igrave;/g, "ì").replace(/&Iuml;/g, "Ï").replace(/&iuml;/g, "ï").replace(/&Oacute;/g, "Ó").replace(/&oacute;/g, "ó").replace(/&Ocirc;/g, "Ô").replace(/&ocirc;/g, "ô").replace(/&Ograve;/g, "Ò").replace(/&ograve;/g, "ò").replace(/&Oslash;/g, "Ø").replace(/&oslash;/g, "ø").replace(/&Otilde;/g, "Õ").replace(/&otilde;/g, "õ").replace(/&Ouml;/g, "Ö").replace(/&ouml;/g, "ö").replace(/&Uacute;/g, "Ú").replace(/&uacute;/g, "ú").replace(/&Ucirc;/g, "Û").replace(/&ucirc;/g, "û").replace(/&Ugrave;/g, "Ù").replace(/&ugrave;/g, "ù").replace(/&Uuml;/g, "Ü").replace(/&uuml;/g, "ü").replace(/&Ccedil;/g, "Ç").replace(/&ccedil;/g, "ç").replace(/&Ntilde;/g, "Ñ").replace(/&ntilde;/g, "ñ").replace(/&lt;/g, "<").replace(/&gt;/g, ">").replace(/&amp;/g, "/").replace(/&quot;/g, "\"").replace(/&reg;/g, "®").replace(/&copy;/g, "©").replace(/&Yacute;/g, "Ý").replace(/&yacute;/g, "ý").replace(/&THORN;/g, "Þ").replace(/&thorn;/g, "þ").replace(/&szlig;/g, "ß"); return str; }		
$(document).ready(function () {
    /*Configura os campos do tipo data*/
    $(".datefield").datepicker().attr("maxlength", "10").keypress(function (e) { if (isNaN(String.fromCharCode(e.which)) && e.which != 0 && e.which != 8 && e.which != 13) return false; if ($(this).val().length == 2 || $(this).val().length == 5) $(this).val($(this).val() + "/"); }).blur(function () { var day = $(this).val().substr(0, $(this).val().indexOf("/")); var month = $(this).val().substr($(this).val().indexOf("/") + 1, 2); var year = $(this).val().substr($(this).val().lastIndexOf("/") + 1, 4); var date = new Date(); date.setFullYear(year, month - 1, day); if (date.getDate() != day || date.getMonth() != month - 1 || date.getFullYear() != year || parseInt(year) < 1900) $(this).val(""); });
    /*Configura os campos do tipo número inteiro*/
    $(".numberfield").keypress(function (e) { if (isNaN(String.fromCharCode(e.which)) && e.which != 0 && e.which != 8 && e.which != 13) return false; });
    /*Configura os campos do tipo número decimal*/
    $(".decimalField").keypress(function (e) { if (isNaN(String.fromCharCode(e.which)) && e.which != 0 && e.which != 8 && e.which != 13 && e.which != 44) return false; });
    /*Configura os campos do tipo telefone*/
    $(".phonefield").attr("maxlength", "12");
    $(".phonefield").keypress(function (e) { if (isNaN(String.fromCharCode(e.which)) && e.which != 0 && e.which != 8 && e.which != 13) return false; /*document.selection.clear();*/var value = $.trim($(this).val()).replace(" ", ""); if (value.length >= 2) value = $.trim(value.substr(0, 2)) + " " + $.trim(value.substr(2, value.length - 1)); $(this).val(value); });
    $(".phonefield").each(function () { var value = $.trim($(this).val()).replace(" ", ""); if (value.length >= 2) value = $.trim(value.substr(0, 2)) + " " + $.trim(value.substr(2, value.length - 1)); $(this).val(value); });
	$(".currencyfield").keypress(function(e) { 
		if (isNaN(String.fromCharCode(e.which))) 
			return false;	
		value = $(this).val().replace(",", "").replace(".", "");		
		var prefix = "0";
		var sufix = "00";
		if (value.length <= 2)
			sufix = left(value, 2);
		if (value.length >= 3)
		{
			sufix = value.substring(value.lenght - 2, 2);
			prefix = value.substring(0, value.length -2);									
		}		
		$(this).val(prefix + "," + sufix);
	});	    
});
