include("/js/ajaxLanguage.php?identifier=LANG_AJAX");
include("/js/ajaxLanguage.php?identifier=JOURNAL_FRIENDS");
include("/js/ajaxLanguage.php?identifier=USER_COMMENT&fullkey=1");
include("/js/ajaxLanguage.php?identifier=JOURNAL&fullkey=1");

////////////////////////////////////////////////
//journalComment, submits a journal comment
//paramaters:
//          form:		current form
//          journal_entry_id:		id for current journal entry

function journalComment(form, journal_entry_id) {

	var journal_comment = form.journal_comment.value;
    journal_comment = journal_comment.replace(/&/g,"%26");
    journal_comment = journal_comment.replace(/\+/g,"%2B");

	if (typeof form.quickform!=='undefined'){
		quickform = 1;
	} else { 
		quickform = 0;
	}

	private_comment = 0;
	if (typeof form.private_comment!=='undefined'){
		if (form.private_comment.checked){
			private_comment = 1;
		}
	}

	preview = 0;
	if (typeof form.preview!=='undefined'){
		if (form.preview.checked){
			preview = 1;
		}
	} 
	
	include_sig = 0;
	if (typeof form.include_sig!=='undefined'){
		if (form.include_sig.checked){
			include_sig = 1;
		}
	} 
	
	rating = 0;
	if (typeof form.rating!=='undefined' && preview=="0"){
		j = form.rating.length;
		for (i=0; i<j; i++){
			if (form.rating[i].checked){
				rating = form.rating[i].value;
			}
		}
	} 

	if (typeof form.comment_subject!=='undefined'){
		comment_subject = form.comment_subject.value;
	    comment_subject = comment_subject.replace(/&/g,"%26");
    	comment_subject = comment_subject.replace(/\+/g,"%2B");		
		
	} else { 
		comment_subject = "";
	}



	if (!journal_comment && rating=="0" && preview=="0"){
		window.location='/journal_comment.php?entry='+journal_entry_id;
		return;
	}

    if (preview=="1"){    

  		var html_id = "last_comment_"+journal_entry_id;             
	    document.getElementById(html_id).innerHTML = "<img src='http://e.vampirefreaks.com/images/ajax-loader-cyber.gif'  align=center>";      	
    	
		document.getElementById("preview").checked = false;    	
	} else {

  		var html_id = "last_comment_"+journal_entry_id;             
	    document.getElementById(html_id).innerHTML = "<img src='http://e.vampirefreaks.com/images/ajax-loader-cyber.gif'  align=center>";  
    
		var html_form_id = "comment_"+journal_entry_id;   
		document.getElementById(html_form_id).style.display = 'none';
		
	}

	timeout_id = setTimeout( "receiveJournalTimeout("+journal_entry_id+");", 20000 );
		
    if (myAjaxObject.ajaxRequest.readyState == 0 || myAjaxObject.ajaxRequest.readyState == 4){
        myAjaxObject.sendRequest(
    		ajaxControllerPHP,
            {
                method: 'POST',
                parameters: {
                    action: 'journalComment',
                    journal_comment: journal_comment,
                    entry:   journal_entry_id,
                    quickform: quickform,
                    private_comment: private_comment,
                    preview: preview,
                    include_sig: include_sig,
                    comment_subject: comment_subject,
                    rating: rating,
                    timeout_id: timeout_id
                },
    			callback: 'journalCommentResponse'
            }
    	);
    }
	return;
} // journalComment

function receiveJournalTimeout(journal_entry_id){
 		var html_id = "comment_"+journal_entry_id;    
 		var comment_html_id = "last_comment_"+journal_entry_id;
  		document.getElementById(comment_html_id).innerHTML = "this is taking too long, please try again";
		document.getElementById(html_id).style.display = 'block';  		
}


