
function showPopUp(name,width,height) 
{ 
    var div=getElement(name);

    
    if (width > 0) {div.width=width; div.style.width=width+"px";}
    if (height > 0){div.height=height;div.style.height=height+"px";}
    div.style.display = 'block';     

    centerDiv (div);
    showBackgroundFilter();
} 

function centerDiv (div) {

   var _top = (f_scrollTop() +  ((f_clientHeight() - div.offsetHeight) / 2));
   if (_top < 20) _top=20;
   div.style.top    = _top+"px";
   div.style.left   = (f_scrollLeft() + ((f_clientWidth()  - div.offsetWidth)  / 2))+"px";
}

function hidePopUp (name) {
    var div=getElement(name);
    div.style.display = 'none';   
    hideBackgroundFilter();
}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		 0
	);
}


function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}


function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function checkMaxLength (event,obj) { 

  var maxLength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")):0;

  if (maxLength > 0) {
    var key = event.keyCode; 
      if(key >= 33 || key == 13) { 
      var length = obj.value.length; 
      if(length >= maxLength)
        return false; 
    }
  }
  return true;
} 

function showBackgroundFilter() 
{ 
    var div=getElement('backgroundFilter');
    var page=getElement("page");
    div.style.height = f_scrollTop() + f_clientHeight()+"px";
    div.style.width  = f_scrollLeft() + f_clientWidth()+"px";
    div.style.display = 'block';
    
    
} 

function hideBackgroundFilter() 
{ 
    var div=getElement('backgroundFilter');
    div.style.display = 'none';
} 


function isExplorer () {
  return (window.navigator.appName == 'Microsoft Internet Explorer');
}


function getElement (nom) {
var control=null;
  control = document.getElementById(nom);
  if (control == null) control = document.getElementsByName(nom)[0];
  
  //if (control == null)
  //  alert ("Error : Control " + nom + " doesn't exists");
    
  return control;
}

function getForm (nom) {
  return document.forms[nom];
}

function makePostParams(form) {
	  var parameters = "";
	  var elements = form.elements;
	  for(i=0; i < elements.length; i++) {
	    if (elements[i].type == 'input') {
	      if (elements[i].clicked) {
	        parameters = parameters + "&"+elements[i].name+"="+encodeURI(elements[i].value);
	        elements[i].clicked=false;
	      }
	    }
	    else if (elements[i].type == 'checkbox') {
	      if (elements[i].checked)    
	        parameters = parameters + "&"+elements[i].name+"="+encodeURI(elements[i].value);
	    }
	    else {
	      parameters = parameters + "&"+elements[i].name+"="+encodeURI(elements[i].value);
	    }
	  }	
	  return parameters;
} 


function clearInnerHTML(obj) {
  while(obj.firstChild) obj.removeChild(obj.firstChild);
}

function fillInnerHTML (obj, content) {
  if (content != null) {
  	clearInnerHTML(obj);
    if (content != "") {
    	divTag = document.createElement('div');
    	divTag.style.display="inline";
    	divTag.innerHTML = content;
  		obj.appendChild(divTag);
  	}
  }
}


function setFloat (nom, value) {
  var control = getElement(nom);
  if (control.style.styleFloat) control.style.styleFloat=value;
  else control.style.cssFloat=value;
}

function dialog (uri,width,height) {
  var argArray = new Object();
  return xShowModalDialog( uri, argArray, "dialogWidth:"+width+"px;dialogHeight:"+height+"px;center:yes;scroll:yes;resizable:yes;status:no;help:no;");
} 

