 /*
	Libraries	 
 */

/*************************************************************************
 Google Map Functions

 To show a Google Map, create an empty div.  Either give the div an ID of
 'gmap' or pass the ID to the gmapInit method.  The body tag of your page
 should look like the following:
	<body onload="gmapInit(?, ?)" onunload="GUnload()" >
*************************************************************************/ 
var map;

/* Pass address in a format similar to:
	'123 Main St, City ST, 01234' */
function gmapInit(address, mapDivId) {
	if (GBrowserIsCompatible()) {
		if (!mapDivId) {
			var mapDivId = "gmap";
		}
		map = new GMap2(document.getElementById(mapDivId));
		map.addControl(new GSmallMapControl());

		if (!address) 
			var address = "5 Upland Road, Cambridge MA, 02140";

		createAddressMarker(address);
	}
}

/* Opens a popup window on the map with the address passed. */
function createAddressMarker(address){
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(address, function(point) {
		if (!point)
			alert(address + " not found.");
		else {			
			map.setCenter(point, 15);
			createMarker(point, address);
		}
	});
}

function createMarker(point, address) {
    var marker = new GMarker(point);
    var html = '<div style="padding: 8px;" ><strong>Address:</strong><br>' + formatAddress(address) + '<br><a href="javascript:getDirections(\'' + address + '\')">Get Directions</a></div>';
	map.addOverlay(marker);
	map.openInfoWindowHtml(map.getCenter(), html);
}

function formatAddress (address) {
	return address.replace(",", "<br>");
}

function getDirections (address) {
	window.open("http://maps.google.com/maps?f=q&hl=en&geocode=&time=&date=&ttype=&q="+escape(address));
}

/*************************************************************************
 Image Popup function
*************************************************************************/
function openImgPopup (name, ext) {
	if (name != "") {
		if (ext == "") {
			ext = "gif";
		}
		window.open("/imgpopup/name/" + escape(name) + "/ext/" + escape(ext), "Synthesis", "status=0,toolbar=0,location=0,menubar=0,directories=0,width=400,height=400");
	}
}

function popupResize (width, height) {
	if (width && height) {
		offsetWidth = 35 + 2;  // padding + border
		offsetHeight = 62 + 2 + 16 + 120;  // logo height + border + 'Close' text height + ?
		if (navigator.appVersion.match("MSIE"))  // if IE, add 20px
			offsetHeight += 20;
		window.resizeTo(width + offsetWidth, height + offsetHeight);
	}
}

/*************************************************************************
 Picture Viewer functions
*************************************************************************/ 
var showing = 1;

function imgShowHide (imgId) {
	var newShowing;
	var imgToShow;

	if (imgId == showing.toString()) { // if img showing == img to show
		imgToShow = null;
	}
	else if (imgId == 'next') {  // if next link clicked
		newShowing = showing+1;
		imgToShow = document.getElementById(newShowing.toString());
	}
	else if (imgId == 'prev') {  // if prev link clicked
		newShowing = showing-1;
		imgToShow = document.getElementById(newShowing.toString());
	}
	else {  // if a numbered link is clicked
		newShowing = parseInt(imgId);
		imgToShow = document.getElementById(imgId);
	}
	
	if (imgToShow) {
		imgToShow.style.display = 'block';
		imgToHide = document.getElementById(showing.toString());
		imgToHide.style.display = 'none';
		showing = newShowing;
	}
}


/*************************************************************************
 radio button easies.
*************************************************************************/ 
 function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function
 
 
