var qw = null;
var uploads_in_progress = 0;

// -----------------------------------------------
function doCheckQuestionsForm() {

	var form = document.getElementById('loginform');
	params = getFormValues1('loginform');
	params += 'task=checkQuestionForm';
	new Ajax.Request('/lib/Ajax/ajax.client.php', {method:'post', postBody:params, onSuccess:doCheckHandler});
	//new Ajax.Updater('debug', '/lib/Ajax/ajax.client.php', {method:'post', postBody:params});
}

// -----------------------------------------------
function doCheckHandler(req)
{
	/*if (t.responseText)
		document.getElementById('errors').innerHTML = t.responseText;
	else
		document.location.href = "/add_client_questions.php?done";*/

	if (req.responseText) {
		ShowErrorMessage('');
		$('submit_question').disabled = false;
		clearErrorsQuestion(name);
		var res = eval('(' + req.responseText + ')');
		for (var i=0; i<res.length; i++) {
			printErrorQuestion(res[i]['field'], res[i]['text']);
		}
	} else {
		document.location.href = "/add_client_questions.php?done";
	}
}

// -----------------------------------------------
function doCheckQuestionsFormOnly() {

	var form = document.getElementById('loginform');
	params = getFormValues1('loginform');
	params += 'task=checkQuestionFormOnly';
	new Ajax.Request('/lib/Ajax/ajax.client.php', {method:'post', postBody:params, onSuccess:doCheckOnlyHandler});
	//new Ajax.Updater('debug', '/lib/Ajax/ajax.client.php', {method:'post', postBody:params});
}

// -----------------------------------------------
function doCheckOnlyHandler(req)
{
	/*if (t.responseText)
		document.getElementById('errors').innerHTML = t.responseText;
	else
		document.location.href = "/add_client_questions.php?done";*/

	if (req.responseText) {
		ShowErrorMessage('');
		$('submit_question').disabled = false;
		clearErrorsQuestion(name);
		var res = eval('(' + req.responseText + ')');
		for (var i=0; i<res.length; i++) {
			printErrorQuestion(res[i]['field'], res[i]['text']);
		}
	} else {
		document.location.href = "/add_client_questions.php?page1";
	}
}

// -----------------------------------------------
function doCheckQuestionsFormModalite() {

	var form = document.getElementById('loginform');
	params = getFormValues1('loginform');
	params += 'task=checkQuestionFormModalite';
	new Ajax.Request('/lib/Ajax/ajax.client.php', {method:'post', postBody:params, onSuccess:doCheckModaliteHandler});
	//new Ajax.Updater('debug', '/lib/Ajax/ajax.client.php', {method:'post', postBody:params});
}

// -----------------------------------------------
function doCheckModaliteHandler(req)
{
	/*if (t.responseText)
		document.getElementById('errors').innerHTML = t.responseText;
	else
		document.location.href = "/add_client_questions.php?done";*/

	if (req.responseText) {
		ShowErrorMessage('');
		$('submit_question').disabled = false;
		clearErrorsQuestion(name);
		var res = eval('(' + req.responseText + ')');
		for (var i=0; i<res.length; i++) {
			printErrorQuestion(res[i]['field'], res[i]['text']);
		}
	} else {
		document.location.href = "/add_client_questions.php?done";
	}
}

// -----------------------------------------------
function printErrorQuestion(name, text)
{
	el = document.getElementById('error_'+name);
	el.innerHTML = text;
}

// -----------------------------------------------
function clearErrorsQuestion()
{
	var errors = Array('titre', 'synthese_question', 'theme_id', 'theme_secteur_id', /*'mots_cles',*/'tags', 'budget', 'modalite', 'descriptif', 'delai_reponse_attendu');
	for (var i=0; i<errors.length; i++) {
		if($("error_"+errors[i])) $("error_"+errors[i]).innerHTML = '';
	}
}

// -----------------------------------------------
function doQuestionInactive() {
	var form = document.getElementById('questionform');
	params = getFormValues('questionform');
	params += 'task=QuestionInactive';
	new Ajax.Request('/lib/Ajax/ajax.client.php', {method:'post', postBody:params, onSuccess:doInactivateHandler});
}

// -----------------------------------------------
function doInactivateHandler(t)
{
	if (t.responseText)
		document.getElementById('errors').innerHTML = t.responseText;
	else
		document.location.href = "/list_client_questions.php";
}

function doNotation() {
alert(1);

}

function checkDataResponse(id,req) {
	if (req.responseText) document.getElementById('errors_'+id).innerHTML = req.responseText;
	else document.forms['qform_'+id].submit();
}

