fila=[]
ifila=0

try{
	xmlhttp = new XMLHttpRequest();
}catch(ee){
	try{
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	}catch(e){
		try{
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		}catch(E){
			xmlhttp = false;
		}
	}
}

function preencheUmCampo(objform,getpage){
	var campo = document.getElementById(objform);
	var results = getpage.split("|");
	campo.value = results[0];
}

function carregapesquisa(){
	var url="../adm/CepAlterar.asp?tipoAcao=carregapesquisa";
	fila[fila.length] = ['pesquisa',url,'carregadiv'];
	if( (ifila+1) == fila.length ){
		ajax();
	}
}

function preencheComboSelecao(objform,getpage){

	varIndex=1;
	combo = document.getElementById(objform)
	combo.options.length = 0;
	results = getpage.split("|");
	combo.selectedIndex = 0;
	for( i = 0; i < results.length; i++ ){
		string = results[i].split( ";" );
		if (string[0]!=''){
			combo.length = i + 1;
			combo.options[i].value = string[0];
			combo.options[i].text = string[1];

			if(string[2]=="S"){
				combo.options[i].selected = true;
				//combo.options[i].style.background="#73cfef";
			}else{
				varIndex=varIndex+1;
			}
			
		}
	}
}


function preencheCombo(objform,getpage){
	
	var combo = document.getElementById(objform);
	combo.options.length = 0;
	var results = getpage.split("|");
	
	for( i = 0; i < results.length; i++ ){
		string = results[i].split( ";" );
		if (string[0]!=''){
			combo.options[i] = new Option();
			combo.options[i].value = string[0];
			combo.options[i].text = string[1];
			
		}
	}
}

function preencheComboOptGruop(objform,getpage){
	
	var combo = document.getElementById(objform);
	dom.empty(combo);
	
	var results = getpage.split("|");
	var temp = '';
	
	for( i = 0; i < results.length; i++ ){
		arrLine = results[i].split( ";" );
		if (i == 0){
			var oOption = document.createElement('option');
			oOption.value = arrLine[0];
			oOption.innerText = arrLine[1];
			combo.appendChild(oOption);
		}else{
			arrLine = results[i].split( ";" );
			if (arrLine[0] != '') {
				if (arrLine[2] != temp){
					if (temp != ''){combo.appendChild(oGroup);}
					temp = arrLine[2];
					var oGroup = document.createElement('optgroup');
					oGroup.value = arrLine[2];
					oGroup.label = arrLine[3];
				}
				var oOption = document.createElement('option');
				oOption.value = arrLine[0];;
				oOption.innerText = arrLine[1];
				oGroup.appendChild(oOption);
			}
		}
	}
	if (temp != ''){combo.appendChild(oGroup);}
}

function ajax(){
	xmlhttp.open("GET",fila[ifila][1],false);
	xmlhttp.onreadystatechange=function(){
		if (xmlhttp.readyState == 4) {
			switch(fila[ifila][2]){
				case "preencheCombo":
					preencheCombo(fila[ifila][0],xmlhttp.responseText);
				break;
				case "preencheComboSelecao":
					preencheComboSelecao(fila[ifila][0],xmlhttp.responseText);
				break;
				case "preencheUmCampo":
					document.getElementById(fila[ifila][0]).value = xmlhttp.responseText;
				break;
				case "preencheComboOptGruop":
					preencheComboOptGruop(fila[ifila][0],xmlhttp.responseText);
				break;
				
				case "carregadiv":
					document.getElementById(fila[ifila][0]).innerHTML = xmlhttp.responseText;
				break;
			}
			ifila++;
			if(ifila < fila.length){
				setTimeout("ajax()",1);
			}
		}
	}
	xmlhttp.send(null);

}
