/***A more elegant implementation of google analytics - by Al Stevens 20 July 2008***/


/* Simplifies onload, you will no longer have to add an onload event call just call addLoadEvent */
function addLoadEvent(func,arg){
  
  if (!arg){
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
      window.onload = func;
    } else {
      window.onload = function() {
	oldonload();
	func();
      }
    }
  }
  else{/*if the onload event has an argument/parameter cater for that*/
    if (arg){
      oldonload = window.onload;
      if (typeof window.onload != 'function') {
	window.onload = func(arg); 
      }
      window.onload = function() {
	oldonload();
	func(arg); 
      }
    }
  }
}

function loadGAScript(){
  /*Check browser for Dom compatibility*/
  if (!document.getElementsByTagName) return false;
  /*Determines whether the page is using a secure or unsecure protocol*/
  var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
  /*Writes in the script to the document head*/
  var gaScript = document.createElement("script");
  gaScript.setAttribute("src",gaJsHost +"google-analytics.com/ga.js");
  gaScript.setAttribute("type","text/javascript");
  var domHead = document.getElementsByTagName("head")[0]
    domHead.appendChild(gaScript);
}

loadGAScript();

/*Calls the analytics function*/
function callGA(){	
  var pageTracker = _gat._getTracker("UA-15254166-1");
  pageTracker._initData();
  pageTracker._trackPageview();
}

addLoadEvent(callGA);

/******************************/

function validate_required( field, message ){
  with( field ){
    if( value == null || value.match( /^ *$/)){
      alert( message );
      field.focus();
      return false;
    } else {
      return true;
    }
  }
}

function validate_checked( field, message ){
  if( !field.checked ){
    alert( message );
    field.focus();
    return false;
  } else {
    return true;
  }
}



function validate_regexp( field, regexp, message){
  with( field ){
    if( !value.match( regexp ) ){
      alert( message );
      field.focus();
      return false;
    } else {
      return true;
    }
  }
}

function validate_email( field, message ){
  return validate_regexp( field, /^ *[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4} *$/, message );
}

function validate_phone_orig( field, message ){
  /* Hmmm, opera doesn't swallow this one, while IE,FF,Chrome and Safari do */
  /*
    optional connection: hyphen or space                              | (-| )?
    country code: e.g. +31, plus optional connection, or empty        | (\+\d{1,3}(-| )?)?
    area code (0)30 or 030 or (030), plus opt. connection, or empty   | (\(\d+\))?// (((\(\d\)\d{1,4})|(\d{1,5}))|(\(\d{1,5}\))(-| )?)?
    1-6 blocks of 2-5 digits, with optional connection                | (\d{2,5}(-| )?){1,6}
    start/end with arbitrary number of spaces.
  */
  return validate_regexp( field, /^ *(\+\d{1,3}(-| )?)?(\(\d+\)(-| )?)?(\d{1,5}(-| )?){1,8} *$/ , message );
}


function validate_phone( field, message ){
  /*  Not exactly fail-safe, but at least Opera swallows this one */ 
  return validate_regexp( field, /^[0-9\(\) \+-]{8,20}$/, message );
}


function validate_human_en( field, answer ){
  with( field ){
    if( value == answer ){
      return true;
    }

    if( !value.match( /^\d+$/) ){
      alert( "To prove that you are not a spam robot, please provide the correct answer to the test question (digits only)." );
    } else {
      alert( "Please try to solve the test question again. Hint: the answer is an integral number between 41.8 and 42.5." );
    }

    field.focus();
    return false;
  }
}


function validate_human_nl( field, answer ){
  with( field ){
    if( value == answer ){
      return true;
    }

    if( !value.match( /^\d+$/) ){
      alert( "Beantwoord alstublieft de testvraag (alleen cijfers) om aan te tonen dat u geen spamrobot bent." );
    } else {
      alert( "Probeer de testvraag alstublief nog eens te beantwoorden. Hint: het antwoord is een geheel getal tussen 41.8 en 42.5." );
    }

    field.focus();
    return false;
  }
}


function validate_form_en( form ){
  with( form ){
    return validate_required( course, "Please select a course" ) &&
      validate_required( gender, "Please indicate your gender") &&
      validate_required( firstname, "Please provide your first name" ) &&
      validate_required( lastname, "Please provide your last name" ) &&
      validate_required( company, "Please provide your company's name" ) &&
      validate_required( address, "Please provide your address" ) &&
      validate_required( zip, "Please provide your zip code" ) &&
      validate_required( city, "Please provide your city name" ) &&
      validate_required( email, "Please provide your email address" ) &&
      validate_email( email, "Please provide a valid email address" ) &&
      validate_required( phone, "Please provide your phone number" ) &&
      validate_phone( phone, "Please provide a valid phone number" ) &&
      validate_human_en( human_answer, 42 ) &&
      validate_checked( terms_agreement, "Please indicate that you accept the 59bits terms and conditions" );
  }
}


function validate_form_nl( form ){
  with( form ){
    return validate_required( course, "Selecteer alstublief een cursus" ) &&
      validate_required( gender, "Selecteer alstublieft uw geslacht") &&
      validate_required( firstname, "Vul alstublieft uw voornaam in" ) &&
      validate_required( lastname, "Vul alstublieft uw achternaam in" ) &&
      validate_required( company, "Vul alstublieft de naam van uw bedrijf in" ) &&
      validate_required( address, "Vul alstublieft uw adres in" ) &&
      validate_required( zip, "Vul alstublieft uw postcode in" ) &&
      validate_required( city, "Vul alstublieft uw stad in" ) &&
      validate_required( email, "Vul alstublieft uw emailadres in" ) &&
      validate_email( email, "Vul alstublieft een geldig emailadres in" ) &&
      validate_required( phone, "Vul alstublieft een telefoonnummer in" ) &&
      validate_phone( phone, "Vul alstublieft een geldig telefoonnummer in" ) &&
      validate_human_nl( human_answer, 42 ) &&
      validate_checked( terms_agreement, "Geef alstublieft aan dat u de algemene voorwaarden van 59bits accepteert" );
  }
}







