
function emailCheck (emailStr) {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=0;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="[^\\s" + specialChars + "]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray===null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

// alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (var i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
// alert("Ths username contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
// alert("Ths domain name contains invalid characters.");
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)===null) {

// user is not valid

// alert("The username doesn't seem to be valid.");
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!==null) {

// this is an IP address

for (i=1;i<=4;i++) {
if (IPArray[i]>255) {
// alert("Destination IP address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
// alert("The domain name does not seem to be valid.");
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
// alert("The address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
// alert("This address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

var MD5 = function (string) {
 
	function RotateLeft(lValue, iShiftBits) {
		return (lValue<<iShiftBits) | (lValue>>>(32-iShiftBits));
	}
 
	function AddUnsigned(lX,lY) {
		var lX4,lY4,lX8,lY8,lResult;
		lX8 = (lX & 0x80000000);
		lY8 = (lY & 0x80000000);
		lX4 = (lX & 0x40000000);
		lY4 = (lY & 0x40000000);
		lResult = (lX & 0x3FFFFFFF)+(lY & 0x3FFFFFFF);
		if (lX4 & lY4) {
			return (lResult ^ 0x80000000 ^ lX8 ^ lY8);
		}
		if (lX4 | lY4) {
			if (lResult & 0x40000000) {
				return (lResult ^ 0xC0000000 ^ lX8 ^ lY8);
			} else {
				return (lResult ^ 0x40000000 ^ lX8 ^ lY8);
			}
		} else {
			return (lResult ^ lX8 ^ lY8);
		}
 	}
 
 	function F(x,y,z) { return (x & y) | ((~x) & z); }
 	function G(x,y,z) { return (x & z) | (y & (~z)); }
 	function H(x,y,z) { return (x ^ y ^ z); }
	function I(x,y,z) { return (y ^ (x | (~z))); }
 
	function FF(a,b,c,d,x,s,ac) {
		a = AddUnsigned(a, AddUnsigned(AddUnsigned(F(b, c, d), x), ac));
		return AddUnsigned(RotateLeft(a, s), b);
	}
 
	function GG(a,b,c,d,x,s,ac) {
		a = AddUnsigned(a, AddUnsigned(AddUnsigned(G(b, c, d), x), ac));
		return AddUnsigned(RotateLeft(a, s), b);
	}
 
	function HH(a,b,c,d,x,s,ac) {
		a = AddUnsigned(a, AddUnsigned(AddUnsigned(H(b, c, d), x), ac));
		return AddUnsigned(RotateLeft(a, s), b);
	}
 
	function II(a,b,c,d,x,s,ac) {
		a = AddUnsigned(a, AddUnsigned(AddUnsigned(I(b, c, d), x), ac));
		return AddUnsigned(RotateLeft(a, s), b);
	}
 
	function ConvertToWordArray(string) {
		var lWordCount;
		var lMessageLength = string.length;
		var lNumberOfWords_temp1=lMessageLength + 8;
		var lNumberOfWords_temp2=(lNumberOfWords_temp1-(lNumberOfWords_temp1 % 64))/64;
		var lNumberOfWords = (lNumberOfWords_temp2+1)*16;
		var lWordArray=Array(lNumberOfWords-1);
		var lBytePosition = 0;
		var lByteCount = 0;
		while ( lByteCount < lMessageLength ) {
			lWordCount = (lByteCount-(lByteCount % 4))/4;
			lBytePosition = (lByteCount % 4)*8;
			lWordArray[lWordCount] = (lWordArray[lWordCount] | (string.charCodeAt(lByteCount)<<lBytePosition));
			lByteCount++;
		}
		lWordCount = (lByteCount-(lByteCount % 4))/4;
		lBytePosition = (lByteCount % 4)*8;
		lWordArray[lWordCount] = lWordArray[lWordCount] | (0x80<<lBytePosition);
		lWordArray[lNumberOfWords-2] = lMessageLength<<3;
		lWordArray[lNumberOfWords-1] = lMessageLength>>>29;
		return lWordArray;
	}
 
	function WordToHex(lValue) {
		var WordToHexValue="",WordToHexValue_temp="",lByte,lCount;
		for (lCount = 0;lCount<=3;lCount++) {
			lByte = (lValue>>>(lCount*8)) & 255;
			WordToHexValue_temp = "0" + lByte.toString(16);
			WordToHexValue = WordToHexValue + WordToHexValue_temp.substr(WordToHexValue_temp.length-2,2);
		}
		return WordToHexValue;
	}
 
	function Utf8Encode(string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";
 
		for (var n = 0; n < string.length; n++) {
 
			var c = string.charCodeAt(n);
 
			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}
 
		}
 
		return utftext;
	}
 
	var x=Array();
	var k,AA,BB,CC,DD,a,b,c,d;
	var S11=7, S12=12, S13=17, S14=22;
	var S21=5, S22=9 , S23=14, S24=20;
	var S31=4, S32=11, S33=16, S34=23;
	var S41=6, S42=10, S43=15, S44=21;
 
	string = Utf8Encode(string);
 
	x = ConvertToWordArray(string);
 
	a = 0x67452301; b = 0xEFCDAB89; c = 0x98BADCFE; d = 0x10325476;
 
	for (k=0;k<x.length;k+=16) {
		AA=a; BB=b; CC=c; DD=d;
		a=FF(a,b,c,d,x[k+0], S11,0xD76AA478);
		d=FF(d,a,b,c,x[k+1], S12,0xE8C7B756);
		c=FF(c,d,a,b,x[k+2], S13,0x242070DB);
		b=FF(b,c,d,a,x[k+3], S14,0xC1BDCEEE);
		a=FF(a,b,c,d,x[k+4], S11,0xF57C0FAF);
		d=FF(d,a,b,c,x[k+5], S12,0x4787C62A);
		c=FF(c,d,a,b,x[k+6], S13,0xA8304613);
		b=FF(b,c,d,a,x[k+7], S14,0xFD469501);
		a=FF(a,b,c,d,x[k+8], S11,0x698098D8);
		d=FF(d,a,b,c,x[k+9], S12,0x8B44F7AF);
		c=FF(c,d,a,b,x[k+10],S13,0xFFFF5BB1);
		b=FF(b,c,d,a,x[k+11],S14,0x895CD7BE);
		a=FF(a,b,c,d,x[k+12],S11,0x6B901122);
		d=FF(d,a,b,c,x[k+13],S12,0xFD987193);
		c=FF(c,d,a,b,x[k+14],S13,0xA679438E);
		b=FF(b,c,d,a,x[k+15],S14,0x49B40821);
		a=GG(a,b,c,d,x[k+1], S21,0xF61E2562);
		d=GG(d,a,b,c,x[k+6], S22,0xC040B340);
		c=GG(c,d,a,b,x[k+11],S23,0x265E5A51);
		b=GG(b,c,d,a,x[k+0], S24,0xE9B6C7AA);
		a=GG(a,b,c,d,x[k+5], S21,0xD62F105D);
		d=GG(d,a,b,c,x[k+10],S22,0x2441453);
		c=GG(c,d,a,b,x[k+15],S23,0xD8A1E681);
		b=GG(b,c,d,a,x[k+4], S24,0xE7D3FBC8);
		a=GG(a,b,c,d,x[k+9], S21,0x21E1CDE6);
		d=GG(d,a,b,c,x[k+14],S22,0xC33707D6);
		c=GG(c,d,a,b,x[k+3], S23,0xF4D50D87);
		b=GG(b,c,d,a,x[k+8], S24,0x455A14ED);
		a=GG(a,b,c,d,x[k+13],S21,0xA9E3E905);
		d=GG(d,a,b,c,x[k+2], S22,0xFCEFA3F8);
		c=GG(c,d,a,b,x[k+7], S23,0x676F02D9);
		b=GG(b,c,d,a,x[k+12],S24,0x8D2A4C8A);
		a=HH(a,b,c,d,x[k+5], S31,0xFFFA3942);
		d=HH(d,a,b,c,x[k+8], S32,0x8771F681);
		c=HH(c,d,a,b,x[k+11],S33,0x6D9D6122);
		b=HH(b,c,d,a,x[k+14],S34,0xFDE5380C);
		a=HH(a,b,c,d,x[k+1], S31,0xA4BEEA44);
		d=HH(d,a,b,c,x[k+4], S32,0x4BDECFA9);
		c=HH(c,d,a,b,x[k+7], S33,0xF6BB4B60);
		b=HH(b,c,d,a,x[k+10],S34,0xBEBFBC70);
		a=HH(a,b,c,d,x[k+13],S31,0x289B7EC6);
		d=HH(d,a,b,c,x[k+0], S32,0xEAA127FA);
		c=HH(c,d,a,b,x[k+3], S33,0xD4EF3085);
		b=HH(b,c,d,a,x[k+6], S34,0x4881D05);
		a=HH(a,b,c,d,x[k+9], S31,0xD9D4D039);
		d=HH(d,a,b,c,x[k+12],S32,0xE6DB99E5);
		c=HH(c,d,a,b,x[k+15],S33,0x1FA27CF8);
		b=HH(b,c,d,a,x[k+2], S34,0xC4AC5665);
		a=II(a,b,c,d,x[k+0], S41,0xF4292244);
		d=II(d,a,b,c,x[k+7], S42,0x432AFF97);
		c=II(c,d,a,b,x[k+14],S43,0xAB9423A7);
		b=II(b,c,d,a,x[k+5], S44,0xFC93A039);
		a=II(a,b,c,d,x[k+12],S41,0x655B59C3);
		d=II(d,a,b,c,x[k+3], S42,0x8F0CCC92);
		c=II(c,d,a,b,x[k+10],S43,0xFFEFF47D);
		b=II(b,c,d,a,x[k+1], S44,0x85845DD1);
		a=II(a,b,c,d,x[k+8], S41,0x6FA87E4F);
		d=II(d,a,b,c,x[k+15],S42,0xFE2CE6E0);
		c=II(c,d,a,b,x[k+6], S43,0xA3014314);
		b=II(b,c,d,a,x[k+13],S44,0x4E0811A1);
		a=II(a,b,c,d,x[k+4], S41,0xF7537E82);
		d=II(d,a,b,c,x[k+11],S42,0xBD3AF235);
		c=II(c,d,a,b,x[k+2], S43,0x2AD7D2BB);
		b=II(b,c,d,a,x[k+9], S44,0xEB86D391);
		a=AddUnsigned(a,AA);
		b=AddUnsigned(b,BB);
		c=AddUnsigned(c,CC);
		d=AddUnsigned(d,DD);
	}
 
	var temp = WordToHex(a)+WordToHex(b)+WordToHex(c)+WordToHex(d);
 
	return temp.toLowerCase();
};

function xmlhttp_email_friend()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = OK
    }
  else
    {
    
    }
  }
}