/*************************************************************************
	String Manipulation
*************************************************************************/	
 
	// only allow $1,234
	function isNumeric(string) {
		var reg = /[^0-9$,.-]/i;
		if(reg.exec(string)) return false;
		return true;
	}

	function numbersOnly(string) {
		return [string.replace(/[^0-9.]/gi, "")].join("");
	}	 
	 
	function trim(str) {
		if(str)
			return str.replace(/^\s*|\s*$/g,"");
		else
			return "";
	}

 /*************************************************************************
	Generic Form Validator
 *************************************************************************/
 
	function validateforms(form){	
		for(i = 0; i < form.elements.length; i++){
			// first check required elements as filled in
			if (trim(form.elements[i].value) == "" && form.elements[i].getAttribute("required") == "true") {
				alert("Please enter a value in the "+ form.elements[i].getAttribute("description") +" field.");
				form.elements[i].focus();
				return false ;
			}		
			
			//Check numeric fields expected	
			if (form.elements[i].getAttribute("numeric") == "true") {
				if(form.elements[i].value != "" && !isNumeric(form.elements[i].value)){
					alert("Please enter a numeric value in the "+ form.elements[i].getAttribute("description") +" field.");
					form.elements[i].focus();
					return false ;
				}
				else if(form.elements[i].value != "" && isNumeric(form.elements[i].value)){
					form.elements[i].value = numbersOnly(form.elements[i].value);				
				}
			}
			
			/* Check Email */
			if (form.elements[i].getAttribute("email") == "true") {
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (!filter.test(form.elements[i].value)){
					alert("The value in the '" +form.elements[i].getAttribute("description") +"' field is not a valid email address.");
					form.elements[i].focus();
					return false;
				}			
			}
			
			/* Check Date
			if (form.elements[i].getAttribute("date") == "true") {
				selecteddate = new Date(form.elements[i].value);
				mynow = new Date();		
				alert(selecteddate.getTime());
				alert(mynow.getTime());	
				if(selecteddate.getTime() <= mynow.getTime()){
					alert("Please enter a date not already passed for the date field.");
					form.elements[i].focus();
					return false ;
				}
			}
			*/
			//		 then check other things				    
		}
		return true ;  
	}

 /*************************************************************************
	Alerts, Messageboxes, Confirms
 *************************************************************************/ 
   
	function deleteconfirm(myform){
		return confirm('Are you sure you wish to delete this record? Delete actions cannot be undone.');
	}

	function cancelAction(){
		history.go(-1);
	}	
	
	function coverSetVisible(obj) {
		var divRef = document.getElementById(obj);
		var ifrRef = document.getElementById(obj+'cover');
	 
		ifrRef.style.display = "block";
		ifrRef.style.width = divRef.offsetWidth;
		ifrRef.style.height = divRef.offsetHeight;
		ifrRef.style.top = divRef.style.top;
		ifrRef.style.left = divRef.style.left;
		ifrRef.style.zIndex = divRef.style.zIndex - 1;
		//alert(IfrRef.style.zIndex);
	}


	 // Selected Tab
	 var tabSelected = "";
	 
	 var dom = document.getElementById;
	 var iex = document.all;
	 var ns4 = document.layers;

	function getElement(name, nest) {
		nest = nest ? 'document.'+nest+'.' : '';
		var el = dom ? document.getElementById(name) : iex ? document.all[name] : ns4 ? eval(nest+'document.'+name) : false;
		el.css = ns4 ? el : el.style;
		return el;
	}

/*************************************************************************
	Form Change Checking (to prevent users from navigating away from a changed form)
*************************************************************************/
		function jsxge(jsxe,jsxn) {
			jsxn = jsxn ? 'document.'+jsxn+'.' : '';
			return document.getElementById ? document.getElementById(jsxe) : document.all ? document.all[jsxe] : document.layers ? eval(jsxn+'document.'+jsxe) : false;
			}
		
		// check for change
		function nav(page) {
			var changelog = jsxge("changelog");
			var string = changelog.value;
			if (string != "" && string) {
				var input = confirm("You have made changes to the following items:\n\n"+string+"\n\nIf you navigate away from this page without saving these changes will be lost.");
			} else input = true;
			if (input) 
				{ return true; } 
			else return false;
		}
		
		//update change log
		function doChange(el) {
			var changelog = jsxge("changelog");
			if (changelog.value != "") changelog.value += "\n";
			changelog.value += this.getAttribute("description");			
		}
		
		function changecheck(form){		
			//Mark all items on the page with an onchange element
			for(i = 0; i < form.elements.length; i++){
				form.elements[i].onchange = doChange;			
			}		
			
			//Then mark all items in the adminColumn with a function to prompt confirm
			var x = document.getElementById('adminColumn');
			if (!x) return;
			var y = x.getElementsByTagName('a');
			for (var i=0;i<y.length;i++){			
				/*if(ns4)
					y[i].addEventListener("onclick", nav, false);
				else if(iex)
					y[i].attachEvent("onclick", nav);				
					*/
				y[i].onclick = nav;
			}
		}

 
/*************************************************************************
	Event Model Normalizers (IE vs DOM)
*************************************************************************/
 
/*
function addEvent( obj, type, fn, useCapture )
{
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent)
	{
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn, useCapture )
{
	if (obj.removeEventListener)
		obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent)
	{
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}
*/
