req = null;
function getCommenterName() {
	cValue = "";
	dc = document.cookie;
	cookies = dc.split(";");
	for (x=0;x<cookies.length;x++) {
		if (cookies[x].match(/commenter_name=/)) {
		cValue = cookies[x].replace(/commenter_name=/, "");
		}
	}
	if (cValue.length>1) {
		return unescape(cValue);
	} else {
		return "";
	}
}

function trimString (str) {
  str = this != window? this : str;
  return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function getSelectedState(st) {
	opts = document.getElementById("statesel").options;
	for (i=0;i<opts.length;i++) {
		if (opts[i].value == st) {
			document.getElementById("statesel").selectedIndex=i;
		}
	}
}

loginWhere = "";

function doLogin(where) {
	var agt=navigator.userAgent.toLowerCase();
	loginWhere=where;
	document.getElementById("loginform_" + where).style.display = "block";
	if (where == "top") { document.getElementById("account-info").style.display = "none"; }
	return "";
}

function ajax(username, password) {
	username = escape(username);
	password = escape(password);
	req = false;
	if(window.XMLHttpRequest) {
		try {
				req = new XMLHttpRequest();
		} catch(e) {
				req = false;
		}
	} else if(window.ActiveXObject) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				req = false;
			}
		}
	}
	if(req) {
		req.onreadystatechange = login_response;
		req.open("POST", "/cgi-bin/mte/plugins/Profile/profile.cgi", true);
		req.send("__mode=login&ajax=true&profile_username=" + username + "&profile_password=" + password);
	} else {
		// something didn't work
	}
}

function login_response() {
	if (req.readyState == 4) {
		if (req.responseText.match(/Success/)) {
			uname = req.responseText.substring(8);
			if (document.getElementById("loginform_bottom")) {
				document.getElementById("loginform_bottom").style.display = "none";
				document.getElementById("post-comment").style.display = "block";
			}
			if (document.getElementById("display_name")) {
				document.getElementById("display_name").value = uname;
			}
			if (document.getElementById('talkform')) {
				document.getElementById('talkform').onsubmit = null;
				document.getElementById('talkform').submit();
			}
			document.getElementById("loginform_top").style.display = "none";
			document.getElementById("account-info").style.display = "block";
			document.getElementById("membername").innerHTML = "Hello, <a href=\"/user/profile/" + trimString(uname) + "\">" + uname + "!</a>";
			document.getElementById("account-info").innerHTML = "You are logged in. <a href=\"/user/logout\">Log Out</a> | View/edit <a href=\"/user/profile.html\">My Account</a>";

		} else {
			document.getElementById("loginMsg_" + loginWhere).innerHTML = req.responseText;
			document.getElementById("loginMsg_" + loginWhere).style.background = "#ffffff";
			document.getElementById("loginMsg_" + loginWhere).style.border = "2px solid #efdf2a";
		}
	}
}

function formCheck(e) {
	form = document.getElementById('profile_signup');
	isEmpty = false;
	elems = form.elements;
	for (x=0;x<elems.length;x++) {
		elem = elems[x];
		if (elem.name != "zip" && elem.name != "terms_accept") {
			if (elem.value == "") {
				elem.style.border = "2px solid red";
				isEmpty = true;
			} else {
				elem.style.border = "";
			}
		} else if (elem.name == "terms_accept") {
			if (!elem.checked) {
				elem.style.backgroundColor = "red";
				isEmpty = true;
			} else {
				elem.style.border = "";
			}
		}
	}
	if (isEmpty) {
		alert("You must fill out all highlighted fields and read and accept the terms of use before submitting.");
		if (e && e.preventDefault) e.preventDefault(); // DOM style
	}
	return !isEmpty;
}

function formListener() {
	form = document.getElementById('profile_signup');
	if (window.attachEvent) {
		form.attachEvent('onsubmit', function(){return formCheck();});
	} else {
		form.addEventListener('submit', formCheck, true);
	}
}

function talkCount(txt) {
	if (txt.value.length>64) txt.value = txt.value.substring(0,64);
	document.getElementById('numLeft').innerHTML = (64-txt.value.length) + " characters left";
}

function showTab(tabNum) {
	document.getElementById("tab_1").style.display = document.getElementById("tab_2").style.display = document.getElementById("tab_3").style.display = "none";
	document.getElementById("tab_" + tabNum).style.display = "block";
	document.getElementById("menu").style.backgroundImage = "url('/images/profile-menu-bgd_" + tabNum + ".gif')";
	if (tabNum == 3) {
		document.getElementById("mode").value = "se_pass_change";
	} else {
		document.getElementById("mode").value = "se_edit";
	}
}
// Copyright (c) 1996-1997 Athenia Associates.
// http://www.webreference.com/js/
// License is granted if and only if this entire
// copyright notice is included. By Tomer Shiran.

