/*
 * @package Component jVoteSystem for Joomla! 1.5
 * @projectsite www.joomess.de/projekte/18
 * @author Johannes Meßmer
 * @copyright (C) 2010- Johannes Meßmer
 * @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
*/
var jVoteSystemAlertDivCloned = new Array();

function jVoteSystemAlertCloneDiv(id) {
	if(jVoteSystemAlertDivCloned[id] == undefined) {
		var obj = document.getElementById('jVoteSystemAlert' + id);
		var newObj = obj.cloneNode(true);
		var newObjIDdiv = document.createElement('div');
		newObjIDdiv.id = 'jvotesystem';
		newObjIDdiv.appendChild(newObj);
		obj.parentNode.removeChild(obj);
		var body = document.getElementsByTagName("body")[0];
		body.insertBefore(newObjIDdiv, body.firstChild);
		jVoteSystemAlertDivCloned[id] = true;
	}
}

function jVoteSystemAlertError(msg, box) {
	jVoteSystemAlertCloneDiv(box);

	document.getElementById('jVoteSystemAlert' + box).style.display='block';
	document.getElementById('jVoteSystemAlert' + box).className = 'alert_Error';
	document.getElementById('jVoteSystemAlert' + box + 'Error').style.display='block';
	document.getElementById('jVoteSystemAlert' + box + 'Success').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'Question').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'Captcha').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonOK').style.display='block';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonYES').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonNO').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonSUBMIT').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'Message').innerHTML = msg;
}

function jVoteSystemAlertSuccess(msg, box) {
	jVoteSystemAlertCloneDiv(box);
	
	document.getElementById('jVoteSystemAlert' + box).style.display='block';
	document.getElementById('jVoteSystemAlert' + box).className = 'alert_Success';
	document.getElementById('jVoteSystemAlert' + box + 'Error').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'Success').style.display='block';
	document.getElementById('jVoteSystemAlert' + box + 'Question').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'Captcha').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonOK').style.display='block';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonYES').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonNO').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonSUBMIT').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'Message').innerHTML = msg;
}

var jVoteSystemAlertQuestionFunction;
function jVoteSystemAlertQuestion(msg, box, f) {
	jVoteSystemAlertCloneDiv(box);
	
	jVoteSystemAlertQuestionFunction = f;
	document.getElementById('jVoteSystemAlert' + box).style.display='block';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonYES').onclick = new Function("jVoteSystemAlertQuestionAnsweredYes('" + box + "')");
	document.getElementById('jVoteSystemAlert' + box).className = 'alert_Question';
	document.getElementById('jVoteSystemAlert' + box + 'Error').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'Success').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'Question').style.display='block'
	document.getElementById('jVoteSystemAlert' + box + 'Captcha').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonOK').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonYES').style.display='block';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonNO').style.display='block';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonSUBMIT').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'Message').innerHTML = msg;
}

function jVoteSystemAlertQuestionAnsweredYes(box) {
   document.getElementById('jVoteSystemAlert' + box).style.display='none';
   var strFun = jVoteSystemAlertQuestionFunction;
   var ret = eval(strFun);
}

var jVoteSystemAlertCaptchaURL;
function jVoteSystemAlertCaptcha(url, box, call) {
	jVoteSystemAlertCloneDiv(box);

	code = 'Captcha is loading..';

	jVoteSystemAlertCaptchaURL = url;
	document.getElementById('jVoteSystemAlert' + box).style.display='block';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonSUBMIT').onclick = new Function("jVoteSystemAlertCaptchaEntered('" + box + "','" + call + "')");
	document.getElementById('jVoteSystemAlert' + box).className = 'alert_Question';
	document.getElementById('jVoteSystemAlert' + box + 'Error').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'Success').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'Question').style.display='none'
	document.getElementById('jVoteSystemAlert' + box + 'Captcha').style.display='block';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonOK').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonYES').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonNO').style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'ButtonSUBMIT').style.display='block';
	document.getElementById('jVoteSystemAlert' + box + 'Container').innerHTML = code;
	document.getElementById('jVoteSystemAlert' + box + 'Message').style.display='none';
	
	showRecaptcha('jVoteSystemAlert' + box + 'Container', 'red');
}

function jVoteSystemAlertCaptchaEntered(box, call) {
	document.getElementById('jVoteSystemAlert' + box + 'Container').style.display='none';
	document.getElementById('jVoteSystemAlert' + box).style.display='none';
	document.getElementById('jVoteSystemAlert' + box + 'Message').style.display='block';
   
   var captcha = document.getElementById('recaptcha_response_field').value;
   var challenge = document.getElementById('recaptcha_challenge_field').value;
   do_http_post_request(jVoteSystemAlertCaptchaURL, call, 'captcha=' + captcha + '&recaptcha_challenge_field=' + challenge, box);
}
