/*--------------------------------------------------------------------------------\
|                                 Security Code                                   |
|---------------------------------------------------------------------------------|
| Author:       Dan Sutton                                                        |
| Date:         January 1st, 2008                                                 |
| Version:      1.0.0                                                             |
| Contact:      dan@opwernby.com                                                  |
|---------------------------------------------------------------------------------|
| Call Syntax:  securityCode.generate(<code name>,<field name>);                  |
|               securityCode.generate();                                          |
| Example:      securityCode.generate("hiddencode","typedvalue");                 |
|---------------------------------------------------------------------------------|
| Dependencies: a.png .. z.png                                                    |
|---------------------------------------------------------------------------------|
| Generates a four-digit security code (alpha) composed of images of hand-drawn,  |
| upper-case letters, and asks that the user type that code into a field.         |
|---------------------------------------------------------------------------------|
| Identifiers reserved by this object:                                            |
|                                                                                 |
| VARIABLE/FUNCTION Identifiers                                                   |
|   securityCode                                                                  |
\--------------------------------------------------------------------------------*/

var securityCode=
{
	imgdir: "/scripts/jsCaptCha/images/security/",						// Directory containing Images
	letters: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",	// Characters to use to create code
	codeLength: 4,													// Length of code created
	codeName: 'sc_Code',										// Default Code Field name/id (hidden field)
	fieldName: 'sc_Typed',									// Defalt User Input Field name/id
	codeBack: "#C0C0C0",                    // Background color for Code area
	
	generate: function(codeName,fieldName)
	{
		if (codeName) this.codeName=codeName;
		if (fieldName) this.fieldName=fieldName;
		var c="";
		for (var i=0; i<this.codeLength; i++)
			c+=this.letters.charAt(Math.floor(Math.random()*this.letters.length));
		HTML="<table cellspacing=0 cellpadding=0 border=0>"
		 +"<tr><td><table cellspacing=0 border=0 cellpadding=1>"
		 +"<tr><td class='formTable' colspan=2>Please enter the code you see below:</td></tr>"
		 +"<tr><td class='formTable'>Security Code:</td>"
		 +"<td class='formTable'>&nbsp;"
		 +"<input type='text' maxlength="+this.codeLength
		 +" size="+(2+(this.codeLength-0))+" name='"+this.fieldName+"' id='"+this.fieldName+"'"
		 +" onkeyup='javascript:securityCode.onkeyup(this.value);'"
		 +" onchange='javascript:securityCode.onchange(this.value);'>"
		 +"</td></tr></table></td><td>"
		 +"<input type='hidden' id='"+this.codeName+"' name='"+this.codeName+"' value='"+c+"'>"
		 +"&nbsp;&nbsp;</td><td align='center'>"
		 +"<table cellspacing=0 cellpadding=0 border=0 style='border:1px solid #000000;' class='formSecurity'><tr>";
		for (var i=0; i<this.codeLength; i++)
			HTML+="<td"+(this.codeBack? " bgcolor='"+this.codeBack+"'" : "")
			 +" style='padding: 2px 2px 2px 2px;'><img height=34 src='"+this.imgdir+c.charAt(i)+".png' border=0></td>";
		HTML+="</tr></table></td></tr></table>";
		document.write(HTML);
	},
	
	correct: function(value)
	{
		var x=document.getElementById(securityCode.codeName);
		var y=document.getElementById(securityCode.fieldName);
		if (!value)
			value=y.value;
		try
		{
			return (x.value.toLowerCase()==y.value.toLowerCase());
		}
		catch(e)
		{
			return false;
		}
	},
	
	// Events which can be overridden
	onkeyup: function(value){},
	onchange: function(value){}
};