function openPopUp(url, _name, isModal)
{
	_width = 640;
	_height = 640;
	_left = (screen.width / 2) - (_width / 2);
	_top  = (screen.height / 2) - (_height / 2);
	params = 		'width=' + _width + ',' + 
							'height=' + _height + ',' + 
							'left=' + _left + ',' + 
							'top=' + _top + ',' + 
							'scrollbars,' + 
							'toolbar=no,' +
							'status=no,' + 
							'menubar=no,' + 
							'directories=no, ' +
							'location=no, ' +
							'border=no';
	if (isModal)
	{
		return window.showModalDialog(url, _name, params);
	}
	else
	{
		window.open(url, _name, params);
	}	
}

function openPopUpImg(url, _name, isModal)
{
	//openPopUp(url, _name, isModal);
	_width = 640;
	_height = 640;
	_left = (screen.width / 2) - (_width / 2);
	_top  = (screen.height / 2) - (_height / 2);
	params = 		'width=' + _width + ',' + 
							'height=' + _height + ',' + 
							'left=' + _left + ',' + 
							'top=' + _top + ',' + 
							'scrollbars,' + 
							'toolbar=no,' +
							'status=no,' + 
							'menubar=no,' + 
							'directories=no, ' +
							'location=no, ' +
							'resizable=yes, ' +
							'border=no';	
	window.open(url, _name, params);
}


var monthLength = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

/**
  * ATTENZIONE!!! FUNZIONE OBSOLETA!!
  * DECIDERE UNA VOLTA PER TUTTE COME TRATTARE LE DATE, SE COME UN UNICO CAMPO O COME TRE CAMPI SEPARATI 
  * E REGOLARSI DI CONSEGUENZA 
  */
function checkDate(name, okNull)
{
	var x = document.forms[0].elements;
	/*
	var day = parseInt(x[name+"_day"].options[x[name+"_day"].selectedIndex].value);
	var month = parseInt(x[name+"_month"].options[x[name+"_month"].selectedIndex].value);
	var year = parseInt(x[name+"_year"].options[x[name+"_year"].selectedIndex].value);
  */
	var day = parseInt(x[name+"_day"].value,10);
	var month = parseInt(x[name+"_month"].value,10);
	var year = parseInt(x[name+"_year"].value,10);	

  if (!day && !month && !year && okNull==true)
    return true
  else
    if (!day || !month || !year)
  		return false;
  
  if ( (!isInteger(x[name+"_day"], '', false, false, false)) ||
       (!isInteger(x[name+"_month"], '', false, false, false)) ||
       (!isInteger(x[name+"_year"], '', false, false, false)) )
  {
    return false;
  }

	if   ( 
			   ( 
			     (year/4 == parseInt(year/4)) &&
			     (year/100 != parseInt(year/100)) 
			   ) 
			   ||
	       (year/400 == parseInt(year/400)) 
	     ) 
		monthLength[1] = 29;

  if ( (month < 1) || (month > 12) )
    return false;
    
	if (day > monthLength[month-1])
		return false;

	monthLength[1] = 28;

	var now = new Date();
	now = now.getTime(); //NN3

	var dateToCheck = new Date();
	dateToCheck.setYear(year);
	dateToCheck.setMonth(month-1);
	dateToCheck.setDate(day);
	var checkDate = dateToCheck.getTime();

	var futureDate = (now < checkDate);
	var pastDate = (now > checkDate);

	return true;
}

function check_date(field, error_descr, show_error_descr, setfocus)
{
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = "/";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp; //FF
   if (DateValue == '')  //FF
   //if (DateTemp == '')
   {
	  	if (show_error_descr)
	  	{
				alert (error_descr);
			}
	    if (setfocus)
			{
				DateField.select();
				DateField.focus();
			}    
			return false;			
	 }
	 /*
	 slashpos = strpos(DateValue, separator);
   day = DateValue.substr(0, slashpos - 1);
   if ((day=='')||(day<1))
   {
      err=22;
   };
   DateValue = substr(DateValue, slashpos + 1, strlen(DateValue));
   
	 slashpos = strpos(DateValue, separator);
   month = DateValue.substr(0, slashpos - 1);
   if ((month=='')||(month<1)||(month>12))
   {
      err=21;
   };
   DateValue = substr(DateValue, slashpos + 1, strlen(DateValue));
   year = DateValue;
   if ( (year=='')||
        (year<1)  ||
        (year==0)  ||
        (strlen(year)==1)||
        (strlen(year)==3)||
        (strlen(year)>4) || )
   {
      err = 19; 
   };
   */
   // Always change date to 8 digits - string
   // if year is entered as 2-digit / always assume 19xx 
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '19' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   // year is wrong if year = 0000 
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   // Validation of month
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   // Validation of day
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = day + seperator + month + seperator + year;
      return true;
   }
   /* Error-message if err != 0 */
   
   else 
   {
  	if (show_error_descr)
  	{
			alert (error_descr);
		}
    if (setfocus)
		{
			DateField.select();
			DateField.focus();
		}    
		return false;
   }
}

