/*
 * Copyright (c) 2007-2008 Josh Bush (digitalbush.com)
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE. 
 */
 
/*
 * Version: 1.1.4
 * Release: 2008-07-29
 */ 
(function($) {

	//Helper Function for Caret positioning
	$.fn.caret=function(begin,end){	
		if(this.length==0) return;
		if (typeof begin == 'number') {
            end = (typeof end == 'number')?end:begin;  
			return this.each(function(){
				if(this.setSelectionRange){
					this.focus();
					this.setSelectionRange(begin,end);
				}else if (this.createTextRange){
					var range = this.createTextRange();
					range.collapse(true);
					range.moveEnd('character', end);
					range.moveStart('character', begin);
					range.select();
				}
			});
        } else {
            if (this[0].setSelectionRange){
				begin = this[0].selectionStart;
				end = this[0].selectionEnd;
			}else if (document.selection && document.selection.createRange){
				var range = document.selection.createRange();			
				begin = 0 - range.duplicate().moveStart('character', -100000);
				end = begin + range.text.length;
			}
			return {begin:begin,end:end};
        }       
	};

	//Predefined character definitions
	var charMap={
		'9':"[0-9]",
		'a':"[A-Za-z]",
		'*':"[A-Za-z0-9]"
	};
	
	//Helper method to inject character definitions
	$.mask={
		addPlaceholder : function(c,r){
			charMap[c]=r;
		}
	};
	
	$.fn.unmask=function(){
		return this.trigger("unmask");
	};
	
	//Main Method
	$.fn.mask = function(mask,settings) {	
		settings = $.extend({
			placeholder: "_",			
			completed: null
		}, settings);		
		
		//Build Regex for format validation
		var re = new RegExp("^"+	
		$.map( mask.split(""), function(c,i){		  		  
		  return charMap[c]||((/[A-Za-z0-9]/.test(c)?"":"\\")+c);
		}).join('')+				
		"$");		

		return this.each(function(){		
			var input=$(this);
			var buffer=new Array(mask.length);
			var locked=new Array(mask.length);
			var valid=false;   
			var ignore=false;  			//Variable for ignoring control keys
			var firstNonMaskPos=null; 
			
			//Build buffer layout from mask & determine the first non masked character			
			$.each( mask.split(""), function(i,c){				
				locked[i]=(charMap[c]==null);				
				buffer[i]=locked[i]?c:settings.placeholder;									
				if(!locked[i] && firstNonMaskPos==null)
					firstNonMaskPos=i;
			});		
			
			function focusEvent(){					
				checkVal();
				writeBuffer();
				setTimeout(function(){
					$(input[0]).caret(valid?mask.length:firstNonMaskPos);					
				},0);
			};
			
			function keydownEvent(e){				
				var pos=$(this).caret();
				var k = e.keyCode;
				ignore=(k < 16 || (k > 16 && k < 32 ) || (k > 32 && k < 41));
				
				//delete selection before proceeding
				if((pos.begin-pos.end)!=0 && (!ignore || k==8 || k==46)){
					clearBuffer(pos.begin,pos.end);
				}	
				//backspace and delete get special treatment
				if(k==8){//backspace					
					while(pos.begin-->=0){
						if(!locked[pos.begin]){								
							buffer[pos.begin]=settings.placeholder;
							if($.browser.opera){
								//Opera won't let you cancel the backspace, so we'll let it backspace over a dummy character.								
								s=writeBuffer();
								input.val(s.substring(0,pos.begin)+" "+s.substring(pos.begin));
								$(this).caret(pos.begin+1);								
							}else{
								writeBuffer();
								$(this).caret(Math.max(firstNonMaskPos,pos.begin));								
							}									
							return false;								
						}
					}						
				}else if(k==46){//delete
					clearBuffer(pos.begin,pos.begin+1);
					writeBuffer();
					$(this).caret(Math.max(firstNonMaskPos,pos.begin));					
					return false;
				}else if (k==27){//escape
					clearBuffer(0,mask.length);
					writeBuffer();
					$(this).caret(firstNonMaskPos);					
					return false;
				}									
			};
			
			function keypressEvent(e){					
				if(ignore){
					ignore=false;
					//Fixes Mac FF bug on backspace
					return (e.keyCode == 8)? false: null;
				}
				e=e||window.event;
				var k=e.charCode||e.keyCode||e.which;						
				var pos=$(this).caret();
								
				if(e.ctrlKey || e.altKey){//Ignore
					return true;
				}else if ((k>=41 && k<=122) ||k==32 || k>186){//typeable characters
					var p=seekNext(pos.begin-1);					
					if(p<mask.length){
						if(new RegExp(charMap[mask.charAt(p)]).test(String.fromCharCode(k))){
							buffer[p]=String.fromCharCode(k);									
							writeBuffer();
							var next=seekNext(p);
							$(this).caret(next);
							if(settings.completed && next == mask.length)
								settings.completed.call(input);
						}				
					}
				}				
				return false;				
			};
			
			function clearBuffer(start,end){
				for(var i=start;i<end&&i<mask.length;i++){
					if(!locked[i])
						buffer[i]=settings.placeholder;
				}				
			};
			
			function writeBuffer(){				
				return input.val(buffer.join('')).val();				
			};
			
			function checkVal(){	
				//try to place charcters where they belong
				var test=input.val();
				var pos=firstNonMaskPos;
				for(var i=0;i<mask.length;i++){					
					if(!locked[i]){
						buffer[i]=settings.placeholder;
						while(pos++<test.length){
							//Regex Test each char here.
							var reChar=new RegExp(charMap[mask.charAt(i)]);
							if(test.charAt(pos-1).match(reChar)){
								buffer[i]=test.charAt(pos-1);								
								break;
							}									
						}
					}
				}
				var s=writeBuffer();
				if(!s.match(re)){							
					input.val("");	
					clearBuffer(0,mask.length);
					valid=false;
				}else
					valid=true;
			};
			
			function seekNext(pos){				
				while(++pos<mask.length){					
					if(!locked[pos])
						return pos;
				}
				return mask.length;
			};
			
			input.one("unmask",function(){
				input.unbind("focus",focusEvent);
				input.unbind("blur",checkVal);
				input.unbind("keydown",keydownEvent);
				input.unbind("keypress",keypressEvent);
				if ($.browser.msie) 
					this.onpaste= null;                     
				else if ($.browser.mozilla)
					this.removeEventListener('input',checkVal,false);
			});
			input.bind("focus",focusEvent);
			input.bind("blur",checkVal);
			input.bind("keydown",keydownEvent);
			input.bind("keypress",keypressEvent);
			//Paste events for IE and Mozilla thanks to Kristinn Sigmundsson
			if ($.browser.msie) 
				this.onpaste= function(){setTimeout(checkVal,0);};                     
			else if ($.browser.mozilla)
				this.addEventListener('input',checkVal,false);
				
			checkVal();//Perform initial check for existing values
		});
	};
})(jQuery);

