//----------------------------------------------------------------------------  
// Code to determine the browser and version.  
//----------------------------------------------------------------------------  
function Browser() {  
  
  var ua, s, i, an;  
  
  this.isKonq  = false;  // Linux Konqueror  
  this.isOmni  = false;  // OmniWeb  
  this.isOpera = false;  // Opera  
  this.isGecko = false;  // Gecko  
  this.isWebTV = false;  // WebTV  
  this.isIcab  = false;  // iCab  
  this.isIE    = false;  // Internet Explorer  
  this.isNS    = false;  // Netscape  
  this.isSafari= false;  // Safari
  this.isMoz   = false;  // Other browsers  
  this.version = null;  
  this.an      = navigator.appName;  
  this.isJaveEn= navigator.javaEnabled();  
  this.cookies = navigator.cookieEnabled;
  if(document.getElementById) {
	  this.isDOM = true;
  }else{
	  this.isDOM = false;
  }
  
  ua = navigator.userAgent.toLowerCase();  
  
  s = "konqueror";  
  if ((i = ua.indexOf(s)) >= 0) {  
    this.isKonq = true;  
    this.version = parseFloat(ua.substr(i + s.length));  
    return;  
  }  
  
  s = "omniweb";  
  if ((i = ua.indexOf(s)) >= 0) {  
    this.isOmni = true;  
    this.version = parseFloat(ua.substr(i + s.length));  
    return;  
  }  
  
  s = "opera";  
  if ((i = ua.indexOf(s)) >= 0) {  
    this.isOpera = true;  
    this.version = parseFloat(ua.substr(i + s.length));  
    return;  
  }  
  
  s = "webtv";  
  if ((i = ua.indexOf(s)) >= 0) {  
    this.isWebTV = true;  
    this.version = parseFloat(ua.substr(i + s.length));  
    return;  
  }  
  
  s = "icab";  
  if ((i = ua.indexOf(s)) >= 0) {  
    this.isIcab = true;  
    this.version = parseFloat(ua.substr(i + s.length));  
    return;  
  }  
  
  s = "msie";  
  if ((i = ua.indexOf(s)) >= 0) {  
    this.isIE = true;  
    this.version = parseFloat(ua.substr(i + s.length));  
    return;  
  }  
  
//  s = "Netscape6/";  
  s = "netscape/";  
  if ((i = ua.indexOf(s)) >= 0) {  
    this.isNS = true;  
    this.version = parseFloat(ua.substr(i + s.length));  
    return;  
  }  
  
  // Treat any other "Gecko" browser as NS 6.1.  
  s = "gecko";  
  if ((i = ua.indexOf(s)) >= 0) {  
    this.isGecko = true;  
    this.version = parseFloat(ua.substr(ua.indexOf('; r') + s.length)) + " (Compatible with Netscape 6.1 or higher)";  
    return;  
  }  
  
  s = "mozilla/";  
  if ((i = ua.indexOf(s)) >= 0) {  
    this.isMozilla = true;  
    this.version = parseFloat(ua.substr(i + s.length));  
    return;  
  }  
  
  s = "safari";  
  if ((i = ua.indexOf(s)) >= 0) {  
    this.isSafari = true;  
    this.version = parseFloat(ua.substr(i + s.length));  
    return;  
  }  
}  
  
//detects OS name
function OS() {  
  var ua;  
  
  ua = navigator.userAgent.toLowerCase();  
  
  this.OS      = null;  
  
  if (checkIt('linux', ua)) {  
    this.OS = "Linux";  
    return;  
  }  
  else if (checkIt('x11', ua)) {  
    this.OS = "Unix";  
    return;  
  }  
  else if (checkIt('mac', ua)) {  
    this.OS = "Mac";  
    return;  
  }  
  else if (checkIt('win', ua)) {  
    this.OS = "Windows";  
    return;  
  }  
  else {  
    this.OS = "an unknown operating system";  
    return;  
  }  
  
  function checkIt(string, u) {  
      place = u.indexOf(string) + 1;  
      return place;  
  }  
}

//set browser and os global variables
var os = new OS();  
var browser = new Browser();

