function startList(nav) {
  if (document.all&&document.getElementById(nav)) {
    navRoot = document.getElementById(nav);
    for (i=0; i<navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i];
      if (node.nodeName=="LI") {
        node.onmouseover=function() {
          this.className+=" over";
          this.style.zIndex=200;
        }
        node.onmouseout=function() {
          this.className=this.className.replace(" over", "");
          this.style.zIndex=0;
        }
      }
    }
  }
}

var themeLabels = new Array();
themeLabels[0] = 'explorer';
themeLabels[1] = 'growing';
themeLabels[2] = 'ebb';
themeLabels[3] = 'art';
themeLabels[4] = 'hiddenbeauty';
var themeNames = new Array();
themeNames[0] = 'Explorer';
themeNames[1] = 'Growing Respect';
themeNames[2] = 'Ebb and Flow';
themeNames[3] = 'Art Form';
themeNames[4] = 'Hidden Beauty';
var styleset = 0;

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;
        styleset = 1;
      }
    }
  }
}
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,hours) {
  if (hours) {
    var date = new Date();
    date.setTime(date.getTime()+(hours*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;
}

function attachContactSearch() {
  if (document.getElementById('regionselect')) {
    document.getElementById('regionselect').innerHTML = '<label for="region">Region:</label><select id="region" name="regionId"><option value="none" selected="selected">Please select</option><option value="4000883" class="africa">Middle East and Africa</option><option value="4000685" class="europe">Europe</option><option value="4000690" class="asia">Asia and Pacific</option><option value="4000677" class="northamerica">North America</option><option value="4000881" class="latinamerica">Latin America</option></select>';
  }
  if (document.getElementById('region') && document.getElementsByTagName) {
    var sel1 = document.getElementById('region');
    var sel2 = document.getElementById('country');
    var clone = sel2.cloneNode(true);
    var clonedOptions = clone.getElementsByTagName("option");
    sel1.onchange = function() {
      refreshCountryOptions(sel1,sel2,clonedOptions);
    };
    sel2.onchange = function() {
      refreshRegionOptions(sel1,sel2);
        refreshUserOptions(sel2);
    };
  }
}
function refreshRegionOptions(sel1,sel2) {
  var pattern = sel2.options[sel2.selectedIndex].className;
  for (var i = 0; i < sel1.length; i++) {
    if (sel1[i].className.match(pattern)) {
      sel1[i].selected = 'true';
    }
  }
}
function refreshUserOptions(sel2) {
    var defaultCountryUserTypes = countryUserTypeArray[sel2.value];
    if (defaultCountryUserTypes) {
        DWRUtil.removeAllOptions("user");
        if (defaultCountryUserTypes == "all") {
            DWRUtil.addOptions("user", { all:'Consumer and Professional', consumer:'Consumer', professional:'Professional/Business' });
        } else if (defaultCountryUserTypes == "consumer") {
            DWRUtil.addOptions("user", { consumer:'Consumer'});
        } else if (defaultCountryUserTypes == "professional") {
            DWRUtil.addOptions("user", { professional:'Professional' });
        }
    }
}
function refreshCountryOptions(sel1,sel2,clonedOptions) {
  while (sel2.options.length) {
    sel2.remove(0);
  }
  if (sel1.options[sel1.selectedIndex].value != 'none') {
    var pleaseselect = new Option('Please select','none');
    sel2.options[0] = pleaseselect;
    var pattern1 = /( |^)(select)( |$)/;
    var pattern2 = new RegExp("( |^)(" + sel1.options[sel1.selectedIndex].className + ")( |$)");
    for (var i = 0; i < clonedOptions.length; i++) {
      if (clonedOptions[i].className.match(pattern1) || clonedOptions[i].className.match(pattern2)) {
        sel2.appendChild(clonedOptions[i].cloneNode(true));
      }
    }
  } else {
    for (var i = 0; i < clonedOptions.length; i++) {
      sel2.appendChild(clonedOptions[i].cloneNode(true));
    }
  }
}

function attachStyleSwitch() {
  if (document.getElementById('styleswitch-sub')) {
    var styleLinks = new Array();
    for (var i = 0; i < themeLabels.length; i++) {
      styleLinks[i] = createStyleString(themeLabels[i],themeNames[i]);
    }
    orderStyleLinks(styleLinks);
//    document.getElementById('styleswitch-sub').onmouseout = function() {
//      orderStyleLinks(styleLinks);
//    }
  }
  if (document.getElementById('homelinks3')) {
    var homeStyleLinks = '<h2><img src="/images/logo-colourfutures.gif" width="117" height="19" alt="COLOUR FUTURES&trade;" /></h2><ul>';
    for (var i = 0; i < themeLabels.length; i++) {
      homeStyleLinks += createStyleString(themeLabels[i],themeNames[i]);
    }
    homeStyleLinks += '</ul>';
    homeStyleLinks += '<p><a href="/colourfutures/index.jsp">Read about COLOUR FUTURES&trade;</a></p>';
    document.getElementById('homelinks3').innerHTML = homeStyleLinks;
  }
}
function createStyleString(themeLabel,themeName) {
  var styleString = '<li><a href="#" onclick="javascript:setActiveStyleSheet(\''+themeLabel+'\');return false;" class="'+themeLabel+'">'+themeName+'</a></li>';
  return styleString;
}

function orderStyleLinks(styleLinks) {
  var currentStyle = getActiveStyleSheet();
  while (!styleLinks[0].match(currentStyle)) {
    var styleLink = styleLinks[0];
    for (var i = 1; i < styleLinks.length; i++) {
      styleLinks[i-1] = styleLinks[i];
    }
    styleLinks[styleLinks.length-1] = styleLink;
  }
  document.getElementById('styleswitch-sub').innerHTML = '<ul>'+styleLinks[0]+styleLinks[1]+styleLinks[2]+styleLinks[3]+styleLinks[4]+'</ul>&nbsp;';
}

function attachFormValidation() {
  if (document.forms) {
    for (var i=0;i<document.forms.length;i++) {
      fineForm = document.forms[i];
      fineForm.onsubmit = function () {
        return validateForm(fineForm);
      }
    }
  }
}
var W3CDOM = (document.getElementsByTagName && document.createElement);
function validateForm(fineForm) {
  validForm = true;
  firstError = null;
  errorstring = '';
  var x = fineForm.elements;
  for (var i=0;i<x.length;i++) {
    if ((!x[i].value || (x[i].value == 'none')) && (x[i].className.indexOf('validate') != -1)) writeError(x[i],'This field is required');
  }
  if (x['email']) {
    if ((x['email'].value.indexOf('@') == -1) || (x['email'].value.indexOf('.') == -1)) writeError(x['email'],'This is not a valid email address');
  }
  if (!W3CDOM) alert(errorstring);
  if (firstError) {
    firstError.focus();
  }
  if (validForm) {
    return true;
  } else {
    return false;
  }
}
function writeError(obj,message) {
  validForm = false;
  if (obj.hasError) return;
  if (W3CDOM) {
    obj.className += ' error';
    obj.onchange = removeError;
    var sp = document.createElement('div');
    sp.className = 'error';
    sp.appendChild(document.createTextNode(message));
    obj.parentNode.appendChild(sp);
    obj.hasError = sp;
  } else {
    errorstring += obj.name + ': ' + message + '\n';
    obj.hasError = true;
  }
  if (!firstError) firstError = obj;
}
function removeError() {
  this.className = this.className.substring(0,this.className.lastIndexOf(' '));
  this.parentNode.removeChild(this.hasError);
  this.hasError = null;
  this.onchange = null;
}

function init() {
  startList('navbar');
  startList('styleswitch');
  var cookie = readCookie("style");
  var title = cookie ? cookie : getPreferredStyleSheet();
  setActiveStyleSheet(title);
  if (!styleset) setActiveStyleSheet(themeLabels[0]);
  attachContactSearch();
  attachStyleSwitch();
  attachFormValidation();
}

function drop() {
  var title = getActiveStyleSheet();
  createCookie("style", title, 4);
}

window.onunload=drop;
