/**
* Funções de Javascript 
*
* @version 1.0
* @copyright ©2007 - Rovisco Solutions <info@rovisco.net>
*/

//Funções de acções dos metodos

function href(url){
	var newWindow = window.open(url, "_blank"); 
	newWindow.focus();	
}

function insertActionConfirm(meth, id, class_name, cat_id){
	conf = confirm("Tem a certeza que pretende continuar com esta operação?");
	if (conf==true){ 
		 insertAction(meth, id, class_name, cat_id);
	}
}

function insertAction(meth, id, class_name, cat_id){
	document.listagem.class_name.value = class_name;
	document.listagem.cat_id.value = cat_id;
	document.listagem.id.value = id;
	document.listagem.meth.value = meth;
	document.listagem.start.value = '';
	document.listagem.submit();
}

function setPaginador(page){
	document.listagem.start.value = page;
	document.listagem.submit();
}

function setOrder(order, dir){
	document.listagem.order.value = order;
	document.listagem.order_dir.value = dir;
	document.listagem.submit();
}

function setAction(class_name,meth,cat_id,id){
	document.setAction.meth.value = meth;
	document.setAction.class_name.value = class_name;
	document.setAction.cat_id.value = cat_id;
	document.setAction.id.value = id;
	document.setAction.submit();
}

function removeFilter(){
	var filtro = document.getElementById('listagem').filtro;
		//para o caso de ser com varios filtros
	if(filtro.length >0){
		for (i=0;i<filtro.length;i++){
			if(filtro[i]){ filtro[i].value = ""; }
		}
	} else {
		//para o caso de ser so um filto
		filtro.value = "";
	}
	document.listagem.start.value = ''; 	
	document.listagem.submit();
}

function setFilter(){
	var filtro = document.getElementById('listagem').filtro;
	var set = false;
	
	//para o caso de ser com varios filtros
	if(filtro.length >0){
		for (i=0;i<filtro.length;i++){
			if((filtro[i] && filtro[i].value!='')){ set = true; }
		}	
	} else {
		//para o caso de ser so um filto
		if(filtro.value!=''){ set = true; }
	}
	
	if(set == true){ 
		document.listagem.start.value = ''; 
		document.listagem.submit(); 
	}
	else{ alert("Tem de preencher pelo menos um filtro!"); }
}


// Funções de validação de formulários

function valida_campos(formName, array){
	var i, name, val, documento, erro, tmp, valNome, tmp_aux, tmp_documento;
	
	erro = '';
	for (i=0; i < array.length; i+=3){
		tmp = '';
		nome = array[i];
		val = array[i+1];
		valNome = array[i+2];
		
		documento = document.getElementById(nome);
		
		//verifica se trás mais alguma info	na validacao	
		tmp = val.split("||");
		val = tmp[0];
		tmp_aux = tmp[1];
		
		switch(val){
			// verifica se está preenchido
			case 'isR':
				erro += isRequired(documento, valNome);
				break;
				
			// verifica se é um numero
			case 'isNum':
				erro += isNum(documento, valNome);
				break;
					
			// verifica se é um email 
			case 'isMail':
				erro += isMail(documento, valNome);
				break;
					
			// verifica se é um URL 
			case 'isUrl':
				erro += isUrl(documento, valNome);
				break;
				
			// verifica se é Hora
			case 'isTime':
				erro += isTime(documento, valNome);
				break;
				
			// faz a relação de preenchido com outro campo do form	
			case 'isRelational':
				tmp_documento = document.getElementById(tmp_aux);
				erro += isRelational(documento, valNome, tmp_documento);
				break;
				
			case 'myRegxp':
				erro += myRegxp(documento, valNome, tmp_aux);
				break;
		}
	}
	
	if(erro != ''){
		alert(erro);
		return false;
	}
	return true;
}

function isRequired(documento, valNome){
	if(documento.value == '') 
		return 'O campo ' + valNome + ' é de preenchimento obrigatório!\n';
	return '';
}

function isNum(documento, valNome){
	if(isNaN(documento.value)){ 
		return 'O campo ' + valNome + ' tem de ser um número!\n';
	}
	return '';
}

