
function validateEmail(str) {
  emails = str.split(',');
  result = true;

  var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
  for (i=0;i<emails.length;i++)
		  {

   if(reg.test(emails[i]) == false) {
	  result =  false;
   } 
   }

   return result;

}

function validate() {

	response = "";


	if (!validateEmail($('friendsEmail').value)) {
		response += "Please enter your friend's valid email.\n";
	}


	if (!validateEmail($('yourEmail').value)) {
		response += "Please enter your valid email.\n";
	}

	if ($("yourName").value.length==0) {
		response += "Please enter your name.\n";
	}

	if (response.length > 0 ) {
		alert(response);
		return false;
	} else {

		document.forms["emailFriend"].submit();
		return true;
	}
}

function updateForm() {

	months = $('birthdayMonth');
	days = $('birthdayDay');
	years = $('birthdayYear');
	hours = $('birthdayHour');

	$('birthdayDST').checked = SNWEBVIEW.situation.currentlyExperiencingDST();
	months.selectedIndex = SNWEBVIEW.situation.dateTime.getMonth();
	days.selectedIndex = SNWEBVIEW.situation.dateTime.getDate()-1;

	//check for microsoft screwie year
	y = SNWEBVIEW.situation.dateTime.getFullYear();
	if (y > 1000) {
		y = y - 1900;
	}
	years.selectedIndex = y;

	h = SNWEBVIEW.situation.dateTime.getHours();
	if (h>12) {
		h = h - 12;
		$('birthdayAMPM').selectedIndex = 1;
	}
	if (h == 0) {
		h = 12
		$('birthdayAMPM').selectedIndex = 0;
	}
	hours.selectedIndex = h-1;

}

function attachEventHandlers()
{
	var i = 0;
	var dateTimeControls = SNWEB.utils.getElementsByClassName('dateTimeControl');
	for (i = 0; i < dateTimeControls.length; i += 1)
	{
		Event.observe(dateTimeControls[i], 'change', onDateChange);
	}
	
	Event.observe($('birthdayDST'), 'click', onDSTChange);
	Event.observe($('birthdayHideDaylight'), 'click', onHideDaylightChange);
	
	Event.observe($('mapButton'), 'click', showMap);
	Event.observe($('mapClose'), 'click', hideMap);
	
	Event.observe($('submitButton'), 'click', validate);
}

var mapWidth = "500px";
var mapHeight = "400px";
var map, myPoint;
var listenToMove;
var myLocation;
var icon;
window.onload = function ()
{
	attachEventHandlers();

	// change the kerver URL to the proxy before attempting to use anything.
	SNWEB.consts.kServerCGI = "snserverproxy.php?";
	
	var mySituation = new SNWEB.SNSituation();
	
	mySituation.options.constImages = true;
	mySituation.options.constStickFigs = false;

	// this call searches for any DIV with the class "snSkyView" and injects the necessary HTML into it
	SNWEBVIEW.init({platform: SNWEB.consts.kGoogleGadget,
					situation: mySituation,
					watermark: { opacity: 0.8, url: 'http://www.starrynighteducation.com' },	
					version: "1.0"});	

	updateForm();
}

function changeDaysInMonth(month, year)
{
	var daysInMonth = SNWEB.utils.getDaysInMonth(year, month);
	var days = $('birthdayDay');
	days.options.length = 0;
	for(i=0; i < daysInMonth; i++) {
		days.options[i] = new Option(i+1,i+1);
	}
}

function onDateChange(inEvt)
{
	// event handling boilerplate
	inEvt = inEvt || window.event;
	var target = inEvt.target || inEvt.srcElement;	
	inEvt.cancelBubble = true;
	if (inEvt.stopPropogation) {
		inEvt.stopPropogation();
	}

	var key = target.id;
	var value = target.value;

	switch (key) {
		case "birthdayMonth":
			SNWEBVIEW.situation.dateTime.setMonth(value);
			changeDaysInMonth(SNWEBVIEW.situation.dateTime.getMonth(),SNWEBVIEW.situation.dateTime.getYear());
			break
		case "birthdayDay":
			SNWEBVIEW.situation.dateTime.setDate(value);
			break
		case "birthdayYear":
			SNWEBVIEW.situation.dateTime.setFullYear(value);
			changeDaysInMonth(SNWEBVIEW.situation.dateTime.getMonth(),SNWEBVIEW.situation.dateTime.getYear());
			break
		case "birthdayHour":
			h = parseInt(value);
			if ($('birthdayAMPM').value == "PM") {
				h=h+12;
			}
			SNWEBVIEW.situation.dateTime.setHours(h);
			break
		case "birthdayMinute":
			SNWEBVIEW.situation.dateTime.setMinutes(value);
			break
		case "birthdayAMPM":
			if (value = "PM") {
				//SNWEBVIEW.situation.dateTime.setHours(value);
				SNWEBVIEW.situation.dateTime.setHours(SNWEBVIEW.situation.dateTime.getHours()+12);
			}
			break

	}

	updateForm();
	SNWEBVIEW.refreshImage();
	setBirthdayText();
}

