 
var ajax_url="http://www.botenmarkt.nl/includes/ajax.php";

// Contact form function

function contactFormCheck()
{
  if (document.add_free_2.contact_name.value =='')
  {
    alert('Vul uw voornaam in');
	return false;
  }
  if (!checkbae(document.add_free_2.contact_email))
	return false;
  if (document.add_free_2.contact_subject.value =='')
  {
    alert('Kies een onderwerp');
	return false;
  }
  if (document.add_free_2.description.value =='')
  {
    alert('Vul een boodschap in');
	return false;
  }
  
}

//Check Price on search_form and freead

function checkPrice(type){
	if(type=='from'){
			if(document.getElementById('price_to').value < document.getElementById('price_from').value){
					for(var i=0;i<document.getElementById('price_to').options.length&&(document.getElementById('price_to').options[i].value<=document.getElementById('price_from').value);i++)
						{
							document.getElementById('price_to').options[i].selected=0;
						}
					document.getElementById('price_to').options[i].selected=1;
				}
		}
}

function check_submit_cmbBrand(){
	if (document.getElementById('cmbBrand') != null && document.getElementById('cmbBrand').value == '' && document.getElementById('title') == null){
		alert('Vul de verplichte velden in: merk en type!');
		return false;
	}
	if (document.getElementById('title') !=null && document.getElementById('title').value == '' && document.getElementById('cmbBrand') == null){
		alert('Vul het verplichte veld in: titel');
		return false;
	} 
	if (document.getElementById('location') !=null && document.getElementById('location').value == ''){
		alert('Vul de locatie in');
		return false;
	} 
	if (document.getElementById('city') !=null && document.getElementById('city').value == ''){
		alert('Vul de plaats in');
		return false;
	} 
	if(text_has_urls(document.getElementById('description_limit').value)) {
		alert('U kunt geen links in de beschrijving opnemen.');
		return false;
	}
}

function text_has_urls(text) {
	tokens=text.split(" ");
	for(var i=0;i<text.length;i++) {
		if(tokens[i].indexOf('http')!=-1) return true;
		tokens2=tokens[i].split('.');
		if(tokens2.length>1&&tokens2[1].length>0) return true;
	}
	return false;
}

function check_advertiser_mail_form(){
	if (!checkemail(document.getElementById('emailFrom').value)) return false;
}

function check_friend_mail_form(){
	if(document.getElementById('emailTo_friend').value=='') {
		alert("Vul een geldig e-mailadres in.");
		return false;
	}
	if(!checkemail(document.getElementById('emailTo_friend').value)) return false;
	if(document.getElementById('emailFrom_friend').value=='') {
		alert("Vul een geldig e-mailadres in.");
		return false;
	}
	if(!checkemail(document.getElementById('emailFrom_friend').value)) return false;
}


