var msg ='', quebralinha = '', nodeBorder = '', nodeBack='#FFFF33', x=-1, countNode=0;

function addTableIrmao(){
    
	var i, x=0;
    var td = document.getElementById('table_irmao');
	var div = document.createElement('div');
	td.appendChild(div);
	for(i=0; i<td.childNodes.length; i++){
		if(td.childNodes[i].tagName == 'DIV'){
		     x = i;	 
		}
	}
	td.childNodes[x].innerHTML = document.getElementById('table_irmao_orginal').innerHTML; 
	addEventsLink();
}

function removeTableIrmao(e){
	if (window.event) {
        target = window.event.srcElement;
    } else if (e) {
        target = e.target;
    } else return;
	
	confirmValue = confirm('Tem certeza que quer excluir?');
	if(confirmValue){
	   target.parentNode.parentNode.removeChild(target.parentNode);	
	}
	else{
		
	}
}

function addEventsLink(){
	objA = document.getElementsByTagName('a');
	for(i=0; i<objA.length; i++){
		if(objA[i].id == 'remover_irmao'){
			addEvent(objA[i], 'click', removeTableIrmao);
		}
	}
}

function addEventForms(){
	var i;
	addEventsLink();
	objforms = document.getElementsByTagName('form');
	for(i=0; i< objforms.length; i++ ){
		addEvent(objforms[i], 'submit', validateForms);
		for(y=0; y<objforms[i].length; y++){
			nodeInput = objforms[i].elements[y];
			if(nodeInput.name == 'cod_cpf_mae'){
				addEvent(nodeInput, 'keyup', mascaraEnet2);
				addEvent(nodeInput, 'blur', veficaCpfAuterado);
			}
			else if (nodeInput.name == 'cod_cpf_pai'){
				addEvent(nodeInput, 'blur', veficaCpfAuterado);
			}
		}
	}
}




function addEventFilhosCpf(){
	var i;
	objforms = document.getElementsByTagName('form');
	for(i=0; i< objforms.length; i++ ){
		for(y=0; y<objforms[i].length; y++){
			nodeInput = objforms[i].elements[y];
			if(/^cod_cpf_irmao/.test(nodeInput.name)){
				addEvent(nodeInput, 'blur', veficaCpfAuterado);
			}
    	}
	}
}


function veficaCpfAuterado(e){
	if (window.event) {
        target = window.event.srcElement;
    } else if (e) {
        target = e.target;
    } else return;
	
    if(target.name == 'cod_cpf_mae') {	
	  if(document.form1.cpf_mae_tmp.value != target.value){
          document.form1.id_mae.value = '';		
	  }
	}
	else if(target.name == 'cod_cpf_pai'){
		if(document.form1.cpf_pai_tmp.value != target.value){
         document.form1.id_pai.value = '';		
	    }
	}
	else if(/^cod_cpf_irmao/.test(target.name)) {
		 arr = target.parentNode.parentNode.parentNode.parentNode.parentNode.getElementsByTagName('input');
		 zerarIdIrmao = false;
		 for(i=0; i< arr.length; i++){
		 	if(arr[i].name == 'cpf_irmao_tmp[]'){
				if(target.value != arr[i].value){
					zerarIdIrmao = true;
				}
			}
			else if(/^id_irmao/.test(arr[i].name)){
			   if(zerarIdIrmao){
				    arr[i].value = '';
				}	
			}
		 }
	}  
}




function validateForms(e)
{	
	if (window.event) {
        target = window.event.srcElement;
    } else if (e) {
        target = e.target;
    } else return;
	
	msg = '';
	x = -1;
	
	for(countNode=0; countNode<target.length; countNode++ ){
	   objNode = target.elements[countNode];
	   if(nodeBorder != ''){
	      objNode.style.borderColor = ''; 	
	   } 
	   else{
	      objNode.style.backgroundColor = '';
	   }
	   if(objNode.getAttribute('mensagem') && objNode.disabled == false){
	   	   if(objNode.type == 'text' || objNode.type == 'textarea'){
		   	  validateInputText(objNode);
			  			  
		   }
		   else if(objNode.type == 'select-one' || objNode.type == 'select' || objNode.type == 'select-multiple'){
		   	   validateSelect(objNode);
		   }
		   
	   }	
	}
    if(msg != ''){	
	   alert(msg);
	   if(x!=-1){
		  target.elements[x].focus();
	   }
	     if (e && e.preventDefault) e.preventDefault(); // DOM style
           return false;
    }
}

