/**
 *	Desabilita tecla Enter
 */
/*
function kH(e) {
	var pK = e ? e.which : window.event.keyCode;
	return pK != 13;
}
if (!isInStr("/admin/",window.location.href)) {
	document.onkeypress = kH;
	if (document.layers) document.captureEvents(Event.KEYPRESS);
}
*/

/*
 *	Strip whitespace from the beginning and end of a string
 *	Input : a string
 */
 
 
if (window.Event)
  document.captureEvents(Event.MOUSEUP);

function nocontextmenu() {
	event.cancelBubble = true;
	event.returnValue = false;
	return false;
}

function norightclick(e) {
	if (window.Event) {
		if (e.which == 2 || e.which == 3) {
			return false
		}
	} else if (event.button == 2 || event.button == 3) {
		event.cancelBubble = true;
		event.returnValue = false;
		return false;
	}
}

document.oncontextmenu = nocontextmenu;
document.onmousedown = norightclick;
 
 
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

function isInStr(pattern, string) {
	var _m = pattern.toLowerCase(); // pattern to match.
	var _s = string.toLowerCase();
	var _c = 0; // count
	for (var i=0;i<_s.length;i++) {
		if (_m == _s.substr(i,_m.length))
		 _c++;
	}
	if (_c > 0) {
		return true;
	} else {
		return false;
	}
}

/*
 *	Make sure that textBox only contain number (accepts dots)
 */
function checkNumber(textBox)
{
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}


function checkNumberOnly(textBox)
{
	var i;
	for (i = 0; i < textBox.value.length; i++) {
		if (textBox.value.charAt(i) == '.') {
			textBox.value = textBox.value.substring(0, i);
		}
	}
	while (textBox.value.length > 0 && isNaN(textBox.value)) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	textBox.value = trim(textBox.value);
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

function setMask(strField, sMask, evtKeyPress) {
	var i, nCount, sValue, fldLen, mskLen,bolMask, sCod, nTecla;
	
	if(document.all) { // Internet Explorer
		nTecla = evtKeyPress.keyCode; }
	else if(document.layers) { // Nestcape
		nTecla = evtKeyPress.which;
	}
	
	sValue = strField.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( " ", "" );
	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) == "/"))
		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++;
	}
	
	strField.value = sCod;
	
	if (nTecla != 8) { // backspace
		if (sMask.charAt(i-1) == "9") { // apenas números...
			return ((nTecla > 47) && (nTecla < 58)); } // números de 0 a 9
		else { // qualquer caracter...
			return true;
		}
	} else {
		return true;
	}
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	_isEmpty = false;
	if (formElement) {
		formElement.value = trim(formElement.value);
		
		if (formElement.value == '') {
			_isEmpty = true;
			alert(message);
			formElement.focus();
		}

	}

	return _isEmpty;

}


/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}

/*
	Deixa so' os digitos no numero
*/
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 popUp(arquivo, nome, attr) {
	if (!arquivo) { arquivo = ''; }
	
	if (!nome) { nome = ''; }
	
	if (!attr) { attr = ''; }
	
	return window.open(arquivo,nome,attr);
}

/*
function popUp(arquivo, attr)
{
	if (!attr) {
		attr = "toolbars=no,status=no,resize=no,scrollbars=no,width=633,height=540,top=50,left=100";
	}
	window.open(arquivo,"",attr);
}
*/