var modalWin=null;
function xShowModalDialog( sURL, vArguments, sFeatures )
{
  if (sURL==null||sURL=='') {
    alert ("Invalid URL input.");
    return false;
  }
  if (vArguments==null||vArguments=='')
    vArguments=''

  if (sFeatures==null||sFeatures=='')
    sFeatures=dFeatures;

  /*if (isExplorer()) {
    return window.showModalDialog ( sURL, vArguments, sFeatures );
  }*/


  sFeatures = sFeatures.replace(/ /gi,'');
  sFeatures = sFeatures.replace(/px/gi,'');
  aFeatures = sFeatures.split(";");
  sWinFeat = "directories=0,menubar=1,titlebar=0,toolbar=0,";
  var pHeight, pWidth;
  
  for ( x in aFeatures )
  {
    aTmp = aFeatures[x].split(":");
    sKey = aTmp[0].toLowerCase();
    sVal = aTmp[1];
    switch (sKey) {
      case "dialogheight":
        sWinFeat += "height="+sVal+",";
        pHeight = sVal;
        break;
      case "dialogwidth":
       sWinFeat += "width="+sVal+",";
       pWidth = sVal;
       break;
     case "dialogtop":
       sWinFeat += "screenY="+sVal+",";
       break;
     case "dialogleft":
       sWinFeat += "screenX="+sVal+",";
       break;
     case "resizable":
       sWinFeat += "resizable="+sVal+",";
       break;
     case "status":
       sWinFeat += "status="+sVal+",";
       break;
     case "center":
       sWinFeat += "center="+sVal+",";
       if ( sVal.toLowerCase() == "yes" )
       {
         sWinFeat += "screenY="+((screen.availHeight-pHeight)/2)+",";
         sWinFeat += "screenX="+((screen.availWidth-pWidth)/2)+",";
       }
       break;
    }
  }
  modalWin=window.open(String(sURL),"",sWinFeat);
  if (vArguments!=null&&vArguments!='')
    modalWin.dialogArguments=vArguments;
  return modalWin;
}


function checkFocus()
{
  //if (window.navigator.appVersion.indexOf("MSIE")==-1)
  {
    if (modalWin!=null && !modalWin.closed)
    {
      modalWin.focus();
      window.blur();
    }
  }
}

//font change functions

function fontsizeup() {
  active = getActiveStyleSheet();
  setActiveStyleSheet('large');
  document.getElementById('u-reg').style.display = 'inline';
  document.getElementById('u-large').style.display = 'none';
}

function fontsizedown() {
  active = getActiveStyleSheet();
  setActiveStyleSheet('reg');
  document.getElementById('u-reg').style.display = 'none';
  document.getElementById('u-large').style.display = 'inline';
}

function setActiveStyleSheet(title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
}

function getActiveStyleSheet() {
  var i, a;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title") && !a.disabled) return a.getAttribute("title");
  }
  return null;
}

function getPreferredStyleSheet() {
	  return ('reg');
}

function loading () {
  var div = getElement('loading');
  div.style.display = 'block';
  centerDiv (div);
}

function unloading () {
  var div = getElement('loading');
  div.style.display = 'none';
}


function formatNumber(num,decimals,forcedecimals) {
	  if (decimals == null || decimals == undefined) decimals=2;
	  if (forcedecimals == null || forcedecimals == undefined) forcedecimals=false;

	  simbol = ''; //'$'
	  num = num.toString().replace(/\$|\,/g,'');
	  if(isNaN(num)) num = "0";
		  
	  sign = (num == (num = Math.abs(num)));
	  var pot = Math.pow(10,decimals);
	  num = Math.floor(num*pot+0.50000000001);
	  cents = num%pot;
	  num = Math.floor(num/pot).toString();
	  for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	    num = num.substring(0,num.length-(4*i+3))+'.'+
	  num.substring(num.length-(4*i+3));
		  
	  var result = "0";
	  if (cents == "0") 
	    result = (((sign)?'':'-') + num + simbol);
	  else {
	    if(cents > 0 && cents < Math.pow(10,decimals-1))
	      cents = "0" + cents;
	    result =  (((sign)?'':'-') +num + ',' + cents + simbol);
	  }
		  
		   
	  if (!forcedecimals) {
		if (result.indexOf(',') >= 0) {
	      while (result.length-1 > result.indexOf(',')) {
		    if (result.charAt(result.length-1) == '0') {
		      result = result.substring(0,result.length-1);
	        }
		    else
		      break;
		  }
		  if (result.charAt(result.length-1) == ',')
		    result = result.substring(0,result.length-1);
		}
	  }

	  return (result);
	}

	function addCommas(nStr)
	{
	  nStr += '';
	  x = nStr.split('.');
	  x1 = x[0];
	  x2 = x.length > 1 ? '.' + x[1] : '';
	  var rgx = /(\d+)(\d{3})/;
	  while (rgx.test(x1)) {
	    x1 = x1.replace(rgx, '$1' + '.' + '$2');
	  }
	  return x1 + x2;
	}

function parseNumber(input) {
	if (input == "") input = "0";
	input = input.replace(new RegExp("\\.", 'g'), "");
	input = input.replace(new RegExp("\\,", 'g'), ".");
	return parseFloat(input);
}

function isNumber(val) {
	
    if(!val || (typeof val != "string" || val.constructor != String)) {
      return false;
    }
    return !isNaN(parseNumber(val));
  }