function validateInputText(objInput){
	if(objInput.value == ''){
		nodeStyleError(objInput);
		if(msg.indexOf(objInput.getAttribute('mensagem')) == -1){
		   msg +=  quebralinha + objInput.getAttribute('mensagem');
		   quebralinha = '\n';
		} 
		x = (x == -1)?countNode:x;
	}
	return;
}

function nodeStyleError(obj){
	if(nodeBorder != ''){
	    obj.style.borderColor = nodeBorder; 	
	} 
	else{
		obj.style.backgroundColor = nodeBack;
	}
	return;
}


function validateSelect(obj){
	var i, sel=false;
	for(i=0; i< obj.options.length; i++){
	  if(obj.options[i].selected && obj.options[i].value !=''){
	   	sel = true;
	  }	
	}
	if(sel){
		return;
	}
	else{
		nodeStyleError(obj);
		if(msg.indexOf(obj.getAttribute('mensagem')) == -1){
		  msg +=  quebralinha + obj.getAttribute('mensagem');
		  quebralinha = '\n';
		} 
		x = (x == -1)?countNode:x;
		return;
	}
}

function getDadosCliente(idmae){
	var ajaxMae = new ajax;
	ajaxMae.setParams('acao', 'getDadosCliente', 'id_cliente', idmae);
	ajaxMae.returnXML = true;
	ajaxMae.sendLoad('cliente_engine.php', '', setDados, 'POST');
}

function setDados(xmlDadosMae){
	//document.getElementById('debug').innerHTML = '';
	var itemsMae =  xmlDadosMae.getElementsByTagName('item');
	var id_estado = '';
	var id_cidades = '';
	for(var i=0; i<itemsMae.length; i++){
		nodeMae = itemsMae[i];
		if(document.getElementById(nodeMae.getAttribute('nodeName')).name == 'id_estados'){
		    id_estado = nodeMae.getAttribute('value');
			setOption(document.getElementById(nodeMae.getAttribute('nodeName')),id_estado); 	
		}
		else if(document.getElementById(nodeMae.getAttribute('nodeName')).name == 'id_cidades')
		{
			
			 id_cidades = nodeMae.getAttribute('value'); 
			 Atualiza(id_estado, id_cidades);		 
		}
		else
		{
		  document.getElementById(nodeMae.getAttribute('nodeName')).value = nodeMae.getAttribute('value');
		} 
		//document.getElementById('debug').innerHTML += nodeMae.getAttribute('nodeName') + '= '+ nodeMae.getAttribute('value') 	 +'<br />';
	}
  
}

function setOption(objSelect, value){
     
   for(var i=0; i<objSelect.options.length; i++){
	   optionNode = objSelect.options[i];
	   if(optionNode.value == value){
	   	  optionNode.selected = true;
	   }  	
	}
}

function getDadosFilhos(idMae, idFilho){
	
	if(idMae == ''){
		return;
	}
	var ajaxFilhos = new ajax;
	
	idsIrmao = document.getElementsByName('id_irmao[]');
	idIrmao = '(-1';
	for(i=0; i<idsIrmao.length; i++){
		if(idsIrmao[i].value){
	      idIrmao += ', ' + idsIrmao[i].value; 		
		}
	}
	idIrmao += ' )'; 
	
    var visual = document.forms[0].visual.value;	
	
	ajaxFilhos.setParams('acao', 'get_dados_filhos', 'id_cliente', idMae, 'id_filho', idFilho);
	ajaxFilhos.setParams('id_irmaos', idIrmao, 'visual', visual);
	ajaxFilhos.sendLoad('../sistemas/cadastro/cliente_engine.php', '', setDadosFilhos, 'POST');
}


function setDadosFilhos(dados){
	document.getElementById('table_irmao').innerHTML +=  dados;
	addEventsLink();
	addEventFilhosCpf();
	document.body.style.cursor = '';
	
}
