var BuscaCidadeIFrameObj;
var BuscaBairroIFrameObj;
var BuscaCidadeV2IFrameObj;

function bindUFCidadeV2(formName, comboCidade, comboBairro, valorUf, valorHttp, tipo)
{
	var BuscaCidadeV2IFrameDoc;
	
	if(tipo == null)
		tipo = "";
	
	form = document.getElementById(formName);
	if (!document.createElement)
		return true;
	limpaCombo(comboCidade);
	limpaCombo(comboBairro);
	if ((valorUf != ''))
	{
		if(valorHttp == 'true')
			URL = comHTTPS + sitePath + comHTTPSComum + "/pgBuscaPorUFCidade/pgBuscaPorUFCidade.aspx?formName=" + formName + "&comboCidade=" + comboCidade + "&valorUf=" + valorUf + "&tipo=" + tipo;
		else
			URL = semHTTPS + sitePath + semHTTPSComum + "/pgBuscaPorUFCidade/pgBuscaPorUFCidade.aspx?formName=" + formName + "&comboCidade=" + comboCidade + "&valorUf=" + valorUf + "&tipo=" + tipo;
		if (!BuscaCidadeV2IFrameObj && document.createElement){
			try {
				var tempIFrame=document.createElement('iframe');
				tempIFrame.setAttribute('id','BuscaCidadeV2IFrame');
				tempIFrame.setAttribute('src','blank.htm');
				tempIFrame.style.border='0px';
				tempIFrame.style.width='0px';
				tempIFrame.style.height='0px';
				BuscaCidadeV2IFrameObj = document.body.appendChild(tempIFrame);				
				if (document.frames) {
					BuscaCidadeV2IFrameObj = document.frames['BuscaCidadeV2IFrame'];
				}
			} catch(exception) {
				iframeHTML='<iframe id="BuscaCidadeV2IFrame" style="';
				iframeHTML+='border:0px;';
				iframeHTML+='width:0px;';
				iframeHTML+='height:0px;';
				iframeHTML+='"><\/iframe>';
				document.body.innerHTML+=iframeHTML;
				BuscaCidadeV2IFrameObj = new Object();
				BuscaCidadeV2IFrameObj.document = new Object();
				BuscaCidadeV2IFrameObj.document.location = new Object();
				//BuscaMotoMarcaAnoIFrameObj.document.location.iframe = form('BuscaMotoMarcaAnoIFrameObj');
				BuscaCidadeV2IFrameObj.document.location.replace = function(location) {
					this.iframe.src = location;
				}
			}
		}
		if (navigator.userAgent.indexOf('Gecko') !=-1 && !BuscaCidadeV2IFrameObj.contentDocument) {
			setTimeout('bindUFCidadeV2("'+formName+'")',10);
			return false;
		}
		if (BuscaCidadeV2IFrameObj.contentDocument) {
			BuscaCidadeV2IFrameDoc = BuscaCidadeV2IFrameObj.contentDocument;
		} else if (BuscaCidadeV2IFrameObj.contentWindow) {
			BuscaCidadeV2IFrameDoc = BuscaCidadeV2IFrameObj.contentWindow.document;
		} else if (BuscaCidadeV2IFrameObj.document) {
			BuscaCidadeV2IFrameDoc = BuscaCidadeV2IFrameObj.document;
		} else {
			return true;
		}
		BuscaCidadeV2IFrameDoc.location.replace(URL);
		return false;
	}
}

