var popup = null;
var popup_id = null;

var blockProps = {
	'theme' : {
		width : 333,
		height : 89,
		title : 'Th&egrave;me'
	}
}

// -----------------------------------------------
function checkData(name, ret)
{

	params  = 'action=checkData&';
	params += getFormValuesWithEmptyValues(name);
	new Ajax.Request('/lib/Ajax/ajax.client.php', {method:'post', postBody:params, onSuccess: function(request){checkDataResponse(name, ret, request)}});
}

// -----------------------------------------------
function checkDataResponse(name, ret, req)
{
   if (req.responseText !== 'null') {
		ShowErrorMessage('');
		clearErrors(name);
		var res = eval('(' + req.responseText + ')');
		for (var i=0; i<res.length; i++) {
			printError(res[i]['field'], res[i]['text']);
		}
	} else {
		
		var get = getGet(document.location.href);
		
		if      (ret == 'overview') document.location.href = "/inscription_client.php?page=3";
		else if (ret == 'modify_overview') document.location.href = "/modify_client.php?page=3";
		else if (ret == 'modify_overview_admin') document.location.href = "/admin/page.php?edit_page=103&" + get;			
		else if (ret == 'modify') document.location.href = "/modify_client.php";
		else if (ret == 'inscription_by_admin')	{document.location.href = "/admin/page.php?" + get; }
		else if (name == 'inscriptionP1') document.location.href = "/inscription_client.php?page=3";
		else     document.location.href = "/inscription_client.php";
	}
}

// -----------------------------------------------
function printError(name, text)
{
	el = document.getElementById('error_'+name);
	el.innerHTML = text;
}
// -----------------------------------------------
function clearErrors(name)
{
	if (name == 'inscriptionP1') {
		var errors = Array('sexe_id', 'client_statut_id', 'telephone1', 'prenom', 'nom', 'societe', 'adresse', 'code_postal', 'ville', 'pays_id', 'email', 'password', 'societe_activite_id', 'effectifs_id');
		for (var i=0; i<errors.length; i++) if ($("error_"+errors[i])) $("error_"+errors[i]).innerHTML = '';
	}
}

function updateTheme(el,id,n)
{
	name = el.name;//alert('div_' + name);
	params = 'action=updateTheme&';
	params += 'name=' + name + '&';
	params += 'n='+n+'&id='+id+'&';
	params += 'table=theme_secteur&';
	params += 'parentField=theme_id&';
	params += 'parentValue=' + getFormElementValue(el);
	//document.write(params);
	new Ajax.Request('/lib/Ajax/ajax.client.php', {method:'post', postBody:params, onSuccess: function(request){updateThemeResponse(name,request,id)}});
//	new Ajax.Updater('div_'+name,'/lib/Ajax/ajax.expert.php', {method:'post', postBody:params});
}

function updateThemeResponse(name,req,id)
{
	document.getElementById('div_' + name + id).innerHTML = req.responseText;
	popup.updateWidth();
	popup.updateHeight();
}

// -----------------------------------------------
function updateSecteur(div, el, name, id)
{
	params = 'theme_id=' + getFormElementValue(el) + '&';
	params += 'task=updateSecteur';
	params += '&name='+name+'&id='+id;

	new Ajax.Updater(div, '/lib/Ajax/ajax.client.php', {method:'post', postBody:params});
}

// -----------------------------------------------
function updateSecteur1(div, el)
{
	var fm = document.getElementById('registerform');
	var elt = el;
	//var elt = fm.elements['theme_id[' + num + ']'];
	//alert(div);
	params = 'theme_id=' + getFormElementValue(elt) + '&';
	params += 'task=updateSecteur1';
	//alert(elt);
	//alert(params);
	new Ajax.Updater(div, '/lib/Ajax/ajax.client.php', {method:'post', postBody:params});
}

// -----------------------------------------------
function addSecteur(div)
{
	params  = getFormValues('registerform');
	params += 'task=addSecteur';
	new Ajax.Updater(div, '/lib/Ajax/ajax.client.php', {method:'post', postBody:params});
}

function addBlock(name,id) {
	params = 'action=addBlock&';
	params += 'BlockName=' + name + '&';
	params += 'id=' + id + '&';
	params += getFormValues1(name + 'Form' + id);
	new Ajax.Request('/lib/Ajax/ajax.client.php', {method:'post', postBody:params, onSuccess: function(request){addBlockResponse(name,id,request)}});
}

function addBlockResponse(name,id,req) {

	var res = eval('('+req.responseText+')');
	if (res['errors']) {
		$('errors_'+name+'_'+id).innerHTML = res['errors'];
		popup.updateWidth();
		popup.updateHeight();
	}
	else {
		hidePopup(name,id);
		$('client_' + name).innerHTML = res['html'];
	}
}

function deleteBlock(name,id) {

	params = 'action=deleteBlock&';
	params += 'BlockName=' + name + '&';
	params += 'id=' + id + '&';
	new Ajax.Request('/lib/Ajax/ajax.client.php', {method:'post', postBody:params, onSuccess: function(request){deleteBlockResponse(name,request)}});
}

function deleteBlockResponse(name,req) {
	var res = eval('('+req.responseText+')');
	if (res['errors']) $('errors_'+name+'_'+id).innerHTML = res['errors'];
	else $('client_' + name).innerHTML = res['html'];
}

function changeSelectIndex(select,newValue) {
	for(i in select.options) {
		alert(select[i].value);
	}
}

function showPopup(name,id) {

	if(popup) {
		popup.destroy();
	}

	popup = new Window(
		'client_window', {
			className: 'pclient',
			//parent: $('content'),
			width: blockProps[name].width,
			height: blockProps[name].height,
			top: 175,
			//right: 20,
			left: 810 - blockProps[name].width,
			zIndex: 999,
			showEffect: Element.show,
			hideEffect: Element.hide,
			minimizable: false,
			maximizable: false,
			resizable: true,
			runInlineScripts: true,
			title: blockProps[name].title
		}
	);

	var onCloseObserver = {
		onClose: function() {
			popup = null;
		}
	}

	Windows.addObserver(onCloseObserver);

	popup.setDestroyOnClose();

	popup.setAjaxContent('/lib/Ajax/ajax.client.php', {
		method: 'post',
		postBody: 'action=getFormContent&name='+name+'&id='+id
	},true);
	
	//document.location.href = '#top';
}

function hidePopup(name,id) {
	popup.destroy();
	popup = null;
}

function getGet(str)
{
	var position = str.indexOf('?');
	if (position == -1)
	{
		return "";
	}
	else
	{
		return str.substring(position + 1, 1000);
	}	
}