//******************************************************************
//
// "Internal" function to return the decoded value of a cookie
//
function getCookieVal (offset) {
	var endstr = document.cookie.indexOf (";", offset);
	if (endstr == -1)
		endstr = document.cookie.length;
	return unescape(document.cookie.substring(offset, endstr));
}
//
// Function to correct for 2.x Mac date bug. Call this function to
// fix a date object prior to passing it to SetCookie.
// IMPORTANT: This function should only be called *once* for
// any given date object! See example at the end of this document.
//
function FixCookieDate (date) {
	var base = new Date(0);
	var skew = base.getTime(); // dawn of (Unix) time - should be 0
	if (skew > 0) // Except on the Mac - ahead of its time
		date.setTime (date.getTime() - skew);
}
//
// Function to return the value of the cookie specified by "name".
// name - String object containing the cookie name.

// returns - String object containing the cookie value, or null if
// the cookie does not exist.
//
function GetCookie (name) {
	var arg = 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)
			return getCookieVal (j);
		i = document.cookie.indexOf(" ", i) + 1;
		if (i == 0) break;
	}
	return null;
}
//
// Function to create or update a cookie.
// name - String object containing the cookie name.
// value - String object containing the cookie value. May contain
// any valid string characters.
// [expires] - Date object containing the expiration data of the cookie. If
// omitted or null, expires the cookie at the end of the current session.
// [path] - String object indicating the path for which the cookie is valid.
// If omitted or null, uses the path of the calling document.
// [domain] - String object indicating the domain for which the cookie is
// valid. If omitted or null, uses the domain of the calling document.
// [secure] - Boolean (true/false) value indicating whether cookie transmission
// requires a secure channel (HTTPS).
//
// The first two parameters are required. The others, if supplied, must
// be passed in the order listed above. To omit an unused optional field,
// use null as a place holder. For example, to call SetCookie using name,
// value and path, you would code:
//
// SetCookie ("myCookieName", "myCookieValue", null, "/");
//
// Note that trailing omitted parameters do not require a placeholder.
//
// To set a secure cookie for path "/myPath", that expires after the
// current session, you might code:
//
// SetCookie (myCookieVar, cookieValueVar, null, "/myPath", null, true);
//
function SetCookie (name,value,expires,path,domain,secure) {
	document.cookie = name + "=" + escape (value) +
		((expires) ? "; expires=" + expires.toGMTString() : "") +
		((path) ? "; path=" + path : "") +
		((domain) ? "; domain=" + domain : "") +
		((secure) ? "; secure" : "");
}
// Function to delete a cookie. (Sets expiration date to start of epoch)
// name - String object containing the cookie name
// path - String object containing the path of the cookie to delete. This MUST
// be the same as the path used to create the cookie, or null/omitted if
// no path was specified when creating the cookie.
// domain - String object containing the domain of the cookie to delete. This MUST
// be the same as the domain used to create the cookie, or null/omitted if
// no domain was specified when creating the cookie.
//
function DeleteCookie (name,path,domain) {
	if (GetCookie(name)) {
		document.cookie = name + "=" +
			((path) ? "; path=" + path : "") +
			((domain) ? "; domain=" + domain : "") +
			"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

// Check if visitor's browser has cookies enabled
function checkCookies() {
	if(browser.cookies) {
		return true;
	}else{
		alert("Sorry, you must enable cookies to vote.");
		return false;
	}
}
//******************************************************************

//******************************************************************
addEvent(window, 'load', init, false);

var favorites = GetCookie("favorites");
var favoritesList = new Array();

if(favorites)
	if(favorites.length > 1) {
		var temp = favorites.split(",");
		for(var i = 0; i < temp.length; i++) {
					   // id     item       title      image
			favoritesList.push([temp[i], temp[i+1], temp[i+2], temp[i+3]]);
			i += 3;
		}
	}
	
function init() {
    if (!Sarissa || !document.getElementsByTagName) return;
    
    var formElements = document.getElementsByTagName('form');
    for (var i = 0; i < formElements.length; i++) {
        if (formElements[i].className.match(/\bitemFav\b/)) {
            addEvent(formElements[i], 'submit', submitFavorite, false);
        }
	if (formElements[i].className.match(/\bitemvote\b/)) {
            addEvent(formElements[i], 'submit', submitVote, false);
        }
    }
}

function submitFavorite(e) {
    /* Cancel the submit event, and find out which form was submitted */
    knackerEvent(e);
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
		
		// check if cookies are enabled
		if(!checkCookies) return;
		
		if(favoritesList.length == 20) {
		  alert("Only 20 items are allowed in your favorites list at one time.");
			return;
		}
	
	var id = target.elements['id'].value
	
	// check here if item already exists
	for(var i = 0; i < favoritesList.length; i++) {
		if(id == (favoritesList[i][0] + "~" + favoritesList[i][1])) {
		//alert("This item is already in your favorites list.");
			return;
		}
	}
    
    /* Set up the request */
    var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('POST', 'includes/addToFavorites.asp', true);
    
    /* The callback function */
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200){
                addFavorite(xmlhttp.responseXML, target);
            }else
                target.submit();
        }
    }
    
    /* Send the POST request */
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('id=' + id);
   
}

