var reEmail = /^.+\@.+\..+$/
var defaultEmptyOK = false
var addChar= '%'

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}


function colorize(id) {
  if(this.visible == true) {
    for(var i = 0; i < document.lists.length; i++) {
      document.lists[i].touch = true;
    }
  }
  var p = this;
  while(p != null) { p.touch = false; p = p.parent; }
  for(var i = 0; i < document.lists.length; i++)
    if(document.lists[i].touch &&
       document.lists[i].visible)
      expand(document.lists[i].id);
}

function returnWinValue(pValue, univid, pField) {
	s=eval('opener.document.srcfrm.'+pField+'dsc');
	s.value=pValue;
	s=eval('opener.document.srcfrm.'+pField);
	s.value=univid;
	top.close()
}

function resizeOuterTo(win,w,h) {
  if (parseInt(navigator.appVersion)>3) {
    if (navigator.appName=="Netscape") {
      win.outerWidth=w+30;
      win.outerHeight=h+50;
    }
    else win.resizeTo(w+30,h+50);
  }
}

function regSession ()
{
	var bName = "Microsoft Internet Explorer" ;
	if ((bName == navigator.appName) || (Session_Id != '' )) {
		var idobj = document.getElementById("sessionId");
		idobj.innerHTML = evalSession(Session_Id);

	};
}


function openUrl(url)
{
	strUrl='/mecgi/drv?urlEsterno&GOURL='+url;
	var options = 'resizable=1,menubar=1,toolbar=0,location=0,status=1,directories=0';
	a=window.open(strUrl,'ExternalLINk',options);
	a.focus();
}
				

function openUrlKey(url)
{
	strUrl='/mecgi/drv?urlEsterno&GOURL='+url;
	var options = 'resizable=1,menubar=1,toolbar=1,location=0,status=1,directories=0';
	a=window.open(strUrl,'ExternalLINk',options);
	a.focus();
}

function openPopUpPage(str,ppname, stile,w,h) {
     myStr=str;
     myPPName=ppname;
     myStyle=stile;
     popUpPage = window.open(str, ppname,stile);
	 if (w){
	   resizeOuterTo(popUpPage,w,h);
	 }
     popUpPage.focus();
     return(popUpPage);
}

function nulla(i)
{
;
}



function evalSession(sessionId)
{
	var vchar = "";
	i=0
    for(var i=0; i < sessionId.length; i++) {
		vchar = vchar.concat(addChar);
		vchar = vchar.concat(sessionId.substr(i,2)); 
		i++
	};
	//return(unescape(sessionId));
	return(unescape(vchar));
}


function isEmail (s)

{   if (isEmpty(s))
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);

    else {
       return reEmail.test(s)
    }
}


function calcolaTot(f,fp,ft)

{

  if (isNaN(f.value))  {
	alert ("Attenzione la quantità deve econtenere solo numeri");
    f.focus();
  } else {
    s = fp.value;
    s= s.replace(",", ".");
    st = s*f.value+"" ;
    n = '';
    arrayOfStrings = st.split(".");
  
    if (arrayOfStrings.length==1) {
      tot = 2;
      n = "00";
    } else {
      if (arrayOfStrings[1].length==1) {
        n = arrayOfStrings[1] + "0";
      } else {
        n = arrayOfStrings[1];
      }
    }
    st = arrayOfStrings[0] + "," + n;
    ft.value = st; 
  }
  return ;
}


function formatCurrency(num) {

alert (num);
num = num.toString().replace(/\$|\,/g,'');


alert (num);
CurrencySymbol = "";

if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
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));

return (((sign)?'':'-') + CurrencySymbol + num + ',' + cents);
}

 



function submitAcquista(f,nr) {
  f.action = nr;
  f.submit();
}

function none() {
  ;
}


///////////////////////////


function GetDecimalDelimiter(countryCode)
{
 
  switch (countryCode)
  {
    case 3:   
           return '#';
    case 2:   
           return ',';
    default:
           return '.';
  }
}

