var type = 0;
var score = null;
var year = 0;
var age = 0;
var gender = 0;
var zip = "";
var OPTOUT="opt-out";
var AACOOKIE="aa";
var opStatus=0;

var EXP_LENGTH=(1000*60*60*24*365 + (1000*60*60*5))*10;

function initData( itype )
{
	type = itype;
}

function initData( itype, iscore, iyear, igender, izip )
{
	type = itype;
	if ( type == "1" )
	{
		score = iscore;
		setYear( iyear );
		gender = igender;
		zip = izip;
	}
}

function setType( value )
{
	type = value;
}

function setScore( value )
{
	score = value;
}

function setYear( iyear )
{
	if ( iyear == "" )
		return;

	year = iyear;
	var currentTime = new Date();
	var currentYear = currentTime.getFullYear();
	age = currentYear - year;
	if ( age >= 19 && age / 2 == 1 )
		age = age + 1;
}

function getYear()
{
	return year;
}


function setAge( value )
{
	var currentTime = new Date();
	var currentYear = currentTime.getFullYear();
	
	if ( value == 0 )
		return;

	age = value;
	year = currentYear - value;
}

function getAge()
{
	return age;
}

function setGender( value )
{
	gender = value;
}

function getGender()
{
	return gender;
}

function setZip( value )
{
	zip = value;
}

function getZip()
{
	return zip;
}


function getTransparencyInfo()
{
	if ( type == 0 )
	{
		return getNoCookieTransparencyInfo();
	}
	else if ( type == 1 )
	{
		return getCookieTransparencyInfo();
	}
	else if ( type == 2 )
	{
		return getOptoutTransparencyInfo();
	}
}

function getNoCookieTransparencyInfo()
{
	return "<p><br/><font size='3'><b><table border='1'><tr><td style='background: #ffffff'>We currently do not have a cookie on your browser.  This means you either have not visited any of our partners or you have recently deleted your cookies.</td></tr></table></b></font></p>";
}

function getCookieTransparencyInfo()
{
	if ( score == "000" )
	{
		var scorePhase = "<br/><table border='1'><tr><td style='background: #ffffff'><font size='3'><b>No AdAdvisor description available.</b></font></td></tr></table>"
		var e = "</b><br/>";
	}
	else
	{
		var scorePhase = "Based on our analytics, we have you in an AdAdvisor household cluster that can be described as:<br/><font size='3'><b><br/><table border='1'><tr><td style='background: #ffffff'>" + 
						score + "</td></tr></table></b></font>";
		var e = "<br/></b></font>"; 
	}
		var dem = "<br/><font size='2'>Your demographics are:</font><br/><font size='3'><b><table border='1'><tr><td style='background: #ffffff'>";
		var a = "Age: ";
		if ( age > 90 )
		{
			a = a + "over 90";
		}
		else if ( age > 19 )
		{
			var a0= age - 1;
			a = a + a0 + " - " + age;
		}

		var g = "<br/>Gender: ";
		if ( gender == "2" )
		{
			g = g +  "Female";
		}
		else if ( gender == "1" )
		{
			g = g +  "Male";
		}
		
		var z= "<br/>Zip Code: ";
		if ( zip  != "" )
		{
			z = z + zip ;
		}

		var d = scorePhase + dem + a + g + z + "</td></tr></table>" + e;
		return d;
}

function getOptoutTransparencyInfo()
{
	return "<p><br/><font size='3'><b><table border='1'><tr><td style='background: #ffffff'>Our cookie indicates that you have previously opted-out of the AdAdvisor service.  To remain opted-out, do not delete this cookie.  To opt-in please provide data below.</td></tr></table></b></font></p>";
}

function getChoiceInfo()
{
	if ( type == 0 )
	{
		return getNoCookieChoiceInfo();
	}
	else if ( type == 1 )
	{
		return getCookieChoiceInfo();
	}
	else if ( type == 2 )
	{
		return getOptoutChoiceInfo();
	}
}

function getNoCookieChoiceInfo()
{
	return getNewChoiceInfo();
}

function getCookieChoiceInfo()
{
	return "Our goal has been to educate you on how we enable our partners to provide you with an improved Internet experience while at the same time explaining how we use data and how we ensure your privacy is protected.   However, we believe that you should have the final say in what data we share with our partners.  If you wish to make changes to the data we store in our cookie about you please click here to update the information you wish to change:"
}

function getOptoutChoiceInfo()
{
	return getNewChoiceInfo();
}

