function StringBuffer() { 
   this.buffer = []; 
}

StringBuffer.prototype.append = function append(string) { 
   this.buffer.push(string); 
   return this; 
}; 

StringBuffer.prototype.toString = function toString() { 
   return this.buffer.join(""); 
};

function processMobileResponse(req, baseUrl, uri)
{   
	var xmlDoc = req.responseXML;
	var mobileTag=xmlDoc.getElementsByTagName("isMobile");
	var isMobile = mobileTag[0].firstChild.nodeValue;

	if (isMobile == 'true') {
		setCookie("isMobile", "true");
		toMobile(baseUrl, uri);
	} else {
		setCookie("isMobile", "false");
		toStandard(baseUrl, uri);
	}
} 

function lookupMobile(mobileBase, pageURI)
{
	var mobileCookie = checkMobileCookie();
	if (mobileCookie == null){
		//alert("no cookie set...checking jsp");
		var parms = '';
		if (window.location.href.indexOf("mob=f") > 1) {
			parms="mob=f";
			//alert("setting cookie to false");
			setCookie("isMobile", "false");
		}
		var myAjax = new Ajax.Request(
			'/checkMobile.jsp', 
			{
				method: 'get', 
				parameters: parms, 
				onSuccess: function(req){
	      			processMobileResponse(req, mobileBase, pageURI)
	    		}
			});
	} else if (mobileCookie == 'true'){
		//alert("cookie says mobile")
		toMobile(mobileBase, pageURI);
	} else {//mobileCookie == false
		//alert("cookie says not mobile, stay here")
		//do nothing
	}
}

//if we've already determined if this device is mobile...don't check again
function checkMobileCookie(){
	return getCookie("isMobile");
}

function toMobile(baseUrl, uri){
	urlParts = baseUrl.split(":");
	if(document.location.href.indexOf(urlParts[1])==-1){
		mobileLocation = (uri=='/')?(baseUrl + "/pda"):(baseUrl + uri);
		document.location.href = mobileLocation;
	}
}

function toStandard(baseUrl, uri){
	urlParts = baseUrl.split(":");
	if(document.location.href.indexOf(urlParts[1])!=-1){
		standardLocation = "http://www.sybase.com" + uri;
		//if (confirm("go to standard site: "+standardLocation+"?")){
			document.location.href = standardLocation;
		//}
	}
}
