var msg = new Array();
var idx = 0;
var fieldtofocus;

function getCaptcha(divname, fieldname) {
	var sed = Math&&Math.random?Math.floor(Math.random()*10000000000000):(new Date()).getTime();
	var url = "http://campbellsoup.promotionexpert.com/CampbellBusloadOfGear/captcha?action=get";
	url += "&rand=" + sed;

	var captchagethttprequest = createRequest();
	captchagethttprequest.onreadystatechange = function () {
		if (captchagethttprequest.readyState == 4) {
			if (captchagethttprequest.status == 200) {
				results = captchagethttprequest.responseText;
				fieldname.value=results;
				var imgtag = '<img src="http://campbellsoup.promotionexpert.com/CampbellBusloadOfGear/ti?nehowma=239&leivviel=19671384011843&scrid=' + results + '&rand=' + sed + '" width="134" height="17" />';
				document. getElementById(divname).innerHTML = imgtag;
				captchagethttprequest = null;
			}
		}
	}

	captchagethttprequest.open('GET', url, true);
	captchagethttprequest.send(null);
}

function validateCaptcha(form, captchaid) {
	var sed = Math&&Math.random?Math.floor(Math.random()*10000000000000):(new Date()).getTime();
	var url = "http://campbellsoup.promotionexpert.com/CampbellBusloadOfGear/captcha?action=validate&scrid=" + captchaid + "&udata=" + form.captcha.value;
	url += "&rand=" + sed;

	var captchahttprequest = createRequest();
	captchahttprequest.onreadystatechange = captchaResponse;

	captchahttprequest.open('GET', url, true);
	captchahttprequest.send(null);

	function captchaResponse() {
		if (captchahttprequest.readyState == 4) {
			if (captchahttprequest.status == 200) {
				var crslt = captchahttprequest.responseText;
				if (crslt == 'mismatch') {
					msg[idx++] = 'Please enter the characters as they appear in the image.';
				}

				if (msg.length > 0) {
					displayErrors(msg, fieldtofocus);
				}
				else {
					document.reg.submit();
				}
			}
		}
	}
}

function checkAll(form) {
        jQuery.facebox(function() {
                checkAllData(form); 
        } 
        );
}

function checkAllData(form) {
	msg = new Array();
	idx = 0;

	if(!checkEmail(form.e)) {
		msg[idx++] = 'Please enter your email address.';
		if (!fieldtofocus) {
			fieldtofocus = form.e;
		}
	}

	if(!isEmpty(form.captcha)) {
		validateCaptcha(form, form.scrid.value);
	}
	else {
		msg[idx++] = 'Please enter the characters as they appear in the image.';

		displayErrors(msg, fieldtofocus);
	}
}
