// Get the cookie value from it's name
function getName(cookie_name) {
	if(document.cookie)
	{
		index = document.cookie.indexOf(cookie_name);
		if (index != -1)
		{
			namestart = (document.cookie.indexOf("=", index) + 1);
			nameend = document.cookie.indexOf(";", index);
			if (nameend == -1) {nameend = document.cookie.length;}
			YouWrote = document.cookie.substring(namestart, nameend);
			return YouWrote;
		}
	}
	return '';
}

// Checks if there is a cookie, and if there is adds the details into the boxes
function CheckForCookie()
{
	var d=document.forms[0];
	var n = getName( 'name')   ;
	var e = getName( 'email')   ;

	if( n!=='' ){
		d.txtLogUserName.value = n;
	}
	if( e!=='' ){
		d.txtLogEmail1.value = e;
	}
	if( e!=='' && n!=='' )
		d.chkSave.checked=1;	

	

}
// Clicked the login button, so check appropriate fields
function login()
{
	var d=document.forms[0];
	var n=d.txtLogUserName.value;
	var e=d.txtLogEmail1.value;
	var e2=d.txtLogEmail12.value;
	var c=d.chkSave.checked;

	if (c ){ // save cookie
		//alert('Saving ' + c);
		//save cookie
		document.cookie='name='+ n +'; expires=Friday, 31-Dec-2010 08:00:00 GMT';
		document.cookie='email='+ e +'; expires=Friday, 31-Dec-2010 08:00:00 GMT';
	}

	if (e2=='') // Login normally
	{
		checklogin();
	}
	else //They gave an email to re send their username to
	{
		checkemail();
	}
}
// Check login fields, and submit if a.o.k
function checklogin() {
	var d=document.forms[0];
	var n=d.txtLogUserName.value;
	var e=d.txtLogEmail1.value;
	var c=d.chkSave.checked;

	if (c ){ // save cookie
		document.cookie='name='+ n +'; expires=Friday, 31-Dec-2010 08:00:00 GMT';
		document.cookie='email='+ e +'; expires=Friday, 31-Dec-2010 08:00:00 GMT';
	}
	
	if (e==''||n=='') {
		alert('You have not filled in all the required fields!');
		return;
	}
	if (NoOfChars(e,"@")!= 1||NoOfChars(e,".")<1) {
		alert('The e-mail address not appear to be valid.  Please check and retry!');
		return;
		}	
	d.submit();
}
// Check email field and email/resend details
function checkemail() {
	var d=document.forms[0];
	var e=d.txtLogEmail12.value;
	if (e=='') {
		alert('You have not filled in all the required fields!');
		return;
	}
	if (NoOfChars(e,"@")!= 1||NoOfChars(e,".")<1) {
		alert('We can not resend to that email address because the e-mail address not appear to be valid.  Please check and retry!');
		return;
		}
	window.location.href='memb-login-emailsend.asp?address='+e;
}
function NoOfChars(srcString,LookFor) {
	var TempLoop;
	var TotalChars=0;
	var TotalLoop=srcString.length;
	for (TempLoop=0;TempLoop<TotalLoop;TempLoop++) {
		if (srcString.substr(TempLoop,1)==LookFor)
			TotalChars++;
		}
	return TotalChars;
}