//receives response from sendUserMessage
function journalCommentResponse() {
	if ( myAjaxObject.success == 1 ) {
		var logged_in	= myAjaxObject.details['logged_in'];
		var user_name	= myAjaxObject.details['user_name'];
        var recipient        = myAjaxObject.details['recipient'];
        var is_blocked        = myAjaxObject.details['is_blocked'];
        var private       = myAjaxObject.details['private'];       
        var comment_insert        = myAjaxObject.details['comment_insert'];            
        var blacklisted        = myAjaxObject.details['blacklisted'];     
        var journal_entry_id        = myAjaxObject.details['journal_entry_id'];    
        var quickform        = myAjaxObject.details['quickform'];   
        var comment        = myAjaxObject.details['comment'];     
        var subject        = myAjaxObject.details['subject']; 
        var icon        = myAjaxObject.details['icon']; 
        var date_formatted        = myAjaxObject.details['date_formatted']; 
        var numrows        = myAjaxObject.details['numrows']; 
        var already_rated        = myAjaxObject.details['already_rated'];
        var points_given        = myAjaxObject.details['points_given'];         		
        var points        = myAjaxObject.details['points'];
                                            
        message =   "";
 		var html_id = "comment_"+journal_entry_id;    
 		var comment_html_id = "last_comment_"+journal_entry_id;
 		var num_comments_html_id = "num_comments_"+journal_entry_id;
        var no_errors = 0;   
        var comment_data = "";
 		var timeout_id = myAjaxObject.details['timeout_id'];      
		clearTimeout(timeout_id);
					       
        if (logged_in !="1"){
        	message = LANG_ARRAY['MUST_BE_LOGGED_IN'];
        } else if (is_blocked == "1"){
        	message =  LANG_ARRAY['BLOCKED'];
        } else if (blacklisted == "1"){
        	message =  LANG_ARRAY['FLAGGED_FOR_SPAM'];
        } else {
        	no_errors = 1; 
        	if (comment){
	        	message =  LANG_ARRAY['MESSAGE_SENT'];     
	        	   	       
	 			if (quickform=="1"){
			 		comment_data = "<table width=90% class=thinbordered> <tr valign=top class=blackrow><td width=100 align=center><a href='/"+user_name+"'>";
				} else {
					comment_data = "<table class='darkbox tdbordered' width=90%> <tr valign=top><td width=100 align=center><a href='/"+user_name+"'>";
				}
		 		if (icon){
		 			comment_data  += "<img src='"+icon+"' border=0 class='member_thumbnail'><br>";
		 		}
		 		comment_data +=  user_name+"</a></td><td><table class= 'subheading' width=100% cellpadding=3 cellspacing = 0>";
		 		comment_data +="<tr><td><span class='comment_subject'><b>"+subject+"</b></span></td><td align=right><small>"+date_formatted+"</small></td></tr></table><div align='left'>"+comment+"</div></td></tr></table>";
	 		}
 		}
 		
 		if (already_rated =="1" && no_errors=="1"){
 			message += "<br>"+ LANG_ARRAY['JOURNAL_ALREADY_GIVEN_PROPS'];
 		} else if (points_given =="1" && no_errors=="1"){
 			message += "<br>"+ LANG_ARRAY['JOURNAL_SUCCESSFULLY_GIVEN_PROPS'];
	  		var props_html_id = "journal_props_"+journal_entry_id;
	  		var props_message = "<b>"+LANG_ARRAY['JOURNAL_PROPS']+":</b> <a href='/journals/points.php?entry="+journal_entry_id+"'>"+points+"</a><br>";
  			document.getElementById(props_html_id).innerHTML = props_message;
 		}
 		 				
         if (preview=="1" && no_errors=="0"){ //show preview , theres errors
  			document.getElementById(comment_html_id).innerHTML = "";
			var html_form_id = "contactForm";
			document.getElementById(html_id).style.display = 'block';  			
  		}	
        
        if (preview=="1" && no_errors=="1"){ //show preview
 			preview_message = "<center><h3>"+LANG_ARRAY['USER_COMMENT_PREVIEW']+"</h3>";
 			preview_message += comment_data;
 			preview_message += "<p>";
 			document.getElementById(comment_html_id).innerHTML = preview_message;		
        
        } else if (no_errors=="1") {	
 
	 		var num_comments_message = numrows+" ";
	
	 		if (numrows >1){
	 			num_comments_message += LANG_ARRAY["COMMENTS"];
	 		} else {
	  			num_comments_message += LANG_ARRAY["COMMENT"];		
			}				
 
 			if (comment){
	 			document.getElementById(comment_html_id).innerHTML = comment_data;		
	 		} else {
 	 			document.getElementById(comment_html_id).innerHTML = "";  	 		
	 		}
	 		
	 		if (quickform == "1"){
	 			document.getElementById(num_comments_html_id).innerHTML = num_comments_message;	
	 		}
	 		document.getElementById(html_id).style.display = 'block';  	
	 	 	document.getElementById(html_id).innerHTML = message;   
        } else {
        	//theres an error
 	 		document.getElementById(comment_html_id).innerHTML = "";       	
		 	document.getElementById(html_id).style.display = 'block';  	
	 	 	document.getElementById(html_id).innerHTML = message;   	
		}
	} else {
		 document.getElementById(html_id).style.display = 'block';  		
	  	 document.getElementById(html_id).innerHTML = myAjaxObject.message;           
	}
}

