// JavaScript Document
var sFormName = "";		// Override in calling document

function validate_fields() {
  var fields = document.getElementById("required").value.split(",");
  var i;
  var entry;
  var badCount = 0;
  var bad = new Array;
  var firstBadField = "";
  
  for ( i = 0; i < fields.length; i++ ) {
    var isBad = true;
    entry = document.getElementById(fields[i]);
//	alert( i + "='" + fields[i] + "' (" + entry + ")" );
	if ( entry == null ) {
		alert( "There is a problem with this web page.\nPlease contact the webmaster (webmaster@cartransportbroker.co.uk)\nand quote the following ID: FIELD_" + fields[i] + "\n" );
		return false;
	}
//	alert( i + "='" + fields[i] + "' (" + entry + ":" + entry.type + ") => '" + entry.value + "'" );
//  commented out from here 27/08/08  if ( entry.type && entry.type == "radio" ) {
//      if ( entry.checked == true ) {
//        isBad = false;
//      } else {
//        var numButtons = entry.count;
//        for ( var j = 1; j < numButtons; j++ ) {
//          entry = document.getElementById(fields[i]+"_"+j);
//          if ( entry.checked == true ) isBad = false;
//        }
//      }
//    } else if ( entry.type && entry.type == "checkbox" ) {
//      isBad = false;
//    } else if ( entry.value != "" ) isBad = false;
//    if ( isBad ) {
//      bad[badCount++] = entry.title;
// commented out up to here 27/08/08      if ( firstBadField == "" ) firstBadField = fields[i];
//	  alert( "Entry is bad: '" + entry.title + "'" );
    }
  }
  if ( badCount > 0 ) {
    document.getElementById(firstBadField).focus();
	
	// All this to try and centre the error window...<sigh>
	var h = 200 + 15 * badCount;
	var w = 440;
	var l = 0;
	var t = 0;
	
	// First part of palaver...try to get position of this window on the screen...
	if ( window.screenLeft != null ) {
	  l += window.screenLeft;
	  t += window.screenTop;
//	  alert("screen top = " + t);
	} else if ( window.screenX != null ) {
	  l += window.screenX;
	  t += window.screenY;
//	  alert("screen Y = " + t);
	}
	// Second part of palaver...try to get dimensions of this window on the screen...
	if ( window.outerHeight != null ) {
	  l += (window.outerWidth - w)/2;
	  t += (window.outerHeight - h)/2;
//	  alert("outerHeight = " + window.outerHeight);
	} else if ( document.documentElement.clientHeight != null ) {
	  l += (document.documentElement.clientWidth - w)/2;
	  t += (document.documentElement.clientHeight - h)/2;
//	  alert("clientHeight = " + document.body.scrollHeight);
	} else if ( document.body.clientHeight != null ) {
	  l += (document.body.clientWidth - w)/2;
	  t += (document.body.clientHeight - h)/2;
//	  alert("clientHeight = " + document.body.clientHeight);
	}
    newwin = window.open( '', 'FormError', 'height='+h+',width='+w+',left='+l+',top='+t+',location=0,menubar=0,resizable=0,scrollbars=0,status=0,toolbar=0' );
	newwin.document.write( '<html><head><title>' + sFormName + ' Form Error</title></head><body>' );
	newwin.document.write( '<p>The following mandatory fields are blank or invalid.</p>\n<ul><li>' + bad.join('</li>\n<li>') + '</li></ul>\n<p>Please fill them in before attempting to submit the form again.</p>' );
	newwin.document.write( '<p><a href="javascript:close()">Close Window</a></p>\n' );
	newwin.document.writeln( '</body></html>' );
    return false;
  } else {
    return true;
  }
}