var msg = new Array();
var idx = 0;
var fieldtofocus;
var userchoice;
var cid;

function setVoteCookie() {
	createCookie("finalist",userchoice,1);
	createCookie("cid",cid,1);
}

function setUpValues() {
	var flst = readCookie("finalist");
	if (flst) {
		userchoice = flst;
	}
	var cmptid = readCookie("cid");
	if (cmptid) {
		cid = cmptid;
	}
	
	if (userchoice && cid) {
		eraseCookie("finalist");
		eraseCookie("cid");
		
		getCaptcha();
	}
}

function setCategory1(finalist) {
	userchoice = finalist;
	cid = '19751956570393';
}

function setCategory2(finalist) {
	userchoice = finalist;
	cid = '19771965628281';
}

function setCategory3(finalist) {
	userchoice = finalist;
	cid = '19791965718557';
}

function setCategory4(finalist) {
	userchoice = finalist;
	cid = '19811965765614';
}

function voteCategory1(finalist) {
	setCategory1(finalist);	
	
	getCaptcha();
}

function voteCategory2(finalist) {
	setCategory2(finalist);	
	
	getCaptcha();
}

function voteCategory3(finalist) {
	setCategory3(finalist);	
	
	getCaptcha();
}

function voteCategory4(finalist) {
	setCategory4(finalist);	
	
	getCaptcha();
}

function getCaptcha() {
	displayCaptcha();
	var fieldname = document.reg.scrid;
	var sed = Math&&Math.random?Math.floor(Math.random()*10000000000000):(new Date()).getTime();
	var url = "http://ebay.promotionexpert.com/eBaySellersChallengeVote/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://ebay.promotionexpert.com/eBaySellersChallengeVote/ti?nehowma=241&leivviel=' + cid + '&scrid=' + results + '&rand=' + sed + '" width="134" height="17" />';
				document. getElementById("captchadiv").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://ebay.promotionexpert.com/eBaySellersChallengeVote/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 {
					vote();
				}
			}
		}
	}
}

function vote(scrid, captcha) {
	var sed = Math&&Math.random?Math.floor(Math.random()*10000000000000):(new Date()).getTime();
	var url = "http://ebay.promotionexpert.com/eBaySellersChallengeVote/HTMLData?nehowma=241";
	url += "&leivviel=" + cid;
	url += "&scrid=" + escape(document.reg.scrid.value);
	url += "&ebayid_enc=" + escape(document.reg.ebayid_enc.value);
	url += "&choice=" + userchoice;
	url += "&captcha=" + escape(document.reg.captcha.value);
	url += "&rand=" + sed;

	var httprequest = null;
	httprequest = createRequest();
	httprequest.onreadystatechange = voteCompleted;
	httprequest.open('GET', url, true);
	httprequest.send(null);
	
	function voteCompleted() {
		// only if req is "loaded"
		if (httprequest.readyState == 4) {
			if (httprequest.status == 200) {
				var crslt = httprequest.responseText;
				if (crslt.indexOf('success') == 0) {
					createThankYou();
				}
				else {
					if (crslt.indexOf('alreadyvoted') == 0) {
						createAlreadyVoted();
					}
					else {
						if (crslt.indexOf('over') == 0) {
							document.location.href='http://ebay.promotionexpert.com/sellerschallenge/over.html';
						}
					}
				}
			}
		}
	}
}


function checkAll(form) {
	msg = new Array();
	idx = 0;

//	if(!checkEmail(form.e)) {
//		msg[idx++] = 'Please enter your email address.';
//		if (!fieldtofocus) {
//			fieldtofocus = form.e;
//		}
//	}
//	if(!hasSelectedAButton(form.choice))
//	{
//		msg[idx++] = 'Please vote.';
//
//	}

	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);
	}
}

