function ElSpazi(data) {
Str = new String(data);
num = 0;
for(i=0; i<Str.length; i++) {
  if(Str.substring(i, i+1) != " ")
    break;
}
Str = Str.substring(i, Str.length);
return(Str.length);
}
function checkMail(x) {
var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
return filter.test(x);
}
function isTelNumber(str) {
var len = str.length;
if(len == 0) return false;
var p = 0;
var ch;
while ( p<len ) {
  ch = str.charAt(p);
  if(('0'<=ch && ch<='9') || ch==' ' || ch=='.' || ch=='/' || ch=='-' || ch=='+' || ch=='(' || ch==')')
    p++;
  else
    return false;
}
return true;
}
function checkDati() {
var f = document.reg;
if ( ElSpazi(f.firstname.value) == 0 ) {
  f.firstname.focus();
  alert ("First name is a required field");
  return false;
}
if ( ElSpazi(f.lastname.value) == 0 ) {
  f.lastname.focus();
  alert ("Last name is a required field");
  return false;
}
if ( ElSpazi(f.affiliation.value) == 0 ) {
  f.affiliation.focus();
  alert ("Affiliation is a required field");
  return false;
}
if ( ElSpazi(f.address.value) == 0 ) {
  f.address.focus();
  alert ("Address is a required field");
  return false;
}
if ( ElSpazi(f.city.value) == 0 ) {
  f.city.focus();
  alert ("City is a required field");
  return false;
}
if ( ElSpazi(f.zip_code.value) == 0 ) {
  f.zip_code.focus();
  alert ("ZIP code is a required field");
  return false;
}
if ( ElSpazi(f.country.value) == 0 ) {
  f.country.focus();
  alert ("Country is a required field");
  return false;
}
if ( ElSpazi(f.badge.value) == 0 ) {
  f.badge.focus();
  alert ("Badge is a required field");
  return false;
}
if ( checkMail(f.email.value) == 0 ) {
  f.email.focus();
  alert ("Please check that you have entered a valid email address");
  return false;
}
if ( !isTelNumber(f.tel.value) ) {
  f.tel.focus();
  alert ("Invalid telephone number");
  return false;
}
if ( !f.card[0].checked && !f.card[1].checked ) {
  f.card[0].focus();
  alert ("Please select a credit card type: only Visa and MasterCard accepted.");
  return false;
}
if ( ElSpazi(f.number.value) == 0 ) {
  f.number.focus();
  alert ("Please enter a valid credit card number.");
  return false;
}
if ( ElSpazi(f.holder.value) == 0 ) {
  f.holder.focus();
  alert ("Please enter the card owner name.");
  return false;
}
if ( f.month.value == 0 || f.year.value == 0 ) {
  f.month.focus();
  alert ("Please enter the card expiration date.");
  return false;
}
if ( ElSpazi(f.inv_instit.value) == 0 ) {
  f.inv_instit.focus();
  alert ("Invoice institution is a required field");
  return false;
}
if ( ElSpazi(f.inv_address.value) == 0 ) {
  f.inv_address.focus();
  alert ("Invoice institution address is a required field");
  return false;
}
if ( !f.noroom.checked && !f.room[0].checked && !f.room[1].checked ) {
    f.noroom.focus();
    alert ("Please, either select a room type or check for no reservation");
    return false;
}
if ( f.shuttle.checked && !f.where[0].checked && !f.where[1].checked && !f.where[2].checked ) {
    f.shuttle.focus();
    alert ("Please, either select a shuttle departure point or set no shuttle");
    return false;
}
if ( f.presenting.checked ) {
  if ( f.edas.value.length != 4 ) {
    f.edas.focus();
    alert ("Paper/poster code must be four digits long.");
    return false;
  }
  if ( f.pages.value == 0 ) {
    f.pages.focus();
    alert ("Please select the number of pages of your contribution.");
    return false;
  }
}
f.totaldue.disabled = false;
return true;
}
function noroomclicked ()
{
var f = document.reg;
var c = f.noroom.checked;
var i;

if (c) {
f.totaldue.value = "300";
}else {
f.totaldue.value = "400";
}

for ( i = 0; i < 16; i++ ) {
  f.room[i].disabled = c;
}
}
function shuttleP ()
{
var f = document.reg;
var c = f.shuttle.checked;
for ( i = 0; i < 3; i++ ) {
  f.where[i].disabled = !c;
}
}
function ispresentingP ()
{
 var f = document.reg;
 if ( f.presenting.checked ) {
f.edas.disabled = false;
f.pages.disabled = false;
if ( f.edas.value == "(none)" )
  f.edas.value = "";
f.edas.focus();
 }
 else {
f.edas.disabled = true;
f.pages.disabled = true;
f.edas.value = "(none)";
 }
}
function namecopy ()
{
var f = document.reg;
f.inv_instit.value = f.affiliation.value;
f.inv_address.value= f.address.value + "\n" + f.zip_code.value + " " + f.city.value +
                   " " + f.state.value + "\n" + f.country.value;
}
function badgecopy ()
{
var f = document.reg;
f.badge.value = f.firstname.value + " " + f.lastname.value + "\n" + f.affiliation.value +
  "\n" + f.country.value;
}
function calculateTotalDue() {
var form = document.reg;
var x = ((form.disc20.checked)?240:300) + 50 * form.addtickets.value;
if ( form.pages.value > 14 ) {
  x += 100 * Math.ceil((form.pages.value-14)/2);
}
form.totaldue.value = x;
}
function singleP()
{
var f = document.reg;
if ( f.room[1].checked ) {
  f.share[0].disabled = false;
  f.share[1].disabled = false;
}
else {
  f.share[0].disabled = true;
  f.share[1].disabled = true;
}
}