function setBirthdayText() {
	birthday = SNWEBVIEW.situation.dateTime;
	$('BirthdayText').innerHTML 
	trad = ASTROSIGN.getTraditionalSign(birthday);
	real = ASTROSIGN.getRealSign(birthday);
	txt = ""+
	"You may have thought your sign is "+
	trad+", but your true sign is "+ real+".";
	if (trad == real) {
	txt = ""+
	"Earth may have shifted, but your sign is still "+real+".";
	}
	//alert(birthday);
	
	$('BirthdayText').innerHTML = txt; 
}

function onDSTChange() {
	SNWEBVIEW.situation.location.useDST = $('birthdayDST').checked;
	SNWEBVIEW.refreshImage();
}


function onHideDaylightChange() {
	SNWEBVIEW.situation.options.daylight = !$('birthdayHideDaylight').checked;
	SNWEBVIEW.refreshImage();
}

  function mapload() {
/*
	if (GBrowserIsCompatible()) {
	  map = new GMap2($("mapContainer"));

	  myPoint = new GLatLng(SNWEBVIEW.situation.location.lat.toString().substring(0,6),SNWEBVIEW.situation.location.lng.toString().substring(0,6));

	  //myPoint = new GLatLng(-129,53);
	  map.disableDoubleClickZoom();
	  map.setCenter(myPoint,4);
	  //map.addControl(new GSmallMapControl());
	  //map.addControl(new GMapTypeControl());

	  icon = new GIcon();
	  icon.image = "img/LocationBubble_small.gif";
//      icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
	  icon.iconSize = new GSize(32, 43);
	  icon.shadowSize = new GSize(22, 20);
	  icon.iconAnchor = new GPoint(0, 43);
//      icon.infoWindowAnchor = new GPoint(5, 1);
	}
*/
    if (GBrowserIsCompatible()) {
      map = new GMap2($("mapContainer"));
      
      myPoint = new GLatLng(43.67,79.4);
      
      map.setCenter(myPoint, 8);
      map.addControl(new GSmallMapControl());
      map.addControl(new GMapTypeControl());
      
      icon = new GIcon();
      icon.image = "img/LocationBubble_small.gif";
//      icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
      icon.iconSize = new GSize(32, 43);
      icon.shadowSize = new GSize(22, 20);
      icon.iconAnchor = new GPoint(0, 43);
//      icon.infoWindowAnchor = new GPoint(5, 1); 

	  GEvent.addListener(map,'dblclick', function(overlay,clickCoord) {
	  										addMarker(clickCoord);
	  										setFormLatLon( clickCoord.lat(), clickCoord.lng() )
	  									});
    }
  }

	//showMap() forces the map div to display
	function showMap() {
		var mapButton = $("mapButton");
		var mapWindow = $('mapWindow');
		var mapContainer = $('mapContainer');
		var appContainer = $('appContainer');
		var mapCenter;

		//this is used to fix the IE bug which shows select boxes right through
		// any DIV
		var mapWindowBack = $('mapWindowBack');

		mapContainer.style.height = mapHeight;
		mapContainer.style.width = mapWidth;


		//mapWindow.style.top = findPosY(mapButton) - findPosY(appContainer) - 5 + 1 + mapButton.height + "px";
	//	mapWindow.style.left = findPosX(mapButton) - findPosX(appContainer) + 1 + "px";
		//mapWindow.style.top = "450px";
		//mapWindow.style.left = "500px";
		mapWindow.style.top = "450px";
		mapWindow.style.left = "500px";

		mapWindow.style.display = 'block';
		mapWindow.style.position = 'absolute';

		mapWindowBack.style.height = mapHeight;
		mapWindowBack.style.width = mapWidth;
		mapWindowBack.style.top = mapWindow.style.top;
		mapWindowBack.style.left = mapWindow.style.left;
		mapWindowBack.style.display = "inline";


		if (!map)
		{
			mapload();
		}

		try
		{
			//alert(SNWEBVIEW.situation.location.lat);
			mapCenter = new GLatLng(SNWEBVIEW.situation.location.lat, SNWEBVIEW.situation.location.lng);
			//mapCenter = new GLatLng(-90,38);

			addMarker(mapCenter);
			//alert(mapCenter);
			map.setCenter(mapCenter,4);
//			map.panTo(mapCenter);
			//alert(map.getCenter+","+mapCenter+"-b");
		}
		catch (err)
		{
			alert (err.message);
		}

		showdiv("mapWindow");

	}

	//hideMap() hides the displayed map
	function hideMap() {
		var mapContainer = $("mapContainer");
		var mapWindowBack = $('mapWindowBack');
//		mapContainer.style.height = "0px";
//		mapContainer.style.width = "0px";

		mapWindowBack.style.display = "none";
		hidediv("mapWindow");
	}

	//findPosX() finds X position of an object on the page (in pixels and from the left)
	function findPosX(obj)
	{
		var curleft = 0;

		if ( obj.offsetParent )
			while(1)
			{
				curleft += obj.offsetLeft;
				if (!obj.offsetParent)
					break;
				obj = obj.offsetParent;
			}
		else if ( obj.x )
			curleft += obj.x;

		return curleft;
	}

	//findPosY() finds Y position of an object on the page (in pixels and from the top)
	function findPosY(obj)
	{
		var curtop = 0;
		if ( obj.offsetParent )
			while(1)
			{
				curtop += obj.offsetTop;
				if ( !obj.offsetParent )
					break;
				obj = obj.offsetParent;
			}
		else if ( obj.y )
			curtop += obj.y;

		return curtop;
	}

	function setOverflow(on)
	{
		if (on)
		{
			$('viewer1').style.overflow = 'visible';
			$('viewer2').style.overflow = 'visible';
		}
		else
		{
			$('viewer1').style.overflow = 'hidden';
			$('viewer2').style.overflow = 'hidden';
		}
	}


	function showdiv(objname) {
		if ($) { // DOM3 = IE5, NS6
			$(objname).style.visibility = 'visible';
		}
		else {
			if (document.layers) { // Netscape 4
				document.hideshow.visibility = 'visible';
			}
			else { // IE 4
				document.all.hideshow.style.visibility = 'visible';
			}
		}
	}

	function hidediv(objname) {
		if ($) { // DOM3 = IE5, NS6
			$(objname).style.visibility = 'hidden';
		}
		else {
			if (document.layers) { // Netscape 4
				document.hideshow.visibility = 'hidden';
			}
			else { // IE 4
				document.all.hideshow.style.visibility = 'hidden';
			}
		}
	}


	//getLat() calculates the latitude selected in the form
	function getLat()
	{

	   // var myLat;

		if (isNaN(document.snWebForm.LatD.value))
		{
			//throw new Error ("Degrees of latitude must be a number.")
		}

		if (isNaN(document.snWebForm.LatM.value))
		{
			//throw new Error ("Minutes of latitude must be a number.")
		}

		//myLat = parseInt(document.snWebForm.LatD.value) + (document.snWebForm.LatM.value/60);

		//if (document.snWebForm.LatRG[1].selected == true)
			//myLat = -1*myLat;

		return SNWEBVIEW.situation.location.lat;
	}

	//getLon() calculates the longitude selected in the form
	function getLon()
	{
	/*
		var myLon;

		if (isNaN(document.snWebForm.LonD.value))
		{
			throw new Error ("Degrees of longitude must be a number.")
		}

		if (isNaN(document.snWebForm.LonM.value))
		{
			throw new Error ("Minutes of longitude must be a number.")
		}

		myLon = parseInt(document.snWebForm.LonD.value) + (document.snWebForm.LonM.value/60);

		if (document.snWebForm.LonRG[0].selected == true)
			myLon = -1*myLon;
			*/
		return SNWEBVIEW.situation.location.lng;
	}


	function setFormLatLon(myLat, myLon)
	{
	/*
		document.snWebForm.LatD.value = Math.floor(Math.abs(myLat));
		document.snWebForm.LatM.value = (Math.abs(myLat) - document.snWebForm.LatD.value)*60;

		if (myLat <= 0 )
			document.snWebForm.LatRG[1].selected = true;
		else
			document.snWebForm.LatRG[0].selected = true;

		document.snWebForm.LonD.value = Math.floor(Math.abs(myLon));
		document.snWebForm.LonM.value = (Math.abs(myLon) - document.snWebForm.LonD.value)*60;

		if (myLon <= 0 )
			document.snWebForm.LonRG[0].selected = true;
		else
			document.snWebForm.LonRG[1].selected = true;
	*/
		//$('LonD').value = myLon;
		//$('LatD').value = myLat;

		SNWEBVIEW.situation.location.lat = myLat;
		SNWEBVIEW.situation.location.lng = myLon;
		SNWEBVIEW.refreshImage();
	}



	function addMarker(point)
	{
		if (myLocation)
		{
			map.removeOverlay(myLocation);
		}
		myLocation = new GMarker(point,icon);
		map.addOverlay(myLocation);

	}