function isMail(documento, valNome){
	var emailRegxp = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
	if (emailRegxp.test(documento.value) != true && documento.value != '')
		return 'O campo ' + valNome + ' tem de ser um E-mail válido!\n';
	return '';
}

function isUrl(documento, valNome){
	//  /^((http(s?)|ftp))\:\/\/)?(www.|[a-zA-Z].)[a-zA-Z0-9\-\.]+\.(com|edu|gov|mil|net|org|biz|info|name|museum|us|ca|uk)(\:[0-9]+)*(\/($|[a-zA-Z0-9\.\,\;\?\'\\\+&%\$#\=~_\-]+))*$/
	//var emailRegxp = (?i)(((https?|ftp|gopher)://([^:]+\:[^@]*@)?)?([\d\w\-]+\.)?[\w\d\-\.]+\.[\w\d]+((/[\w\d\-@%]+)*(/([\w\d\.\_\-@]+\.[\w\d]+)?(\?[\w\d\?%,\.\/\##!@:=\+~_\-&]*(?<![\.]))?)?)?)\/\/([-\w\.]+)+(:\d+)?(\/([\w/_\.]*(\?\S+)?)?)?/;
	/*
	var urlRegxp = new RegExp("(((https?|ftp|svn|nntp|file|aim|webcal)://)(www\.)?[][a-z0-9_$!&%?,#@'/.*+;:=~-]+)");
	if (urlRegxp.test(documento.value) != true && documento.value != '')
		return 'O campo ' + valNome + ' tem de ser um URL válido!\n';
	*/
	return '';
}

function isRelational(documento, valNome, tmp_documento){
	if(tmp_documento.value != '' && documento.value == '') 
		return 'O campo ' + valNome + ' é de preenchimento obrigatório!\n';
	return '';
}

function isTime(documento, valNome){
	var timeRegxp = /^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/;
	if (timeRegxp.test(documento.value) != true && documento.value != '')
		return 'O campo ' + valNome + ' tem de ser uma Hora válida! (Ex. 23:59)\n';
	return '';
}

function myRegxp(documento, valNome, myXp){
	var rgxp = new RegExp(myXp);
	if (rgxp.test(documento.value) != true && documento.value != '')
		return 'O campo ' + valNome + ' não está no formato correcto\n';
	return '';
}

/*
function valida_campos_temp()
{
	var regresa = true;
	var array_objetos = new Array(<?echo $Campos_a_Validar;?>);
	var array_nombres = new Array(<?echo $Campos_a_Validar_e;?>);
	var i=0;

	for (i=0;i<array_objetos.length;i++) {
		var objeto = document.getElementById(array_objetos[i]);
		if (objeto.value == '' || objeto.value == false) {
			alert("<?=$error[0]?>"+array_nombres[i].replace(':','')+"<?=$error[1]?>");
			objeto.focus();
			regresa=false;
			break;
		}
	}

	if(regresa==true)
		return(true);
	else
		return(false);
}


function valida_checkbox()
{
	var regresa = true;
	var arreglo = new Array(<?echo $Checkbox_a_Validar;?>);
	var arreglo_2 = new Array(<?echo $Checkbox_a_Validar_e;?>);
	var i=0;

	for (i=0;i<arreglo.length;i++) {
		var objeto = document.getElementById(arreglo[i]);
		if(objeto.checked == false ) {
			alert("<?=$error[0]?>"+arreglo_2[i].replace(':','')+"<?=$error[1]?>");
			regresa=false;
			break;
		}
	}

	if(regresa==true)
		return(true);
	else
		return(false);
}

function radio_check()
{
	var radio_choice = false;
	var  length_r = '';
	length_r = document.<?echo $Current_Name_Form;?>.<?echo $name;?>.length;

	if (!length_r) {
		if (document.<?echo $Current_Name_Form;?>.<?echo $name;?>.checked) {
			radio_choice = true; }
	}
	else
	{
		for (counter = 0; counter < length_r; counter++) {
			if (document.<?echo $Current_Name_Form;?>.<?echo $name;?>[counter].checked){
				radio_choice = true; }
		}
	}

	if (!radio_choice) {
		radio_name="<?echo $Radios_a_Validar_e[$valor];?>";
		alert("<?=$error[0]?>"+radio_name.replace(':','')+"<?=$error[1]?>");
		return (false);
	}
	return (true);
}
*/
