
var district1_UK_id 		= new Array();
var district1_UK_name 		= new Array();
var district2_UK_ALL_id 	= new Array();
var district2_UK_ALL_name 	= new Array();


district1_UK_id[0] 		 	= "";
district1_UK_name[0] 	 	= "The whole country";
district2_UK_ALL_id[0] 	 	= "";
district2_UK_ALL_name[0] 	= "Whole of UK";



function write_district1_options(cid)
{
  var id_array = eval("district1_" + cid + "_id");
  var name_array = eval("district1_" + cid + "_name");
  for (var i = 0; i < id_array.length; i++)
    document.write('<option value="' + id_array[i] + '">' + name_array[i] + '</option>');
}



function write_district2_options(cid, d1)
{
  var id_array = eval("district2_" + cid + "_" + (d1 ? d1 : "ALL") + "_id");
  var name_array = eval("district2_" + cid + "_" + (d1 ? d1 : "ALL") + "_name");
  for (var i = 0; i < id_array.length; i++)
    document.write('<option value="' + id_array[i] + '">' + name_array[i] + '</option>');
}



function set_district2_options(cid, d1, obj)
{
  var id_array = eval("district2_" + cid + "_" + (d1 ? d1 : "ALL") + "_id");
  var name_array = eval("district2_" + cid + "_" + (d1 ? d1 : "ALL") + "_name");
  obj.length = id_array.length;

  for (var i = 0; i < id_array.length; i++)
    { obj[i] = new Option(name_array[i], d1 + "£" + id_array[i]); }

  obj.selectedIndex = 0;
}



function set_district_additional_options(obj)
{
  var newItems = new Array();
  newItems[1] = new Array("Lincolnshire", "128£1233");
  newItems[0] = new Array("Newark", "127£86£49");

  for (var j = 0; j < newItems.length; j++)
  {
    var bInserted = false;
    for (var i = 0; obj.options.length; i++)
    {
      if (newItems[j][0] <= obj.options[i].text && obj.options[i].value != "127£")
      {
        obj.options.add(new Option(newItems[j][0], newItems[j][1]), i);
        bInserted = true;

        break;
      }
    }

    if (!bInserted)
      obj.options.add(new Option(newItems[j][0], newItems[j][1]));
  }
}

function validate_not_blank(field, name)
{
    // strip all spaces at start and end of line
    var regx_output = new String(field.value);
    var re1 = new RegExp("^[ ]*|([ ]*)\$", 'g');
    field.value = regx_output.replace(re1, "");

    if (field.value == "")
    {
        alert(format_str('The field "%s" can not be blank.', name));
        focus_field(field);
        return false;
    }
    return true;
}



function logon()
{
    if (!validate_not_blank(document.this_page.usr, "Email Address"))
        return;
    if (!validate_not_blank(document.this_page.pwd, "Password"))
        return;
  document.this_page.action = "/shared/cv/source/cv_start.hts?nav_to=no_logon,cvs";
  document.this_page.submit();
}

function show_forgot_password_page()
{
    var forgot_password_window = window.open("/shared/cv/source/cv_forgot_password.hts", "", "toolbar=no,directories=no,menubar=no,scrollbars=no,status=yes,resizable=1,left=50,top=50,width=600,height=150");
    return false;
}

function forgot_password_pop () {
    mywindow = window.open ("./forgot_password.asp", "tipWindow","location=0,status=0,scrollbars=0,width=420,height=200");
    mywindow.moveTo(470,200);
    return false;
}

function forgot_password()
{
    if (!validate_not_blank(document.this_page.myForgotPassword, "E-mail"))
        return false;
    if (!validate_email_address(document.this_page.myForgotPassword, document.this_page.myForgotPassword.value))
        return false;

    document.this_page.command.value = "forgot_password";
    document.this_page.submit();
}


function user_logoff()
{
  // relocate("index.asp", "self");

  document.this_page.command.value = "user_logoff";
  document.this_page.submit();
}

