/* populate the supplied select element with a list of countries
 * with UK as the default selection */
function populateCountrySelect(type) {
    var selectElement = document.getElementById(type+'country');

    // add options
   addOption(selectElement, 'Afghanistan', 'Afghanistan');
   addOption(selectElement, 'Albania', 'Albania');
   addOption(selectElement, 'Algeria', 'Algeria');
   addOption(selectElement, 'Andorra', 'Andorra');
   addOption(selectElement, 'Angola', 'Angola');
   addOption(selectElement, 'Anguilla', 'Anguilla');
   addOption(selectElement, 'Antigua', 'Antigua');
   addOption(selectElement, 'Antilles', 'Antilles');
   addOption(selectElement, 'Argentina', 'Argentina');
   addOption(selectElement, 'Ascension Island', 'Ascension Island');
   addOption(selectElement, 'Australia', 'Australia');
   addOption(selectElement, 'Austria', 'Austria');
   addOption(selectElement, 'Bahamas', 'Bahamas');
   addOption(selectElement, 'Bahrain', 'Bahrain');
   addOption(selectElement, 'Balearic Islands', 'Balearic Islands');
   addOption(selectElement, 'Bangladesh', 'Bangladesh');
   addOption(selectElement, 'Barbados', 'Barbados');
   addOption(selectElement, 'Barundi', 'Barundi');
   addOption(selectElement, 'Belgium', 'Belgium');
   addOption(selectElement, 'Belize', 'Belize');
   addOption(selectElement, 'Bermuda', 'Bermuda');
   addOption(selectElement, 'BFPO', 'BFPO');
   addOption(selectElement, 'Bhutan', 'Bhutan');
   addOption(selectElement, 'Bolivia', 'Bolivia');
   addOption(selectElement, 'Borneo', 'Borneo');
   addOption(selectElement, 'Botswana', 'Botswana');
   addOption(selectElement, 'Brasil', 'Brasil');
   addOption(selectElement, 'Brunei', 'Brunei');
   addOption(selectElement, 'Bulgaria', 'Bulgaria');
   addOption(selectElement, 'Burma', 'Burma');
   addOption(selectElement, 'Burundi', 'Burundi');
   addOption(selectElement, 'Cambodia', 'Cambodia');
   addOption(selectElement, 'Cameroon', 'Cameroon');
   addOption(selectElement, 'Canada', 'Canada');
   addOption(selectElement, 'Canary Islands', 'Canary Islands');
   addOption(selectElement, 'Cayman Islands', 'Cayman Islands');
   addOption(selectElement, 'Chad', 'Chad');
   addOption(selectElement, 'Chile', 'Chile');
   addOption(selectElement, 'China', 'China');
   addOption(selectElement, 'Colombia', 'Colombia');
   addOption(selectElement, 'Comoro Islands', 'Comoro Islands');
   addOption(selectElement, 'Costa Rica', 'Costa Rica');
   addOption(selectElement, 'Cuba', 'Cuba');
   addOption(selectElement, 'Cape Verdi Isle', 'Cape Verdi Isle');
   addOption(selectElement, 'Cyprus', 'Cyprus');
   addOption(selectElement, 'Czech and Slovak Federative Rep', 'Czech and Slovak Federative Rep');
   addOption(selectElement, 'Dahomey', 'Dahomey');
   addOption(selectElement, 'Denmark', 'Denmark');
   addOption(selectElement, 'Dominican Republic', 'Dominican Republic');
   addOption(selectElement, 'Dubai', 'Dubai');
   addOption(selectElement, 'Ecuador', 'Ecuador');
   addOption(selectElement, 'Egypt', 'Egypt');
   addOption(selectElement, 'El Salvador', 'El Salvador');
   addOption(selectElement, 'Equatorial Guinea', 'Equatorial Guinea');
   addOption(selectElement, 'Equador', 'Equador');
   addOption(selectElement, 'Estonia', 'Estonia');
   addOption(selectElement, 'Ethiopia', 'Ethiopia');
   addOption(selectElement, 'Falkland Islands', 'Falkland Islands');
   addOption(selectElement, 'Faroe Islands', 'Faroe Islands');
   addOption(selectElement, 'Fiji', 'Fiji');
   addOption(selectElement, 'Finland', 'Finland');
   addOption(selectElement, 'France', 'France');
   addOption(selectElement, 'Gabon', 'Gabon');
   addOption(selectElement, 'The Gambia', 'The Gambia');
   addOption(selectElement, 'Germany', 'Germany');
   addOption(selectElement, 'Ghana', 'Ghana');
   addOption(selectElement, 'Gibraltar', 'Gibraltar');
   addOption(selectElement, 'Greece', 'Greece');
   addOption(selectElement, 'Greenland', 'Greenland');
   addOption(selectElement, 'Grenada', 'Grenada');
   addOption(selectElement, 'Guatemala', 'Guatemala');
   addOption(selectElement, 'Guinea Bissau', 'Guinea Bissau');
   addOption(selectElement, 'Guyana', 'Guyana');
   addOption(selectElement, 'Haiti', 'Haiti');
   addOption(selectElement, 'Holland', 'Holland');
   addOption(selectElement, 'Hong Kong', 'Hong Kong');
   addOption(selectElement, 'Hungary', 'Hungary');
   addOption(selectElement, 'Iceland', 'Iceland');
   addOption(selectElement, 'India', 'India');
   addOption(selectElement, 'Indonesia', 'Indonesia');
   addOption(selectElement, 'Irish Republic', 'Irish Republic');
   addOption(selectElement, 'Iran', 'Iran');
   addOption(selectElement, 'Iraq', 'Iraq');
   addOption(selectElement, 'Israel', 'Israel');
   addOption(selectElement, 'Italy', 'Italy');
   addOption(selectElement, 'Ivory Coast', 'Ivory Coast');
   addOption(selectElement, 'Jamaica', 'Jamaica');
   addOption(selectElement, 'Japan', 'Japan');
   addOption(selectElement, 'Jordan', 'Jordan');
   addOption(selectElement, 'Kampuchea', 'Kampuchea');
   addOption(selectElement, 'Kazakhstan', 'Kazakhstan');
   addOption(selectElement, 'Kenya', 'Kenya');
   addOption(selectElement, 'Kiribati', 'Kiribati');
   addOption(selectElement, 'Korea', 'Korea');
   addOption(selectElement, 'Kuwait', 'Kuwait');
   addOption(selectElement, 'Laos', 'Laos');
   addOption(selectElement, 'Latvia', 'Latvia');
   addOption(selectElement, 'Lebanon', 'Lebanon');
   addOption(selectElement, 'Lesotho', 'Lesotho');
   addOption(selectElement, 'Liberia', 'Liberia');
   addOption(selectElement, 'Libya', 'Libya');
   addOption(selectElement, 'Liechtenstein', 'Liechtenstein');
   addOption(selectElement, 'Luxembourg', 'Luxembourg');
   addOption(selectElement, 'Macau', 'Macau');
   addOption(selectElement, 'Madagascar', 'Madagascar');
   addOption(selectElement, 'Maldive Islands', 'Maldive Islands');
   addOption(selectElement, 'Mali', 'Mali');
   addOption(selectElement, 'Mallorca', 'Mallorca');
   addOption(selectElement, 'Malta', 'Malta');
   addOption(selectElement, 'Mauritius', 'Mauritius');
   addOption(selectElement, 'Malawi', 'Malawi');
   addOption(selectElement, 'Malaysia', 'Malaysia');
   addOption(selectElement, 'Mexico', 'Mexico');
   addOption(selectElement, 'Monaco', 'Monaco');
   addOption(selectElement, 'Montserrat', 'Montserrat');
   addOption(selectElement, 'Morroco', 'Morroco');
   addOption(selectElement, 'Mozambique', 'Mozambique');
   addOption(selectElement, 'Namibia', 'Namibia');
   addOption(selectElement, 'Nauru', 'Nauru');
   addOption(selectElement, 'Netherlands Antilles', 'Netherlands Antilles');
   addOption(selectElement, 'Nepal', 'Nepal');
   addOption(selectElement, 'Netherlands', 'Netherlands');
   addOption(selectElement, 'New Zealand', 'New Zealand');
   addOption(selectElement, 'Nicaragua', 'Nicaragua');
   addOption(selectElement, 'Nigeria', 'Nigeria');
   addOption(selectElement, 'Niger', 'Niger');
   addOption(selectElement, 'Norway', 'Norway');
   addOption(selectElement, 'Oman', 'Oman');
   addOption(selectElement, 'Pakistan', 'Pakistan');
   addOption(selectElement, 'Panama', 'Panama');
   addOption(selectElement, 'Papua New Guinea', 'Papua New Guinea');
   addOption(selectElement, 'Paraguay', 'Paraguay');
   addOption(selectElement, 'Peru', 'Peru');
   addOption(selectElement, 'Philippines', 'Philippines');
   addOption(selectElement, 'Poland', 'Poland');
   addOption(selectElement, 'Portugal', 'Portugal');
   addOption(selectElement, 'Puerto Rico', 'Puerto Rico');
   addOption(selectElement, 'Qatar', 'Qatar');
   addOption(selectElement, 'Reunion Island', 'Reunion Island');
   addOption(selectElement, 'Republic of Rwanda', 'Republic of Rwanda');
   addOption(selectElement, 'Rep of Singapore', 'Rep of Singapore');
   addOption(selectElement, 'Rumania', 'Rumania');
   addOption(selectElement, 'Russia', 'Russia');
   addOption(selectElement, 'Republic of Vanuatu', 'Republic of Vanuatu');
   addOption(selectElement, 'South Africa', 'South Africa');
   addOption(selectElement, 'San Marino', 'San Marino');
   addOption(selectElement, 'Saudi Arabia', 'Saudi Arabia');
   addOption(selectElement, 'Senegal', 'Senegal');
   addOption(selectElement, 'Serbia', 'Serbia');
   addOption(selectElement, 'Seychelles', 'Seychelles');
   addOption(selectElement, 'Sierra Leone', 'Sierra Leone');
   addOption(selectElement, 'Slovenia', 'Slovenia');
   addOption(selectElement, 'Solomon Islands', 'Solomon Islands');
   addOption(selectElement, 'Somalia', 'Somalia');
   addOption(selectElement, 'Spain', 'Spain');
   addOption(selectElement, 'Sri Lanka', 'Sri Lanka');
   addOption(selectElement, 'St Helena', 'St Helena');
   addOption(selectElement, 'South Pacific', 'South Pacific');
   addOption(selectElement, 'St Kitts', 'St Kitts');
   addOption(selectElement, 'St Lucia', 'St Lucia');
   addOption(selectElement, 'St Vincent', 'St Vincent');
   addOption(selectElement, 'Sudan', 'Sudan');
   addOption(selectElement, 'Swaziland', 'Swaziland');
   addOption(selectElement, 'Sweden', 'Sweden');
   addOption(selectElement, 'Switzerland', 'Switzerland');
   addOption(selectElement, 'Syria', 'Syria');
   addOption(selectElement, 'Taiwan ROC', 'Taiwan ROC');
   addOption(selectElement, 'Tanzania', 'Tanzania');
   addOption(selectElement, 'Thailand', 'Thailand');
   addOption(selectElement, 'The Democratic Republic of Congo', 'The Democratic Republic of Congo');
   addOption(selectElement, 'The Republic of the Congo', 'The Republic of the Congo');
   addOption(selectElement, 'Togo', 'Togo');
   addOption(selectElement, 'Trinidad and Tobago', 'Trinidad and Tobago');
   addOption(selectElement, 'Tunisia', 'Tunisia');
   addOption(selectElement, 'Turkey', 'Turkey');
   addOption(selectElement, 'Turks and Caicos Isles', 'Turks and Caicos Isles');
   addOption(selectElement, 'Tuvalu', 'Tuvalu');
   addOption(selectElement, 'United Arab Emirates', 'United Arab Emirates');
   addOption(selectElement, 'Uganda', 'Uganda');
   addOption(selectElement, 'UK', 'UK', true);
   addOption(selectElement, 'Ukraine', 'Ukraine');
   addOption(selectElement, 'Uruguay', 'Uruguay');
   addOption(selectElement, 'USA', 'USA');
   addOption(selectElement, 'USSR', 'USSR');
   addOption(selectElement, 'Venezuela', 'Venezuela');
   addOption(selectElement, 'Vietnam', 'Vietnam');
   addOption(selectElement, 'Virgin Islands', 'Virgin Islands');
   addOption(selectElement, 'Western Somoa', 'Western Somoa');
   addOption(selectElement, 'Yemen', 'Yemen');
   addOption(selectElement, 'Yugoslavia', 'Yugoslavia');
   addOption(selectElement, 'Zambia', 'Zambia');
   addOption(selectElement, 'Zimbabwe', 'Zimbabwe');
}