function isDigit (c)
{   
	return ((c >= "0") && (c <= "9"))
}

function isInteger (elem, error_descr, show_error_descr, setfocus)
{   
	 	var i;
		var s = elem.value;		
		
		var something_wrong;
    // Search through string's characters one by one
    // until we find a non-numeric character.
    // When we do, return false; if we don't, return true.
    
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);        
        if (!isDigit(c))
        {
        	something_wrong = true;
        }        
    }
    if (something_wrong)
    {
   		if (show_error_descr)
   		{
				alert(error_descr);
			};
   		if (setfocus)
   		{
				elem.focus();
				elem.select();
			};
			return false;
		}
		else
      // All characters are numbers.
      return true;
}

function ltrim ( s )
{
	return s.replace( /^\s*/, "" );
}

function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}

function trim ( s )
{
	return rtrim(ltrim(s));
}

// validates that the field value string has one or more characters in it
function isNotEmpty(elem, error_descr, show_error_descr, setfocus, trim_value) {    
    var str = elem.value;
    if (trim_value)
    {
			str = trim(str);
		}
    var re = /.+/;
    if(!str.match(re)) 
    {
    	if (show_error_descr)
    	{
				alert(error_descr);
			};
			if (setfocus)
			{
				elem.focus();
			}
      return false;
    } 
  	else 
  	{
      return true;
    }
}
   
// validates that the entry is 16 characters long when
// input field's maxlength attribute is set to 16
function isRightLen(elem, len, error_descr, show_error_descr, setfocus) {
    var str = elem.value;       
    if (str.length!=len)
    {
    	if (show_error_descr)
    	{
				alert(error_descr);
			};
			if (setfocus)
			{
				elem.focus();
				elem.select();
			}    	
			return false;
		}
  	else 
  	{
      return true;
    }
}
   
// validates that the entry is formatted as an email address
function isEMailAddr(elem, error_descr, show_error_descr, setfocus) {
    var str = elem.value;
    var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    if (!str.match(re)) 
    {
    	if (show_error_descr)
    	{
				alert(error_descr);
			};
			if (setfocus)
			{
				elem.focus();
				elem.select();
			}    	
      return false;
    } 
  	else 
  	{
      return true;
    }
}

function isLetter (c)
{   return ( ((c >= "a") && (c <= "z")) || ((c >= "A") && (c <= "Z")) )
}



function isAlphabetic (elem, error_descr, show_error_descr, setfocus)

{   
		var i;
		var s = elem.value;
		var something_wrong;
    // Search through string's characters one by one
    // until we find a non-alphabetic character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is letter.
        var c = s.charAt(i);

        if (!isLetter(c))
        {
					something_wrong = true;
				}
    }

		if (something_wrong)
		{
    	if (show_error_descr)
    	{
				alert(error_descr);
			};
			if (setfocus)
			{
				elem.focus();
				elem.select();
			}    	
			return false;			
		}
		else
		{
    	// All characters are letters.
    	return true;
		}
}

function findPosX(obj)
{
 var curleft = 0;
 if (document.getElementById || document.all)
 {
  while (obj.offsetParent)
  {
   curleft += obj.offsetLeft
   obj = obj.offsetParent;
  }
 }
 else if (document.layers)
  curleft += obj.x;
 return curleft;
}

function findPosY(obj)
{
 var curtop = 0;
 if (document.getElementById || document.all)
 {
  while (obj.offsetParent)
  {
   curtop += obj.offsetTop
   obj = obj.offsetParent;
  }
 }
 else if (document.layers)
  curtop += obj.y;
 return curtop;
}
