// ====================================================================
// ====================================================================
// ======== helper functions =================
// == parse out the query variables ===
// starting from Pete Freitag, pete@cfdev.com
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];
    }
  } 
  return "NULL";
}


// == get the new, changed URL (for use in sidebar, for humans to click on, for this program ==
function changeUrlForElement(anElement, aLat, aLong, aZoom, aColumn, aNormalizer, aMin, aMax, aTitle)
{
  aTitle = aTitle.replace(/ /g, "+");	
  aTitle = aTitle.replace(/\//g, "");	
  aTitle = aTitle.replace(/\\/g, "");	
  aTitle = aTitle.replace(/\\/g, "");	
  aTitle = aTitle.replace(/\!/g, "");	
  aTitle = aTitle.replace(/\?/g, "");	
  // Note that this needs to use NEW v2 zoom level
  anElement.href = "./CensusOverlays.html?column="+aColumn+"&normalizer="+aNormalizer+"&title="+aTitle+"&min="+aMin+"&max="+aMax+"&long="+aLong+"&lat="+aLat+"&zoom="+aZoom;
}


// ---------------------
function validateQueryLatLong(defaultLat, defaultLong)
{
  var defaultStartPoint = new GLatLng(defaultLat, defaultLong);
  var latitude = parseFloat(getQueryVariable("lat"));
  var longitude = parseFloat(getQueryVariable("long"));

  if(isNaN(longitude) || (longitude < -180) || (longitude > 360) || (isNaN(latitude) || (latitude < -180) || (latitude > 90)))
  {
    return defaultStartPoint;
  }

  return new GLatLng(latitude, longitude); 
}


// ---------------------
function validateQueryInt(key, min, max, defaultValue)
{
  var value = parseInt(getQueryVariable(key));
  if(isNaN(value) || value > max || value < min)
  {
    return defaultValue;
  }
  return value;
}


// ---------------------
function validateQueryFloat(key, min, max, defaultValue)
{
  var value = parseFloat(getQueryVariable(key));
  if(isNaN(value) || value > max || value < min)
  {
    return defaultValue;
  }
  return value;
}


// ---------------------
// Normalizer specifies how the data in column should be normalized:
//	1	no normalization
//	A	normalize by census tract area
//	P	normalize by total population
//	H	normalize by number of households
//	U	normalize by number of housing units
//	F	normalize by number of families
//	R	random color
function validateQueryNormalizer()
{
  var normalizer = getQueryVariable("normalizer").charAt(0);
  var validNormalizers = "1APHFRU";
  if(("NULL" == normalizer) || (-1 == validNormalizers.indexOf(normalizer)))
  {
	if(4 == column)
      return 'A'
	
	return 'P';	// TODO be slightly more clever about which to use
  }
  return normalizer;
}


function validateQueryMax(normalizer)
{
  var max = parseFloat(getQueryVariable("max"));
  if(isNaN(max))
  {
    switch(normalizer)
    {
      case 'P':
         max = 1;
         break;
      case 'A':
         max = 10000;
         break;
      case '1':
         max = 3000;
         break;
      case 'U':
         max = 1000;
      case 'H':
         max = 1000;
         break;
      case 'F':
         max = 1000;
         break;
      case 'R':	// random
         max = 1;
         break;
      default:	// shouldn't get here
         max = 1;
         break;
    }
  }
  return max;
}


function validateQueryTitle()
{
  var title=getQueryVariable("title");

  // == set up to change the title ==
  // TODO need to de-base64 the title completely and strip
  // dangerous characters
  title = title.replace(/\+/g, " ");
  title = title.replace(/\!/g, " ");	// What are the escape chars in JS?
  title = title.replace(/\$/g, " ");
  title = title.replace(/\#/g, " ");
  title = title.replace(/%20/g, " ");
  title = title.replace(/:/g, " - ");
  title = title.replace(/\"/g, " ");
  title = title.replace(/\'/g, " ");
  return title; 
}
