﻿//*************************************************************************************
// common js functions for the website
// variables
// 24/12/2007
//*************************************************************************************

var menuNum1 = "";
var menuNum2 = "";

var map;
var gdir;
var geocoder = null;
var addressMarker;

var addrpnt ;
var geocoder ;
var address ;
var icon;
var map ;
var allregions = 0;

//*************************************************************************************
// EnsureNumeric 
// check if the entry in numeric only
// 24/12/2007
//*************************************************************************************

function EnsureNumeric(){
    var key = window.event.keyCode; 
    if (key <48 || key >57) 
      
        window.event.returnValue = false;
}

//*************************************************************************************
// CheckBoxListSelect(cbControl, state)
//check all the checkboxes in the checkboxlist
// 24/12/2007
//*************************************************************************************

function CheckBoxListSelect(cbControl, state){    
    var chkBoxList = document.getElementById(cbControl);
    var chkBoxCount= chkBoxList.getElementsByTagName("input");
      /*alert(allregions) commented out by jess crossley on 10/07/08*/
        for(var i=0;i<chkBoxCount.length;i++) 
            {
                if(allregions = "0")
                    {
                        
			chkBoxCount[i].checked = true;
                    }
                else if (allregions = "1")
                    {
                        chkBoxCount[i].checked = false;
                    }
            }

     
	if (allregions= "0")
	{
		allregions = "1";
	}
	else
	{
		allregions = "0";	
	}
 
 return false;
}

//*************************************************************************************
// switchMenu(obj)
//show hides divs
// 24/12/2007
//*************************************************************************************

function switchMenu(obj){
	var el = document.getElementById(obj);
	    if(el.style.display != "block")
			{
				el.style.display = "block";
								
			}
		else 
			{
				el.style.display = "none";
					
			}
}



//*************************************************************************************
// switchMenu(obj)
//show hides divs
// 24/12/2007
//*************************************************************************************

function getQueryVariable(variable)	{
	var query = window.location.search.substring(1);
	var vars = query.split("&");
	
	for (var i=0;i<vars.length;i++)	{
    var pair = vars[i].split("=");
    if (pair[0] == variable)	{
      return pair[1];
		}
	} 
}

//*************************************************************************************
// load(msg,lat,longitude)
//loads the map and the message 
// 24/12/2007
//*************************************************************************************
	
function getMailPageLink(page){
    var maillink = page + "?ID=" + getQueryVariable("ID");
    window.location.href = maillink;
   
}

function getPageLink(page){
    var pagelink = page //+ "?ID=" + getQueryVariable("ID");
    window.location.href = pagelink;
   
}


//*************************************************************************************
// load(msg,lat,longitude)
//loads the map and the message 
// 24/12/2007
//*************************************************************************************


//function loadmap(msg,coords){
//    var myString = coords;
//    var myCOORDS = myString.split(',');

//    load((+myCOORDS[0]),(+myCOORDS[1]));

//}	
	
//*************************************************************************************
// switchMenu(obj)
//show hides divs
// 24/12/2007
//*************************************************************************************

function loadmap(){
    map = new GMap2(document.getElementById("map"));
    var start = new GLatLng(-27.46758,153.027892);
    //var start = new GLatLng(lat,lang);
    map.setCenter(start,12);
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl()) ;
    map.addControl(new GOverviewMapControl()) ;
    geocoder = new GClientGeocoder() ;
    icon = new GIcon();
    icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
    icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    icon.iconSize = new GSize(12, 20);
    icon.shadowSize = new GSize(22, 20);
    icon.iconAnchor = new GPoint(6, 20);
}
    
//*************************************************************************************
// load(msg,lat,longitude)
//loads the map and the message 
// 24/12/2007
//*************************************************************************************

function showAddress(addr){
    loadmap()
    
    address = addr ;
        
    geocoder.getLatLng( address, function(point) {
        if (!point){
            document.getElementById("map").innerHTML = "Address is invalid or not formatted correctly.<br>Please re-enter correct address for the activity.";} 
        else{
        if (addrpnt){
            map.removeOverlay( addrpnt ) ;
            }
    addrpnt = new GMarker(point, {icon:icon, draggable: true,  title: address}) ;
    addrpnt.enableDragging() ;
    map.addOverlay(addrpnt) ;
    
    GEvent.addListener(addrpnt,'dragend',function() {newpoint()}) ;
    map.setCenter(point, 16) ;
    // Our info window content
    var infoTabs = [
    new GInfoWindowTab("Tab #1",'<div style="width:auto;overflow:auto;height:auto;font-weight:bold;font-size:11pt;">' +  address + '</div>')
      ];
      
    // Place a marker in the center of the map and open the info window
        // automatically
     var marker = new GMarker(map.getCenter(point, 16));
   
     //var marker = new GMarker(-27.55370000,152.9599);
    GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowTabsHtml(infoTabs);
        });
    map.addOverlay(marker);

    marker.openInfoWindowTabsHtml(infoTabs);
        
          
    document.getElementById("ctl00_ContentPlaceHolder1_txtCOORDS").value =  addrpnt.getPoint().toUrlValue() ;
    var zoom = map.getZoom() ;
    var escaddr = address.replace(/\s/g, '+') ;
    }
    }) ;
}

