if (!window.Element) Element = {};

Element.extend = function(destination, source)
{
	for (var property in source)
	{
		destination[property] = source[property];
	}
	return destination;
};

Element.extend(Element, {
	listen: function (name, observer)
	{
		if (this.addEventListener)
		{
			this.addEventListener(name, observer, false);
		}
		else if (this.attachEvent)
		{
			this.attachEvent('on' + name, function () {observer(null)});
			return false;
		}
	},
	setClass: function(name)
	{
		this.setAttribute("class", name);
		this.setAttribute("className", name);
	}
});

Element.extend(window, Element);

function $boopGet(name)
{
	var element = document.getElementById(name);
	return Element.extend(element, Element);
}

var IE = navigator.userAgent.match(/MSIE/);
var iPhone = navigator.userAgent.match(/iPhone/);
var uid =readcookie("uid");
var flavor = "oxy.boopsie";
var showDiv = false;

//Checking for version of browser
var nAgt = navigator.userAgent;
var fullVersion  = ''+parseFloat(navigator.appVersion);
var majorVersion = parseInt(navigator.appVersion,10);
var verOffset;
// In MSIE, the true version is after "MSIE" in userAgent
if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
  fullVersion = nAgt.substring(verOffset+5);
}
majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
 majorVersion = parseInt(navigator.appVersion,10);
}
//alert("majorVersion="+majorVersion);

if (flavor == "boopsie")
{
	flavor = "";
}
else
{
	//Added this temp variable thing cause it was removing the title! ( aaron )
	var tmptitle = document.title;
	$boopGet("boopsie").innerHTML = document.title = "";
	document.title = tmptitle;
}

var filter = $boopGet("boopsie_filter");
var modal = $boopGet("boopsie_modal");
var list2 = $boopGet("boopsie_list2");
var current = 1;
var serial_sent = 1000;
var serial_recv = 1000;
var iframeheight = 250;
var curPos = -1;
var maxElements = 2;
var isIE7 = false;

if(IE)
{
	if(majorVersion < 8)
	{
		 isIE7 = true;
	}
}
//alert("isIE7=" + isIE7);
if (iPhone)
{

}
else
{
	try
	{
		if (parent && parent.upcall)
		{
			parent.upcall(downcall);
			if (parent.iframeheight > 0) iframeheight = parent.iframeheight;
		}
	}
	catch (err)
	{
	}
}

if (IE)
{
	modal.style.filter = "alpha(opacity=33)";
}



list2.listen("click", click_handler);

filter.onkeydown = function( e ) {
    e = e || window.event;
    var doHighlight = false;
    var doEnter = false;
    switch( e.keyCode ) {
        case 38: //up
            curPos --;
            if(curPos < 0)
               curPos=0;
            doHighlight = true;
            break;
        case 40: //down
            curPos++;
            if(curPos > (maxElements-1))
                curPos = (maxElements-1);
            doHighlight = true;
            break;
        case 13: // Enter
            doEnter = true;
    }
    if(doEnter)
    {
        if(showDiv)
        {
            var elms1 = list2.getElementsByTagName("a");
            if(elms1)
            {
                for(var cn11 = 0; cn11 < elms1.length; cn11++)
                {
                    var elm1 = elms1[cn11];

                    if(elm1)
                    {
                        var idval1 = eval(elm1.id);

                        if(idval1 == curPos)
                        {
                            processClick(e,elm1);
                            break;
                        }
                    }
                }
            }
        }
    }
    if(doHighlight)
    {
        if(showDiv)
        {
            var elms = list2.getElementsByTagName("a");
            if(elms)
            {
                for(var cn1 = 0; cn1 < elms.length; cn1++)
                {
                    var elm = elms[cn1];

                    if(elm)
                    {
                        var idval = eval(elm.id);
                        if(idval == curPos)
                        {
                            elm.className = "highlightBG";
                        }
                        else
                        {
                            elm.className = "acir";
                        }
                    }
                }
            }
        }
    }
}

var page = [];
var bmenu = null;
var currentHash = "x";
var lastFilter;
var mylocation = new Object;
mylocation.hash = "";

setTimeout(initialize_tic_handler, 1000);

function downcall(action)
{
	if (action == "back")
	{
	    history.go(-1);
	}
	else
	{
		alert(action + " - use Menu button on screen");
	}
}



function initialize_tic_handler()
{
	setInterval(tic_handler, 100);
}

