
function getCookie(name) {
	var tC = unescape(document.cookie).split("; ");
	for (var i = tC.length - 1; i >= 0; i--) {
		var x = tC[i].split("=");
		if (name == x[0]) return unescape(x[1]);
	}
	return "";
}

function displayPasswordInput() {
	document.getElementById("pwsLbl").style.display = "none";
	document.getElementById("pws").style.display = "inline";
	document.getElementById("pws").focus();
}

function loadUsername() {
	var loginControl = document.getElementById("username");
	var cookie_value = getCookie("ANALOGINID");
	if (loginControl != undefined) {
		if (cookie_value.length > 0)
			loginControl.value = cookie_value;
	}
}

var pic1;
function UpdateSignonS6() {
	if (name == undefined || name == "") {
		if (document.images) {
			pic1 = new Image(1, 1);
			pic1.src = "https://secure.hq.staystudio6.com/ss/login-m6.do?ProfileId=NONE";
		}
	}
}

var name = getCookie("ANAUSERNAME");

function UpdateSignOnDiv() {
	if (name == undefined || name == "") {
		var loginid = getCookie("ANALOGINID");
		if (loginid.length == 0) loginid = "Username";
		document.write("<form method=\"post\" id=\"signonForm\" action=\"" + SecureURL + "/ms/verify-customer.do\">");
		document.write("<div class=\"navSignOn\">");
		document.write("<input id=\"username\" name=\"loginid\" type=\"text\" value=\"" + loginid + "\" onfocus=\"this.value=''; focusHasBeenSet = true;\" autocomplete=\"off\" />");
		document.write("<input id=\"pwsLbl\" name=\"pwsLbl\" type=\"text\" value=\"Password\" style=\"display:inline;\" onfocus=\"displayPasswordInput()\" autocomplete=\"off\" />");
		document.write("<input id=\"pws\" name=\"pwd\" type=\"password\" autocomplete=\"off\" style=\"display:none;\" onfocus=\"this.value=''; focusHasBeenSet = true; \"/>");
		document.write("<input id=\"btnSignIn\" type=\"image\" name=\"imageField\" src=\"" + ImgARL + "/i/btn_signin_sm.gif\" style=\"\" align=\"absmiddle\"/><div class=\"navProfile\"><a href=\"" + SecureURL + "/ms/enrollment.do\">Create a Profile</a></div>");
		document.write("</div>");
		document.write("</form>");
	} else {
	document.write("<div class=\"navSignOn\"><span class=\"user\">Welcome, " + name + " </span> | <span class=\"profile\"><a href=\"" + SecureURL + "/ms/my-motel6-login.do\">My Account</a></span> | <span class=\"logout\"><a href=\"" + SecureURL + "/ms/logout.do\">Sign Out </a></span></div>");
	}
}

/* MY FAVORITES */
var req; //httprequest

function HideErrDiv() {
	var errDiv = document.getElementById("errDiv");
	if (errDiv != undefined) errDiv.style.display = "none";
	try {//if on property map page, this call will reset the nav and poi controls
		ResetUIControls();
	} catch (e) {
		//otherwise,do nothing
	}
}

function ShowErrDiv(header,msg) {
	var errDiv = document.getElementById("errDiv");
	var errDescDiv = document.getElementById("errDescDiv");
	var errHeader = document.getElementById("errHeader");
	if (errDiv != undefined) errDiv.style.display = "block";
	if (errDescDiv != undefined) errDescDiv.innerHTML = msg;
	if (errHeader != undefined) errHeader.innerHTML = header;
	try {//if on property map page, this call will reset the nav and poi controls
		ResetUIControls();
	} catch (e) {
		//otherwise,do nothing
	}
}

function HideFavDiv() {
	var favDiv = document.getElementById("favDiv");
	if (favDiv != undefined) favDiv.style.visibility = "hidden";
}

function trimNumber(s) {
  while (s.substr(0,1) == '0' && s.length>1) { s = s.substr(1,9999); }
  return s;
}

function ShowFavDiv(innerHTML) {
	var favDiv = document.getElementById("favDiv");
	if (favDiv != undefined) {
		favDiv.style.visibility = "visible";
		favDiv.innerHTML = innerHTML;
	}
}

function UpdateAddFavoritesLink() {
	HideFavDiv();
	if (name != undefined && name != "") {
		var motelid = GetMotelID();
		var brand = GetBrand();
		CheckFavListAndUpdateDiv(motelid, brand);
	}
}

