function nl2br(str) {
    return str.replace(/(\r\n)|(\n\r)|\r|\n/g,'<br />');
}

function trim (str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}

function board_edit_call_ajax(subd,frm){ // Update Post AJAX
	start_ajax();
	xmlHttp.onreadystatechange=function(){ // When the server responds with a new readystate

   		if(xmlHttp.readyState==4){ // 4 = process completed - we want the response then!
			showhide('load'+frm.post_id.value);
			// Replace is used as a subsitute for the mssql_unescape function that's in the php global file
			document.getElementById('msg'+frm.post_id.value).innerHTML = xmlHttp.responseText.replace(/''/g,"'");
			showhide('msg'+frm.post_id.value);
		}
    }

	// THE FOLLOWING SENDS THE REQUEST TO THE SERVER
	var the_url = subd + "?ms=" + new Date().getTime(); // To prevent IE 6/7 caching		
	xmlHttp.open("POST",the_url,true); // true = do this ASYNCHRONOUSLY = that's the first A in AJAX
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlHttp.send('post_id='+frm.post_id.value+'&user_id='+frm.user_id.value+'&edit='+encodeURI(frm.edit.value)+'&SF_token='+frm.SF_token.value); 
}

function board_delete_call_ajax(subd,frm){ // Delete Post AJAX
	start_ajax();
	xmlHttp.onreadystatechange=function(){ 

   		if(xmlHttp.readyState==4){ 
			showhide('delete'+frm.del_id.value);
			document.getElementById('msg'+frm.del_id.value).innerHTML = xmlHttp.responseText;
			document.getElementById('options'+frm.del_id.value).innerHTML = '';
		}
    }

	// THE FOLLOWING SENDS THE REQUEST TO THE SERVER
	var the_url = subd + "?ms=" + new Date().getTime(); // To prevent IE 6/7 caching		
	xmlHttp.open("POST",the_url,true); 
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xmlHttp.send('del_id='+frm.del_id.value+'&user_id='+frm.user_id.value+'&delete=yes&SF_token='+frm.SF_token.value); 
}

function board_qr_call_ajax(subd,frm){ // Quick Reply AJAX
	start_ajax();
	xmlHttp.onreadystatechange=function(){ 

   		if(xmlHttp.readyState==4){ 
			document.getElementById('quick_reply').innerHTML = xmlHttp.responseText;
		}
    }

	// THE FOLLOWING SENDS THE REQUEST TO THE SERVER
	var the_url = subd + "?ms=" + new Date().getTime(); // To prevent IE 6/7 caching		
	xmlHttp.open("POST",the_url,true); 
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	xmlHttp.send('forum='+frm.forum.value+'&thread='+frm.thread.value+'&replyto='+frm.replyto.value+'&qrt='+encodeURI(frm.qrt.value)+'&SF_token='+frm.SF_token.value);
}

function class_remove_call_ajax(subd,frm){ // Delete Classified Ad AJAX
	start_ajax();
	xmlHttp.onreadystatechange=function(){ 

   		if(xmlHttp.readyState==4){ 
			document.getElementById('class_ad'+frm.class_id.value).innerHTML = xmlHttp.responseText;

		}
    }

	// THE FOLLOWING SENDS THE REQUEST TO THE SERVER
	var the_url = subd + "?ms=" + new Date().getTime(); // To prevent IE 6/7 caching		
	xmlHttp.open("POST",the_url,true); 
	xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

	var poststr;
	for (i = 0; i < frm.length; i++) {
		poststr += '&'+frm.elements[i].name+'='+encodeURI(frm.elements[i].value);
	}
	xmlHttp.send(poststr);
}

function showhide(id){ 
	if (document.getElementById){ 
		obj = document.getElementById(id); 
		if (obj.style.display == "none" || obj.style.display == ""){ 
			obj.style.display = "block"; 
		} else { 
			obj.style.display = "none"; 
		} 
	} 
} 
function arrowswitch(id,img){ 
	if (document.getElementById){ 
		obj = document.getElementById(id); 
		arr = document.getElementById(img); 
		if (obj.style.display == "none" || obj.style.display == ""){ 
			var arrow_src = arr.src.replace(/up/,"down");
			arr.src = arrow_src;
		} else {
			var arrow_src = arr.src.replace(/down/,"up");
			arr.src = arrow_src;
		}
	} 
}

