function trim(str){
	str = str.replace(/^\s+/,'');
	for (var i = str.length - 1; i >= 0; i--) {
		if(/\S/.test(str.charAt(i))) {
			str = str.substring(0, i + 1);
			break;
		}
	}
	return str;
}

/*===============================================================================*/

var theLoginDivOpen = 0;
var blockStatusses = new Array();

function expandLogin(){
	if((typeof(blockStatusses['loginform']) == 'undefined') || (blockStatusses['loginform'] == 1)){
		blockStatusses['loginform'] = 0;

		if(theLoginDivOpen == 1){
			$('loginform').slideUp({
				afterFinish: function() {
					theLoginDivOpen = 0;
					blockStatusses['loginform'] = 1;
				}
			});
		}
		else{
			$('loginform').slideDown({
				afterFinish: function() {
					theLoginDivOpen = 1;
					blockStatusses['loginform'] = 1;
				}
			});
		}
	}
}

/*===============================================================================*/

function checkText(theType,theStatus){
	var theTypeText = (theType == 'username') ? 'Uw gebruikersnaam' : ((theType == 'password') ? 'Uw wachtwoord' : 'Uw e-mailadres');

	if((theStatus == 0) && ($F(theType) == ''))
		$(theType).value = theTypeText;
	if((theStatus == 1) && ($F(theType) == theTypeText))
		$(theType).value = '';
}

/*===============================================================================*/

function checkEmail(theCheckField){
	var reg1 = new RegExp('(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)');
	var reg2 = new RegExp('^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,4})(\\]?)$');

	if(!(!reg1.test(theCheckField) && reg2.test(theCheckField)))
		return false;

	return true;
}

/*===============================================================================*/

function checkField(theForm,theField,theFieldType){
	var theCheckField = theForm.elements[theField];
	var errorDetected = false;

	switch(theFieldType){
		case 'textarea':
		case 'date':
		case 'text':
	  		if(trim(theCheckField.value) == ''){
	  			errorDetected = true;
	  		}
	  		break;
		case 'select':
	  		if((theCheckField.selectedIndex == -1) || (theCheckField.options[theCheckField.selectedIndex].value == 0)){
	  			errorDetected = true;
	  		}
	  		break;
	}
	
	if(errorDetected)
		return false;
	else
		return true;
}


/*===============================================================================*/

function showLoader(destinationElement){
	$('loaderDiv').style.display = '';
	$('loaderDiv').clonePosition($(destinationElement));

	$('loaderDiv').style.left = (parseInt($('loaderDiv').style.left) + 15)+'px';
	$('loaderDiv').style.width = (parseInt($('loaderDiv').style.width) - 35)+'px';
}

/*===============================================================================*/

function sendRequest(){
	if(checkEmail($F('email'))){
		showLoader('content-right-mailing-form');

		new Ajax.Request('request.php', {
			method: 'get',
			parameters: {
				'email': $F('email')
			},
			onComplete: function(transport) {
				$('loaderDiv').innerHTML = 'Uw gegevens zijn opgeslagen. U ontvangt van ons een bericht zodra onze nieuwe website gelanceerd is.';
				$('loaderDiv').className = 'loaderDivDone';
			}
		});
	}
	else
		alert('Geen geldig e-mailadres');
}

/*===============================================================================*/

function sendSubscribeRequest(){
	if(checkField($('theSubscribeForm'),'organisatie','text') && checkField($('theSubscribeForm'),'contactpersoon','text') && checkField($('theSubscribeForm'),'functie','select') && checkField($('theSubscribeForm'),'telefoon','text') && checkField($('theSubscribeForm'),'eerstevoorkeur','select') && checkField($('theSubscribeForm'),'tweedevoorkeur','select') && checkEmail($F('email')) && ($F('eerstevoorkeur') != $F('tweedevoorkeur'))){
		return true;
	}
	else
		alert('U dient alle gegevens correct in te voeren om uw aanmelding te versturen. Tevens mag uw eerste keuze niet gelijk zijn aan uw tweede keuze.');
	
	return false;
}
