/* Validacao de formularios
	- Validador generico de campos de formularios
*/

function _trim(str) {
	return str.replace(/(^([ \t\n\r]|&nbsp;)*|([ \t\n\r]|&nbsp;)*$)/, '');
}

function TestaControle(control, testRequired)
{
	var j, s;
	var type, param;
	var grupos = new Array();
	var nome_grupo = new Array();
	var set_of_rules;
	var splitted_rule;
		
	// elimina espacos do valor antes de fazer o teste
	control.value = _trim(control.value)

	// se o controle possuir uma regra especificada, verifica se a regra confere
	if (control.rule != null) {
		set_of_rules = String(control.rule).split('|');
		if (control.label == null)
		    control.label = control.getAttribute('label');

		if (control.label == null)
			control.label = control.name;
		
		for (j = 0; (j < set_of_rules.length); j++) {
			splitted_rule = String(set_of_rules[j]).split(':');
			type = splitted_rule[0];
			if (splitted_rule.length > 1)
				param = splitted_rule[1];
				
			switch (type) {
			case 'required':
				if (!NotEmpty(control) && testRequired) {
					return  'O campo ' + control.label + ' é obrigatório.';
				}
				break;
			case 'grouprequired':
				if (grupos[param] == null) {
					nome_grupo[nome_grupo.length] = param;
					grupos[param] = false;
				}
				grupos[param] = grupos[param] || NotEmpty(control);
				break;
			case 'email':
				if (!validateMail(control)) {
					return  'O campo ' + control.label + ' exige email válido.';
				}
				break;
			case 'date':
				if (!validateDate(control)) {
					return  'O campo ' + control.label + ' exige data válida (dd/mm/aaaa).';
				}
				break;
				
			case 'timestamp':
				if (!validateTimestamp(control)) {
					return  'O campo ' + control.label + ' exige data/hora válida (dd/mm/aaaa hh:mm).';
				}
				break;					
				
			case 'hora':
				if (!validateTime(control)) {
					return  'O campo ' + control.label + ' exige hora válida (hh:mm).';
				}
				break;
			case 'intervalo_hora':
				if (!validateTimeInterval(control)) {
					return  'O campo ' + control.label + ' exige um intervalo de hora válido (hh:mm-hh:mm).';
				}
				break;
				
			case 'number':
				if (!validateNumber(control)) {
					return  'O campo ' + control.label + ' deve ser um número inteiro positivo.';
				}
				break;
			case 'money':
				if (!validateMoney(control)) {
					return  'O campo ' + control.label + ' deve ser um número real (inteiro c/ 2 casas dec.).';
				}
				break;
			case 'money_any':
				if (!validateMoneyAny(control)) {
					return  'O campo ' + control.label + ' deve ser um número real (inteiro c/ 2 casas dec.).';
				}
				break;					
			case 'alpha':
				if (!validateAlphanumeric(control)) {
					return  'O campo ' + control.label + ' deve ser alfanumérico.';
				}
				break;
			case 'onlychar':
				if (!validateAlpha(control)) {
					return  'O campo ' + control.label + ' deve ter apenas caracteres, sem espaços.';
				}

			case 'text':
				if (!validateText(control)) {
					return  'O campo ' + control.label + ' só pode conter texto.';
				}
				break;
			case 'address':
				if (!validateAddress(control)) {
					return  'O campo ' + control.label + ' deve ser um endereco valido.';
				}
				break;					
			case 'telefone':
				if (!validatePhoneNumber(control)) {
					return  'O campo ' + control.label + ' deve ser um telefone válido.';
				}
				break;
			case 'cpf':
				if (!validateCPF(control)) {
					return  'O campo ' + control.label + ' deve ser um CPF válido.';
				}
				break;
			case 'rg':
				if (!validateRG(control)) {
					return  'O campo ' + control.label + ' deve ser um RG válido.';
				}
				break;
			
			case 'cep':
				if (!validateCEP(control)) {
					return  'O campo ' + control.label + ' deve ser um CEP válido.';
				}
				break;
			case 'length':
				if (!validateLength(control, param)) {

					s = String(param).split('-');						
					if ((s[0] == null) || (s[0] == ''))
						msg = 'no máximo ' + s[1];
					else
					if (s[1] == null)
						msg = 'no mínimo ' + s[0];
					else
						msg = 'entre ' + s[0] + ' e ' + s[1];
						
					return  'O campo ' + control.label + ' deve ter ' + msg + ' caracteres.';
				}
				break;
			case 'date_range':
				if (!validateYearRange(control, param)) {						
				
					s = String(param).split('-');

					var DataAtual = new Date();
					if ((s[0] != null) && (s[0] != '')) {
						var DataLoStr = new String();

						DataLoStr = (1900 + DataAtual.getYear() - Number(s[0]));
					}
					
					if ((s[1] != null) && (s[1] != '')) {
						var DataUpStr = new String();
						
						DataUpStr = (1900 + DataAtual.getYear() - Number(s[1]));
					}
								
					if ((s[0] == null) || (s[0] == '')) {
						msg = 'no mínimo ' + DataUpStr;
					}
					else
					if (s[1] == null) {
						msg = 'no máximo ' + DataLoStr;
					}
					else {
						msg = 'entre ' + DataLoStr + ' e ' + DataUpStr;
					}
						
					return  'O ano na data do campo ' + control.label + ' deve ser ' + msg + '.';
				}
				break;
			
			case 'eq':
				var toCompare;
				toCompare = eval('Form.' + param);
				if (toCompare.label == null)
					toCompare.label = toCompare.name;
				
				if (!validateEqualsTo(control, toCompare)) {
					return  'O campo ' + control.label + ' deve ser igual a ' + toCompare.label + '.';
				}
				break;
				
			case 'gt':
				var toCompare;
				toCompare = eval('Form.' + param);
				if (toCompare.label == null)
					toCompare.label = toCompare.name;
				
				if (!validateHigherThan(control, toCompare)) {
					return  'O campo ' + control.label + ' deve ser maior que a ' + toCompare.label + '.';
				}
				break;
			case 'gte':
				var toCompare;
				toCompare = eval('Form.' + param);
				if (toCompare.label == null)
					toCompare.label = toCompare.name;
				
				if (!validateHigherThan(control, toCompare) && !validateEqualsTo(control, toCompare)) {
					return  'O campo ' + control.label + ' deve ser maior ou igual a ' + toCompare.label + '.';
				}
				break;					

			default:
				// regra nao reconhecida
				break;
			}// fim switch
			
		}// fim for de regras			
	}// fim if existe regra	
	
	// verifica os grupos
	for (i = 0; i < nome_grupo.length; i++) {
		if (!grupos[nome_grupo[i]]) {
			return nome_grupo[i] + ' deve ser ter alguma opção selecionada ou setada.\n';
		}
	}
	
	return '';
}

