//
// $Id: site.js 587 2007-04-16 22:25:52Z dtlrjt $
//
// Jeromes site javascript code
//

// Browser gestalt
Browser = {
 id		: function() {return navigator.userAgent.toLowerCase();},
 ver	: function() {
	var x = 0;
 	if (this.isIE()) {
		x = this.id().indexOf("msie ");
		ver = parseFloat(this.id().substring(x + 5));
	} else {
	  x = this.id().length - 1;
	  while (x >= 0 && this.id().charAt(x) != ' ' && this.id().charAt(x) != '/')
		  x--;
	  if (x >= 0)
		  ver = parseFloat(this.id().substring(x + 1));
	  else
		  ver = parseFloat(navigator.appVersion);
	}
  	return ver;
 },

 mozVer			: function() {return parseFloat(navigator.appVersion);},
 isAdvanced		: function() {return Boolean(this.hasDOM() && this.hasCreateElement());},

 isWindows		: function() {return this.id().indexOf("win")!=-1;},
 isMac			: function() {return this.id().indexOf("mac")!=-1;},

 isIE			: function() {return this.id().indexOf("msie")!=-1 && !this.isOpera();},
 isFirefox		: function() {return this.id().indexOf("firefox")!=-1;},
 isGecko		: function() {return this.id().indexOf("gecko")!=-1;},
 isOpera		: function() {return this.id().indexOf("opera")!=-1;},
 isSafari		: function() {return this.id().indexOf("safari")!=-1;},
 isOmniWeb		: function() {return this.id().indexOf("omniweb")!=-1;},
 isKonqueror	: function() {return this.id().indexOf("konqueror")!=-1;},
 isWebTV		: function() {return this.id().indexOf("webtv")!=-1;},
 isNetscape		: function() {return !this.isGecko() && this.id().indexOf("compatible")<0;},
 isMozilla		: function() {return this.isGecko() && !this.isFirefox() && !this.isSafari() && !this.isNetscape();},

 hasLayers		: function() {return Boolean(document.layers);},
 hasAll			: function() {return Boolean(document.all);},
 hasImages		: function() {return Boolean(document.images);},
 hasForms		: function() {return Boolean(document.forms);},
 hasCreateElement	: function() {return Boolean(document.createElement);},
 hasCreateDoc	: function() {return Boolean(document.implementation && document.implementation.createDocument);},
 hasRegExp		: function() {return Boolean(window.RegExp);},
 hasDOM			: function() {return Boolean(document.getElementById);},
 hasGEBI		: function() {return this.hasDOM();},
 hasGEBTN		: function() {return Boolean(document.getElementsByTagName);},
 hasActiveX		: function() {return Boolean(window.ActiveXObject);},
 WindowOnload	: function(f) {
	var prev = window.onload;
	window.onload = function() { if (prev) prev(); f(); }
 },

 // Browser-independent return of DOM element by ID attribute (most modern browsers)
 getElementById : function(id) {
    if (document.getElementById)
        return document.getElementById(id);
    else if (document.all)
        return document.all[id];

    alert('Browser not supported');
    return null;
 },

 // Get a style object for item by id
 getStyleObj : function(id) {
  var obj = this.findObj(id);
  if (obj) {
   if ((this.hasDOM() || this.hasAll()))
    return obj.style;
   else if (this.hasLayers())
    return obj;
  }
  alert("JavaScript error: could not find style object for '" + id + "'");
  return null;
 },

 // Set opacity (%) on an object by id or obj
 setOpacity : function(pct, id, obj) {
  if (id != '')
  	obj = this.findObj(id);
  if (obj.filters)
   obj.filters("alpha").opacity = pct;
  else if (obj.style) {
   var amt = pct / 100;
   if (amt > .99999)
   	amt = .99999;
   obj.style.MozOpacity = amt;
   obj.style.khtmlOpacity = amt;
   obj.style.opacity = amt;
  }
 },

 // get opacity (%) on an object by id or obj
 getOpacity : function(id, obj) {
  if (id != '')
  	obj = this.findObj(id);
  if (obj.filters)
   return obj.filters("alpha").opacity;
  else if (obj.style) {
  	var amt = obj.style.MozOpacity ||
			  obj.style.khtmlOpacity ||
   			  obj.style.opacity;

	return Math.round(amt * 100);
  }

 },

 // xGetElementsByTagName r4, Copyright 2002-2007 Michael Foster (Cross-Browser.com)
 // Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

 xGetElementsByTagName : function(t,p) {
   var list = null;
   t = t || '*';
   p = p || document;
   if (typeof p.getElementsByTagName != 'undefined') { // DOM1
	 list = p.getElementsByTagName(t);
	 if (t=='*' && (!list || !list.length)) list = p.all; // IE5 '*' bug
   }
   else { // IE4 object model
	 if (t=='*') list = p.all;
	 else if (p.all && p.all.tags) list = p.all.tags(t);
   }
   return list || new Array();
 },

 // xGetElementsByClassName r5, Copyright 2002-2007 Michael Foster (Cross-Browser.com)
 // Part of X, a Cross-Browser Javascript Library, Distributed under the terms of the GNU LGPL

 xGetElementsByClassName : function(c,p,t,f) {
  var r = new Array();
  var re = new RegExp("(^|\\s)"+c+"(\\s|$)");
//  var e = p.getElementsByTagName(t);
  var e = this.xGetElementsByTagName(t,p); // See xml comments.
  for (var i = 0; i < e.length; ++i) {
    if (re.test(e[i].className)) {
      r[r.length] = e[i];
      if (f) f(e[i]);
    }
  }
  return r;
}


}; // class Browser