function limitTextBrand(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function limitText(limitField, limitCount, limitNum) {
	if (limitField.value.length > limitNum) {
		limitField.value = limitField.value.substring(0, limitNum);
	} else {
		limitCount.value = limitNum - limitField.value.length;
	}
}

function checkemail(str){

	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		return true;
	else{
		alert("Vul een geldig e-mailadres in.");
		return false;
	}
	return (testresults)
}
function checkwebsite(str){

	var filter=/^((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (filter.test(str))
		return true;
	else{
		alert("Vul en geldige url in");
		return false;
	}
	return (testresults)
}
function checkbae(element){
	if (document.layers||document.getElementById||document.all){
		return checkemail(element.value)
	}else
		return true
}

// Calendar

function check_fields(){
	if (document.getElementById('start_date').value != ''){
		if (document.getElementById('end_date').value != ''){
			if (document.getElementById('titletext').value != ''){ 
				if (document.getElementById('phone').value != ''){
					if (checkemail(document.getElementById('emailevent').value)){
						if(document.getElementById('website').value != '') {
							if(!checkwebsite(document.getElementById('website').value)) return false;
						}
						return true;
					}//else alert(' Vul een e-mailadres in');
				}else alert(' Vul een telefoonnummer in');
			}else alert(' Vul een titel in');
		}else alert(' Selecteer een einddatum');
	}else alert(' Selecteer een begindatum');
	return false;
}


function validateNumber(event) {
    var key = window.event ? event.keyCode : event.which;
    if (event.keyCode == 40 || event.keyCode == 41 || event.keyCode == 43 || event.keyCode == 45) {
        return true;
    }
    else if ( key < 48 || key > 57 ) {
        return false;
    }
    else return true;
}

$(document).ready(function(){
	$(".description_limit").bind('paste', function(e) {
		setTimeout(function() {
			text = $('.description_limit').val();
			if (text.length > 400) {			
				$('.description_limit').val(text.substring(0, 400));
			} else {
				$(".description_countdown").val((400 -  text.length));
			}
			
        }, 100);	
	});
	
	$("#brand").change(function(){
		id=$(this).val();
		$("#type").html('');
		var options = '<option value="">Alle types</option>';
		$("#type").html(options);
		$.post(ajax_url, 
			{
				action: "getTypesByBrand",		
				args: id
			},
			function(json){
				data=$.parseJSON(json);
				for (var i = 0; i < data.length; i++) {
					options += '<option value="' + data[i].optionValue + '">' + data[i].optionDisplay + '</option>';
				}
				$("#type").html('');
				$("#type").html(options);		
			});
		return false;
	});
	$("#cmbBrand").change(function(){
		id=$(this).val();
		$("#type").html('');
		var options = '<option value="">Alle types</option>';
		$("#type").html(options);
		$.post(ajax_url, 
			{
				action: "getTypesByBrand",		
				args: id
			},
			function(json){
				data=$.parseJSON(json);
				for (var i = 0; i < data.length; i++) {
					options += '<option value="' + data[i].optionValue + '">' + data[i].optionDisplay + '</option>';
				}
				$("#cmbType").html('');
				$("#cmbType").html(options);		
			});
		return false;
	});

	$(".delete_ad").click(function(){
		if(!confirm('Weet u zeker dat u deze advertentie wilt verwijderen?')) return false;
		id=$(this).attr('id');
		ad_row=$(this).parent().parent(); //daca butonul de stergere se afla intr-un td, astfel obtinem un pointer catre tr-ul care contine anuntul pe care vrem sa-l stergem
		$.post(ajax_url, 
			{
				action: "deleteAd",		
				args: id
			},
			function(data){
				//asta e fucntia care se ruleaza in caz de succes
				alert('Advertentie verwijderd.');	
				//se sterge tr-ul din tabel care contine anuntul sters
				ad_row.remove();
			});
		return false;
	});
	
	// Inlog Company Ads
	
	$("#choose").change(function(){
		if ($(this).val() == 'offered')
			{
				$("#offered").show();
				$("#asked").hide();				
			}
		else if ($(this).val() == 'asked')
			{
				$("#asked").show();
				$("#offered").hide();				
			}
		else
			{
				$("#offered").show();
				$("#asked").show();	
			}
	});
	
	$(".select-all-offered").change(function(){
		if ($(this).attr('checked'))
			$('.check-offered').attr('checked', true);
		else	
			$('.check-offered').attr('checked', false);
	});

	$(".select-all-asked").change(function(){
		if ($(this).attr('checked'))
			$('.check-asked').attr('checked', true);
		else	
			$('.check-asked').attr('checked', false);
	});
	
	$("img.put-ad-online").live('click', function(){
		var ad_row = $(this).parent().parent();
		var ad_id  = $(this).parent().parent().attr("id");
		var image = $(this);
		$.post(ajax_url, 
			{
				action: "putAdOnline",		
				args: ad_id
			},
			function(data){
				//asta e functia care se ruleaza in caz de succes
				alert('Advertentie is online.');	
				//se sterge tr-ul din tabel care contine anuntul sters
				image.attr("src","pictures/Pause.png").removeClass('put-ad-online').addClass('put-ad-offline');
			});
		return false;
	});
	
	$("img.put-ad-offline").live('click', function(){
		var ad_row = $(this).parent().parent();
		var ad_id  = $(this).parent().parent().attr("id");
		var image2 = $(this);
		$.post(ajax_url, 
			{
				action: "putAdOffline",		
				args: ad_id
			},
			function(data){
				//asta e functia care se ruleaza in caz de succes
				alert('Advertentie is offline.');	
				//se sterge tr-ul din tabel care contine anuntul sters
				image2.attr("src","pictures/PlayNormal.png").removeClass('put-ad-offline').addClass('put-ad-online');
			});
		return false;
	});
	
	$("input.delete-offered-bulk").click(function(){
		if(!confirm('Weet u zeker dat u deze advertenties wilt verwijderen?')) return false;
		$("#offered table tr td .check-offered:checked").each(function (i) {
			var ad_row = undefined;
			var ad_id = undefined;
			var ad_row  = $(this).parent().parent();
			var ad_id  = ad_row.attr("id");
			if (ad_id!='')
				{
					$.post(ajax_url, 
						{
							action: "deleteAd",		
							args: ad_id
						},
						function(data){
							//se sterge tr-ul din tabel care contine anuntul sters
							ad_row.remove();
						});
				}		
		});
		return false;
	});
	$("input.delete-asked-bulk").click(function(){
		if(!confirm('Weet u zeker dat u deze advertenties wilt verwijderen?')) return false;
		$("#asked table tr td .check-asked:checked").each(function (i) {
			var ad_row = undefined;
			var ad_id = undefined;
			var ad_row  = $(this).parent().parent();
			var ad_id  = ad_row.attr("id");
			if (ad_id!='')
				{
					$.post(ajax_url, 
						{
							action: "deleteAd",		
							args: ad_id
						},
						function(data){
							//se sterge tr-ul din tabel care contine anuntul sters
							ad_row.remove();
						});
				}		
		});
		return false;
	});
	
	$("input.put-offered-online-bulk").click(function(){
		if(!confirm('Weet u zeker dat u deze advertenties online wilt zetten?')) return false;
		$("#offered table tr td .check-offered:checked").each(function (i) {
			
			var ad_row  = $(this).parent().parent();
			var ad_id  = ad_row.attr("id");
			if (ad_id!='')
				{
					$.post(ajax_url, 
						{
							action: "putAdOnline",		
							args: ad_id
						},
						function(data){
							ad_row.find("td img.put-ad-online").attr("src","pictures/Pause.png").removeClass('put-ad-online').addClass('put-ad-offline');
						});
				}		
		});
		$('.check-offered').attr('checked', false);
		$('.select-all-offered').attr('checked', false);
		alert("Advertenties zijn online.");		
		return false;
	});
	
	$("input.put-offered-offline-bulk").click(function(){
		if(!confirm('Weet u zeker dat u deze advertenties offline wilt zetten?')) return false;
		$("#offered table tr td .check-offered:checked").each(function (i) {
			var ad_row  = $(this).parent().parent();
			var ad_id  = ad_row.attr("id");
			if (ad_id!='')
				{
					$.post(ajax_url, 
						{
							action: "putAdOffline",		
							args: ad_id
						},
						function(data){
							ad_row.find("td img.put-ad-offline").attr("src","pictures/PlayNormal.png").removeClass('put-ad-offline').addClass('put-ad-online');
						});
				}		
		});
		$('.check-offered').attr('checked', false);
		$('.select-all-offered').attr('checked', false);
		alert("Advertenties zijn offline.");		
		return false;
	});
	
	$("input.put-asked-online-bulk").click(function(){
		if(!confirm('Weet u zeker dat u deze advertenties online wilt zetten?')) return false;
		$("#asked table tr td .check-asked:checked").each(function (i) {
			var ad_row  = $(this).parent().parent();
			var ad_id  = ad_row.attr("id");
			if (ad_id!='')
				{
					$.post(ajax_url, 
						{
							action: "putAdOnline",		
							args: ad_id
						},
						function(data){
							ad_row.find("td img.put-ad-online").attr("src","pictures/Pause.png").removeClass('put-ad-online').addClass('put-ad-offline');
						});
				}		
		});
		$('.check-asked').attr('checked', false);
		$('.select-all-asked').attr('checked', false);
		alert("Advertenties zijn online.");	
		return false;
	});
	
		
	$("input.put-asked-offline-bulk").click(function(){
		if(!confirm('Weet u zeker dat u deze advertenties offline wilt zetten?')) return false;
		$("#asked table tr td .check-asked:checked").each(function (i) {
			var ad_row = undefined;
			var ad_id = undefined;
			var ad_row  = $(this).parent().parent();
			var ad_id  = ad_row.attr("id");
			if (ad_id!='')
				{
					$.post(ajax_url, 
						{
							action: "putAdOffline",		
							args: ad_id
						},
						function(data){
							ad_row.find("td img.put-ad-offline").attr("src","pictures/PlayNormal.png").removeClass('put-ad-offline').addClass('put-ad-online');
						});
				}		
		});
		$('.check-asked').attr('checked', false);
		$('.select-all-asked').attr('checked', false);
		alert("Ads put offline!");	
		return false;
	});

	$("a.confirm_delete").click(function() {
		if(confirm('Weet u zeker dat u deze advertentie wilt verwijderen?')) return true;
		else return false;
	});
	
	$("#freead_submit").click(function() {
		if($("#paid").val()=='1') {
			if($("#name").val()=='') {
				alert('Vul uw voornaam in');
				return false;
			}
			if($("#surname").val()=='') {
				alert('Vul uw achternaam in');
				return false;
			}
			if($("#residence").val()=='') {
				alert('Vul uw woonplaats in');
				return false;
			}
			if($.trim($("#bankaccount").val())=='') {
				alert('Vul uw bankrekeningnummer in');
				return false;
			}
			return true;
		}
		return true;
	});

	$("#delete_comp_picture").click(function(){
		if(!confirm('Weet u zeker dat u deze foto wilt verwijderen?')) return false;
		id=$(this).attr('rel');
		$.post(ajax_url, 
			{
				action: "deleteCompPicture",		
				args: id
			},
			function(data){
				location.reload(true);
			});
		return false;
	});
	$(".delete_picture").click(function(){
		if(!confirm('Weet u zeker dat u deze foto wilt verwijderen?')) return false;
		id="'"+$(this).attr('rel')+"'";
		$.post(ajax_url, 
			{
				action: "deletePicture",		
				args: id
			},
			function(data){
				location.reload(true);
			});
		return false;
	});
	$(".picture_thumb").click(function(){
		$('#main_picture').attr('src',$(this).attr('src'));
	});
	$('#price_to').change(function(){
		if($('#price_to').val()*1 < $('#price_from').val()*1){
			document.getElementById('price_from').options[0].selected=1;
		}
	});
	$('#price_from').change(function(){
		if($('#price_to').val()*1 < $('#price_from').val()*1){
			len=document.getElementById('price_to').options.length;
			document.getElementById('price_to').options[len-1].selected=1;
		}
	});
	$('#year_to').change(function(){
		if($('#year_to').val()*1 < $('#year_from').val()*1){
			document.getElementById('year_from').options[0].selected=1;
		}
	});
	$('#year_from').change(function(){
		if($('#year_to').val()*1 < $('#year_from').val()*1){
			document.getElementById('year_to').options[0].selected=1;
		}
	});
	$('#length_to').change(function(){
		if($('#length_to').val()*1 < $('#length_from').val()*1){
			document.getElementById('length_from').options[0].selected=1;
		}
	});
	$('#length_from').change(function(){
		if($('#length_to').val()*1 < $('#length_from').val()*1){
			len=document.getElementById('length_to').options.length;
			document.getElementById('length_to').options[len-1].selected=1;
		}
	});
	if($('#phone')) {
		$('#phone').keypress(validateNumber);
		$('#phone').keyup(function(){
			val=$(this).val();
			var chars=new RegExp('[^0-9\-\+\(\)]'); 
			if (chars.test(val)) {
				$(this).val('');
			} else {
				if(val.length>20) {
					$(this).val(val.substring(0,20))
					return false;
				}
			}
		});
	}
});

