
var win;


function PopWin(pagename,winname,trgt)
{
	if (!win || win.closed)
	{
		win = window.open(pagename,winname,'width=500,height=500,scrollbars=yes,resizable=1');
		win.focus();

	} else {
		win.location.href=pagename;
		win.focus();
	}
}


function openMySelection()	{
	if (this.opener)	{
		opener.location.href="orderlist.asp";
		//opener.location.refresh();
		this.close();
	}	
}

function continueOrder()	{
	if (this.opener)	{
		opener.location.href="ordermailing.asp";
		this.close();
	}	
}


function checkEmail (strng) {
	var error="";
	if (strng == "") {
	   error = "You didn't enter an email address.\n";
	}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "Please enter a valid email address.\n";
    }
    else {
		//test email for illegal characters
		var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
        //"
		if (strng.match(illegalChars)) {
    		error = "The email address contains illegal characters.\n";
		}
    }
	return error;    
}

function checkLibrarianEmail(theForm)	{
	var email = theForm.email.value;
	var query = theForm.query.value;
	
	//1. check required fields
	if (email == "") 	{ alert("Please fill your email address."); theForm.email.focus(); 		return false; }
	if (email != "")	{
		if(checkEmail(email) != "") {
			alert (checkEmail(email))
			theForm.email.focus();
			return false;
		}
	}	
	if (query == "") 		{ alert("Please enter a query."); 		theForm.query.focus(); 	return false; }

	return true;	
}

function checkRegistrationWholeForm(theForm) {
	var pwd 			= theForm.pwd.value;
	var pwdconf 		= theForm.pwdconf.value;
	var email			= theForm.email.value;

	//1. check required fields
	if (email == "") 	{ alert("Please fill your email address."); theForm.email.focus(); 		return false; }
	if (email != "")	{
		if(checkEmail(email) != "") {
			alert (checkEmail(email))
			theForm.email.focus();
			return false;
		}
	}	
	if (pwd == "") 		{ alert("Please enter a password."); 		theForm.pwd.focus(); 	return false; }
	if (pwd.length < 4)	{ alert("Please enter a password of at least 4 characters."); 		theForm.pwd.focus(); 	return false; }
	if (pwd != pwdconf)	{ alert("Your password and confirmation do not match."); 		theForm.pwd.focus(); 	return false; }
	return true;
}

function changeDetails(theForm,popupwin)	{
	var pwd 			= theForm.pwd.value;
	var email			= theForm.email.value;
//	alert("theForm.email = " + email + ", pwd = " + pwd);

	if (email == "") 	{ alert("Please fill your email address."); theForm.email.focus(); return;}
	if (email != "")	{
		if(checkEmail(email) != "") {
			alert (checkEmail(email))
			theForm.email.focus();
			return;
		}
	}	
	if (pwd == "") 		{ alert("Please enter a password."); 		theForm.pwd.focus(); return;}

	if(popupwin)	{
		urlString = "register.asp?order=true&email=" + email + "&pwd=" + pwd;
		PopWin(urlString,'register','register')	
	} else {
		this.location.href = "register.asp?email=" + email + "&pwd=" + pwd;
	}
}

function setLoginState(loggedIn)	{
	
	var d = opener.document;
	var loginState;
	
	// get login link number	
	for (var i = 0; i < d.links.length; i++)	{
		
		if (d.links[i].name == "loginlink")	{
			loginState = false;
		}
		
		if (d.links[i].name == "logoutlink")	{
			loginState = true;
		}
		
	}
	
	if (loginState != loggedIn)	{
		opener.location.reload();
	}
}


function makeDialog()	{

	if (window.screen) {
    	var aw = screen.availWidth;
    	var ah = screen.availHeight;
	} else {
		var aw = 800;
		var ah = 600;
	}

	window.resizeTo (250,ah/100*80);
	window.moveTo(aw-275, 50);	

}

