var map;
var point;
var marker;
var html;

window.onload = initPage;

function initPage() {
  externalLinks();
  if(document.getElementById('q_contact_name')) {
    document.getElementById('q_contact_name').onfocus = clearfield;
    document.getElementById('q_contact_company').onfocus = clearfield;
    document.getElementById('q_contact_telephone').onfocus = clearfield;
    document.getElementById('q_contact_email').onfocus = clearfield;
    
    document.getElementById('q_contact_name').onblur = resetfield;
    document.getElementById('q_contact_company').onblur = resetfield;
    document.getElementById('q_contact_telephone').onblur = resetfield;
    document.getElementById('q_contact_email').onblur = resetfield;
  }
  if(document.getElementById('calculatesavings')) {
    document.getElementById('calculatesavings').onclick = initCalculator;
  }
  if(document.getElementById('map')) {
    load();
  }
}

function externalLinks() { 
  if (!document.getElementsByTagName) return; 
  var anchors = document.getElementsByTagName("a"); 
  for (var i=0; i<anchors.length; i++) { 
    var anchor = anchors[i]; 
    if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") 
      anchor.target = "_blank"; 
  } 
}

function clearfield() {
  if(this.id == 'q_contact_name') {
    if(this.value == 'Name') {
      this.value = '';
    }
  }
  if(this.id == 'q_contact_company') {
    if(this.value == 'Company') {
      this.value = '';
    }
  }
  if(this.id == 'q_contact_telephone') {
    if(this.value == 'Telephone Number') {
      this.value = '';
    }
  }
  if(this.id == 'q_contact_email') {
    if(this.value == 'E-mail') {
      this.value = '';
    }
  }
}

function resetfield() {
  if(this.id == 'q_contact_name') {
    if(this.value == '') {
      this.value = 'Name';
    }
  }
  if(this.id == 'q_contact_company') {
    if(this.value == '') {
      this.value = 'Company';
    }
  }
  if(this.id == 'q_contact_telephone') {
    if(this.value == '') {
      this.value = 'Telephone Number';
    }
  }
  if(this.id == 'q_contact_email') {
    if(this.value == '') {
      this.value = 'E-mail';
    }
  }
}

function load() {
  if (GBrowserIsCompatible()) {
    var html = document.getElementById('cont').innerHTML;
    html = html.replace('linkname','linkname2');
    html = html.replace('getDirToggle','getDirToggle2');  
    
    map = new GMap2(document.getElementById("map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(51.433491, -1.301461), 16);
    point = new GLatLng(51.433491, -1.301461);
    marker = new GMarker(point);
    map.addOverlay(marker)
    map.openInfoWindowHtml(map.getCenter(), html);
    
    GEvent.addListener(marker, "click", function() {  
      map.openInfoWindowHtml(point, html);
    });
  }
}
function prepareForm(form, cid){
  if (GBrowserIsCompatible()) {
    gApplication.prepareForm(form, cid);
  }
}


function displayDir(){    
  if (GBrowserIsCompatible()) {  
    var toggle = document.getElementById("getDirToggle").style.display;
    if(toggle == 'none'){      
      document.getElementById("getDirToggle").style.display='block';
      document.getElementById("linkname").innerHTML = "Don't get directions";      
    }else{
        document.getElementById("getDirToggle").style.display='none';
        document.getElementById("linkname").innerHTML = "Get directions";
  }
    
    var html = document.getElementById('cont').innerHTML;
    html = html.replace('linkname','linkname2');
    html = html.replace('getDirToggle','getDirToggle2');      
    map.openInfoWindowHtml(point, html);
  }
}
