﻿var objField,objCountField,objCountFieldText;

// Removes leading whitespace
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}
// Removes trailing whitespace
function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}
// Removes leading and trailing whitespace
function trim( value ) {
	return LTrim(RTrim(value));
}
// If the String object doesn't have a trim method add one.
if(typeof "".trim == "undefined"){
	// Trim leading and trailing spaces from a string.
	String.prototype.trim = function(str) {
		str = this != window? this : str;
		return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
	}
}

function SwapVideo(videoId)
{
    var VideoCode = "<div style='height:350px; width:425px; text-align:center;'><object width='425' height='350'><param name='movie' value='http://www.youtube.com/v/" + videoId +
                    "'></param><embed src='http://www.youtube.com/v/" + videoId + "' type='application/x-shockwave-flash' width='425' height='350'></embed></object></div>";
    var VideoContainer = document.getElementById("MainVideo");
    
    VideoContainer.innerHTML = VideoCode;
}


function inputCounter(field, countfield, maxlimit, action) {
    
	if (!objField) objField = document.getElementById(field);
	if (!objCountField) objCountField = document.getElementById(countfield);
	if (!objCountFieldText) objCountFieldText = document.getElementById(countfield).childNodes[0]; 

	if (objField.value.length > maxlimit){ 
            objCountFieldText.nodeValue = "Text box is full";
            objField.value = objField.value.substring(0,maxlimit);
	    }
	    else
	    {
	        objCountFieldText.nodeValue = maxlimit + " chars max";
	    }
}

function setPos(obj){
    var rng   =   obj.createTextRange();  
        rng.moveStart("character",obj.value.length);  
        rng.moveEnd("character",0);  
        rng.select();
}

function insertAtCursor(myField, myValue) {
    if (document.selection) {
        myField.focus();

        sel = document.selection.createRange();
        sel.text = myValue;
    }

    else if (myField.selectionStart || myField.selectionStart == '0') {

            var startPos = myField.selectionStart;
            var endPos = myField.selectionEnd;
            myField.value = myField.value.substring(0, startPos)+ myValue+ myField.value.substring(endPos, myField.value.length);
        } 
    else {
    myField.value += myValue;
    }
}


function stripTags(s){
	s = s.replace(/<br \/>/gi,"\r");
	s = s.replace(/<br\/>/gi,"\r");
	s = s.replace(/<br>/gi,"\r");
	s = s.replace(/<i>/gi,"");
	s = s.replace(/<\/i>/gi,"");
	s = s.replace(/<b>/gi,"");
	s = s.replace(/<\/b>/gi,"");
	s = s.replace(/<blockquote>/gi,"[quote]");
	s = s.replace(/<\/blockquote>/gi,"[/quote]");
	s = s.replace(/<p>/gi,"");
	s = s.replace(/<\/p>/gi,"");
	s = s.replace(/<a[\s]*target=\"_blank\"[\s]*rel=\"nofollow\"[\s]*href=\"[\w.\?=_\-\/:]+\">/gi,"");
	s = s.replace(/<\/a>/gi,"");
	s = s.replace(/<img[\s]*class=\"Thumb\"[\s]*src=\"([^\"]*)*\"[^>]*>/gi,"[img]$1[/img]");
    s = s.replace(/<.*\/>/gi,"");
    s = s.replace(/<.*>/gi,"");
    s = s.replace(/<\/.*>/gi,"");
    while(s != s.replace(/\r[\s]*\r/gi,"\r")){
        s = s.replace(/\r[\s]*\r/gi,"\r");
    }
    
    return s;
}