function PG_isEmailAddr(s)
{ 
  var result = false;
  var theStr = new String(s);
  var index = theStr.indexOf("@");

  if (index > 0)
  {
    var pindex = theStr.indexOf(".",index);
    if ((pindex > index+1) && (theStr.length > pindex+1))
	result = true;
  }
  
  if (!result)
  	alert('L\'indirizzo inserito non č corretto (es.: mario@server.com).');
  return result;
}

function PG_noEmptyFields(form)
{
		$noEmptyFields=true;
		for (var i=0;i<form.elements.length;i++)
		{
			if ((form.elements[i].type=="text")||(form.elements[i].type=="password")){
				if (form.elements[i].value.length<=2){$noEmptyFields=false;};
			}
			if (form.elements[i].type=="select-one"){
				if (form.elements[i].selectedIndex<=1){$noEmptyFields=false;};
			}
		}
		
		if (!$noEmptyFields)
			alert('Tutti i campi del modulo devono essere compilati.');
		return $noEmptyFields;
}



function PG_changeAtt(id,att,new_v)
{
    obj=document.getElementById(id);
	obj.att=new_v;
}

function PG_load_cities(province_abbr,provinces,cities,cities_ids)
{
	document.forms[0].city.options.length = 0;
	k=0;
	for (i=0;i<cities.length;i++)
	{
		if (provinces[i]==province_abbr){
			document.forms[0].city.options[k] = new Option(cities[i],cities_ids[i]);
			k++;
		}
	}
}

function PG_today(what)
{
	var now = new Date();
	switch (what)
	{
		default:
		case 'mday':
			return now.getDate();
		break;
		case 'mon':
			return now.getMonth()+1;
		break;
		case 'year':
			return now.getFullYear();
		break;
	}
}

function PG_onOffPrivateData()
{
	PG_onOff('address');
	PG_onOff('tel');
	PG_onOff('fax');
	PG_onOff('email');
	PG_onOff('province');
	PG_onOff('city');
}

function PG_onOffToday()
{
	obj=document.getElementById('pub_today');	
	if (obj.checked)
	{
		PG_selectOption('year_pub_date',PG_today('year'));
		PG_selectOption('month_pub_date',PG_today('mon'));
		PG_selectOption('day_pub_date',PG_today('mday'));
	}
	PG_onOff('year_pub_date');
	PG_onOff('month_pub_date');
	PG_onOff('day_pub_date');
}

function PG_onOffUnlimited()
{
	obj=document.getElementById('exp_unlimited');
	if (!obj.checked)
	{
		PG_deleteOption('year_exp_date','-1');
		PG_deleteOption('month_exp_date','-1');
		PG_deleteOption('day_exp_date','-1');
		PG_selectOption('year_exp_date',PG_today('year'));
		PG_selectOption('month_exp_date',PG_today('mon'));
		PG_selectOption('day_exp_date',PG_today('mday'));
	}else{
		PG_addOption('year_exp_date','ill.','-1');
		PG_addOption('month_exp_date','ill.','-1');
		PG_addOption('day_exp_date','ill.','-1');
		PG_selectOption('year_exp_date','-1');
		PG_selectOption('month_exp_date','-1');
		PG_selectOption('day_exp_date','-1');
	}
	PG_onOff('year_exp_date');
	PG_onOff('month_exp_date');
	PG_onOff('day_exp_date');
}

function PG_onOff(id)
{
	obj=document.getElementById(id);
	if ((obj.type=="text")||(obj.type=="select-one"))
	{
		if (obj.disabled)
		{
			obj.disabled=false;
		}else{
			obj.disabled=true;
		}
	}else{
		alert('L\'elemento non puņ essere disabilitato!');
	}
}

function PG_addOption(id,s,v)
{
	obj=document.getElementById(id);
	newOption=document.createElement('OPTION');
	obj.appendChild(newOption);
	newOption.value=v;
	newOption.innerHTML=s;
}

function PG_selectOption(id,v)
{ 
  i=0;
  obj=document.getElementById(id);
  while ((obj.options.length>i)&&(v!=obj.options[i++].value));
  if (v==obj.options[i-1].value)
  	{
		obj.selectedIndex=i-1;
		return i-1;
	}else{
		obj.selectedIndex=0;
		return -1;
	}
}

function PG_deleteOption(id,v)
{
  obj=document.getElementById(id);
  i=PG_selectOption(id,v);
  if (i!=-1)obj.removeChild(obj.options[i]);
}