function addFavorite(responseXML, target) {

	var id    = responseXML.getElementsByTagName('id')[0].firstChild.data;
	var item  = responseXML.getElementsByTagName('item')[0].firstChild.data;
	var img   = responseXML.getElementsByTagName('image')[0].firstChild.data;
	var title = responseXML.getElementsByTagName('title')[0].firstChild.data;

	// write Favs to cookies!!!!
	addToFavoriteArray(id, item, title, img);
}

function addToFavoriteArray(id, item, title, image) {
	var found = false;
	var changed = false;

	// get out of function if any value is empty
	if(isNaN(id) || id == null) return;
	if(item == null) return;
	if(title == null) return;
	
	for(var i = 0; i < favoritesList.length; i++) {
		if((id+item) == (favoritesList[i][0]+favoritesList[i][1])) {

			//favorite already exists in array, ignore
			found = true;
			break;
		}
	}
					
	//favorite is new to array, add'em
	if(!found) {
	
		//var singQuot = /\'/g;
		title = title.replace(/,/g, "~");
		//title = title.replace(singQuot, "\'");
		//alert(title);
		favoritesList.push([id, item, title, image]);
		changed = true;
	}

	//only write page contents if changes have taken place
	if(changed) {
	
		// set date for cookie expiration
		var myDate=new Date();
		myDate.setDate(myDate.getDate()+365);

		SetCookie ("favorites",favoritesList,myDate);

		writeFavorites();
	}
}

function removeFromFavoriteArray() {

	var f = document.favoritesForm;
	var changed = false;
	
	for(var i = 0; i < f.elements.length; i++) {
		if(f.elements[i].type == "checkbox") {
			if(f.elements[i].checked) {
				var val  = f.elements[i].value;
				for(var j = 0; j < favoritesList.length; j++) {
					if(val == favoritesList[j][0]) {
						//remove selected trustee
						favoritesList.splice(j, 1);
						changed = true;
						break;
					}
				}
			}
		}
	}

	//only write page contents if changes have taken place
	if(changed) {
		if(favoritesList.length == 0) {
			DeleteCookie ("favorites");
		}else{
			SetCookie ("favorites",favoritesList);
		}
		writeFavorites();
	}
}

function writeFavorites() {

	var html = '';
	var favs = document.getElementById('favorites');
	var id = 0;
	var img = '';
	var tipe = '';
	var name = '';

	for(var i = 0; i < favoritesList.length; i++) {
		
		id   = favoritesList[i][0];
		tipe = favoritesList[i][1];
		name = favoritesList[i][2];
		img  = favoritesList[i][3];

		html += "<input type=\'checkbox\' name=\'id\' value=\'" + id + "\' />" + "<img src=\'" + img + "\' width=\'20\' height=\'20\' alt=\'\' />&nbsp;<a href=\'details.asp?type=" + tipe + "&amp;id=" + id + "\'>" + name.replace(/~/g, ",") + "</a><br />";

	}
	favs.innerHTML = html;
}

