var dist = 'Source';
function showCal(fieldname){
cal_fieldname=fieldname;

	var newX = findPosX(fieldname);
	var newY = findPosY(fieldname);
//alert("X:"+newX +", Y:"+newY);
	document.all.calendar.style.top = (newY+20) + 'px';
	document.all.calendar.style.left = (newX-10) + 'px';


document.all.calendar.style.visibility='visible';
}

function setDate(newDate){
cal_fieldname.value=newDate;
//document.all.Search1_searchCalendar.value=newDate;

}

function findPosX(obj)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj)
{
	var curtop = 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		curtop += obj.y;
	return curtop;
}

// Obsolete
function Open_Middle_Win(dest,win_name,win_wdt,win_hgt,params)
	{	
	if (win_wdt == 0 && win_hgt==0)
		{
		win_wdt = window.screen.width-15;
		win_hgt = window.screen.height-70;
		}
	var scr_mdl_left = (screen.width / 2) - (win_wdt / 2);
	var scr_mdl_top = (screen.height / 2) - (win_hgt / 2);		
	
	if(params!='')
		params = ',' + params;
	var win = window.open(dest,win_name,'width=' + win_wdt + ',height=' + win_hgt + 
						  ',left=' + scr_mdl_left + ',top=' + scr_mdl_top + params);
	win.focus();
	}

var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

function fixPNG(myImage) 
{
    if ((version >= 5.5) && (version < 7) && (document.body.filters)) 
    {
       var imgID = (myImage.id) ? "id='" + myImage.id + "' " : ""
	   var imgClass = (myImage.className) ? "class='" + myImage.className + "' " : ""
	   var imgTitle = (myImage.title) ? 
		             "title='" + myImage.title  + "' " : "title='" + myImage.alt + "' "
	   var imgStyle = "display:inline-block;" + myImage.style.cssText
	   var strNewHTML = "<span " + imgID + imgClass + imgTitle
                  + " style=\"" + "width:" + myImage.width 
                  + "px; height:" + myImage.height 
                  + "px;" + imgStyle + ";"
                  + "cursor:hand;"
                  + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
                  + "(src=\'" + myImage.src + "\', sizingMethod='scale');\"></span>"
	   myImage.outerHTML = strNewHTML	  
    }
}

function showOnMap(loc,lat,lng)
{
 var vAddress;
 
if (loc=='s')
 {
 vAddress= 'Address=' + document.TripPlanner.S_ADDRESS1.value +' ' + document.TripPlanner.S_ADDRESS2.value +", " + document.TripPlanner.S_CITY.value +", " + document.TripPlanner.S_STATEPRO.value;
 document.all.SourceTable.style.backgroundColor="#94d823";
 document.all.DestinationTable.style.backgroundColor="#cbd8e9";
 }
else
 {
 vAddress= 'Address=' + document.TripPlanner.E_ADDRESS1.value +' ' + document.TripPlanner.E_ADDRESS2.value +", " + document.TripPlanner.E_CITY.value +", " + document.TripPlanner.E_STATEPRO.value;
 document.all.SourceTable.style.backgroundColor="#cbd8e9";
 document.all.DestinationTable.style.backgroundColor="#94d823";
 }
var url = "ShowMap.aspx?" + vAddress + "&S_LAT=" + lat + "&S_LNG=" + lng;
document.all.Treeframe.src=url;
//noShowPlaces();
}


function showPlacesOnMap(city,state,group)
 {
 var url = "ShowMap.aspx?city=" + city + "&state=" + state + "&group=" + group;
 document.all.Treeframe.src=url;
 //noShowPlaces();
 }
 
var savedLoc;
var savedcity;
var savedstate;
var savedcondition = 0;
var savedmode = "a";

function popular_addresses(me,kind)
	{
	var target_url;
	dist=me;
	if (kind == "All")
		{
		selected_city = "";
		selected_state = "";
		}
	else if (kind == "State")
		{
		selected_city = "";
		if (dist=='Source')
			{
			selected_state = document.TripPlanner.S_STATEPRO.value;
			}
		else 
			{
			selected_state = document.TripPlanner.E_STATEPRO.value;
			}
		}
	else
		{
		if (dist== 'Source')
			{
			selected_city = getCity(document.TripPlanner.S_CITYTOWN);
			selected_state = document.TripPlanner.S_STATEPRO.value;
      document.all.SourceTable.style.backgroundColor="#94d823";
		  document.all.DestinationTable.style.backgroundColor="#cbd8e9";
			}
		else 
			{
			selected_city = getCity(document.TripPlanner.E_CITYTOWN);
			selected_state = document.TripPlanner.E_STATEPRO.value;
      document.all.SourceTable.style.backgroundColor="#cbd8e9";
		  document.all.DestinationTable.style.backgroundColor="#94d823";
			}
		}
	if (savedmode == "s")
	 showSched(dist,selected_city,selected_state);
	else
	 showPlaces(dist,selected_city,selected_state);
	}
