

function GetCookie(c_name){
  if (document.cookie.length>0){
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1){ 
      c_start=c_start + c_name.length+1 ;
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return "";
}

function SetCookie(value){
  var exdate=new Date();
  exdate.setDate(exdate.getDate()+365);
  document.cookie="location=" +escape(value)+";expires="+exdate.toGMTString();
}

function CheckCookie(){
  address=GetCookie('location');
  if (address!=null && address!="") {
      var a=document.getElementById('homeadd');
      a.value=address;
  }
}

var diaWindow,zillWindow,ppdWindow,smWindow,dirWindow;

function CloseThem() {

//alert(typeof diaWindow + '\n' + typeof smWindow + '\n' + typeof zillWindow + '\n' + typeof dirWindow + '\n' + typeof ppdWindow);
  if(typeof diaWindow != 'undefined') diaWindow.close();
  if(typeof smWindow != 'undefined') smWindow.close();
  if(typeof zillWindow != 'undefined') zillWindow.close();
  if(typeof dirWindow != 'undefined') dirWindow.close();
  if(typeof ppdWindow != 'undefined') ppdWindow.close();

}

var windowProperties='toolbar=0,location=0,status=0,scrollbars=1,resizable=1,width=800,height=' + (screen.height*0.9).toString();


function Diagram(map) {

  CloseThem();
  diaWindow = window.open ("","diagWindow",windowProperties);
  diaWindow.moveTo(0,0);
  diaWindow.location=map;
}

function Zill(address) {

  CloseThem();
  zillWindow = window.open ("","satWindow",windowProperties);
  zillWindow.moveTo(0,0);
//split(delimiter, [limit])
  var field=address.split(',',3);
//alert(' address=' + field[0] + '\n city=' + field[1] + '\n city=' + field[2]);
  var a=document.getElementById('zillsearch');
  var b=document.getElementById('addrstrthood');
  var c=document.getElementById('citystatezip');
  b.value=field[0];
  c.value=field[1] + ',' + field[2];
  a.submit();

}

function PPD(page) {

  CloseThem();
  ppdWindow = window.open ("","parkWindow",windowProperties);
  ppdWindow.moveTo(0,0);
  ppdWindow.location=page;
}

function SetMapFields(address) {

  var a=document.getElementById('q1');
  a.value=address;
  var k=document.getElementById('q_d');
  k.value=address;

//split(delimiter, [limit])
  var field=address.split(',',3);
//alert(' address=' + field[0] + '\n city=' + field[1] + '\n state=' + field[2]);
  var b=document.getElementById('address');
  var c=document.getElementById('city');
  var d=document.getElementById('state');
  var e=document.getElementById('randadd');
  var f=document.getElementById('randcit');
  var g=document.getElementById('randst');
  var h=document.getElementById('strt1');
  var i=document.getElementById('city1');
  var j=document.getElementById('stnm1');
  b.value=e.value=h.value=field[0];
  c.value=f.value=i.value=field[1];
  d.value=g.value=j.value=field[2];
  
}

function SetDirFields(address) {

  var a=document.getElementById('homeadd');
  
  var b=document.getElementById('q1dd');
  b.value=a.value;              
  var c=document.getElementById('d_d');
  c.value=a.value;              

//replace( regexp, replacetext)
  var addCitSt=a.value.replace(/, /g, ",") //removes spaces;
//split(delimiter, [limit])
  var field=addCitSt.split(',',3);
//alert(' address=' + field[0] + '\n city=' + field[1] + '\n state=' + field[2]);
  var d=document.getElementById('a1');
  var e=document.getElementById('c1');
  var f=document.getElementById('s1');
  var g=document.getElementById('randstadd');
  var h=document.getElementById('randstcit');
  var i=document.getElementById('randstst');
  var j=document.getElementById('strt1dd');
  var k=document.getElementById('city1dd');
  var l=document.getElementById('stnm1dd');
  d.value=g.value=j.value=field[0];
  e.value=h.value=k.value=field[1];
  f.value=i.value=l.value=field[2];

  var m=document.getElementById('q2dd');
  m.value=address;
  var n=document.getElementById('d_daddr');
  n.value=address;
  
//split(delimiter, [limit])
  var field=address.split(',',3);
//alert(' address=' + field[0] + '\n city=' + field[1] + '\n state=' + field[2]);
  var o=document.getElementById('a2');
  var p=document.getElementById('c2');
  var q=document.getElementById('s2');
  var r=document.getElementById('randdestadd');
  var s=document.getElementById('randdestcit');
  var t=document.getElementById('randdestst');
  var u=document.getElementById('strt2dd');
  var v=document.getElementById('city2dd');
  var w=document.getElementById('stnm2dd');
  o.value=r.value=u.value=field[0];
  p.value=s.value=v.value=field[1];
  q.value=t.value=w.value=field[2];

  
}

function SM(address) {
  
  for (i=0; i<document.all.details.length; i++){
    if(document.all.details[i].checked){
      var mapper=document.all.details[i].value;
//alert("You chose: " + mapper);
    }
  }
  
  if(mapper)  {
    CloseThem();
    SetMapFields(address);
    smWindow=window.open ("","mapWindow",windowProperties);
    smWindow.focus();
    smWindow.moveTo(0,0);
    
    switch (mapper) {
      case 'Yahoo!': 
              var a=document.getElementById('yahoosm');
              a.submit();
              break;
      case 'Google': 
              var a=document.getElementById('googlesm');
              a.submit();
              break;
      case 'Mapquest': 
              var a=document.getElementById('mapquestsm');
              a.submit();
              break;
      case 'Rand': 
              var a=document.getElementById('randsm');
              a.submit();
              break;
      case 'Expedia': 
              var a=document.getElementById('expediasm');
              a.submit();
              break;
      default: alert('Please select a map generator');
    }
  }
  else {
    var gotoGoogle = confirm('If you would like to see a Google map, please click "OK".  Otherwise, please click "Cancel" and make a choice in the control panel at the top of this page.');
    if (gotoGoogle) {
      for (i=0; i<document.all.details.length; i++){
        if(document.all.details[i].value == 'Google') document.all.details[i].checked=true;
      }
      SM(address);
    }
    else {
      document.location=thisPage+'#control';
    }
  }
  
}

function DD(address) {

  var d=document.getElementById('homeadd');
  if(d.value=='Address, City, State' || d.value=='') {
    d.value=prompt('Please enter an "Address, City, State" for your starting point:','Address, City, State');
    if(d.value=='null' || d.value=='' || d.value=='Address, City, State') {
      d.value='Address, City, State';
      return;
    }
    else {
      SetCookie(d.value)
    }
  }
  
  for (i=0; i<document.all.drive.length; i++){
    if(document.all.drive[i].checked){
      var mapper=document.all.drive[i].value;
//alert("You chose: " + mapper);
    }
  }

  if(mapper)  {
    CloseThem();
    SetDirFields(address);
    dirWindow=window.open ("","ddWindow",windowProperties);
    dirWindow.focus();
    dirWindow.moveTo(0,0);
  
    switch (mapper) {
      case 'Yahoo!': 
              var a=document.getElementById('yahoodd');
              a.submit();
              break;
      case 'Google': 
              var a=document.getElementById('googledd');
              a.submit();
              break;
      case 'Mapquest': 
              var a=document.getElementById('mapquestdd');
              a.submit();
              break;
      case 'Rand': 
              var a=document.getElementById('randdd');
              a.submit();
              break;
      case 'Expedia': 
              var a=document.getElementById('expediadd');
              a.submit();
              break;
      default:  alert('Please select a driving directions generator');
    }
  }
  else {
    var gotoGoogle = confirm('If you would like to see a Google map, please click "OK".  Otherwise, please click "Cancel" and make a choice in the control panel at the top of this page.');
    if (gotoGoogle) {
      for (i=0; i<document.all.drive.length; i++){
        if(document.all.drive[i].value == 'Google') document.all.drive[i].checked=true;
      }
    DD(address);
    }
    else {
      document.location=thisPage+'#control';
    }
  }
  
}

//Not all map generators handle rural locations well.  
//The DirectSub function overides user preferences for difficult locations.
//
//sample implementation of user preference buttons
//<input class=strt type=button value="Streets Map" onclick="SM('E 8th Rd & Park,Oglesby,IL')">
//<input class=drdir type=button value="Driving Directions" onclick="DD('E 8th Rd & Park,Oglesby,IL')">
//
//sample implementation of user preference overide buttons
//<input class=strt type=button value="Streets Map" onclick="DirectSub('Unknown road @41.321280, -89.010780','g','sm')">
//<input class=drdir type=button value="Driving Directions" onclick="DirectSub('Unknown road @41.321280, -89.010780','g','dd')">

function DirectSub(address,mapper,mapType) {

    CloseThem();
    switch (mapType) {
      case 'sm': 
               SetMapFields(address);
               smWindow=window.open ("","mapWindow","location=0,status=0,scrollbars=1,width=800,height='100%'");
               smWindow.focus();
               smWindow.moveTo(0,0);
               break;
      case 'dd': 
            var d=document.getElementById('homeadd');
            if(d.value=='Address, City, State' || d.value=='') {
              d.value=prompt('Please enter an "Address, City, State" for your starting point:','Address, City, State');
              if(d.value=='null' || d.value=='' || d.value=='Address, City, State') {
                d.value='Address, City, State';
                return;
              }
              else {
                SetCookie(d.value)
              }
            }
               SetDirFields(address);
               dirWindow=window.open ("","ddWindow",windowProperties);
               dirWindow.focus();
               dirWindow.moveTo(0,0);
               break;
      default: alert(mapType);
    }
    
    switch (mapper) {
      case 'y': 
              var a=document.getElementById('yahoo' + mapType);
              a.submit();
              break;
      case 'g': 
              var a=document.getElementById('google' + mapType);
              a.submit();
              break;
      case 'm': 
              var a=document.getElementById('mapquest' + mapType);
              a.submit();
              break;
      case 'r': 
              var a=document.getElementById('rand' + mapType);
              a.submit();
              break;
      case 'e': 
              var a=document.getElementById('expedia' + mapType);
              a.submit();
              break;
      default: 
              var a=document.getElementById('yahoo' + mapType);
              a.submit();
    }
}