//function submitVote(e) {
function submitVote(target) {

    // check if cookies are enabled
		if(!checkCookies) return;
	
	var id = target.elements['id'].value;
	var rs = target.elements['rs'].value;
	var filters = target.elements['filters'].value;
	var rating = 0;

	for(var i = 0; i < target.elements.length; i++) {
		if(target.elements[i].type == "radio") {
		  if(target.elements[i].name == "rating") {
  			if(target.elements[i].checked) {
  				rating = target.elements[i].value;
  				//break;
  			}
				target.elements[i].disabled = true;
			}
		}
	}

    /* Set up the request */
    var xmlhttp =  new XMLHttpRequest();
    xmlhttp.open('POST', 'includes/vote.asp', true);
    
    /* The callback function */
    xmlhttp.onreadystatechange = function() {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200){
                completeVote(xmlhttp.responseXML, target);
            }else
								alert(xmlhttp.status);
                //target.submit();
        }
    }
    
    /* Send the POST request */
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send('id=' + id + '&rs=' + rs + '&filters=' + filters + '&rating=' + rating);
   
}

function completeVote(responseXML, target) {

	var success = responseXML.getElementsByTagName('success')[0].firstChild.data;
	var rating  = responseXML.getElementsByTagName('rating')[0].firstChild.data;

	if(success) {
		//target.innerHTML = "Your Vote: " + rating;
		document.getElementById("vote__layer").innerHTML = "your vote: <b>" + rating + "</b>";
	}else{
	  alert(success);
	}
	
	for(var i = 0; i < target.elements.length; i++) {
		if(target.elements[i].type == "radio") {
		  if(target.elements[i].name == "rating") {
				target.elements[i].disabled = false;
			}
		}
	}
}
//******************************************************************


//changes obj display property from none to block
var visible = false;
function switchDisplay(obj, url) {
	if(browser == null)
		browser = new Browser();

	if (!browser.isOpera && document.getElementById) {
		var n = document.getElementById(obj);
		if (visible == false) {
			n.style.display = 'block';
			visible = true;
		}else{
			n.style.display = 'none';
			visible = false;
		}
	}else{	//if browser doesn't read getElementById send visitor to given url
		location.href = url;
	}
}

//returns current users year
function getThisYear(){
	var today = new Date();
	var year = today.getYear();
	var day = today.getDay();
	var month = today.getMonth();

	//if (browser == "Opera" || browser == "Gecko" || browser == "Netscape Navigator") 
	//	year = year + 1900;
	if(year < 2000) 
		year += 1900;

	return year;
}

function getOtherNavigation() {
	var nav = "";
	
	nav = "<a href=\"advsearch.asp\" onmouseover=\"status='Advanced Search';return true;\" onmouseout=\"status='';return false;\" title=\"Advanced Search\">advanced search</a> | ";
	nav += "<a href=\"links.asp\" onmouseover=\"status='Doors Links';return true;\" onmouseout=\"status='';return false;\" title=\"Links\">links</a> | ";
	nav += "<a href=\"help.asp\" onmouseover=\"status='Help';return true;\" onmouseout=\"status='';return false;\" title=\"Help\">help</a> | ";
	nav += "<a href=\"credits.asp\" onmouseover=\"status='Credits';return true;\" onmouseout=\"status='';return false;\" title=\"Credits\">credits</a> | ";
	nav += "<a href=\"feedback.asp\" onmouseover=\"status='Feedback';return true;\" onmouseout=\"status='';return false;\" title=\"Feedback\">feedback</a> | ";
	nav += "<a href=\"privacy.asp\" onmouseover=\"status='Privacy';return true;\" onmouseout=\"status='';return false;\" title=\"Privacy\">privacy</a> | ";
	nav += "<a href=\"terms.asp\" onmouseover=\"status='Terms &amp; Conditions';return true;\" onmouseout=\"status='';return false;\" title=\"Terms &amp; Conditions\">terms &amp; conditions</a> | ";
	nav += "<a href=\"disclaimer.asp\" onmouseover=\"status='Disclaimer';return true;\" onmouseout=\"status='';return false;\" title=\"Disclaimer\">disclaimer</a> | ";
	nav += "<a href=\"mailto:webmaster&#64;doorsinfo.com\" onmouseover=\"status='contact us';return true;\" onmouseout=\"status='';return false;\" title=\"contact us\">contact us</a>";
	
	return nav;
}

