var an = navigator.appName.toUpperCase();
var av = navigator.appVersion.toUpperCase();
var ua = navigator.userAgent.toUpperCase();

var ns = (an.indexOf('NETS') != -1) ? true : false; // NS
var mac = (av.indexOf('MAC') != -1) ? true : false; // Mac
var webtv = (ua.indexOf("WEBTV") != -1) ? true : false; // WebTV

function popup(page) {
	var tmp = window.open(page,"terms","width=640,height=400,scrollbars=yes,resize=yes");
}

function loginPopup() {
	if (mac || webtv) { // mac/webtv fix
		location.href = "/signin.jsp";
		return;
	}
	var winDef = "left=" + (screen.width/2-200) + ",top=" + (screen.height/2-200) + ",width=350,height=100,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no";
	var loginWin = window.open("","loginpop",winDef);
	with (loginWin.document) {
		open();
		writeln('<html>');
		writeln('<head>');
		writeln('	<title>Login</title>');
		writeln('	<script language="javascript">');
		writeln('	<!--');
		writeln('	function hasError(a,b,c) {');
		writeln('		alert("There was an unexpected error.\\nIf problem persists, contact support.");');
		writeln('	}');
		writeln('	window.onerror=hasError;');
		writeln('	function login(form) {');
		writeln('		if (!window.opener.closed) {');
		writeln('			window.opener.doLogin(form.username.value, form.password.value);');
		writeln('			self.close();');
		writeln('		} else {');
		writeln('			alert("There was an unexpected error.\\nIf problem persists, please contact support.");');
		writeln('			self.close();');
		writeln('		}');
		writeln('	}');
		writeln('	function openPage(url) {');
		writeln('		if (!window.opener.closed) {');
		writeln('			window.opener.location.href=url;');
		writeln('			self.close();');
		writeln('		} else {');
		writeln('			alert("There was an unexpected error.\\nIf problem persists, contact support.");');
		writeln('			self.close();');
		writeln('		}');
		writeln('	}');
		writeln('	//-->');
		writeln('	</script>');
		writeln('	<style type="text/css">');
		writeln('	<!--');
		writeln('	.logintext { color: #333333; margin-left: 3px; font-size: 11px; font-family: arial,helvetica,sans-serif; }');
		writeln('	.loginfield { color: #000000; width: 100px; font-size: 10px; font-family: arial,helvetica,sans-serif; }');
		writeln('	.loginbutton { color: #FFFFFF; font-size: 12px; font-weight: bold; font-family: arial,helvetica,sans-serif; }');
		writeln('	//-->');
		writeln('	</style>');
		writeln('</head>');
		writeln('<body onload="document.loginForm.username.focus();"><form name="loginForm" action="" onsubmit="login(this);return false;"><center>');
		writeln('<table width="100%" border="0" cellpadding="0" cellspacing="0">');
		writeln('	<tr>');
		writeln('		<td>');		
		writeln('		<table cellpadding="0" cellspacing="4" border="0">');
		writeln('			<tr>');
		writeln('				<td><input class="loginfield" type="text" name="username" size="12"></td>');
		writeln('				<td><input class="loginfield" type="password" name="password" size="12"></td>');
		writeln('				<td><input class="loginbutton" type="image" src="/images/login.gif" value="log in" border="0" alt="log in"></td>');
		writeln('			</tr>');
		writeln('			<tr valign="top">');
		writeln('				<td><div class="logintext">Username</div></td>');
		writeln('				<td><div class="logintext">Password</div></td>');
		writeln('				<td nowrap><a href="#" onclick=\'openPage("/lostpassword.jsp"); return false;\'><div class="logintext"><u>Lost password?</u></div></a></td>');
		writeln('			</tr>');
		writeln('		</table>');
		writeln('		</td>');
		writeln('	</tr>');
		writeln('</table>');
		writeln('</center></form></body></html>');
		close();
	}
}

function searchPopup() {		
	var winDef = "left=" + (screen.width/2-200) + ",top=" + (screen.height/2-200) + ",width=475,height=150,toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes";
	var searchWin = window.open("","searchpop",winDef);
	with (searchWin.document) {
		open();   
		writeln('<html><head>');
		writeln('	<title>Advanced Search</title>');
		writeln('	<style type="text/css"><!--');
		writeln('	div { color: #333333; font-size: 11px; font-family: arial,helvetica,sans-serif; }');
		writeln('	.big { color: FF7700; font-size: 18px; font-family: arial,helvetica,sans-serif; }');
		writeln('	.big2 { color: FF7700; font-size: 24px; font-family: arial,helvetica,sans-serif; }');
		writeln('	.o { color: #FF7700; font-size: 14px; }');
		writeln('	//--></style>');
		writeln('</head>');
		writeln('<body onload="self.focus()"><div align="center">');
		writeln('<b>Advanced Search</b><br>');

		writeln('Add <span class="big">+</span> before terms that <b>must</b> occur in the results.<br>');
		writeln('Add <span class="big2">-</span> before terms that <b>must not</b> occur in the results.<br>');

		writeln('Example:<span class="o">+ </span><b>paco</b> <span class="o">- </span><b>lucia</b>, will match:<br> <i>Paco Pena</i>, <i>Paco del Gastor</i>, but <b>not</b> <i>Paco de Lucia</i><br><br>');

		writeln('<a href="#" onclick="self.close();">close</a>');
		writeln('</div></body></html>');
		close();
	}
}

function doLogin(user,pass) {
	document.login.username.value=user;
	document.login.password.value=pass;
	document.login.submit();
}

function validateSearchTerms(formObject) {
	var terms = formObject.q.value.split(" ");
	var n_terms = "";
	for (var n = 0; n < terms.length; n++)
		if (terms[n].length > 3) n_terms += terms[n] + " ";

	document.forms[formObject.name].q.value = n_terms;
}

function escapeChars(val) {
	if (!this.toString())
		return "";
	return this.toString().replace(eval("/" + val + "/g"),"\\" + val);
}

function setFocus(inputField) {
  inputField.focus();
}

function limitTextarea( textField, count, max )
{
  if ( textField.value.length > max )
    textField.value = textField.value.substring(0, max);
  else 
    count.value = max - textField.value.length;
}


Object.prototype.escapeChars = escapeChars;