/* Fade 
* http://www.htmlstaff.org/ver.php?id=9312
* adaptado, zechim 2008
*/
fdObjects = new Object();
fdTimers = new Object();

function fd(id, destOp, rate, delta){
	
	var classe = $('#'+id).attr('class');
	
	if (classe == 'nofade') {
		
		return false;
		
	} else if (classe == 'fade') {
		
		
	   	object = document.getElementById(id);
    	clearTimeout(fdTimers[object.sourceIndex]);
    	bname=navigator.appName;

	    if(bname.indexOf("Microsoft")!=-1) {
	        opacidade = object.filters.alpha.opacity;
	        diff = destOp-opacidade;
	        direction = 1;
	        
	        if (opacidade > destOp){
	            direction = -1;
	        }
	        
	        delta=Math.min(direction*diff,delta);
	        object.filters.alpha.opacity+=direction*delta;
	        opacidade = object.filters.alpha.opacity;
	        rate = 1;
	        
	    } else if(bname.indexOf("Netscape")!=-1) {
	    	
	        opacidade = object.style.MozOpacity*100;
	        diff = destOp-opacidade;
	        direction = 1;
	        
	        if (opacidade > destOp){
	            direction = -1;
	        }
	
	        delta=Math.min(direction*diff,delta);
	        object.style.MozOpacity = (opacidade+(direction*delta))/100;
	        opacidade = object.style.MozOpacity*100;
	        rate = 1;
	        
	    } else
	        return;
	        
	    if (opacidade != destOp){
	        fdObjects[object.sourceIndex]=object;
	        fdTimers[object.sourceIndex]=setTimeout("fd('"+id+"', "+destOp+", "+rate+", "+delta+")",rate);
	    }
	}
}