function writeBrowserLink() {
	var sniff = "";
	
	if(browser == null)
		browser = new Browser();
	
	if (browser.isIE) {
		sniff = ("&nbsp;<a style=\"cursor:hand\" onMouseOver=\"status='Make DoorsInfo.com my start page!';return true;\" onMouseOut=\"status='';return false;\"onclick=\"this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.doorsinfo.com');\"><img src=\"images/common/sethome.gif\" border=\"0\" width=\"14\" height=\"14\" alt=\"Make DoorsInfo.com my start page!\"/></a>&nbsp;<a style=\"cursor:hand\" onMouseOver=\"status='Make DoorsInfo.com my start page!';return true;\" onMouseOut=\"status='';return false;\" onclick=\"this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.doorsinfo.com');\" title=\"Make DoorsInfo.com my start page!\">Make DoorsInfo.com your start page!</a><br />");
		sniff += ("&nbsp;<a style=\"cursor:hand\" onMouseOver=\"status='Bookmark DoorsInfo.com NOW!';return true;\" onMouseOut=\"status='';return false;\" onclick=\"window.external.AddFavorite(location.href, document.title);\"><img src=\"images/common/favorites.gif\" width=\"15\" height=\"13\" alt=\"Bookmark DoorsInfo.com NOW!\" /></a>&nbsp;<a style=\"cursor:hand\" onMouseOver=\"status='Bookmark DoorsInfo.com NOW!';return true;\" onMouseOut=\"status='';return false;\" onclick=\"window.external.AddFavorite(location.href, document.title);\" title=\"Bookmark DoorsInfo.com NOW!\">Bookmark DoorsInfo.com NOW!</a><br />");
	}else if (browser.isOpera) {
		sniff = ("<img src=\"images/common/opera_bookmark.gif\" width=\"12\" height=\"15\" alt=\"Bookmark DoorsInfo.com NOW!\" />&nbsp;Bookmark DoorsInfo.com NOW! Press Crtl & Alt & B<br />");
	}else if (browser.isNS || browser.isGecko) {
		sniff = ("<img src=\"images/common/ns_bookmark.gif\" width=\"16\" height=\"16\" alt=\"Bookmark DoorsInfo.com NOW!\" />&nbsp;Bookmark DoorsInfo.com NOW! Press Crtl & D<br />");
	}else {
		sniff = ("&nbsp;Don\'t Forget to Bookmark DoorsInfo.com NOW!<br />");
	}
document.write(sniff);
}

//set selected vote link style read from query string
function setVoteLinkStyle() {
	var elm;		//document element
	var obj;		//document object
	var url = "";	//url query string
	
	if(browser == null)
		browser = new Browser();
	
	//get querystring from parent url
	url = unescape(self.location.search);
	
	//check url "type" parameter
	if(url.indexOf("type=book") != -1) {
		elm = "books";
	}else if(url.indexOf("type=video") != -1) {
		elm = "videos";
	}else if(url.indexOf("type=mag") != -1) {
		elm = "mags";
	}else{
		elm = "albums";
	}

	if(document.getElementById)
		obj = document.getElementById(elm)
	else if(document.all)
		obj = document.all(elm)

	//set style properties
	if(browser.isIE || browser.isNS || browser.isGecko) {
		obj.className = "current";
	}
}

function writeBrowserSniffImage(type) {
	if(browser == null)
		browser = new Browser();

	var sniff = "";
	if (type == "rate") {
		sniff = "<img src=\"/images/common/chart003_6641.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Rate This Item\" />&nbsp;Rate";
	}else
		if (browser.isIE) {
			sniff = "<img src=\"/images/common/ie_mail.gif\" width=\"16\" height=\"16\" border=\"0\" alt=\"Send Detail\'s to a Friend!\" />&nbsp;Send to a friend";
		}else if (browser.isOpera) {
			sniff = "<img src=\"/images/common/opera_mail.gif\" width=\"14\" height=\"11\" border=\"0\" alt=\"Send Detail\'s to a Friend!\" />&nbsp;Send to a friend";
		}else{
			sniff = "<img src=\"/images/common/ns_mail.gif\" width=\"17\" height=\"12\" border=\"0\" alt=\"Send Detail\'s to a Friend!\" />&nbsp;Send to a friend";
		}
document.write(sniff);
}

