// Scripts used by MyMetCar.com
// AGC - 2009
function getElement (the_id) {
	if (typeof the_id != 'string') {
		return the_id;
	}
	if (typeof document.getElementById != 'undefined') {
		return document.getElementById(the_id);
	} else if (typeof document.all != 'undefined') {
		return document.all[the_id];
	} else if (typeof document.layers != 'undefined') {
		return document.layers[the_id];
	} else {
		return null;
	}
}

function EmailTest (emf, em) {
  var p;
  p = em.indexOf('@');
  if (p > 0 && p < (em.length-1)) {
    p = em.indexOf('.',p+1);
	if (p > 0 && p < (em.length-1)) {
	  return true;
	}
  }
  alert ('Please enter a complete e-mail address (e.g. name@domain.com)');
  emf.focus();
  emf.select();
  return false;
}

function CheckOneEmail (emailfield) {
  var emf, em;
  emf = getElement(emailfield);
  em = emf.value;
  return (EmailTest (emf, em));
}
  
function CheckEmails (email1field, email2field) {
  var em1f, em2f, em1, em2;
  em1f = getElement(email1field);
  em2f = getElement(email2field);
  em1 = em1f.value;
  em2 = em2f.value;
  if (EmailTest (em1f, em1) && EmailTest (em2f, em2)) {
    if (em1 == em2) {
	  return true;
	}
	alert ('e-mail addresses do not match');
	em1f.focus();
	em1f.select();
  }
  return false;
}

function CheckPasswords (pw1field, pw2field, required) {
	var pw1f, pw2f, pw1, pw2;
	pw1f = getElement(pw1field);
	pw2f = getElement(pw2field);
	pw1 = pw1f.value;
	pw2 = pw2f.value;
	if ((required) && (pw1 == '')) {
		alert ('You must select a password (it cannot be blank)');
		pw1f.focus();
		pw1f.select();
		return false;
	}
	if (pw1 != pw2) {
		alert ('Passwords do not match');
		pw1f.focus();
		pw1f.select();
		return false;
	}
	return true;
}

function CheckUserAndPasswords (ufield, pw1field, pw2field) {
	var uf, u;
	uf = getElement(ufield);
	u = uf.value;
	if (u == '') {
		alert ('User name may not be blank');
		uf.focus();
		uf.select();
		return false;
	}
	if (u.search('[^A-Za-z0-9\-_&\.\, ]') >= 0) {
		alert ('The user name you chose contains an invalid character. Letters, digits, spaces, and few punctuation characters are accepted');
		uf.focus();
		uf.select();
		return false;
	}
	return CheckPasswords (pw1field, pw2field);
}

function ValueOf(field) {
  var f;
  f = getElement(field);
  if (f) return (f.value); else return ('');
}

function isNum(str) {
   var test = "0123456789"
   for (i=0; i <= str.length-1; i++) {
      if (test.indexOf(str.charAt(i)) == -1) return false;
   }
   return true;
}

function NumValueOf(field) {
	var v = ValueOf(field);
	if (isNum(v)) return v; else return 0;
}

function isAlpha(str) {
   var test = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
   for (i=0; i <= str.length-1; i++) {
      if (test.indexOf(str.charAt(i)) == -1) return false;
   }
   return true;
}

function Trim(s) {
	return s.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

function ShowRow(rowid) {
	var r = getElement(rowid);
	if (r) r.style.display = '';
}

function HideRow(rowid) {
	var r = getElement(rowid);
	if (r) r.style.display = 'none';
}

// Thanks to Simon Willison (http://simonwillison.net/) for the following
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

// Tooltip class, to allow popup of cool, rounded, fading in/out tooltips when rolling over certain screen elements.
// Thanks to Michael Leigeber at sixrevisions.com
var tooltip=function(){
 var id = 'tt';
 var top = 3;
 var left = 3;
 var maxw = 300;
 var speed = 10;
 var timer = 20;
 var endalpha = 95;
 var alpha = 0;
 var tt,t,c,b,h;
 var ie = document.all ? true : false;
 return{
  show:function(v,w){
   if(tt == null){
    tt = document.createElement('div');
    tt.setAttribute('id',id);
    t = document.createElement('div');
    t.setAttribute('id',id + 'top');
    c = document.createElement('div');
    c.setAttribute('id',id + 'cont');
    b = document.createElement('div');
    b.setAttribute('id',id + 'bot');
    tt.appendChild(t);
    tt.appendChild(c);
    tt.appendChild(b);
    document.body.appendChild(tt);
    tt.style.opacity = 0;
    tt.style.filter = 'alpha(opacity=0)';
    document.onmousemove = this.pos;
   }
   tt.style.display = 'block';
   c.innerHTML = v;
   tt.style.width = w ? w + 'px' : 'auto';
   if(!w && ie){
    t.style.display = 'none';
    b.style.display = 'none';
    tt.style.width = tt.offsetWidth;
    t.style.display = 'block';
    b.style.display = 'block';
   }
  if(tt.offsetWidth > maxw){tt.style.width = maxw + 'px'}
  h = parseInt(tt.offsetHeight) + top;
  clearInterval(tt.timer);
  tt.timer = setInterval(function(){tooltip.fade(1)},timer);
  },
  pos:function(e){
   var u = ie ? event.clientY + document.documentElement.scrollTop : e.pageY;
   var l = ie ? event.clientX + document.documentElement.scrollLeft : e.pageX;
   tt.style.top = (u - h) + 'px';
   tt.style.left = (l + left) + 'px';
  },
  fade:function(d){
   var a = alpha;
   if((a != endalpha && d == 1) || (a != 0 && d == -1)){
    var i = speed;
   if(endalpha - a < speed && d == 1){
    i = endalpha - a;
   }else if(alpha < speed && d == -1){
     i = a;
   }
   alpha = a + (i * d);
   tt.style.opacity = alpha * .01;
   tt.style.filter = 'alpha(opacity=' + alpha + ')';
  }else{
    clearInterval(tt.timer);
     if(d == -1){tt.style.display = 'none'}
  }
 },
 hide:function(){
  clearInterval(tt.timer);
   tt.timer = setInterval(function(){tooltip.fade(-1)},timer);
  }
 };
}();