////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////
//journalComment, submits a journal comment
//paramaters:
//          form:		current form
//          journal_entry_id:		id for current journal entry

function journalPoints(journal_entry_id, points) {


  		var html_form_id = "win_fail_"+journal_entry_id;           
		//document.getElementById(html_form_id).style.display = 'none';


  		var html_id = "point_feedback_"+journal_entry_id;             
	    document.getElementById(html_id).innerHTML = "<img src='http://e.vampirefreaks.com/images/ajax-loader-cyber.gif'  align=center>";  
    

	timeout_id = setTimeout( "receiveJournalPointsTimeout("+journal_entry_id+");", 20000 );
		
    if (myAjaxObject.ajaxRequest.readyState == 0 || myAjaxObject.ajaxRequest.readyState == 4){
        myAjaxObject.sendRequest(
    		ajaxControllerPHP,
            {
                method: 'POST',
                parameters: {
                    action: 'givePoints',
                    content_type: 'journal_entry',
                    entry_id:   journal_entry_id,
                    points_value: points,
                    timeout_id: timeout_id
                },
    			callback: 'journalPointsResponse'
            }
    	);
    }
	return;
} // journalComment

function receiveJournalPointsTimeout(journal_entry_id){
 		var comment_html_id = "point_feedback_"+journal_entry_id;
  		document.getElementById(comment_html_id).innerHTML = "this is taking too long, please try again";	
}


//receives response from sendUserMessage
function journalPointsResponse() {
	if ( myAjaxObject.success == 1 ) {
		var logged_in	= myAjaxObject.details['logged_in'];
		var user_name	= myAjaxObject.details['user_name'];
        var is_blocked        = myAjaxObject.details['is_blocked']; 
        var entry_id        = myAjaxObject.details['entry_id'];    
        var points_given        = myAjaxObject.details['points_given'];         		
        var points        = myAjaxObject.details['points'];
        var points_down        = myAjaxObject.details['points_down'];
        var points_up        = myAjaxObject.details['points_up'];
        var points_value        = myAjaxObject.details['points_value'];
        var already_rated        = myAjaxObject.details['already_rated'];
                                                    
        message =   "";
 		var html_id = "comment_"+entry_id;    
 		var feedback_html_id = "point_feedback_"+entry_id;
 		var num_comments_html_id = "num_comments_"+entry_id;
        var no_errors = 0;   
        var comment_data = "";
 		var timeout_id = myAjaxObject.details['timeout_id'];      
		clearTimeout(timeout_id);
					       
        if (logged_in !="1"){
        	message = LANG_ARRAY['MUST_BE_LOGGED_IN'];
        } else if (is_blocked == "1"){
        	message =  LANG_ARRAY['BLOCKED'];
 		} else if (already_rated =="1"){
 			message = "<font color='red'>"+LANG_ARRAY['JOURNAL_ALREADY_GIVEN_PROPS']+"</font>";
        } else {
        	no_errors = 1; 
        	if (points_up > 0){
       		html_id = "up_num_"+entry_id;
       		document.getElementById(html_id).style.display='';
       		html_id = "up_digit_"+entry_id;
  			document.getElementById(html_id).innerHTML = ""+points_up;   
  			}   
  			if (points_down > 0){ 		
       		html_id = "down_num_"+entry_id;
			document.getElementById(html_id).style.display='';       		
       		html_id = "down_digit_"+entry_id;
  			document.getElementById(html_id).innerHTML = ""+points_down;        
       		}
 			message = "";//LANG_ARRAY['JOURNAL_SUCCESSFULLY_GIVEN_PROPS'];


		    if (points_value<'1'){
				var html_form_id = "fail_"+entry_id;   
				document.getElementById(html_form_id).style.display = 'inline';
			} else {
				var html_form_id = "win_"+entry_id;   
				document.getElementById(html_form_id).style.display = 'inline';	
			}

 		}
 		

  			document.getElementById(feedback_html_id).innerHTML = message;
 		
	} else {
		 document.getElementById(html_id).style.display = 'block';  		
	  	 document.getElementById(html_id).innerHTML = myAjaxObject.message;           
	}
}



