r_escape = function(text) {
  if (!arguments.callee.sRE) {
    var specials = [
      '/', '.', '*', '+', '?', '|',
      '(', ')', '[', ']', '{', '}', '\\'
    ];
    arguments.callee.sRE = new RegExp(
      '(\\' + specials.join('|\\') + ')', 'g'
    );
  }
  return text.replace(arguments.callee.sRE, '\\$1');
}

Array.prototype.in_array = function(search_term) {
  var i = this.length;
  if (i > 0) {
	 do {
		if (this[i] === search_term) {
		   return true;
		}
	 } while (i--);
  }
  return false;
}

function stripHTML(oldString) {
  return oldString.replace(/<&#91;^>&#93;*>/g, "");
}

function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function leftTrim(sString){
	while (sString.substring(0,1) == ' '){
		sString = sString.substring(1, sString.length);
	}
	return sString;
}

function rightTrim(sString){
	while (sString.substring(sString.length-1, sString.length) == ' '){
		sString = sString.substring(0,sString.length-1);
	}
	return sString;
}
function trimAll(sString){
	return leftTrim(rightTrim(sString));
}
function formError(obj){
	try{
	  Element.addClassName(obj, 'error');
	  obj.focus();
	  alert(obj.getAttribute("required"));
	  return false;
	}catch(e){};
}
function checkFormErrorRadios(obj){
  var form = obj.form;
  for( var i=0; i<form.elements.length; i++){
    if( (obj.name == form.elements[i].name) && (form.elements[i].checked) ) return true;
  }
  return formError(obj);
}

function autoCheck_onSubmit(form){
  var checkedradios = [];
  var found = false;
  for(var k=0; k<form.elements.length; k++){
    if(Element.hasClassName(form.elements[k], 'required') ){
      if( ((form.elements[k].type.toLowerCase()=='text') || (form.elements[k].type.toLowerCase()=='textarea')) && (form.elements[k].value.length<1) ){
        return formError(form.elements[k]);
      }else
        if((form.elements[k].type.toLowerCase()=='checkbox') && (!form.elements[k].checked) ){
          if( form.elements[k].name.indexOf('[]')>-1 ){
            return checkFormErrorRadios(form.elements[k]);
          }else return formError(form.elements[k]);
        }else
          if((form.elements[k].type.toLowerCase()=='radio') && (1) ){
            found = false;
            if(checkedradios.length)
              for(t in checkedradios){
                if(checkedradios[t] == form.elements[k].name) found = true;
              }
            if(!found){
              checkedradios.push(form.elements[k].name);
              return checkFormErrorRadios(form.elements[k]);
            }
          }else
            if( ((form.elements[k].type.toLowerCase()=='select-one') || (form.elements[k].type.toLowerCase()=='select-multiple')) ){
              if(form.elements[k].selectedIndex == -1)
                return formError(form.elements[k]);
              if(form.elements[k].value < 1)
                return formError(form.elements[k]);
            }
    }
  }
  return true;
}

function autoCheckForms(){
  for(var i=0; i< document.forms.length; i++){
    for(var k=0; k<document.forms[i].elements.length; k++){
      if(Element.hasClassName(document.forms[i][k], 'required')){
        if(typeof document.forms[i].onsubmit != 'function')
          document.forms[i].onsubmit = function(){return autoCheck_onSubmit(this)}
      }
    }
  }
}

Event.observe(window, 'load', autoCheckForms, false);

function putRemove(obj){
  obj = $(obj);
  if( (obj.style.display=="")){
      obj.style.display="none";
  }else{
      obj.style.display="";
  }
}
function show(obj){
  $(obj).style.display="";
}
function hide(obj){
  $(obj).style.display="none";
}
function addClass(obj, cls){
  Element.addClassName(obj, cls);
}
function removeClass(obj, cls){
  Element.removeClassName(obj, cls);
}
function toggleClass(obj, cls){
  Element.toggleClassName(obj, cls);
}
function changeClass(obj, cls1, cls2){
  Element.removeClassName(obj, cls1);
  Element.addClassName(obj, cls2);
}

function timer(obj, interval, url){
  obj = $(obj);
  if(parseInt(obj.innerHTML-1)>=0){
    setTimeout("timer('"+obj.id+"',"+interval+",'"+url+"')", interval*1000);
    obj.innerHTML = parseInt(obj.innerHTML-interval);
  }else{
    if(url.length){
      location.href = url;
    }
  }
}

function openWindow(document,name,width,height) {
   window.open(document,name,'width='+width+',height='+height+',scrollbars=yes,resizable=yes,toolbar=no,directories=no,location=no,menubar=no,status=no,left=100,top=100');
}

function formCheckBox(form, flag){
	form = $(form);
	if(!flag) flag=false;
		else flag=true;
	for(i=0; i<form.elements.length;i++){
		if('checkbox' == form[i].type) form[i].checked = flag;
	}
}

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function showSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

function hideSelectBoxes(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

var mousex = 0;
var mousey = 0;

Event.observe(document, 'mousemove', function(e){ try{ mousex=Event.pointerX(e);mousey=Event.pointerY(e); }catch(e){};}, false);

function fieldToggleFocused(obj, type){
	if(type) Element.addClassName(obj, "focused");
	if(!type) Element.removeClassName(obj, "focused");
}
function setFocusFields(){
	$A(document.getElementsByTagName("input")).each( function(inp){
			if(inp.type=='text' || inp.type=='password'){
				Event.observe(inp, "focus", function(){fieldToggleFocused(inp, 1)});
				Event.observe(inp, "blur", function(){fieldToggleFocused(inp, 0)});
			}
		}
	);
}

//Event.observe(window, "load", function(){setFocusFields();});


var Base64 = {

    // private property
    _keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

    // public method for encoding
    encode : function (input) {
        var output = "";
        var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
        var i = 0;

        input = Base64._utf8_encode(input);

        while (i < input.length) {

            chr1 = input.charCodeAt(i++);
            chr2 = input.charCodeAt(i++);
            chr3 = input.charCodeAt(i++);

            enc1 = chr1 >> 2;
            enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
            enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
            enc4 = chr3 & 63;

            if (isNaN(chr2)) {
                enc3 = enc4 = 64;
            } else if (isNaN(chr3)) {
                enc4 = 64;
            }

            output = output +
            this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
            this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

        }

        return output;
    },

    // public method for decoding
    decode : function (input) {
        var output = "";
        var chr1, chr2, chr3;
        var enc1, enc2, enc3, enc4;
        var i = 0;

        input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

        while (i < input.length) {

            enc1 = this._keyStr.indexOf(input.charAt(i++));
            enc2 = this._keyStr.indexOf(input.charAt(i++));
            enc3 = this._keyStr.indexOf(input.charAt(i++));
            enc4 = this._keyStr.indexOf(input.charAt(i++));

            chr1 = (enc1 << 2) | (enc2 >> 4);
            chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
            chr3 = ((enc3 & 3) << 6) | enc4;
            output = output + String.fromCharCode(chr1);
            if (enc3 != 64) {
                output = output + String.fromCharCode(chr2);
            }
            if (enc4 != 64) {
                output = output + String.fromCharCode(chr3);
            }
        }
        output = Base64._utf8_decode(output);
        return output;
    },
    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },
    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;
        while ( i < utftext.length ) {
            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }
        }
        return string;
    }
}