// JavaScript Document
function MM_reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; }}
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
}
MM_reloadPage(true);

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
      } else if (test!='R') {
        if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (val<min || max<val) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}

var resetlist = new Array();
function JC_clearField(form, field){
	if(resetlist[field] != true){
		document.forms[form][field].value='';
		resetlist[field] = true;
	}
}

function JC_hideMenus(exception) {
	var i, menulist = new Array('wmarketing', 'marktopic', 'eurosearch', 'aboutwc', 'ownerserv', 
								'casestudy', 'translation', 'semarketing', 'measurement', 
								'emailmarket', 'news', 'agencysupport');
	for (i=0; i < menulist.length; i++) {
		if(menulist[i] != exception){
			document.getElementById(menulist[i]).style.overflow = 'hidden';
			document.getElementById(menulist[i]).style.height   = '21px';		
		}
	}
}

function JC_showHideMenu(menuname) {
	JC_hideMenus(menuname);
	if(document.getElementById(menuname).style.overflow == 'visible'){
		document.getElementById(menuname).style.overflow = 'hidden';
		document.getElementById(menuname).style.height   = '21px';	
	}else{
		document.getElementById(menuname).style.overflow = 'visible';
		document.getElementById(menuname).style.height   = 'auto';		
	}
}



function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1
       && a.getAttribute("rel").indexOf("alt") == -1
       && a.getAttribute("title")
       ) return a.getAttribute("title");
  }
  return null;
}

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

window.onload = function(e) {
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
}

window.onunload = function(e) {
  var title = getActiveStyleSheet();
  createCookie("style", title, 365);
}

var cookie = readCookie("style");
var title = cookie ? cookie : getPreferredStyleSheet();
setActiveStyleSheet(title);

function launchHandbook()
{
	var newurl = 
	newwindow=window.open('/safe-parenting/index.htm','handbook','toolbar=no,resizable=yes,scrollbars=yes,height=600,width=800');
	if (window.focus) {newwindow.focus()}
	return false;
}


function JC_validateTrainingForm()
{
	var errors = '';
	
	//MM validation for select boxes is this: CRAP!
	MM_validateForm(
	    'title',        '','R',
	    'date',         '','R',
	    'venue',        '','R',
	    'name',         '','R',
	    'job',          '','R',
	    'address',      '','R',
	    'telephone',    '','R',
	    'email',        '','RisEmail',
	    'manager',      '','R',
	    'manager_tel',  '','R');
	
	//Make sure user has selected 'permission' option.
	if(document.MM_returnValue){
		perm = MM_findObj('permission');
		if (!perm.options[0].selected){
			errors = 'You must have your manager\'s permission before booking a training course.';
			alert(errors);
			document.MM_returnValue = (errors == '')
		} 
	}
}