var msg = new Array();
var idx = 0;
var fieldtofocus;

function vote() {
	var rateform = document.reg;
        var sed = Math&&Math.random?Math.floor(Math.random()*10000000000000):(new Date()).getTime();
        var url = "http://ebay.promotionexpert.com/MotorsMaster/SaveRating";
        url += "?nehowma=244";
        url += "&leivviel=2004173009428";
        url += "&ef=Mu5-GkY8F7HisKjEPMjL0g..";
        url += "&scrid=" + escape(rateform.scrid.value);
        url += "&e=" + escape(rateform.e.value);
        url += "&rating=" + rateform.rating.value;
        url += "&entry_id=" + escape(rateform.entry_id.value);
        url += "&captcha=" + escape(rateform.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) {
					var todaydt = getTodayDate();
					if (!hasSeenSweepsToday(todaydt)) { 
	                                        show_thanks_for_ratingDiv();
					}
					else {
                                        	show_thanks_for_ratingNoSweepsDiv();
                                        }
                                }
                                else {
                                        if (crslt.indexOf('alreadyvoted') == 0) {
						var todaydt = getTodayDate();
						if (!hasSeenSweepsToday(todaydt)) { 
							show_thanks_for_ratingDiv();
						}
						else {
							show_thanks_for_ratingNoSweepsDiv();
						}
                                        }
                                        else {
                                                if (crslt.indexOf('over') == 0) {
                                                        document.location.href='http://ebay.promotionexpert.com/motorsmaster/over.html';
                                                }
                                        }
                                }
                        }
                }
        }
}

function createThankYou() { alert('success'); }

function createAlreadyVoted() { alert('success'); }

function getCaptcha(divname, fieldname) {
	var sed = Math&&Math.random?Math.floor(Math.random()*10000000000000):(new Date()).getTime();
	var url = "http://ebay.promotionexpert.com/MotorsMaster/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/MotorsMaster/ti?nehowma=244&leivviel=2004173009428&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://ebay.promotionexpert.com/MotorsMaster/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 checkAll(form) {
	msg = new Array();
	idx = 0;


	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);
	}
}

function rate(form, r)
{
	getCaptcha('captchadivimg', document.reg.scrid);
	form.rating.value = r;
}