// Validate form
//
// Input: form = form object
//        valid_set = array of strings containing item descriptors:
//
//			items = new Array("name:req/filter:msg");
//
// Where name is the name of a form item, req is "req" (for required)
// or "opt" (for optional), filter is one of "word", "words", "email", "phone"
// or "any", or a regular expression (e.g., "name:req//\\w+ \\w+/:...").
// msg is the error message displayed if the form item fails validation.

function validate(form, valid_set, msg_prefix) {
 var wrong = '';
 var wrong_copy;
 var first_bad = '';
 var is_radio;

 for (i = 0; i < valid_set.length; i++) {
  is_radio = false;
  wrong_copy = wrong;

  s = valid_set[i];

  p = s.indexOf(':');
  q = p + 1;
  name = s.substring(0, p);

  p = s.indexOf(':', q);
  filter = s.substring(q, p);
  msg = s.substr(p + 1) + "\n";

  p = filter.indexOf('/');
  q = p + 1;
  req = filter.substring(0, p);
  type = filter.substr(p + 1);

  eval('v = form.' + name + '.value;');

  if (v == undefined) { // probably a radio button
   eval('itemSet = form.' + name);
   is_radio = !!itemSet.length;
   for (x = 0; x < itemSet.length; x++) {
    if (itemSet[x].checked == true) {
     v = itemSet[x].value;
     break;
    }
    v = '';
   }
  }

  // trim spaces from ends
  while (((p = v.indexOf(' ')) == 0 || (p > 0 && p == (v.length - 1))))
   v = p ? v.substr(0, v.length - 1) : v.substr(1);

  if (v == '' && req == 'req')
   wrong += msg;
  else if (v != '') {
   switch (type) {

   case "any":
    break;

   case "word":
    if (v.indexOf(' ') != -1)
     wrong += msg;
    break;

   case "words":
    if (v.indexOf(' ') < 0)
     wrong += msg;
    break;

   case "email":
    var emf2 = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    var emailFilter = /^.+@.+\..{2,4}$/;
    var illegalChars = /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
    if (!(emailFilter.test(v)) || v.match(illegalChars))
     wrong += msg;
    break;

   case "phone":
    var isDigit = /[0123456789]/;
    var validChars = /[\(\)\s\.\-\+\/xX]/;
    var digits = 0;
    for (j = 0; j < v.length; j++) {
     c = v.charAt(j);
     if (c.match(isDigit))
      digits++;
     else if (!c.match(validChars))
      break;
    }
    if (digits < 10)
      wrong += msg;
    break;

   case "ccard":
    var cc = "", j, l, c, n = 0;
    for (j = 0; j < v.length; j++) {
      c = v.charAt(j);
      if (c >= '0' && c <= '9') cc += c;
    }
    if ((l = cc.length) >= 15) {
      for (j = 0; j < l; j++) {
	var d = (cc.charCodeAt(l - j - 1) - 48) * (j % 2 + 1);
	n += d > 9 ? d % 10 + 1 : d;
      }
      if (n % 10 == 0) {
	v.value = cc;
	break;
      } 
    }
    wrong += msg;
    break;

   default:
    // regexp
    eval('var re = ' + type + ';');
    ok = re.test(v);
    if (!ok)
     wrong += msg;
    break;
    
   }
  }

  if (wrong != wrong_copy && first_bad == '' && !is_radio)
	first_bad = name;
 }

 if (wrong != '') {
  alert(msg_prefix + wrong);
  eval('form.' + first_bad + '.focus()');
  return false;
 }

 return true;
}