function xmlhttp_pass_reset(){
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = OK
    	if (xmlhttp.responseText.match("confirm")){
			document.getElementById("prd_msg").style.backgroundColor = "#cfc";
			document.getElementById("prd_p_msg").innerHTML = xmlhttp.responseText;
			document.getElementById("prd_body").style.display = "none";
			document.getElementById("prd_close").style.display = "block";
			document.getElementById("prd_email_address").value = '';
    	} else {
    		document.getElementById("prd_msg").style.backgroundColor = "#fcc";
    		document.getElementById("prd_p_msg").innerHTML = xmlhttp.responseText;
    	}
		document.getElementById("prd_msg").style.display = "block";
    }
  else
    {
    
    }
  }
}

function move_box(){
	document.getElementById("email_friend_div").style.top = document.body.scrollTop +200+ "px";
	document.getElementById("create_account_div").style.top = document.body.scrollTop +200+ "px";
	document.getElementById("login_div").style.top = document.body.scrollTop +200+ "px";
	document.getElementById("pass_reset_div").style.top = document.body.scrollTop +200+ "px";
}


function send_email(){
	var post_id = document.getElementById("ef_post_id").value;
	var post_url = document.getElementById("ef_post_url").value;
	var from_email = document.getElementById("ef_from_email").value.replace(/[,\s]+/g, '');
	var to_emails = document.getElementById("ef_to_email").value.replace(/[,\s]+/g, ',');
	var message = document.getElementById("ef_message").value;
	var to_emails_array = [];
	to_emails_array = to_emails.split(",");
	to_emails = '';
	for (var i=0;i<to_emails_array.length;i++){
		if(emailCheck(to_emails_array[i])){
			to_emails += to_emails_array[i];
			if (i+1 < to_emails_array.length){
				to_emails += ',';
			}
		}
	}
	if(!emailCheck(from_email) || !to_emails){
		document.getElementById("efd_msg").style.backgroundColor = "#fcc";
		document.getElementById("efd_p_msg").innerHTML = "There we're errors in the form, please try again";
		document.getElementById("efd_msg").style.display = "block";
	} else {
		var url = '/includes/email_friend.php?' + "post_id=" + post_id + "&post_url=" + post_url + "&from_email=" + from_email + "&to_emails=" + to_emails + "&message=" + message; 
		loadXMLDoc(url, xmlhttp_email_friend);
		document.getElementById("efd_msg").style.backgroundColor = "#cfc";
		document.getElementById("efd_p_msg").innerHTML = "Your email has been sent.";
		document.getElementById("ef_post_id").value = '';
		document.getElementById("ef_post_url").value = '';
		document.getElementById("ef_from_email").value = '';
		document.getElementById("ef_to_email").value = '';
		document.getElementById("ef_message").value = '';
		document.getElementById("efd_body").style.display = "none";
		document.getElementById("efd_msg").style.display = "block";
		document.getElementById("efd_close").style.display = "block";
	}
}


