
var utils = new Object();

utils.rtrim = function(str){
  while(str.charAt((str.length -1))==" "){
    str = str.substring(0,str.length-1);
  }
  return str;
}


utils.addEvent = function(obj, eventName, handler)  {
  if (obj==null) return;
  if(window.addEventListener) { // Mozilla, Netscape, Firefox
  	obj.addEventListener(eventName, handler, false);
  } 
  else { // IE
  	obj.attachEvent('on' + eventName, handler);
  }
}

utils.removeEvent = function(obj, eventName, handler) {
  if(window.removeEventListener) { // Mozilla, Netscape, Firefox
  	obj.removeEventListener(eventName, handler, false);
  } 
  else { // IE
  	obj.detachEvent('on' + eventName, handler);
  }
}

utils.preventEvent = function(e) {
  e = e || window.event;
  if(e.preventDefault){
    e.preventDefault();
    e.stopPropagation();
  }
  else{
    e.cancelBubble = true;
    e.returnValue = false;
  }
};


utils.getCursorPosition = function(obj) { 
  var cur;
  if (document.selection!=null) {
    if (obj==null) obj = document.activeElement; 
    cur = document.selection.createRange(); 
  }
  var pos = 0; 
  if (obj!=null && cur!=null && obj.createTextRange!=null) {  // IE
    try {
      var tr = obj.createTextRange(); 
      if (tr) { 
        while (cur.compareEndPoints("StartToStart", tr) > 0) { 
          tr.moveStart("character", 1); 
          pos++; 
        } 
        return pos; 
      } 
    }
    catch(e) {
      return -1;
    }
  } 
  else if (obj!=null && obj.selectionStart!=null) {  // Firefox
    return obj.selectionStart;
  }
  return -1; 
} 


utils.getTarget = function(e) {
	var targ;
	if (!e) var e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
  return targ;
}


utils.isRightClick = function(e) {
  if (!e) var e = window.event;
  if (e.which!=null) {
    if (e.which > 1) {
      return true;
    }
  }
  if (e.button!=null) {
    if (e.button > 1) {
      return true;
    }
  }
  return false;
}


utils.isEMailValid = function(str) {
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  if (str.indexOf(at)==-1) {
    return false;
  }
  
  if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) {
    return false;
  }
  
  if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) {
   return false;
  }
  
  if (str.indexOf(at,(lat+1))!=-1) {
    return false;
  }
  
  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) {
    return false;
  }
  
  if (str.indexOf(dot,(lat+2))==-1) {
    return false;
  }
  
  if (str.indexOf(" ")!=-1) {
    return false;
  }
  
  return true;			
}


utils.fadeIn = function(obj, opacity, increment, interval, initFunction) {
  if (browser.isIE && browser.version < 7) return;  // IE6 doesn't support fading
  if (opacity==null) opacity = 15;
  if (opacity > 100) opacitiy = 100;
  if (increment==null) increment = 30;
  if (interval==null) interval = 1;
  obj.style.opacity = opacity / 100;
  obj.style.filter = "alpha(opacity=" + opacity + ")";
  if (opacity < 100) {
    setTimeout(function() { utils.fadeIn(obj, opacity + increment, increment, interval, initFunction); }, interval);  
  }
  else {
    if (initFunction!=null) initFunction();
  }
}

utils.fadeOut = function(obj, opacity, decrement, interval, cleanupFunction) {
  if (browser.isIE && browser.version < 7) {  // IE6 doesn't support fading
    if (cleanupFunction!=null) {
      cleanupFunction();
    }
    else {
      obj.parentNode.removeChild(obj);
    }
    return;
  }
  if (opacity==null) opacity = 85;
  if (opacity < 0) opacitiy = 0;
  if (decrement==null) decrement = 10;
  if (interval==null) interval = 1;
  obj.style.opacity = opacity / 100;
  obj.style.filter = "alpha(opacity=" + opacity + ")";
  if (opacity > 0) {
    setTimeout(function() { utils.fadeOut(obj, opacity - decrement, decrement, interval, cleanupFunction); }, interval);  
  }
  else {
    // remove object after it has faded out
    if (cleanupFunction!=null) {
      cleanupFunction();
    }
    else {
      obj.parentNode.removeChild(obj);
    }
  }
}

utils.filterTypes =
[
	"numericslash",
	"numeric",
	"numericdash",
	"alpha",
	"alphaspace",
	"alphanumeric",
	"alphanumericspace",
	"license",
	"float",
	"fulladdress",
	"negfloat",
	"ccard",
	"alphaspacedash",
	"lastname",
	"alphanumericspacedash",
];

utils.filterRegExp =
[
	/[0-9\/]/,
	/[0-9]/,
	/[0-9\-]/,
	/[a-z]/i,
	/[a-z\s]/i,
	/[a-z0-9]/i,
	/[a-z0-9\s]/i,
	/[a-z0-9\*]/i,
	/[0-9\.]/,
	/[a-z0-9\s\/\.\#]/i,
	/[0-9\.\-]/,
	/[0-9\s\-]/,
	/[a-z\s\-]/i,
	/[a-z\s\-\']/i,
	/[a-z0-9\s\-]/i,	
];

utils.keyFilter = function(type,e){
	var key;
	if (e.keyCode) key = e.keyCode;
	else if (e.which) key = e.which;
	else return true;
	if (key < 32) return true;
	if(!document.all){
		if(key == 37 || key == 39 || key == 46) return true;
	}
	var valid=true;
	var keyChar=String.fromCharCode(key);

	for(i=0;i<utils.filterTypes.length;i++){
		if(type==utils.filterTypes[i]){
			var pattern=utils.filterRegExp[1];
			valid=pattern.test(keyChar);
			break;
		}
	}
	if(!valid){
		e.returnValue=false;
		e.cancelbubble=true;
		return false;
	}
	return true;
}

utils.getCookie = function(c_name) {
  if (document.cookie.length>0) {
    c_start=document.cookie.indexOf(c_name + "=");
    if (c_start!=-1) { 
      c_start=c_start + c_name.length+1; 
      c_end=document.cookie.indexOf(";",c_start);
      if (c_end==-1) c_end=document.cookie.length;
      return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
  return "";
}