/* Listagem */
function go(i) { $("#"+i).addClass("d_212on"); }
function go1(i) { $("#"+i).removeClass("d_212on"); }
function marca(i, imgid) {
	$(".nofade").addClass("fade");
	$(".nofade").removeClass("nofade");
	$("#"+imgid).removeClass("fade");
	$("#"+imgid).addClass("nofade");
	bname=navigator.appName;
    if(bname.indexOf("Microsoft") ==-1) {
    	$('.fade').fadeTo("slow", 0.3);
    }
	$(".d_212m").removeClass("d_212m");
	$("#"+i).addClass("d_212m");
}

/* Abas */

$(document).ready(function(){
	
	function isanimate (id1, id2, id3, id4, mostra) {
		//if ( !$('#'+id1).is(':animated') & !$('#'+id2).is(':animated') & !$('#'+id3).is(':animated') & !$('#'+id4).is(':animated') & !$('#'+mostra).is(':animated') ) {
      		$('#'+mostra).slideDown("slow");
   		//} 
	}
	
	function gohide (id1, id2, id3, id4) { $('#'+id1).slideUp(); $('#'+id2).slideUp(); $('#'+id3).slideUp(); $('#'+id4).slideUp(); }
	
	function changeback (id1, id2, id3, id4, mostra) {
		
		$('#'+id1).addClass(id1+'off');
		$('#'+id1).removeClass(id1+'on');
		$('#'+id1+"fonte").addClass('txt14');
		$('#'+id1+"fonte").removeClass('txt15');
		
		$('#'+id2).addClass(id2+'off');
		$('#'+id2).removeClass(id2+'on');
		$('#'+id2+"fonte").addClass('txt14');
		$('#'+id2+"fonte").removeClass('txt15');
		
		$('#'+id3).addClass(id3+'off');
		$('#'+id3).removeClass(id3+'on');
		$('#'+id3+"fonte").addClass('txt14');
		$('#'+id3+"fonte").removeClass('txt15');
		
		$('#'+id4).addClass(id4+'off');
		$('#'+id4).removeClass(id4+'on');
		$('#'+id4+"fonte").addClass('txt14');
		$('#'+id4+"fonte").removeClass('txt15');
		
		$('#'+mostra).addClass(mostra+'on');
		$('#'+mostra).removeClass(mostra+'off');
		$('#'+mostra+"fonte").addClass('txt15');
		$('#'+mostra+"fonte").removeClass('txt14');
	}
	
	$(".aba1").click(function(){
		gohide ('aba2c','aba3c', 'aba4c', 'aba5c');
		isanimate ('aba2c', 'aba3c', 'aba4c', 'aba5c', 'aba1c');
		changeback ('aba2', 'aba3', 'aba4', 'aba5', 'aba1');
		return false;
	});
	$(".aba2").click(function(){
		gohide ('aba1c','aba3c', 'aba4c', 'aba5c');
		isanimate ('aba1c', 'aba3c', 'aba4c', 'aba5c', 'aba2c');
		changeback ('aba1', 'aba3', 'aba4', 'aba5', 'aba2');
		return false;
	});
	$(".aba3").click(function(){
		gohide ('aba1c','aba2c', 'aba4c', 'aba5c');
		isanimate ('aba1c', 'aba2c', 'aba4c', 'aba5c', 'aba3c');
		changeback ('aba1', 'aba2', 'aba4', 'aba5', 'aba3');
		return false;
	});
	$(".aba4").click(function(){
		gohide ('aba1c','aba2c', 'aba3c', 'aba5c');
		isanimate ('aba1c', 'aba2c', 'aba3c', 'aba5c', 'aba4c');
		changeback ('aba1', 'aba2', 'aba3', 'aba5', 'aba4');
		return false;
	});
	$(".aba5").click(function(){
		gohide ('aba1c','aba2c', 'aba3c', 'aba4c');
		isanimate ('aba1c', 'aba2c', 'aba3c', 'aba4c', 'aba5c');
		changeback ('aba1', 'aba2', 'aba3', 'aba4', 'aba5');
		return false;
	});
});

/* Fade Menu */

