// JavaScript Document

function check_username_exists(username,return_div) {
	
	usr = $(username).value;
	
	if (usr.length > 2) {
		
		url = ajax_root + "check_username_exists.php";
		
		//new ajax(url,{postBody:'username='+usr, onComplete: showResponse, update:return_div}).request();
		new Ajax(url, {
			method: 'post', 
			data: {'username': usr},
			update: $(return_div)
			}).request();

	}
	
	
	
}

function check_email_type(inp,return_div) {
	
	email = $(inp).value;
	
	if (email.length > 7) {
		
		var mail_client = "";
		
		if (email.indexOf("@live.") != -1) {
			mail_client = "Live.com";
		}

		if (email.indexOf("@hotmail.") != -1) {
			mail_client = "Hotmail.com";
		}

		if (email.indexOf("@yahoo.") != -1) {
			mail_client = "Yahoo.com";
		}
		
		if (!mail_client == "") {
		
			div = $(return_div);
			div.innerHTML = "<b>Let op!</b> Het kan zijn dat de activatiemail in de box 'ongewenste berichten' van "+mail_client+" terecht komt.";
		
		}

	}
	
	
	
}

function get_sub_categories(div,cat,depth,selected) {
	
	url = ajax_root + "get_sub_catagories.php";
	
	new Ajax(url, {
		method: 'get', 
		data: {'cat_id': cat,'depth': depth,'selected':selected},
		update: $(div)
		}).request();
	
}

function vote(item_id,vote) {
	
	url = ajax_root + "vote.php";
	
	new Ajax(url, {
		method: 'get', 
		data: {'id': item_id,'vote': vote}
		}).request();
	
	div = $("vote_box");
	div.innerHTML = "<img src='/img/vote/"+vote+".gif'>";
	
}

function place_offer() {
	
	var error = '';
	var error_count = 0;
	
	id = $('auction_id').getValue();
	//alert("id: "+id);
	name = $('auction_name').getValue();
	//alert("name: "+name);
	email = $('auction_email').getValue();
	//alert("email: "+email);
	offer_val = $('auction_offer').getValue();
	
	
	
	if (name.length == 0) {
		error += '- Vul je naam in\n';
		error_count ++;
	}

	
	if (check_email(email) == false) {
	
		error += '- Vul een geldig e-mailadres in\n';
		error_count ++;
	
	}

	if (offer_val.length == 0) {
		error += '- Plaats een bod\n';
		error_count ++;
	}

	if (error_count > 0) {
		
		alert(error)
		
	} else {
		
		url = ajax_root + "place_offer.php";
	
		new Ajax(url, {
			method: 'post', 
			data: {'name': name,'email': email,'offer': offer_val, 'id': id},
			update: $('last_offers')
			}).request();
		
		$("offer_form").setStyle("display", "none");
		var myFx = new Fx.Scroll(window).toElement('last_offers');
	
	}

}

