/*COOKIE-PAL STUFF*/
BEFORE = 1; AFTER = 2;
function cookiePal(_name,_value,_expires,_path,_domain,_secure) {
this.name = _name; this.value = _value; this.keyArray = new Object(); this.keyArray.length = 0;
if (_expires) this.expires = new Date(_expires);
this.path = _path; this.domain = _domain; this.secure = _secure; this.get = _getCookie; this.set = _setCookie; this.del = _deleteCookie; this.getKey = _getKey; this.setKey = _setKey; this.delKey = _delKey; this.clrKeys = _clearValue; this.getKeyArray = _getKeyArray; this.setValue = _setValue; this.getValue = _getValue; this.clrValue = _clearValue; this.setExpires = _setExpires; this.setPath = _setPath; this.setDomain = _setDomain; this.setSecure = _setSecure; this._buildValue = _buildValue; this._buildKeyArray = _buildKeyArray;
if (!this.value) { this.value = ""; this.get(); } else { this.value = escape(this.value); this.set(); }	
}
function _getCookieVal (offset) {
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1) endstr = document.cookie.length;
return document.cookie.substring(offset, endstr);
}
function _fixCookieDate () {
var base = new Date(0); var skew = base.getTime(); 
if (skew > 0) date.setTime (this.date.getTime() - skew);
}
function _getCookie () {
var i,j; var arg = this.name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg) { this.value = _getCookieVal (j); this._buildKeyArray(); return unescape(this.value); }
i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break;
}
return null;
}
function _setCookie () {
document.cookie = this.name + "=" + this.value + ((this.expires) ? "; expires=" + this.expires.toGMTString() : "") + ((this.path) ? "; path=" + this.path : "") + ((this.domain) ? "; domain=" + this.domain : "") + ((this.secure) ? "; secure" : "");
var returnTest =  _getCookie(); return (returnTest != "");
}
function _deleteCookie () {
if (_getCookie()) {
document.cookie = this.name + "=" + ((this.path) ? "; path=" + this.path : "") + ((this.domain) ? "; domain=" + this.domain : "") + "; expires=Thu, 01-Jan-70 00:00:01 GMT";
}
this.value = "";
}
function _getKey(keyString) {
var i; for (i=0;i<this.keyArray.length;i++){ if (this.keyArray[i][0] == keyString) { return this.keyArray[i][1]; break; }} return null;
}
function _setKey(keyString,keyValue) {
if (!keyValue) keyValue = null; var i;
for (i=0;i<this.keyArray.length;i++) {
if (this.keyArray[i][0] == keyString) { this.keyArray[i][1] = keyValue; this._buildValue(); return keyValue; break; }
}
this.keyArray[this.keyArray.length] = new Object(); this.keyArray[this.keyArray.length][0] = keyString; this.keyArray[this.keyArray.length][1] = keyValue; this.keyArray.length++; this._buildValue(); return keyValue;
}
function _delKey(keyString) {
var i; var found = false;
for (i=0;i<this.keyArray.length;i++){ if (this.keyArray[i][0] == keyString) { found = true; break; }}
if (found) { this.keyArray.length--; this.keyArray[i][0] = this.keyArray[this.keyArray.length][0]; this.keyArray[i][1] = this.keyArray[this.keyArray.length][1]; this._buildValue();
}}
function _getKeyArray() { return this.keyArray; }
function _buildValue() {
var i; this.value = "";
for (i=0;i<this.keyArray.length;i++) {
if (i>0) this.value = this.value + "&"; this.value = this.value + this.keyArray[i][0] + "=" + escape(this.keyArray[i][1]);
}}
function _buildKeyArray() {
var tempArray = split("&",this.value); this.keyArray.length = tempArray.length;
for (i=0;i<tempArray.length;i++) { this.keyArray[i] = split("=",tempArray[i]); if (this.keyArray[i][1]) this.keyArray[i][1] = unescape(this.keyArray[i][1]); }
}
function _setValue(value) { this.value = escape(value);	}
function _getValue() { return unescape(this.value); }
function _clearValue() { this.value = ""; this.keyArray.length = 0; }
function _setExpires(expires) { if (expires) this.expires = new Date(expires); return this.expires; }
function _setPath(path) { this.path = path; return this.path; }
function _setDomain(domain) { this.domain = domain; return this.domain; }
function _setSecure(secure) { this.secure = secure; return this.secure; }
function split(delimiter,splitString) {
var returnArray=new Object(); var i,j; var temp='';
for (i=0,j=0;i<=splitString.length;i++) {
if(splitString.charAt(i)==delimiter || i==splitString.length){ returnArray[j++]=temp; temp=''; } else { temp+=splitString.charAt(i); }
}
returnArray.length = j; return returnArray;
}
/*END COOKIE-PAL STUFF*/

/////////////////// P O P O M A T I C ////////////////////////////
// PopOMatic requires cookiepal.js to be included on any HTML page on which it is used
// not including it will cause a "cookiePal is undefined" error

// height = The height in pixels of the popup window
// width = The width in pixels of the popup window
// url = The URL of the HTML page to be loaded into the popup window
// number_of_visits = The number of visits, after the first, before the popup is displayed again
// number_of_days = A specific number of days, after the first visit, before the popup is displayed again

function PopOMatic(height,width,url,name,number_of_visits,number_of_days){
PopOMaticGlobal("240","320","http://radio.disney.go.com/popup/popup.html","NewWindow",4,7,"aCookie");
}

function PopOMaticGlobal(height,width,url,name,number_of_visits,number_of_days,cookieName){
	var visit_tally = 0;
	var cookie_date = 0;
	aCookie = new cookiePal(cookieName,"","","/","radio.disney.go.com");
	// assign default values if any are left null
	if (url==null){url = "popup.html"}
	if (height==null){height = "300"}
	if (width==null){width = "300"}
	if (name==null){name = "NewWindow"}
	if (number_of_visits==null){number_of_visits = 10}
	if (number_of_days==null){number_of_days = 10}
	var features = 'height=' + height + ',width='+width;
	// check for cookie
if (document.cookie.indexOf("rd-request-popup-clicked=1")==-1){
	if (aCookie.getKey("visit_tally") == null)
		{
		visit_tally = 1;
		today = new Date();
		//today.setMonth(today.getDate()+1);
		today.setDate(today.getDate()+number_of_days);
		cookie_date = today;
		//alert("cookie date variable is: " + cookie_date);
		aCookie.setExpires(cookie_date);
		aCookie.setKey("visit_tally",visit_tally);
		aCookie.setKey("cookie_date", cookie_date);
		aCookie.set();
		//alert("no cookie set");
		window.open(url, name, features);
		}
	// if cookie exists - increment tally
	else
		{
		visit_tally = aCookie.getKey("visit_tally");
		cookie_date = aCookie.getKey("cookie_date");
		visit_tally++;
		//alert("date pulled from cookie is: " + cookie_date);
		aCookie.setExpires(cookie_date);	
		aCookie.setKey("visit_tally",visit_tally);
		aCookie.set();
		//alert("cookie is set -- tally is: " + visit_tally);
		}
	
	// if tally reaches set amount - reset
	if (visit_tally >= number_of_visits + 1)
		{
		window.open(url, name, features);
		visit_tally = 1;
		aCookie.setKey("visit_tally",visit_tally);
		aCookie.set();
		}	
}
	}
	