//
// Done by ViaTecla, P.P. 3/8/2004
// utils.js : generic lib
//

// opens a pop up win
function PopUp(aUrl, title, w, h, isResizable) {
	if ( arguments.length < 5 )
		isResizable = 0;
	var options = ",status=no,toolbar=no,menubar=no,location=no";
	options += ( isResizable == 1 ? ",resizable=yes" : ",resizable=no" );
	var w = window.open(aUrl, title, "height="+h+",width=" + w + options );
	if ( w != null ) {
		w.focus();
	}
}

function NewWindow(mypage, myname, w, h, scroll) 
{
	var winl = (screen.width - w) / 2;
    var wint = (screen.height - h) / 2;
    var winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable=no,location=no';
    var win = window.open(mypage, myname, winprops);
      
    if (parseInt(navigator.appVersion) >= 4 && win != null) 
    {
		if (win.window != null)
			win.window.focus(); 
	}
}

// resets a form given a name
function ResetForm(aFormName) {
	var f = document.getElementById(aFormName);
	if ( f!=null )
		f.reset();
}

// used on mouseover / mouseout without preload
function ChangeImgSrc(e, newImg) {
	e.src = newImg;
	return true;
}
function ChangeImgSrcById(aId, newImg) {
	return ChangeImgSrc(document.getElementById(aId),newImg);
}
// OnMouseOver Handle - to use with preload images
function over(aImgName) {
  if(document.images) {
    revert[aImgName] = document.images[inames[aImgName]].src;
    document.images[inames[aImgName]].src = flipped[aImgName].src;
  }
}
// OnMouseOut Handle - to use with preload images
function out(aImgName) {
  if(document.images) document.images[inames[aImgName]].src = revert[aImgName];
}
function formatNumberAsCurrency( aNumber ) {
	aNumber = String(aNumber).replace(",",".");
	var n = parseFloat( aNumber );
	var aux = n * 100;
	n = Math.round( aux ) / 100;
	var s = String( n );
	if( s.indexOf(".") == -1 )
		s += ".00";
	else {
		var i = s.indexOf(".");
		var len = s.length;
		i = len - i - 1;
		if(i<2)
			while( i-- )
				s += "0";
	}
	return s.replace(".",",");
}
function resizePopUp(str, isPopUser) {
	var obj = (document.all)? document.getElementById(str) : document.body;
	//get body size
	var w = obj.clientWidth + 50;
	if(isPopUser)
		w += 100;
	var h = obj.clientHeight + 50;
	if(isPopUser == "isCITemplate")
	{
		w = 600;
		h += 150;
	}
	if (!document.all)
	{
		h = (h*3);
		w = (w/2) - 5;
	}	
	//get screen disponibility
	if (h > screen.availHeight)
		h = screen.availHeight - 100;
				
	if (w > screen.availWidth)
		w = screen.availWidth - 100;
	//window.moveTo(50, 50);
	window.resizeTo(w,h);
}
function OnKeyPressEnter( aFunctionToCall ) {
	if ( event.keyCode == 13 )
		aFunctionToCall();
	return true;
}
function OnKeyPressEnterWithArg( aFunctionToCall, arg ) {
	if ( event.keyCode == 13 )
		aFunctionToCall(arg);
	return true;
}

function OnKeyPressEnterFirefox(evt, aFunctionToCall)
{
	var key;
	
	// The keyCode property in a event works in IE, not on Firefox.
	if (evt.keyCode)
		key = evt.keyCode;
	else
		key = evt.which;
		
	if (key == 13)
		aFunctionToCall();
	return true;
}

function OnKeyPressEnterWithArgFirefox(evt, aFunctionToCall, arg)
{
	var key;
	
	// The keyCode property in a event is works in IE, not on Firefox.
	if (evt.keyCode)
		key = evt.keyCode;
	else
		key = evt.which;
		
	if (key == 13)
		aFunctionToCall(arg);
	return true;
}

function GetPageCoords(element)
{
     var coords = {x: 0, y: 0};
     while (element)
     {
       coords.x += element.offsetLeft;
       coords.y += element.offsetTop;
       element = element.offsetParent;
     }
     return coords;
}

function GetPageCoordsById(elementId)
{
	if (document.all)
	{
       return GetPageCoords(document.all[elementId]);
    }
    else if (document.getElementById)
    {
		return GetPageCoords(document.getElementById(elementId));
    }    
}
