function getcookie(cookiename) {
 var cookiestring=""+document.cookie;
 var index1=cookiestring.indexOf(cookiename);
 if (index1==-1 || cookiename=="") return ""; 
 var index2=cookiestring.indexOf(';',index1);
 if (index2==-1) index2=cookiestring.length; 
 return unescape(cookiestring.substring(index1+cookiename.length+1,index2));
}
	
	if(getcookie ( "subpop" ) != "1")
	{
		var myDate = new Date();
		myDate.setDate(myDate.getDate() + 7);
		document.cookie = "subpop=1; expires=" + myDate.toGMTString() + "; path=/;";
		// openSupPopup('https://w1.buysub.com/servlet/OrdersGateway?cds_mag_code=USW&cds_page_id=49164', 'subpopup', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=550,height=700');
		// July 3rd, 2008 change to this url.
		//openSupPopup('https://securesite.usmagazine.com/corkboard', 'subpopup', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,width=585,height=700');
	}
	else
	{
	}
function openSupPopup( url, name, widgets )
{
	var popupWin = window.open( url, name, widgets );
		popupWin.blur();
	if ( popupWin) {
		popupWin.opener.focus();
	}
}

// check if the first page cookie is set
if(getcookie("firstpage") != "1") {
	//alert('firstpage cookie is not set');
	var myDate = new Date();
	myDate.setMinutes(myDate.getMinutes() + 5);
	document.cookie = "firstpage=1; expires=" + myDate.toGMTString() + "; path=/;";
	
}

else {
	//first page cookie set
	if(getcookie("secondpage") != "1") {
	//alert('secondpage cookie is not set');
	var myDate = new Date();
	myDate.setMinutes(myDate.getMinutes() + 5);
	document.cookie = "secondpage=1; expires=" + myDate.toGMTString() + "; path=/;";
		if(getcookie("subdhtmlpop") != "1") {
			//alert('dhtml popup should fire here');
			//dhtml popup time
			// the popup needs to display a form
			// pass the form data along to ?
			// hide/close itself on submit or when the x is clicked
			$(document).ready(function() {
			var formAction = "https://w1.buysub.com/servlet/PrePopGateway?cds_mag_code=USW&cds_page_id=70750";						   
			var hello = '<div id="sub_form_popup">';
			hello += '<a href="http://ad.doubleclick.net/jump/usweek.channels/services;kw=popup;tile=1;sz=1x1;ord=123456789?" target="_blank" style="position:absolute;top:0;"><img src="http://ad.doubleclick.net/ad/usweek.channels/services;kw=popup;tile=1;sz=1x1;ord=123456789?" width="1" height="1" alt="" border="0"/></a>';
			hello += '<div id="sub_form_popup_close"><a id="formClose">CLOSE X</a></div>';
			hello += '<div id="sub_form_popup_content">';
			hello += '<div id="sub_form_popup_header"><span>header</span></div>';
			hello += '<div id="sub_form_popup_form">';
			hello += '<form action="' + formAction + '" method="post"  target="_blank">';
			hello += '<ul>';
			hello += '<li><label for="cds_name">Name</label><input type="text" name="cds_name" id="cds_name" /></li>';
            hello += '<li><label for="cds_address_1">Address</label><input type="text" name="cds_address_1" id="cds_address_1" /></li>';
            hello += '<li><label for="cds_city">City</label><input type="text" name="cds_city" id="cds_city" /></li>';
            hello += '<li><label for="cds_state">State</label><select name="cds_state" id="cds_state"><option VALUE="AL">AL</option> <option VALUE="AK">AK</option> <option VALUE="AZ">AZ</option> <option VALUE="AR">AR</option> <option VALUE="CA">CA</option> <option VALUE="CO">CO</option> <option VALUE="CT">CT</option> <option VALUE="DE">DE</option> <option VALUE="FL">FL</option> <option VALUE="GA">GA</option> <option VALUE="HI">HI</option> <option VALUE="ID">ID</option> <option VALUE="IL">IL</option> <option VALUE="IN">IN</option> <option VALUE="IA">IA</option> <option VALUE="KS">KS</option> <option VALUE="KY">KY</option> <option VALUE="LA">LA</option> <option VALUE="ME">ME</option> <option VALUE="MD">MD</option> <option VALUE="MA">MA</option> <option VALUE="MI">MI</option> <option VALUE="MN">MM</option> <option VALUE="MS">MS</option> <option VALUE="MO">MO</option> <option VALUE="MT">MT</option> <option VALUE="NE">NE</option> <option VALUE="NV">NV</option> <option VALUE="NH">NH</option> <option VALUE="NJ">NJ</option> <option VALUE="NM">NM</option> <option VALUE="NY">NY</option> <option VALUE="NC">NC</option> <option VALUE="ND">ND</option> <option VALUE="OH">OH</option> <option VALUE="OK">OK</option> <option VALUE="OR">OR</option> <option VALUE="PA">PA</option> <option VALUE="RI">RI</option> <option VALUE="SC">SC</option> <option VALUE="SD">SD</option> <option VALUE="TN">TN</option> <option VALUE="TX">TX</option> <option VALUE="UT">UT</option> <option VALUE="VT">VT</option> <option VALUE="VA">VA</option> <option VALUE="WA">WA</option> <option VALUE="WV">WV</option> <option VALUE="WI">WI</option> <option VALUE="WY">WY</option></select></li>';
      		hello += '<li class="small"><label for="cds_zip">ZIP</label><input type="text" name="cds_zip" id="cds_zip" /></li>';
            hello += '<li class="clear"><label for="cds_email">Email</label><input type="text" name="cds_email" id="cds_email" /></li>';
            hello += '<li class="last"><label>&nbsp;</label><input type="submit" class="btn_submit" value="" id="submitBtn" /></li>';
			
            hello += '</ul>';
            hello += '</form>';
			hello += '<div id="sub_form_popup_subtext"><strong>Red Carpet Guarantee:</strong> Your subscription will automatically renew at the end of each term at a guaranteed low price. You will be notified in advance of any price increase. We\'ll mail a bill if a valid credit card isn\'t available. Cancel anytime for a 100% refund on un-mailed copies.</div></div>'; // close sub_form_popup_form
			hello += '</div>'; // close sub_form_popup_content
			hello += '</div>'; // close sub_form_popup
			
			popupStatus = 1;
			$("div.window").append(hello);
			//$("#sub_form_popup").draggable();
			/*
		 	function enablePopup() {
			if(popupStatus==0){  
   				$("#backgroundPopup").fadeIn("slow");  
   				$("#popupContact").fadeIn("slow");  
   				popupStatus = 1;  
   				}  
   			  	
			}
			*/
   			function disablePopup(){  
   			//disables popup only if it is enabled  
   			if(popupStatus==1){  
   				//$("div.window").remove(hello); 
				$("#sub_form_popup").fadeOut("slow"); 
				//alert('trying to close');
   				popupStatus = 0;  
   				}  
   			}  
		    	//alert('hello, world!!!');
			 $("#formClose").click(function(){  
				disablePopup();  
				});  
			 $("#submitBtn").click(function(){  
				disablePopup();  
				}); 
			});
			var myDate = new Date();
			myDate.setDate(myDate.getDate() + 7);
			document.cookie = "subdhtmlpop=1; expires=" + myDate.toGMTString() + "; path=/;";
		}
		else {
		// subdhtmlpop cookie is set. nothing to do	
		}
	}
	else {
	//second page cookie set. nothing to do	
	}
	
}
