/**
 * Create the ajax responders
 */
Ajax.Responders.register( {
	onCreate: function() {
		Ajax.activeRequestCount++;
		showLoader();
	},
	onComplete: function() {
		Ajax.activeRequestCount--;
		showLoader();
	}
});

/**
 * Show the ajax loader
 */
function showLoader()
{
	if(Ajax.activeRequestCount) {
		// Show the loader
		$('loader').show();
		$('errormessage').hide();	
		$('noticemessage').hide();			
		// Disable all forms
		$$('form').each(function(obj){
			obj.disable();
		});	
	}
	else {
		// Hide the loader
		$('loader').hide();
		
		// Disable all forms
		$$('form').each(function(obj){
			obj.enable();
		});		
	}
}

//function sendRequest(file, methode, postid) {
function sendRequest(file, methode, param1,param2) {
	//If this call is already running stop!
	if(Ajax.activeRequestCount && !div) {	
		return false;		
	}
	// Prepare the request url
	
	/*
	 * NOTE FROM CORRADO: this is a loooong story, it took almost 2 full days to fix the bug..
	 * there was this error: a school was logged in, after having clicked "Klas toevoegen"
	 * and having selected a class, the system was not able to retrieve the class details.
	 * more specifically, the POST request was not handled, the module's method was not able
	 * to retrieve the POST parameters. furthermore, FIREBUG was showing, once the edit class
	 * button was clicked, that TWO messages were sent, both POST and GET. So i started looking
	 * around, and found here, in this function, that by removing the final '/' on the URL
	 * variable everything was working fine. SO the moral is: if something goes wrong somewhere
	 * else it is advisable to create a new function sendRequest as a patch.. do you know
	 * what i mean?
	 */
	
	//old statement
	//var url = basepath + 'ajax/' + file + '/' + methode + '/';
	
	//new statement
	var url = basepath + 'ajax/' + file + '/' + methode;

	// Do the ajax request
	new Ajax.Request(url, {
		method: 'post',
		parameters: { id: param1, id2: param2 },
		onSuccess: function(transport, json) {
			if(!json) {
				json = eval('(' + transport.responseText + ')');
			}

			// If we have an alert string we alert it
			if(json.alert) {
				showErrormessage(json.alert,json.type);
				return false;
			}
			
			//Display the notice
			if(json.notice) {
				showNotice(json.notice);
			}	
			
			//Update the list if present
			if(json.list) {
				$('admin_list').innerHTML = json.list;			
			}
			
			//If theres a notice, update the admin_list frame with method X
			$('admin_result').innerHTML = json.html;
		},
		onFailure: function(transport) {
			alert('getUpdate ' + methode + ' mislukt');
		}
	});
	return false;
}

function updateRequest(file, methode, div) {
	// Prepare the request url
	
	//CORRADO NOTE: read the note I wrote on sendRequest function!
	
	//old statement
	//var url = basepath + 'ajax/' + file + '/' + ucFirst(methode) + '/';
	//new statement
	var url = basepath + 'ajax/' + file + '/' + ucFirst(methode);

	// Do the ajax request
	new Ajax.Request(url, {
		method: 'post',
		parameters: { update: div },
		onSuccess: function(transport, json) {
			if(!json) {
				json = eval('(' + transport.responseText + ')');
			}
			
			// If we have an alert string we alert it
			if(json.alert) {
				showErrormessage(json.alert,json.type);
				return false;
			}

			// Hide the menu
			$('midcontent').hide();
			$('topimage').hide();

			//Display the notice
			if(json.notice) {
				showNotice(json.notice);
			}
			//Update the list if present
			if(json.list) {
				$('admin_list').innerHTML = json.list;			
			}		
			$(div).innerHTML = json.html;
		},
		onFailure: function(transport) {
			alert('getUpdate ' + methode + ' mislukt');
		}
	});
	return false;
}

function postRequest(file, methode) {
	//If this call is already running stop!
	if(Ajax.activeRequestCount)
	{	
		return false;		
	}
	// Prepare the request url
	
	// NOTE FROM CORRADO: read the comments i wrote about sendRequest to understand this change
	
	//old statement
	//var url = basepath + 'ajax/' + file + '/' + methode + '/';
	
	//new statement
	var url = basepath + 'ajax/' + file + '/' + methode;
	
	var params = $(methode).serialize(true);

	// Do the ajax request
	new Ajax.Request(url, {
		method: 'post',
		parameters: params,
		onSuccess: function(transport, json) {
			if(!json) {
				json = eval('(' + transport.responseText + ')');
			}
						
			// If we have an alert string we alert it
			if(json.alert) {
				showErrormessage(json.alert, json.type);
				return false;
			}
			
			//Display the notice
			if(json.notice) {
				showNotice(json.notice);
			}
			
			//Update the list if present
			if(json.list) {
				$('admin_list').innerHTML = json.list;			
			}					
			$('admin_result').innerHTML = '';
		},
		onFailure: function(transport) {
			alert('postRequest ' + methode + ' mislukt');
		}
	});
	return false;
}

function postQuestion(qstion, anwsr)
{
	//If this call is already running stop!
	if(Ajax.activeRequestCount) {	
		return false;		
	}
	// Prepare the request url
	var url = basepath + 'ajax/kanjertest/questions';

	// Do the ajax request
	new Ajax.Request(url, {
		method: 'post',
		parameters: {
			question: qstion,
			anwser: anwsr
		},
		onSuccess: function(transport, json) {
			if(!json) {
				json = eval('(' + transport.responseText + ')');
			}
			
			// If we have an alert string we alert it
			if(json.alert) {
				showErrormessage(json.alert, json.type);
				return false;
			}
			
			//Display the notice
			if(json.notice) {
				showNotice(json.notice);
			}
			
			//Update the list if present
			$('maincontent').innerHTML = json.html;			
		},
		onFailure: function(transport) {
			alert('postRequest ' + methode + ' mislukt');
		}
	});
	return false;
}

