/** Common functions for all pages
  * Author : Ahamed Aslam Kannanari
  **/
//global variables
window.acms = new Object();

lib = new Object();
function printObject(obj)
{
	var str='';
	for(var i in obj)
	{
		str+=(i+":"+typeof(i)+":"+obj[i]+"\n")	
	}
	alert(str);
}
lib.getAbsoluteLeft = function(objectId) {
	// Get an object left position from the upper left viewport corner
	// Tested with relative and nested objects
	o = getElement(objectId)
	oLeft = o.offsetLeft            // Get left position from the parent object
	while(o.offsetParent!=null) {   // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent    // Get parent object reference
		oLeft += oParent.offsetLeft // Add parent left position
		o = oParent
	}
	// Return left postion
	return oLeft
}

lib.getAbsoluteTop = function (objectId) {
	// Get an object top position from the upper left viewport corner
	// Tested with relative and nested objects
	o = getElement(objectId)
	oTop = o.offsetTop            // Get top position from the parent object
	while(o.offsetParent!=null) { // Parse the parent hierarchy up to the document element
		oParent = o.offsetParent  // Get parent object reference
		oTop += oParent.offsetTop // Add parent top position
		o = oParent
	}
	// Return top position
	return oTop
}

function getElement(elemid) {
	var res = null;
	if(typeof(document.getElementById) != 'undefined' && (res = document.getElementById(elemid)) != null){
    	return res;
	}
	if(typeof(document.all) != 'undefined') {
		return document.all[elemid];
	}
	if(typeof(document.getElementsByName) != 'undefined')
	{
		res = document.getElementsByName(elemid);
		if(res.length > 1)
		{
			return res;
		}
		if(res.length > 0)
		{
			return res[0];
		}
	}
	return null;
}

if(typeof($) == 'undefined')
{
	$ = getElement;
}

if(typeof($$) == 'undefined')
{
	$$ = function(objID) {
		var o = $(objID);
		return o == null ? '' : o.value;
	}
}
if(typeof(_) == 'undefined')
{
	_ = getElement;
}

if(typeof(__) == 'undefined')
{
	__ = function(objID) {
		var o = _(objID);
		return o == null ? '' : o.value;
	}
}
function wordWrap(type,classname, width){
	if(navigator.userAgent.indexOf("MSIE") != -1 ){
		return;
	}
    var larg_total;
    var elementos,quem,texto;
    
    elementos = document.getElementsByTagName(type)
    
    var elems = new Array();
    for(var i=0; i<elementos.length;i++){
        if(elementos[i].className==classname){
            elems[i] = elementos[i].innerHTML;
            elementos[i].innerHTML = ""
         }
     }
     for(var i=0; i<elementos.length;i++){
        if(elementos[i].className==classname){
            quem = elementos[i];
            quem.innerHTML = elems[i];
            texto = quem.innerHTML;

            larg_total = quem.offsetWidth;

            quem.innerHTML = " "
            if(larg_total>width){
				var div = document.createElement('span');
	            div.innerHTML = "";
				quem.appendChild(div);
	            for(var j=0; j<texto.length;j++)
	            {
		            div.innerHTML = div.innerHTML + texto.charAt(j);
		            if(div.offsetWidth >= width)
		            {
		            	var inner = div.innerHTML;
			            div.innerHTML = inner.substring(0,inner.length - 1) + " " + texto.charAt(j);
			        }
	            }
            }else{
                quem.innerHTML = texto;
            }
        }
    }
}

function getSelectedAsCSV(field)
{
	var o = null;
	if(typeof(field) == 'string')//if it the field name
	{
		o = _(field).options;
	}else{//if it is an object
		o = field.options;
	}
	var res = '';
	for(i = 0; i < o.length; i++)
	{
		if(o[i].selected) res = res + o[i].value + ',' ;
	}
	if(res != '') res = res.substring(0,res.length - 1);
	return res;
}

function selectOptionsFromCSV(field, csv)
{
	if(csv == '')
	{
		return;
	}
	var o = null;
	if(typeof(field) == 'string')//if it the field name
	{
		o = _(field).options;
	}else{//if it is an object
		o = field.options;
	}
	var ops = csv.split(',');
	for(var j=0; j<ops.length; j++)
	{
		var op = ops[j];
		for(var i = 0; i < o.length; i++)
		{
			if(o[i].value == op) o[i].selected = true;
		}
	}
}

function getLevelValueText(nodename)
{
	var o = getElement(nodename);
	if(typeof(o) == 'undefined')
		return '';
	if(o.selectedIndex == 0)
		return '';
	else
		return o.options[o.selectedIndex].text;
}
window.acms.getOption = function(selobj,opname)
{
	var o = selobj.options;
	var len = o.length;
	for(i = 0; i < len; i++)
	{
		if(o[i].value == opname)
		{
			return i;
		}
	}
	return null;
}

//this is a generic event handler, for both ie & firefox
//this does not hinder the the eexisting event handlers
window.acms.addEventHandler = function(event,handler)
{
	//setup onload function
	if(typeof window.addEventListener != 'undefined')
	{
		//.. gecko, safari, konqueror and standard
		window.addEventListener(event, handler, false);
	}else if(typeof window.attachEvent != 'undefined')
	{
		//.. win/ie
		window.attachEvent('on'+event, handler);
	}
}

window.acms.isEmpty = function(o)
{
	if(typeof(o) == 'undefined' || o == null || o == '')
	{
		return true;
	}
	return false;
}
