
// this fixes an issue with the old method, ambiguous values
// with this test document.cookie.indexOf( name + "=" );
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 false;
	}
}

function 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";
}

function setCookie(c_name,value,domain,path,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ";domain=" + domain + ";path=" + path + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}

function isCookieEnabled()
{
   var result = false;
   setCookie ("testcookie","test",".staradvertiser.com","/");
   
   if (Get_Cookie("testcookie")) {
     result = true;
   }
   
   Delete_Cookie("testcookie");
   return result;
}


function greeting()
{
alert("Welcome " + document.forms["email"]["password"].value + "!")
}

function display_alert()
{
  if (!isCookieEnabled()) {
    var errorDiv = document.getElementById("cookies_disabled_message");
    errorDiv.style.display = "block";
  }
}

var currentLocation = location.href;

if(currentLocation != "http://www.staradvertiser.com/" && currentLocation != "http://www.staradvertiser.com/?byRefresh=y&c=y")
{
	if ( Get_Cookie( 'UID2' ) )
	{
		var UID = Get_Cookie( 'UID2' );
                var uid_expires = Get_Cookie('uid_expires');
                if (uid_expires) {
		      document.cookie = 'UID=' + UID + ';domain=.staradvertiser.com;path=/;expires='+uid_expires;
                }
                else {
		      document.cookie = 'UID=' + UID + ';domain=.staradvertiser.com;path=/';
                }
	}
}