function startTest() {
	updateRequest('kanjertest', 'questions', 'maincontent');	
}

function stopTest() {
	location.href = basepath + 'logout/';
}

function showErrormessage(message,type) {
	if(type)
	{
		return alert(message);
	}
	var elem = $('errormessage');
	elem.show();
	elem.innerHTML=message;	
}

function showNotice(message) {
	var elem = $('noticemessage');
	elem.show();
	elem.innerHTML=message;	
}

function confirmation(file, method, nname, id) {
	var test = confirm('Wilt u ' + nname + ' verwijderen?');
	if(test) {
		sendRequest(file, 'delete' + ucFirst(method), id);
		return true;
	}
	return false;
}

function ucFirst(value) {
	return value.substr(0,1).toUpperCase() + value.substr(1,value.length);	
}

function hideError() {
	$('errormessage').hide();
}

function toggleDiv(name) {
	$(name).toggle();
}

function printDiv(div) {
	if(!div) {
		var div = 'docentenscroll';
	}
	var a = window.open(div,div + 'Kanjer','width=750,height=600,scrollbars=yes');
	a.document.open("text/html");
	a.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">');
	a.document.write('<html><head>');
	a.document.write('<link rel="stylesheet" type="text/css" href="http://www.kanjertraining.nl/kanjertraining/res/css/style.css" media="screen" />');
	a.document.write('<link rel="stylesheet" type="text/css" href="http://www.kanjertraining.nl/kanjertraining/res/css/print.css" media="print" />');
	a.document.write('<link rel="stylesheet" type="text/css" href="http://www.kanjertraining.nl/kanjertraining/res/css/admin.css" media="screen" />');
	a.document.write('</head><body><font size="2" family="arial">Kanjertraining volg en advies systeem</font><hr>');
	a.document.write('<div id="docentenscroll" class="scrollbar">');
	a.document.write($(div).innerHTML);
	a.document.write('</div>');
	a.document.write('</body></html>');
	a.document.close();
	a.print();
}

function windowopen(name, url) {
	var popup = window.open(url,name,'width=400,height=450,top=200,left=500,location=no,status=no,menubar=no,directories=no,toolbar=no,resizable=no,scrollbars=yes');
	popup.focus();
	return false;
}

function enableSel(sel1,sel2,sel3)
{
	var obj1 = document.getElementById(sel1);
	var obj1_length = obj1.length;
	
	var obj2 = document.getElementById(sel2);
	var obj2_length = obj2.length;
	
	var obj3 = document.getElementById(sel3);
	
	// Removing of eventual elements in the
	// second drop-down list
	if(obj2_length >0){
		for(h=0;h<obj2_length;h++){
			obj2.remove(obj2.options[h]);
		}
	}
	
	// Copying of all the elements from the first to the second
	// drop-down list on exeption of the selected element
	if(obj1.options[obj1.selectedIndex].value != ''){
		// Option Index of the element to remove
		var rem_index   = obj1.options[obj1.selectedIndex].value;
		// Enable the select-object
		obj2.disabled = false;
		obj3.disabled = false;
		
		for(i=0;i<obj1_length;i++){
			if( (obj1.options[i].value != obj1.options[obj1.selectedIndex].value) &&
			(obj1.options[i].value != '')){
				var temp  = document.createElement('option');
				temp.text = obj1.options[i].text;
				temp.value= obj1.options[i].value;
				try{
					obj2.add(temp,null); // standards compliant
				}
				catch(ex){
					obj2.add(temp); // IE only
				}
			}
		}
		return rem_index;
	}else if (obj1.value == ''){
		// Disabling the second drop-down list if an empty element
		// in the first list is choosen
		obj2.disabled = true;
		obj3.disabled = true;
		return false;
	}
}

function checkSelectForm(field1,field2){
	obj1 = document.getElementById(field1);
	obj2 = document.getElementById(field2);
	if(obj1.options[obj1.selectedIndex].value == ''){
		alert('Om door te gaan dient u een klas te selecteren');
		//obj1.options[0].focus();
		return false;
	} else if(obj2.options[obj2.selectedIndex].value == ''){
		alert('Het is van belang een 2de niveau klas te kiezen');
		//obj2.options[0].focus();
		return false;
	}
	return true;
}

function confirmChange(){
	var risp = confirm('Klik om door te gaan.\nLet Op! Deze stap is onkeerbaar');
	if(risp==true){
		return true;
	}else {
		return false;
  	}
}

function change_disabled(id){
	var but = document.getElementById(id);
	if(!but.disable){
		but.disable=true;
	} else{
		but.disable=false;
	}
}

 function chMd()
 {
 	if (document.insertemail.emailRadio[0].checked) {
		document.insertemail.emailField.disabled = false;
	} else if(document.insertemail.emailRadio[1].checked) {
		document.insertemail.emailField.disabled = true;
	}
}

function handleOnChange(dd1)
{
  var idx = dd1.selectedIndex;
  var val = dd1[idx].text;
  var par = document.forms["selectKlas"];
  var parelmts = par.elements;
  var prezsel = parelmts["klas"];
  var country = val;
  
  var pumello = parelmts["pumello"];
  pumello.value = parelmts["klas"].value;
  pumello.action= parelmts["klas"].value;
    
  var testo = parelmts["pippo"];
  testo.value = parelmts["klas"].value.toString();
}

function SetAllCheckBoxes(FormName, FieldName)
{
	if(!document.forms[FormName])
		return;
		
	var CheckValue = document.forms[FormName].elements['selectAll'].checked;
		
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if(!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if(!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for(var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;
}