$(document).ready(function(){
	$("#showmenu").click(function(){
		$('#topo2_1').fadeIn();
		clearTimeout();
		setTimeout("$(\'#topo2_1\').fadeOut();",5000);
	});
	$("#showmenu2").click(function(){
		$('#topo2_1').fadeIn();
		clearTimeout();
		setTimeout("$(\'#topo2_1\').fadeOut();",5000);
	});
});

function b1aa () { $('#ipt2').addClass('ch_2_3on'); }
function b2   () { $('#ipt2').removeClass('ch_2_3on'); }
function b3   () { $('#ipt3').addClass('ch_2_3on'); }
function b4   () { $('#ipt3').removeClass('ch_2_3on'); }

function b5(){
	qtd = document.busca.bs.value.length;
	palavra = document.busca.bs.value;
	if (!(palavra == 'PALAVRA-CHAVE')) {
		if (qtd > 2) {
			$('#b2_2').addClass('bg5');
		} else {
			$('#b2_2').removeClass('bg5');
		}	
	}
}

// Envia Form
function fb(){
	qtd = document.busca.bs.value.length;
	palavra = document.busca.bs.value;
	if (!(palavra == 'PALAVRA-CHAVE')) {
		if (qtd > 3) {
			document.busca.submit();
		}	
	}
}


// Focus Busca
function b(i){ $("#" + i).addClass("i_2111b"); $("#" + i).removeClass("i_2111a"); }

// Out Busca
function b1(i){ $("#" + i).removeClass("i_2111b"); $("#" + i).addClass("i_2111a"); }		

// Focus Busca
function ba(i){ $("#" + i).addClass("i_2111_1b"); $("#" + i).removeClass("i_2111_1a"); }

// Out Busca
function b1a(i){ $("#" + i).removeClass("i_2111_1b"); $("#" + i).addClass("i_2111_1a"); }	


function cm (i) {
	 $("#" + i).addClass("listagemON");
	 $("#" + i).removeClass("listagemOFF");
}
function cm2 (i) {
	 $("#" + i).removeClass("listagemON");
	 $("#" + i).addClass("listagemOFF");
	 
}

function marca(i, imgid) {
	$(".nofade").addClass("fade");
	$(".nofade").removeClass("nofade");
	$("#"+imgid).removeClass("fade");
	$("#"+imgid).addClass("nofade");
	bname=navigator.appName;
    if(bname.indexOf("Microsoft") ==-1) {
    	$('.fade').fadeTo("slow", 0.3);
    }
	$(".d_212m").removeClass("d_212m");
	$("#"+i).addClass("d_212m");
}

$(document).ready(function(){
	$("#Add").click(function(){
		if (!$("#faixa4").is(':visible')) { $("#faixa4").fadeIn('slow'); return false; }
		if (!$("#faixa5").is(':visible')) { $("#faixa5").fadeIn('slow'); return false; }
		if (!$("#faixa6").is(':visible')) { $("#faixa6").fadeIn('slow'); return false; }
		if (!$("#faixa7").is(':visible')) { $("#faixa7").fadeIn('slow'); return false; }
		if (!$("#faixa8").is(':visible')) { $("#faixa8").fadeIn('slow'); return false; }
		if (!$("#faixa9").is(':visible')) { $("#faixa9").fadeIn('slow'); return false; }
		return false;
	});
});


function createRequestObject() {
    var ro;
    var browser = navigator.appName;
    if(browser == "Microsoft Internet Explorer"){
        ro = new ActiveXObject("Microsoft.XMLHTTP");
    }else{
        ro = new XMLHttpRequest();
    }
    return ro;
}

var http = createRequestObject();

