var ie=document.all;
var nn6=document.getElementById&&!document.all;
var isdrag=false;
var x,y;
var dobj;

function movemouse(e)
{
  if (isdrag)
  {
    dobj.style.left = nn6 ? tx + e.clientX - x : tx + event.clientX - x;
    dobj.style.top  = nn6 ? ty + e.clientY - y : ty + event.clientY - y;
    return false;
  }
}

function selectmouse(e)
{
  var fobj       = nn6 ? e.target : event.srcElement;
  var topelement = nn6 ? "HTML" : "BODY";
  while (fobj.tagName != topelement && fobj.className != "dragme")
  {
    fobj = nn6 ? fobj.parentNode : fobj.parentElement;
  }

  if (fobj.className=="dragme")
  {
    isdrag = true;
    dobj = fobj;
    tx = parseInt(dobj.style.left+0);
    ty = parseInt(dobj.style.top+0);
    x = nn6 ? e.clientX : event.clientX;
    y = nn6 ? e.clientY : event.clientY;
    document.onmousemove=movemouse;
    return false;
  }

}

document.onmousedown=selectmouse;
document.onmouseup=new Function("isdrag=false");

//----------------------------------------------------------------------
//----------------------------INDICE------------------------------------
//
//0014  upper_0014
//0015  aplicarMascara_0015
//0016  isNumberKey_0016
//0018  currencyFormatInput_0018
//0019	dateFormatInput_0019
//0020	fieldOnFocus_0020
//0021	fieldOnBlur_0021
//0022	numberAccountFormatInput_0022
//0023	validateCPFInput_0023
//0024	validateCNPJInput_0024
//0025	removeCaracteres_0025
//0026	validateEmailInput_0026
//0027	isCharKey_0027
//0028	lower_0028
//0031	isCharKeyEnter_0031
//0033	aplicarMascaraCpfCnpj_0033
//0034	porcentagemFormatInput_0034
//0035	isCharNumberLoginKey_0035
//0036	isCharNumberDigitoContaCorrente_0036
//0038	aplicarMascaraDataCep_0038
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
function upper_0014(pText) {
	if (pText == null) {
		return false;
	};

	pText.value = pText.value.toUpperCase();
	return true;
};
//-----------------------------------------------------------------------------
function aplicarMascara_0015(src, mask){
	var i = src.value.length;
	var saida = mask.substring(0,1);
	var texto = mask.substring(i);
	if (texto.substring(0,1) != saida){
	    src.value += texto.substring(0,1);
	};
}
//-----------------------------------------------------------------------------
function isNumberKey_0016(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;

	if (isCharKeyEnter_0031(evt)){	
		// Permissão de teclas pré-definidas para liberdade de navegação dentro do campo:
		// 8 - Backspace, 9 - TAB
		// 46 - Delete, 36 - Home
		// 35 - End,  37 - Seta Esquerda, 39 - Seta Direita
		if ((charCode == 8) || (charCode == 9) || (charCode == 46) || (charCode == 36) || (charCode == 35) || (charCode == 37) || (charCode == 39))
			return true;
	
		if (charCode > 31 && (charCode < 48)||(charCode > 57)){
	    	if ((charCode < 96)||(charCode > 106)){
				return false;
			}
		}
	
		return true;
	} else {
		return false;
	}
}
//-----------------------------------------------------------------------------
function currencyFormatInput_0018(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.addEventListener) ? e.which : event.keyCode;

	if (isCharKeyEnter_0031(e)){	
		// Permissão de teclas pré-definidas para liberdade de navegação dentro do campo:
		// 8 - Backspace, 9 - TAB
		// 46 - Delete, 36 - Home
		// 35 - End,  37 - Seta Esquerda, 39 - Seta Direita
		if ((whichCode == 8) || (whichCode == 9) || (whichCode == 46) || (whichCode == 36) || (whichCode == 35) || (whichCode == 37) || (whichCode == 37))
			return true;
	
		if (whichCode == 96){
			key = "0";
		} else {
			if (whichCode == 97){
				key = "1";
			} else {
				if (whichCode == 98){
					key = "2";
				} else {
					if (whichCode == 99){
						key = "3";
					} else {
						if (whichCode == 100){
							key = "4";
						} else {
							if (whichCode == 101){
								key = "5";
							} else {
								if (whichCode == 102){
									key = "6";
								} else {
									if (whichCode == 103){
										key = "7";
									} else {
										if (whichCode == 104){
											key = "8";
										} else {
											if (whichCode == 105){
												key = "9";
											} else {
												key = String.fromCharCode(whichCode); // Valor para o cÃ³digo da Chave
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
	
		if (strCheck.indexOf(key) == -1)
			return false; // Chave invÃ¡lida
		len = objTextBox.value.length;
	
		if(len >= 9)
			return false;
	
		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;
	} else {
		return false;
	}
}
//-----------------------------------------------------------------------------
function dateFormatInput_0019(campo) {
	var date=campo.value;
	var ardt=new Array;
	var corCampo = '#FF6666';
	var ExpReg = new RegExp("(0[1-9]|[12][0-9]|3[01])/(0[1-9]|1[012])/[12][0-9]{3}");
	var mensagem = "data inválida, ela é maior que Data corrente.";
	var dataDigitada = campo.value.split('/');
	ardt=date.split("/");
	
	var vDataAtual = new Date();
	var vDataDigitada = new Date();
	vDataDigitada.setDate(dataDigitada[0]);
	vDataDigitada.setMonth(dataDigitada[1] - 1);
	vDataDigitada.setFullYear(dataDigitada[2]);

	erro=false;
	erro2=false;
	
	if (date.length > 0){
		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 (vDataDigitada > vDataAtual){
			erro2 = true;
		}
		
		if (erro) {
			campo.style.backgroundColor = corCampo;
			alert("Data Inválida!");
			return false;
		} else {
			if (erro2){
				campo.style.backgroundColor = corCampo;
				alert(campo.value + " " + mensagem);
				campo.value = "";				
				return false;
			}
		}
		
	}
	return true;
}
//-----------------------------------------------------------------------------
function fieldOnFocus_0020(campo) {
	campo.style.backgroundColor='#FAEDDA';
}
//-----------------------------------------------------------------------------
function fieldOnBlur_0021(campo) {
	campo.style.backgroundColor='#FFFFFF';
}
//-----------------------------------------------------------------------------
function numberAccountFormatInput_0022(e){
	var charCode = (window.addEventListener) ? e.which : e.keyCode;

	if (isCharKeyEnter_0031(e)){
		// Permissão de teclas pré-definidas para liberdade de navegação dentro do campo:
		// 8 - Backspace, 9 - TAB
		// 46 - Delete, 36 - Home
		// 35 - End,  37 - Seta Esquerda, 39 - Seta Direita
		if ((charCode == 8) || (charCode == 9) || (charCode == 46) || (charCode == 36) || (charCode == 35) || (charCode == 37) || (charCode == 39))
			return true;
	
		if (charCode > 31 && (charCode < 48)||(charCode > 57)){
	    	if ((charCode < 96)||(charCode > 106)){
				return false;
			}
		}
	
		return true;
	} else {
		return false;
	}
}
//-----------------------------------------------------------------------------
function validateCPFInput_0023(Ncampo){
	var corCampo = '#FF6666';
	var mensagem = "Numero de CPF inválido!";
	
	erro = new String;
	cpf=Ncampo.value;
  if (cpf.length == 14)
	 {
		cpf = cpf.replace('.', '');
		cpf = cpf.replace('.', '');
		cpf = cpf.replace('-', '');

		var nonNumbers = /\D/;

		if (cpf == "00000000000" ||	cpf == "11111111111" || cpf == "22222222222" ||	cpf == "33333333333" || cpf == "44444444444" || cpf == "55555555555" ||
			cpf == "66666666666" || cpf == "77777777777" || cpf == "88888888888" ||	cpf == "99999999999") {
			erro = mensagem;
		}

		var a = [];
		var b = new Number;
		var c = 11;

		for (i=0; i<11; i++){
			a[i] = cpf.charAt(i);
			if (i < 9) b += (a[i] * --c);
		}

		if ((x = b % 11) < 2) { 
			a[9] = 0;
		} else { 
			a[9] = 11-x;
		}
		
		b = 0;
		c = 11;

		for (y=0; y<10; y++) 
			b += (a[y] * c--);

		if ((x = b % 11) < 2) { 
			a[10] = 0; 
		} else { 
			a[10] = 11-x; 
		}

		if ((cpf.charAt(9) != a[9]) || (cpf.charAt(10) != a[10])) {
			erro = mensagem;
		}
	} else {
		if(cpf.length == 0)	{
			return false;
		} else {
			erro = mensagem;
		}
	}
	if (erro.length > 0) {
		Ncampo.style.backgroundColor = corCampo;
		alert(erro);

		if (cpf.length == 11){
			str = cpf.value;
			str2 = cpf.value.substring(0,3);
			str3 = cpf.value.substring(3,6);
			str4 = cpf.value.substring(6,9);
			str5 = cpf.value.substring(9,12);
			result=str.replace(str,str2+'.');
			result2=str.replace(str,str3+'.');
			result3=str.replace(str,str4+'-');
			return false;
		}

		return false;
	}
	
	str = cpf.value;
	str2 = cpf.value.substring(0,3);
	str3 = cpf.value.substring(3,6);
	str4 = cpf.value.substring(6,9);
	str5 = cpf.value.substring(9,12);
	result=str.replace(str,str2+'.');
	result2=str.replace(str,str3+'.');
	result3=str.replace(str,str4+'-');
	Ncampo.value = result + result2 + result3 + str5;
	
	return true;
}
//-----------------------------------------------------------------------------
function validateCNPJInput_0024(campo){

	var numeros, digitos, soma, i, resultado, pos, tamanho, digitos_iguais;
	var mensagem = "CNPJ inválido!";
	var corCampo = '#FF6666';
	var numCnpj = campo.value;
	
	if (numCnpj.length > 0){
		digitos_iguais = 1;
		
		numCnpj = removeCaracteres_0025(numCnpj, ".");
		numCnpj = removeCaracteres_0025(numCnpj, "-");
		numCnpj = removeCaracteres_0025(numCnpj, "/");
		
		if (numCnpj.length < 14 && numCnpj.length < 15){
			campo.style.backgroundColor = corCampo;
			window.alert(mensagem);
			return false;
		}
	
		for (i = 0; i < numCnpj.length - 1; i++)
			if (numCnpj.charAt(i) != numCnpj.charAt(i + 1)){
	            digitos_iguais = 0;
	            break;
			}
		
		if (!digitos_iguais){
			tamanho = numCnpj.length - 2
			numeros = numCnpj.substring(0,tamanho);
			digitos = numCnpj.substring(tamanho);
			soma = 0;
			pos = tamanho - 7;
			for (i = tamanho; i >= 1; i--){
	            soma += numeros.charAt(tamanho - i) * pos--;
	            if (pos < 2)
	                  pos = 9;
			}
			
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
	      
			if (resultado != digitos.charAt(0)){
				campo.style.backgroundColor = corCampo;
				window.alert(mensagem);
				return false;
			}
	      
			tamanho = tamanho + 1;
			numeros = numCnpj.substring(0,tamanho);
			soma = 0;
			
			pos = tamanho - 7;
			for (i = tamanho; i >= 1; i--){
				soma += numeros.charAt(tamanho - i) * pos--;
	            if (pos < 2)
	                  pos = 9;
			}
	      
			resultado = soma % 11 < 2 ? 0 : 11 - soma % 11;
			if (resultado != digitos.charAt(1)){
				campo.style.backgroundColor = corCampo;
				window.alert(mensagem);
				return false;
			}
		} else {
			campo.style.backgroundColor = corCampo;
			window.alert(mensagem);
			return false;
		}
	}
	return true;
} 
//-----------------------------------------------------------------------------
function removeCaracteres_0025(str, sub) {
 i = str.indexOf(sub);
 r = "";
 if (i == -1) return str;
 r += str.substring(0,i) + removeCaracteres_0025(str.substring(i + sub.length), sub);
 return r;
}
//-----------------------------------------------------------------------------
function validateEmailInput_0026(campo) {
	var valueEmail = campo.value;
	var mensagem = "E-mail inválido!";
	var corCampo = '#FF6666';
	
	if(valueEmail.length > 0) {
		if(!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(valueEmail))) {
			//campo.style.backgroundColor = corCampo;
			window.alert(mensagem);
			return false;
		}
	}	
	return true;
}
//-----------------------------------------------------------------------------
function isCharKey_0027(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;

	if (isCharKeyEnter_0031(evt)){	
		// Permissão de teclas pré-definidas para liberdade de navegação dentro do campo:
		// 8 - Backspace, 9 - TAB 
		// 46 - Delete, 36 - Home
		// 35 - End,  37 - Seta Esquerda, 39 - Seta Direita
		if ((charCode == 8) || (charCode == 9) || (charCode == 46) || (charCode == 36) || (charCode == 35) || (charCode == 37) || (charCode == 37))
			return true;
	
		if (!(charCode > 31 && (charCode < 48 || charCode > 57))){
			return false;
		} else {
			if (!((charCode < 96) || (charCode > 106))){
				return false;
			}
		}
	
		return true;
	} else {
		return false;
	}
}
//-----------------------------------------------------------------------------
function lower_0028(pText) {
	if (pText == null) {
		return false;
	};

	pText.value = pText.value.toLowerCase();
	return true;
};
//-----------------------------------------------------------------------------
function isCharKeyEnter_0031(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;

	// Realiza o bloqueio da tecla ENTER
	if (charCode == 13)
		return false;

	return true;
}
//-----------------------------------------------------------------------------
function aplicarMascaraCpfCnpj_0033(evt, src, mask){
	var charCode = (evt.which) ? evt.which : event.keyCode;

	if ((charCode == 8) || (charCode == 9)){
	} else {
		var i = src.value.length;
		var saida = mask.substring(0,1);
		var texto = mask.substring(i);
		if (texto.substring(0,1) != saida){
			src.value += texto.substring(0,1);
		};
	}
}
//----------------------------------------------------------------------------
function porcentagemFormatInput_0034(objTextBox, SeparadorMilesimo, SeparadorDecimal, e){
	var sep = 0;
	var key = '';
	var i = j = 0;
	var len = len2 = 0;
	var strCheck = '0123456789';
	var aux = aux2 = '';
	var whichCode = (window.addEventListener) ? e.which : e.keyCode;
	
	if (isCharKeyEnter_0031(e)){	
		// Permissão de teclas pré-definidas para liberdade de navegação dentro do campo:
		// 8 - Backspace, 9 - TAB
		// 46 - Delete, 36 - Home
		// 35 - End,  37 - Seta Esquerda, 39 - Seta Direita
		if ((whichCode == 8) || (whichCode == 9) || (whichCode == 46) || (whichCode == 36) || (whichCode == 35) || (whichCode == 37) || (whichCode == 37))
			return true;
	
		if (whichCode == 96){
			key = "0";
		} else {
			if (whichCode == 97){
				key = "1";
			} else {
				if (whichCode == 98){
					key = "2";
				} else {
					if (whichCode == 99){
						key = "3";
					} else {
						if (whichCode == 100){
							key = "4";
						} else {
							if (whichCode == 101){
								key = "5";
							} else {
								if (whichCode == 102){
									key = "6";
								} else {
									if (whichCode == 103){
										key = "7";
									} else {
										if (whichCode == 104){
											key = "8";
										} else {
											if (whichCode == 105){
												key = "9";
											} else {
												key = String.fromCharCode(whichCode); // Valor para o código da Chave
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		
		if (strCheck.indexOf(key) == -1)
			return false; // Chave inválida
		len = objTextBox.value.length;
	
		if(len >= 6)
			return false;
	
		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;
	} else {
		return false;
	}
}
//----------------------------------------------------------------------------
function isCharNumberLoginKey_0035(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;

	if (isCharKeyEnter_0031(evt)){	
		// Permissão de teclas pré-definidas para liberdade de navegação dentro do campo:
		// 8 - Backspace, 9 - TAB 
		// 46 - Delete, 36 - Home
		// 35 - End,  37 - Seta Esquerda, 39 - Seta Direita
		if ((charCode == 8) || (charCode == 9) || (charCode == 46) || (charCode == 36) || (charCode == 35) || (charCode == 37) || (charCode == 39)){
			return true;
		} else {
			if ((charCode > 47)&&(charCode < 58) || (charCode > 64)&&(charCode < 91) || (charCode > 96)&&(charCode < 123)){
				return true;
			}
		}
		
		return false;
	} else {
		return false;
	}
}
//----------------------------------------------------------------------------
function isCharNumberDigitoContaCorrente_0036(evt)
{
	var charCode = (evt.which) ? evt.which : event.keyCode;

	if (isCharKeyEnter_0031(evt)){	
		// Permissão de teclas pré-definidas para liberdade de navegação dentro do campo:
		// 8 - Backspace, 9 - TAB 
		// 46 - Delete, 36 - Home
		// 35 - End,  37 - Seta Esquerda, 39 - Seta Direita
		if ((charCode == 8) || (charCode == 9) || (charCode == 46) || (charCode == 36) || (charCode == 35) || (charCode == 37) || (charCode == 39)){
			return true;
		} else {
			if ((charCode > 47)&&(charCode < 58) || (charCode > 64)&&(charCode < 91) || (charCode > 96)&&(charCode < 123)){
				return true;
			}
		}
		
		return false;
	} else {
		return false;
	}
}
//----------------------------------------------------------------------------
function aplicarMascaraDataCep_0038(evt, src, mask){
	var charCode = (evt.which) ? evt.which : event.keyCode;

	if ((charCode == 8) || (charCode == 9)){
	} else {
		var i = src.value.length;
		var saida = mask.substring(0,1);
		var texto = mask.substring(i);
		if (texto.substring(0,1) != saida){
			src.value += texto.substring(0,1);
		};
	}
}