function OnlineValidation(l)
{
	msg = TestaControle(this, false);//tell to ignore 'required'
	if (msg != '') {
		AlertBubble(this, msg);
	}
}
/*
function addEvent(obj, evType, fn){ 
 if (obj.addEventListener){ 
   obj.addEventListener(evType, fn, false); 
   return true; 
 } else if (obj.attachEvent){ 
   var r = obj.attachEvent("on"+evType, fn); 
   return r; 
 } else { 
   return false; 
 } 
}
*/
function AddOnlineValidation(theForm)
{
	for (i = 0; i < theForm.elements.length; i++) {
		control = theForm.elements[i];
		if (control.rule == null) {
		    control.rule = control.getAttribute('rule');
		}
		if (control.label == null) {
		    control.label = control.getAttribute('label');
		}

		if (control.rule != null) {
			addEvent(control, 'blur', OnlineValidation);
		}
	}
}

function ValidaFormNovo(theForm)
{
	var i;
	var ha_erro, msg_erro;
	var first_control;

	ha_erro = false;
	msg_erro = '';


	for (i = 0; i < theForm.elements.length; i++) {
		control = theForm.elements[i];
		msg = TestaControle(control, true);
		if (msg != '') {
			msg_erro += msg + '\n';
			if (!ha_erro) {
				ha_erro = true;
				first_control = i;		
			}
		}
	}
	
	if (ha_erro) {
		alert(msg_erro);
		if (first_control != null) {
		    try {
		        theForm.elements[first_control].focus();
		    }
		    catch(e) {
		    }
		}
	} 

	return !ha_erro;
}

