/* Basic functions - some need jQuerry */

// trim
String.prototype.trim = function()
{
	return this.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}

// wordwrap
String.prototype.wordWrap = function(m, b, c){
	var i, j, l, s, r;
	if(m < 1)
		return this;
	for(i = -1, l = (r = this.split("\n")).length; ++i < l; r[i] += s)
		for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : ""))
			j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length
			|| c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
	return r.join("\n");
};

// scroll to id in element
function scrollToPos(id, elem)
{
	if(elem == null)
		elem = window;
	
	var elem2	 = elem.getElementById(id);
	var xpos	 = 0;
	var ypos	 = 0;
	
	while(elem2 != null && typeof(elem2) == 'object' && typeof(elem2.tagName) != 'undefined' && (elem.id == null || elem2.id != elem.id))
	{
		xpos	+= elem2.offsetLeft;
		ypos	+= elem2.offsetTop;
		
		elem2 = elem2.offsetParent;
	}
	
	elem.scrollTo(xpos,ypos);
}

// set info text in input  if needed
function setInfoText(element, infoText, infoClass){
    infoClass	 = infoClass || 'infoText';
    element		 = $(element);
	
	
	element.addEvent('focus', function()
    {
        if ($(this).value == infoText)
        {
            $(this).removeClass(infoClass);
            $(this).value	 = '';
        }
    });
    
    element.addEvent('blur', function()
    {
        if ($(this).value == '')
        {
            $(this).addClass(infoClass);
            $(this).value	 = infoText;
        }
    });
    
	if(element.value == '')
	{
		element.addClass(infoClass);
		element.value	 = infoText;
	}
	
	var form	 = element;
	
	while(form.nodeName != 'FORM')
	{
		form	 = form.parentNode;
	}
	
	form.addEvent('submit', function() {
		if(element.value == infoText) {
			element.value	 = '';
		}
	});
};

// empty Form
function emptyForm(id) {
	var cnt = document.getElementById(id).getElementsByTagName('input').length;
	
	for(i=0; i < (cnt-2); i++) {
		document.getElementById(id).getElementsByTagName('input')[i].set('value', '');
	}
}

// highlight
function highlightBox(id, interval, num) {
	if(interval == null)
		interval = 300;
	
	if(num == null)
		num = 5;
	
	if(num%2 == 1)
		$(id).style.borderColor	 = 'blue';
	else
		$(id).style.borderColor	 = '';
	
	if(num > 0)
	{
		num	 = num-1;
		setTimeout(function(){highlightBox(id, interval, num);},interval);
	}
}
