//funciones varias
//var locationBox = "http://localhost/shared/sections_inc/search/locationSelectBox.php";
//var regionsList = "http://localhost/shared/sections_inc/search/regionsList.php";
//var citiesList	= "http://localhost/shared/sections_inc/search/citiesList.php";
var shapeObject = "shape";
var transparentClassName = "transparent";
var locationBox = "http://www.viveamor.com/shared/sections_inc/search/locationSelectBox.php";
var regionsList = "http://www.viveamor.com/shared/sections_inc/search/regionsList.php";
var citiesList	= "http://www.viveamor.com/shared/sections_inc/search/citiesList.php";
function AJAXobj(){
	try{
		AJAXobj = new ActiveXObject("Msxml2.XMLHTTP");
	} catch(e){
		try{
			AJAXobj = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			AJAXobj = false;
		}
	}
	if(! AJAXobj && typeof XMLHttpRequest != 'undefined'){
		AJAXobj = new XMLHttpRequest();
	}
	return AJAXobj;
}

function loadLocations(_DIV_, _URL_){
	
	document.getElementById("loading_flag").style.display = 'block';

	try{
		_obj = AJAXobj();
	}catch(err){
		document.getElementById("loading_flag").style.display = 'block';
	}
	_obj.open("GET", _URL_, true);
	_obj.onreadystatechange = function(){
		if(_obj.readyState == 4){
			if(_obj.status == 200){
				document.getElementById(_DIV_).innerHTML = _obj.responseText;
				document.getElementById("loading_flag").style.display = 'none';
			}else{
				document.getElementById(_DIV_).innerHTML = '<b>Error:</b><br/>'+_obj.status+'Intentalo nuevamente<br/>';
				document.getElementById("loading_flag").style.display = 'none';
			}
		}
	}
	_obj.send(null);	
	
}

function loadRegions(country, index){
	
	var params = '?country='+country;
	
	document.getElementById("city").innerHTML ="";
	document.getElementById("selectButton").style.display = "block";
	document.getElementById("RRcountry").value	= country;
	document.getElementById("RRstate").value	= -1;
	document.getElementById("RRcountryT").value = document.getElementById("countryListBox").options[index].text;
	document.getElementById("RRcity").value		= -1;
	
	loadLocations('region', regionsList+params);
	
}

function loadCities(country, region, index){
	
	var params = '?country='+country+'&region='+escape(region);
	document.getElementById("RRstate").value = escape(region);
	document.getElementById("RRstateT").value = document.getElementById("regionsListBox").options[index].text;
	loadLocations('city', citiesList+params);
	
}

function citySelect(city, index){
	document.getElementById("RRcity").value	 = escape(city);
	document.getElementById("RRcityT").value = document.getElementById("citiesListBox").options[index].text;
}

function setLocation2Search(){
	
	var selectedCountry	 = document.getElementById("RRcountry").value;
	var selectedCountryT = document.getElementById("RRcountryT").value;
	var selectedRegion	 = document.getElementById("RRstate").value;
	var selectedRegionT  = document.getElementById("RRstateT").value;
	var selectedCity	 = document.getElementById("RRcity").value;
	var selectedCityT	 = document.getElementById("RRcityT").value;
	var locationLen	 	 = document.getElementById("location").length;
	var newLocation		 = selectedCountry+'||'+selectedRegion+'||'+selectedCity;
	var xt;
	var found;
	
	for(xt = 0; xt <= parseInt(locationLen)-1; xt++){
		if(newLocation == document.getElementById("location").options[xt].value){
			found = true;
			break;
		}else{
			found = false;
		}
	}
	
	if(!found){
		if(false == isNaN(parseInt(selectedRegion))){
			if(parseInt(selectedRegion) > -1){
				if(parseInt(selectedCity) > -1){
					document.getElementById("location").options[locationLen] = new Option(selectedCityT+', '+selectedCountryT, newLocation, true);
					document.getElementById("location").options[locationLen].selected = true;
				}else{
					document.getElementById("location").options[locationLen] = new Option(selectedRegionT+', '+selectedCountryT, newLocation, true);
					document.getElementById("location").options[locationLen].selected = true;
				}
			}else{
				document.getElementById("location").options[locationLen] = new Option(selectedCountryT, newLocation, true);
				document.getElementById("location").options[locationLen].selected = true;
			}
		}else{
			if(isNaN(selectedCity)){
				document.getElementById("location").options[locationLen] = new Option(selectedCityT+', '+selectedCountryT, newLocation, true);
				document.getElementById("location").options[locationLen].selected = true;
			}else{
				document.getElementById("location").options[locationLen] = new Option(selectedRegionT+', '+selectedCountryT, newLocation, true);
				document.getElementById("location").options[locationLen].selected = true;
			}
		}
	}else{
		document.getElementById("location").options[xt].selected = true;
	}
	
	closeLocationBox();

}

function closeLocationBox(){
	var value = document.getElementById("location").value;
	
	if(value == 0){
		document.getElementById("location").options[0].selected = true;
	}
	
	unshapePage();
	document.getElementById("locationSelect").style.display = "none";
}

function setAgeRange(age){
	var maxAgeRange = 60;
	var minAgeRange = 18;
	var avgAgeMax	= 5;
	var i;
	var z = 0;
	var strLabel;
	
	document.getElementById("age2").length = 0;
	
	for(i = age; i <= maxAgeRange; i++){
		
		if(i >= maxAgeRange){
			strLabel = "60+";
		}else{
			strLabel = i;
		}
		
		document.getElementById("age2").options[z] = new Option(strLabel, i);
		
		if(i == parseInt(age)+parseInt(avgAgeMax)){
			document.getElementById("age2").options[z].selected = true;
		}
		
		z++;
	}
}

function selectLocation(range){

	var sw = screen.width;
	var sh = screen.height;
	var aw = 550;
	var ah = 280;
	var nx, ny;
	var objectName  = "locationSelect";
	
	nx = parseInt(sw/2)-parseInt(aw/2);
	ny = parseInt(sh/2)-parseInt(ah/2);
	
	if(range == 0){
		document.getElementById("loading_flag").style.left = parseInt(sw/2)-50;
		document.getElementById("loading_flag").style.top  = parseInt(sh/2)-20;
		document.getElementById(objectName).style.left	= nx+"px";
		document.getElementById(objectName).style.top	= ny+"px";
		document.getElementById(objectName).style.display	= "block";
		shapePage();
		loadLocations(objectName, locationBox);
		
	}
	
}

function shapePage(){
	var pageSize = getPageSizeWithScroll();
	document.getElementById(shapeObject).classname		= transparentClassName;
	document.getElementById(shapeObject).style.width	= pageSize[0];
	document.getElementById(shapeObject).style.height	= pageSize[1];
	document.getElementById(shapeObject).style.left		= "0px";
	document.getElementById(shapeObject).style.top		= "0px";
	document.getElementById(shapeObject).style.display	= "block";
}

function unshapePage(){
	document.getElementById(shapeObject).style.display	= "none";
}

function getPageSizeWithScroll(){
	if(window.innerHeight && window.scrollMaxY){
		//para firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	}else if(document.body.scrollHeight > document.body.offsetHeight){
		//para mac (ie)
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	}else{
		//para IE 6 Strict, Mozilla (no FF) y Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
  	}
	arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	return arrayPageSizeWithScroll;
}