var emailriddler={

defaultmsg: 'Press "Encrypt Email" to get corresponding code to paste to your page',
randomdigit: Math.floor(Math.random()*89)+10,

validateaddress:function(){
	var emailfilter=/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i
	return emailfilter.test(this.originalemail) //if valid email
},

displaypreview:function(){
	this.init()
	if (!this.validateaddress()){
		this.previewbox.innerHTML="<i>Incomplete email address...</i>"
		return this.catcherror()
	}
	this.randomdigit=Math.floor(Math.random()*89)+10
	var previewhtml=""
	if (this.titlebox.value=="##variable##")
	previewhtml='Variable <code style="font-size: 16px">'+this.emailtovar()+'</code> will contain the email address "'+this.originalemail+'"'
	else{
		previewhtml='<a href="mailto:'+this.emailbox.value
		previewhtml+=(this.subjectbox.value!="")? '?subject='+this.subjectbox.value+'">' : '">'
		previewhtml+=(this.titlebox.value=="##email##")? this.emailbox.value : this.titlebox.value=="##image##"? '<img src="letter.gif" border="0">' : this.titlebox.value
		previewhtml+='</a>'
	}
	this.previewbox.innerHTML=previewhtml
	if (this.titlebox.value.length>0){ //If email text isn't empty
		this.sourcebox.value=this.defaultmsg
		this.sourcebox.style.color="gray"
		this.previewbox.parentNode.style.borderColor="green"
	}
	else{
		this.previewbox.innerHTML='<i>Email Title is empty...<i>'
		return this.catcherror()
	}
},


catcherror:function(stage){
	this.sourcebox.value=''
	this.previewbox.parentNode.style.borderColor="red"
	if (stage=="encrypt"){
		this.emailbox.focus()
		this.emailbox.select()
		alert("Please input a valid email address!")
	}
	return false
},


generatecomment:function(){
var comment=this.originalemail.split("@")
comment='<!-- Encrypted version of: '+comment[0]+" [at] "+comment[1].replace(/[^\.]/g, "*")+' \/\/-->'
return comment
},


inputsign:function(what){
this.titlebox.value=what
},

encryptmail:encryptmail,

emailtovar:function(){ //generate a random variable as part of generated code based on original email
	var emailvariable=this.originalemail.split("@")[0] //Get first portion of email address
	emailvariable="encryptedemail_id"+this.randomdigit
	return emailvariable
},

outputcode:function(){
	var creditnotice='/***********************************************\n'
	creditnotice+='* Encrypt Email script- Please keep notice intact\n'
	creditnotice+='* Tool URL: http://www.dynamicdrive.com/emailriddler/\n'
	creditnotice+='* **********************************************/\n'
	creditnotice+=this.generatecomment()+'\n\n'

		var emailtext=(this.titlebox.value=="##email##")? '\'\+'+this.emailtovar()+'\+\'' : this.titlebox.value=="##image##" ? '<img src="yourimage.gif" alt="Email Us" />' : this.addslash(this.titlebox.value)

 var emailsubject=this.subjectbox.value
	emailsubject=(emailsubject!="")? "?subject="+this.addslash(emailsubject) : ""

	var outputstring='<script type="text/javascript">\n/*<![CDATA[*/\n\n' + creditnotice
	outputstring+="var emailriddlerarray="+this.encryptedmail+'\n'
	outputstring+="var "+this.emailtovar()+"='' //variable to contain encrypted email \n"
	outputstring+='for (var i=0; i<emailriddlerarray.length; i++)\n'
	outputstring+=" "+this.emailtovar()+'\+=String.fromCharCode(emailriddlerarray[i])\n\n'
	if (this.titlebox.value=="##variable##"){
		outputstring+='//--- VARIABLE "'+this.emailtovar()+'" NOW CONTAINS YOUR ENCRYPTED EMAIL. USE AS DESIRED. ---// \n\n'
	}
	else //tricky string below:
		outputstring+='document.write(\'<a href=\"mailto:\'\+'+this.emailtovar()+'\+\''+emailsubject+'\">'+emailtext+'</a>\')\n\n'
	outputstring+='/*]]>*/\n<\/script>'
	this.sourcebox.value=outputstring
},

addslash:function(str){ //adds slash to apostrophes
var str=str.replace(/'/g, "\\'")
str=str.replace(/"/g, "\\\"")
return str

},

sethooks:function(){
	document.getElementById("emailsign").onclick=function(){
	emailriddler.inputsign("##email##")
	emailriddler.displaypreview()
	return false
	}

	document.getElementById("imagesign").onclick=function(){
	emailriddler.inputsign("##image##")
	emailriddler.displaypreview()
	return false
	}

	document.getElementById("varsign").onclick=function(){
	emailriddler.inputsign("##variable##")
	emailriddler.displaypreview()
	return false
	}
},

resetit:function(button){
this.previewbox.innerHTML=''
button.form.reset()
this.displaypreview()
},


init:function(){
	this.emailbox=document.getElementById("rawemail")
	this.titlebox=document.getElementById("rawlinktext")
	this.subjectbox=document.getElementById("rawsubject")
	this.previewbox=document.getElementById("previewbox")
	this.sourcebox=document.getElementById("postemail")
	this.originalemail=this.emailbox.value
}

}

emailriddler.init()
emailriddler.sethooks()

window.onload=function(){
emailriddler.displaypreview()
}