function bindUFCidade(formName, comboCidade1, comboCidade2, comboCidade3, comboBairro1, comboBairro2, comboBairro3, valorUf, valorHttp)
{
	var BuscaCidadeIFrameDoc;
	form = document.getElementById(formName);
	if (!document.createElement)
		return true;
	limpaCombo(comboCidade1);
	limpaCombo(comboCidade2);
	limpaCombo(comboCidade3);
	limpaCombo(comboBairro1);
	limpaCombo(comboBairro2);
	limpaCombo(comboBairro3);
	if (valorUf != '')
	{
		if(valorHttp == 'true')
			URL = comHTTPS + sitePath + comHTTPSComum + "/pgBuscaPorUFCidade/pgBuscaPorUFCidade.aspx?formName=" + formName + "&comboCidade1=" + comboCidade1 + "&comboCidade2=" + comboCidade2 + "&comboCidade3=" + comboCidade3 + "&valorUf=" + valorUf;
		else
			URL = semHTTPS + sitePath + semHTTPSComum + "/pgBuscaPorUFCidade/pgBuscaPorUFCidade.aspx?formName=" + formName + "&comboCidade1=" + comboCidade1 + "&comboCidade2=" + comboCidade2 + "&comboCidade3=" + comboCidade3 + "&valorUf=" + valorUf;
		if (!BuscaCidadeIFrameObj && document.createElement){
			try {
				var tempIFrame=document.createElement('iframe');
				tempIFrame.setAttribute('id','BuscaCidadeIFrame');
				tempIFrame.setAttribute('src','blank.htm');
				tempIFrame.style.border='0px';
				tempIFrame.style.width='0px';
				tempIFrame.style.height='0px';
				BuscaCidadeIFrameObj = document.body.appendChild(tempIFrame);				
				if (document.frames) {
					BuscaCidadeIFrameObj = document.frames['BuscaCidadeIFrame'];
				}
			} catch(exception) {
				iframeHTML='<iframe id="BuscaCidadeIFrame" style="';
				iframeHTML+='border:0px;';
				iframeHTML+='width:0px;';
				iframeHTML+='height:0px;';
				iframeHTML+='"><\/iframe>';
				document.body.innerHTML+=iframeHTML;
				BuscaCidadeIFrameObj = new Object();
				BuscaCidadeIFrameObj.document = new Object();
				BuscaCidadeIFrameObj.document.location = new Object();
				//BuscaMotoMarcaAnoIFrameObj.document.location.iframe = form('BuscaMotoMarcaAnoIFrameObj');
				BuscaCidadeIFrameObj.document.location.replace = function(location) {
					this.iframe.src = location;
				}
			}
		}
		if (navigator.userAgent.indexOf('Gecko') !=-1 && !BuscaCidadeIFrameObj.contentDocument) {
			setTimeout('bindUFCidade("'+formName+'")',10);
			return false;
		}
		if (BuscaCidadeIFrameObj.contentDocument) {
			BuscaCidadeIFrameDoc = BuscaCidadeIFrameObj.contentDocument;
		} else if (BuscaCidadeIFrameObj.contentWindow) {
			BuscaCidadeIFrameDoc = BuscaCidadeIFrameObj.contentWindow.document;
		} else if (BuscaCidadeIFrameObj.document) {
			BuscaCidadeIFrameDoc = BuscaCidadeIFrameObj.document;
		} else {
			return true;
		}
		BuscaCidadeIFrameDoc.location.replace(URL);
		return false;
	}
}

function bindCidadeBairro(formName, comboBairro, valorCidade, valorHttp, tipo)
{
	if(tipo == null)
		tipo = "";
	
	var BuscaBairroIFrameDoc;
	form = document.getElementById(formName);
	if (!document.createElement)
		return true;
	limpaCombo(comboBairro);
	
	if ((valorCidade != ''))
	{
		if(valorHttp == 'true')
			URL = comHTTPS + sitePath + comHTTPSComum + "/pgBuscaPorCidadeBairro/pgBuscaPorCidadeBairro.aspx?formName=" + formName + "&comboBairro=" + comboBairro + "&valorCidade=" + valorCidade + "&tipo=" + tipo;
		else
			URL = semHTTPS + sitePath + semHTTPSComum + "/pgBuscaPorCidadeBairro/pgBuscaPorCidadeBairro.aspx?formName=" + formName + "&comboBairro=" + comboBairro + "&valorCidade=" + valorCidade + "&tipo=" + tipo;
		if (!BuscaBairroIFrameObj && document.createElement){
			try {
				var tempIFrame=document.createElement('iframe');
				tempIFrame.setAttribute('id','BuscaBairroIFrame');
				tempIFrame.setAttribute('src','blank.htm');
				tempIFrame.style.border='0px';
				tempIFrame.style.width='0px';
				tempIFrame.style.height='0px';
				BuscaBairroIFrameObj = document.body.appendChild(tempIFrame);				
				if (document.frames) {
					BuscaBairroIFrameObj = document.frames['BuscaBairroIFrame'];
				}
			} catch(exception) {
				iframeHTML='<iframe id="BuscaBairroIFrame" style="';
				iframeHTML+='border:0px;';
				iframeHTML+='width:0px;';
				iframeHTML+='height:0px;';
				iframeHTML+='"><\/iframe>';
				document.body.innerHTML+=iframeHTML;
				BuscaBairroIFrameObj = new Object();
				BuscaBairroIFrameObj.document = new Object();
				BuscaBairroIFrameObj.document.location = new Object();
				//BuscaMotoMarcaAnoIFrameObj.document.location.iframe = form('BuscaMotoMarcaAnoIFrameObj');
				BuscaBairroIFrameObj.document.location.replace = function(location) {
					this.iframe.src = location;
				}
			}
		}
		if (navigator.userAgent.indexOf('Gecko') !=-1 && !BuscaBairroIFrameObj.contentDocument) {
			setTimeout('bindCidadeBairro("'+formName+'")',10);
			return false;
		}
		if (BuscaBairroIFrameObj.contentDocument) {
			BuscaBairroIFrameDoc = BuscaBairroIFrameObj.contentDocument;
		} else if (BuscaBairroIFrameObj.contentWindow) {
			BuscaBairroIFrameDoc = BuscaBairroIFrameObj.contentWindow.document;
		} else if (BuscaBairroIFrameObj.document) {
			BuscaBairroIFrameDoc = BuscaBairroIFrameObj.document;
		} else {
			return true;
		}
		BuscaBairroIFrameDoc.location.replace(URL);
		return false;
	}
}