function class_switch(id){ 
	obj = document.getElementById(id); 

	if (obj.className == "btn_edit"){ 
		obj.className = 'btn_preview';
	} else {
		obj.className = 'btn_edit';
	}
 
}

function verify_post_form(frm){
	var err = '';
	if (frm.subject.value == null || frm.subject.value == '' || frm.subject.value == 'Subject') { err += 'Please enter a subject into the top text box<br />';}
	if (frm.post.value == null || frm.post.value == '' || frm.post.value == 'Post') { err += 'Please enter a post into the second text box<br />';}
	if (err == '') { 
		return true;
	} else {
		document.getElementById('board_error').innerHTML = err;
		return false;
	}
}
function verify_reply_form(frm){
	var err = '';
	if (frm.reply.value == null || frm.reply.value == '' || frm.reply.value == 'Reply') { err += 'Please enter a reply into the text box<br />';}
	if (err == '') { 
		return true;
	} else {
		document.getElementById('board_error').innerHTML = err;
		return false;
	}
}

function verify_ad_form(frm){
	var err = '';
	if (frm.subc.value == null || frm.subc.value == '') { err += 'Please select a category from the top right menu<br />';}
	if (frm.title.value == null || frm.title.value == '' || frm.title.value == 'Title') { err += 'Please enter a title into the top text box<br />';}
	if (frm.ad.value == null || frm.ad.value == '' || frm.ad.value == 'Description') { err += 'Please enter a description into the second text box<br />';}
	if (frm.tc.value == null || frm.tc.value == '' || frm.tc.value == 0) { err += 'Please select your full location including Town/City<br />';}

	if (err == '') { 
		return true;
	} else {
		document.getElementById('board_error').innerHTML = err;
		return false;
	}
}

function verify_news_form(frm){
	var err = '';
	if (frm.headline.value == null || frm.headline.value == '' || frm.headline.value == 'Headline') { err += 'Please enter a Headline into the top text box<br />';}
	if (frm.story.value == null || frm.story.value == '' || frm.story.value == 'Story') { err += 'Please enter a Story into the second text box<br />';}
	if (err == '') { 
		return true;
	} else {
		document.getElementById('news_error').innerHTML = err;
		return false;
	}
}

function bb_code(text) {
	search = new Array(
                                /\[b\](.*?)\[\/b\]/,                                
                                /\[i\](.*?)\[\/i\]/,                                
                                /\[u\](.*?)\[\/u\]/,
                                /\[s\](.*?)\[\/s\]/,
                                /\[small\](.*?)\[\/small\]/,
                                /\[red\](.*?)\[\/red\]/,
                                /\[br\]/,
                                /\[hr\]/,
				/\[link\=(.*?)\](.*?)\[\/link\]/,
				/\[email\=(.*?)\](.*?)\[\/email\]/,
				/\[post\=(.*?)\.(.*?)\]/,
				/\[sp\=(.*?)\]/,
				/\[listing\=(.*?)\]/);
 
	replace = new Array(
                                '<strong>$1</strong>',
                                '<em>$1</em>',
                                '<u>$1</u>',
                                '<span style="text-decoration: line-through;">$1</span>',
                                '<span class="board_desc">$1</span>',
                                '<span style="color: #990000;">$1</span>',
                                '<br />',
                                '<hr />',
				'<a href="$1" target="_blank">$2</a>',
				'<a href="mailto:$1">$2</a>',
				'<a href="board.php?topic=$1&amp;post=$2">($1.$2)</a>',
				'<a href="supplier_feedback_reviews.php?id=$1">($1)</a>',
				'<a href="classifieds.php?listing=$1">($1)</a>');
	for(i = 0; i < search.length; i++) {
    	 text = text.replace(search[i],replace[i]);
	} 
	return text;
}
function class_tabs(id) {
	document.getElementById('class_jobs').style.display = 'none';
	document.getElementById('class_vehicles').style.display = 'none';
	document.getElementById('class_realestate').style.display = 'none';
	document.getElementById('class_listings').style.display = 'none';
	document.getElementById('class_'+id).style.display = 'block';

	document.getElementById('jobs').className = 'closed';
	document.getElementById('vehicles').className = 'closed';
	document.getElementById('realestate').className = 'closed';
	document.getElementById('listings').className = 'closed';
	document.getElementById(id).className = 'open';
}