
//Google Map Functionality
			   
var clickHandler;
var map;
var lat;
var lng;
var locations;
var bounds;
var routes;
var pLines = new Array();
var startmarker;
var routeHandlerUrl;
/*
mapID : id of div where map should be loaded
x, y : initial position of map
z : initial zoom of map
url : callback url to fetch marker coordinates in JSON format
*/

function addGMap(mapID, x,y,z,url,routeurl)
{
routeHandlerUrl = routeurl;
  if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById(mapID));
//        map.addControl(new GSmallMapControl());
//        map.addControl(new GMapTypeControl());
        map.setCenter(new GLatLng(x, y), z);

//CEDjs.Xmlhttp.get(url, function(retval){{fromTo('{1}', retval, '{2}');}},false)
CEDjs.Xmlhttp.get(url, processLocations,false);
//$.get(url, processLocations );
var customUI = map.getDefaultUI();
customUI.controls.largemapcontrol3d = false;
customUI.controls.smallzoomcontrol3d = true;
customUI.controls.maptypecontrol = false;
customUI.controls.menumaptypecontrol = true;
map.setUI(customUI);
     map.disableScrollWheelZoom();
  }
}
 
function setNewMarker(point) {
	lat = point.lat();
	lng = point.lng();
}
	
function processLocations(content) {
    eval("locations = "+content);
	//marker = new Array();
	var i = 0;
	for(var l in locations)
	{
		var element = locations[l];
				map.addOverlay(CreateMarker(element));
		i++;
	}
}
function CreateMarker(element)
{


var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow  = "/img/destshadow.png";
baseIcon.iconSize = new GSize(32,49);
baseIcon.shadowSize = new GSize(39,16);
baseIcon.iconAnchor = new GPoint(16, 48);
baseIcon.infoWindowAnchor = new GPoint(9, 2);

	var specialicon = new GIcon(baseIcon);
	specialicon.image = "/img/desticon.png";

  	var markerOptions = {icon:specialicon, title: element.Name };
  	var marker = new GMarker(new GLatLng(element.X, element.Y), markerOptions);

	//var marker = new GMarker(new GLatLng(element.X, element.Y), {title: element.Name});
		
	GEvent.addListener(marker, 'click', function() {marker.openInfoWindowHtml(element.BubbleHTML);});
	//GEvent.addListener(marker, 'mouseover', function() { showDestinations(marker, routeHandlerUrl,  element.CityCode, element.X, element.Y); });
	return marker;
}

function CreateMarkerWithPopup(x,y,name,divID,type)
{

var baseIcon = new GIcon(G_DEFAULT_ICON);
baseIcon.shadow  = "/img/shadow.png";
baseIcon.iconSize = new GSize(28, 44);
baseIcon.shadowSize = new GSize(48, 17);
baseIcon.iconAnchor = new GPoint(28, 44);
baseIcon.infoWindowAnchor = new GPoint(9, 2);

	var specialicon = new GIcon(baseIcon);
	specialicon.image = "/img/" + type +".png";

  	var markerOptions = {icon:specialicon, title: name };
  	var marker = new GMarker(new GLatLng(x, y), markerOptions);
	
	GEvent.addListener(marker, 'click', function() {amex.popup.showdiv(divID);});
	return marker;
}

function showDestinations(fromMarker, routeUrl, citycode, x,y)
{
	startmarker = new GMarker(new GLatLng(x, y), {title: ""});
	for(var l in pLines)
	{
		var element= pLines[l];
		map.removeOverlay(element);
	}
	CEDjs.Xmlhttp.get(routeUrl + "?from=" + citycode, processRoutes,false);
	//$.get(routeUrl + "?from=" + citycode, processRoutes );
}

function processRoutes(content) {
    eval("routes = "+content);
	for(var l in routes)
	{
		var element = routes[l];
		
		var endmarker = new GMarker(new GLatLng(element.X, element.Y), {title: "hello"});
		DrawRoute(startmarker.getPoint(), endmarker.getPoint());
	}
}