function deleteAllCookies() {
    var cookies = document.cookie.split(";");

    for (var i = 0; i < cookies.length; i++) {
    	var cookie = cookies[i];
    	var eqPos = cookie.indexOf("=");
    	var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
    	document.cookie = name + "=;domain=.staradvertiser.com;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }

    for (var i = 0; i < cookies.length; i++) {
    	var cookie = cookies[i];
    	var eqPos = cookie.indexOf("=");
    	var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
    	document.cookie = name + "=;domain=.www.staradvertiser.com;path=/;expires=Thu, 01-Jan-1970 00:00:01 GMT";
    }
}

if(currentLocation == "http://www.staradvertiser.com/?logout=y")
{
	Delete_Cookie('UID', '/', '.staradvertiser.com');
	Delete_Cookie('scun', '/', '.staradvertiser.com');
	Delete_Cookie('uid_expires', '/', '.staradvertiser.com');
	Delete_Cookie('reqContentID', '/', '.www.staradvertiser.com');
	Delete_Cookie('reqPageURL', '/', '.www.staradvertiser.com');
}
else if (currentLocation == "http://www.staradvertiser.com/?reset=y")
{
	Delete_Cookie('UID', '/', '.staradvertiser.com');
	Delete_Cookie('scun', '/', '.staradvertiser.com');
	Delete_Cookie('uid_expires', '/', '.staradvertiser.com');
	Delete_Cookie('reqContentID', '/', '.www.staradvertiser.com');
	Delete_Cookie('reqPageURL', '/', '.www.staradvertiser.com');
        deleteAllCookies();
}

var first_name = "";

if ( !Get_Cookie( 'UID' ) ) 
{

	document.write('      <div id="hsa_top-nav_paywall">');
	document.write('      <!--LOGIN AREA-->');
	document.write('        <div style="width:465px; height:auto; min-height:30px; margin-top:16px; float:right;">');
	document.write('              <form id="form3" name="form3" method="post" action="https://gateway.staradvertiser.com/clickability/set_value.php">');
	document.write('        <div style="width:350px; height:auto; font-family:Arial, Helvetica, sans-serif; font-size:9px; float:left"> <img src="http://sa-cmedia01.staradvertiser.com/sa_com_header/images/hsa_paywall_star_sm.gif" style="margin-right: 3px; padding" />Login to access subscriber content and manage your account online.<br />Not subscribed? <a href="https://gateway.staradvertiser.com/">Click here</a>. </div>');
	document.write(' ');
	document.write('        <div style="width:100%; height:auto; margin-top:3px; font-family:Arial, Helvetica, sans-serif; font-size:9px; float:left">');
	document.write('          <div style="width:150px; height:auto; min-height:15px; float:left">');
	document.write('            <label>');
	document.write('              <input type="hidden" name="action" value="doLogin" />');
	
	

	
			document.write('              <input type="text" name="email" id="email" class="pw-text" onclick="display_alert()" style="width:140px; margin:4px 0px;" />');
	document.write('              Email </label>');
	document.write('          </div>');
	document.write('          <div style="width:150px; height:auto; min-height:15px; margin:0 0 4px 8px; float:left">');
	document.write('            <label>');
	document.write('');
	document.write('              <input type="password" name="password" id="password" class="pw-text" style="width:140px; margin:4px 0px;" />');
	document.write('              Password | <a href="http://www.staradvertiser.com/manage/password_reminder/">Forgot password?</a> <br /><a href="https://gateway.staradvertiser.com/help.php" target="_blank">Having trouble logging-in?</a></label>');
	document.write('          </div>');
	document.write('          <div style="width:140px; height:auto; min-height:15px; margin-left:8px; float:left">');
	document.write('            <div style="width:130px; height:auto;"><input type="image" src="http://sa-media.s3-website-us-east-1.amazonaws.com/sa_com_header/images/pw-button-sign-in.png" width="87" height="25" border="0"/> </div>');
	document.write('          </div>');
	document.write('          <div style="width:140px; height:auto; min-height:15px; margin-left:8px; font-family:Arial, Helvetica, sans-serif; font-size:9px; margin-top:3px; float:left">');
	document.write('');
	document.write('            <input name="permanent" type="checkbox" value="y" style="margin-left:3px; margin-right:3px" />');
	document.write('            Stay logged in for 7 days </div>');
	document.write('        </div>');
	document.write('      </form>');
	document.write('      ');
	document.write('      <!--END LOGIN AREA--> ');
	document.write('    </div>');
	document.write('    </div>');
	Delete_Cookie('scem', '/', '.staradvertiser.com');
	Delete_Cookie('scid', '/', '.staradvertiser.com');
	Delete_Cookie('scnm', '/', '.staradvertiser.com');
	Delete_Cookie('scun', '/', '.staradvertiser.com');
	Delete_Cookie('UID', '/', '.staradvertiser.com');
	Delete_Cookie('UID2', '/', '.staradvertiser.com');
	var NDlink ="http://staradvertiser.newspaperdirect.com/epaper/viewer.aspx";
}
else
{

	var NDlink ="http://staradvertiser.newspaperdirect.com/epaper/viewer.aspx?key=";
	document.write('      <div id="hsa_top-nav_paywall">');
	if (Get_Cookie('SUB')=='PD') {
		document.write('		<div style="float:left;"><a href="https://gateway.staradvertiser.com/myaccount/login.php?ref=washington_post" target="_blank"><img src="http://sa-media.s3.amazonaws.com/sa_com_header/images/washpo-bug.jpg" width="209" heigh="50" border="0"> </a></div>');
	}
        else {
                document.write('           <div style="float:left;"><a href="http://staradvertiser.newspaperdirect.com/epaper/viewer.aspx?key=" target="_blank"><img src="https://sa-media.s3.amazonaws.com/sa_com_header/images/pw-e-edition-header-small-v2.gif" width="209" heigh="50" border="0"> </a></div>');
        }
	document.write('        <div id="pw-user-name-header"><span class="pw-user-name-top-header"><img src="https://sa-media.s3.amazonaws.com/sa_com_header/images/star_access.jpg" />Aloha, !</span><br />');
	document.write('          <a href="http://www.staradvertiser.com/s?action=doLogout&rurl=http://www.staradvertiser.com/?logout=y">Logout</a> | <a href="https://gateway.staradvertiser.com/myaccount/login.php">My Account </a>');
	if (Get_Cookie('SUB')=='PD') {
                document.write('<br /><a href="https://gateway.staradvertiser.com/myaccount/usatoday.php" target="_blank">$10&nbsp;USA&nbsp;Today&nbsp;Special</a>'); 
        }
	document.write("</div>");
	document.write('      </div>');
	document.write('<div class="clearAll"></div>');

	
	var UID2 = Get_Cookie( 'UID' );

	var today = new Date();
	var scun_expires = new Date(today.getTime() + (30 * 1000 * 60 * 60 * 24));

	document.cookie = 'scem=;domain=.staradvertiser.com;path=/';
	document.cookie = 'scid=;domain=.staradvertiser.com;path=/';
	document.cookie = 'scnm=;domain=.staradvertiser.com;path=/';
	document.cookie = 'scun=;domain=.staradvertiser.com;path=/;expires=' + scun_expires.toGMTString();
	document.cookie = 'UID2=' + UID2 + ';domain=.staradvertiser.com;path=/';

       if (Get_Cookie('uid_permanent') && !Get_Cookie( 'uid_expires' )) {
             var uid_expires = new Date(today.getTime() + (7 * 1000 * 60 * 60 * 24));
	     document.cookie = 'uid_expires='+uid_expires+';domain=.staradvertiser.com;path=/;expires=' + uid_expires.toGMTString();
       }
	
	var customerNumber = '';
	
	//var elem = document.getElementById("hsa_subscribe_button");
        //elem.style.display = 'none';

    // Delay scchk check by 5 seconds			
    setTimeout('checkScchkCookie()', 5000);

    // Delay cookie check by 5 seconds			
    setTimeout('checkScunCookie()', 5000);

    function checkScchkCookie() {
			if(Get_Cookie( 'scchk' ) && Get_Cookie( 'scchk' ) != "")
			{
				if(Get_Cookie( 'scchk' ) != "" && customerNumber != "")
				{
					<!--
					window.location = "http://www.staradvertiser.com/multi_login"
					//-->	
				}
			} 
     }

     function checkScunCookie() {
             if (!Get_Cookie('scun') || !Get_Cookie('scem')) {
         	Delete_Cookie('scem', '/', '.staradvertiser.com');
	        Delete_Cookie('scid', '/', '.staradvertiser.com');
	        Delete_Cookie('scnm', '/', '.staradvertiser.com');
	        Delete_Cookie('scun', '/', '.staradvertiser.com');
	        Delete_Cookie('UID', '/', '.staradvertiser.com');
	        Delete_Cookie('UID2', '/', '.staradvertiser.com');
             }
     }
			
	/*
	var name = Get_Cookie('scchk');
	document.write('<!-- scchk = ' + name + ' custom1 =  -->');
	
				
								*/
	
	/*
	Used for debug<br />
	$oasZone<br />
	true<br />
	$expireDate<br />
	Sun Oct 19 08:23:47 PDT 2014
	*/



}
