function ww_form_validate_required_text(field,alerttxt)
{
  with (field)
  {
    if (value==null||value==""||value=="[not provided]")
    {alert(alerttxt);return false}
    else
    {return true}
  }
}

function ww_form_validate_email(field,alerttxt)
{
  with (field)
  {
    apos=value.indexOf("@")
    dotpos=value.lastIndexOf(".")

    if (apos<1||dotpos-apos<2)
    {alert(alerttxt);return false}
    else
    {return true}
  }
}

function ww_form_validate_phone(thisform)
{
  with (thisform)
  {
    if (ww_form_validate_required_text(phone, "Phone must be filled in")==false)
      {phone.focus();return false;}
    else
    {
      var phoneValue = phone.value;
      var phoneValidChars = /^[0-9()+. \t-]+$/;
      var digCount = 0;
      var digits = "0123456789";

      for (var ix = 0; ix < phoneValue.length; ix++)
      {
        if (digits.indexOf(phoneValue.charAt(ix)) != -1)
          {digCount++;}
      }

      if ((!phoneValidChars.test(phoneValue)) || (digCount < 8) || (digCount > 15)) 
        {alert("Please enter a valid telephone number");phone.focus();return false}
      else
        {return true}
    }
  }
}

function ww_form_validate_name_email(thisform)
{
  with (thisform)
  {
    if (ww_form_validate_required_text(first_name,"First Name must be filled in")==false)
    {first_name.focus();return false;}

    if (ww_form_validate_required_text(last_name,"Last Name must be filled in")==false)
    {last_name.focus();return false;}

    if (ww_form_validate_email(email,"Email Address must be valid")==false)
    {email.focus();return false;}
  }
  
  return true;
}

function ww_form_validate_country(thisform)
{
  with (thisform)
  {
    if (country.selectedIndex == 0)
    {
      alert("Country must be selected");
      return false;
    }
  }

  return true;
}

function ww_form_calculate_profile_fields(thisform)
{
  with (thisform)
  {
    try
    {
    var group = elements["wwdc_cb[]"];
    var x, len = 0;
    var lead_info = elements["00N50000001osYg"];
    var lead_page = elements["00N50000001sthj"];
    var returl = elements["retURL"];
    var country_field = elements["country"];
    var country_local = country_field.value;
    var form_id = elements["wwdc_form_id"];

    // Capture website page url
    lead_page.value = window.location.href;

    // Set the default return URL
    if ((window.location.pathname == "/Products/ePublisher/Purchase") ||
         (window.location.pathname == "/Products/ePublisher/Purchase/"))
    {
      returl.value = ww_form_calc_return_page_purchase(country_local) + "?wwdc_conversion=true";
    }
    else
    {
      returl.value = window.location.href + "?wwdc_conversion=true";
    }

    // Set cookie so that form will not show up again for a while
    ww_form_set_completed(form_id.value);

    if (group != null)
      len = group.length;
    // Calculate profile checked boxes and append to info field
    for(x=0; x<len; x++)
    {
      if(group[x].checked)
      {
        if (group[x].value == "Ready to purchase")
        {
          returl.value = ww_form_calc_return_page_purchase(country_local) + "?wwdc_conversion=true";
        }

        if (x == 0)
        {
          lead_info.value = "";
        }
        else
        {
          lead_info.value += "\n";
        }
        lead_info.value += group[x].value;
      }
    }
    }
    catch (errror) {
      alert('Error: ' + error);
    }
  }
}

function ww_form_calculate_lead_fields(thisform)
{
  with (thisform)
  {
    try
    {
    var group = elements["wwdc_cb[]"];
    var x, len = 0;
    var lead_info = elements["00N50000001osYg"];
    var lead_page = elements["00N50000001sthj"];
    var returl = elements["retURL"];
    var country_field = elements["country"];
    var country_local = country_field.value;
    var form_id = elements["wwdc_form_id"];

    // Capture website page url
    lead_page.value = window.location.href;

    // Set the default return URL
    returl.value = window.location.href + "?wwdc_conversion=true";

    // Set cookie so that form will not show up again for a while
    ww_form_set_completed(form_id.value);

    if (group != null)
      len = group.length;
    // Calculate profile checked boxes and append to info field
    for(x=0; x<len; x++)
    {
      if(group[x].checked)
      {
        if (x == 0)
        {
          lead_info.value = "";
        }
        else
        {
          lead_info.value += "\n";
        }
        lead_info.value += group[x].value;
      }
    }
    }
    catch (errror) {
      alert('Error: ' + error);
    }
  }
}