function showQuestionPopup(qid,qtype,title) {

	if(qw) qw.destroy();

   qw = new Window(
		'question_window', {
			className: 'pclient',
			minWidth: 330,
			top: 175,
			left: 485,
			zIndex: 150,
			showEffect: Element.show,
			hideEffect: Element.hide,
			minimizable: false,
			maximizable: false,
			resizable: true,
			title: title
		}
	);

	var myObserver = {
		/*onShow: function() {
			qw.updateWidth();
			qw.updateHeight();
		}*/
		onClose: function() {
			qw = null;
		}
	}

	Windows.addObserver(myObserver);
	qw.setDestroyOnClose();

	qw.setTitle(title);

	qw.setAjaxContent('/lib/Ajax/ajax.client.php', {
		method: 'post',
		postBody: 'action=showQuestionPopup&qtype=' + qtype + '&qid='+qid
	});

	document.location.href = '#top';

	//w.show();
}

// --------------------------------------------------------
function initUpload()
{
	if (!$("registerform").media.value) return false;
	params = 'task=getSID';
	new Ajax.Request('/lib/Ajax/ajax.question.php', {method:'post', postBody:params, onSuccess:initUploadHandler});
}

// --------------------------------------------------------
function initUploadHandler(t)
{
	var repObj = eval('(' + t.responseText + ')');
	$("registerform").sid.value = repObj['sid'];
   $("_progress").id = repObj['sid'] + "_progress";
	uploadMedia();
}

// --------------------------------------------------------
function uploadMedia()
{
	var frm = $('registerform');
	var sid = frm.sid.value;
	var pb  = $(sid + "_progress");

	frm.action = '/cgi-bin/upload.cgi?sid=' + sid;
	frm.target = 'upload_target';
	frm.submit();

	uploads_in_progress = uploads_in_progress + 1;

	Element.hide($("media"));
	pb.style.width = "0%";
	Element.show(pb.parentNode);
   new Ajax.Request('/fileprogress.php', {'method': 'post','parameters': 'sid=' + sid, 'onSuccess' : function(request){updateProgress(pb,sid,request)},'onFailure':function(request){updateFailure(pb,sid,request)}});
}

// --------------------------------------------------------
function updateProgress(pb,sid,req) {
	var frm = $('registerform');
	var percent = parseInt(req.responseText);
	if(!percent) percent = 0;
	pb.style.width = percent + "%";

	if(percent >= 100) {
		pb.id = pb.id.replace(sid,"");
		uploads_in_progress = uploads_in_progress - 1;
		Element.hide(pb.parentNode);
		Element.show($("media"));
		params = 'task=uploadFile&sid='+sid;
		new Ajax.Request('/lib/Ajax/ajax.question.php', {method:'post', postBody:params, onSuccess:init});
	}
	else {
		new Ajax.Request('/fileprogress.php', {'method': 'post','parameters': 'sid=' + sid, 'onSuccess' : function(request){updateProgress(pb,sid,request)},'onFailure':function(request){updateFailure(pb,sid,request)}});
	}
}

function init(req) {
	if (req.responseText) document.getElementById('errors').innerHTML = req.responseText;
	selectFiles();
}

function addAttach() {
	params = 'action=addAttach&';
	params += 'pid=' + document.getElementById('pid').value + '&';
	params += 'id=' + getFormElementValue(document.forms['attacher_form'].attacher);
	new Ajax.Request('/lib/Ajax/ajax.comment.php', {method:'post', postBody:params, onSuccess:init});
}

function deleteAttaches() {
	params = 'action=deleteAttaches&';
	params += 'pid=' + document.getElementById('pid').value + '&';
	params += getFormValues1('delete_form');
	new Ajax.Request('/lib/Ajax/ajax.comment.php', {method:'post', postBody:params, onSuccess:init});
}

function selectAttaches() {

	params = 'action=selectAttaches&';
	params += 'pid=' + document.getElementById('pid').value;
	new Ajax.Updater('attaches', '/lib/Ajax/ajax.comment.php', {method:'post', postBody:params});
}

function selectFiles(req) {

	params = 'task=selectFiles&key=question';
	new Ajax.Updater('attaches', '/lib/Ajax/ajax.upload.php', {method:'post', postBody:params});
}

function deleteFile(id) {

	params = 'task=deleteFile&id='+id+'&key=question';
	new Ajax.Request('/lib/Ajax/ajax.upload.php', {method:'post', postBody:params, onSuccess:init});
}

//function init(req) {
//document.getElementById('debug').innerHTML=req.responseText;
//	selectAttaches();
//}