function validateForm(fields, form) {
	erro = '';
	fldNP = ''; // Nao preenchidos
	fldNE = ''; // Não definidos
	fldIN = ''; // Preenchidos incorretamente
	fldQty = fields.length;
	
	cleanHighlight(form);
		for (i = 0; i < fldQty; i++) {
			field = document.getElementById(fields[i][0]);
			if (!field) {
				fldNE += '   - ' + fields[i][2] + '\n';
			} else {
				fldValue = trim(field.value);
				fldType  = fields[i][1];
				fldDescription = fields[i][2];
				
				// Campo de Texto
				if (fldType == 'text') {
					if (fldValue.length == 0) {
						fldNP += '   - ' + fldDescription + '\n';
						highlightField(fields[i][0]);
					}
					
					
				// Campo de Radio
				} else if (fldType == 'radio') {
					if (field.type != 'radio' && field.type != 'checkbox') {
						fldNE += '   - ' + fldDescription + '\n';
					} else {
						if (checkRadioButton(fields[i][0], form) == null) {
							fldNP += '   - ' + fldDescription + '\n';
							highlightField(fields[i][0]);
						} 
					}
					
					
				// Campo de Checkbox
				} else if (fldType == 'check') {
					if (field.type != 'checkbox') {
						fldNE += '   - ' + fldDescription + '\n';
					} else {
						if (!field.checked) {
							fldNP += '   - ' + fldDescription + '\n';
							highlightField(fields[i][0]);
						} 
					}
					
				// Campo de Senha
				} else if (fldType == 'pass') {
					if (fldValue.length == 0) {
						fldNP += '   - ' + fldDescription + '\n';
						highlightField(fields[i][0]);
					} else if (fldValue.length < 6) {
						fldIN += '   - ' + fldDescription + ' (mínimo 6 caracteres)\n';
						highlightField(fields[i][0]);
					} else {
						fldCheck = fields[i][0] + '2';
						fldCheck = document.getElementById(fldCheck);
						if (fldCheck) {
							if (fldCheck.value != '' && fldCheck.value != fldValue) {
								fldIN += '   - As duas senhas digitadas não são iguais\n';
								highlightField(fldCheck);
							}
						}
					}
					
					
				// Campo de Usuario
				} else if (fldType == 'user') {
					if (fldValue.length == 0) {
						fldNP += '   - ' + fldDescription + '\n';
						highlightField(fields[i][0]);
					} else if (fldValue.length < 6) {
						fldIN += '   - ' + fldDescription + ' (mínimo 6 caracteres)\n';
						highlightField(fields[i][0]);
					}
					
					
				// Campo de CPF
				} else if (fldType == 'cpf') {
					if (fldValue.length == 0) {
						fldNP += '   - ' + fldDescription + '\n';
						highlightField(fields[i][0]);
					} else if (!checkCPF(fldValue)) {
						fldIN += '   - ' + fldDescription + '\n';
						highlightField(fields[i][0]);
					}
					
					
				// Campo de CNPJ
				} else if (fldType == 'cnpj') {
					if (fldValue.length == 0) {
						fldNP += '   - ' + fldDescription + '\n';
						highlightField(fields[i][0]);
					} else if (!checkCGC(fldValue)) {
						fldIN += '   - ' + fldDescription + '\n';
						highlightField(fields[i][0]);
					}
					
					
				// Campo de CEP
				} else if (fldType == 'cep') {
					if (fldValue.length == 0) {
						fldNP += '   - ' + fldDescription + '\n';
						highlightField(fields[i][0]);
					} else if (!checkCEP(fldValue)) {
						fldIN += '   - ' + fldDescription + ' (8 caracteres numéricos)\n';
						highlightField(fields[i][0]);
					}
				
				// Campo de Email
				} else if (fldType == 'email') {
					if (fldValue.length == 0) {
						fldNP += '   - ' + fldDescription + '\n';
						highlightField(fields[i][0]);
					} else {
						if (!checkEmail(field.value)) {
							fldIN += '   - ' + fldDescription + '\n';
							highlightField(fields[i][0]);
						} else {
							fldCheck = fields[i][0] + '2';
							fldCheck = document.getElementById(fldCheck);
							if (fldCheck) {
								if (fldCheck.value != '' && fldCheck.value != fldValue) {
									fldIN += '   - Os dois emails digitados não são iguais\n';
									highlightField(fldCheck);
								}
							}
						}
					}
					
				// Campo de Data
				} else if (fldType == 'data') {
					if (fldValue.length == 0) {
						fldNP += '   - ' + fldDescription + '\n';
						highlightField(fields[i][0]);
					} else if (!checkDate(fldValue)) {
						fldIN += '   - ' + fldDescription + ' (dd/mm/yyyy)\n';
						highlightField(fields[i][0]);
					}
					
				// Campo de Hora
				} else if (fldType == 'hora') {
					if (fldValue.length == 0) {
						fldNP += '   - ' + fldDescription + '\n';
						highlightField(fields[i][0]);
					} else if (!checkTime(fldValue)) {
						fldIN += '   - ' + fldDescription + ' (hh:mm)\n';
						highlightField(fields[i][0]);
					}
				}
			}
		}
		
	if (fldNE != '') {
		erro += 'Campo(s) não encontrado(s)\n\n';
		erro += fldNE + '\n';
	}
	if (fldNP != '') {
		erro += 'Campo(s) não preenchido(s)\n\n';
		erro += fldNP + '\n';
	}
	if (fldIN != '') {
		erro += 'Campo(s) invalido(s)\n\n';
		erro += fldIN;
	}
	
	return erro;
}

