function displayWindow(url, width, height) 
{
    var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=1,scrollbars=1,menubar=0,status=0' );
}

function validbirth()
{
	$required_fields = new Array('birthplace', 'birthsurname', 'birthforename');
	$field_descriptions = new Array('Place of Birth', 'Surname at Birth', 'Forename(s)');
	$extra_fields = new Array('fathersurname','fatherforename','mothermaidenname','mothersforename');
	$extra_descriptions = new Array('Father\'s Surname','Father\'s Forename(s)','Mother\'s Maiden Name','Mother\'s Forename(s)');
	

	// if birth year is less than 50 years ... names required
	$birthyear = document.getElementById('birthyear').value;
	$date = new Date();
	$checkyear = $date.getFullYear() - 50;
	if ($birthyear != '' && $birthyear > $checkyear)
	{
 		var birthf = $required_fields.concat($extra_fields);
		var birthd = $field_descriptions.concat($extra_descriptions);
	} else {
		var birthf = $required_fields;
		var birthd = $field_descriptions;
	}
	
	return validateCertificate('birthday', 'birthmonth', 'birthyear', birthf, birthd, 'Date of Birth');
}

function validmarriage()
{
//	$required_fields = new Array('marriageplace', 'mansurname', 'mansforename', 'womansurnameatmarriage', 'womanforenameatmarriage');
	$required_fields = new Array('marriageplace');
//	$field_descriptions = new Array('Place of Marriage', 'Man\'s Surname', 'Man\'s Forename(s)', 'Woman\'s Surname at Marriage', 'Woman\'s Forename(s) at Marriage');
	$field_descriptions = new Array('Place of Marriage');
	
	return validateCertificate('marriageday', 'marriagemonth', 'marriageyear', $required_fields, $field_descriptions, 'Date of Marriage');
}

function validdeath()
{
	$required_fields = new Array('deathsurname', 'deathforename','placeofdeath');
	$field_descriptions = new Array('Surname of Deceased','Forename(s) of Deceased','Place of Death, or last known address');
	
	return validateCertificate('deathday', 'deathmonth', 'deathyear', $required_fields, $field_descriptions, 'Date of Death');
}

function validateCertificate(day, month, year, fields, descriptions, type)
{
	var err = '';

	for(i=0;i<fields.length;i++)
	{
		check = document.getElementById(fields[i]).value
		if (check=='' || check==null)
		{
			err += descriptions[i] + "\n";
		}
	}
	
	var year_err = checkDate(day, month, year, type);
	err = year_err + err;

	if(err == '')
	{
		return true;
	} else {
		err = "These fields are required:\n" + err + "\nPlease fill in all required fields.";
		alert(err);
		return false;
	}
}

function checkBirthYear(id)
{
	$today = new Date();
	$thisyear = $today.getFullYear();
	$fifty = $thisyear - 50;
	
    var year = document.getElementById(id).value;
    if (year>$fifty)
        return addStarForBirth(true);
    else
        return addStarForBirth(false);
}

function checkDeathYear(id)
{	
	var year = document.getElementById(id).value;
    if (!isInteger(year))
        return false; 
    if (year>=1955)
        return addStarForDeath(true);
    else
        return addStarForDeath(false);
}

function addStarForBirth(vBool)
{
    var bFields = new Array('sfathersurname','sfatherforename','smothermaidenname','smothersforename');
    var star = '';
    var msg = '';
    if (vBool)
    {
        star = '*';
        msg = 'Father\'s and Mother\'s details should be supplied, if known, if birth occured in last 50 years';
    }
    for (i=0;i<bFields.length;i++)
    {
        document.getElementById(bFields[i]).value = star;

    }
    document.getElementById('smsg').innerHTML = msg;
    return vBool;
}

function addStarForDeath(vBool)
{

    var star = '';
    var msg = '';
    if (vBool)
    {
        star = '*';
        msg = '<br />Required after 1955';
    }
   document.getElementById('aatdeath').innerHTML = star;
   document.getElementById('aatdeath2').innerHTML = msg;
}

function checkDate(fday, fmonth, fyear, type)
{
	var year = document.getElementById(fyear).value;
	var m = document.getElementById(fmonth);
    var month = m.options[m.selectedIndex].value;
    var d = document.getElementById(fday);
    var day = d.options[d.selectedIndex].value;
	
	$today = new Date();
	$thisyear = $today.getFullYear();
	$thismonth = $today.getMonth(); // JANUARY is 0, DECEMBER is 11
	$fifty = $thisyear - 50;
	
	// CHECK DATE MUST BE 19 MONTHS AGO. (1 year and 7 months)
	$check_year = $thisyear - 1;
	$check_month = $thismonth - 7;
	if ($check_month < 0)
	{
		$check_month += 12;
		$check_year -= 1;
	}
	// now fix the index
	$check_month += 1;
	
	// Date checks
	if (year.length != 4)
	{
		return "The " + type + " must be 1837 or later\n";
	}
	if (!isInteger(year) || year < 1837 || year > $check_year)
	{
		return "The " + type + " must be between 1837 and " + $check_year + "\n";
	}
	if (year == $check_year && month > $check_month)
	{
		return "The " + type + " must be between 1837 and " + $check_year + "\n";
	}
	
	if (type == "Date of Death")
	{
		var dage = document.getElementById('deathage').value;
		
		if(!isInteger(dage) && year >= 1955)
		{
			return "Age at death is required after 1955.\n";
		}
		if(dage > 130 && year >= 1955)
		{
			return "Please enter a valid age at death.\n";
		}
	}
	
	return '';
}

function isInteger(s)
{
    var i;
    if (isEmpty(s))
        if (isInteger.arguments.length == 1) 
            return 0;
        else 
            return (isInteger.arguments[1] == true);
                               
    for (i = 0; i < s.length; i++)
    {
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}
function isEmpty(s)
{
    return ((s == null) || (s.length == 0))
}
function isDigit (c)
{
    return ((c >= "0") && (c <= "9"))
}

function dispPrice()
{
	// get the service level and display price accordingly
	service = document.getElementById('service').value;
	
	var price = '0';
	
	if (service == 'standard')
	{
		price = '25.00';
	} else if (service == 'swift') {
		price = '40.00';
	} else {
		price = '60.00';
	}
	
	document.getElementById('price').innerHTML = price;
}