function trim(storeLabel) {
	var lab = storeLabel;
	if (storeLabel.length > 30) 
		lab = storeLabel.substring(0,30);
	var a = lab.split('+');
	lab = a[0];
	for (var i = 1; i < a.length; i++) {
		lab += " "+a[i];
	}
	return lab;
}

function buildReachHistory(reachNumber) {
	var select = document.dialForm.numList;
	var reachHistory = "";
	for (var i=1; i < select.length; i++) {
		if (reachHistory != "")
			reachHistory += '*'+select.options[i].value;
		else 
			reachHistory = select.options[i].value;
	}
	var selectEntry = reachNumber;
	if (reachHistory == "") {
		select.options[select.length] = new Option(selectEntry,selectEntry,false,true);
		reachHistory = select.options[i].value;
	} else if (reachHistory.indexOf(selectEntry) == -1) {
		select.options[select.length] = new Option(selectEntry,selectEntry,false,true);
		reachHistory += '*'+select.options[i].value;
	}
	return reachHistory;
}

function setReachHistory(reachNumber) {
//	rnData.reachNumber = reachNumber;
//	rnData.store();
}

function setCallHistory(dialName,dialNum,dialCode) {
/*
	var select = document.callHistory.callsDialed;
	var callHistory = "";
	for (var i=1; i < select.length; i++) {
		if (callHistory != "")
			callHistory += '*'+select.options[i].value;
		else 
			callHistory = select.options[i].value;
	}
	var selectEntry = dialName+'-'+dialNum+'-'+dialCode;
	if (callHistory == "") {
		select.options[select.length] = new Option(trim(dialName),selectEntry,false,true);
		callHistory = select.options[i].value;
	} else if (callHistory.indexOf(selectEntry) == -1) {
		select.options[select.length] = new Option(trim(dialName),selectEntry,false,true);
		callHistory += '*'+select.options[i].value;
	}
	westfieldData.callHistory = callHistory;
	westfieldData.store();
*/
	refreshPage(location.href);
}

function refreshCookies() {
	refreshPage(location.href);
}

function resetCookies() {
	westfieldData.remove();
	rnData.remove();
	chData.remove();
	usageData.remove();
	refreshPage(location.href);
}

function refreshPage(url) {
	var ts = new Date();
	var loc;
	// add timestamp to make sure we don't retrieve a cached copy
	idx = url.indexOf("?timestamp=");
	if (idx != -1) {
		loc = url.substring(0,idx+11) + ts.getTime();
	} else if (url.indexOf("?") == -1) {
		loc = url + "?timestamp=" + ts.getTime();
	} else {
		idx = url.indexOf("&timestamp=");
		if (idx != -1) {
			loc = url.substring(0,idx+11) + ts.getTime();
		} else {
			loc = url + "&timestamp=" + ts.getTime();
		}
	}
	location.replace(loc);
}

function listingDial(dialName,dialNumber,dialCode) {
	var currEntry = document.dialForm;
	currEntry.dialName.value = dialName;
	currEntry.dialNumber.value = dialNumber;
	currEntry.dialCode.value = dialCode;
	westfieldData.dialName = dialName;
	westfieldData.dialNumber = dialNumber;
	westfieldData.dialCode = dialCode;
	westfieldData.store();
}

function listingCall(dialName,dialNumber,dialCode) {
	listingDial(dialName,dialNumber,dialCode);
	c2dPopup(dialName,dialNumber,dialCode);
}

function historyCall() {
	var select = document.callHistory.callsDialed;
	var callHistory = "";
	var dialInfo = select.options[select.selectedIndex].value;
	if (dialInfo.indexOf("none",0) > -1) {
		alert("You do not have a call history yet. Please click one of the small phones to dial a listing.");
		return;
	}
	var dialCode = dialInfo.substring(dialInfo.lastIndexOf('-')+1,dialInfo.length);
	var dialNumber = dialInfo.substring(dialInfo.length-dialCode.length-11,dialInfo.length-dialCode.length-1);
	var dialName = dialInfo.substring(0,dialInfo.length-dialCode.length-12);
	c2dPopup(dialName,dialNumber,dialCode);
}