function setCookie (name, value, expires, path, domain, secure) {
    var curCookie = name + "=" + escape(value) + (expires ? "; expires=" + expires : "") +
        (path ? "; path=" + path : "") + (domain ? "; domain=" + domain : "") + (secure ? "secure" : "");
    document.cookie = curCookie;
}

function getCookie (name) {
    var prefix = name + '=';
    var c = document.cookie;
    var nullstring = '';
    var cookieStartIndex = c.indexOf(prefix);
    if (cookieStartIndex == -1)
        return nullstring;
    var cookieEndIndex = c.indexOf(";", cookieStartIndex + prefix.length);
    if (cookieEndIndex == -1)
        cookieEndIndex = c.length;
    return unescape(c.substring(cookieStartIndex + prefix.length, cookieEndIndex));
}

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";
}

function footerNumbers(page,index,entryCount) {
	var pageRange; 

	var filePrefix; var indexFile;
	if (index == "videos-main" || index == "talk-main" || index == "features-main") { filePrefix = ""; indexFile = "index"; }
	if (index == "videos-viewed" || index == "features-viewed") { filePrefix = "viewed"; indexFile = filePrefix; }
	if (index == "talk-active") { filePrefix = "active"; indexFile = filePrefix; }
	if (index == "talk-titles") { filePrefix = "titles"; indexFile = filePrefix; }
	
	if ( entryCount > 50) {	var outputP6 = "<a href=\"" + filePrefix + "6.html\">6</a>"; } else { var outputP6 = ""; }
	if ( entryCount > 40) {	var outputP5 = "<a href=\"" + filePrefix + "5.html\">5</a>"; } else { var outputP5 = ""; } 
	if ( entryCount > 30) {	var outputP4 = "<a href=\"" + filePrefix + "4.html\">4</a>"; } else { var outputP4 = ""; }
	if ( entryCount > 20) {	var outputP3 = "<a href=\"" + filePrefix + "3.html\">3</a>"; } else { var outputP3 = ""; }
	if ( entryCount > 10) {	var outputP2 = "<a href=\"" + filePrefix + "2.html\">2</a>"; } else { var outputP2 = ""; }
	var outputP1 = "<a href=\"" + indexFile + ".html\">1</a>";
	
	var previousPage = ""; 
	var nextPage = "";
	
	if (page == 1) {
		if (entryCount < 10 ) { pageRange = "1-" + entryCount ; } else { pageRange = "1-10"; }		
		outputP1 = "<span class=\"active\">1</span>";
		if (entryCount > 10) { nextPage = " <a href=\"" + filePrefix + "2.html\">Next &raquo;</a>"; }
	}
	if (page == 2) {
		if (entryCount < 20 && entryCount > 10 ) { pageRange = "11-" + entryCount; } else { pageRange = "11-20"; }
		outputP2 = "<span class=\"active\">2</span>";
		if (entryCount > 20) { nextPage = " <a href=\"" + filePrefix + "3.html\">Next &raquo;</a>"; }
		previousPage = "<a href=\"" + indexFile + ".html\">&laquo; Previous</a> ";
	}
	if (page == 3) {
		if (entryCount < 30 && entryCount > 20 ) { pageRange = "21-" + entryCount; } else { pageRange = "21-30"; }
		outputP3 = "<span class=\"active\">3</span>";
		if (entryCount > 30) { nextPage = " <a href=\"" + filePrefix + "4.html\">Next &raquo;</a>"; }
		previousPage = "<a href=\"" + indexFile + "2.html\">&laquo; Previous</a> ";
	}
	if (page == 4) {
		if (entryCount < 40 && entryCount > 30 ) { pageRange = "31-" + entryCount; } else { pageRange = "31-40"; }
		outputP4 = "<span class=\"active\">4</span>";
		if (entryCount > 40) { nextPage = " <a href=\"" + filePrefix + "5.html\">Next &raquo;</a>"; }
		previousPage = "<a href=\"" + indexFile + "3.html\">&laquo; Previous</a> ";
	}
	if (page == 5) {
		if (entryCount < 50 && entryCount > 40 ) { pageRange = "41-" + entryCount; } else { pageRange = "41-50"; }
		outputP5 = "<span class=\"active\">5</span>";
		if (entryCount > 50) { nextPage = " <a href=\"" + filePrefix + "6.html\">Next &raquo;</a>"; }
		previousPage = "<a href=\"" + indexFile + "4.html\">&laquo; Previous</a> ";
	}
	if (page == 6) {
		if (entryCount < 60  && entryCount > 50 ) { pageRange = "51-" + entryCount; } else { pageRange = "51-60"; }
		outputP6 = "<span class=\"active\">6</span>";
		previousPage = "<a href=\"" + indexFile + "5.html\">&laquo; Previous</a> ";
	}

	pageRange = pageRange + " of " + entryCount;

	var output = "<p><span class=\"pages\">" + outputP1 + " " + outputP2 + " " + outputP3 + " " + outputP4 + " " + outputP5 + " " + outputP6 + "</span> " + previousPage + pageRange + nextPage + "</p>";

	document.write(output);
}