function tic_handler()
{
	var wantHash = decodeURI(mylocation.hash);


	if (wantHash != currentHash)
	{

		var n = 0;
		var direction = 1;
		var replace = false;
		var c = "";

		if (wantHash.match(/^\#\[(\d+)\](.*?)(?:@(.*))?$/))
		{
			n = +RegExp.$1;
			var u = RegExp.$2;
			c = RegExp.$3;
			if (0 && u && (page.length <= n || u != page[n]))
			{
				add_page(u);
				n = page.length - 1;
			}
			else if (currentHash.match(/^\#\[(\d+)\]/) && n < RegExp.$1) direction = -1;

			replace = true;
		}
		else if (wantHash.length > 1)
		{
			page.length = 0;
			add_page(mylocation.hash.substring(1));
			direction = 0;
		}

		if (page.length == 0) add_page("Home");
		if (n >= page.length) n = 0 ;
		tempindex = c.indexOf("}");
		if(tempindex >= 0)
		{
		    c = c.substring(tempindex+1);
		}

		filter_set(c);


		currentHash = "#[" + n + "]" + page[n];
		//
		if (mylocation.hash != currentHash)
		{
		    var temp = mylocation.hash;
			mylocation.hash = currentHash;
			filter_set("");

		}

		ajax_load_page(page[n], direction);
	}

	check_filter_change();
}


function add_page(hash)
{
	page.push(hash);
	var cookie = "boopsie_page=" + page.join("*");
	document.cookie = cookie;
}

function on_focus() {
    var t2 = document.getElementById("boopsie_filter");
    t2.focus();
    t2.value = t2.value;
}

function check_filter_change()
{

	var x = filter.value.replace(/_?$/, '').replace(/^\s+/, '').replace(/\s+$/, '').replace(/\s\s+/, ' ');
	var t = lastFilter;
	if (lastFilter == null) lastFilter = x;
	if (x == lastFilter) return;
	lastFilter = x;
	curPos = -1;
	if(x)
	{
	    showDiv = true;
	}
	else
	{
	    showDiv = false;
	}
	ajax_update_page(x);
}

function filter_change(x)
{
	filter.value = x ;
	if (!iPhone)
	{
		filter.select();
		filter.focus();
	}
}

function filter_set(x)
{
	lastFilter = null;
	filter_change(x);
}

function processClick(event, anchor)
{
    curPos = -1;
    if (event)
	{
        if(IE){
			//event = window.event;
			event.cancelBubble = true;
			event.returnValue = false;
		}
		else
		{
			//event = window.event;
			//event.cancelBubble = true;
			//event.returnValue = false;
			event.preventDefault();
		}
	}
	else
	{
		event = window.event;
		event.cancelBubble = true;
		event.returnValue = false;
	}
	//alert("calling processEvents");
    processEvents(anchor);
}

function click_handler(event)
{

	var target
    curPos = -1;
	if (event)
	{
		event.preventDefault()
		target = event.target
	}
	else
	{
		event = window.event
		event.cancelBubble = true
		event.returnValue = false
		target = event.srcElement
	}

	var anchor = findParent(target, "a")

	processEvents(anchor);

	function findParent(node, localName)
	{
	    while (node && (node.nodeType != 1 || node.nodeName.toLowerCase() != localName))
	        node = node.parentNode
	    return node
	}
}

function processEvents(anchor)
{
    if (!anchor)
	{

	}
	else if (anchor.hash == "#erase")
	{
		erase()
	}
	else if (anchor.hash == "#menu")
	{
		menu_off()

		var special = target.getAttribute("special") || ""
		var action = unescape(target.getAttribute("action") || "")

		if (special == "Map")
		{
			action = "http://maps.google.com/maps?q=" + unescape(anchor.getAttribute("arg") || "")
		}
		else if (special == "Directions")
		{
			var part = unescape(anchor.getAttribute("arg") || "").match(/^(.*?) -- (.*?)$/)
			action = "http://maps.google.com/maps?daddr=" + unescape(part[1] || "") + "&saddr=" + unescape(part[2] || "")
		}

		processLink(anchor.getAttribute("control") || "", action)
	}
	else if (anchor.hash == "#channel")
	{
	}
	else
	{
		var click = "";//show_menu(anchor, "click")

		if (click)
		{
			processLink(click.control, click.action, click.follow)
		}
		else
		{
			var link = anchor.getAttribute("link")
			if (link)
			{
				anchor.setAttribute("class", "boopsie_asel")
				anchor.setAttribute("className", "boopsie_asel")

				processLink("xON", link);
			}
		}
	}
}

function erase()
{
	filter.focus();
	filter.value = "";
	return false;
}

function readcookie(name)
{
	var cookie = document.cookie.replace(/\s*;\s*/g, ";").split(";")
	for (var i in cookie)
	{
		if (cookie[i].substr(0, name.length + 1) == name + "=") return cookie[i].substr(name.length + 1)
	}
}


function ajax(link, callback)
{
	var req = new XMLHttpRequest();

	req.onreadystatechange = function ()
	{
		if (req.readyState == 4)
		{
			callback(req.responseText);
		}
	}

	req.open("GET", link, true);
	req.setRequestHeader("UA-OS", "Widget - Version (2.0); Carrier (none); Boopsie - Version (2.0)");
	req.setRequestHeader("GUID", uid);
	req.setRequestHeader("B-PIN", flavor);
	req.send(null);
}

function ajax_load_page(item, direction)
{
	var base = "http://live.boopsie.com/i/";
	if (item.match(/^(.*?)?(?:@(.*))?$/))
	{
		var x = RegExp.$1;
		if (x.substr(0, 2) == "i/") x = x.substr(2);
		var u = base + x + "/";
		var c = RegExp.$2 || "";
		var link = "/boopsie/?c=" + c + "&u=" + "http://live.boopsie.com/i/surterre_search_2/";
		var serial = ++serial_sent;
		ajax(link, function (html) {convert(serial, html, direction)});
	}
	else
	{
		alert("can't " + item);
	}
}

function ajax_update_page(filter)
{
	var base = "http://live.boopsie.com/i/";
	if (currentHash.match(/^\#\[\d+\](.*?)?(?:@(.*))?$/))
	{
		var u = base + RegExp.$1 + "/";
		var c = filter || "";
		var link = "/boopsie/?c=" + c + "&u=" +  "http://live.boopsie.com/i/surterre_search_2/";
		var serial = ++serial_sent;
		ajax(link, function (html) {convert(serial, html, 0)});
	}
	else
	{
		alert("can't " + item);

	}
}


function convert(serial, data, direction)
{
	if (serial <= serial_recv) return;
	serial_recv = serial;
    var rowHeight = 88;
   var coll1Width = 18;
    var coll2Width=80;
    var divWidth = 550;
 //   var coll1Width = 30;
 //   var coll2Width=70;
   // var divWidth = 320;

    imgwidth = (divWidth * coll1Width ) / 100;
    imgheight = rowHeight;

	var part = data.match(/^((?:.|[\r\n])*?\r?\n)\r?\n((?:.|[\r\n])*)$/);

	bmenu = part[1];
    itemdata = part[2];
	var objPicURLInfo = new BMenuPictureURL(data);
	var imgInfo = objPicURLInfo.getImageInfo(0);
	var item = part[2].split("\n");
	item.pop();

	var html = "";
	var counter =-1;
	maxElements = 0;
	var itemcount = 0;

	for (var i = 0; i < item.length; i++)
	{
		if (item[i].match(/^\s*$/))
		{
			continue;
		}

		var cell = item[i].split("\t");
		var c = cell[0].match(/^(?:(\#[0-9a-f]{3}(?:[0-9a-f]{3})?)\s?(?:(\#[0-9a-f]{3}(?:[0-9a-f]{3})?)\s?)?)?(.*)$/i);
		var text = c[3];
		var fg = c[1] || "#000";
		var style = "";
		if (text.match(/\|/))
		{
			text = text.replace(/\s*\|/g, "<br>").replace(/\\<br>/g, "|");
		}
		if (fg) style += "color: " + fg + ";";
		//if (c[2]) style += "background-color:" + c[2] + ";";
		if (c[2]) style += "background-color:#67720a; color:#fff;";
		if (style) style = ' style="' + style + '"';

		var attr = [];
		for (var j in cell)
		{
			if (j == 0 ) continue;
			attr.push("c" + (parseInt(j) + 1) + '="' + cell[j] + '"');
		}

		if (cell[1] || attr.length)
		{
		    counter++;
		    maxElements++;
		    itemcount++;
		    if(cell[0] == "more...")
			{
				morelink = cell[1];

				var ps = morelink.substring(0,4);
				var il = morelink.indexOf("}");
				var pv = morelink.substring(4,il);
				pv = parseInt(pv) - 15;
				var pl = morelink.substring(il);
				cell[1] = ps + pv +  pl;

			}
			else
			{
				if(itemcount > 5)
				{
					continue;
				}
			}

			html += "<a " + attr.join(" ");
			if (cell[1])
			{
				html += " href=#link link=\"" + cell[1] + "\"";
			}
			html += style + " class='boopsie_acir' id='" + counter + "' name='" + counter + "' >";
			var rowDat = "";
			if(isIE7)
			{
				rowDat = rowDat+ "<span style='display:block;min-height:" + rowHeight  + "px;float:none;text-align:left;width:100%'>";
			}
			else
			{
				rowDat = rowDat+ "<span style='display:table;min-height:" + rowHeight  + "px;float:none;text-align:left;width:100%'>";
			}

			httpIndex = cell[1].indexOf("http://");
			if(httpIndex >= 0)
			{
			    if(cell[6])
				{
					newimgurl = "http://host.boopsie.bredir.com/service/convert/-geometry%20"+imgwidth+"x"+imgheight+"/"+cell[6];
		            if(isIE7)
					{
						rowDat = rowDat + "<span style='display:block;width:" + coll1Width +  "%;min-height:" + rowHeight + "px;vertical-align:middle;text-align:center;float:left;'>";
					}
					else
					{
						rowDat = rowDat + "<span style='display:table-cell;width:" + coll1Width +  "%;min-height:" + rowHeight + "px;vertical-align:middle;text-align:left;float:none'>";
					}
					rowDat = rowDat + "<img src='" + newimgurl + "' alt='' />";
			        rowDat = rowDat + "</span>";
					if(isIE7)
					{
						rowDat = rowDat+ "<span style='display:block;width:" + (parseInt(coll2Width)-2) + "%;min-height:"+  rowHeight +"px;vertical-align:middle;float:left;'>";
					}
					else
					{
						rowDat = rowDat+ "<span style='display:table-cell;width:" + coll2Width + "%;min-height:"+  rowHeight +"px;vertical-align:middle;float:none'>";
					}
					ctemp = cell[0].match(/^(?:(\#[0-9a-f]{3}(?:[0-9a-f]{3})?)\s?(?:(\#[0-9a-f]{3}(?:[0-9a-f]{3})?)\s?)?)?(.*)$/i);
					if(ctemp){
						temptext = ctemp[3];
					}
					temptext = temptext.replace(/\s*\|/g, "<br/>").replace(/\\<br>/g, "|");
					rowDat = rowDat + temptext + "</span>";
				}
				else
				{
					if(isIE7)
					{
						rowDat = rowDat+ "<span style='display:block;width:" + (parseInt(coll2Width)-2) + "%;min-height:"+  rowHeight +"px;vertical-align:middle;float:left;'>";
					}
					else
					{
						rowDat = rowDat+ "<span style='display:table-cell;width:" + coll2Width + "%;min-height:"+  rowHeight +"px;vertical-align:middle;float:none'>";
					}
					ctemp = cell[0].match(/^(?:(\#[0-9a-f]{3}(?:[0-9a-f]{3})?)\s?(?:(\#[0-9a-f]{3}(?:[0-9a-f]{3})?)\s?)?)?(.*)$/i);
					if(ctemp){
						temptext = ctemp[3];
					}
					temptext = temptext.replace(/\s*\|/g, "<br/>").replace(/\\<br>/g, "|");
					rowDat = rowDat + temptext + "</span>";
				}
			}
			else
			{
			    var imgURL = imgInfo.URL;
			    var namePosition = imgInfo.imageNamePosition;
			    var imageName = extractImageName(namePosition,cell);
			    if(imageName)
			    {
			        newimgurl = "http://host.boopsie.bredir.com/service/convert/-geometry%20"+imgwidth+"x"+imgheight+"/"+imgURL + imageName;
		            if(isIE7)
					{
						rowDat = rowDat + "<span style='display:block;width:" + coll1Width +  "%;min-height:" + rowHeight + "px;vertical-align:middle;text-align:center;float:left;'>";
					}
					else
					{
						rowDat = rowDat + "<span style='display:table-cell;width:" + coll1Width +  "%;min-height:" + rowHeight + "px;vertical-align:middle;text-align:left;float:none'>";
					}

			        rowDat = rowDat + "<img src='" + newimgurl + "' alt='' />";
			        rowDat = rowDat + "</span>";
			    }
                if(isIE7)
				{
					rowDat = rowDat+ "<span style='display:block;width:" + (parseInt(coll2Width)-2) + "%;min-height:"+  rowHeight +"px;vertical-align:middle;float:left;'>";
				}
				else
				{
					rowDat = rowDat+ "<span style='display:table-cell;width:" + coll2Width + "%;min-height:"+  rowHeight +"px;vertical-align:middle;float:none'>";
				}

			    ctemp = cell[0].match(/^(?:(\#[0-9a-f]{3}(?:[0-9a-f]{3})?)\s?(?:(\#[0-9a-f]{3}(?:[0-9a-f]{3})?)\s?)?)?(.*)$/i);
		        if(ctemp){
		            temptext = ctemp[3];
		        }
			    temptext = temptext.replace(/\s*\|/g, "<br/>").replace(/\\<br>/g, "|");
			    rowDat = rowDat + temptext + "</span>";

			}
			rowDat = rowDat + "</span>";
			if(isIE7)
			{
				rowDat = rowDat + "<div style='clear:both;'></div>";
			}

			html = html + rowDat + "</a>" ;
	    }
	    else
	    {
		    html += "<a" + style + " class=anon>" +  text + "</a>";
	    }
	    html += "\n";
    }

	list2.innerHTML = html;
	if(showDiv)
	{
	    list2.setClass("boopsie_liston");
	}
	else
	{
	    list2.setClass("boopsie_listoff");
	}
}

function processLink(control, link, link2, callback)
{
	if (link.match(/^i:(.*)$/))
	{
		hash_push(decodeURI(RegExp.$1));
	}
	else if (link.match(/^i:@(.*)$/))
	{
		var c = unescape(RegExp.$1);
		filter_change(c);
	}
	else if (link.match(/^i:(.+)$/))
	{
		hash_push(decodeURI(RegExp.$1));
	}
	else if (link.match(/^tel:/))
	{
		location = link;
	}
	else if (control.match(/^.O.$/))
	{
		if (link.match(/^https?:\/\//))
		{
			location = addUID(link);
		}
		else
		{
			var base = "http://www.surterreproperties.com/idx/listings/";
			if (currentHash.match(/^\#\[\d+\](.*)(?:\@(.*))?$/))
			{
				location = addUID(base + link.replace("!","") + "/details.html");
			}
		}
	}
	else if (control.match(/^.I.$/))
	{
		var refresh;
		if (control.match(/^..R$/)) refresh = function () {filter_set(""); lastFilter = -1};
		else if (control.match(/^..S$/)) refresh = function () {lastFilter = -1};
		else if (control.match(/^..F$/)) refresh = function () {processLink("xIN", link2, null, callback)};

		if (link)
		{
			ajax_internal(addUID(link), refresh);
		}
		else if (refresh)
		{
			refresh();
		}
	}
}

function addUID(href)
{
	if (href.match(/http:\/\/[\w\-\.]*(?:bredir|boopsie)\.(?:com|mobi)/))
	{
		href += (href.match(/\?/))? "&": "?";
		href += "uid=" + uid;
		if (flavor) href += "&B-PIN=" + flavor;
	}
	return href;
}

function combine(u, v)
{
	if (v.match(/^\/(.*)\/$/)) return RegExp.$1;

	u = u.match(/^(.*?)(\/?@.*)?$/)[1];

	while (v.substr(0, 3) == "../")
	{
		v = v.substr(3);
		if (u.match(/^(.*)\//)) u = RegExp.$1
		else u = "";
	}

	if (v.match(/^(?:(.*)\/)?(@.*)?$/))
	{
		if (RegExp.$1)
		{
			if (u) u += "/";
			u += RegExp.$1;
		}
		if (RegExp.$2) u += RegExp.$2;
	}

	return u;
}

function combineurl(base, uri)
{
	var url = base + uri;
	while (url.match(/\/\.\.\//))
	{
		url = url.replace(/[^\/]*\/\.\.\//, "");
	}
	url = url.replace(/\/@/, "@");
	return url;
}

function hash_push(hash)
{
	if (currentHash.match(/^\#\[(\d+)\](.*?)(?:@(.*))?$/))
	{
		var n = +RegExp.$1;
		page.length = n + 1;
		var was = hash;
		hash = combine(page[n], hash);
	}

	add_page(hash);
	filter_set("");
	mylocation.hash = "#[" + (page.length - 1) + "]" + hash;
}


function BMenuPictureURL(menuText)
{
	this.noOfImages = 0;
	var menuLines = null;
	var imageInfo = null;
	if(menuText == null || menuText == "")
		return;
	var menuLines = menuText.split("\n");
	if(menuLines != null)
    {
        for(cnt =0; cnt < menuLines.length; cnt++)
        {
	    imageHint = menuLines[cnt].match(/(?:B-Menu-Entry[-\d{0,4}]*:\s(.+?);\s(.+?);\s(.+?);(.+?))(.*)/i);
            if(imageHint != null)
            {
                if(imageHint[5] && imageHint[5].match(/Image/i))
                {
                    this.noOfImages = this.noOfImages+1;
                    if(imageInfo == null)
                    {
                        imageInfo = new Array();
                    }
                    URLOrg = trimstr(imageHint[3]);
                    index1 = URLOrg.indexOf("$");
                    positionProp = new Array();
                    URL = "";
                    if(index1>=0)
                    {
                        imgVal = URLOrg.substring(index1,URLOrg.length);
                        URL = URLOrg.substring(0,index1);
                        imgVals = imgVal.split("/");
                        for(x =0; x < imgVals.length;x++)
                        {
                            positionProp.push(imgVals[x]);
                        }
                    }
                    else
                    {
                        URL = URLOrg;
                    }
                    imgLastPortion = trimstr(imageHint[5]);
                    reimg = imgLastPortion.match(/\d/);
                    if(reimg)
                    {
                    imagePosition = reimg[0];
                    }
                    else
                    {
                    imagePosition =0;
                    }
                    var obj = new Object;

                    obj.URL = URL;
                    obj.imageNamePosition = positionProp;
                    obj.imagePosition = imagePosition;
                    imageInfo.push(obj);
                }
            }
        }
    }

		this.hasImageURL = hasImageURL;

		function hasImageURL()
		{
			if(imageInfo == null)
				return false;
			else
				return true;
		}
		this.getImageInfo = getImageInfo;

			function getImageInfo(position)
			{
				if(position == null || position == '')
					position = 0;
				obj1 = null;

				if(imageInfo != null)
				{
					for(cnt =0; cnt < imageInfo.length; cnt++)
					{
						objTemp = imageInfo[cnt];
						if(objTemp.imagePosition == position)
						{
							obj1 = objTemp;
							break;
						}
					}
				}
				return obj1;
			}
			function trimstr(str)
			{
				if(str == null) return null;
		            str = ""+str;

    		    return str.replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[\s]{2,}/,' ');
			}
	}

	function extractImageName(imgPositions,rowData)
	{
	    var tempImgName = "";
	    var invalidImgURL = false;
	    if((imgPositions != null) && (rowData != null))
	    {
	        for(x = 0; x < imgPositions.length; x++)
	        {
	            p = imgPositions[x];
	            var imgExt;
	            enc = true;
	            if(p)
	            {
	                tempImgName = tempImgName + "/";
	                re = p.match(/\d/g);
	                if(re != null)
	                {
	                    if(p.indexOf(".")>-1)
	                    {
	                        imgExt = p.substring(p.indexOf("."),p.length);
	                    }
	                    if(p.indexOf("%")>-1)
	                    {
	                        enc = false;
	                    }
	                    for(y=0;y<re.length;y++)
	                    {
	                        if(re[y])
	                        {
	                            try{
	                                t="";
	                                t = rowData[re[y]-1];
	                                if(t){
	                                    if(enc)
                                        {
                                            t = WebHelper.encode(t);
                                        }
                                        tempImgName = tempImgName + t;

                                    }
                                    else
                                    {
                                        invalidImgURL = true;
                                        break;
                                    }
	                            }catch(oError){
	                                invalidImgURL = true;
	                                break;
	                            }

	                        }
	                        else
	                        {
	                            invalidImgURL = true;
	                        }
	                    }
	                    if(invalidImgURL)
	                    {
	                        break;
	                    }
	                }
	                else
	                {
	                }
	            }
	        }
	    }
	    if(invalidImgURL)
        {
            tempImgName = "";
        }
        if(tempImgName)
        {
            if(imgExt)
            {
                tempImgName = tempImgName + imgExt;
            }
            tempImgName = tempImgName.substring(1,tempImgName.length);
        }
	    return tempImgName;
	}

    var WebHelper = {

	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}