function validate_email_address(field, name, allow_name_in_address, display_error)
{
    if (typeof display_error == "undefined")
    {
        display_error = true;
    }

    // Get field value
    var fieldval = field.value;

    // Remove spaces before and after email
    fieldval = fieldval.replace(/^\\s*/, "");
    fieldval = fieldval.replace(/\\s*\$/, "");

    // Update field
    field.value = fieldval;

    // If "name <email>" format is available - get email address
    if (allow_name_in_address)
    {
        var a = fieldval.indexOf("<");
        var b = fieldval.indexOf(">");
        if (a < b)
        {
            fieldval = fieldval.substring(a + 1, b);
        }
    }

    // Initialize
    var msg     = "";
    var last_at = fieldval.lastIndexOf("\@");
    var before  = fieldval.substr(0, last_at);
    var after   = fieldval.substr(last_at + 1, fieldval.length - last_at - 1);
    var reg;

    // CHECK: the @ character must exist
    if (last_at < 0)
    {
        msg = "There is no @ in your e-mail address.";
    }

    // CHECK: there must be at least one character preceeding and one character following @
    if (!msg)
    {
        if ((before.length < 1) || (after.length < 1))
        {
            msg = "Your e-mail address must contain at least 1 character in front of @.";
        }
    }

    // CHECK: characters below 32 are illegal
    if (!msg)
    {
        for (var i = 0; i < fieldval.length; i++)
        {
            var code = fieldval.charCodeAt(i);
            if (code < 32)
            {
                msg = "You have used an invalid character in your email address.";
                break;
            }
        }
    }

    // CHECK: the ".@" combination is illegal
    if (!msg)
    {
        if (before.substr(before.length - 1, 1) == ".")
        {
            msg = "You have written a bad sign for @.";
        }
    }

    // CHECK: the "@." combination is illegal
    if (!msg)
    {
        if (after.substr(0, 1) == ".")
        {
            msg = "A full stop cannot be the first character after @.";
        }
    }

    // CHECK: at least one "." is required in the host section
    if (!msg)
    {
        if (after.indexOf(".") < 0)
        {
            msg = "There is no full stop in your e-mail address.";
        }
    }

    // CHECK: "." cannot be the last character
    if (!msg)
    {
        if (after.lastIndexOf(".") == (after.length - 1))
        {
            msg = "A full stop cannot be the last character in an e-mail address.";
        }
    }

    // CHECK: the "-." combination is illegal following @
    if (!msg)
    {
        reg = /\\-{1}\\.{1}/;
        if (reg.test(after))
        {
            msg = "A dash  (-) cannot be placed in front of a full stop (.)";
        }
    }

    // CHECK: "-" cannot be the last character
    if (!msg)
    {
        if (after.lastIndexOf("-") == (after.length - 1))
        {
            msg = "A dash (-) cannot be the last character in an e-mail address.";
        }
    }

    // CHECK: certain characters preceeding @ must be escaped
    if (!msg)
    {
        // 1) remove all escaped characters
        // 2) all remaining special characters were not escaped - which is illegal
        before = before.replace(/\\\\{1}.{1}/g, "");
        reg = /[\\s<>()\\[\\]\\\\,;:@\\"]{1,}/;
        if (reg.test(before))
        {
            msg = "You have used a character that cannot be used in front of the @.";
        }
    }

    // CHECK: the characters following @ are restricted
    // ASCII Characters below 32, not allowed
    if (!msg)
    {
        reg = new RegExp("^[\\x00-\\x20]*\$");
        if (reg.test(after))
        {
            msg = "You have used a character that cannot be used after the @.";
        }
    }

    // If the address is illegal
    if ((msg) && (display_error = true))
    {
        alert(format_str("The e-mail address '%s' is incorrect:\n%s\n\nE-mail addresses are usually formatted in the following way:\n - name.surname@company.co.uk\n - username@internetserviceprovider.com\n - initials@serviceprovider.com\n   etc.", name, msg));
        focus_field(field);
        return false;
    }

    // The address seems to be in order
    return true;
}
