// detect if cookies are being blocked: set, then check.
var x_cookieName = "xyzzy";
var x_cookieName1 = "xxx"; // unique
var x_domain  = ".huduser.org;"; // set to whatever domain you want to test, with the standard provisos
var x_path = "/"
function detectPersistantCookies(){
	var x_result = true;
	var x_expires = new Date(); x_expires.setFullYear(x_expires.getFullYear()+1); // testing persistent cookies
	xDeleteCookie(x_cookieName1); // don't get false positive

	// add path if you want to. drop expires for session cookie. drop domain for default domain test.
	document.cookie = x_cookieName1 + "=test; expires=" + x_expires.toGMTString() + ";path=/; domain=" + x_domain;

	// now look for it.
	var x_cookieString = document.cookie || "";
	var x_cookies = x_cookieString.split(/\s*;\s*/);
	var x_found = 0;
	var x_cookie_val;
	for (var i in x_cookies) {
		var cookie = x_cookies[i];
		var dough = cookie.split(/\s*=\s*/);
    		if (dough[0] == x_cookieName1) { x_found = 1; x_cookie_val = dough[1]; break; }
	}

	// ensure it's gone
	xDeleteCookie(x_cookieName1);

	// do whatever you want with x_found bool
	if (x_found == 1) {
		x_result = true;
	}
	else {
		x_result = false;
	}
	return x_result;

}//end Function DetectPersistantCookies

function xDeleteCookie(name) {
	var oldDate = new Date(1970, 1, 1);
	document.cookie = name + "=; expires=" + oldDate.toGMTString() + ";path=/; domain=" + x_domain;
}

function Delete_Cookie(name) {
	var oldDate = new Date(1970, 1, 1);
	document.cookie = name + "="  + "; expires=" + oldDate.toGMTString() + ";path=/; domain=" + x_domain;
	var pophtml = window.location.href;
	if(pophtml.indexOf("popup.html") > 0){
		//BabyPants.close();		
	}
}

function StoreDomainCookie(){
	var cookie_name = "checkDomain";
	var cookieStore;
	var pers1 = detectPersistantCookies();
	var x_expires = new Date(); x_expires.setFullYear(x_expires.getFullYear()+1); // testing persistent cookies
	xDeleteCookie(cookie_name); // 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 + "=" + window.location.hostname; 
	if(pers1){
		cookieStore = cookieStore + "; expires=" + x_expires.toGMTString();
	}
	document.cookie= cookieStore + ";path=/; domain=" + x_domain;
}

function DetectSessionCookies(){

	// detect if cookies are being blocked: set, then check.
	var x_result = true;
	var x_expires = new Date(); x_expires.setFullYear(x_expires.getFullYear()+1); // testing persistent cookies
	xDeleteCookieSession(x_cookieName); // don't get false positive

	// add path if you want to. drop expires for session cookie. drop domain for default domain test.
	document.cookie = x_cookieName + "=test" + "; domain=" + x_domain; + ";path=/; domain=" + x_domain

	// now look for it.
	var x_cookieString = document.cookie || "";
	var x_cookies = x_cookieString.split(/\s*;\s*/);
	var x_found = 0;
	for (var i in x_cookies) {
   		var cookie = x_cookies[i];
		var dough = cookie.split(/\s*=\s*/);
		if (dough[0] == x_cookieName) { x_found = 1; break; }
	}

	// ensure it's gone
	xDeleteCookieSession(x_cookieName);
	
	// do whatever you want with x_found bool
	if (x_found == 1) {
		x_result = true;
	}
	else {
		x_result = false;
	}
	return x_result;

}//End DetectSessionCookies

function xDeleteCookieSession(name) {
    var oldDate = new Date(1970, 1, 1);
    document.cookie = x_cookieName + "=" + ";path=/; domain=" + x_domain
}

function isSurveyDone()
{

		//to enable comment the following line
		return true;

		var cookieString = document.cookie || "";
		if(cookieString.indexOf("SurveyDone") >= 0){
			return true;
		}
		else{
			return false;
		}
}