function DrawRoute(p1, p2) { 
		var fPoints = new Array();
		with (Math) {
			var lat1 = p1.y * (PI/180);
			var lon1 = p1.x * (PI/180);
			var lat2 = p2.y * (PI/180);
			var lon2 = p2.x * (PI/180);
 
			var d = 2*asin(sqrt( pow((sin((lat1-lat2)/2)),2) + cos(lat1)*cos(lat2)*pow((sin((lon1-lon2)/2)),2)));
			var bearing = atan2(sin(lon1-lon2)*cos(lat2), cos(lat1)*sin(lat2)-sin(lat1)*cos(lat2)*cos(lon1-lon2))  / -(PI/180);
			bearing = bearing < 0 ? 360 + bearing : bearing;
  
			for (var n = 0 ; n < 51 ; n++ ) {
				var f = (1/50) * n;
				f = f.toFixed(6);
				var A = sin((1-f)*d)/sin(d)
				var B = sin(f*d)/sin(d)
				var x = A*cos(lat1)*cos(lon1) +  B*cos(lat2)*cos(lon2)
				var y = A*cos(lat1)*sin(lon1) +  B*cos(lat2)*sin(lon2)
				var z = A*sin(lat1)           +  B*sin(lat2)
 
				var latN = atan2(z,sqrt(pow(x,2)+pow(y,2)))
				var lonN = atan2(y,x)
				var p = new GLatLng(latN/(PI/180), lonN/(PI/180));
				fPoints.push(p);
//				bounds.extend(p);
			} 
		}
 		//routes.push(fPoints);
		var pLine = new GPolyline(fPoints,'#000000',1,1);
		pLines.push(pLine);
		map.addOverlay(pLine);
}

function RedirectSelectAction(selectBoxId)
{

	var selectedLink = document.getElementById(selectBoxId).value //$("#" + selectBoxId).val();
	top.location.href = selectedLink;
	
	return false;
	
}

function ShowHide(id)
{
if(id.style.display == "")
{
   id.style.display = "none";
}
else
{
  id.style.display = "";
}

    amex.checkContentHeight();
}


sas_tmstp=Math.round(Math.random()*10000000000);sas_masterflag=1;

function SmartAdServer(sas_pageid,sas_formatid,sas_target) {
if (sas_masterflag==1) {sas_masterflag=0;sas_master='M';} else {sas_master='S';};
document.write('<scr'+'ipt SRC="http://ww13.smartadserver.com/call/pubj/' +
sas_pageid + '/' + sas_formatid + '/'+sas_master + '/' + sas_tmstp + '/' +
escape(sas_target) + '?"></scr'+'ipt>');
}




function fromTo(idTo, values, inittxt)
{
	var result = eval('(' + values + ')');
	clearOptions(idTo);
	appendOption(inittxt,'',idTo);
	for(var i in result)
	{	
		appendOption(result[i],i,idTo);
	}
}
function clearOptions(sel)
{
	var tolist = document.getElementById(sel);
	while (tolist.options.length > 0) {
		tolist.options[0] = null;
	}
}
function appendOption(txt,val,sel)
{
  var elOptNew = document.createElement('option');
  elOptNew.text = txt;
  elOptNew.value = val;
  var elSel = document.getElementById(sel);

  try {
    elSel.add(elOptNew, null); // standards compliant; doesn't work in IE
  }
  catch(ex) {
    elSel.add(elOptNew); // IE only
  }
}

function limitText(limitField, limitNum) {
    if (limitField.value.length > limitNum) {
        limitField.value = limitField.value.substring(0, limitNum);
    } 
}


function getKeyCode(e)// v1.0
{
	if (window.event)
	   return window.event.keyCode;
	else if (e)
	   return e.which;
	else
	   return null;
}
function keyRestrict(e, validchars) { // v3.0

if(validchars == '') return true;

	var key='', keychar='';
	key = getKeyCode(e);
	if (key == null) return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	validchars = validchars.toLowerCase();
	if (validchars.indexOf(keychar) != -1)
		return true;
	if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
		return true;
	return false;
}

function toggledate(status,radiobutton,calendarID)
{
   var cal = document.getElementById(calendarID);
   if(status=="off")
   {
       cal.style.visibility = 'hidden';
   }
   else
   {
      cal.style.visibility = 'visible';
   }
}