/***********************************************************************************
Calendar functions
***************************/
//This is now in _header.php because of PHP functions
	
/***********************************************************************************
Subpage hide/show functions
***************************/

function moreinfo(count){
	var status = document.getElementById("moreinfo_"+count).style.display;
	if (status == "none"){
		document.getElementById("moreinfo_"+count).style.display = "block";
	} else {
		document.getElementById("moreinfo_"+count).style.display = "none";
	}
}
google.maps.event.addDomListener(window, 'load', init_map);
var nmap;
var nmarker;
var mapCenter;
var circle;
var geocoder;
var bounds;
var infoWindow;
function init_map()
{
	
	mapCenter = new google.maps.LatLng(38, -97);
	nmap = new google.maps.Map(document.getElementById('resultsmap'), {
          'zoom': 10,
          'center': mapCenter,
          'mapTypeId': google.maps.MapTypeId.ROADMAP
        });
		
    //circle = new google.maps.Circle({
      //    map: nmap,
        //  radius: 1610
        //});
	document.getElementById("vmap").style.display='block';

    //circle.bindTo('center', nmarker, 'position');	
}
var pinColor1;
var pinColor;
var pinImage;
var pinImage1;
var pinShadow;
function showmap()
{
  	geocoder = new google.maps.Geocoder();
	bounds = new google.maps.LatLngBounds();
	infoWindow = new google.maps.InfoWindow();
	pinColor = "FE7569";
	pinColor1 = "1E7569";
    pinImage = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + pinColor,
        new google.maps.Size(21, 34),
        new google.maps.Point(0,0),
        new google.maps.Point(10, 34));
	pinImage1 = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=%E2%80%A2|" + pinColor1,
        new google.maps.Size(21, 34),
        new google.maps.Point(0,0),
        new google.maps.Point(10, 34));
    var pinShadow = new google.maps.MarkerImage("http://chart.apis.google.com/chart?chst=d_map_pin_shadow",
        new google.maps.Size(40, 37),
        new google.maps.Point(0, 0),
        new google.maps.Point(12, 35));
    rec_showmap(0);
}
function rec_showmap(i){

    if(i<=num_lots)
	{   
	    var address = lot_addr[i];
        geocoder.geocode( { 'address': address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
             var marker1 = new google.maps.Marker({
                  map: nmap,
				  draggable: false,
                  position: results[0].geometry.location,
				  title: lot_name[i],
				  shadow: pinShadow,
				  icon: ((i==0)? pinImage:pinImage1)
              });
			  google.maps.event.addListener(marker1, 'click', (function(markerArg,
					index) {
					         return function() {
							  infoWindow.setContent('<div class="iwContainer">' + lot_name[index] +'   <br>' + lot_addr[i]+'   <br>' + lot_phone[i]+'</div>' );
							  infoWindow.open(nmap, markerArg);
							  };
							})(marker1, i));
							
	          if(i==num_lots)
			  {
			      nmap.setCenter(new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng() ));
               	  document.getElementById("resultsmap").style.display='block';
	              google.maps.event.trigger(nmap, "resize");
			  }
			  
			  bounds.extend(results[0].geometry.location);
			  nmap.fitBounds(bounds);
        } else {
                 alert("Geocode was not successful for the following reason: " + status);
        }
      });
	  setTimeout(function(){rec_showmap(i+1);},0);  
	}
}

/***********************************************************************************
Sneaky Email Script
***************************/
function codify (thisguy) {
	str = "";
	for (i=0; i<thisguy.length; i++) {
		str += "&#" + thisguy.charCodeAt(i) + ";";
	}	
	return str;
}
function buildaddress (one, two, three) {
	str = "";
	str += codify(three);
	str += "&#" + "064" + ";";
	str += codify(two);
	str += "&#" + "046" + ";";
	str += codify(one);
	return str;
}
// Our most popular function -- creates mailto link with email address as link text
function sneaky (one, two, three, four) {
	str = buildaddress(one, two, three);
	if (four != "") four = "class=" + four + " ";
	document.write("<a " + four + "hre" + "f=mai" + "lto:" + str +">" + str + "</" + "a>");
}
// Creates mailto link with fourth parameter as the link text
function sneaky2 (one, two, three, four, five) {
	str = buildaddress(one, two, three);
	if (four != "") five = "class=" + four + " ";
	document.write("<a " + five + "hre" + "f=mai" + "lto:" + str +">" + four + "</" + "a>");
}
// Simply prints the email address (great for forms!)
function sneaky3 (one, two, three) {
	document.write(buildaddress(one, two, three));
}

/***********************************************************************************
Form Validaator
***************************/
function isMailAddress(m) {
   if ((m == '') || (m == ' ')) { return false; }

    var a = 1;
    var ml = m.length;

    while ((ml>a) && (m.charAt(a) != "@")) { a++ }
    if ((a >= ml) || (m.charAt(a) != "@")) {
	return false;
    } else {
	a = a + 2;
    }
    while ((ml > a) && (m.charAt(a) != ".")) { a++ }
    if ((a >= ml - 1) || (m.charAt(a) != ".")) {
	return false;
    } else {
    return true;
    }
}
// For English Contact Form
function validate(obj) {
	var valid = true;
	var msg = "";
	if (obj.elements["name"].value == "") {
		msg += "Please enter your name.\n";
		valid = false;
		obj.elements["name"].focus();
	}
		// Check for white space
		reWhiteSpace = new RegExp(/^\s+$/);
		if (reWhiteSpace.test(obj.elements["name"].value)) {
			msg += "Please enter your name.\n";
			valid = false;
			obj.elements["name"].focus();
		}
	if (!isMailAddress(obj.elements["email"].value)) {
		msg += "Please enter a valid email address.\n";
		if (valid) {
			valid = false;
		    obj.elements["email"].focus();
		}
	}
	if (!valid) alert(msg);
	return valid;
}
