You're going to need to validate the data coming from getCookieValue. If you're expecting a number, ensure that the value returned is numeric. Also ensure that any escape characters (e.g. quotes that break out of your javascript) are not present in the field. A fix for this would look like:
function is_valid(value) {
// Do some check here depending on what you're expecting.
// I also recommend escaping any quotes (i.e. " becomes \")
// Ideally, you'd just whitelist what is acceptable input (A-Z0-9 or whatever,
// and return false from this function if something else is present in
// value!)
}
var cookie_value = getCookieValue('fieldval');
if(is_valid(cookie_value)) {
document.write('<input type="hidden name="field1" value="' + cookie_value + '" />');
}
Long story short, sanitize the data before you document.write or you end up with a reflected XSS.
As mentioned in the comments above, an XSS originating from a user's own cookies (something they modify themselves) is not particularly worrisome. However, whatever coding practices lead to this are likely present elsewhere. I'd recommend reviewing your source and ensuring that all input from users is treated as untrusted and sanitized appropriately.