function highlightField(field, color) {
	if (!color && color != '') {
		color = '#ffaaaa';
	}
	
	if (typeof field == 'string') {
		campo = document.getElementById(field).style;
	} else {
		campo = field.style
	}
	if (campo) {
		campo.backgroundColor = color;
	}
}

function cleanHighlight(form) {
	if (form) {
		for (i=0;i<form.elements.length;i++) {
			highlightField(form.elements[i], '');
		}
	}
}

function checkCPF(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;
	}
        return true;
}

function checkCGC(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 checkCEP(cep) {
     s = limpa_string(cep);
     if (s.length != 8) {
        return false;
     }
     return true;
}
 
function checkEmail(email)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		//alert('O email informado é inválido');
		return false;
	} else {
		return true;
	}
}

function checkDate(strDate) {
	 // regular expression to match required date format 
	 re = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/; 
	 
	 if(strDate != '') { 
	 	if(regs = strDate.match(re)) { 
	 		if(regs[1] < 1 || regs[1] > 31) { 
	 			return false;
	 		} else if(regs[2] < 1 || regs[2] > 12) { 
	 			return false;
//	 		} else if(regs[3] < minYear || regs[3] > maxYear) { 
//	 			return false;
	 		} 
	 	} else { 
 			return false;
	 	} 
	 } else { 
		return false
	 } 
	 
	 return true;
}

function checkTime(strTime) {
	// regular expression to match required time format 
	re = /^(\d{1,2}):(\d{2})(:00)?$/; 
	if(strTime != '') { 
		if(regs = strTime.match(re)) { 
			if(regs[1] > 23) { 
				return false;
			} 
			
			if(regs[2] > 59) { 
				return false;
			}
			
			if(regs[3]) {
				if (regs[3] > 59) {
					return false;
				}
			}
		} else { 
			return false;
		} 
	} else {
		return false;
	}
	
	return true;
}

function checkRadioButton(group, form) {
    if (typeof group == 'string') group = form.elements[group];
    for (var i = 0, n = group.length; i < n; ++i)
        if (group[i].checked) return group[i].value;
    return null;
}

function setRadio(form, group, value) {
    if (typeof group == 'string') group = form.elements[group];
    for (var i = 0, n = group.length; i < n; ++i) {
        if (group[i].value == value) {
        	group[i].checked = 'checked';
        }
    }
}

function cleanSelect(id) {
	field = document.getElementById(id);
	if (field) {
		for (i = field.length; i>= 0; i--) {
			field.options[i] = null;
		}
	}
	/*
	for(i = x.autor.length; i>= 0; i--){
		x.autor.options[i] = null;

	}
	*/
}

function popPauta (url) {
	var attr = "toolbars=no,status=no,scrollbars=yes,width=450,height=320,top=10,left=100";
	window.open(url,"p1",attr);
}

function checkFrmLogin(form)
{
	fields = new Array( new Array('txtUserName', 		'user', 	'Login'),
						new Array('txtPassword',		'pass', 	'Senha')
						);
	
	result = validateForm(fields, form);
	
	if (result == '') {
		return true;
	} else {
		alert(result);
		return false;
	}
}

function setActiveTab(ediId) {
	// Desativado por solicitação do Fernando
	return true;
	divEditorial = document.getElementById('divPagAtual');
	imgEditorial = document.getElementById('imgPagAtual');

	if (divEditorial && imgEditorial) {
		switch (ediId) {
			case 1 :
				imgEditorial.alt = 'Cidades';
				imgEditorial.src = 'img/bt-cidades-on.gif';
				divEditorial.style.left = '183px';
				divEditorial.style.visibility = 'visible';
			break;
			
			case 2 :
				imgEditorial.alt = 'Esporte';
				imgEditorial.src = 'img/bt-esporte-on.gif';
				divEditorial.style.left = '269px';
				divEditorial.style.visibility = 'visible';
			break;
			
			case 3 :
				imgEditorial.alt = 'Variedades';
				imgEditorial.src = 'img/bt-variedades-on.gif';
				divEditorial.style.left = '355px';
				divEditorial.style.visibility = 'visible';
			break;
			
			case 4 :
				imgEditorial.alt = 'Divulgação';
				imgEditorial.src = 'img/bt-divulgacao-on.gif';
				divEditorial.style.left = '462px';
				divEditorial.style.visibility = 'visible';
			break;
		}
	}
}