//*************************************************************************************
// load(msg,lat,longitude)
//loads the map and the message 
// 24/12/2007
//*************************************************************************************

function newpoint(){
 map.setCenter(addrpnt.getPoint()) ;
 var zoom = map.getZoom() ;
}



//*************************************************************************************
// load(msg,lat,longitude)
//loads the map and the message 
// 24/12/2007
//*************************************************************************************

function load(msg,lat,longitude) {
    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map"));
        gdir = new GDirections(map, document.getElementById("directions"));
        GEvent.addListener(gdir, "load", onGDirectionsLoad);
        GEvent.addListener(gdir, "error", handleErrors);
       
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
   
   		map.setCenter(new GLatLng(lat,longitude),16);

		// Our info window content
        var infoTabs = [
        new GInfoWindowTab("Tab #1",'<div style="font-size:10pt;color:steelblue;">' +  msg + '</div>')
        ];

        // Place a marker in the center of the map and open the info window
        // automatically
        var marker = new GMarker(map.getCenter());
   
        //var marker = new GMarker(-27.55370000,152.9599);
        GEvent.addListener(marker, "click", function() {
          marker.openInfoWindowTabsHtml(infoTabs);
        });
        map.addOverlay(marker);

        marker.openInfoWindowTabsHtml(infoTabs);
    }
}
  
//*************************************************************************************
// setDirections(fromAddress, locale)
//set the directons on google maps
// 24/12/2007
//*************************************************************************************
      
function setDirections(fromAddress, locale) {
  //  gdir.load("from: " + document.getElementById('txtUserAddresss').value + " to: " + fromAddress,
  //  { "locale": locale });
     gdir.load("from: " + "385 Seventeen Mile Rocks Road,Oxley,Brisbane" + " to: " + "722 Brunswick Street,New farm, brisbane",
    { "locale": locale });
}

//*************************************************************************************
// handleerrors()
//handles google maps errors
// 24/12/2007
//*************************************************************************************
	
function handleErrors(){
    if (gdir.getStatus().code == G_GEO_UNKNOWN_ADDRESS)
	    alert("No corresponding geographic location could be found for one of the specified addresses. This may be due to the fact that the address is relatively new, or it may be incorrect.\nError code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_SERVER_ERROR)
	    alert("A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: " + gdir.getStatus().code);
	else if (gdir.getStatus().code == G_GEO_MISSING_QUERY)
	    alert("The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_KEY)
	     alert("The given key is either invalid or does not match the domain for which it was given. \n Error code: " + gdir.getStatus().code);
    else if (gdir.getStatus().code == G_GEO_BAD_REQUEST)
	     alert("A directions request could not be successfully parsed.\n Error code: " + gdir.getStatus().code);
	   else alert("An unknown error occurred.");
}


//*************************************************************************************
// onGDirectionsLoad()
// Use this function to access information about the latest load()
// results.
// 24/12/2007
//*************************************************************************************	

function onGDirectionsLoad(){ 
     

      // e.g.
      // document.getElementById("getStatus").innerHTML = gdir.getStatus().code;
	  
}

//*************************************************************************************
// doHide(cbControl,state)
// hides the panel(none:inline
// results.
// 24/12/2007
//*************************************************************************************	

function doHide(cbControl,state) {
        document.all(cbControl).style.display = state;
}
  
//*************************************************************************************
// init()
// creates a fade effect
// results.
// 24/12/2007
//*************************************************************************************	

function init() {

   blendImages('searchImages', 150, 200);
}

window.onload = init;  

//*************************************************************************************
// hideErrors(cbControl,state)
// hides the js errors
// results.
// 24/12/2007
//*************************************************************************************	
function no_error()	{ 
		return true; 
}
window.onerror=no_error;

function toggleBoth(id,imgElm,id2){
    var d = document;	
    
	if(imgElm.src == 'http://www.arts.qld.gov.au/in2arts/_resources/_images/miscellaneous/plus.gif'){
		imgElm.src = 'http://www.arts.qld.gov.au/in2arts/_resources/_images/miscellaneous/minus.gif';
		d.getElementById(id).style.display = 'block';
		d.getElementById(id2).innerHTML = 'Hide Map';
	}else{
		imgElm.src = 'http://www.arts.qld.gov.au/in2arts/_resources/_images/miscellaneous/plus.gif';
		d.getElementById(id).style.display = 'none';
		d.getElementById(id2).innerHTML = 'Show Map';
	}
}