function create_account(){
	var first_name = document.getElementById("ca_first_name").value;
	var last_name = document.getElementById("ca_last_name").value;
	var email_address = document.getElementById("ca_email_address").value;
	var password = document.getElementById("ca_password").value;
	var password_reenter = document.getElementById("ca_password_reenter").value;
	if(!first_name || !last_name || first_name == 'first name' || last_name == 'last name'){
		document.getElementById("cad_msg").style.backgroundColor = "#fcc";
		document.getElementById("cad_p_msg").innerHTML = "Please enter your first and last name.";
		document.getElementById("cad_msg").style.display = "block";	
	} else if(!emailCheck(email_address)){
		document.getElementById("cad_msg").style.backgroundColor = "#fcc";
		document.getElementById("cad_p_msg").innerHTML = "Please enter a valid email address.";
		document.getElementById("cad_msg").style.display = "block";
	} else if (password != password_reenter) {
		document.getElementById("cad_msg").style.backgroundColor = "#fcc";
		document.getElementById("cad_p_msg").innerHTML = "Your passwords didn't match, please try again.";
		document.getElementById("cad_msg").style.display = "block";
		document.getElementById("ca_password").value = '********';
		document.getElementById("ca_password_reenter").value = '********';
	} else if (password == '********' || !password || password_reenter == '********' || !password_reenter) {
		document.getElementById("cad_msg").style.backgroundColor = "#fcc";
		document.getElementById("cad_p_msg").innerHTML = "Please choose a valid password.";
		document.getElementById("cad_msg").style.display = "block";
		document.getElementById("ca_password").value = '********';
		document.getElementById("ca_password_reenter").value = '********';
	} else {
		password = MD5(password);
		objHTTP = xmlhttp_obj();
		objHTTP.open('POST',"/account/new",false);
		objHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		objHTTP.send("e="+escape(email_address)+"&p="+escape(password)+"&f="+escape(first_name)+"&l="+escape(last_name));
		strResult=objHTTP.responseText;
    	if (xmlhttp.responseText.match("confirm")){
			document.getElementById("cad_msg").style.backgroundColor = "#cfc";
			document.getElementById("cad_p_msg").innerHTML = strResult;
			document.getElementById("ca_first_name").value = 'first name';
			document.getElementById("ca_last_name").value = 'last name';
			document.getElementById("ca_email_address").value = '';
			document.getElementById("ca_password").value = '********';
			document.getElementById("ca_password_reenter").value = '********';
			document.getElementById("cad_body").style.display = "none";
			document.getElementById("cad_close").style.display = "block";
    	} else {
    		document.getElementById("cad_msg").style.backgroundColor = "#fcc";
    		document.getElementById("cad_p_msg").innerHTML = strResult;
    	}
		document.getElementById("cad_msg").style.display = "block";
	}
}

