window.onload = initForm;

function initForm() { 
  external_links();
  
  if(document.getElementById('contact_name')) {
    document.getElementById('contact_name').onfocus = clearfield;
    document.getElementById('contact_telephone').onfocus = clearfield;
    document.getElementById('contact_email').onfocus = clearfield;
    document.getElementById('contact_address').onfocus = clearfield;
    document.getElementById('contact_address2').onfocus = clearfield;
    document.getElementById('contact_enquiry').onfocus = clearfield;
    document.getElementById('contact_name').onblur = resetfield;
    document.getElementById('contact_telephone').onblur = resetfield;
    document.getElementById('contact_email').onblur = resetfield;
    document.getElementById('contact_address').onblur = resetfield;
    document.getElementById('contact_address2').onblur = resetfield;
    document.getElementById('contact_enquiry').onblur = resetfield;
  }
  if(document.getElementById('contact_map_map')) {
    mapload(); 
  }
}

// Open links in new window
function external_links() { 
  if(!document.getElementsByTagName) {
    return;
  } else { 
    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.getAttribute("rel") == "external nofollow") { 
        anchor.target = "_blank";
      }
    }
  } 
}



  
  
function clearfield() {
  
  var formValue = this.value.replace(/^\s+|\s+$/g, '');
  formValue = formValue.toLowerCase();
  
  switch(this.id) {
    case 'contact_name':
      if(formValue == 'name') {
        document.getElementById('contact_name').value = ""; 
      }
    break;
    case 'contact_telephone':
      if(formValue == 'tel') {
        document.getElementById('contact_telephone').value = ""; 
      }
    break;
    case 'contact_email':
      if(formValue == 'email') {
        document.getElementById('contact_email').value = ""; 
      }
    break;
    case 'contact_address':
      if(formValue == 'address') {
        document.getElementById('contact_address').value = ""; 
      }
    break;
    case 'contact_address2':
      if(formValue == 'address 2') {
        document.getElementById('contact_address2').value = ""; 
      }
    break;
    case 'contact_enquiry':
      if(formValue == 'enquiry') {
        document.getElementById('contact_enquiry').value = ""; 
      }
    break;
  } 
}
  
function resetfield() {
    
  var formValue = this.value.replace(/^\s+|\s+$/g, '');
   
  switch(this.id) {
    case 'contact_name':
      if(formValue == '') {
        document.getElementById('contact_name').value = "Name"; 
      }
    break;
    case 'contact_telephone':
      if(formValue == '') {
        document.getElementById('contact_telephone').value = "Tel"; 
      }
    break;
    case 'contact_email':
      if(formValue == '') {
        document.getElementById('contact_email').value = "Email"; 
      }
    break;
    case 'contact_address':
      if(formValue == '') {
        document.getElementById('contact_address').value = "Address"; 
      }
    break;
    case 'contact_address2':
      if(formValue == '') {
        document.getElementById('contact_address2').value = "Address 2"; 
      }
    break;
    case 'contact_enquiry':
      if(formValue == '') {
        document.getElementById('contact_enquiry').value = "Enquiry"; 
      }
    break;
  } 
}

// make global variables.
var map;
var point;
var marker;
var html;

function mapload() {
  if (GBrowserIsCompatible()) {
    // get the HTML which we need to use to create the new info box
    var html = document.getElementById('contact_map_html').innerHTML;
    // replace the linkname tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('linkname','linkname2');
    // replace the getDirToggle tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('getDirToggle','getDirToggle2');  
    
    map = new GMap2(document.getElementById("contact_map_map"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    map.setCenter(new GLatLng(51.41876,-1.514139), 15);
    point = new GLatLng(51.41876,-1.514139);
    marker = new GMarker(point);
    map.addOverlay(marker)
    // open the info window with the modified HTML. no duplicate ID's for us!
    map.openInfoWindowHtml(map.getCenter(), html);
    
    // add an onclict event, too!
    GEvent.addListener(marker, "click", function() {  
      // open the info window with the modified HTML. no duplicate ID's for us!    
      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";
  }
    
    // get the HTML which we need to use to create the new info box
    var html = document.getElementById('contact_map_html').innerHTML;
    // replace the linkname tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('linkname','linkname2');
    // replace the getDirToggle tag for a different one, so our toggle function doesnt get confused.
    //html = html.replace('getDirToggle','getDirToggle2');      
    // open the info window with the modified HTML. no duplicate ID's for us!
    map.openInfoWindowHtml(point, html);
  }
}
            