function html2entities(str){
	var keys=new Array("&amp;", "&nbsp;", "&iexcl;", "&cent;", "&pound;", "&curren;", "&yen;", "&brvbar;", "&sect;", "&uml;", "&copy;", "&ordf;", "&laquo;", "&not;", "&shy;", "&reg;", "&macr;", "&deg;", "&plusmn;", "&sup2;", "&sup3;", "&acute;", "&micro;", "&para;", "&middot;", "&cedil;", "&sup1;", "&ordm;", "&raquo;", "&frac14;", "&frac12;", "&frac34;", "&iquest;", "&Agrave;", "&Aacute;", "&Acirc;", "&Atilde;", "&Auml;", "&Aring;", "&AElig;", "&Ccedil;", "&Egrave;", "&Eacute;", "&Ecirc;", "&Euml;", "&Igrave;", "&Iacute;", "&Icirc;", "&Iuml;", "&ETH;", "&Ntilde;", "&Ograve;", "&Oacute;", "&Ocirc;", "&Otilde;", "&Ouml;", "&times;", "&Oslash;", "&Ugrave;", "&Uacute;", "&Ucirc;", "&Uuml;", "&Yacute;", "&THORN;", "&szlig;", "&agrave;", "&aacute;", "&acirc;", "&atilde;", "&auml;", "&aring;", "&aelig;", "&ccedil;", "&egrave;", "&eacute;", "&ecirc;", "&euml;", "&igrave;", "&iacute;", "&icirc;", "&iuml;", "&eth;", "&ntilde;", "&ograve;", "&oacute;", "&ocirc;", "&otilde;", "&ouml;", "&divide;", "&oslash;", "&ugrave;", "&uacute;", "&ucirc;", "&uuml;", "&yacute;", "&thorn;", "&yuml;", "&quot;", "&lt;", "&gt;", "");
	var values=new Array("&", " ", "¡", "¢", "£", "¤", "¥", "¦", "§", "¨", "©", "ª", "«", "¬", "­", "®", "¯", "°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "×", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ", "\"", "<", ">", "");
	for(var i=0; i<keys.length; i++){
	  if(values[i]!="&nbsp;"){
		str=str.replace(keys[i], values[i]);
		}
	}
    return str;
}

function psel(a, b, c, d, e) {
	// a = P|E|F
	// b = opt1
	// c = opt2
	// d = P|E|F (nome)
	// e = P|E|F (id)
	
	$('#entidades').addClass("fade1");
	$('#planos').addClass("fade1");
	$('#profissoes').addClass("fade1");
	
	switch(a){
		case 'planos':
			$('#planos').removeClass("fade1");
			$('#entidades').removeClass("fade1");
		break;    
		case 'entidades':
			$('#entidades').removeClass("fade1");
			$('#planos').removeClass("fade1");
		break;
		case 'profissoes':
			$('#profissoes').removeClass("fade1");
			$('#planos').removeClass("fade1");
		break;
	}
	
	document.getElementById('planos').src = 'images/plano_on.jpg';
	document.getElementById('entidades').src = 'images/entidade_on.jpg';
	document.getElementById('profissoes').src = 'images/profissao_on.jpg';
	
	q = document.getElementById('opt3');
	q.options.length= 0;
	q.options[0]=new Option("Selecione", "1", true, false);
	
	p = document.getElementById(c);
	p.disabled=true;
	p.disabled=false; 
	p.options.length= 0;
	p.options[0]=new Option("Carregando...", "1", true, false)
	document.getElementById(b).options.length= 0;
	document.getElementById(b).options[0]=new Option(d, e, true, false)
	
	
	http.open('POST', 'inp.php', true);
	var params = 'i='+e+'&a='+a+'&p=1';
	http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    http.setRequestHeader("Content-length", params.length);
    http.setRequestHeader("Connection", "close");
	http.onreadystatechange = 
	function () {
    	if(http.readyState == 4){
    		var response = http.responseText;
    		response = response.split("+");
    		document.getElementById(a).src = response[0];
			response = response[1].split("|");
			for (i=0; i<response.length-1; i++) {
				response2 = response[i].split("$&");
				p.options[i] = new Option(html2entities(response2[0]), response2[1]);
			}
    	}  
	};
   	http.send(params);
}