/* Add an option element to the supplied select element */
function addOption(selectElement, value, text, selected) {
    var oOption = document.createElement("OPTION");
    oOption.text=text;
    oOption.value=value;
    selectElement.options[selectElement.options.length] = oOption; // NB: MUST ADD the option element BEFORE marking it selected!
    if( selected ) {
        oOption.selected = true;
    }
}

/* Remove all option elements from the supplied select element */
function clearSelectOptions(selectElement) {
    var l = selectElement.options.length;
    for( var i=0; i<l; i++ ) {
        selectElement.remove(0);
    }
}

/* hides the named element */
function hideElement(elemId) {
    if( document.getElementById(elemId) ) {
        document.getElementById(elemId).style.display='none';
    }
}

/* Check that an item has been selected from a drop-down selection box
 * where the first item is a "Please select" item
 * (and hence a selection index of 0 indicates non-selection) */
function checkSelection(selectId, errorMsg, errorDiv, validationErrors) {
    if( document.getElementById(selectId) && document.getElementById(selectId).selectedIndex < 1 ) {
        showError(errorMsg, errorDiv, validationErrors);
        return false;
    }
    return true;
}

/* Check that an input field has been populated */
function checkField(fieldId, errorMsg, errorDiv, validationErrors) {
    if( document.getElementById(fieldId) && document.getElementById(fieldId).value == '') {
        showError(errorMsg, errorDiv, validationErrors);
        return false;
    }
    return true;
}

