/*******************************
	JavaScript Document
	VisitRhodeIsland.com
	version: 2.1
	developer: rsoares [at] riedc.com
*******************************/

/*******************************************************
DESCRIPTION: Generates a fixed size browser pop-up window 
without a toolbar, scrollbar, or resizable. 

PARAMETERS: 
	url - source string containing destination url.
	height - the browser winf height.
	width - the browser window width.
	
USAGE: javascript:popUp("url_path",100,200);
*******************************************************/
function popUp( url, height, width, chkcookie ) {
	theCookie = "";
	if (chkcookie) {
		//Does exist?
		theCookie = getCookie("poponce");
		//else Set cookie.
		setCookie("poponce", "true");
	}
	if (theCookie != "true") {
		var name = "form" ;
		var str = "height=" + height + ",innerHeight=" + height ;
		var strn = "'toolbar=no,scrollbars=no,resizable=no'" ;
		str += ",width=" + width + ",innerWidth=" + width ;
		if ( window.screen ) {
			var ah = screen.availHeight - 30 ;
			var aw = screen.availWidth - 10 ;
			var xc = ( aw - width ) / 2 ;
			var yc = ( ah - height ) / 2 ;
		
			str += ",left=" + xc + ",screenX=" + xc ;
			str += ",top=" + yc + ",screenY=" + yc ;
		}       
		window.open( url, name, str, strn ) ;
	}
}

/*******************************************************
DESCRIPTION: Cookie Management - Set, Get, Delete, FixDate
*******************************************************/

/*
   name - name of the cookie
   value - value of the cookie
   [expires] - expiration date of the cookie
     (defaults to end of current session)
   [path] - path for which the cookie is valid
     (defaults to path of calling document)
   [domain] - domain for which the cookie is valid
     (defaults to domain of calling document)
   [secure] - Boolean value indicating if the cookie transmission requires
     a secure transmission
   * an argument defaults when it is assigned null as a placeholder
   * a null placeholder is not required for trailing omitted arguments
*/

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}


/*
  name - name of the desired cookie
  return string containing value of specified cookie or null
  if cookie does not exist
*/

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}


/*
   name - name of the cookie
   [path] - path of the cookie (must be same as path used to create cookie)
   [domain] - domain of the cookie (must be same as domain used to
     create cookie)
   path and domain default if assigned null or omitted if no explicit
     argument proceeds
*/

function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"

function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}

/*******************************************************
DESCRIPTION: Creates a link to MapQuest in one of two
formats: As an ICON or as a styled LINK.

ASSUMPTIONS: The script assumes all address are RI based.

PARAMETERS:
	street - source street address.
	city - source city name.
	
USAGE: createMapLink("123 Smith Street","Providence");
*******************************************************/
function createMapLink( street, city ) {
	
	if ( street == "" && city == "" ) {
		// Missing required parameters, generate HTML error comment.
		document.write( '<em class="errormsg">No Map Available.</em>' ) ;
		
	} else {
		
		// Change forms of "City-Wide" in <street>, to make EMPTY.
		street = street.toLowerCase() ;
		if ( street == "city-wide" || street == "citywide" || street == "city wide" ) {
				street = "";
		}
		
		// Replace all space chars in strings with HTTP safe chars "+".
		objRegExp	= / /g ;
		urlStreet 	= street.replace( objRegExp, "+" ) ;
		urlCity		= city.replace( objRegExp, "+" ) ;
		
		// Construct Proper URL.
		strURL  = "http://www.mapquest.com/maps/map.adp?country=US&countryid=US&searchtype=address" ;
		strURL += "&address=" + urlStreet ;
		strURL += "&city=" + urlCity ;
		strURL += "&state=ri" ;
		strURL += "&search=++Search++" ;
		
		// Format URL string as LINK or ICON.
		mapLink = '<a href="' + strURL + '" target="_blank" title="View map for this event" class="url-popup">View Map</a>' ;
		
		// Render final URL source code to the page.
		document.write( mapLink ) ;
	}
}
