var kbytes_of_data = 31.468;
var linespeed;
var kbps;
var kbytes_sec;
//var x_domain  = "beta.huduser.org"; 
var x_domain  = "huduser.org"; 
 //var x_domain  = ".sagesurveys.com"; 
//var x_domain  = ""; 
var issurveypop = false;
var isdeclined = false;
var islater = false;

time      = new Date();
starttime = time.getTime();

time          = new Date();
endtime       = time.getTime();
if (endtime == starttime)
	{downloadtime = 0.00001;
	}
else
	{downloadtime = (endtime - starttime)/1000;
	}

linespeed     = kbytes_of_data/downloadtime;
kbps          = (Math.round((linespeed*8)*10*1.02))/10;
kbytes_sec    = (Math.round((kbytes_of_data*10)/downloadtime))/10;


var slowline = false;
var lineset = false;
var wtime = 3;
var onesection, datasets, periodicals, publications, email, webstore, regbarriers, pdrSearch, intercept, hudindex, biblio, about, whatsnew, faq, sitemap;

function getRef(){
	var fromW = document.referrer;
	if(fromW != "")
		return fromW;
	else
		return "";

}

function setWaitTime(intime) {
		wtime = intime;
	}
// Estimate the type of connection this is
var linetype_text = "WOA! You are going fast!";
if (kbytes_sec > 2000) { setWaitTime(3); }
if (kbytes_sec <= 2000) { linetype_text = "T1"; setWaitTime(3); }
if (kbytes_sec <= 1000) { linetype_text = "Cable or DSL"; setWaitTime(3); }
if (kbytes_sec <= 135) { linetype_text = "ISDN Line"; setWaitTime(4); }
if (kbytes_sec <= 56) { linetype_text = "56k Modem"; setWaitTime(7); }
if (kbytes_sec <= 30) { linetype_text = "28.8 Modem"; setWaitTime(7); }
if(kbytes_sec <= 20) { linetype_text = "14.4 Modem"; setWaitTime(7); }


function setSlowLine() {
	slowline = true;
	Set_Cookie('isslowline','yes', '', '/', x_domain, '' );
	lineset = true;

}

function setFastLine() {
	slowline = false;
	lineset = true;
	Set_Cookie('isslowline','no', '', '/', x_domain, '' );
}

function getSettings() {
	var settings = "";
	//settings += "Popup Allowed|";
	if(checkSessionCookieEnb()) {
		settings += "S Cookie Enabled|";
		if(checkPersisCookieEnb()) {
			settings += "P Cookie Enabled|";
		} else {
			settings += "P Cookie Disabled|";
		}

		var csCookieString = document.cookie || "";
		if(csCookieString.indexOf("isslowline=yes") > 0)
			settings += "Speed-Slow|";
		else
			settings += "Speed-Fast|";

	} else {
		settings += "S Cookie Disabled|P Cookie Disabled|Speed-NA|";
	}
	settings += "Browser-" + navigator.appName + "|" ;
	//settings += "Agent-" + navigator.userAgent + "|" ;
	//alert("settings  len"+settings.length);
	
	return  settings ;
}

    //written by W.Moshammer
   
    function yhostip(){
      if((navigator.appName == "Microsoft Internet Explorer") &&
        ((navigator.appVersion.indexOf('3.') != -1) ||
        (navigator.appVersion.indexOf('4.') != -1))){
	 
        return null;
      }else {
        window.onerror=null;
        yourAddress=java.net.InetAddress.getLocalHost();
        yourAddress2=java.net.InetAddress.getLocalHost();
        yhost=yourAddress.getHostName();
        yip=yourAddress2.getHostAddress();
        //document.write("Your host name is "+yhost);
        //document.write("<br>Your IP address is "+yip);
        return yip;
      }
    }


     


var y1 = 200;   // change the # on the left to adjuct the Y co-ordinate
(document.getElementById) ? dom = true : dom = false;

function hideIt() {
  if (dom) {document.getElementById("layer1").style.visibility='hidden';}
}

function showIt() {
placeIt();
  if (dom) {document.getElementById("layer1").style.visibility='visible';}
}

function placeIt() {
  if (dom && !document.all) {document.getElementById("layer1").style.top = window.pageYOffset + (window.innerHeight - (window.innerHeight-y1)) + "px";}
  if (document.all) {document.all["layer1"].style.top = document.documentElement.scrollTop + (document.documentElement.clientHeight - (document.documentElement.clientHeight-y1)) + "px"; }
  if (document.body) {document.all["layer1"].style.top = document.body.scrollTop + (document.body.clientHeight - (document.body.clientHeight-y1)) + "px"; }

  window.setTimeout("placeIt()", 10); }




function isSurveyDone()
{

	var cookieString = document.cookie || "";

	if(cookieString.indexOf("SurveyDone") >= 0){
	//alert("Survey is already taken!!!!!!!!!!!!");
		return true;
	}
	else{
	//alert("Survey is not taken yet!!!!!!!!!!!");
		return false;
	}
}