// Validador de forms
function ValidaForm(Form)
{
	var i, j, s;
	var type, param;
	var ha_erro, ha_erro_atual, msg_erro, ind_erro;
	var grupos = new Array();
	var nome_grupo = new Array();
	var control, set_of_rules;
	var splitted_rule;

	ha_erro = false;
	msg_erro = '';

	for (i = 0; i < Form.elements.length; i++) {
		control = Form.elements[i];
		if (control.rule == null) {
		    control.rule = control.getAttribute('rule');
		}
		if (control.label == null) {
		    control.label = control.getAttribute('label');
		}
		//alert(control.name + "  " + control.rule);
		if (control.rule != null) {
    		control.value = _trim(control.value)
			set_of_rules = String(control.rule).split('|');
			ha_erro_atual = false

			if (control.label == null)
				control.label = control.name;

			for (j = 0; (j < set_of_rules.length) && (!ha_erro_atual); j++) {
				splitted_rule = String(set_of_rules[j]).split(':');
				type = splitted_rule[0];
				if (splitted_rule.length > 1)
					param = splitted_rule[1];
					
				switch (type) {
				case 'required':
					if (!NotEmpty(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' é obrigatório.\n';
						ha_erro_atual = true;
					}
					break;
				case 'grouprequired':
					if (grupos[param] == null) {
						nome_grupo[nome_grupo.length] = param;
						grupos[param] = false;
					}
					grupos[param] = grupos[param] || NotEmpty(control);
					break;
				case 'email':
					if (!validateMail(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' exige email válido.\n';
						ha_erro_atual = true;					
					}
					break;
				case 'date':
					if (!validateDate(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' exige data válida (dd/mm/aaaa).\n';
						ha_erro_atual = true;
					}
					break;
					
				case 'timestamp':
					if (!validateTimestamp(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' exige data/hora válida (dd/mm/aaaa hh:mm).\n';
						ha_erro_atual = true;
					}
					break;					
					
				case 'hora':
					if (!validateTime(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' exige hora válida (hh:mm).\n';
						ha_erro_atual = true;
					}
					break;
				case 'intervalo_hora':
					if (!validateTimeInterval(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' exige um intervalo de hora válido (hh:mm-hh:mm).\n';
						ha_erro_atual = true;
					}
					break;
					
				case 'number':
					if (!validateNumber(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' deve ser um número inteiro positivo.\n';
						ha_erro_atual = true;
					}
					break;
				case 'money':
					if (!validateMoney(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' deve ser um número real (inteiro c/ 2 casas dec.).\n';
						ha_erro_atual = true;
					}
					break;
				case 'money_any':
					if (!validateMoneyAny(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' deve ser um número real (inteiro c/ 2 casas dec.).\n';
						ha_erro_atual = true;
					}
					break;					
				case 'alpha':
					if (!validateAlphanumeric(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' deve ser alfanumérico.\n';
						ha_erro_atual = true;
					}
					break;
				case 'onlychar':
					if (!validateAlpha(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' deve ter apenas caracteres, sem espaços.\n';
						ha_erro_atual = true;
					}

				case 'text':
					if (!validateText(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' só pode conter texto.\n';
						ha_erro_atual = true;
					}
					break;
				case 'address':
					if (!validateAddress(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' deve ser um endereco valido.\n';
						ha_erro_atual = true;
					}
					break;					
				case 'telefone':
					if (!validatePhoneNumber(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' deve ser um telefone válido.\n';
						ha_erro_atual = true;
					}
					break;
				case 'cpf':
					if (!validateCPF(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' deve ser um CPF válido.\n';
						ha_erro_atual = true;
					}
					break;
				case 'rg':
					if (!validateRG(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' deve ser um RG válido.\n';
						ha_erro_atual = true;
					}
					break;
				
				case 'cep':
					if (!validateCEP(control)) {
						if (!ha_erro)
							ind_erro = i;
						msg_erro += 'O campo ' + control.label + ' deve ser um CEP válido.\n';
						ha_erro_atual = true;
					}
					break;
				case 'length':
					if (!validateLength(control, param)) {
						if (!ha_erro)
							ind_erro = i;
						
						s = String(param).split('-');						
						if ((s[0] == null) || (s[0] == ''))
							msg = 'no máximo ' + s[1];
						else
						if (s[1] == null)
							msg = 'no mínimo ' + s[0];
						else
							msg = 'entre ' + s[0] + ' e ' + s[1];
							
						msg_erro += 'O campo ' + control.label + ' deve ter ' + msg + ' caracteres.\n';
						ha_erro_atual = true;
					}
					break;
				case 'date_range':
					if (!validateYearRange(control, param)) {						
						if (!ha_erro)
							ind_erro = i;
						
						s = String(param).split('-');

						var DataAtual = new Date();
						if ((s[0] != null) && (s[0] != '')) {
							var DataLoStr = new String();

							DataLoStr = (1900 + DataAtual.getYear() - Number(s[0]));
						}
						
						if ((s[1] != null) && (s[1] != '')) {
							var DataUpStr = new String();
							
							DataUpStr = (1900 + DataAtual.getYear() - Number(s[1]));
						}
									
						if ((s[0] == null) || (s[0] == '')) {
							msg = 'no mínimo ' + DataUpStr;
						}
						else
						if (s[1] == null) {
							msg = 'no máximo ' + DataLoStr;
						}
						else {
							msg = 'entre ' + DataLoStr + ' e ' + DataUpStr;
						}
							
						msg_erro += 'O ano na data do campo ' + control.label + ' deve ser ' + msg + '.\n';
						ha_erro_atual = true;
					}
					break;
				
				case 'eq':
					var toCompare;
					toCompare = eval('Form.' + param);
					if (toCompare.label == null)
						toCompare.label = toCompare.name;
					
					if (!validateEqualsTo(control, toCompare)) {
						if (!ha_erro)
							ind_erro = i;													
						msg_erro += 'O campo ' + control.label + ' deve ser igual a ' + toCompare.label + '.\n';
						ha_erro_atual = true;
					}
					break;
					
				case 'gt':
					var toCompare;
					toCompare = eval('Form.' + param);
					if (toCompare.label == null)
						toCompare.label = toCompare.name;
					
					if (!validateHigherThan(control, toCompare)) {
						if (!ha_erro)
							ind_erro = i;													
						msg_erro += 'O campo ' + control.label + ' deve ser maior que a ' + toCompare.label + '.\n';
						ha_erro_atual = true;
					}
					break;
				case 'gte':
					var toCompare;
					toCompare = eval('Form.' + param);
					if (toCompare.label == null)
						toCompare.label = toCompare.name;
					
					if (!validateHigherThan(control, toCompare) && !validateEqualsTo(control, toCompare)) {
						if (!ha_erro)
							ind_erro = i;													
						msg_erro += 'O campo ' + control.label + ' deve ser maior ou igual a ' + toCompare.label + '.\n';
						ha_erro_atual = true;
					}
					break;					

				default:
					// regra nao reconhecida
					break;
				}// fim switch
				
				ha_erro = ha_erro || ha_erro_atual;
			}// fim for de regras			
		}// fim if existe regra
	}// fim for dos controles

	// verifica os grupos
	for (i = 0; i < nome_grupo.length; i++) {
		if (!grupos[nome_grupo[i]]) {
			msg_erro += nome_grupo[i] + ' deve ser ter alguma opção selecionada ou setada.\n';
			ha_erro = true;
		}
	}

	if (ha_erro) {
		alert(msg_erro);
		if (ind_erro != null) {
			if (!Form.elements[ind_erro].disabled) {
			    try {
				    Form.elements[ind_erro].focus();
				}
				catch(ex) {
				    // just ignores when the focus can't be assigned
				}
			}
			
		}
	} else
		return true;
	
	return false;
}

// verifica se campo tem algum valor atribuido
function NotEmpty(field)
{
	var res;
	var texto;

	if (field.type != 'radio')
		if ((field.value == null) || (field.value == ''))
			return false;

	res = false;
	switch (field.type) {
		case 'text': case 'textarea': case 'password': case 'hidden':
			texto = String(field.value).replace(/\ /, '');
			res = texto.length > 0;			
			break;
		case 'checkbox':
			res = field.checked;
			break;
		case 'select-one':
			res = String(field.value) != '';
			break;	
		case 'radio':
			res = field.checked;
			break;
	}

	return res;
}

// verifica se o email no campo eh valido
function validateMail(field)
{
	var regxp, param;
	
	if ((field.value == null) || (field.value == ''))
		return true;
			
	regxp = /^[a-zA-Z0-9._]+@[a-zA-Z1-9]+(\.[a-zA-Z1-9]+)*$/;
	param = regxp.exec(field.value)
	
	return (param != null);
}

// retorna True se o ano e' bissexto
function AnoBissexto(intYear) {
	if (intYear % 100 == 0) {
		if (intYear % 400 == 0) { return true; }
	}
	else {
		if ((intYear % 4) == 0) { return true; }
	}
	return false;
}

// verifica se a data no campo eh valida
function validateDate(field)
{
	var regxp;
	var res, param;

	if ((field.value == null) || (field.value == ''))
		return true;
	
	regxp = /^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4,4})$/;
	param = regxp.exec(field.value);
	if (param != null) {
		res = true;
		
		intMonth = param[2];
		intday = param[1];
		intYear = param[3];

		if ((intMonth == 1 || intMonth == 3 || intMonth == 5 ||
			intMonth == 7 || intMonth == 8 || intMonth == 10 ||
			intMonth == 12) && (intday > 31 || intday < 1)) {
			return false;
		}
	
		if ((intMonth == 4 || intMonth == 6 || intMonth == 9 ||
			intMonth == 11) && (intday > 30 || intday < 1)) {
			return false;
		}
	
		if (intMonth == 2){
			if ( AnoBissexto(intYear) == true ) {
				if (intday > 29 || intday < 1)
					return false;
			}else{
				if (intday > 28 || intday < 1)
					return false;
			}
		}
	
	} else
		res = false;
	
	return res;
}

function validateTimestamp(field)
{
	var regxp;
	var res, param;

	if ((field.value == null) || (field.value == ''))
		return true;
	
	regxp = /^([0-9]{1,2})\/([0-9]{1,2})\/([0-9]{4,4})[ ][ ]*([0-9]{1,2}):([0-9]{1,2})$/;
	param = regxp.exec(field.value);
	if (param != null) {
		res = true;
		
		intday = param[1];		
		intMonth = param[2];
		intYear = param[3];

		if ((intMonth == 1 || intMonth == 3 || intMonth == 5 ||
			intMonth == 7 || intMonth == 8 || intMonth == 10 ||
			intMonth == 12) && (intday > 31 || intday < 1)) {
			return false;
		}
	
		if ((intMonth == 4 || intMonth == 6 || intMonth == 9 ||
			intMonth == 11) && (intday > 30 || intday < 1)) {
			return false;
		}
	
		if (intMonth == 2){
			if ( AnoBissexto(intYear) == true ) {
				if (intday > 29 || intday < 1)
					return false;
			}else{
				if (intday > 28 || intday < 1)
					return false;
			}
		}

		res = res && (param[4] > 0) && (param[4] < 24);
		res = res && (param[5] >= 0) && (param[5] < 60);
	} else
		res = false;
	
	return res;	
}

// verifica se a data no campo estah de acordo com restricoes
function validateYearRange(field, bounds)
{
	var res;

	if ((field.value == null) || (field.value == ''))
		return true;

	// a data deve ser obrigatoriamente valida		
	if (!validateDate(field))
		return false;

	// obtem o ano apenas
	var DataArray;
	DataArray = String(field.value).split('/');
	
	var Ano = new Number(DataArray[2]);
	
	// os limites sao dados em anos relativos a data atual
	var bounds_array;
	bounds_array = String(bounds).split('-');
	
	var DataAtual = new Date();

	res = true;
	if ((bounds_array[0] != null) && (bounds_array[0] != '')) {
		res = res && (Ano <= ((1900 + DataAtual.getYear()) - Number(bounds_array[0])));
	}
	
	if (bounds_array.length > 1)
		if ((bounds_array[1] != null) && (bounds_array[1] != '')) {
			res = res && (Ano >= ((1900 + DataAtual.getYear()) - Number(bounds_array[1])));
		}
	
	return res;
}


// valida se a hora no campo eh valida
function validateTime(field)
{
	var regxp;
	var res, param;
	
	if ((field.value == null) || (field.value == ''))
		return true;	
	
	regxp = /^([0-9]{1,2}):([0-9]{1,2})(:([0-9]{1,2}))?$/;
	param = regxp.exec(field.value);
	if (param != null) {
		res = true;
		res = res && (param[1] >= 0) && (param[1] < 24);
		res = res && (param[2] >= 0) && (param[2] < 60);
		if (param[3]) {
		    param[3] = param[3].replace(':','');
		    res = res && (param[3] >= 0) && (param[3] < 60);
		}
	} else
		res = false;
	
	return res;
}

// valida se a hora no campo eh valida
function validateTimeInterval(field)
{
	var regxp;
	var res, param;
	
	if ((field.value == null) || (field.value == ''))
		return true;	
	
	regxp = /^([0-9]{1,2}):([0-9]{1,2})-([0-9]{1,2}):([0-9]{1,2})$/;
	param = regxp.exec(field.value);
	if (param != null) {
		res = true;
		res = res && (param[1] > 0) && (param[1] < 24);
		res = res && (param[2] >= 0) && (param[2] < 60);
		res = res && (param[3] > 0) && (param[3] < 24);
		res = res && (param[4] >= 0) && (param[4] < 60);
	} else
		res = false;
	
	return res;
}

function validateNumber(field)
{
	var regxp;
	var param;

	if ((field.value == null) || (field.value == ''))
		return true;
	
	regxp = /^[0-9]*$/;
	param = regxp.exec(field.value);
	
	return (param != null);
}

function validateMoney(field)
{
	var regxp;
	var param;

	if ((field.value == null) || (field.value == ''))
		return true;
	
	regxp = /^[0-9]*(\.[0-9]{1,2})?$/;
	param = regxp.exec(field.value);
	
	return (param != null);
}

function validateMoneyAny(field)
{
	var regxp;
	var param;

	if ((field.value == null) || (field.value == ''))
		return true;
	
	regxp = /^[0-9]*([.,][0-9]{1,2})?$/;
	param = regxp.exec(field.value);
	
	return (param != null);
}

function validateAlphanumeric(field)
{
	var regxp;
	var param;

	if ((field.value == null) || (field.value == ''))
		return true;	
	
	regxp = /^[A-Za-z0-9_]*$/;
	param = regxp.exec(field.value);
	
	return (param != null);
}

function validateAlpha(field)
{
	var regxp;
	var param;

	if ((field.value == null) || (field.value == ''))
		return true;	
	
	regxp = /^[A-Za-z]*$/;
	param = regxp.exec(field.value);
	
	return (param != null);
}



function validateText(field)
{
	var regxp;
	var param;

	if ((field.value == null) || (field.value == ''))
		return true;	
	
	regxp = /^[A-Za-z ]*$/;
	param = regxp.exec(field.value);
	
	return (param != null);
}

function validateAddress(field)
{
	var regxp;
	var param;

	if ((field.value == null) || (field.value == ''))
		return true;	
	
	regxp = /^[A-Za-z .]*$/;
	param = regxp.exec(field.value);
	
	return (param != null);
}


function validatePhoneNumber(field)
{
	var regxp;
	var param;

	if ((field.value == null) || (field.value == ''))
		return true;	
	
	regxp = /^(\([ ]*[0-9]{2}[ ]*\))?[ ]*[0-9]{2,4}-[0-9]{4,4}$/;
	param = regxp.exec(field.value);
	
	return (param != null);
}

function validateRG(field)
{
	var regxp;
	var param;

	if ((field.value == null) || (field.value == ''))
		return true;	
	
	regxp = /^[0-9\-.\/a-zA-z]*$/;
	param = regxp.exec(field.value);

	return (param != null);
}

function validateCEP(field)
{
	var regxp;
	var param;

	if ((field.value == null) || (field.value == ''))
		return true;	
	
	regxp = /^[0-9\-.]*$/;
	param = regxp.exec(field.value);

	return (param != null);
}

function validateCPF(field) 
{
	var i, j;
	var iDigito1, iDigito2;
	var CPF, CPF_Array;
	var regexp; 

	if ((field.value == null) || (field.value == ''))
		return true;
	
	// primeiramente, verifica caracteres e comprimento
	regexp = /^[0-9.-]{14,14}$/
	if (!regexp.test(field.value)) 
		return false;

	// verifica digito verificador	
	CPF_Array = field.value.split('-');

	CPF = String(CPF_Array[0]).replace(/\./g, "");

	iDigito1 = 0;
	for (i = 10, j = 0; i > 1; i--, j++) {
		iDigito1 = iDigito1 + (Number(CPF.substring(j,j+1)) * i);
	}
	
	iDigito1 = iDigito1 % 11;
	
	iDigito1 = (iDigito1 < 2)? 0 : 11-iDigito1;
	
	iDigito2 = 0;	
	for (i = 11, j = 0; i > 2; i--, j++) 
		iDigito2 = iDigito2 + (Number(CPF.substring(j,j+1)) * i);
		
	iDigito2 = (iDigito2 + (iDigito1 * 2))% 11;
	iDigito2 = (iDigito2 < 2)? 0 : 11-iDigito2;

	Verificador = String(iDigito1) + String(iDigito2);
	
	return Verificador == CPF_Array[1];
}

function validateLength(field, bounds) 
{
	var bounds_array;
	var res;
	
	bounds_array = String(bounds).split('-');
	
	res = true;
	if ((bounds_array[0] != null) && (bounds_array[0] != '')) 
		res = res && (field.value.length >= bounds_array[0]);
		
	if ((bounds_array[1] != null) && (bounds_array[1] != '')) 
		res = res && (field.value.length <= bounds_array[1]);			

	return res;
}

function validateEqualsTo(field, field_to_compare)
{
	return (field.value == field_to_compare.value);
}

function validateHigherThan(field, field_to_compare)
{
	return (field.value > field_to_compare.value);
}
