// A function which validates and submits the "Call me" quickcall form and
// calls an AJAX request to send an e-mail
function fCallMeSend() {
	// Validate the form first
	var iValidation = 0;
	if (($('txtName').get('value') == "") || ($('txtName').get('value') == "Vul uw naam in..")) {
		setErrText2('txtName', 'naam is een verplicht veld', 'do');
		iValidation++;
	} else {
		setErrText2('txtName', '', 'undo');
	}
	
	if (($('txtTelNr').get('value') == "") || ($('txtTelNr').get('value') == "Vul uw telnr in..")) {
		setErrText2('txtTelNr', 'telefoonnummer is een verplicht veld', 'do');
		iValidation++;
	} else {
		setErrText2('txtTelNr', '', 'undo');
	}

	if (iValidation == 0) {
		// Hide all form elements
		$$('#quickcall div.err_message').setStyle('display', 'none');
		$$('#quickcall input').setStyle('display', 'none');
		$$('#quickcall #label1').setStyle('display', 'none');
		$$('#quickcall #label2').setStyle('display', 'none');

		// Generate an AJAX icon and information message for the user during the wait
		var divAjax = new Element("div", {"class": "ajaxInfo"}).injectInside($$('#quickcall fieldset')[0]);
		var imgAjax = new Element("img", {
			"src": sUrlPrefix + "media/img/icon-ajax.gif",
			"width": "32",
			"height": "32",
			"class": "ajaxIcon",
			"alt": ""
		}).injectInside(divAjax);
		var spanInfo = new Element("span", {"text": "Bericht wordt verzonden..."}).injectInside(divAjax);
		
		// Make the AJAX call
		var sendmailRequest = new Request.JSON({
			url: sUrlPrefix + "media/include/ajaxreq_sendmail.asp",
			onSuccess: function(response) {
				
				// Get rid of the AJAX message and icon
				$$('#quickcall div.ajaxInfo')[0].empty();

				// Depending on the JSON result, display an error or success message
				if (response.result == "error") {
					var spanError = new Element("span", {
						"class": "error",
						"text": "Het bericht kon niet worden verstuurd, want: "
					}).injectInside(divAjax);

					var spanReason = new Element("span", {
						"class": "error",
						"text": response.message
					}).injectAfter(spanError);
					var brReason = new Element("br").injectAfter(spanReason);

					var aAttempt = new Element("a", {
						"href": "#",
						"title": "Probeer het nogmaals",
						"html": "Probeer het nogmaals &raquo;"
					}).injectAfter(brReason);

					aAttempt.addEvent('click', fResetCallMe);
				} else if (response.result == "success") {
					// Remove all of the form elements from the DOM
					$$('#quickcall div.err_message').dispose();
					$$('#quickcall input').dispose();

					// Display a success message
					var imgSuccess = new Element("img", {
						"src": sUrlPrefix + "media/img/icon-success_lightbrown.gif",
						"width": "16",
						"height": "16",
						"alt": ""
					}).injectInside(divAjax);
					var spanSuccess = new Element("span", {
						"class": "success_message",
						"text": response.message
					}).injectInside(divAjax);
				}
			}
		}).get({
			'req': 'quickcall',
			'title': $('hidTitle').get('value'),
			'name': $('txtName').get('value'),
			'telnr': $('txtTelNr').get('value'), 
			'email': cfgContactMail
		});
	} else {
		OverText.update();
	}
	
}

function setErrText2(objName, txt, direction) {
	if (!$(objName + '_err')) { alert('SCRIPTFOUT: '+objName+'_err object bestaat niet!'); return false; }
	var objErr = $(objName + '_err')
	var objField = $(objName);
	if (direction == 'do') {
		objErr.innerHTML = txt;
		objErr.style.display = 'block';
		objField.style.border = '1px solid #50a8d8';


	} else {
		objErr.innerHTML = '';
		objErr.style.display = 'none';
		objField.style.border = '1px solid #50a8d8';

	}
}


// A function which returns the quickcall form to its original, pre-AJAX state
function fResetCallMe() {
	$$('#quickcall div.ajaxInfo')[0].dispose();
	$$('#quickcall input').setStyles({
		display: 'inline', 
		border: '1px solid #ffffff'
	});
	$$('#quickcall #label1').setStyles({
		display: 'block'
	});
	$$('#quickcall #label2').setStyles({
		display: 'block'
	});

	return false;
}

