var geocoder = null;

function replaceContent(lat,lon)
{
	str = contentStr();
	document.getElementById("btnBuild").disabled = true;
	document.getElementById("btnKML").disabled = false;
	document.getElementById("btnXML").disabled = false;
	str = str.replace(/\[name\]/g,document.getElementById("txtBusiness").value);
	str = str.replace(/\[description\]/g,document.getElementById("txtDescription").value);
	str = str.replace(/\[address\]/g,document.getElementById("txtAddress").value);
	str = str.replace(/\[city\]/g,document.getElementById("txtCity").value);
	str = str.replace(/\[state\]/g,document.getElementById("selState").options[document.getElementById("selState").selectedIndex].value);
	str = str.replace(/\[zip\]/g,document.getElementById("txtZip").value);
	str = str.replace(/\[phone\]/g,document.getElementById("txtPhone").value);
	str = str.replace(/\[url\]/g,document.getElementById("txtURL").value);
	str = str.replace(/\[latitude\]/g,lat);
	str = str.replace(/\[longitude\]/g,lon);
	document.getElementById("dvOutDiv").innerHTML = str;
	str = kmlStr();
	str = str.replace(/\[name\]/g,document.getElementById("txtBusiness").value);
	str = str.replace(/\[description\]/g,document.getElementById("txtDescription").value);
	str = str.replace(/\[address\]/g,document.getElementById("txtAddress").value);
	str = str.replace(/\[city\]/g,document.getElementById("txtCity").value);
	str = str.replace(/\[state\]/g,document.getElementById("selState").options[document.getElementById("selState").selectedIndex].value);
	str = str.replace(/\[zip\]/g,document.getElementById("txtZip").value);
	str = str.replace(/\[phone\]/g,document.getElementById("txtPhone").value);
	str = str.replace(/\[url\]/g,document.getElementById("txtURL").value);
	str = str.replace(/\[latitude\]/g,lat);
	str = str.replace(/\[longitude\]/g,lon);
	document.getElementById("kmlStr").value = str;	
}


function contentStr()
{
	str = "<xmp>";
	str += '<div itemscope itemtype="http://schema.org/LocalBusiness">\n';
	str += '<h1><span itemprop="name">[name]</span></h1>\n';
	str += '<span itemprop="description">[description]</span>\n';
	str += '<span itemprop="url">[url]</span>\n';
	str += '<div itemprop="address"\n'; 
	str += 'itemscope itemtype="http://schema.org/PostalAddress">\n';
	str += '<span itemprop="streetAddress">[address]</span>\n';
	str += '<span itemprop="addressLocality">[city]</span>, \n';
	str += '<span itemprop="addressRegion">[state]</span>\n';
	str += '<span itemprop="postalCode">[zip]</span>\n';
	str += '</div>\n';
	str += '<div itemprop="geo" itemscope itemtype="http://schema.org/GeoCoordinates">\n';
	str += 'Latitude: [latitude]\n';
	str += 'Longitude: [longitude]\n';
	str += '<meta itemprop="latitude" content="[latitude]" />\n';
	str += '<meta itemprop="longitude" content="[longitude]" />\n'
	str += '</div>\n';
	str += 'Phone: <span itemprop="telephone">[phone]</span>\n';
	str += '</div>\n';
	str += '</xmp>';
	return str;
}

function resetContent()
{
	document.getElementById("btnBuild").disabled = false;
	document.getElementById("btnKML").disabled = true;
	document.getElementById("btnXML").disabled = true;
	document.getElementById("dvOutDiv").innerHTML = contentStr();
	//document.getElementById("dvOutKML").innerHTML = kmlStr();
}

function downloadKML()
{
	var ifrm = document.getElementById("frame1");
	ifrm.src = "http://www.51blocks.com/free-seo-tools1/geo-sitemap-tool/php/download.php?path=location.kml&stream=" +encodeURIComponent(document.getElementById("kmlStr").value.replace(/&/g,"&amp;"));
}

function downloadXML()
{
	var ifrm = document.getElementById("frame2");
	xmlStr = '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" ';
	xmlStr += 'xmlns:geo="http://www.google.com/geo/schemas/sitemap/1.0"> ';
	xmlStr += '<url><loc>http://' + document.getElementById("txtURL").value + '/location.kml</loc> ';
	xmlStr += '<geo:geo><geo:format>kml</geo:format></geo:geo></url></urlset> ';
	ifrm.src = "http://www.51blocks.com/free-seo-tools1/geo-sitemap-tool/php/download.php?path=geo-sitemap.xml&stream=" +encodeURIComponent(xmlStr);
}

function geocodeAddress(address) 
{
	geocoder = new google.maps.Geocoder();
	address = document.getElementById("txtAddress").value + " " + document.getElementById("txtCity").value;
	address += ", " + document.getElementById("selState").options[document.getElementById("selState").selectedIndex].value;
	address += " " + document.getElementById("txtZip").value
	geocoder.geocode( { 'address': address}, 
		function(results, status) 
		{
			if (status == google.maps.GeocoderStatus.OK) 
			{
				latitude = results[0].geometry.location.lat();
				longitude = results[0].geometry.location.lng();
				replaceContent(latitude,longitude);
			}
			else
			{
				alert("Unable to locate address\nUsing 'lat=0, lon=0'");
				latitude = 0;
				longitude = 0;
				replaceContent(latitude,longitude);				
			}
		}
	)
}

				
function kmlStr()
{
	str = '<kml xmlns="http://www.opengis.net/kml/2.2" ';
	str += 'xmlns:atom="http://www.w3.org/2005/Atom">';
	str += '<Document>';
	str += '<name>[name]</name>';
	str += '<atom:author>';
	str += '<atom:name>[name]</atom:name>';
	str += '</atom:author>';
	str += '<atom:link href="http://[url]" rel="related" />';
	str += '<Placemark>';
	str += '<name>[name], [address] [city], [state] [zip]</name>';
	str += '<description>';
	str += '<![CDATA[';
	str += '<address>';
	str += '<a href="http://[url]">[name]</a><br />';
	str += 'Address: [address], [city] [state] [zip] <br />';
	str += 'Phone: [phone]';
	str += '</address>';
	str += '<p>[description]</p>';
	str += ']]>';
	str += '</description>';
	str += '<Point>';
	str += '<coordinates>[longitude],[latitude],0</coordinates>';
	str += '</Point>';
	str += '</Placemark>';
	str += '</Document>';
	str += '</kml>';
	return str;
}

