// global.js for Natural Cleaners site
// site-wide functionality
// code by CN

/**********************************************************************
   Page initialization
 *********************************************************************/

function pageInit() {
  if (isDefined(document.getElementsByTagName)) {
    
    // links
    var links = document.getElementsByTagName('a');
    for (var linkIndex = links.length - 1; linkIndex >= 0; linkIndex--) {
      var linkElement = links[linkIndex];
      
      // external links
      if (linkElement.className.indexOf('external') > -1) {
        linkElement.onclick = function() {
          openExternalLink(this.href);
          return false;
        };
      }
    }
    
    // page-specific initialization
    var pageId = document.getElementsByTagName('body')[0].id;
    if (pageId && pageId == 'contact') {
      contactFormInit(pageId + '-form');
    }
  }
}

function contactFormInit(formId) {
  var form = document.getElementById(formId);
  if (form) {
    form.doExtraValidation = function() {
      
      // either phone number or email filled in
      var phone1Input = document.getElementById('input-phone-area-code');
      var phone2Input = document.getElementById('input-phone-prefix');
      var phone3Input = document.getElementById('input-phone-suffix');
      var emailInput = document.getElementById('input-email');
      if (emailInput.value === '' && (phone1Input.value === '' || phone2Input.value === '' || phone3Input.value === '')) {
        this.showError('phone-or-email');
        document.getElementById('field-phone').showError('phone-or-email');
        document.getElementById('field-email').showError('phone-or-email');
        
        return false;
      }
      
      return true;
    }
  } 
}


/**********************************************************************
   Popups and New Windows
 *********************************************************************/

// openExternalLink(url) - use to open all off-site links
// ie: <a href="http://url.here" onclick="openExternalLink(this.href); return false;">
function openExternalLink(url) {
  return window.open(url, 'external', 'location,menubar,resizable,scrollbars,status,toolbar');
}


/**********************************************************************
   Basic Event Registration
   - has issues see http://www.quirksmode.org/blog/archives/2005/08/addevent_consid.html
 *********************************************************************/

function addEvent(object, eventName, functionRef) {
  if (isDefined(window.addEventListener)) {
    object.addEventListener(eventName, functionRef, false);
  }
  else if (isDefined(window.attachEvent)) {
    object.attachEvent('on' + eventName, functionRef);
  }
}

function removeEvent(object, eventName, functionRef){
  if (isDefined(window.addEventListener)) {
    object.removeEventListener(eventName, functionRef, false);
    return true;
  }
  else if (isDefined(window.attachEvent)) {
    var r = object.detachEvent('on' + eventName, functionRef);
    return r;
  }
}


/**********************************************************************
   Utilities
 *********************************************************************/

function isDefined(property) {
  return (typeof property != 'undefined');
}


// call pageInit when document finishes loading
window.addEvent(window, 'load', pageInit);