function writeSocialBookmarkLinks(m_text) {
	var bookmarks = '<span style="font-size:10px;">' + m_text + '&nbsp;</span>';
	var m_title = encodeURIComponent(document.title);
	var m_url = encodeURIComponent(location.href);
	
	var delicious = '<a href="http:\/\/del.icio.us\/post?title=' + m_title + '&amp;url=' + m_url + '"><img src="images/social/delicious.png" alt="Delicious" width="18" height="18" border="0" title="Delicious" /></a>&nbsp;&nbsp;';
	var spurl = '<a href="http:\/\/www.spurl.net\/spurl.php?v=3&amp;title=' + m_title + '&amp;url=' + m_url + '&amp;blocked=' + m_title + '"><img src="images/social/spurl.jpg" alt="Spurl" width="18" height="18" border="0" title="Spurl" /></a>&nbsp;&nbsp;';
	var furl = '<a href="http:\/\/www.furl.net\/storeIt.jsp?t=' + m_title + '&amp;u=' + m_url + '"><img src="images/social/furl.png" alt="Furl" width="19" height="18" border="0" title="Furl" /></a>&nbsp;&nbsp;';
	var digg = '<a href="http:\/\/digg.com\/submit?phase=2&amp;url=' + m_url + '"><img src="images/social/diggman.png" alt="Digg" width="18" height="18" border="0" title="Digg" /></a>';
	
	bookmarks = bookmarks + delicious + spurl + furl + digg;
	
	document.write(bookmarks);
}

//opens new send to window
function rec(item, type){
  var recommend = "recommend.asp?id=" + item + "&type=" + type
  var newWindow
	openWindow(recommend,"","alwaysraised=yes,width=550,height=325");
/*	if (!newWindow || newWindow.closed) { 
		newWindow = window.open (recommend,"","alwaysraised=yes,width=550,height=325"); 
	} else { newWindow.focus(); }*/
}

function popUp(pic,type) {
	var newWindow
	var locat = "pics.asp?pic=" + pic
	var ion = "&type=" + type
	pic = locat + ion
	openWindow(pic,"","alwaysraised=yes,width=275,height=285");
/*	if (!newWindow || newWindow.closed) { 
		newWindow = window.open (pic,"","alwaysraised=yes,width=275,height=285"); 
	} else { newWindow.focus(); }*/
}

function checkCookies() {
	if(browser.cookies) {
		return true;
	}else{
		alert("Sorry, you must enable cookies to vote.");
		return false;
	}
}

var $focus = 0;
function searchFocus() {

	if($focus < 1) {
		document.searchX.qu.value = '';
		$focus = 1;
	}	
}

var oldObj = "";
function showVoteLink(objIDName) {
	
	if(oldObj != "")
		document.getElementById(oldObj).style.display = 'none';
	
	document.getElementById(objIDName).style.display = 'block';


	oldObj = objIDName;
}

function htmlEncode(s) {
        var str = new String(s);
        str = str.replace(/&/g, "&amp;");
        str = str.replace(/</g, "&lt;");
        str = str.replace(/>/g, "&gt;");
        str = str.replace(/"/g, "&quot;");
        return str;
}

function checkOnce(elm) {
  var x = document.getElementsByTagName("input");

	for(var i = 0; i < x.length; i++) {
	  if(x[i].type == "checkbox")
	    if(elm.value != x[i].value)
	  	  x[i].checked = false;
	}
}

function openWindow(win, name, props) {
	var newWindow
	if (!newWindow || newWindow.closed) { 
		newWindow = window.open (win,name,props); 
	} else { newWindow.focus(); }
}