/* 
vPIP version 0.13 Beta
Uses formatted HTML code to embed a movie when a link is clicked.  Allows the embedded movie to revert to it's link when another movie is
selected.

Installation and usage page at:  http://utilities.cinegage.com/videos-playing-in-place/

Coming for version 0.14 Beta 
   - FLV player included

vPIP generates code from the hVlog format:
   <div class="HVLOG">
      <a href="{url to videoblog file}" rel="enclosure" title="{title}" {type="video/{mime type}}" onclick=”vPIPShow({...})”> 
	  	<img src="{url to image file}" />
	</a>
	<p>{Text on videoblog}</p>
   </div>
Note:  <a has the "onclick=..." to run vPIP:
      <a href="{url to videoblog file}" rel="enclosure" title="{title}" {type="video/{mime type}} 
	       onclick=onclick="vPIPShow({'width={width number including controller}, height={height number including controller},controller={true/false}, revert={true/false}...'})"> 

Acknowledgements
----------------
vPIP was originaly inspired in August, 2005 in seeing videos that popped into the location on 
Steve Garfields, http://stevegarfield.blogs.com/, vlog site.  The current version is partially 
based on input from Andreas Haugstrup, http://www.solitude.dk/ ,  and his script, video-link.js, 
and input from Josh Kinberg, http://fireant.tv/ .  Encouragement, testing and usage comes from 
the members of the videoblogging yahoo group, http://groups.yahoo.com/group/videoblogging/ .

March 2006

Copyright 2006  Enric Teller  (email: enric@cirne.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	

*/

/* Debug messages disabled:
// Debug print statements
function printfire()
{
    if (document.createEvent)
    {
        printfire.args = arguments;
        var ev = document.createEvent("Events");
        ev.initEvent("printfire", false, true);
        dispatchEvent(ev);
    }
}
*/

/* aDIVs structure:
	[0]... divs
		[0] oDiv
		[1] DIVid
		[2] OrigHTML
		[3]... links
			[0] Open/Close
			[1] HREF
			[2] Width
			[3] Height
			[4] Autostart
			[5] Controller
			[6] Quality
			[7] BGColor
			[8] Revert
*/
var aDIVs = new Array();
//DIV Array positions
var iDIVPos = 0;
var iDIVIDPos = 1;
var iOrigHTMLPos = 2;

//link Array positions
var iOpenPos = 0;
var iHREFPos = 1;
var iWidthPos = 2;
var iHeightPos = 3;
var iAutostartPos = 4;
var iControllerPos = 5;
var iNamePos = 6;
var iQualityPos = 7;
var iBGColorPos = 8;
var iRevertPos = 9;
var iLinkIDPos = 10;

//Current array position

var iInitiator = 0;

// Get path to vPIP.js
var vPIPpath = "";
getvPIPPath();

