if (typeof(getCookie) == "undefined") { var getCookie = function(cookieName) { var cookieKey = cookieName + "="; var cookieArray = document.cookie.split(';'); for (var i=0; i < cookieArray.length; ++i) { var cookieReference = cookieArray[i]; while (cookieReference.charAt(0)==' ') { cookieReference = cookieReference.substring(1,cookieReference.length); } if (cookieReference.indexOf(cookieKey) == 0) { return ( unescape (cookieReference.substring(cookieKey.length,cookieReference.length)) ); } } } } var MYT = function(){ var _authUrl; var _adId, _clickPosition; var _loginWindow, _loginFormName = "savevehiclesloginform"; var _ajaxResponseCodes = { "Already Saved": "That listing has already been saved.", "Saved Sucessfully": "The listing has been saved." }; var _setLoginWindowVisibility = function(visibility) { if (_loginWindow) { _loginWindow.style.visibility = visibility; } }; return { hideLoginWindow: function(){ _setLoginWindowVisibility("hidden"); }, showLoginWindow: function(){ _setLoginWindowVisibility("visible"); }, isLoggedIn: function(){ if (getCookie('MYT_UID')) return true; return false; }, loginSaveListing: function(){ var _handleId = document[_loginFormName].HANDLE_ID.value; var _password = document[_loginFormName].PASSWORD.value; if (!_handleId) { alert("You must enter your user name."); return; } if (!_password) { alert("You must enter your password."); return; } this.hideLoginWindow(); var _query = "&HANDLE_ID=" + _handleId + "&PASSWORD=" + _password; return this.saveListing(_adId, _clickPosition, _query); }, saveListing: function(adId, clickPosition){ // register the adId and click position for this request _adId = adId, _clickPosition = clickPosition; // build the query var _query = "action=save1&AD_ID=" + _adId; if (arguments.length > 2) { _query += arguments[2]; } // make a request to the authentication "service" var _req = new ajax(); _req.callBackFunc = function(){ MYT.handleSaveListingResponse(_req, _adId, _clickPosition); }; _req.postData = _query; _req.loadXMLDoc("/find/login-popup"); }, handleSaveListingResponse: function(req, adId, clickPosition){ // get the response var _text = req.responseText; // handle any messages sent for (var _code in _ajaxResponseCodes) { if (_text.search("&" + _code) != -1) { alert(_ajaxResponseCodes[_code]); this.sendAuthentication(); return; } } // if no messages are sent, setup the login window var _left = 0, _top = 0; if (clickPosition.pageX) { _left = 200; _top = clickPosition.pageY-40; } else { _left = 380; _top = document.body.scrollTop+40; } var _divs = document.getElementsByTagName("div"); for (var i=0; i<_divs.length; i++) { if (_divs[i].className == "mytlogindiv") { _loginWindow = _divs[i]; } } _loginWindow.style.top = _top > 0 ? _top : 0; _loginWindow.style.left = _left > 0 ? _left : 0; _loginWindow.innerHTML = _text; // show the login window this.showLoginWindow(); }, sendAuthentication: function(){ if (!_authUrl) return; var _credentials = { "HANDLE_ID": document[_loginFormName].HANDLE_ID.value, "PASSWORD": document[_loginFormName].PASSWORD.value, "REDIRECT": window.location.href }; var _authForm = document.createElement("form"); _authForm.setAttribute("method", "post"); _authForm.setAttribute("action", _authUrl); for (var _key in _credentials) { var _field = document.createElement("input"); _field.setAttribute("type", "hidden"); _field.setAttribute("name", _key); _field.setAttribute("value", _credentials[_key]); _authForm.appendChild(_field); } document.body.appendChild(_authForm); _authForm.submit(); }, init: function(){ var _notLoggedInHeader = document.getElementById('mytLogoCntrNotLoggedIn'); var _loggedInHeader = document.getElementById('mytLogoCntrLoggedIn'); if (this.isLoggedIn()) { _notLoggedInHeader.style.display ='none'; _loggedInHeader.style.display ='block'; } else { _loggedInHeader.style.display ='none'; _notLoggedInHeader.style.display ='block'; _authUrl = _notLoggedInHeader.getElementsByTagName("a")[0].href; } } }; }(); /******************************************************************************* * The following aliases exist for backward compatibility. New code should make * use of the MYT namespace methods. */ if (window.location.href.search("listing") != -1) { var saveVehicle = function(adId, thisev) { return MYT.saveListing(adId, thisev); }; var performLogin = function(){ return MYT.loginSaveListing(); }; } var hideLogin = MYT.hideLoginWindow; /** * end aliases /******************************************************************************/ MYT.init();