/*
Filename: common.js
Created:
  2008/01/14 - HM

Description:
  JavaScript functions generally useful for EVERY application
  
Revisions:
*/

// add a function to a loadevent
function addLoadEvent(func)
{	var oldonload = window.onload;
    if (typeof window.onload != 'function')
    {   window.onload = func;
    }
    else
    {	window.onload = function()
    	{	if (oldonload)
    		{   oldonload();
            }
            func();
        }
    }
}

// force a redraw of the page
function redrawPage()
{	var newBodyClass = " abcdefghijklm ";
	document.body.className += newBodyClass;
	document.body.className = document.body.className.replace(newBodyClass,'');
}

/****************************
	BROWSER DETECTION
****************************/
function isIE()
{	return navigator.appName=="Microsoft Internet Explorer" && !window.opera;
}
function isIELess7()
{	return isIE() && !window.XMLHttpRequest;
}
function isIE7()
{	return isIE() && window.XMLHttpRequest;
}

/***************************
	COOKIE MANAGMENT
****************************/
// function setCookie
function setCookie(c_name, value, expiredays, path)
{	var exdate= new Date();
	if (expiredays==null)
		expiredays = 0;
	exdate.setDate(exdate.getDate()+ expiredays);
	var expires = (expiredays==null) ? "" : ";expires=" + exdate.toGMTString();
	if (path==null)
		path = '/';
	document.cookie = c_name + "=" + escape(value) + expires  + "; path=" + path;
//alert(document.cookie);
}

// function getCookie
function getCookie(c_name, defaultValue)
{	//alert(document.cookie);
	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));
    	} 
  	}
  	if (defaultValue)
  		return defaultValue;
	return "";
}

/***************************
	DOM MANAGMENT
****************************/
function dump(obj)
{	var alertString = "";
	for (var x in obj)
	{	alertString += x + ": " + typeof x + "\r\n";
	}
	alert(alertString);
}

function getIFrameDocument(iFrame)
{	var ret = null;
	try
	{	if (iFrame.contentWindow && iFrame.contentWindow.document)
			ret = iFrame.contentWindow.document;
		else if (iFrame.contentDocument)
			ret = iFrame.contentDocument;
	}
	catch(e)
	{
	}
	
	return ret;
}

// tag "*" may result in problems if scripts are on other server than the html-page (fragment-interface, IE, Opera) 
function getElementsByClassName(clsName, domObject, tag)
{	var retVal = new Array();
    var elements = null;
    var useTag = "*";
    
    if (tag)
    	useTag = tag;
    try
    {	elements = domObject.getElementsByTagName(useTag);
    }
    catch(e)
    {	elements = document.body.getElementsByTagName(useTag);
    }

    for (var i = 0;i < elements.length; i++)
    {	
    	if(elements[i].className.indexOf(" ") >= 0)
    	{	var classes = elements[i].className.split(" ");
            for (var j = 0;j < classes.length;j++)
            {	if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}

function scrollElementInView(obj)
{	try 
	{	if (!obj)
			return;
		
		var top = 0;
		if (isIE()) // IE scrolling
		{	if (document.all.detail)
				top = document.all.detail.offsetTop;
		}
		else // other browsers 
		{	top = 0;
			if (obj)
			do
			{	top += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		window.scroll(0, top );
	}
	catch(e){}
}

/***************************
	STRING MANAGMENT
****************************/
function trim(stringToTrim)
{   return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim)
{   return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim)
{   return stringToTrim.replace(/\s+$/,"");
}



function locationEndsWith(s) {
    return document.location.href.substr(document.location.href.length - s.length) == s;
}

// old function for historical reasons
function checkEmail(emailAddress)
{	return isValidEmail(emailAddress);
	
	/*var at = "@";
    var dot = ".";
    var lat = emailAddress.indexOf(at);
    var lstr = emailAddress.length;
    var ldot = emailAddress.indexOf(dot);

    if (emailAddress.indexOf(at) == -1) return false;
    if (emailAddress.indexOf(at) == -1 || emailAddress.indexOf(at) == 0 || emailAddress.indexOf(at) == lstr) return false;
    if (emailAddress.indexOf(dot) == -1 || emailAddress.indexOf(dot) == 0 || emailAddress.indexOf(dot) == lstr) return false;
    if (emailAddress.indexOf(at, (lat + 1)) != -1) return false;
    if (emailAddress.substring(lat - 1, lat) == dot || emailAddress.substring(lat + 1, lat + 2) == dot) return false;
    if (emailAddress.indexOf(dot, (lat + 2)) == -1) return false;
    if (emailAddress.indexOf(" ") != -1) return false;

    return true;*/
}
function isValidEmail(address)
{	try
	{	var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		return filter.test(address);
	}
	catch(e)
	{	return false;
	}
}


// appends a parameter to a url
function appendUrlParameter( url, parameter, value )
{
	if( !url )
	{
		url = "";
	}
	if( url.indexOf("?") == -1 )
	{
		url += "?";
	}
	else
	{
		url += "&";
	}
	
	return url + parameter + "=" + value;
}

