
var chat_trnumber = ""; //have to do make this global since we might have to make an ifc call
var chat_prop_code = ""; //variable so that john's team can set on pages that dont swap domain
var chat_name = "";
var chat_number = "";
var chat_on = true;  // True is on; False is off

function chat_popup() {
	var c_teir = chat_getTeir();
	var c_propcode = chat_getPropertyCode();

	if (chat_name == "") {
		chat_name = "chatuser";
	}

	var id, day, url;
	url = "https://chat.harrahs.com/I3Root/chatlaunch.asp?queue=" + c_teir +
		"&Name=" + chat_name + "&type=chat&CustomData=" + chat_trnumber + "|" + c_propcode +"|" +s_pageName;

	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(url, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=400');");
}

function chat_display1(t) {
	var chatdisplay = document.getElementById("chatdisplay");
	chatdisplay.innerHTML = t.getContent();
}

function chat_getTeir() {
	var ucookie = readCookie("userInfoCookie");
	var dcookie = readCookie("demarcationCookie");
	var uarr = ucookie.split(dcookie);
	var teir = "";

	if (uarr.length > 1) {
		chat_name = uarr[0];
		teir = uarr[1];
	}


	if (teir == '') {
		try {
			teir = s_prop10;
		} catch (e) { }
	}
	teir = teir.toLowerCase();

	if (teir.indexOf("gold") > -1) return "GOLD";
	if (teir.indexOf("diam") > -1) return "DIAMOND";
	if (teir.indexOf("seven") > -1) return "SEVENSTAR";
	if (teir.indexOf("plati") > -1) return "PLATINUM";


	return "GR";
}

function chat_getPropertyCode() {
	if (chat_prop_code != "") return chat_prop_code;

	var propcode = '';
	var propMap = getPropCodeMap();  //function included by utility.js ... everyone should use this for the mapping
	var urlArray = window.location.href.split("/");
	var domain = urlArray[2];
	var folder = (urlArray[4] != null) ? urlArray[4] : '';

	domain = (domain == null) ? '' : domain.toLowerCase();
	folder = (folder == null) ? '' : folder.toLowerCase();

	propcode = (propMap[domain] != null) ? propMap[domain] : propMap[folder]; //check domain then folder for special case
	propcode = (propcode == null) ? '' : propcode; //make sure propcode is not null

	try {
		//getting it from s_prop1
		if (propcode == '') {
			propcode = s_prop1;
			if (propcode == 'corp') {
				if (document.forms['CheckAvailabilityForm']) {
					var regionCodeObj = document.forms['CheckAvailabilityForm'].regionCode;
					if (regionCodeObj) {
						propcode = regionCodeObj.value;
					}
				}
			}
		}
	} catch (e) { }


	if (propcode) propcode = propcode.toUpperCase();
	return propcode;

}

function chat_setTrNumberCallback(t) {
	var content = t.getContent();
	var start_pos = content.indexOf(">") + 1;
	var tmp_accountId = "";

	try {
		tmp_accountId = content.substring(start_pos, start_pos + 11);
		var tst_accountId = tmp_accountId - tmp_accountId;
		if (tst_accountId == 0) {
			chat_trnumber = tmp_accountId;
		}
	} catch (e) { }

	writeCookie("accountId", chat_trnumber, 0, "", ""); //write until browser is closed
}

function chat_setTrNumber() {
	//first check for sprop
	if (s_prop9) {
		if (s_prop9 != 0 && (s_prop9 - s_prop9) == 0) { //makes sure it is a number
			chat_trnumber = s_prop9;
			return; //done
		}
	}

	var this_url = window.location.href;
	var protocol = (this_url.indexOf("https") > -1) ? "https://" : "http://";
	var this_domain = window.location.host;
	var isLocalHost = (this_domain == "localhost");

	//alert("this_domain:"+this_domain);
	//alert("this_url:"+this_url);
	//alert("root_domain:"+root_domain);
	if (this_domain.toLowerCase() == root_domain &&
		readCookie("userSessionCookie") == "true") { //when on root domain we can check for accountid cookie if we are logged in
		chat_trnumber = readCookie("accountId");
	}
	if (this_domain.toLowerCase() != root_domain) { //if we are not on the root domain we can check because only this process would have written it
		chat_trnumber = readCookie("accountId");
	}

	//alert("chat_trnumber:"+chat_trnumber);
	if (chat_trnumber == "" &&
		(this_domain.toLowerCase() != root_domain ||
		(this_domain.toLowerCase() == root_domain && readCookie("userSessionCookie") == "true"))) { //if it is still blank then we have go to the specail viewer

//alert("Inside main IF");
		var specialviewer_url = "";
		if (this_domain.toLowerCase() == root_domain) {
			specialviewer_url = protocol + this_domain + "/content/specialviewer.jsp?_cb=" + escape(Date());
		} else {
			//have to add a refer
			if (isLocalHost) { //so i can test locally
				specialviewer_url = protocol + root_domain + "/content/specialviewer.jsp?_cb=" + escape(Date()) +
				"&refer=" + protocol + this_domain + "/asp_net/specialviewer.aspx";
			} else {//this is the one that would work with vanity domains
				specialviewer_url = protocol + root_domain + "/content/specialviewer.jsp?_cb=" + escape(Date()) +
					"&refer=" + protocol + this_domain + "/content/specialviewer.jsp";
			}
		}
//alert("specialviewer_url:"+specialviewer_url);		
		IFC.sync(specialviewer_url, "chat_setTrNumberCallback"); //remaining work will be done in callback
	}
}

function dsCallBack(t){

	//alert(document.cookie);
	return;
}

function chat_load(location) {

	if (chat_on == false) {

		if (chat_number != "") {
			var chatdisplay = document.getElementById("chatdisplay");
			if (chatdisplay) {
				chatdisplay.innerHTML = "Questions? Call " + chat_number;
			}
		}
		return;
	}


	var this_url = window.location.href;
	var protocol = (this_url.indexOf("https") > -1) ? "https://" : "http://";
	var this_domain = window.location.host;

	//what could happen on the propery pages is the domain could be not the root domain and then we wouldn't have a tier
	//can't sync accountid
	if (this_domain.toLowerCase() != root_domain) {
		var dsUrl = protocol + root_domain + "/asp_net/domainsync.aspx?names=PZN_userRecommendationType;userInfoCookie;demarcationCookie&referrerDomain=" + this_domain;
		IFC.sync(dsUrl, "dsCallBack");
	}
	
	chat_setTrNumber(); //performs a sync call to get the trnumber


	var chat_moduleUrl = protocol + this_domain +
		"/asp_net/chatmodule.aspx?propertyCode=" + chat_getPropertyCode() + "&displaylocation=" + location+"&title="+s_pageName;
	IFC.sync(chat_moduleUrl, 'chat_display1'); //loads the module
}

