function URLencode(sStr)
{
    return escape(sStr).replace(/\+/g, '%2C').replace(/\"/g,'%22').replace(/\'/g, '%27');
}

function download(theURL)
{
    //var newURL = appendParentQueryStr(theURL);
    var newURL = theURL;		

    if((newURL.indexOf('?')) == -1)
        location.href = newURL + '?Type=.exe';
    else
        location.href = newURL + '&Type=.exe';
}


function stream(theURL, theTitle, w, h)
{
    //newWindow2(appendParentQueryStr(theURL), theTitle, w, h, 'no','center' );
	//newWindow2(theURL, theTitle, w, h, 'no','center' );
	newWindow3(theURL, theTitle, w, h, 'no','center' );
}

function getddsNavCookies()
{
	// call from body of stand-alone launch page like:
	// <BODY>
	// <script type="text/javascript" language="JavaScript">
	// <!--
	// var parentPageName="";var parentPageURL="";getddsNavCookies();
	// -- >
	// </script>
	
  	if(parentPageName !=null && parentPageURL !=null)
  	{	  
	  if((parentPageName=GetCookie("ddsPreviousPageName"))!=null)
	  	parentPageName=unescape(unescape(parentPageName));
	  if((parentPageURL=GetCookie("ddsPreviousPageURL"))!=null)
	  	parentPageURL=unescape(unescape(parentPageURL));
  	}
}

function resetddsNavCookies()
{
	// call from body of stand-alone launch page nav links like:
	//  onClick="javascript:resetddsNavCookies();followLink(appendParentQueryStr(this.href));return false"
	document.cookie = "ddsPreviousPageName=; domain=ibm.com; path=/ expires=Thu, 01-Jan-1970 00:00:01 GMT";  
  	document.cookie = "ddsPreviousPageURL=; domain=ibm.com; path=/ expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// follow link (download wrappers)
// call: onClick="javascript:downloadFunction(this.href, ...);return false"
//       OR onClick="javascript:downloadFunction(appendFunction(this.href), ...);return false"
function downloadWithParentQueryStr(theURL)
{
    download(appendParentQueryStr(theURL));
}

function downloadWithCookie(theURL, cookieName)
{
    download(appendCookie(theURL, cookieName));
}

function downloadWithParentQueryStrAndCookie(theURL, cookieName)
{
    download(appendParentQueryStrAndCookie(theURL, cookieName));
}


// follow link (main)
function followLink(theURL)
{
    location.href = theURL;
}



// open window functions
// call: onClick="javascript:streamFunction(this.href, ...);return false"
//       OR onClick="javascript:streamFunction(appendFunction(this.href), ...);return false"

var win=null;


function stream1024(theURL,theTitle)
{
    newWindow2(theURL, theTitle, '1024', '768', 'no', 'center');
}

function stream800(theURL,theTitle)
{
    newWindow2(theURL, theTitle, '800', '600', 'no', 'center');
}

function newWindow2(theURL,myname,w,h,scroll,pos)
{
    if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
    if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
    else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
    settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';

    win=window.open(theURL,myname,settings);
}

function newWindow3(theURL,myname,w,h,scroll,pos)
{
    var scrollb='scrollbars=yes';
    var fixedURL=theURL+'#IBM_Recorded_Demonstration';
    if((GetCookie("RDPfullscreen") != null)) { scrollb='scrollbars=no'; fixedURL=theURL;}    
    if(pos=="random"){LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
    if(pos=="center"){LeftPosition=(screen.width)?(screen.width-w)/2:100;TopPosition=(screen.height)?(screen.height-h)/2:100;}
    else if((pos!="center" && pos!="random") || pos==null){LeftPosition=0;TopPosition=20}
    settings=',fullscreen=no,width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
    //settings=',fullscreen=no,width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=yes';   
    if(screen.width > w && screen.height > h) { win=window.open(theURL,myname,'scrollbars='+scroll+settings); }
    else {win=window.open(fixedURL,myname,scrollb+settings); }
}



// append to URL functions
// call: onClick="javascript:followLink(appendFuntion(this.href)), ...);return false;"
//       OR onClick="javascript:streamWrapper(appendFunction(this.href), ...);return false"


function appendParentQueryStr(theURL)
{
    var appendBegin;

    if(theURL.indexOf('S=')!=-1 || theURL.indexOf('s=')!=-1)
    	return (theURL); 

    if(theURL.indexOf('?') == -1)
        appendBegin = '?';
    else
        appendBegin = '&';

    if (location.search.length > 0)
        return (theURL + appendBegin + location.search.substring(1));
    else
        return (theURL);
}

function appendParentQueryStrWithIndex(theURL)
{
    var appendBegin;

    // if the child doesn't yet have a query string of it's own
    if(theURL.indexOf('?') == -1)
        appendBegin = '?';
    else
        appendBegin = '&';

    // if there's a query string to append from parent
    if (location.search.length > 0)
    {
    	// if there appears to be no source tag, add S=index
    	if(location.search.indexOf('S=')==-1 && location.search.indexOf('s=')==-1)
    		return (theURL + appendBegin + 'S=index&' + location.search.substring(1)); 
    	
        return (theURL + appendBegin + location.search.substring(1));
    }
    else
    {
    	// if there appears to be no source tag, add S=index
    	if(location.search.indexOf('S=')==-1 && location.search.indexOf('s=')==-1)
    		return (theURL + appendBegin + 'S=index');
    	else
        	return (theURL);
    }
}

function appendCookie(theURL, cookieName)
{
    var cookieValue;
    var appendBegin;

    if(theURL.indexOf('?') == -1)
        appendBegin = '?';
    else
        appendBegin = '&';

    if(cookieValue = GetCookie(cookieName))
        return (theURL + appendBegin + cookieName+'='+cookieValue);
    else
        return theURL;

}

function appendParentQueryStrAndCookie(theURL, cookieName)
{
    var cookieValue;
    var appendBegin;

    if(theURL.indexOf('?') == -1)
        appendBegin = '?';
    else
        appendBegin = '&';


    if(cookieValue = GetCookie(cookieName))
    {
        if (location.search.length > 0)
            return (theURL+appendBegin+location.search.substring(1)+'&'+cookieName+'='+cookieValue);
        else
            return (theURL+appendBegin+cookieName+'='+cookieValue);
    }
    else
    {
        if (location.search.length > 0)
            return (theURL + appendBegin + location.search.substring(1));
        else
            return (theURL);
    }
}



// cookie funtions
// (internally called from append cookie wrappers)
function getCookieVal (offset)
{
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1)
    endstr = document.cookie.length;
    return URLencode(document.cookie.substring(offset, endstr));
}

function GetCookie (name)
{
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen)
    {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg)
        return getCookieVal (j);
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break;
    }
    return null;
}