/* Check that a gender has been selected*/
function checkGender(male, female, errorDiv, validationErrors) {
    if( !document.getElementById(male).checked && !document.getElementById(female).checked ) {
        showError('Please select a gender', errorDiv);
        return false;
    }
    return true;
}

/* Check that one of a pair of radio buttons has been selected */
function checkRadioPair(radio1, radio2, errorMsg, errorDiv, validationErrors) {
    if( !document.getElementById(radio1).checked && !document.getElementById(radio2).checked ) {
        showError(errorMsg, errorDiv);
        return false;
    }
    return true;
}

/* Check that two input fields have been populated with the same text,
 * for example, email addresses or passwords */
function checkEquals(field1, field2, type, errorDiv, validationErrors) {
    if( document.getElementById(field1).value == document.getElementById(field2).value) {
        return true;
    }
    else {
        showError('The two versions of the '+type+' do not match', errorDiv, validationErrors);
        return false;
    }
}

/* Checks that the value of the field is an integer. This can
 * be used for an onChange event check as it hides the error message
 * if the value has been corrected. */
function checkInteger(intField, errorMsg, errorDiv, validationErrors) {
	var val = document.getElementById(intField).value;
	if( val > 0 ) {
        hideElement(errorDiv, validationErrors);
		return true;
	}
	else {
		document.getElementById(intField).value = '';
        showError(errorMsg, errorDiv, validationErrors);
        return false;
	}
}

/* Displays an error message (errorDiv) local to the error and a
 * general message (validationErrors) indicating something went wrong.
 * The errorMsg supplied is set on the local message adn the named div
 * should be placed eith just above or along side the errored field..
 * The general message is expected to have a defualt message set on it
 * which is just made visible, for example,
 * "Please correct all errors on this form before proceeding" */
function showError(errorMsg, errorDiv, validationErrors) {
    if( errorDiv && document.getElementById(errorDiv) ) {
        document.getElementById(errorDiv).innerHTML=errorMsg;
        document.getElementById(errorDiv).style.display='block';
    }
    if( validationErrors && document.getElementById(validationErrors) ) {
        document.getElementById(validationErrors).style.display='block';
    }
}

/* transfers a check box tick to a hidden input field */
function transferCheck( fromId, toId ) {
    if( document.getElementById(fromId).checked) {
        document.getElementById(toId).value='Y';
    }
    else {
        document.getElementById(toId).value='N';
    }
}