// Activated directly from onscript for when vPIPInit doesn't execute
function vPIPPlay(oLink, sParams, sOptional) {
	
	var oDiv = oLink.parentNode;
	while (oDiv != undefined && oDiv.nodeName.toLowerCase() != "div") {
		oDiv = oDiv.parentNode;
	}
	if (oDiv != undefined) {
		var byDivFound = false;
		
		iNextPos = findDIV(oDiv)
		if (iNextPos == -1) 
			iNextPos = aDIVs.length;
		else
			byDivFound = true;
			
		var sLinkid = "";
		
		if (! byDivFound) {	
			oDiv.setAttribute("id", "vPIP" + iNextPos);
			var sHREF = toAlphaNum(oLink.href);
			var sOnClick = toAlphaNum(oLink.onclick.toString());
			var sLinkid = "vPIPMovie" + iInitiator;
			oLink.setAttribute("id", sLinkid);
			
			aDIVs[iNextPos] = new Array(3);
			aDIVs[iNextPos][iDIVPos] = oDiv;
			aDIVs[iNextPos][iDIVIDPos] = "vPIP" + iNextPos;
			
			aDIVs[iNextPos][3] = new Array(iLinkIDPos+1);
			aDIVs[iNextPos][3][iOpenPos] = false;
			aDIVs[iNextPos][3][iHREFPos] = sHREF;
			aDIVs[iNextPos][3][iLinkIDPos] = parseInt(sLinkid.substring(9));;
			
			iInitiator++;
		}
		else {
		
			var byLinkFound = false;
			var iNextLinkPos = -1;
			
			sLinkid = oLink.id;
			
			if (sLinkid != undefined && sLinkid.length > 9) {
				var iLinkid = parseInt(sLinkid.substring(9));
				iNextLinkPos = findLinkInDiv(aDIVs[iNextPos], iLinkid);
				if (iNextLinkPos < 3) 
					iNextLinkPos = 3;
				else 
					byLinkFound = true;
			}
			else {
				iNextLinkPos = aDIVs[iNextPos].length;
				if (iNextLinkPos < 3) 
					iNextLinkPos = 3;
			}
			
			if (! byLinkFound) {
				aDIVs[iNextPos][iNextLinkPos] = new Array(iLinkIDPos+1);
				aDIVs[iNextPos][iNextLinkPos][iOpenPos] = false;
				aDIVs[iNextPos][iNextLinkPos][iHREFPos] = sHREF;
				aDIVs[iNextPos][iNextLinkPos][iLinkIDPos] = parseInt(sLinkid.substring(9));;
			}
			
		}
		
		
		vPIPShow(sLinkid, sParams, sOptional);
		
		return false;
	}
	else {
		window.open(oLink.href, "_self");
		setTimeout("video Playing In Place cannot execute because the containing <DIV ...>...</DIV> tag is missing.", 0);
		return true;
	}
}