function psel2(a, b, c, d) {
	
	// a = P|E|F
	// b = opt1
	// c = opt2
	
	$('#entidades').addClass("fade1");
	$('#planos').addClass("fade1");
	$('#profissoes').addClass("fade1");
	
	switch(a){
		case 'planos':
			$('#planos').removeClass("fade1");
			$('#entidades').removeClass("fade1");
		break;    
		case 'entidades':
			$('#entidades').removeClass("fade1");
			$('#planos').removeClass("fade1");
		break;
		case 'profissoes':
			$('#profissoes').removeClass("fade1");
			$('#planos').removeClass("fade1");
		break;
	}
	
	document.getElementById('planos').src = 'images/plano_on.jpg';
	document.getElementById('entidades').src = 'images/entidade_on.jpg';
	document.getElementById('profissoes').src = 'images/profissao_on.jpg';
	
	q = document.getElementById('opt3');
	q.options.length= 0;
	q.options[0]=new Option("Selecione", "1", true, false);
	q.disabled = true;
	
	q = document.getElementById('opt2');
	q.options.length= 0;
	q.options[0]=new Option("Selecione", "1", true, false);
	q.disabled = true;

	if (d >0) {
	
		p = document.getElementById(c);
		p.disabled=true;
		p.disabled=false; 
		p.options.length= 0;
		p.options[0]=new Option("Carregando...", "1", true, false)
		
		http.open('POST', 'inp.php', true);
		var params = 'i='+d+'&a='+a+'&p=1';
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    http.setRequestHeader("Content-length", params.length);
    	http.setRequestHeader("Connection", "close");	
		http.onreadystatechange = 
		function () {
	    	if(http.readyState == 4){
	    		var response = http.responseText;
	    		response = response.split("+");
	    		document.getElementById(a).src = response[0];
				response = response[1].split("|");
				for (i=0; i<response.length-1; i++) {
					response2 = response[i].split("$&");
					p.options[i] = new Option(html2entities(response2[0]), response2[1]);
				}
	    	}  
		};
	   	http.send(params);
	}
	
}

function ssel(a, b, c, d) {
	// a = P|E|F
	// b = opt1
	// c = opt2
	// d = P|E|F (nome)
	switch(a){
		case 'planos':
			$('#profissoes').removeClass("fade1");
		break;    
		case 'entidades':
			$('#profissoes').removeClass("fade1");
		break;   
		case 'profissoes':
			$('#entidades').removeClass("fade1");
		break; 
		
	}
	
	p = document.getElementById(d);
	p.disabled=false; 
	
	// 1
	var idopt1 = document.getElementById(b).value;
	//alert('drop1 '+idopt1);
	// 2
	var idopt2 = document.getElementById(c).value;
	//alert('drop2 '+idopt2);
	
	p.options.length= 0;
	p.options[0]=new Option("Carregando...", "1", true, false)
	
	if (!(idopt2 == "0")) {
	
		http.open('POST', 'inp.php', true);
		var params = 'i='+idopt1+'&i2='+idopt2+'&a='+a+'&p=2';
		//alert('i='+idopt1+'&i2='+idopt2+'&a='+a+'&p=2');
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    http.setRequestHeader("Content-length", params.length);
    	http.setRequestHeader("Connection", "close");
		http.onreadystatechange = 
		function () {
    		if(http.readyState == 4){
    			var response = http.responseText;
    			response = response.split("+");
    			if (a == "profissoes") {
    				a = "planos";
    			} else {
    				if (a == "entidades") {
	    				a = "planos";
    				} else {
    					if (a == "planos") {
    					a = "entidades";
		    			}
    				}
    			}
    		
    			document.getElementById(a).src = response[0];
				response = response[1].split("|");
				for (i=0; i<response.length-1; i++) {
					response2 = response[i].split("$&");
					p.options[i] = new Option(html2entities(response2[0]), response2[1]);
				}
    		}  
		};
   		http.send(params);
   	} else {
   		p.options[0]=new Option("Selecione", "1", true, false)
   }
}

function tsel(a, b, c, d, e) {
	
	p = document.getElementById(d);
	p.disabled=false; 
	
	var idopt1 = document.getElementById(b).value;
	var idopt2 = document.getElementById(c).value;
	var idopt3 = document.getElementById(d).value;
	if (idopt3 != 0) {
		http.open('POST', 'inp.php', true);
		var params = 'i='+idopt3+'&a='+a+'&p=3';
		http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    http.setRequestHeader("Content-length", params.length);
		http.setRequestHeader("Connection", "close");
		http.onreadystatechange = 
		function () {
			if(http.readyState == 4){
				var response = http.responseText;
				document.getElementById(a).src = response;
				document.getElementById('img_004').src = 'images/listar2.jpg';
				function gt(){
					var idopt3a = document.getElementById(d).value;
					window.location.href = 'resultado.php?a='+idopt1+'&b='+idopt2+'&c='+idopt3a+'&o='+e+'&g=1';
				}
	    		function addEvent(obj, evType, fn){
	                if (obj.addEventListener) { obj.addEventListener(evType, fn, true); }
	            	if (obj.attachEvent) { obj.attachEvent("on"+evType, fn); }
	   			}
		        btn=document.getElementById("img_004")
		        addEvent(btn,"click",gt)
			}  
			
		};
		http.send(params);
	}
}



