function readCookie(sApp)
{
	var nameEQ = sApp + "=";
	var ca = document.cookie.split(';')

	for(var i=0;i<ca.length;i++)
	{
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length)
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length)
	}
	return null;
}

function setupLink( sApp, sHref, bDownload )
{
	var count = readCookie(sApp);
	var sDoc = '';
	if (count ==null) // no cookie found for this app (suite), show user-info form
	{
		if ( bDownload )
			sHref = '';

		sDoc = '<a href="" onclick="UserFormWindow( \'' + sApp + '\', \'' + sHref + '\' ); return false;">';
		count = 0;
	}
	else              // cookie found! Go right in
	{
		if ( bDownload )   // it's a download, just expose link
			sDoc = '<a href="" onclick="StartDownload( \'' + sApp + '\' );return false;">';
		else               // it's a demo, load in demo window
			sDoc = '<a href="" onclick="DemoWindow( \'' + sApp + '\', \'' + sHref + '\' ); return false;">';
	}

	document.write( sDoc );
}

var win = null;
function NewWindow(mypage,myname,w,h,scroll) 
{ 
	LeftPosition = (screen.width) ? (screen.width-w)/2 : 0;
	TopPosition = (screen.height) ? (screen.height-h)/2 : 0;
	settings =
	'height='+h+',width='+w+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',resizable';
	win = window.location = mypage;
	//win = window.open(mypage,myname,settings)	
}

var winUser = null;
function UserFormWindow( sAppName, sHref)
{
	var count = readCookie(sAppName);
	if (count != null ) // cookie has been written in since the page was loaded
	{
		if ( sHref == '' )   // download
			StartDownload(sAppName);
		else                 // demo
			DemoWindow(sAppName, sHref);
			
		return;
	}

	var LeftPosition = (screen.width) ? (screen.width-350)/2 : 0;
	var TopPosition = (screen.height) ? (screen.height-395)/2 : 0;

	var settings='width=500,height=400,top='+TopPosition+',left='+LeftPosition+',scrollbars=yes,resizable=yes';

	if ( sHref == '' )
		winUser = window.location = 'http://www.cadsglobal.net/csp/demos/ukintergage/user_info.asp?app_name=' + sAppName;
	else
		winUser = window.location = 'http://www.cadsglobal.net/csp/demos/ukintergage/user_info.asp?app_name='+ sAppName + ',' + sHref;
}

var winDemo = null;
function DemoWindow(sApp, sHref)
{
	var LeftPosition = (screen.width) ? (screen.width-1024)/2 : 0;
	var TopPosition = (screen.height) ? (screen.height-768)/2 : 0;

	var Scroll = 'yes';
	if ( sApp == 'HSteel' || sApp == 'AdvanceSteel' || sApp == 'ASAyr' )
		Scroll = 'no';

	var Resiz = 'yes';
	if ( sApp == 'HSteel' || sApp == 'AdvanceSteel' || sApp == 'ASAyr' )
		Resiz = 'no';

	var settings = 'width=1024,height=768,top='+TopPosition+',left='+LeftPosition+',scrollbars='+Scroll+',resizable='+Resiz;	
	//winDemo = window.location(sHref);
	winDemo = window.open(sHref,'CADS',settings);
	//winDemo = winDemo.resizeTo(1024,768);
	//winDemo = winDemo.moveTo(LeftPosition, TopPosition);
}

// If you add any new download in download.asp and mail.asp, please update this list
function StartDownload(sApp)
{
	var sExeName = "http://www.cadsglobal.net/";
	switch ( sApp )
	{
		case 'RCBD' :
		{
			sExeName = sExeName + 'csp/demos/Products/CADSRCBD/RCBD_Demo.zip';
			break;
		}
		case 'RCCD' :
		{
			sExeName = sExeName + 'csp/demos/Products/CADSRCCD/RCCD_Demo.zip';
			break;
		}
		case 'RCSlab' :
		{
			sExeName = sExeName + 'csp/demos/Products/CADSRCSD/RCSD_Demo.zip';
			break;
		}
		case 'SMARTEngineer' :
		{
			sExeName = sExeName + 'csp/demos/Products/SmartEngineer/SmartEngineer_Demo.zip';
			break;
		}
		case 'WallPanel' :
		{
			sExeName = sExeName + 'csp/demos/Products/CADSWallPanel/Wallpanel_Demo.zip';
			break;
		}
		case 'WallPanelMAX' :
		{
			sExeName = sExeName + 'csp/demos/Products/CADSWallPanel/WallpanelMax_Demo.zip';
			break;
		}
		case 'STEP' :
		{
			sExeName = sExeName + 'csp/downloads/demos/ScaffoldingSoftware/stepdemo.zip';
			break;
		}
		default :
		{
			alert( 'The evaluation site is currently experiencing technical difficulties and is not able to provide the product\nthat you have requested.\n\n\nPlease accept our apologies and contact CADS at sales@cads.co.uk to request an evaluation CD.' );
			return;
		}
	}
	window.location = sExeName;
}

