
function trim(str){
    while (str.charAt(0) == " ")
    str = str.substr(1,str.length -1);

    while (str.charAt(str.length-1) == " ")
    str = str.substr(0,str.length-1);

    return str;
}

function limpa_string(s){
    var Digitos = "0123456789";
    var temp = "";
    var digito = "";
    for (var i=0; i<s.length; i++)  {
        digito = s.charAt(i);
        if (Digitos.indexOf(digito)>=0) {
        temp=temp+digito;   }
    }
    return temp;
}

function valida_CPF(s)
{

    var i;
    s = limpa_string(s);
    var c = s.substr(0,9);
    var dv = s.substr(9,2);
    var d1 = 0;
    for (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);
    }

    if(s=='11111111111' ||  s=='22222222222' || s=='33333333333' || s=='44444444444' || s=='55555555555' || s=='66666666666' || s=='77777777777' || s=='88888888888' || s=='99999999999' || s=='00000000000')
        return(false);
    else
        return(true);
}

function valida_CNPJ(s)
{
    var i;
    s = limpa_string(s);
    var c = s.substr(0,12);
    var dv = s.substr(12,2);
    var d1 = 0;
    for (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 getHTMLfunction(url,pars,target,method,funcao){
		var myAjax = new Ajax.Updater(
			target, 
			url, 
			{
				method: method, 
				parameters: pars,
				onComplete: eval(funcao)
			});}
function getHTMLscript(url,pars,target,method){
	$('div_loading').show();
		var myAjax = new Ajax.Updater(
			target, 
			url, 
			{
				method: method, 
				parameters: pars,
				evalScripts: true
			});}
function getHTMLscriptfront(url,pars,target,method){
		var myAjax = new Ajax.Updater(
			target, 
			url, 
			{
				method: method, 
				parameters: pars,
				evalScripts: true
			});}
function getHTMLFormscript(url,formulario,target,method){
	show_div('div_loading');
		var pars = Form.serialize(formulario);
		var myAjax = new Ajax.Updater(
			target, 
			url, 
			{
				method: method, 
				parameters: pars,
				evalScripts: true
			});}
function getHTMLFormscript1(url,formulario,target,method){
		var pars = Form.serialize(formulario);
		var myAjax = new Ajax.Updater(
			target, 
			url, 
			{
				method: method, 
				parameters: pars,
				evalScripts: true
			});}
function getHTMLFormscriptFront(url,formulario,target,method){
		var pars = Form.serialize(formulario);
		var myAjax = new Ajax.Updater(
			target, 
			url, 
			{
				method: method, 
				parameters: pars,
				evalScripts: true
			});}
		
function getHTMLFormfunction(url,formulario,target,method,funcao){
		var pars = Form.serialize(formulario);
		var myAjax = new Ajax.Updater(
			target, 
			url, 
			{
				method: method, 
				parameters: pars,
				evalScripts: true
			});}
function show_div(obj){
	$(obj).style.display='block';}
function hide_div(obj){
	$(obj).style.display='none';}
function hide_div_parent(obj){
	parent.document.getElementById(obj).style.display = 'none';}

function url_encode(str) {  
        var hex_chars="0123456789ABCDEF";  
        var noEncode=/^([a-zA-Z0-9\_\-\.])$/;  
        var n, strCode, hex1, hex2, strEncode = "";  
        for(n=0;n<str.length;n++) {  
            if (noEncode.test(str.charAt(n))) {  
                strEncode+=str.charAt(n);  
            } else {  
                strCode=str.charCodeAt(n);  
                hex1=hex_chars.charAt(Math.floor(strCode / 16));  
                hex2=hex_chars.charAt(strCode % 16);  
                strEncode += "%" + (hex1 + hex2);  
            }  
        }  
        return strEncode;}  
 
function url_decode(str){  
        var n, strCode, strDecode = "";  
        for (n=0;n<str.length;n++) {  
            if (str.charAt(n) == "%") {  
                strCode=str.charAt(n + 1) + str.charAt(n + 2);  
                strDecode += String.fromCharCode(parseInt(strCode, 16));  
                n += 2;  
            } else {  
                strDecode += str.charAt(n);  
            }  
        }  
        return strDecode;}

function textCounter(field, countfield, maxlimit) {
if ($(field).value.length > maxlimit) // if too long...trim it!
$(field).value = $(field).value.substring(0, maxlimit);
else 
$(countfield).value = maxlimit - $(field).value.length;
}
function abrePopUP(URL,nome,comprimento,altura){window.open(URL,nome,'top=100,left=200,width='+comprimento+',height='+altura+'');}

function doClick(index, numTabs, id, class_on, class_off) {
$("tab"+id+index).className=class_on;
for (var i=1; i < numTabs; i++) {
$("tab"+id+(index+i)%numTabs).className=class_off;
}
$("ev"+id+index).style.display="block";
for (var j=1; j < numTabs; j++) {
$("ev"+id+(index+j)%numTabs).style.display="none";
}
}
function rePosition(objbase,objpos,pX,pY){
	var obj=$(objbase);
	var curleft=curtop=0;
	if (obj.offsetParent) {
		curleft=obj.offsetLeft
		curtop=obj.offsetTop
		while (obj=obj.offsetParent) {
			curleft+=obj.offsetLeft
			curtop+=obj.offsetTop
		}
	}
	var x=curleft;
  	var y=curtop;
	$(objpos).style.left=pX+"%";
    $(objpos).style.top=y+pY+"px";		
}
function loadRpos(obj,objPos,vLeft,vTop){
	var obj=$(obj);
	var curleft=curtop=0;
	if (obj.offsetParent) {
		curleft=obj.offsetLeft
		curtop=obj.offsetTop
		while (obj=obj.offsetParent) {
			curleft+=obj.offsetLeft
			curtop+=obj.offsetTop
		}
	}
	var x=curleft;
  	var y=curtop;
	$(objPos).style.left=x+vLeft+"px";
    $(objPos).style.top=y+vTop+"px";		
}

function montaCombo(url,target,vlr_campo,tipo,vlr_select){
    getHTMLscript(url,'vlr='+vlr_campo+'&tp='+tipo+'&selected='+vlr_select,target,'get');
}

function SoNumero(e) {
    if (window.event)
    {tecla=e.keyCode;}
    else if (e.which)
    {tecla=e.which;}
    if (tecla==8 || tecla==13)
     {event.keyCode=0; 
     }else if (tecla<40 && tecla>37)
	 {event.keyCode=0;}
    if (tecla!=8 || tecla<48 || tecla>57) return ((tecla>47) && (tecla<58))
    {return true;}
}


function val_num(campo)
{
v = document.getElementById(campo).value;
RegExp = /^[0-9]+$/;
if (!RegExp.test(v))
    {
    alert("Campo só pode conter números!");
    return false;
    }  
  return true;
}

function SoNumero(e) {
    if (window.event)
    {tecla=e.keyCode;}
    else if (e.which)
    {tecla=e.which;}
    if (tecla==8 || tecla==13)
     {event.keyCode=0; 
     }else if (tecla<40 && tecla>37)
	 {event.keyCode=0;}
    if (tecla!=8 || tecla<48 || tecla>57) return ((tecla>47) && (tecla<58))
    {return true;}
}



function moeda(z){  
v = z.value;
v=v.replace(/\D/g,"")  //permite digitar apenas nmeros
v=v.replace(/[0-9]{12}/,"inválido")   //limita pra mximo 999.999.999,99
//v=v.replace(/(\d{1})(\d{8})$/,"$1.$2")  //coloca ponto antes dos ltimos 8 digitos
//v=v.replace(/(\d{1})(\d{5})$/,"$1.$2")  //coloca ponto antes dos ltimos 5 digitos
v=v.replace(/(\d{1})(\d{1,2})$/,"$1,$2")    //coloca virgula antes dos ltimos 2 digitos
z.value = v;
}
 function validaDat(campo,valor,id) {
	var date=valor;
	var ardt=new Array;
	var ExpReg=new RegExp("(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/[12][0-9]{3}");
	ardt=date.split("/");
	erro=false;
	if ( date.search(ExpReg)==-1){
		erro = true;
		}
	else if (((ardt[1]==4)||(ardt[1]==6)||(ardt[1]==9)||(ardt[1]==11))&&(ardt[0]>30))
		erro = true;
	else if ( ardt[1]==2) {
		if ((ardt[0]>28)&&((ardt[2]%4)!=0))
			erro = true;
		if ((ardt[0]>29)&&((ardt[2]%4)==0))
			erro = true;
	}
	if (erro) {
		alert("\"" + valor + "\" não é uma data válida, verifique.");
		campo.focus();
		campo.value = "";
		return false;
	}

	window.open('http://www.solitica.com.br/boletos/back_boleto.asp?Dt='+valor,'Boleto','scrollbars=yes','width=768,height=2024');
	
	return true;

}






//Exceptions
var myGlobalHandlers = {
				onException: function(request, exception) {
					alert("Js Exception: " + exception);
				}
				};
				
				Ajax.Responders.register(myGlobalHandlers);
