//functions.js file used for site-wide functions of music pilgrimages.

function spam(id){

jQuery.post("../ajax/spam.php", {id: id},
  function(data){
    //alert("Data Loaded: " + data);
	//disable spam link
	jQuery("#spam"+id).attr({ href: "#", disabled: "disabled"});
	//$("#comment-no"+id).attr({ style: "border:1px dotted #FF0000"});
	//$("#comment-no"+id).attr({ style: "border:1px dotted #FF0000"});
	jQuery("#comment-no"+id).attr({ style: "color:#999999"});
	alert("Kommentaren har anmälts för granskning.");
  }
);

}

function postComment(){
	
	var usercomment = jQuery("#commentfield").val();
	var name = jQuery("#name").val();
	var title = jQuery("#title").val();
	
	var article_id = jQuery("#art").val();
	//visual confirmation of details
	//alert('art value:' + article_id + ' comment value:' + usercomment + ' user' + name + ' title' + title);
	
	if(usercomment !="" && article_id !="" && name !="" && title !=""){
		
		if(usercomment.length < 501){
			//alert('size is ok ' + usercomment.length);
		
			//ajax call
			jQuery.post("../ajax/comment-action.php", {text: usercomment,name: name,title: title,art: article_id},
 			function(data){
   				alert(data);
				jQuery("#comment-button").attr({ onclick: "void(0);", disabled: "disabled"});
				jQuery("#commentfield").attr({ disabled: "disabled"});
					//alert('Kommentaren är skickad');
				}
			);
		
		}else{
			alert('Din kommentar får inte vara längre än 500 tecken, för tillfället är den ' + comment.length + ' tecken');
		}
		
	} else {
		alert("Du måste fylla i alla fält för att kunna kommentera artikeln.");	
	}
}

function show_hide(){
	jQuery("#comment-post").toggle("fast");
}

function show_hide_upload(){
	jQuery("#upload-post").toggle("fast");
}

function comment_action(){
	var article_id = jQuery("#art").val();
	var show_no_comments = jQuery("#show_no_comments").val();
	//alert(article_id + " " + show_no_comments);

		if(article_id !='' || show_no_comments !=''){
			//ajax call
			jQuery.post("../ajax/comment-retrieveal.php", {art: article_id, no_comments: show_no_comments},
 				function(data){
   					//alert("Data Loaded: " + data);
					document.getElementById('comment-container').innerHTML = data;
					//alert('retrieving comment');
				}
			);
		
		}else{
			alert('Allting var inte ifyllt korrekt');
		}
}

function reply(title){
	var title = 'Re: ' + title;
	jQuery("#title").val(title);
	jQuery("#comment-post").show("fast");
	jQuery("#comment-button").focus();
}

function checkSubForm(){
	
	var registration_email = $("#registration_email").val();
  	var subcri = document.getElementById('registration_type_newslettersubscription').checked;
  	var unsubscri = document.getElementById('registration_type_newsletterunsubscription').checked;
	
	if(registration_email == ''){
		alert('Du har glömt fylla i din e-postadress.');
	} else {
		
		if(validMail(registration_email)){
			
			if(subcri == false && unsubscri == false){
				alert('Välj om du vill prenumerera på Nyhetsbrevet eller avbeställa det.');
			} else {
				jQuery("#new_registration").submit();
			}
			
		} else {
			alert('Denna e-postadress är ej godkänd, kontrollera stavningen.');
		}//end if
	}
	
}

function validMail(email)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(email)){
		return true;
	} else {
		return false;	
	}
}

//for toggling images/video in article
function show_tab(show_id){
	if(show_id == 'video'){
		hide_tab('image');
		hide_tab('map');
		hide_tab('street');
	} else if(show_id == 'image'){
		hide_tab('video');
		hide_tab('map');
		hide_tab('street');
	} else if (show_id == 'map'){
		hide_tab('image');
		hide_tab('video');
		hide_tab('street');
	} else if (show_id == 'street'){
		hide_tab('map');
		hide_tab('video');
		hide_tab('image');
	}

	jQuery('#' + show_id + '-div').show();
	jQuery('#' + show_id + '-tab').attr("style","background-color:#1A1A1A");
}

//same as above function
function hide_tab(hide_id){
	jQuery('#' + hide_id +'-div').hide();
	jQuery('#' + hide_id +'-tab').attr("style","background-color:#666666");	
}