function get(div_name){
	try{
		return document.getElementById(div_name);
	}catch(e){
		alert('Seu browser não possui recursos para visualização desta aplicação!');
		parent.location = 'http://br.mozdev.org/firefox/download.html';
	}
}



//esconde os selects de uma pagina
function hideSelect(){
	if(isIE()){
		for(i=0 ; i<document.forms.length ; i++){
			for(j=0 ; j<document.forms[i].elements.length ; j++){
				try{
					if(document.forms[i].elements[j].type.indexOf('select')>=0){
						document.forms[i].elements[j].style.display='none';
					}
				}catch(e){}
			}
		}
	}
}

//exibe os selectes de uma pagina
function showSelect(){
	if(isIE()){
		for(i=0 ; i<document.forms.length ; i++){
			for(j=0 ; j<document.forms[i].elements.length ; j++){
				try{
					if(document.forms[i].elements[j].type.indexOf('select')>=0){
						document.forms[i].elements[j].style.display='block';
					}
				}catch(e){}
			}
		}
	}
}


//esconde os swf
function hideSWF(){
	var swfs = document.getElementsByTagName("object");
	for(i=0 ; i<swfs.length ; i++){
		swfs[i].style.display = 'none';
	}
}


//exibe os swf
function showSWF(){
	var swfs = document.getElementsByTagName("object");
	for(i=0 ; i<swfs.length ; i++){
		swfs[i].style.display = 'block';
	}
}



//alimenta um select
function select_add(obj, str_opt){
	for(i=0 ; i<get(obj).options.length ; i++){
		get(obj).remove(i);
	}
	var opts = str_opt.split(";");
	if(opts.length > 1){
		for(i=0 ; i<(opts.length-1) ; i++){
			str = opts[i].split(":");
			get(obj).options[i] = new Option(str[1], true);
			get(obj).options[i].text = str[0];
			get(obj).options[i].value = str[1];
		}
	}else{
		get(obj).options[0] = new Option('-1' , true);
		ge(obj).options[0].text = 'busca sem resultados!';
		get(obj).options[0].value = '-1'
	}
}




//valida os campos em branco de um formulario
function valida(f, l){
	for(i=0 ; i<f.length ; i++){
		switch(get(f[i]).type){
			case 'select-one':
				if(get(f[i]).options[get(f[i]).selectedIndex].value == "nulo"){
					alert('Selecione uma opção em '+l[i]+'!');
					return false;
				}
			break;
			
			case 'textarea':
			case 'password':
			case 'hidden':
			case 'text':
			if(get(f[i]).value == ''){
				alert('Preencha o campo '+l[i]+'!');
				get(f[i]).focus();
				return false;
			}
			break;
		}
	}
	return true;
}




//pega os dados de um form em forma de url
function extract(f){
	if(get(f)){
		var url = new Array();
		for(i=0 ; i<get(f).elements.length ; i++){
			switch(get(f).elements[i].type){
				case 'submit':
				case 'button':
				case 'textarea':
				case 'password':
				case 'hidden':
				case 'text':
					url[i] = get(f).elements[i].name+'='+filter(get(f).elements[i].value);
				break;
				
				case 'select-one':
					url[i] = get(f).elements[i].name+'='+filter(get(f).elements[i].options[get(f).elements[i].selectedIndex].value);
				break;
				
				case 'radio':
				case 'checkbox':
					if(get(f).elements[i].checked == true){
						url[i] = get(f).elements[i].name+'='+filter(get(f).elements[i].value);
					}
				break;
			}
		}
		
		return(url.join("&"));
	}
}




//tenta limpar todos os campos de um formulario
function clear(f){
	if(get(f)){
		for(i=0 ; i<get(f).elements.length ; i++){
			switch(get(f).elements[i].type){
				case 'textarea':
				case 'password':
				//case 'hidden':
				case 'text':
					get(f).elements[i].value = '';
				break;
				
				case 'select-one':
					get(f).elements[i].selectedIndex=0;
				break;
				
				case 'radio':
				case 'checkbox':
					//
				break;
			}
		}
	}
}


//aplica um filtro sobre uma string
function filter(str){
	return escape(str);
}


//pega o actio de um formulario e envia por ajax, retorna o resultado (boolean) do ajax
function send(form_name){
	ajax.assincr = false;
	ajax.method = "POST";
	if(get(form_name).action.indexOf('?')>=0){
		uri = get(form_name).action+'&'+extract(form_name);
	}else{
		uri = get(form_name).action+'?'+extract(form_name);
	}
	var res = ajax.loadResult(uri);
	if(res == "true"){
		return true;
	}else{
		return false;
	}
}

//mesma coisa que o send, só que retorna de tudo cara...
function sendAndGet(form_name){
	ajax.assincr = false;
	ajax.method = "POST";
	if(get(form_name).action.indexOf('?')>=0){
		uri = get(form_name).action+'&'+extract(form_name);
	}else{
		uri = get(form_name).action+'?'+extract(form_name);
	}
	return( ajax.loadResult(uri) );
}


// retorna os tamanhos da pagina
function getPageSize(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}



//se tá no IE, retorna true
function isIE(){
	return ( (navigator.userAgent.toString().indexOf("IE") >= 0) ? true : false );
}


//exibe um popup na tela
function popup(url, width, height){
	var size = getPageSize();
	var left = Math.ceil((size[2]/2)-(width/2));
	var top = Math.ceil((size[3]/2)-(height/2));
	window.open(url , 'WFramework' , 'width='+width+' , height='+height+' , left='+left+' , top='+top+' , scrollbars=yes');
}


//retorna um elemento centralizado na pagina
function centralize(el, width, height){
	var top = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
	var left = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
	var size = getPageSize();
	var width = Math.ceil(width/2);
	var height = Math.ceil(height/2);
	el.style.top = ((size[3]/2)+top-height)+'px';
	el.style.left = ((size[2]/2)+left-width)+'px';
	return el;
}