function getNewChoiceInfo()
{
	return "Our goal has been to educate you on how we enable our partners to provide you with an improved Internet experience while at the same time explaining how we use data and how we ensure your privacy is protected.   However, we believe that you should have the final say in what data we share with our partners.  If you wish provide information to us so that we may inform our partners on how to best provide you with relevant advertising and content please click here:";
}

function preloadSelection()
{
		if ( type == "2" )
		{

			var msg = document.getElementById( "opt_message" );
		 	var btn = document.getElementById( "opt_button" );

			 msg.innerHTML="<h1>Opt In</h1><br/>Would you like to opt-in AdAdvisor Services?"
			 btn.onclick=function(){setOptOut(false);}
			 btn.value="OPT-IN";
		}
		else
		{
			var msg = document.getElementById( "opt_message" );
			 	msg.innerHTML="<h1>Opt Out</h1><p>If you prefer that we not provide any data to our partners, we provide an opt-out button below.  Opting out here does not mean you will not see any advertising online.  Rather, it means that the advertising and perhaps the content you see will not be as relevant or tailored to your interests.  Also note that if you delete cookies after opting-out, the opt-out is also deleted.</p><br />"
		}
}

function writeCookie (name,val,time)
{
	var numChars = "0123456789";
	var domain = document.domain;
	if ( numChars.indexOf( domain.charAt( 0 ) ) == -1 )
	{
		var dotted = 0;
		var index = domain.lastIndexOf( "." );
		if ( index > 0 )
		{
			dotted = 1;
			index = domain.substring( 0, index ).lastIndexOf( "." );
		}
	
		if ( index > 0 )
			domain = "; domain=" + domain.substring( index );
		else if ( dotted == 1 )
		{
			domain = "; domain=." + domain;
		}
		else
		{
			domain = "";
		}
	}
	else
	{
		domain = "; domain=." + domain;
	}

	document.cookie = name+"="+escape(val)+"; expires=" + time.toUTCString()
						+ domain + "; path=/";
}

function removeCookie ()
{
	var today = new Date();
	today.setTime (today.getTime() -1);
	writeCookie( AACOOKIE, OPTOUT,today );
}

function thankYouMessage(bool)
{
	var msg = document.getElementById( "opt_message" );
	var btn = document.getElementById( "opt_button" );
	btn.style.visibility='hidden';
	var action = bool ? "Opted Out of" : "Opted In for";
	var title = bool ? "<h1>Opt Out</h1><br/>" 
			 : "<h1>Opt In</h1><br />";
	var html = title + "<b>You have " + action + " AdAdvisor Services Thank you.</b>";

	msg.innerHTML=html;
}

function setOptOut( bool )
{
	if (bool)
	{
		var expDate = new Date();
		expDate.setTime (expDate.getTime() + EXP_LENGTH); 
		writeCookie( AACOOKIE,OPTOUT,expDate );
		removeAllCookies();
	}
	else
	{
		removeCookie();
	}
	thankYouMessage(bool);
}

function createGenderOptionList()
{
	var genderOptions = document.getElementById( 'selectGender' );
	
	var option = document.createElement( "option" );
	option.value = 2;
	option.text = "Female";
	genderOptions.add( option, null );
	option = document.createElement( "option" );
	option.value = 1;
	option.text = "Male";
	genderOptions.add( option, null );
}

function getURLParams()
{
	var qstr = location.search.split( "?" );
	if ( qstr.length == 2 )
	{
		var parms = qstr[1].split( "&" );
		for ( p in parms )
		{
			var pair = parms[p].split( "=" );
			if ( pair[0] == "t" )
				setType( pair[1] );
			else if ( pair[0] == "s" )
				setScore( pair[1] );
			else if ( pair[0] == "a" )
				setAge( pair[1] );
			else if ( pair[0] == "g" )
				setGender( pair[1] );
			else if ( pair[0] == "z" )
				setZip( pair[1] );
		}
	}
}
function removeAllCookies()
{
    $.each( document.cookie.split(/; */), function( idx, ckyStr )
    {
        var str = ckyStr.split( '=' );

        if( "aa" != str[0] )
        {
            if( "ab" == str[0] )
            {
                try
                {
                    $.ajax( {
                        type: "GET",
                        url: "http://adadvisor.net/adscores/oo?" + str[1],
                    });
                }
                catch ( e )
                {
                }
            }
            var yesterday = new Date();
            yesterday.setTime (yesterday.getTime() -1);

            writeCookie( str[0], str[1], yesterday );

        }
    });
}


window.onload = preloadSelection;
window.reload = preloadSelection;

