var txt_country;
var	txt_state;
var	txt_city;
var country_target;
var state_target;
var city_target;

function cargar()
{
	sacar_publicidad();
	setTimeout("inicializar()",5000);
}

function inicializar()
{	
	initialize_locator2();	
	setTimeout("posicionar()",1000);
}

function posicionar()
{
	ajax_load("locator/locator.class.php","","div_country");
}

function initialize_locator(country,state,city) 
{
	txt_country=country;
	txt_state=state;
	txt_city=city;
	
	if (document.addEventListener) {
		document.addEventListener("DOMContentLoaded", cargar, false);
	} else 
		document.body.onload=cargar;
}

function initialize_locator2()
{
	//document.write ("<input id='launch' type='image' src='./images/globe15.gif' alt='launch world locator' title='launch world locator' onclick='show_location_form();'>");
		
	country_target=document.getElementById(txt_country);
	state_target=document.getElementById(txt_state);
	city_target=document.getElementById(txt_city);

	if (!country_target)
		alert ("Error: " + txt_country + " is not a valid html object. Locator will not work!!!!");
	else {
		country_target.readOnly=true;
//		country_target.onclick=show_location_form;
	}
		
	if (!state_target)
		alert ("Error: " + txt_state + " is not a valid html object. Locator will not work!!!!");
	else {
		state_target.readOnly=true;
//		state_target.onclick=show_location_form;
	}
	
	if (!city_target)
		alert ("Error: " + txt_city + " is not a valid html object. Locator will not work!!!!");
	else {
		city_target.readOnly=true;	
//		city_target.onclick=show_location_form;
	}

	pos1=findPos(country_target);
	pos2=findPos(state_target);
	pos3=findPos(city_target);
	
	
	div_country = document.createElement("div"); // create dynamically div tag
	div_country.setAttribute("id","div_country");       //give id to it
	div_country.style.position="absolute";       
	div_country.style.left=pos1[0] + "px";
	div_country.style.top=pos1[1] + "px";
	
	div_country.innerHTML="<span style='color: blue; font-size: 12px'><b>initializing world global locator powered by: GLTTB.com...</b></span>";

	div_state = document.createElement("div"); // create dynamically div tag
	div_state.setAttribute("id","div_state");       //give id to it
	div_state.style.position="absolute";       
	div_state.style.left=pos2[0] + "px";
	div_state.style.top=pos2[1] + "px";
	div_state.innerHTML="no data";

	div_city = document.createElement("div"); // create dynamically div tag
	div_city.setAttribute("id","div_city");       //give id to it
	div_city.style.position="absolute";       
	div_city.style.left=pos3[0] + "px";
	div_city.style.top=pos3[1] + "px";
	div_city.innerHTML="no data";

	div_extra = document.createElement("div"); // create dynamically div tag
	div_extra.setAttribute("id","div_extra");       //give id to it
	div_extra.style.position="absolute";       
	div_extra.style.width=300;
	div_extra.style.left=getPositionLeft(city_target);
	div_extra.style.top=getPositionTop(city_target) + 20;
	div_extra.style.display='none';
	div_extra.innerHTML="";

	country_target.parentNode.appendChild(div_country);	
	//country_target.parentNode.insertBefore(div_country,country_target);	
	state_target.parentNode.appendChild(div_state);
	//state_target.parentNode.insertBefore(div_state,state_target);
	city_target.parentNode.appendChild(div_city);
	//city_target.parentNode.insertBefore(div_city,city_target);
	city_target.parentNode.appendChild(div_extra);
	
	country_target.style.visibility='hidden';
	state_target.style.visibility='hidden';
	city_target.style.visibility='hidden';
	
}

function copy_values(obj)
{	
	var country_source=document.getElementById('countries');
	var country_target=document.getElementById(txt_country);
	var state_source=document.getElementById('states');
	var state_target=document.getElementById(txt_state);
	var city_source=document.getElementById('cities');
	var city_target=document.getElementById(txt_city);
	
	if (obj.id == "countries") {	
		copy_values2("",state_target);
		copy_values2("",city_target);
		
		if (country_source.selectedIndex <= 0)
			copy_values2("",country_target);			
		else
			copy_values2(country_source.options[country_source.selectedIndex].text, country_target);			
		return;
	}
	
	if (obj.id == "states") {
		copy_values2("",city_target);		
		if (state_source) {		
			if (state_source.selectedIndex <= 0)
				copy_values2("",state_target);				
			else
				copy_values2(state_source.options[state_source.selectedIndex].text,state_target);				
		} else
			copy_values2("",state_target);			
		return;
	}
	
	if (city_source != "no data") {
		if (city_source.selectedIndex <= 0)
			copy_values2("",city_target);			
		else
			copy_values2(city_source.options[city_source.selectedIndex].text,city_target);			
	} else
		copy_values2("",city_target);	
	
}

function copy_values2(valor,target)
{
	if (target.type == "text") {
		target.value=valor;		
	}
	
	if (target.type == "select-one") {
		if (valor == "")
			target.selectedIndex=-1;
		else {
			target.options[0]=new Option(valor,valor);
			target.selectedIndex=0;
		}
				
	}
}

//muestra el fomrulario de country, state, city
function show_location_form() {
	var locator=document.getElementById('div_locator');
	var ok=locator.style.display=='block';
	
	if (ok) 
		locator.style.display='none';
	else
		locator.style.display='block';	
		
}

function getPositionLeft(This){
	var el = This;
	var pL = 0;
	while(el) {
		pL += el.offsetLeft;
		el=el.offsetParent;
	}
	return pL;
}

// To find the top position, add this snippet to your code:
function getPositionTop(This){
	var el = This;
	var pT = 0;
	while(el){
		pT += el.offsetTop;
		el=el.offsetParent;
	}
	return pT;
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