////////////////////////////////////////////////////////////////////////



////////////////////////////////////////////////
//journalCommentPoints, submit journal points
//paramaters:
//          form:		current form
//          journal_entry_id:		id for current journal entry

function journalCommentPoints(id, points) {

  		var html_form_id = "win_fail_comment_"+id;           
		//document.getElementById(html_form_id).style.display = 'none';


  		var html_id = "point_feedback_comment_"+id;             
	    document.getElementById(html_id).innerHTML = "<img src='http://e.vampirefreaks.com/images/ajax-loader.gif'  align=center>";  
    
	timeout_id = setTimeout( "receiveJournalCommentPointsTimeout("+id+");", 20000 );
		
    if (myAjaxObject.ajaxRequest.readyState == 0 || myAjaxObject.ajaxRequest.readyState == 4){
        myAjaxObject.sendRequest(
    		ajaxControllerPHP,
            {
                method: 'POST',
                parameters: {
                    action: 'givePoints',
                    content_type: 'journal_comment',
                    entry_id:   id,
                    points_value: points,
                    timeout_id: timeout_id
                },
    			callback: 'journalCommentPointsResponse'
            }
    	);
    }
	return;
} // journalComment

function receiveJournalCommentPointsTimeout(journal_entry_id){
 		var comment_html_id = "point_feedback_"+journal_entry_id;
  		document.getElementById(comment_html_id).innerHTML = "this is taking too long, please try again";	
}


//receives response from sendUserMessage
function journalCommentPointsResponse() {
	if ( myAjaxObject.success == 1 ) {
		var logged_in	= myAjaxObject.details['logged_in'];
		var user_name	= myAjaxObject.details['user_name'];
        var is_blocked        = myAjaxObject.details['is_blocked']; 
        var entry_id        = myAjaxObject.details['entry_id'];    
        var points_given        = myAjaxObject.details['points_given'];         		
        var points        = myAjaxObject.details['points'];
        var points_down        = myAjaxObject.details['points_down'];
        var points_up        = myAjaxObject.details['points_up'];
        var points_value        = myAjaxObject.details['points_value'];
        var already_rated        = myAjaxObject.details['already_rated'];
                                                    
        message =   "";
 		var feedback_html_id = "point_feedback_comment_"+entry_id;
        var no_errors = 0;   
        var comment_data = "";
 		var timeout_id = myAjaxObject.details['timeout_id'];      
		clearTimeout(timeout_id);
					       
        if (logged_in !="1"){
        	message = LANG_ARRAY['MUST_BE_LOGGED_IN'];
        } else if (is_blocked == "1"){
        	message =  LANG_ARRAY['BLOCKED'];
 		} else if (already_rated =="1"){
 			message = "<font color='red'>"+LANG_ARRAY['JOURNAL_ALREADY_GIVEN_PROPS']+"</font>";
        } else {
        	no_errors = 1; 
        	if (points_up > 0){
       		html_id = "up_num_comment_"+entry_id;
       		document.getElementById(html_id).style.display='';
       		html_id = "up_digit_comment_"+entry_id;
  			document.getElementById(html_id).innerHTML = ""+points_up;   
  			}   
  			if (points_down > 0){ 		
       		html_id = "down_num_comment_"+entry_id;
			document.getElementById(html_id).style.display='';       		
       		html_id = "down_digit_comment_"+entry_id;
  			document.getElementById(html_id).innerHTML = ""+points_down;        
       		}
 			message = "";//LANG_ARRAY['JOURNAL_SUCCESSFULLY_GIVEN_PROPS'];


		    if (points_value<'1'){
				var html_form_id = "fail_comment_"+entry_id;   
				document.getElementById(html_form_id).style.display = 'inline';
			} else {
				var html_form_id = "win_comment_"+entry_id;   
				document.getElementById(html_form_id).style.display = 'inline';	
			}

 		}
 		
  			document.getElementById(feedback_html_id).innerHTML = message;
 		
	} else {
		 document.getElementById(html_id).style.display = 'block';  		
	  	 document.getElementById(html_id).innerHTML = myAjaxObject.message;           
	}
}

