/**
 * 
 * IFV javascript functions
 * @copyright (c) 2008 NonFood Werbeagentur Unit2 GmbH
 * @version $Revision: 1.5 $
 * @id $Id: config.inc.php,v 1.5 2007/11/21 13:17:59 kai Exp $
 * 
 */

/**
 * @author Mitch
 */
function showDot(targetID) {
	var target = document.getElementById("button_eee_" + targetID);
	target.className = (target.className == 'buttons_dot_green_none')
			? 'buttons_dot_green_hover'
			: 'buttons_dot_green_none';
}
function showLupe(targetID) {
	var target = document.getElementById("button_eea_" + targetID);
	target.className = (target.className == 'buttons_lupe_none')
			? 'buttons_lupe_hover'
			: 'buttons_lupe_none';
}

/**
 * @author kai
 */

var win = null;
function openView(WinUrl, WinName, WinWidth, WinHeight) {
	Left = (screen.width) ? (screen.width - WinWidth) / 2 : 0;
	Top = (screen.height) ? (screen.height - WinHeight) / 2 : 0;
	settings = 'height='
			+ WinHeight
			+ ', width='
			+ WinWidth
			+ ', top='
			+ Top
			+ ', left='
			+ Left
			+ ', dependent=yes,hotkeys=yes,innerHeight=680,innerWidth=640,location=no,resizable=no,scrollbars=no,status=no,toolbar=no'
	win = window.open(WinUrl, WinName, settings);
	win.focus();
}

/* ----------- tooltip ----------- */
wmtt = null;

document.onmousemove = updateWMTT;

function updateWMTT(e) {
	x = (document.all) ? window.event.x + document.body.scrollLeft : e.pageX;
	y = (document.all) ? window.event.y + document.body.scrollTop : e.pageY;
	if (wmtt != null) {
		wmtt.style.left = (x + 15) + "px";
		wmtt.style.top = (y + 15) + "px";
	}
}

function showWMTT(id) {
	wmtt = document.getElementById(id);
	wmtt.style.display = "block"
}

function hideWMTT() {
	wmtt.style.display = "none";
}

/* ----------- Seminar Selectboxen ----------- */

function toggleSelect(sBox) {
	var x = document.getElementById('select_container')
			.getElementsByTagName('select');
	for (var i = 0;i < x.length; i++) {
		x[i].style.display = 'none';
	}
	document.getElementById(sBox).style.display = 'block';
}
/* ----------- Switch Select ----------- */
var step=0;
function switchSel(selObj, EvId, maxCount) {
	if (selObj.id.indexOf('available') != -1 & selObj.selectedIndex != -1 & document.getElementById('select' + EvId).length < maxCount) {
		var source = selObj;
		var dest = document.getElementById('select' + EvId);
		dest.appendChild(source.options[source.selectedIndex]);
		step++;
	}
	
	if (selObj.id.indexOf('select') != -1 & selObj.selectedIndex != -1) {
		var source = selObj;
		var dest = document.getElementById('available' + EvId);
		dest.appendChild(source.options[source.selectedIndex]);
		step--;
	}
	
	document.getElementById('label' + EvId).firstChild.nodeValue = document.getElementById('select' + EvId).length + ' von ' + maxCount + ' Teilnehmer ausgewählt';
	
	if (document.getElementById('select' + EvId).length == maxCount) {
		document.getElementById('available' + EvId).disabled = true;
	} else {
		document.getElementById('available' + EvId).disabled = false;
	}
	
	//dump(step + " : " + maxUser + "\n");
       var abschicken = document.getElementById('savebookingbtn');
	if (step == maxUser) {
		//document.getElementById('savebookingbtn').disabled = false;
      abschicken.removeAttribute('disabled');
	} else {
		//document.getElementById('savebookingbtn').disabled = true;
      abschicken.setAttribute('disabled','disabled');
	}
}

/* --------------------------------------------------------------------------
 * user list handling
 ----------------------------------------------------------------------------*/
var fproc;
var fid;
function addUserList(formId, did) {
	fid = did;
	/*document.getElementById('loading').style.display='block';*/
	document.getElementById('success' + fid).style.display = 'none';
	document.getElementById('error' + fid).style.display = 'none';
	if (!fproc) {
		fproc = new fProcess();
	}

	fproc.onsuccess = addUserListSuccessHandler;
	fproc.onerror = addUserListErrorHandler;
	fproc.init(formId, '/binary/' + fid + '/page/addUserList.xml');
	fproc.submit();
}

function addUserListSuccessHandler(res) {
	var list = document.getElementById('teaser').getElementsByTagName('select');
	if (list.length == 0)
		return;
	var workList = [];
	for (var t = 0;t < list.length; t++) {
		if (list[t].id.indexOf('available') == -1)
			continue;
		workList.push(list[t]);
	}
	for (t = 0;t < workList.length; t++) {
		var newSel = document.createElement('option');
		newSel.setAttribute('value', res.content.uid);
		newSel.appendChild(document.createTextNode(res.content.user));
		workList[t].appendChild(newSel);
	}
	document.getElementById('success' + fid).style.display = 'block';
	document.getElementById('error' + fid).style.display = 'none';
	document.getElementById('addUserList' + fid).reset();
	fproc.resetStyles();
}

function addUserListErrorHandler(res) {
	/*document.getElementById('loading').style.display='none';*/
	document.getElementById('success' + fid).style.display = 'none';
	document.getElementById('error' + fid).style.display = 'block';
	fproc.applyErrorStyles(res.errorNodes);
}


var fjson;
function saveBooking() {
	var data = {};
	var list = document.getElementsByTagName('select');
	for(x=0;x<list.length;x++) {
		if (list[x].id.indexOf('select') == -1) {
			continue;
		}
		items = list[x].getElementsByTagName('option');
		if (items.length > 0) {
			id = list[x].id.split('select')[1];
			tmp = [];
			for (i=0;i<items.length;i++) {
				tmp.push(items[i].getAttribute('value'));
			}
			data[id] = tmp;
		}
	}
	

	if (!fjson) {
		fjson = new fJsonRPC();
	}
	
	fjson.init('/binary/fjson.xml','ifv::web');
	fjson.onload = saveBookingResultHandler;
	fjson.exec('createBooking', data);
}

function saveBookingResultHandler(res, callMethod) {
	if (res == 'OK') {
		window.location.href = '/de/site/Unsere_Angebote/Seminare/Order/order_succ.xml';
	}
}



