function copy_licensee_to_delivery()
{
  document.Form1.fDeliveryOrganisation.value= document.Form1.fLicenseeOrganisation.value;
  document.Form1.fDeliveryDepartment.value  = document.Form1.fLicenseeDepartment.value;
  document.Form1.fDeliveryContact.value     = document.Form1.fLicenseeRepresentative.value;
  document.Form1.fDeliveryAddress1.value    = document.Form1.fLicenseeAddress1.value;
  document.Form1.fDeliveryAddress2.value    = document.Form1.fLicenseeAddress2.value;
  document.Form1.fDeliveryCity.value        = document.Form1.fLicenseeCity.value;
  document.Form1.fDeliveryPostalCode.value  = document.Form1.fLicenseePostalCode.value;
}


function copy_licensee_to_billing()
{
  document.Form1.fBillingOrganisation.value= document.Form1.fLicenseeOrganisation.value;
  document.Form1.fBillingDepartment.value  = document.Form1.fLicenseeDepartment.value;
  document.Form1.fBillingContact.value     = document.Form1.fLicenseeRepresentative.value;
  document.Form1.fBillingAddress1.value    = document.Form1.fLicenseeAddress1.value;
  document.Form1.fBillingAddress2.value    = document.Form1.fLicenseeAddress2.value;
  document.Form1.fBillingCity.value        = document.Form1.fLicenseeCity.value;
  document.Form1.fBillingPostalCode.value  = document.Form1.fLicenseePostalCode.value;
}


function copy_delivery_to_billing()
{
  document.Form1.fBillingOrganisation.value= document.Form1.fDeliveryOrganisation.value;
  document.Form1.fBillingDepartment.value  = document.Form1.fDeliveryDepartment.value;
  document.Form1.fBillingContact.value     = document.Form1.fDeliveryContact.value;
  document.Form1.fBillingAddress1.value    = document.Form1.fDeliveryAddress1.value;
  document.Form1.fBillingAddress2.value    = document.Form1.fDeliveryAddress2.value;
  document.Form1.fBillingCity.value        = document.Form1.fDeliveryCity.value;
  document.Form1.fBillingPostalCode.value  = document.Form1.fDeliveryPostalCode.value;
}


function MM_findObj (arg, doc) 
{ //v4.01
  var pos, i, val, frame;
    
  if (!doc) doc= document;                  // if no 'doc' supplied, 'doc' becomes the current document 
  pos= arg.indexOf("?");                    // 'pos' is the position of the question mark in the 'arg' string 
  if (pos>0 && parent.frames.length)   
    {
    // if there is a question mark AND there are frames
    frame= arg.substring(pos+1);            // 'frame' becomes the indicated frame
    doc= parent.frames[frame].document;     // 'doc' becomes the document within the indicated frame  
    arg= arg.substring(0,pos);              // remove question mark and the frame from 'arg'
    }
  val= doc[arg];                            // 'val' becomes the value of the parameter 'arg' within the document 'doc' 
  if (!val && doc.all)  val= doc.all[arg];  // 'val' becomes the value of the 'document.all[arg]'
  // if 'val' still unknown, find it from all available forms
  for (i=0; !val &&               i<doc.forms.length;  i++)  val= doc.forms[i][arg];
  // if 'val' still unknown, find it from all available layers
  for (i=0; !val && doc.layers && i<doc.layers.length; i++)  val= MM_findObj(arg,doc.layers[i].document);
  // if 'val' still unknown, find it with a call to getElementById(arg)
  if (!val && doc.getElementById)  val= doc.getElementById(arg); 
  return val;
}


function MM_validateForm() 
{ //v4.0
  var i,p,q,nm,test,num,min,max,warn,val,errors='';

  for (i=0; i<(arguments.length-2); i+=3) 
    { 
    warn= arguments[i+1];  
    test= arguments[i+2];  
    val= MM_findObj(arguments[i]);
    if (val) 
      { 
      nm= val.name; 
      if ((val=val.value)!="") 
        {
        if (test.indexOf('isEmail') != -1) 
          { 
          p= val.indexOf('@');
          if (p<1 || p==(val.length-1))
            if (warn.length>0)  errors+= '- '+warn+' cannot be correct.\n';
            else                errors+= '- '+nm  +' cannot be correct.\n';
          } 
        else if (test.indexOf('inRange') != -1) // a numeric field
          { 
          num = parseFloat(val);
          if (isNaN(num))  
              if (warn.length>0) errors+= '- '+warn+' must be a number.\n';
              else               errors+= '- '+nm  +' must be a number.\n';
          if (test.indexOf('inRange') != -1) 
            { 
            p= test.indexOf(':');
            min= test.substring(8,p); 
            max= test.substring(p+1);
            if (num<min || num>max)  
              if (warn.length>0) errors+= '- '+warn+' must be a number between '+min+' and '+max+'\n';
              else               errors+= '- '+nm  +' must be a number between '+min+' and '+max+'\n';
            } 
          } 
        } // if ((val=val.value)!="")
      else if (test.charAt(0) == 'R') 
        {
        if (warn.length>0) errors+= '- '+warn+' is required.\n';
        else               errors+= '- '+nm  +' is required.\n';
        } 
      } // if val
    } 
  if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}


var EU_country = ["Austria",  "Belgium",   "Bulgaria","Cyprus",     "Czech Republic",
                  "Denmark",  "Estonia",   "Finland", "France",     "Germany",
                  "Greece",   "Hungary",   "Ireland", "Italy",      "Latvia",
                  "Lithuania","Luxembourg","Malta",   "Netherlands","Poland",
                  "Portugal", "Romania",   "Slovakia","Slovenia",   "Spain",
                  "Sweden",   "United Kingdom"];
                  
var EU_country_abbr = ["AT","BE","BG","CY","CZ",
                       "DK","EE","FI","FR","DE",
                       "EL","HU","IE","IT","LV",
                       "LT","LU","MT","NL","PL",
                       "PT","RO","SK","SI","ES",
                       "SE","GB"];

function clear_VAT()
{
  if (document.Form1.fLicenseeVAT.value.length<=3)  document.Form1.fLicenseeVAT.value= "";
}


function on_country_change()
{
  var i, country; 

  clear_VAT();

  i = document.Form1.fLicenseeCountry.selectedIndex;
  if (i<0)  return;

  country= document.Form1.fLicenseeCountry.options[i];

  // copy the country to the billing and delivery addresses, if present
  if (document.Form1.fBillingCountry )  document.Form1.fBillingCountry.value = country.value;
  if (document.Form1.fDeliveryCountry)  document.Form1.fDeliveryCountry.value= country.value;

  // adjust the Customs_ID field  

  // adjust the VAT field  and the Customs_ID field
  for (i=0; i<EU_country.length; i++)  if (country.value==EU_country[i])
    { 
    if (document.Form1.Customs_ID)  document.Form1.Customs_ID= "--";
    if (document.Form1.fLicenseeVAT.value.length<=3 || document.Form1.fLicenseeVAT.value=="(VAT always charged)")  
      {
      if (country.value=="Netherlands")
        document.Form1.fLicenseeVAT.value= "(VAT always charged)";
      else
        document.Form1.fLicenseeVAT.value= EU_country_abbr[i] + " ";
      }
    return; 
    }
  document.Form1.fLicenseeVAT.value= "";
} // on_country_change()