function showPlaces(loc,c,s)
 {
 document.all.Treeframe.width="0";
 var url = "TreePlaces.aspx?Loc=" + loc;
 //var url = "sched/schedule.aspx?Loc" + loc;
 url += "&city=" + c + "&state=" + s;
 document.all.Treeframe.src=url;
 document.all.Tree.height="100%";
 document.all.Tree.width="80%";
 document.all.Treeframe.height="100%";
 document.all.Treeframe.width="100%";
 savedcity = c;
 savedstate = s;
 savedcondition = 1;
 savedmode = "a";
 }
 
function noShowPlaces()
 {
 document.all.Treeframe.src="Calnections/map.html";
 savedcondition = 0;
 }
 
function schedule(me,kind)
	{
	var target_url;
	dist=me;
	if (kind == "All")
		{
		selected_city = "";
		selected_state = "";
		}
	else if (kind == "State")
		{
		selected_city = "";
		if (dist=='Source')
			{
			selected_state = document.TripPlanner.S_STATEPRO.value;
			}
		else 
			{
			selected_state = document.TripPlanner.E_STATEPRO.value;
			}
		}
	else
		{
		if (dist== 'Source')
			{
			selected_city = getCity(document.TripPlanner.S_CITYTOWN);
			selected_state = document.TripPlanner.S_STATEPRO.value;
      document.all.SourceTable.style.backgroundColor="#94d823";
		  document.all.DestinationTable.style.backgroundColor="#cbd8e9";
			}
		else 
			{
			selected_city = getCity(document.TripPlanner.E_CITYTOWN);
			selected_state = document.TripPlanner.E_STATEPRO.value;
      document.all.SourceTable.style.backgroundColor="#cbd8e9";
		  document.all.DestinationTable.style.backgroundColor="#94d823";
			}
		}
  savedmode = "s";
  showSched(dist,selected_city,selected_state);
	}
function showSched(loc,c,s)
 {
 document.all.Treeframe.width="0";
 //var url = "TreePlaces.aspx?Loc=" + loc;
 var url = "sched/schedule.aspx?Loc" + loc;
 url += "&city=" + c + "&state=" + s;
 document.all.Treeframe.src=url;
 document.all.Tree.height="100%";
 document.all.Tree.width="80%";
 document.all.Treeframe.height="100%";
 document.all.Treeframe.width="100%";
 savedcity = c;
 savedstate = s;
 savedcondition = 1;
 savedmode = "s";
 }
 
function getAddress(alias,address1,address2,citytown,statepro,lat,lng)
	{
	if(dist=='Source')
		{
		document.TripPlanner.S_ADDRESS1.value=address1;
		document.TripPlanner.S_ADDRESS2.value=address2;
		var i;
		var j = document.TripPlanner.S_CITYTOWN.options.length;
		for (i = 0; i < j; i++)
		 {
		 if (document.TripPlanner.S_CITYTOWN.options[i].text == citytown &&
		     document.TripPlanner.S_CITYTOWN.options[i].value == statepro)
		     {
		     document.TripPlanner.S_CITYTOWN.options.selectedIndex = i;
		     break;
		     }
		 }
		document.TripPlanner.S_CITY.value=citytown;
		document.TripPlanner.s_usercity.value=citytown;
		document.TripPlanner.S_STATEPRO.value=statepro;
		document.TripPlanner.S_LAT.value=lat;
		document.TripPlanner.S_LNG.value=lng;
		document.TripPlanner.s_alias.value=alias;
    noShowPlaces();
		showOnMap('s',lat,lng);
		document.all.popAddStartText.innerHTML='<B>'+alias +'</B>';
		}
	else 
		{
		document.TripPlanner.E_ADDRESS1.value=address1;
		document.TripPlanner.E_ADDRESS2.value=address2;
		var i;
		var j = document.TripPlanner.E_CITYTOWN.options.length;
		for (i = 0; i < j; i++)
		 {
		 if (document.TripPlanner.E_CITYTOWN.options[i].text == citytown &&
		     document.TripPlanner.E_CITYTOWN.options[i].value == statepro)
		     {
		     document.TripPlanner.E_CITYTOWN.options.selectedIndex = i;
		     break;
		     }
		 }
		document.TripPlanner.E_CITY.value=citytown;
		document.TripPlanner.e_usercity.value=citytown;
		document.TripPlanner.E_STATEPRO.value=statepro;
		document.TripPlanner.E_LAT.value=lat;
		document.TripPlanner.E_LNG.value=lng;
		document.TripPlanner.e_alias.value=alias;
    noShowPlaces();
		showOnMap('d',lat,lng);
		document.all.popAddEndText.innerHTML='<B>'+ alias + '</B>';
		}
	}

