String.prototype.trim = function() { return (this.replace(/^[\s]+/g,"")).replace(/[\s]+$/g,"") }  		
String.prototype.trimNull = function() { var s = this.trim(); return  (s == null || s == "")  ? null : s; }
String.prototype.isEmail = function() { 
	if (!this.trimNull()) return false
	var re = /^[^\s()<>@,;:\/]+@\w[\w\.-]+\.[a-z]{2,}$/i
  	return re.test(this)
}
		
function input_focus(e) { e.style.backgroundColor='#1c530a' }
function input_blur(e) { e.style.backgroundColor='#000000'}

function getCookie( name ) {
	var start = document.cookie.indexOf( name + "=" );
	var len = start + name.length + 1;
	if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) ) {
		return null;
	}
	if ( start == -1 ) return null;
	var end = document.cookie.indexOf( ";", len );
	if ( end == -1 ) end = document.cookie.length;
	return unescape( document.cookie.substring( len, end ) );
}

function setCookie( name, value, expires, path, domain, secure ) {
	var today = new Date();
	today.setTime(today.getTime());
	if (expires) {
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name+"="+escape( value ) +
		( ( expires ) ? ";expires="+expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) +
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function openImage(img,wname) {
	var w = open('',(wname) ? wname:'_blank','menubar=0,status=0,location=0,toolbar=0,titelbar=0,resizable=1,scrollbars=1')
    var d = w.document
    d.open()
    d.write('<body style="background-color:#000;">')
    d.write('<img src="' + img.src + '" border="0"/>');
    d.write('</body>')
    d.close()
    w.focus()
}

function pad(s) {
	s+=''
	return (s.length == 1) ? '0'+s:s
}



function setupForm(f) {
	var fields = $(f).elements
    for (var i = 0; i < fields.length; ++i) {
    	var f = fields[i]
        var type = f.type.toLowerCase()
        if (type == 'text' || type == 'textarea' || type == 'password' || type == 'file') {
         	f.onfocus=new Function("input_focus(this)")
            f.onblur =new Function("input_blur(this)")
         }
     }
}

function setupButtons() {
	var btns = document.getElementsByClassName('btn')
	for (var i = 0; i < btns.length; ++i) {
		btns[i].onmouseover=new Function("this.className='btn_hover'" )
		btns[i].onmouseout=new Function("this.className='btn'")
	}
}


function setDisabler(el) {
	if (el.disabler) {
   		el.disabler.style.display='block'
        return
    }
    var dis = document.createElement('div')
    dis.style.position='absolute'
    dis.style.zIndex = 1000
         
    var p = Position.page(el)
    var dim = Element.getDimensions(el);
    dis.style.left = p[0]+'px'
    dis.style.top = p[1]+'px'
    dis.style.width = dim.width+'px'
    dis.style.height = dim.height+'px'
    dis.style.display='block'               
    dis.style.backgroundColor='transparent'
    document.body.insertBefore(dis,document.body.firstChild)
    el.disabler=dis    
}


function disableForm(f,contName) {
	f=$(f)
	if (!f) return
	Form.disable(f)
	var btns = $A(f.getElementsByTagName('button'))
	btns.each(function(btn) { btn.disabled = true })		
	contName = contName || f.id+'_container'	
    var cont = $(contName) || f
    try {
    	new Effect.Opacity(cont,{from:1.0,to:0.5})
    	//if (!f.fxOpacity) f.fxOpacity = new fx.Opacity(cont)
    	//f.fxOpacity.setOpacity(0.5)
    }
    catch (err) {}
    //setDisabler(cont)     
}

function enableForm(f,contName) {
	f=$(f)
	if (!f) return
	Form.enable(f)
	var btns = $A(f.getElementsByTagName('button'))
	btns.each(function(btn) { btn.disabled = false })			
	contName = contName || f.id+'_container'
    var cont = $(contName) || f        
    try {
    	new Effect.Opacity(cont,{from:0.5,to:1.0})    	
    }
    catch (err) {}
    //if (cont.disabler) cont.disabler.style.display='none'
}


var Olsa = {       
	cloneNode: function(ele,addition)  {
	
  	  	ele = $(ele)
  		if (ele == null) return null;
  		var clone = ele.cloneNode(true);
  		this.rewriteIds(clone,addition);
  		ele.parentNode.insertBefore(clone, ele);
  		return clone;
	},

	rewriteIds: function(ele,addition) {
  		if (ele == null) return;
  		if (ele.id) {
  			(addition) ? ele.id += addition : ele.removeAttribute("id");
  		}
  		var children = ele.childNodes;
  		for (var i = 0; i < children.length; i++) {
    		var child = children.item(i);
    		if (child.nodeType == 1) {
      			this.rewriteIds(child,addition);
    		}
  		}
	},
	
	childByName: function (name,el,deep) {
		el = $(el) || window.document.body
		for (var i=0; i < el.childNodes.length; ++i) {
			var nd = el.childNodes[i]
			if (nd.nodeType == 1) {
				if (nd.getAttribute('name') == name) return nd
			}
		}
		deep = deep || false
		if (deep) {
			for (var i=0; i < el.childNodes.length; ++i) {	
				if (el.childNodes[i].nodeType == 1) {
					var result = this.childByName(name,el.childNodes[i],true)
					if (result) return result
				}			
			}
		}
		return null
	}	
}

       