function c2dPopup(partyName,telno,code) {
	if (partyName == "" || telno == "" || code == "") {
		alert("You have not selected a listing to dial. Please browse a listing page, or click on one of the small phones.");
		return;
	}
		
	url = generateC2DURL(escape(partyName),telno,code);
	OpenPopup(url,"joinPopup",500,500);
}

// Given a URL, urlStr, this windows opens up a popup window of size popW, popH 
// and centers it on the screen. If centered is true, the popup is centered on the screen. 
// If centered is false, it is not.

function OpenPopup(urlStr, wname, wd, ht, popupFeatures) {
	var w = 1000, h = 600;
	var popW = wd, popH = ht;
	var centered = false;

	if (document.all || document.layers) {
		w = screen.availWidth;
		h = screen.availHeight;
	}

	var leftPos = (w - popW) / 2
	var topPos = (h - popH) / 2;

	if(popupFeatures == null) {
		if (centered == true) {
			popupFeatures  = "width=" + popW + ",height=" + popH + ",top=" + topPos + ",left=" + leftPos + ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes";
		} else {
			popupFeatures  = "width=" + popW + ",height=" + popH + ",toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes";
		}
	}
	thePopup = window.open(urlStr, wname, popupFeatures);
	thePopup.focus();
}

/* cookie functions */


function Cookie(document,name,hours,path,domain,secure) {
	this.$document = document;
	this.$name = name;
	if (hours)
		this.$expiration = new Date(new Date().getTime() + hours*3600000);
	else
		this.$expiration = null;
	if (path) 
		this.$path = path;
	else
		this.$path = null;
	if (domain) 
		this.$domain = domain;
	else
		this.$domain = null;
	if (secure) 
		this.$secure = true;
	else
		this.$secure = false;
}

function _Cookie_store() {
	var cookieval = "";
	for (var prop in this) {
		if ((prop.charAt(0) == '$') || (typeof this[prop] == 'function'))
			continue;
		if (cookieval != "")
			cookieval += '&';
		cookieval += prop + ':' + escape(this[prop]);
	}

	var cookie = this.$name + '=' + cookieval;
	if (this.$expiration)
		cookie += '; expires=' + this.$expiration.toGMTString();
	if (this.$path) 
		cookie += '; path=' + this.$path;
	if (this.$domain) 
		cookie += '; domain=' + this.$domain;
	if (this.$secure)
		cookie += '; secure';
	this.$document.cookie = cookie;
}

function _Cookie_load() {
	var allcookies = this.$document.cookie;
	if (allcookies == "") return false;

	var start = allcookies.indexOf(this.$name + '=');
	if (start == -1) return false;
	start += this.$name.length + 1;
	var end = allcookies.indexOf(';',start);
	if (end == -1) end = allcookies.length;
	var cookieval = allcookies.substring(start,end);

	var a = cookieval.split('&');

	for (var i=0; i < a.length; i++) {
		a[i] = a[i].split(':');
	}

	for (var i=0; i < a.length; i++) {
		this[a[i][0]] = unescape(a[i][1]);
	}

	return true;
}

function _Cookie_remove() {
	var cookie;
	cookie = this.$name + '=';
	if (this.$path) cookie += '; path='+ this.$path;
	if (this.$domain) cookie += '; domain='+ this.$domain;
	cookie += '; expires=Fri, 02 Jan 1970 00:00:00 UTC';
	this.$document.cookie = cookie;
	document.cookie = cookie;
}

new Cookie();
Cookie.prototype.store = _Cookie_store;
Cookie.prototype.load = _Cookie_load;
Cookie.prototype.remove = _Cookie_remove;

var westfieldData = new Cookie(document,"P_DW_data",12*365,"/","");
westfieldData.load();

var rnData = new Cookie(document,"P_RN_data",12*365,"/","");
rnData.load();

var chData = new Cookie(document,"P_CH_data",12*365,"/","");
chData.load();

var usageData = new Cookie(document,"P_c2dCookie",12*365,"/","");
usageData.load();

function showCookies() {
	var allcookies = this.document.cookie;
	alert("all = "+allcookies);
}

function clearCookies() {
	westfieldData.remove();
	rnData.remove();
	chData.remove();
	usageData.remove();
}

