function addLoadEvent(func) {
    var oldonload = window.onload;
    if (typeof window.onload != 'function') {
        window.onload = func;
    } else {
        window.onload = function() {
            if (oldonload) {oldonload();}
            func();
        }
    }
}

var LOC = {
 'corp' : {
  'language' : 'english',
  'time_format' : 'eu'
 },
 'nl' : {
  'language' : 'dutch',
  'time_format' : 'eu'
 },
 'uk' : {
  'language' : 'english',
  'time_format' : 'eu'
 },
 'usa' : {
  'language' : 'english',
  'time_format' : 'us'
 }
}


window.onload = function() {
 var img      = document.getElementById('billboard'),
     masthead = document.getElementById('masthead'),
     form     = document.getElementById('selectcountry'),
     select   = false,
     forms    = document.getElementsByTagName('form'),
     cookiename = 'exp_user_email';

 if (img && masthead) {
  masthead.style.backgroundImage = 'url('+img.src+')';
  var parent = img.parentNode;
  if (parent.nodeName == 'A') {
   masthead.style.cursor = 'pointer';
   masthead.onclick = function() { location.href = parent.href; }
  }
 }
 
 if (form) {
  form.getElementsByTagName('button')[0].style.display = 'none';
  select = document.getElementById('country');
  if (select) { select.onchange = function() {form.submit();} }
 }

 if (forms) {
  for (var i = 0; i < forms.length; i++) {
   var F = forms[i];
   var inputs = F.getElementsByTagName('input');
   for (var j = 0; j < inputs.length; j++) {
    var I = inputs[j];
    if (I.name != 'email') continue;
    F.onsubmit = function() {Cookies.create(cookiename,this.elements['email'].value,365);}
    if (Cookies[cookiename] && I.value.length == 0 && I.id == 'registeremail') { I.value = Cookies[cookiename]; }
   }
  }
 }

}

function selectState() {
 var block = document.getElementById('selectstate');
 var select = document.getElementById('m_country');

 if (!block || !select) return;

 select.onchange = function() {
  var val = this.options[this.selectedIndex].value;
  document.getElementById('selectstate').style.display = (val == 'usa') ? 'block' : 'none';
  if (language = document.getElementById('language')) { language.value = LOC[val].language; }
  if (time_format = document.getElementById('time_format')) { time_format.value = LOC[val].time_format; }
 }
}
addLoadEvent(selectState);


/* COOKIES, thanks to PPK */
var Cookies = {
	init: function () {
		var allCookies = document.cookie.split('; ');
		for (var i=0;i<allCookies.length;i++) {
			var cookiePair = allCookies[i].split('=');
			this[cookiePair[0]] = cookiePair[1];
		}
	},
	create: function (name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
		this[name] = value;
	},
	erase: function (name) {
		this.create(name,'',-1);
		this[name] = undefined;
	}
};
Cookies.init();