function send_auction_message() {
	
	var error = '';
	var error_count = 0;
	
	id = $('auction_id');
	id = id.value;
	
	name = $('auction_name');
	name = name.value;
	
	if (name.length == 0) {
		error += '- Vul je naam in\n';
		error_count ++;
	}

	email = $('auction_email');
	email = email.value;
	
	if (check_email(email) == false) {
	
		error += '- Vul een geldig e-mailadres in\n';
		error_count ++;
	
	}

	message = $('message');
	message = message.value;
	
	if (message.length == 0) {
		error += '- Vul een bericht in\n';
		error_count ++;
	}

	if (error_count > 0) {
		
		alert(error)
		
	} else {
		
		url = ajax_root + "send_auction_message.php";
	
		new Ajax(url, {
			method: 'post', 
			data: {'name': name,'email': email,'message': message, 'id': id},
			update: $('contact_form')
			}).request();
		
		//$("offer_form").setStyle("display", "none");
	
	}

}
function make_request() {
	
	var error = '';
	var error_count = 0;
	var date = '';
	var has_date = true;
	
	id = $('user_id').getValue();
	//id = id.value;
	
	name = $('request_name').getValue();
	//name = name.value;
	
	if (name.length == 0) {
		error += '- Vul je naam in\n';
		error_count ++;
	}

	email = $('request_email').getValue();
	//email = email.value;
	
	if (check_email(email) == false) {
	
		error += '- Vul een geldig e-mailadres in\n';
		error_count ++;
	
	}

	telephone = $('request_telephone').getValue();
	//telephone = telephone.value;
	
	day = $('request_day').getValue();
	//day = day.value;
	if (isNaN(day)) {
		var has_date = false;
//		error += '- Geef een datum op (dag)\n';
//		error_count ++;
	}
	month = $('request_month').getValue();
	//month = month.value;
	if (isNaN(month)) {
		var has_date = false;
//		error += '- Geef een datum op (maand)\n';
//		error_count ++;
	}
	year = $('request_year').getValue();
	//year = year.value;
	if (isNaN(year)) {
		var has_date = false;
//		error += '- Geef een datum op (jaar)\n';
//		error_count ++;
	}
	
	if (has_date == true) {
		date = year + "-" + month + "-" + day;
	}

	message = $('request_message').getValue();
	//message = message.value;
	
	if (message.length == 0) {
		error += '- Vul de aanvullende informatie in\n';
		error_count ++;
	}

	if (error_count > 0) {
		
		alert(error)
		
	} else {
		
		url = ajax_root + "request_booking.php";
	
		new Ajax(url, {
			method: 'get', 
			data: {'name': name,'email': email,'telephone': telephone,'date': date,'message': message, 'id': id},
			update: $('last_bookings')
			}).request();
		
		$("div_request_form").setStyle("display", "none");
		var myFx = new Fx.Scroll(window).toElement('last_bookings');
	
	}

}

function post_reaction() {
	
	loader('comment_loader');
	
	url = ajax_root + 'post_reaction.php';
	
	item_id = $('item_id').getValue();
	name = $('comment_name').getValue();
	text = $('comment_text').getValue();
	
	new Ajax(url, {
		method: 'get',
		data: {'id': item_id,'name': name,'text': text},
		onRequest: function() { loader('comment_loader'); },
		onComplete: function(response) {
			var r = response.split('][');
			if (r[0] == 0) {
				$('comments_error_holder').setHTML(r[1]);	
			} else {
				$('comments_holder').setHTML(r[1]);	
				$('comments_error_holder').setHTML('');
				$('comment_text').value = '';
				var myFx = new Fx.Scroll(window).toElement('comments_holder');
			}
			
		}
	}).request();
	
	unload('comment_loader');
}

function loader(div) {
	$(div).setHTML('<img src="/img/loader.gif" alt="loading..." title="loading..." />');
}
function unload(div) {
	$(div).setHTML('');
}


function send_taf() {
	
	url = ajax_root + 'tellafriend.php';
	
	var from_name = $('from_name').getValue();
	var from_email = $('from_email').getValue();
	var to_name = $('to_name').getValue();
	var to_email = $('to_email').getValue();
	var pageurl = location.href;
	
	//alert(url);
	
	new Ajax(url, {
		method: 'get',
		data: {'from_name': from_name,'from_email': from_email,'to_name': to_name, 'to_email': to_email, 'url': pageurl},
		onRequest: function() { loader('taf_loader'); },
		onComplete: function(response) {
			var r = response.split('][');
			if (r[0] == 0) {
				$('taf_loader').setHTML(r[1]);	
			} else {
				$('taf_loader').setHTML('');	
				$('taf_form_holder').setHTML('De pagina is doorgestuurd naar <strong>'+to_email+'</strong>');	
				//$('comments_error_holder').setHTML('');
				//$('comment_text').value = '';
				//var myFx = new Fx.Scroll(window).toElement('comments_holder');
			}
			
		}
	}).request();
	
	//unload('taf_loader');

}