function pass_reset(){
	var email_address = document.getElementById("prd_email_address").value;
	if(emailCheck(email_address)) {
		var url = '/account/password-reset/' + email_address; 
		loadXMLDoc(url, xmlhttp_pass_reset);
	} else {
		document.getElementById("prd_msg").style.backgroundColor = "#fcc";
		document.getElementById("prd_p_msg").innerHTML = "Please enter a valid email address.";
		document.getElementById("prd_msg").style.display = "block";		
	}
}


function login_account(){
	var email_address = document.getElementById("ld_email_address").value;
	var password = document.getElementById("ld_password").value;
	if(emailCheck(email_address) && password) {
		password = MD5(password);
		var objHTTP, strResult;
		objHTTP = xmlhttp_obj();
		objHTTP.open('POST',"/account/login",false);
		objHTTP.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		objHTTP.send("e="+escape(email_address)+"&p="+escape(password));
		strResult=objHTTP.responseText;
		if(strResult.match(/log(.*)?in/i)){
			document.getElementById("ld_msg").style.backgroundColor = "#cfc";
			document.getElementById("ld_p_msg").innerHTML = strResult;
			document.getElementById("ld_msg").style.display = "block";
			window.location = document.getElementById("ld_page").value
		}else{
			document.getElementById("ld_msg").style.backgroundColor = "#fcc";
			document.getElementById("ld_p_msg").innerHTML = strResult;
			document.getElementById("ld_password").value = '';
			document.getElementById("ld_msg").style.display = "block";
		}
	} else if (!emailCheck(email_address)) {
		document.getElementById("ld_msg").style.backgroundColor = "#fcc";
		document.getElementById("ld_p_msg").innerHTML = "Please enter a valid email address.";
		document.getElementById("ld_msg").style.display = "block";		
	} else {
		document.getElementById("ld_msg").style.backgroundColor = "#fcc";
		document.getElementById("ld_p_msg").innerHTML = "Please enter a password.";
		document.getElementById("ld_msg").style.display = "block";	
	}
}

