var sAnswer, sReply;

function initValidation(iForm) {
	sAnswer = "subform" + iForm;
	sReply = "subreply" + iForm;

	CommentFrmValidator = getValidator("formulario" + iForm);
	CommentFrmValidator.addRule("nome", "required", "Nome");
	CommentFrmValidator.addRule("email", ["email", "required"], "Email");
	
	CommentFrmValidator.focusOnError = false;
	
	CommentFrmValidator.showErrorMessage = function() {
		for (lnField = 0; lnField < arguments[1].length; lnField ++) {
			arguments[1][lnField].className = "badDataInput";
			if (arguments[1][lnField].name=='nome' && arguments[1][lnField].value=='') {
				arguments[1][lnField].value='Nome';
			}
			if (arguments[1][lnField].name=='email' && arguments[1][lnField].value=='') {
				arguments[1][lnField].value='E-mail';
			}			
		}
	}
}

self.OnSendBuyError = function() {
	show(sAnswer);
	hide(sReply);
};
self.OnSendBuySuccess = function() {
	show(sReply);
	hide(sAnswer);
};

function sendBuy(sForm) {
	initValidation(sForm);
	if (document.forms[sForm]['nome'].value=='Nome') {
		document.forms[sForm]['nome'].value='';
	}
	if (document.forms[sForm]['email'].value=='E-mail') {
		document.forms[sForm]['email'].value='';
	}	
	if (CommentFrmValidator.execute()) {
		Frm = document.forms[sForm];
		lsQuerystring = "/img_gateway.php?action=sendBuy";
		lsQuerystring += "&nome=" + escape(Frm["nome"].value);
		lsQuerystring += "&email=" + escape(Frm["email"].value);
		lsQuerystring += "&comentario=" + escape(Frm["descricao"].value);
		lsQuerystring += "&produto=" + escape(Frm["produto"].value);
		lsQuerystring += "&" + Math.random();
		ImgGW = new Image();
		ImgGW.onerror = function() {
			self.OnSendBuyError();
		}
		ImgGW.onload = function() {
			self.OnSendBuySuccess();
		}
		ImgGW.src = lsQuerystring;
	}
}

