
//######################################################
//############################################################ IFRAME NONSENSE
//#################################################

var IFrameObj; // our IFrame object
function getData() {
  if (!document.createElement) {return true};
  var IFrameDoc;
  var URL = '<?php echo($labbase);?>'
//   =' + document.viewform.timespan.value;
  if (!IFrameObj && document.createElement) {
    // create the IFrame and assign a reference to the
    // object to our global variable IFrameObj.
    // this will only happen the first time 
    // callToServer() is called
   try {
      var tempIFrame=document.createElement('iframe');
      tempIFrame.setAttribute('id','RSIFrame');
      tempIFrame.style.border='0px';
      tempIFrame.style.width='0px';
      tempIFrame.style.height='0px';
      IFrameObj = document.body.appendChild(tempIFrame);
      
      if (document.frames) {
        // this is for IE5 Mac, because it will only
        // allow access to the document object
        // of the IFrame if we access it through
        // the document.frames array
        IFrameObj = document.frames['RSIFrame'];
      }
    } catch(exception) {
      // This is for IE5 PC, which does not allow dynamic creation
      // and manipulation of an iframe object. Instead, we'll fake
      // it up by creating our own objects.
      iframeHTML='\<iframe id="RSIFrame" style="';
      iframeHTML+='border:0px;';
      iframeHTML+='width:0px;';
      iframeHTML+='height:0px;';
      iframeHTML+='"><\/iframe>';
      document.body.innerHTML+=iframeHTML;
      IFrameObj = new Object();
      IFrameObj.document = new Object();
      IFrameObj.document.location = new Object();
      IFrameObj.document.location.iframe = document.getElementById('RSIFrame');
      IFrameObj.document.location.replace = function(location) {
        this.iframe.src = location;
      }
    }
  }
  
  if (navigator.userAgent.indexOf('Gecko') !=-1 && !IFrameObj.contentDocument) {
    // we have to give NS6 a fraction of a second
    // to recognize the new IFrame
    setTimeout('getData()',10);
    return false;
  }
  
  if (IFrameObj.contentDocument) {
    // For NS6
    IFrameDoc = IFrameObj.contentDocument; 
  } else if (IFrameObj.contentWindow) {
    // For IE5.5 and IE6
    IFrameDoc = IFrameObj.contentWindow.document;
  } else if (IFrameObj.document) {
    // For IE5
    IFrameDoc = IFrameObj.document;
  } else {
    return true;
  }
      
  IFrameDoc.location.replace(URL);
  return false;
}


function doClear(theText) {
    if (theText.value == theText.defaultValue) {
        theText.value = ""
    }
}

function doAdd(theText) {
	if (theText.value == theText.defaultValue) {
		theText.value = "notes"
		}
}

function gotoForm() {
    document.getElementById('formDiv').style.display = 'block';
    document.getElementById('textDiv').style.display = 'none';
	
    //AddMarkerListener = 
    AddMarkerListener = GEvent.addListener(map, 'click', 
        function(overlay,point){
            if(overlay) {
            	if (typeof submarker != "undefined") {
            		map.removeOverlay(submarker);
            		document.dataform.latitude.value = "";
        			document.dataform.longitude.value = "";
            		};
        		}
            else if(point) {
                document.dataform.latitude.value = point.y;
                document.dataform.longitude.value = point.x;
                if (typeof submarker != "undefined") {
                	map.removeOverlay(submarker)
                };
                submarker = new GMarker(point);
                map.addOverlay(submarker);
            }
        });

}

function gotoView() {
    document.getElementById('formDiv').style.display = 'none';
    document.getElementById('textDiv').style.display = 'block';

    if (typeof AddMarkerListener != "undefined") {
        GEvent.removeListener(AddMarkerListener);
    };
}

function gotoAbout() {
    document.getElementById('formDiv').style.display = 'none';
    document.getElementById('textDiv').style.display = 'none';
    
    if (typeof AddMarkerListener != "undefined") {
        GEvent.removeListener(AddMarkerListener);
    };
}

// Get the union of n arrays
Array.prototype.union =
  function() {
    var a = [].concat(this);
    var l = arguments.length;
    for(var i=0; i<l; i++) {
      a = a.concat(arguments[i]);
    }
    return a.unique(a);
  };

// Return new array of intersection of n groups

Array.prototype.intersect=function() {
	if(!arguments.length) return[];
	var a1=this;
	var a=a2=null;
	var n=0;
	while( n<arguments.length) { 
		a=[];
		a2=arguments[n];
		var l=a1.length;
		var l2=a2.length;
			for(var i=0;i<l;i++) { 
				for(var j=0;j<l2;j++){
					if(a1[i]===a2[j]) a.push(a1[i]);
				}
			}
		a1=a;
		n++;
	}
	return a.unique();
}

// Return new array with duplicate values removed
Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };

Array.find = function(ary, element) {
    for (var i = 0; i < ary.length; i++) {
        if (ary[i] == element) {
            return i;
        }
    }
    return - 1;
}

//
//add a function to get a list of all the tables
//
function checkAll(field) {
    for (var i = 0; i < allboxes.length; i++) {
//     alert(allboxes[i]);
        document.viewform.elements[allboxes[i]].checked++;

    }
}

function uncheckAll(field) {
    for (var i = 0; i < allboxes.length; i++) {
//     alert(allboxes[i]);
        document.viewform.elements[allboxes[i]].checked--;

    }
}

function include(filename)
{
	var head = document.getElementsByTagName('head')[0];
	
	script = document.createElement('script');
	script.src = filename;
	script.type = 'text/javascript';
	
	head.appendChild(script)
}