function CheckFavListAndUpdateDiv(motelid, brand) {
	HideErrDiv();
	if (isNaN(motelid)) {
		return false;
	}
	else {
		if (motelid < 1 && motelid > 9999) {
			return false;
		}
		else {
			var url = "/ms/check-my-favorite-Property.do?propertyId=" + motelid + "&propertyBrandId=" + brand ;
			loadXMLDoc(url, UpdateFavDivInnerHTML, "GET", null);
		}
	}
}

function UpdateFavDivInnerHTML() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var resp = req.responseText;
			if (resp != null) {
				var status = resp.substr(resp.length - 4);
				if (status == "true")
					ShowFavDiv(GetInFavoritesMsg());
				else
					ShowFavDiv(GetAddFavoritesLink());
			}
		} 
		else {
			return false;
		}
	}
}

function AddToMyFavorites(motelid, brand) {
	HideErrDiv();
	if (isNaN(motelid)) {
		return false;
	}
	else {
		if (motelid < 1 && motelid > 9999) {
			return false;
		}
		else {
			var url = "/ms/add-my-favorite-Property.do?propertyId=" + motelid + "&propertyBrandId=" + brand;
			loadXMLDoc(url, UpdateFavDivInnerHTMLAfterAddCall, "GET", null);
		}
	}
}

function UpdateFavDivInnerHTMLAfterAddCall() {
	if (req.readyState == 4) {
		if (req.status == 200) {
			var resp = req.responseText;
			if (resp != null) {
				var status = resp.substr(resp.length - 4);
				if (status == "true")
					ShowFavDiv(GetInFavoritesMsg());
				else 
					ShowErrDiv("Favorite Motel Not Added.", "There was a problem trying to add this motel to your favorites. Please try again later.");
			}
		} 
		else {
			return false;
		}
	}
}

function GetAddFavoritesLink() {
	var link = "<img src=\"/i/m6_fav.gif\" width=\"23\" height=\"25\" style=\"margin:0px 0px -7px 5px;\" /><a href=\"javascript:void(0);\" onclick=\"AddToMyFavorites(" + GetMotelID() + ",'" + GetBrand() + "');\">Add to Favorites</a>";
	return link;
}

function GetInFavoritesMsg() {
	var link = "<img src=\"/i/m6_fav.gif\" width=\"23\" height=\"25\" style=\"margin:0px 0px -7px 5px;\" />My Favorite Motel";
	return link;
}

function GetBrand() {
	var checkPropertyObj = GetControlByID("input", "checkProperty");
	var brand = "M6";
	if (checkPropertyObj) {
		var value = checkPropertyObj.value;
		var idAndBrand = value.split("+");
		brand = idAndBrand[1];
	}
	return brand;
}

function GetMotelID() {
	var motelid = getQueryStringValue("num"); //.net pages
	if (motelid == "") motelid = getQueryStringValue("property"); //jsp page
	if (motelid == "") {
		var checkPropertyObj = GetControlByID("input", "checkProperty");
		if (checkPropertyObj) {
			var value = checkPropertyObj.value;
			var idAndBrand = value.split("+");
			motelid = idAndBrand[0];
			
		}
	}
	motelid = trimNumber(motelid);
	return motelid;
}

function GetXmlHttpObject() {
	var xmlHttp = null;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch (e) {
		// Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function loadXMLDoc(url, processFunc, method, data) {
	req = GetXmlHttpObject();
	if (req) {
		req.onreadystatechange = processFunc;
		req.open(method, url, true);
		req.send(data);
	}
}

/* My Motel6 Menu */
function GetM6Motel6SubMenu() {
	if (name != undefined && name != "") {
		document.write("<li class=\"sub\"><a href=\"" + SecureURL + "/ms/get_all_my_upcoming_reservation.do\">Upcoming Reservations</a></li>");
		document.write("<li class=\"sub\"><a href=\"" + SecureURL + "/ms/get_all_my_past_reservation.do\">Past Reservations</a></li>");
		document.write("<li class=\"sub\"><a href=\"" + SecureURL + "/ms/my-motel6-login.do\">My Favorite Motels</a></li>");
		document.write("<li class=\"sub\"><a href=\"" + SecureURL + "/ms/edit-profile.do\">Edit Profile</a></li>");
		document.write("<li class=\"sub\"><a href=\"" + HrefVar + "/reservations/my_motel6_faq.aspx\">Frequently Asked Questions</a></li>");
	}
}