function vPIPShow(sInitiator, sParams, sOptional) {
	
	var iWidth = 320; //Movie width with controller if enabled
	var iHeight  = 260; //Movie height
	var byAutostart = "true"; //Whether the movie automaticaly plays on initiation
	var byController = "true"; //Whether the movie controller is active
	var sName = "" //Name and ID of movie
	var sQuality = "high" //Flash parameter
	var sBGColor = "#FFFFFF"; //Flash parameter
	var byRevert = "true"; //Whether to revert to original elements in DIV container when another movie is selected
	var iPos;   // General purpose variable for holding an array or string position.
	var oInitiator = document.getElementById(sInitiator);
	var oDiv = oInitiator.parentNode;
	while (oDiv != undefined && oDiv.nodeName.toLowerCase() != "div") {
		oDiv = oDiv.parentNode;
	}
	if (oDiv.nodeName != undefined && oDiv.nodeName.toLowerCase() == "div") {
		var iCurrDIVid = parseInt(oDiv.id.substring(4));
		var iCurrLinkid = parseInt(oInitiator.id.substring(9));
		var iCurrLink = findLinkID(aDIVs[iCurrDIVid], iCurrLinkid);
		var sHREF;
		if (iCurrLink > -1)
			sHREF = aDIVs[iCurrDIVid][iCurrLink][iHREFPos];
			
			
			if (sHREF != undefined) {

				// Get movie parameters
				var byInitArray = true;
				//If movie operation settings already loaded
				if (aDIVs[iCurrDIVid][iCurrLink][iWidthPos] != undefined) {
				  iWidth = aDIVs[iCurrDIVid][iCurrLink][iWidthPos];
				  iHeight = aDIVs[iCurrDIVid][iCurrLink][iHeightPos];
				  byAutostart = aDIVs[iCurrDIVid][iCurrLink][iAutostartPos];
				  byController = aDIVs[iCurrDIVid][iCurrLink][iControllerPos];
				  sName = aDIVs[iCurrDIVid][iCurrLink][iNamePos];
				  sQuality = aDIVs[iCurrDIVid][iCurrLink][iQualityPos];
				  sBGColor = aDIVs[iCurrDIVid][iCurrLink][iBGColorPos];
				  byRevert = aDIVs[iCurrDIVid][iCurrLink][iRevertPos];
				  byInitArray = false;
				}
				// Load user movie operation settings
				else {
					if (sParams != undefined) {
						var aParams = sParams.split(",");
						for (var i=0; i < aParams.length; i++) {
							if (aMatch = aParams[i].match(/(width\s*=\s*)(\d*)/i)) {
							  iWidth = aMatch[2];
							}
							else if (aMatch = aParams[i].match(/(height\s*=\s*)(\d*)/i)) {
							  iHeight = aMatch[2];
							}
							else if (aMatch = aParams[i].match(/(autostart\s*=\s*)(\w*)/i)) {
							  byAutostart = (aMatch[2].toLowerCase() === "true");
							}
							else if (aMatch = aParams[i].match(/(controller\s*=\s*)(\w*)/i)) {
							  byController = (aMatch[2].toLowerCase() === "true");
							}
							else if (aMatch = aParams[i].match(/(name\s*=\s*)(\w*)/i)) {
								sName = aMatch[2];
							}
							else if (aMatch = aParams[i].match(/(quality\s*=\s*)(\w*)/i)) {
							  sQuality = aMatch[2];
							}
							else if (aMatch = aParams[i].match(/(bgcolor\s*=\s*)(\w*)/i)) {
							  sBGColor = aMatch[2];
							}
							else if (aMatch = aParams[i].match(/(revert\s*=\s*)(\w*)/i)) {
							  byRevert = (aMatch[2].toLowerCase() === "true");
							}
						}
					}
				}

				var sInnerHTML = oDiv.innerHTML;
				//Add the 2nd dimension
				aDIVs[iCurrDIVid][iCurrLink][iOpenPos] = false;  // default to embed not opened.
				// If array already initialized, don't init.
				if (byInitArray) {
					aDIVs[iCurrDIVid][iOrigHTMLPos] = sInnerHTML;
					
					aDIVs[iCurrDIVid][iCurrLink][iWidthPos] = iWidth;
					aDIVs[iCurrDIVid][iCurrLink][iHeightPos] = iHeight;
					aDIVs[iCurrDIVid][iCurrLink][iAutostartPos] = byAutostart;
					aDIVs[iCurrDIVid][iCurrLink][iControllerPos] = byController;
					aDIVs[iCurrDIVid][iCurrLink][iNamePos] = sName;
					aDIVs[iCurrDIVid][iCurrLink][iQualityPos] = sQuality;
					aDIVs[iCurrDIVid][iCurrLink][iBGColorPos] = sBGColor;
					aDIVs[iCurrDIVid][iCurrLink][iRevertPos] = byRevert;
				}
				
				//Get anything prior to and after <a href...rel="enclosure"...>...</a> 
				var sHTML = sInnerHTML;
				var iHTMLOffset = 0;
				var AStartRegex = /\<a\s*/i;
				var AEndRegex = /\<\/a\s*\>/i;
				var iAStart = sHTML.search(AStartRegex);
				var iAEnd = sHTML.search(AEndRegex);
				var sASegment = "";
				if (iAStart > -1 && iAEnd > -1) 
					sASegment = sHTML.substring(iAStart, iAEnd);
				var ASegmentRegex = /(rel\s*=\s*)[\'\"]?(enclosure)[\'\"]?/i;
				while (sASegment.length > 0 && sASegment.search(ASegmentRegex) == -1) {
					// Go past the last </a
					iHTMLOffset += iAEnd+3;
					sHTML = sHTML.substring(iHTMLOffset);
					iAStart = sHTML.search(AStartRegex);
					if (iAStart == -1)
						break;
					iAEnd = sHTML.search(AEndRegex);
					if (iAEnd == -1)
						break;
					sASegment = sHTML.substring(iAStart, iAEnd);
				}
				
				var sPrior = "";
				var sAfter = "";
				/*  Text prior and after vPIP link referencs 
                                    is Not implemented in 0.11 > release
                                if ( iAStart > -1 && iAEnd > iAStart) {
					iAStart += iHTMLOffset;
					iAEnd += iHTMLOffset;
					sPrior = sInnerHTML.substring(0, iAStart);
					sAfter = sInnerHTML.substring(iAEnd);
					iAEnd = sAfter.search(/\>/);
					if (iAEnd > -1) 
						sAfter = sAfter.substring(iAEnd+1);
				}
                                */
				
				var sMimeType = oInitiator.type;
				if (sMimeType != undefined) {
					iPos = sMimeType.search(/\//);
					if (iPos > -1) 
						sMimeType = sMimeType.substring(iPos+1);
					else 
						sMimeType = null;
				}
				
				// Type of media
				var type = false;
				var sMediaFormat = "";
				if (sMimeType != undefined) {
					switch (sMimeType.toLowerCase()) {
						case "quicktime":
						case "mp4":
						case "x-m4v":
						case "x-mp3":
							sMediaFormat = "quicktime";
							type = "video";
						break;
						case "x-msvideo":
						case "x-ms-wmv":
						case "x-ms-asf":
						case "x-ms-wma":
							sMediaFormat = "windowsmedia";
							type = "video";
						break;
						case "x-shockwave-flash":
							sMediaFormat = "flash";
							type = "application";
						break;
					}
				}
				else {
					sFileExt = sHREF.substring(sHREF.lastIndexOf('.'), sHREF.length).toLowerCase();
					switch (sFileExt.toLowerCase()) {
						case ".mov":
						case ".mp4":
						case ".m4v":
						case ".mp3":
							sMediaFormat = "quicktime";
							type = "video";
						break;
						case ".avi":
						case ".wmv":
						case ".asf":
						case ".wma":
							sMediaFormat = "windowsmedia";
							type = "video";
						break;
						case ".swf":
							sMediaFormat = "flash";
							type = "application";
						break;
					}
				}
				
				// Replacement text into DIV
				var sReplace = "";
				if (type == "video" || type == "application") {
					if (sMediaFormat == "quicktime") {
						sReplace = sPrior + "<object width=\"" + iWidth + "\" height=\"" + iHeight + "\" id=\"" + sName + "\" classid=\"clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B\" ";
						sReplace += "codebase=\"http://www.apple.com/qtactivex/qtplugin.cab\"> <param name=\"src\" value=\"" + sHREF;
						sReplace += "\"><param name=\"autoplay\" value=\"" + byAutostart + "\"><param name=\"controller\" value=\"";
						sReplace += byController + "\"><embed src=\"" + sHREF + "\" width=\""+ iWidth + "\" height=\"" + iHeight;
						sReplace += "\" name=\"" + sName + "\" autoplay=\"" + byAutostart + "\" controller=\"" + byController; 
						sReplace += "\" pluginspage=\"http://www.apple.com/quicktime/download/\"></embed></object>" + sAfter;
					}
					else if (sMediaFormat == "windowsmedia") {
							
//	<OBJECT CLASSID='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95'  codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701' standby='Loading Microsoft Windows Media Player components...' type='application/x-oleobject' width='320' height='260' id='wmTest' > 
//	   <PARAM NAME='fileName' VALUE='http://enric.blip.tv/file/get/Enric-EnteringMashupCampBreakfast136560.wmv' >
//	   <PARAM NAME='autoStart' VALUE='true'><PARAM NAME='showControls' VALUE='true'>
//	   <EMBED type='application/x-mplayer2" pluginspage="http://www.microsoft.com/Windows/MediaPlayer/' id='wmTest' name='wmTest' showcontrols='true' width='320' height='260' src='http://enric.blip.tv/file/get/Enric-EnteringMashupCampBreakfast136560.wmv' autostart='true'>
//	   </EMBED>
//	</OBJECT>
						sReplace = sPrior + "<OBJECT CLASSID='CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95'  ";
						sReplace += "codebase='http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701' ";
						sReplace += "standby='Loading Microsoft Windows Media Player components...' type='application/x-oleobject'  ";
						sReplace += "width='" + iWidth + "' height='" + iHeight + "' id='" + sName + "' >";
						sReplace += "<PARAM NAME='fileName' VALUE='" + sHREF + "' ><PARAM NAME='autoStart' VALUE='" + byAutostart;
						sReplace += "'><PARAM NAME='showControls' VALUE='" + byController + "'>";
						sReplace += "<EMBED type='application/x-mplayer2' pluginspage='http://www.microsoft.com/Windows/MediaPlayer/' id='";
						sReplace += sName + "' name='" + sName + "' showcontrols='" + byController + "' width='" + iWidth + "' height='"; 
						sReplace += iHeight + "' src='" + sHREF + "' autostart='" + byAutostart + "'></EMBED></OBJECT>" + sAfter;
					}
					else if (sMediaFormat == "flash") {
						sReplace = sPrior + "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' ";
						sReplace += "codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0' ";
						sReplace += "WIDTH='" + iWidth + "' HEIGHT='" + iHeight + "' id='" + sName + "' > <PARAM NAME=movie VALUE='";
						sReplace += sHREF + "'> <PARAM NAME=quality VALUE=" + sQuality + " > <PARAM NAME=bgcolor VALUE=" + sBGColor + "> ";
						if (sOptional != undefined && sOptional.length > 0)
							sReplace += "<PARAM NAME=flashvars VALUE=" + sOptional + " > ";
						sReplace += "<EMBED src='" + sHREF + "' quality='" + sQuality + "' bgcolor='" + sBGColor + "' width='" + iWidth + "' height='" + iHeight + "' ";
						if (sOptional != undefined && sOptional.length > 0)
							sReplace += "flashvars='" + sOptional + "' ";
						sReplace += "NAME='' ALIGN='' TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/go/getflashplayer'> ";
						sReplace += "</EMBED> </OBJECT>" + sAfter;
					}
					
					if (sReplace.length > 0) {
						// Mac Safari version 1.3.2 does not correctly close a replaced media object, so revert is disabled for Safari
						if (navigator.userAgent.indexOf('Safari') == -1) {
							//Close any <DIVs to revert
							for(var j=0; j < aDIVs.length; j++) {
								for(var k=3; k<aDIVs[j].length; k++) {
									if (aDIVs[j][k][iRevertPos]) {
										aDIVs[j][iDIVPos].innerHTML = aDIVs[j][iOrigHTMLPos];
										aDIVs[j][k][iOpenPos] = false;
									}
								}
							}
						}
						oDiv.innerHTML = sReplace;
						aDIVs[iCurrDIVid][iCurrLink][iOpenPos] = true;
					}
						
				}
				else {
					if (sMimeType != undefined) {
						setTimeout("Unsuported mime type: \"" + sMimeType + "\".", 0);
						if (oInitiator.href.toLowerCase().indexOf("http://") > -1)
							window.open(oInitiator.href, "_self");
						else if (sHREF != undefined) 
							window.open(sHREF, "_self");
					}
					else  {
						setTimeout("Unsuported file extension: \"" + sFileExt + "\".", 0);
						if (oInitiator.href.toLowerCase().indexOf("http://") > -1)
							window.open(oInitiator.href, "_self");
						else if (sHREF != undefined) 
							window.open(sHREF, "_self");
					}
				}
			}
			else {
				setTimeout("Missing href=\"...\" attribute.", 0);
				if (oInitiator.href.toLowerCase().indexOf("http://") > -1)
					window.open(oInitiator.href, "_self");
				else if (sHREF != undefined) 
					window.open(sHREF, "_self");
			}
	}
	else {
		setTimeout("video Playing In Place cannot execute because the containing <DIV ...>...</DIV> tag is missing.", 0);
		if (oInitiator.href.toLowerCase().indexOf("http://") > -1)
			window.open(oInitiator.href, "_self");
	}
	
	return;
	
}

//Initialize the containing DIV and store base information in aDIVs
function vPIPInit() {
	var a, href, type, ftype;
	a = document.getElementsByTagName("a");
	for (i=0; i<a.length; i++) {
		if (a[i].onclick != undefined && a[i].onclick.toString().toLowerCase().indexOf("vpip") > -1) {
			var oDiv = a[i].parentNode;
			if (oDiv.className != undefined && oDiv.className.toLowerCase() == "hvlog") {
				var sOnClick = toAlphaNum(a[i].onclick.toString());
				if (sOnClick != undefined && sOnClick.length > 0) {
					var ivPIPShowPos = sOnClick.toLowerCase().search(/vpipshow\(/);
					if (ivPIPShowPos > -1) {
						var byDivFound = true;
						var iNextPos;
						if ((iNextPos = findDIV(oDiv)) == -1) {
							iNextPos = aDIVs.length;
							byDivFound = false;
						}
						oDiv.setAttribute("id", "vPIP" + iNextPos);
						var sHREF = toAlphaNum(a[i].href);
						var sID = "vPIPMovie" + i;
						a[i].setAttribute("id", sID);
						iInitiator = i + 1;
						ivPIPShowPos += 9;
						var sDelim = "', ";
						if (sOnClick.toLowerCase().search(/vpipshow\(\s*\)/) > -1)
							sDelim = "'";
						sOnClick = sOnClick.substring(ivPIPShowPos-9, ivPIPShowPos) + "'" + sID + sDelim + sOnClick.substring(ivPIPShowPos);
						if (sOnClick.search(/^\s*javascript:/i) == -1)
							sOnClick = "javascript: " + sOnClick;
						var ivPIPPlayEnd = sOnClick.search(/\)/);
						if (ivPIPPlayEnd > -1)
							sOnClick = sOnClick.substring(0, ivPIPPlayEnd+1);
							
						a[i].setAttribute("href", sOnClick);
						a[i].onclick = null;
						
						if (! byDivFound) {
							aDIVs[iNextPos] = new Array(3);
						}
						aDIVs[iNextPos][iDIVPos] = oDiv;
						aDIVs[iNextPos][iDIVIDPos] = "vPIP" + iNextPos;
						
						var iNextDIVLink = aDIVs[iNextPos].length
						if (iNextDIVLink < 3) 
							iNextDIVLink = 3;
						aDIVs[iNextPos][iNextDIVLink] = new Array(iLinkIDPos+1);
						aDIVs[iNextPos][iNextDIVLink][iOpenPos] = false;
						aDIVs[iNextPos][iNextDIVLink][iHREFPos] = sHREF;
						aDIVs[iNextPos][iNextDIVLink][iLinkIDPos] = parseInt(sID.substring(9));
						
					}
				}
			}
			
		}
	}
}

function findDIV(oDiv) {
	var i;
	var iFound = -1;
	if (oDiv.id != undefined || oDiv.id.length > 0) {
		
		for(i=0; i<aDIVs.length; i++) {
			if (aDIVs[i][iDIVIDPos] === oDiv.id) {
				iFound = i;
				break;
			}
		}
	}
	
	return iFound;
}

function findLinkID(aDIV, iCurrLinkid) {
	var iFound = -1;
	for (var i=3; i<aDIV.length; i++) {
		if (aDIV[i][iLinkIDPos] == iCurrLinkid) {
			iFound = i;
			break;
		}
	}
	
	return iFound;
}

function findLinkInDiv(aDiv, iLinkid) {
	var iLinkPosInDiv = -1;
	for(var i=3; i< aDiv.length; i++) {
		if (aDiv[i][iLinkIDPos] != undefined) {
			if (aDiv[i][iLinkIDPos] == iLinkid) {
				iLinkPosInDiv = i;
				break;
			}
		}
	}
	
	return iLinkPosInDiv;
}

function addEvent(obj, evType, fn){
	if (obj.addEventListener) {
		obj.addEventListener(evType, fn, false);
		return true;
	} else if (obj.attachEvent){
		var r = obj.attachEvent("on"+evType, fn);
		return r;
	} else {
		return false;
	}
}

//Run vPIPInit on body load
addEvent(window, 'load', vPIPInit);

function getvPIPPath() {
	var scripts = document.getElementsByTagName ( "script" );
	var src;
	var index;
	for (var i=0; i<scripts.length; i++) {
		src = scripts[i].getAttribute ( "src" );
		if (src != undefined) {
			index = src.search(/vpip/i);
			if (index > -1) {
				vPIPpath = src.substring ( 0, index);
				break;
			}
		}
	}
}

function toAlphaNum(sString) {
	var i;
	var sNewString = "";
	if (sString == undefined) {
		return sString;
	}
	else {
		for (i=0; i<sString.length; i++) {
			if (sString.charAt(i) >= " "  && sString.charAt(i) <= "z") {
				sNewString += sString.charAt(i);
			}
		}
		return sNewString;
	}
}