function chengeS_STATE(city)
 {
 var s = getState(city);
 var c = getCity(city);
 if (document.TripPlanner.S_STATEPRO.value != s || document.TripPlanner.S_CITY.value != c)
	{
  document.TripPlanner.S_STATEPRO.value=s;
	document.TripPlanner.S_CITY.value=s;
	if (document.all.popAddStartText.innerHTML != '')
	 {
	 document.all.popAddStartText.innerHTML='Depart from: ';
	 //document.TripPlanner.S_ADDRESS1.value='';
	 ///document.TripPlanner.S_ADDRESS2.value='';
   document.TripPlanner.S_LAT.value='0.0';
   document.TripPlanner.S_LNG.value='0.0';
   //document.TripPlanner.E_LAT.value='0.0';
   //document.TripPlanner.E_LNG.value='0.0';
	 }
	}
 if (savedcondition == 1)
  {
  popular_addresses("Source","City");
  }
 }
function doAddrClear(se)
 {
 //document.TripPlanner.S_LAT.value='0.0';
 //document.TripPlanner.S_LNG.value='0.0';
 //document.TripPlanner.E_LAT.value='0.0';
 //document.TripPlanner.E_LNG.value='0.0';
 }
function doAliasClear(se)
 {
 if (se == 'S')
	{
  document.all.popAddStartText.innerHTML='Depart from: ';
	}
 else
  {
  document.all.popAddEndText.innerHTML='Arrive at: ';
  }
 }
function chengeE_STATE(city)
 {
 var s = getState(city);
 var c = getCity(city);
 if (document.TripPlanner.E_STATEPRO.value != s || document.TripPlanner.E_CITY.value != c)
	{
  document.TripPlanner.E_STATEPRO.value=getState(city);
  document.TripPlanner.E_CITY.value=getCity(city);
  if (document.all.popAddEndText.innerHTML != '')
	 {
   document.all.popAddEndText.innerHTML='Arrive at: ';
   //document.TripPlanner.E_ADDRESS1.value='';
   //document.TripPlanner.E_ADDRESS2.value='';
   //document.TripPlanner.S_LAT.value='0.0';
   //document.TripPlanner.S_LNG.value='0.0';
   document.TripPlanner.E_LAT.value='0.0';
   document.TripPlanner.E_LNG.value='0.0';
   }
  }
 if (savedcondition == 1)
  {
  popular_addresses("Destination","City");
  }
 }

function getCity(city)
 {
 return city.options[city.selectedIndex].text;
 }
function getState(city)
 {
 return city.options[city.selectedIndex].value;
 }
 
 /// City combo box...
function doCityChange(ele,obj,dir)
	{
	var i;
	var j = obj.options.length;
	var str=ele.value.toUpperCase();
	ele.value = str;
 //obj.options.style.visibility='hidden';
	for (i = 0; i < j; i++)
		 {
		 if (obj.options[i].text.indexOf(str) == 0)
	     {
	     obj.options.selectedIndex = i;
	     if (dir == 's')
							chengeS_STATE(obj);
						else
						 chengeE_STATE(obj);
		    break;
		    }
		 }
	synccity(ele,dir);
	}
function doCitySelect(obj,ele,dir)
	{
	ele.value=obj.options[obj.options.selectedIndex].text;
	synccity(ele,dir);
	}
function synccity(ele,dir)
 {
 if (dir == 's')
		{
		document.TripPlanner.S_CITY.value = ele.value;
		document.TripPlanner.S_LAT.value='0.0';
		document.TripPlanner.S_LNG.value='0.0';
		document.all.SourceTable.style.backgroundColor="#94d823";
		document.all.DestinationTable.style.backgroundColor="#cbd8e9";
		}
	else
		{
		document.TripPlanner.E_CITY.value = ele.value;
    document.TripPlanner.E_LAT.value='0.0';
    document.TripPlanner.E_LNG.value='0.0';
		document.all.SourceTable.style.backgroundColor="#cbd8e9";
		document.all.DestinationTable.style.backgroundColor="#94d823";
		}
 }