function GetCommaDelimiter(countryCode)
{
 
  switch (countryCode)
  { 
    case 3:          
           return '*';
    case 2:   
           return ',';
    default:
           return ',';
  }
 
}

function FormatClean(num)
{
     var sVal='';
     var nVal = num.length;
     var sChar='';
     
   try
   {
      for(c=0;c<nVal;c++)
      {
         sChar = num.charAt(c);
         nChar = sChar.charCodeAt(0);
         if ((nChar >=48) && (nChar <=57))  { sVal += num.charAt(c);   }
      }
   }
    catch (exception) { AlertError("Format Clean",exception); }
    return sVal;
}
  

function FormatNumber(num,countryCode,decimalPlaces)
{       

  var minus='';
  var comma='';
  var dec='';
  var preDecimal='';
  var postDecimal='';
  
  try 
  {
   
    decimalPlaces = parseInt(decimalPlaces);
alert (decimalPlaces);
    comma = GetCommaDelimiter(countryCode);
    dec = GetDecimalDelimiter(countryCode);

    if (decimalPlaces < 1) { dec = ''; }
//    if (num.lastIndexOf("-") == 0) { minus='-'; }
    preDecimal = 2;// FormatClean(num);
    
    // preDecimal doesn't contain a number at all.
    // Return formatted zero representation.
    
    if (preDecimal.length < 1)
    {
       return minus + FormatEmptyNumber(dec,decimalPlaces);
    }
    
    // preDecimal is 0 or a series of 0's.
    // Return formatted zero representation.
    
    if (parseInt(preDecimal) < 1)
    {
       return minus + FormatEmptyNumber(dec,decimalPlaces);
    }
    
    // predecimal has no numbers to the left.
    // Return formatted zero representation.
    
    if (preDecimal.length == decimalPlaces)
    {
      return minus + '0' + dec + preDecimal;
    }
    
    // predecimal has fewer characters than the
    // specified number of decimal places.
    // Return formatted leading zero representation.
    
    if (preDecimal.length < decimalPlaces)
    {
       if (decimalPlaces == 2)
       {
        return minus + FormatEmptyNumber(dec,decimalPlaces - 1) + preDecimal;
       }
       return minus + FormatEmptyNumber(dec,decimalPlaces - 2) + preDecimal;
    }
    
    // predecimal contains enough characters to
    // qualify to need decimal points rendered.
    // Parse out the pre and post decimal values
    // for future formatting.
    
    if (preDecimal.length > decimalPlaces)
    {
      postDecimal = dec + preDecimal.substring(preDecimal.length - decimalPlaces,
                                               preDecimal.length);
      preDecimal = preDecimal.substring(0,preDecimal.length - decimalPlaces);
    }

    // Place comma oriented delimiter every 3 characters
    // against the numeric represenation of the "left" side
    // of the decimal representation.  When finished, return
    // both the left side comma formatted value together with
    // the right side decimal formatted value.
    
    var regex  = new RegExp('(-?[0-9]+)([0-9]{3})');
 
    while(regex.test(preDecimal))
    {
       preDecimal = preDecimal.replace(regex, '$1' + comma + '$2');
    }
alert (preDecimal);
alert (postDecimal);
       
  }
  catch (exception) { AlertError("Format Number",exception); }
  return minus + preDecimal + postDecimal;
}

function FormatEmptyNumber(decimalDelimiter,decimalPlaces)
{
    var preDecimal = '0';
    var postDecimal = '';
 
    for(i=0;i<decimalPlaces;i++)
    {
      if (i==0) { postDecimal += decimalDelimiter; }
      postDecimal += '0';
    }
   return preDecimal + postDecimal;
}
  

 function AlertError(methodName,e)
 {
            if (e.description == null) { alert(methodName + " Exception: " + e.message); }
            else {  alert(methodName + " Exception: " + e.description); }
 }


  function trim(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
  }

function alphaNumericCheck(pwd){
  var regex=/^[0-9A-Za-z]+$/;
  if(regex.test(pwd)){
    return true;
  } else {
    return false;
  }
}
