/**
 * Copyright (C) 2002-2004, CodeHouse.com. All rights reserved.
 * CodeHouse(TM) is a registered trademark.
 *
 * THIS SOURCE CODE MAY NOT BE MODIFIED, DISTRIBUTED, OR EXECUTED
 * FROM OUTSIDE THE CONTEXT OF CODEHOUSE.COM
 *
 * Program Name: Email Obfuscator
 *
 * Written by: Ed Phillips (EID#2) of CodeHouse.com
 *
 * Created: 9/14/02
 *
 * Dependencies:
 *
 * ../js/uniConverter.js 
 */

EMAIL_REQ = 'You must first enter an e-mail address into the "Email Address" field'

EMAIL_BAD = 'The e-mail address you\'ve entered into the "Email Address" field is invalid'

A_END="</a>"

TMPL_A_START="<a href=\"javascript:location='mailto:__EMAIL__';void 0\">"

//Default Display (User didn't enter anything into the display field)
TMPL_DISP_DEF = TMPL_A_START +
"<script type=\"text/javascript\">document.write('__EMAIL__')<\/script>" + A_END

//User Defined Display
TMPL_DISP_UDEF = TMPL_A_START + "__UDEF__" + A_END

TMPL_CHILD_WIN =
'<html>\
<head><title>Link Tester</title></head>\
<body>\
<div style="text-align: center">\
__LINK__\
</div>\
</body>\
</html>'


function submitHndlr()
{
   if( ! window.eOut )
   {
      eOut = document.getElementById("output");
      eEmail = document.getElementById("email");
      eDisp = document.getElementById("display");
      eSelect = document.getElementById("select");
      eReset = document.getElementById("reset");
   }


   if( ! eEmail.value.length )
   {
      alert(EMAIL_REQ);
	  eEmail.focus();
   }
   else if( ! eEmail.value.match(/\S+@\S+\.\S/) ) //EID#2: This is very weak validation
   {
      alert(EMAIL_BAD);
      eEmail.focus();
   }
   else
   {
      eSelect.disabled = eReset.disabled = eOut.disabled = false;

      var outTmpl = eDisp.value.length && eDisp.value != eEmail.value
          ? TMPL_DISP_UDEF.replace(/__UDEF__/, eDisp.value)
          : TMPL_DISP_DEF;

      eOut.value = outTmpl.replace(/__EMAIL__/g, convertString2Unicode(eEmail.value));

      var child = window.open("", "", "width=300,height=100,resizable");
	    
	  child.document.write(TMPL_CHILD_WIN.replace(/__LINK__/, eOut.value));
						   
	  child.document.close();
	  	  
	  child.focus();	
   }

   return false;
}

function resetHndlr()
{
   eSelect.disabled = eReset.disabled = eOut.disabled = true;
   eEmail.value = eDisp.value = eOut.value = "";
}