function ww_form_hide_show_form()
{
  var form_id = document.getElementById("wwdc_form_id");

  if (form_id != null)
  {
    var form_id_value = form_id.value;
    var regex = new RegExp(form_id_value + "=");
    var match = regex.exec(document.cookie);
    var element1 = document.getElementById(form_id_value);
    var element2 = document.getElementById(form_id_value + "_completed");

    if (match==null)
    {
      element1.style.display = "block";
      element2.style.display = "none";
    }
    else
    {
      element1.style.display = "none";
      element2.style.display = "block";
    }
  }
}

function ww_form_set_completed(form_id)
{
  var d = new Date();

  // Set cookie to expire in 48 hours
  d.setTime(d.getTime() + (1 * 48 * 60 * 60 * 1000));
  document.cookie = form_id + "=1" + "; expires=" + d.toGMTString() + "; path=/";
}

var ww_form_is_partner_country_data = {
'AUSTRALIA': true,
'NEW ZEALAND': true,
'BULGARIA': true,
'GERMANY': true,
'RUSSIAN FEDERATION': true,
'UKRAINE': true,
'AUSTRIA': true,
'SWITZERLAND': true,
'DENMARK': true,
'FINLAND': true,
'ICELAND': true,
'NORWAY': true,
'SWEDEN': true,
'FRANCE': true,
'ISRAEL': true,
'JAPAN': true,
'CHINA': true,
'MALAYSIA': true,
'SINGAPORE': true,
'SOUTH KOREA': true,
'TAIWAN': true,
'THAILAND': true,
'VIETNAM': true,
'UNITED KINGDOM': true,
'INDIA': true
};

var ww_form_partner_page_data = {
'BULGARIA': 'SQUIDDS',
'GERMANY': 'SQUIDDS',
'RUSSIAN FEDERATION': 'SQUIDDS',
'UKRAINE': 'SQUIDDS',
'AUSTRIA': 'SQUIDDS',
'SWITZERLAND': 'SQUIDDS',
'DENMARK': 'Splendid',
'FINLAND': 'Splendid',
'ICELAND': 'Splendid',
'NORWAY': 'Splendid',
'SWEDEN': 'Splendid',
'FRANCE': 'Antea',
'ISRAEL': 'Almond',
'JAPAN': 'XL_Soft',
'CHINA': 'XL_Soft',
'MALAYSIA': 'XL_Soft',
'SINGAPORE': 'XL_Soft',
'SOUTH KOREA': 'XL_Soft',
'TAIWAN': 'XL_Soft',
'THAILAND': 'XL_Soft',
'VIETNAM': 'XL_Soft',
'UNITED KINGDOM': 'Mekon',
'INDIA': 'Shree'
};

function ww_form_is_partner_country(country)
{
  try
  {
    if (ww_form_is_partner_country_data[country])
      return true;
  }
  catch(ex)
  {
  }

  return false;
}

function ww_form_partner_page(country)
{
  try
  {
    var page = ww_form_partner_page_data[country];

    return page;
  }
  catch(ex)
  {
  }

  return '';
}

function ww_form_calc_return_page_purchase(country)
{
  var page;

  page = "http://www.webworks.com/Products/ePublisher/Purchase/Authorized_Representatives";

  if (ww_form_is_partner_country(country))
  {
    var sub_page = ww_form_partner_page(country);

    page = page + '/' +sub_page;
  }

  page = page + '/' + 'Purchase_request_reply.shtml';

  return page;
}

function ww_form_setStateChoices()
{
  var country = document.getElementById("country");
  var state = document.getElementById("state");
  var state_usa = document.getElementById("state_usa");
  var state_div = document.getElementById("state_div");
  var state_usa_div = document.getElementById("state_usa_div");

  state.value = "";
  state_usa.value = "";

  if (country.value == "USA")
  {
    state_div.style.display = "none";
    state_usa_div.style.display = "inline";
  }
  else
  {
    state_div.style.display = "inline";
    state_usa_div.style.display = "none";
  }
}

function ww_form_setState()
{
  var country = document.getElementById("country");
  var state = document.getElementById("state");
  var state_usa = document.getElementById("state_usa");

  if (country.value == "USA")
  {
    state.value = state_usa.value;
  }
}