function isLater()
{

	var cookieString = document.cookie || "";

	if(cookieString.indexOf("Later") >= 0){
	
		return true;
	}
	else{
	
		return false;
	}
}

function checkCookieNew()
{

}

function Set_Cookie( name, value, expires, path, domain, secure )
{
// set time, it's in milliseconds
var today = new Date();
today.setTime( today.getTime() );

/*
if the expires variable is set, make the correct
expires time, the current script below will set
it for x number of days, to make it for hours,
delete * 24, for minutes, delete * 60 * 24
*/
if ( expires )
{
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date( today.getTime() + (expires) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
( ( path ) ? ";path=" + path : "" ) +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );

}


function Get_Cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f

	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );


		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');

		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}


// this deletes the cookie when called
function OLD_Delete_Cookie( name, path, domain ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// this deletes the cookie when called
function Delete_Cookie( name, path, domain,expires ) {
if ( Get_Cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
( ( expires ) ? ";expires="+expires:"" ) ;
}


function checkPersisCookieEnb(){
var cookie_set = true;

// remember, these are the possible parameters for Set_Cookie:
// name, value, expires, path, domain, secure
Set_Cookie( 'test', 'test', '', '/', x_domain, '' );
// if Get_Cookie succeeds, cookies are enabled, since
//the cookie was successfully created.

if ( Get_Cookie( 'test' ) )
{
	//document.write( 'cookies are currently enabled.' );
	/*
	this is an example of a set cookie variable, if
	you want to use this on the page or on another script
	instead of writing to the page you would just check that value
	for true or false and then do what you need to do.
	*/
	cookie_set = true;
	// and these are the parameters for Delete_Cookie:
	// name, path, domain
	// make sure you use the same parameters in Set and Delete Cookie.
	var oldDate = new Date(1970, 1, 1);
	expires= oldDate.toGMTString();
	Delete_Cookie('test', '/', x_domain,expires);
}
// if the Get_Cookie test fails, cookies
//are not enabled for this session.
else
{
	//document.write( 'cookies are not currently enabled.' );
	cookie_set = false;
}

//alert("checkPersisCookieEnb"+cookie_set);
return cookie_set;


}


function checkSessionCookieEnb(){
var cookie_set = true;

// remember, these are the possible parameters for Set_Cookie:
// name, value, expires, path, domain, secure
Set_Cookie( 'test', 'test', '', '/', x_domain, '' );
// if Get_Cookie succeeds, cookies are enabled, since
//the cookie was successfully created.

if ( Get_Cookie( 'test' ) )
{
	//document.write( 'cookies are currently enabled.' );
	/*
	this is an example of a set cookie variable, if
	you want to use this on the page or on another script
	instead of writing to the page you would just check that value
	for true or false and then do what you need to do.
	*/
	cookie_set = true;
	// and these are the parameters for Delete_Cookie:
	// name, path, domain
	// make sure you use the same parameters in Set and Delete Cookie.
	Delete_Cookie('test', '/', x_domain,'');
}
// if the Get_Cookie test fails, cookies
//are not enabled for this session.
else
{
	//document.write( 'cookies are not currently enabled.' );
	cookie_set = false;
}

//alert("checkSessionCookieEnb"+cookie_set);
return cookie_set;


}



function StoreDomainCookie(){
	var cookie_name = "checkDomain";
	var cookieStore;
	var pers1 = checkPersisCookieEnb();
	var ckname = window.location.hostname;;
	var x_expires = new Date(); x_expires.setFullYear(x_expires.getFullYear()+1); // testing persistent cookies
	Delete_Cookie(cookie_name, '/', x_domain,''); // don't get false positive
	// add path if you want to. drop expires for session cookie. drop domain for default domain test.
	cookieStore = cookie_name + "=" + ckname;
	//cookieStore = cookie_name + "=" + window.location.hostname;
	if(pers1){
		cookieStore = cookieStore + "; expires=" + x_expires.toGMTString();
		Set_Cookie( cookie_name, ckname, 183, '/', x_domain, '' );

	}
	//document.cookie= cookieStore + ";path=/; domain=" + '';
	Set_Cookie( cookie_name, ckname, '', '/', x_domain, '' );

}




function declineSurvey() {
		isdeclined = true;

		Set_Cookie( 'SurveyDone', 'SurveyDone', 183, '/', x_domain, '' );

		
		document.surveyform.surveynumber.value = "Declined";
		document.surveyform.action = "/pdrwebsurvey/SaveUnSurvey.asp";
		document.surveyform.submit();
		
}

function laterSurvey() {
		//if(confirm("There is only 1 day left for you to share your opinion with HUD USER. Please do take 2 minutes of your time to respond today.")) {
		//} else {
		//islater = true;
		//Set_Cookie( 'Later', 'Later', '', '/', x_domain, '' );

		//document.forms[0].surveynumber.value = "Later";
		//document.forms[0].action = "../pdrwebsurvey/SaveUnSurvey.asp";
		//document.forms[0].submit();
		//window.close();
		//}
		islater = true;
		Set_Cookie( 'Later', 'Later', '', '/', x_domain, '' );

		document.surveyform.surveynumber.value = "Later";
		document.surveyform.action = "/pdrwebsurvey/SaveUnSurvey.asp";
		document.surveyform.submit();
		
}

function closeSurvey() {
		if(issurveypop || islater || isdeclined) {
			return true;
		}
		document.surveyform.surveynumber.value = "Closed";
		document.surveyform.action = "/pdrwebsurvey/SaveUnSurvey.asp";
		document.surveyform.submit();
		//window.close();
}

function popSurvey(){
	issurveypop = true;
	Set_Cookie( 'SurveyDone', 'SurveyDone', 183, '/', x_domain, '' );
	window.location.href=' http://rudra/pdrweb/index.asp?browserType=' + navigator.appName;
}

function getCurrURL() {
	//return document.location.pathname;
	
		if(window.location.search.substring(1) != null && window.location.search.substring(1).length > 0)
		 	var cUrl = window.location.pathname +"?"+ window.location.search.substring(1);
		else
		 	var cUrl = window.location.pathname ;
	return cUrl;	
}


//Get sections Visited
var sections = "";
function GetSections(cookieVal){
if(cookieVal == null) {
		sections = "onesection;";
		onesection = true;
		return;
}
var sectionarray = cookieVal.split("/");
//alert(sectionarray.length);
//visited only one section in the website and exited
if(cookieVal.indexOf("/") < 0  || sectionarray.length == 2){
		sections = "onesection;";
		onesection = true;
}
else{
		onesection = false;
}
if(cookieVal.indexOf("datasets") > 0){
		sections += "datasets;";
		datasets = true;
}
else{
		datasets = false;
}
if(cookieVal.indexOf("periodicals") > 0){
		sections += "periodicals;";
		periodicals = true;
}
else{
		periodicals = false;
}
if(cookieVal.indexOf("publications") > 0){
		sections += "publications;";
		publications = true;
}
else{
		publications = false;
}

if(cookieVal.indexOf("email") > 0){
		sections += "email;";
		email = true;
}
else{
		email = false;
}

if(cookieVal.indexOf("order") > 0){
		sections += "webstore;";
		webstore = true;
}
else{
		webstore = false;
}

if(cookieVal.indexOf("regbarriers") > 0){
		sections += "regbarriers;";
		regbarriers = true;
}
else{
		regbarriers = false;
}

if(cookieVal.indexOf("rbc") > 0){
		sections += "regbarriers;";
		regbarriers = true;
}
else{
		regbarriers = false;
}
if(cookieVal.indexOf("bibliodb") > 0){
		sections += "pdrsearch;";
		pdrSearch = true;
}
else{
		pdrSearch = false;
}

if(cookieVal.indexOf("intercept") > 0){
		sections += "intercept;";
		intercept = true;
}
else{
		intercept = false;
}

if(cookieVal.indexOf("om") > 0){
		sections += "hudindex;";
		hudindex = true;
}
else{
		hudindex = false;
}

if(cookieVal.indexOf("about") > 0){
		sections += "about;";
		about = true;
}
else{
		about = false;
}

if(cookieVal.indexOf("whatsnew") > 0){
		sections += "whatsnew;";
		whatsnew = true;
}
else{
		whatsnew = false;
}

if(cookieVal.indexOf("faq") > 0){
		sections += "faq;";
		faq = true;
}
else{
		faq = false;
}

if(cookieVal.indexOf("sitemap") > 0){
		sections += "sitemap;";
		sitemap = true;
}
else{
		sitemap = false;
}

}//End Get Section


function whenLoaded(){
/*
//alert("url:"+getCurrURL());
	substr = getCurrURL().substring(1);
	//alert("substr:" + substr);
	//alert("section:"+  substr.substring(0,substr.indexOf("/")));
	
isnews="";
section=substr.substring(0,substr.indexOf("/"));

if(section == "rbc")
{
isrbc = (substr.substring(substr.indexOf("/"))).substring(1);
	isnews = isrbc.substring(0,isrbc.indexOf("/"));
	//alert("isrbc:"+isrbc);
	//alert("isnews:"+isnews);
}

if(section == "publications" || section == "datasets" || section == "periodicals" || (section == "rbc" && isnews == "newsletter")){
 



  if(isSurveyDone() != true){
  //	 alert("Survey is not taken yet!!!!!!!!!!!");
	if(kbytes_sec <= 56) {
		setSlowLine();
	} else {
		setFastLine();
	}

	sett = getSettings();
	getRef();
	StoreDomainCookie();

	

	if(isLater() != true)
	setTimeout('showIt()', 3000);
	//else
	//alert("Survey is set for Later!!!!!!!!!!!");
	
  } 
   //else { alert("Survey is already taken!!!!!!!!!!!!");}
   

}
*/

}