function setUFCidade(comboCidade, comboCidade1, comboCidade2, comboCidade3, valorCidade, valorCidade1, valorCidade2, valorCidade3){
	if (comboCidade != "")
		setUFCidadeV2(comboCidade, valorCidade);
	else
		setUFCidadeV1(comboCidade1, comboCidade2, comboCidade3, valorCidade1, valorCidade2, valorCidade3);
}

function setUFCidadeV1(comboCidade1, comboCidade2, comboCidade3, valorCidade1, valorCidade2, valorCidade3)
{
	var array1 = new Array;
	var array2 = new Array;	
	var array3 = new Array;	
	array1 = document.getElementById('BuscaCidadeIFrame').contentWindow.CidadeUF;
	array2 = copiarArray(array1);
	array3 = copiarArray(array1);
	
	limpaCombo(comboCidade1);
	limpaCombo(comboCidade2);
	limpaCombo(comboCidade3);

	for (i = 0; i < array1.length; i++)
	{
		document.getElementById(comboCidade1).options[i] = array1[i];
		document.getElementById(comboCidade2).options[i] = array2[i];
		document.getElementById(comboCidade3).options[i] = array3[i];		
		
		if (array1[i].value == valorCidade1)
			document.getElementById(comboCidade1).options.selectedIndex = i;
			
		if (array2[i].value == valorCidade2)
			document.getElementById(comboCidade2).options.selectedIndex = i;
			
		if (array3[i].value == valorCidade3)
			document.getElementById(comboCidade3).options.selectedIndex = i;
	}
}

function setUFCidadeV2(comboCidade, valorCidade)
{
	array = document.getElementById('BuscaCidadeV2IFrame').contentWindow.CidadeUF;
	limpaCombo(comboCidade);

	for (i = 0; i < array.length; i++)
	{
		document.getElementById(comboCidade).options[i] = array[i];
		
		if (array[i].value == valorCidade)
			document.getElementById(comboCidade).options.selectedIndex = i;
	}
}

function setCidadeBairro(comboBairro, valorBairro){
	var array = new Array;
	
	array = document.getElementById('BuscaBairroIFrame').contentWindow.BairroUF;
	
	
	limpaCombo(comboBairro);
	

	for (i = 0; i < array.length; i++)
	{
		document.getElementById(comboBairro).options[i] = array[i];
		
		
		if (array[i].value == valorBairro)
			document.getElementById(comboBairro).options.selectedIndex = i;
		
	}
}

function copiarArray(arr)
{
	var arrayresult = new Array();
	for (i=0; i<arr.length; i++)
	{
		arrayresult[i] = new Option(arr[i].text, arr[i].value);
	}
	return arrayresult;
}

function limpaCombo(combo)
{
	if (document.getElementById(combo) != null)
		document.getElementById(combo).options.length = 0;
}
