/**
 * Form class scripts
 *
 * @package     UTPC Form
 * @author      Real Deal Marketing, www.realdealmarketing.net
 * @version     4.0
 * @copyright   All rights reserved
 *
 */

var ft = {
	version: '0.5beta',

	ref: function (id)
	{
		return document.getElementById(id);
	},

	style: function (id)
	{
		return document.getElementById(id).style;
	},

	css: function (id, element, value)
	{
		return document.getElementById(id).style[element] = value;
	},

	html: function (id, text)
	{
		document.getElementById(id).innerHTML = text;
	},

	toggle: function (id)
	{
		s = this.style(id);
		s.display = (s.display == 'none') ? 'block' : 'none';
	},

	show: function (id)
	{
		this.style(id).display = 'block';
	},

	hide: function (id)
	{
		this.style(id).display = 'none';
	},

	form: function (form_name)
	{
		return document.forms[form_name];
	},

	datetime: {

		month_names: new Array('', 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'),
		month_days: new Array(0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31),
		day_names: new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'),

		set: function (form, field, ds)
		{
			f = document.forms[form];

			if (ds == '')
			{
				f.elements[field+'_y'].selectedIndex = 0;
				f.elements[field+'_m'].selectedIndex = 0;
				f.elements[field+'_d'].selectedIndex = 0;
			}
			else
			{
				a = ds.split ('-');
				fy = f.elements[field+'_y'].options[0].value == 0 ? f.elements[field+'_y'].options[1].value : f.elements[field+'_y'].options[0].value;
				dy = f.elements[field+'_y'].options[0].value == 0 ? 1 : 0;
				f.elements[field+'_y'].selectedIndex = parseInt(a[0])-parseInt(fy)+dy;
				f.elements[field+'_m'].selectedIndex = f.elements[field+'_m'].options[0].value == 0 ? parseInt(a[1]) : parseInt(a[1]-1);
				f.elements[field+'_d'].selectedIndex = f.elements[field+'_d'].options[0].value == 0 ? parseInt(a[2]) : parseInt(a[2]-1);
			}
		},

		showPicker: function (e, id, starty_y, end_y)
		{
			ft.css(id+'_datePicker', 'left', e.clientX+'px');
			ft.css(id+'_datePicker', 'z-index', '100');
			ft.toggle(id+'_datePicker');

			var d = ft.ref(id).value.split("-");
			var selDate = new Date(parseInt(d[0]), parseInt(d[1])-1, parseInt(d[2]));
			this.calendar(selDate, id, starty_y, end_y);
		},

		isLeapYear: function (y)
		{
			return (y % 4==0 && y % 100 != 0) || y % 400 == 0;
		},

		calendar: function (selDate, id, starty_y, end_y)
		{
			var yearVal = selDate.getFullYear();
			var monthVal = selDate.getMonth()+1;

			if (this.isLeapYear(yearVal))
			{
				this.month_days[2] = 29;
			}
			var totalDays	= this.month_days[monthVal];
			var firstDay	= new Date(yearVal,selDate.getMonth(),1);
			var monthStart	= firstDay.getDay()-1;

			if (monthStart < 0)
			{
				monthStart = 6;
			}

			s = '<select name="mon" onchange="ft.datetime.changeMonth(\''+id+'\', '+yearVal+', this.value, '+starty_y+', '+end_y+');" >';
			for (i = 1; i <= 12; i++)
			{
				s += '<option value="'+i+'"';
				if (monthVal == i)	{ s += ' selected'; }
				s += '>'+this.month_names[i]+'</option>';
			}
			s += '</select>';
			s += '<select name="year" onchange="ft.datetime.changeMonth(\''+id+'\', this.value, '+monthVal+', '+starty_y+', '+end_y+');" >';
			for (i = end_y; i >= starty_y; i--)
			{
				s += '<option value="'+i+'"';
				if (yearVal == i)	{ s += ' selected'; }
				s += '>'+i+'</option>';
			}
			s += '</select>';


			s += '<table width="100%" cellpadding="2" cellspacing="1" border="0">';
			s += '<tr>';
			for (i=1; i <= 7; i++)
			{
				s += '<th';
				if (i == 7)	{ s += ' class="weekend"'; }
				s += '>'+this.day_names[i]+'</td>';
			}
			s += '</tr>';
			if (monthStart < 7)
			{
				s += '<tr>';
				for (i=0; i < monthStart; i++)
				{
					s += '<td class="empty">&nbsp;</td>';
				}
		//		s += '</tr>';
			}
			if (monthVal < 10)	{ monthVal = '0'+monthVal; }
			for (i=1; i <= totalDays; i++)
			{
				dayVal = i < 10 ? '0'+i : i;
				if ((i+monthStart) % 7 == 1)	{ s += '<tr>'; }
				s += '<td onclick="ft.datetime.setNewDate(\''+id+'\', \''+yearVal+'-'+monthVal+'-'+dayVal+'\');">'+i+'</td>';
				if ((i+monthStart) % 7 == 0)	{ s += '</tr>'; }
			}
			s += '</table>';

			ft.ref(id+'_datePickerCal').innerHTML = s;
		},

		changeMonth: function (id, year, month, starty_y, end_y)
		{
			this.calendar(new Date(year, month-1, 1), id, starty_y, end_y);
		},

		setNewDate: function (id, d)
		{
			ft.ref(id).value = d;
			this.hidePicker(id);
		},

		hidePicker: function (id)
		{
			ft.css(id+'_datePicker', 'z-index', '1');
			ft.hide(id+'_datePicker');
		},

		init: function ()
		{
		}

	},

	field: {
		step: 5,

		enlarge: function (form, field)
		{
			document.forms[form].elements[field].rows += this.step;
		},

		reduce: function (form, field)
		{
			if (document.forms[form].elements[field].rows > this.step)
			{
				document.forms[form].elements[field].rows -= this.step;
			}
		}
	},

	html: {
		userSelF: 	'',
		userSelA: 	'http://',
		userSelCLR: 'red',
		userSelE: 	'username@domain.com',
		userSelIMG: '.jpg',

		getKey: function (e)
		{
			if (window.event)	{ return window.event.keyCode; }
			else if (e)			{ return e.which; }
			else 				{ return null; }
		},

		keyPress: function (e, id)
		{
			k = null;

			if (window.event)	{ k = window.event.keyCode;	a = window.event.altKey;	c = window.event.ctrlKey;	s = window.event.shiftKey;}
			else if (e) 		{ k = e.which;				a = e.altKey;			 	c = e.ctrlKey;				s = e.shiftKey; }
			else 				{ k = null; }

			if (k != null)
			{
				if (k == 49 && a) 		{ this.setTag(id, 'H1'); }	/* alt+1 */
				else if (k == 50 && a)	{ this.setTag(id, 'H2'); }	/* alt+2 */
				else if (k == 51 && a)	{ this.setTag(id, 'H3'); }	/* alt+3 */
				else if (k == 117 && a)	{ this.setTag(id, 'UL'); }	/* alt+U */
				else if (k == 111 && a)	{ this.setTag(id, 'OL'); }	/* alt+O */
				else if (k == 105 && a)	{ this.setTag(id, 'LI'); } 	/* alt+i */
				else if (k == 66 && a)	{ this.setTag(id, 'B'); }	/* alt+B */
				else if (k == 73 && a)	{ this.setTag(id, 'I'); }	/* alt+Shift+I */
				else if (k == 68 && c)	{ this.setTag(id, 'DIV'); } /* Ctrl+D */
				else if (k == 80 && c)	{ this.setTag(id, 'P'); }	/* Ctrl+P */
				else if (k == 66 && c)	{ this.setTag(id, 'B'); }	/* Ctrl+Shift+B */
				else if (k == 13 && s)	{ this.setTag(id, 'BR'); } 	/* Shift+Enter */
			}

			this.generatePreview(id);
		},

		keyDown: function (e, id)
		{
/*
			k = this.getKey (e);
			if (k == 9)	{ this.setTag(id, "TAB") };
*/
		},

		setTag: function (id, what, preview)
		{
			this.atCursor(ft.ref(id), what);
/*			if (preview)
			{
				this.generatePreview(id);
			}
*/
			setTimeout('document.getElementById("'+id+'").focus();',0);
		},

		atCursor: function (field, what)
		{
			field.focus();

			if 		(what == 'B')		{ start_tag = '<strong>'; end_tag = '</strong>'; }
			else if (what == 'I')		{ start_tag = '<i>'; end_tag = '</i>'; }
			else if (what == 'U')		{ start_tag = '<u>'; end_tag = '</u>'; }
			else if (what == 'L')		{ start_tag = '<div align="left">'; 	end_tag = '</div>'; }
			else if (what == 'C')		{ start_tag = '<div align="center">';	end_tag = '</div>'; }
			else if (what == 'R')		{ start_tag = '<div align="right">';	end_tag = '</div>'; }
			else if (what == 'FL')		{ start_tag = '<span style="font-size:larger;">'; end_tag = '</span>'; }
			else if (what == 'FS')		{ start_tag = '<span style="font-size:smaller;">'; end_tag = '</span>'; }
			else if (what == 'FS8')		{ start_tag = '<span style="font-size:8pt;">'; end_tag = '</span>'; }
			else if (what == 'FS9')		{ start_tag = '<span style="font-size:9pt;">'; end_tag = '</span>'; }
			else if (what == 'FS10')	{ start_tag = '<span style="font-size:10pt;">'; end_tag = '</span>'; }
			else if (what == 'FS11')	{ start_tag = '<span style="font-size:11pt;">'; end_tag = '</span>'; }
			else if (what == 'FS12')	{ start_tag = '<span style="font-size:12pt;">'; end_tag = '</span>'; }
			else if (what == 'FS13')	{ start_tag = '<span style="font-size:13pt;">'; end_tag = '</span>'; }
			else if (what == 'FS14')	{ start_tag = '<span style="font-size:14pt;">'; end_tag = '</span>'; }
			else if (what == 'FS15')	{ start_tag = '<span style="font-size:15pt;">'; end_tag = '</span>'; }
			else if (what == 'FS16')	{ start_tag = '<span style="font-size:16pt;">'; end_tag = '</span>'; }
			else if (what == 'FS17')	{ start_tag = '<span style="font-size:17pt;">'; end_tag = '</span>'; }
			else if (what == 'FS18')	{ start_tag = '<span style="font-size:18pt;">'; end_tag = '</span>'; }
			else if (what == 'FS24')	{ start_tag = '<span style="font-size:24pt;">'; end_tag = '</span>'; }
			else if (what == 'F1')		{ start_tag = '<span style="font-family:Arial;">'; end_tag = '</span>'; }
			else if (what == 'F2')		{ start_tag = '<span style="font-family:\'Times New Roman\';">';	end_tag = '</span>'; }
			else if (what == 'F3')		{ start_tag = '<span style="font-family:Verdana;">'; end_tag = '</span>'; }
			else if (what == 'F4')		{ start_tag = '<span style="font-family:Tahoma;">'; end_tag = '</span>'; }
			else if (what == 'F5')		{ start_tag = '<span style="font-family:\'Courier New\';">'; end_tag = '</span>'; }
			else if (what == 'F6')		{ start_tag = '<span style="font-family:\'Trebuchet MS\';">'; end_tag = '</span>'; }
			else if (what == 'P')		{ start_tag = '<p>'; end_tag = '</p>'; }
			else if (what == 'H1')		{ start_tag = '<h1>'; end_tag = '</h1>'; }
			else if (what == 'H2')		{ start_tag = '<h2>'; end_tag = '</h2>'; }
			else if (what == 'H3')		{ start_tag = '<h3>'; end_tag = '</h3>'; }
			else if (what == 'UL')		{ start_tag = "<ul>\n\t<li>"; end_tag = "</li>\n</ul>"; }
			else if (what == 'OL')		{ start_tag = "<ol>\n\t<li>"; end_tag = "</li>\n</ol>"; }
			else if (what == 'LI')		{ start_tag = '<li>'; end_tag = '</li>'; }
			else if (what == 'DIV')		{ start_tag = '<div>'; end_tag = '</div>'; }
			else if (what == 'indent')	{ start_tag = '<blockquote>';	end_tag = '</blockquote>'; }
			else if (what == 'BR')		{ start_tag = ''; end_tag = "<br />\n"; }
			else if (what == 'TAB')		{ start_tag = '\t'; end_tag = ''; }
			else if (what == 'F')
			{
				this.userSelF = window.prompt('Please enter font name', this.userSelF);
				if (this.userSelF == null) { this.userSelF = ''; }
				start_tag = '<span style="font-family:'+this.userSelF+'">';
				end_tag = '</span>';
			}
			else if (what == 'A')
			{
				this.userSelA = window.prompt('Please enter url', this.userSelA);
				if (this.userSelA == null)
				{
					this.userSelA = '';
				}
				start_tag = '<a href="'+this.userSelA+'" ';
				this.userSelATarget = window.prompt('Target window', '');
				if (this.userSelATarget != null)
				{
					start_tag += ' target="'+this.userSelATarget+'" ';
				}
				this.userSelATitle = window.prompt('Link title', '');
				if (this.userSelATitle != null)
				{
					start_tag += ' title="'+this.userSelATitle+'" ';
				}
				start_tag += '>';
				end_tag = '</a>';
			}
			else if (what == 'CLR')
			{
				this.userSelCLR = window.prompt('Please color code or name', this.userSelCLR);
				if (this.userSelCLR == null)
				{
					this.userSelCLR = '';
				}
				start_tag = '<span style="color:'+this.userSelCLR+'">';
				end_tag = '</span>';
			}
			else if (what == '@')
			{
				this.userSelE = window.prompt('Please enter email address', this.userSelE);
				if (this.userSelE == null)
				{
					this.userSelE = '';
				}
				start_tag = '<a href="mailto:'+this.userSelE+'">';
				end_tag = '</a>';
			}
			else if (what == 'IMG')
			{
				this.userSelIMG = window.prompt('Please enter image location and filename', this.userSelIMG);
				if (this.userSelIMG == null)
				{
					this.userSelIMG = '';
				}
				alt = window.prompt('Please enter ALT text', 'description');
				if (alt == null)
				{
					alt = '';
				}
				start_tag = '<img src="'+this.userSelIMG+'" border=0 alt="'+alt+'">';
				end_tag = '';
			}
			else
			{
				start_tag = what;
				end_tag = '';
			}

			if (start_tag == '' && end_tag == '')
			{
				return false;
			}
			if (document.selection)
			{
				sel = document.selection.createRange();
				sel.text = start_tag + sel.text + end_tag;
			}
			else {
				var startPos = field.selectionStart;
				var endPos = field.selectionEnd;
				field.value = field.value.substring(0, startPos) + start_tag + field.value.substring(startPos, endPos) + end_tag + field.value.substring(endPos, field.value.length);
				if (what == 'BR' || what == '13')
				{
					field.selectionStart = endPos + start_tag.length + end_tag.length; field.selectionEnd = endPos + start_tag.length + end_tag.length;
				}
				else
				{
					field.selectionStart = endPos + start_tag.length;
					field.selectionEnd = endPos + start_tag.length;
				}
			}

			return false;
		},

		generatePreview: function (id)
		{
			pc = ft.ref(id+'PreviewContainer');
			if (pc.style.display == 'block') {
				ft.ref(id+'Preview').innerHTML = ft.ref(id).value;
			}
		},

		preview: function(id)
		{
			pc = ft.ref(id+'PreviewContainer');

			if (pc.style.display == 'block') {
				ft.ref(id+'TextContainer').className = 'inputContainerFull';
				ft.hide(id+'PreviewContainer');
				ft.ref('toggle'+id+'preview').innerHTML = 'show preview &raquo; ';
			} else {
				ft.ref(id+'TextContainer').className = 'inputContainer';
				ft.show(id+'PreviewContainer');
				ft.ref('toggle'+id+'preview').innerHTML = '&laquo; hide preview';
			}
			ft.ref(id).focus();
		},

		init: function ()
		{
		}
	},

	bbcode: {

		userSelF: 	'',
		userSelA: 	'http://',
		userSelCLR: 'red',
		userSelE: 	'username@domain.com',
		userSelIMG: '.jpg',

		setTag: function (id, what, preview)
		{
			el = ft.ref(id);
			this.atCursor(el, what);
			if (preview)
			{
				this.generatePreview(id);
			}
			setTimeout('document.getElementById("'+id+'").focus();',0);
		},

		atCursor: function (field, what)
		{
			field.focus();

			if 		(what == 'B')		{ start_tag = '[b]'; 		end_tag = '[/b]'; }
			else if (what == 'I')		{ start_tag = '[i]'; 		end_tag = '[/i]'; }
			else if (what == 'U')		{ start_tag = '[u]'; 		end_tag = '[/u]'; }
			else if (what == 'L')		{ start_tag = '[left]'; 	end_tag = '[/left]'; }
			else if (what == 'C')		{ start_tag = '[center]';	end_tag = '[/center]'; }
			else if (what == 'R')		{ start_tag = '[right]';	end_tag = '[/right]'; }
			else if (what == 'indent')	{ start_tag = '[indent]';	end_tag = '[/indent]'; }
			else if (what == 'FL')		{ start_tag = '[large]'; 	end_tag = '[/large]'; }
			else if (what == 'FS')		{ start_tag = '[small]'; 	end_tag = '[/small]'; }
			else if (what == 'TAB')		{ start_tag = '\t'; 		end_tag = ''; }
			else if (what == 'quot')	{ start_tag = '[quote]'; 	end_tag = '[/quote]'; }
			else if (what == 'CLR')
			{
				this.userSelCLR = window.prompt('Please color code or name', this.userSelCLR);
				if (this.userSelCLR == null)
				{
					this.userSelCLR = '';
				}
				start_tag = '[color='+this.userSelCLR+']';
				end_tag = '[/color]';
			}
			else if (what == 'A')
			{
				this.userSelA = window.prompt('Please enter url', this.userSelA);
				if (this.userSelA == null)
				{
					this.userSelA = '';
				}
				start_tag = '[url='+this.userSelA+']';
				this.userSelATitle = window.prompt('Link title', '');
				if (this.userSelATitle != null)
				{
					start_tag += this.userSelATitle;
				}
				end_tag = '[/url]';
			}
			else if (what == 'IMG')
			{
				this.userSelIMG = window.prompt('Please enter image location and filename', this.userSelIMG);
				if (this.userSelIMG == null)
				{
					this.userSelIMG = '';
				}
				start_tag = '[img]'+this.userSelIMG+'[/img]';
				end_tag = ' ';
			}

			if (document.selection)
			{
				sel = document.selection.createRange();
				sel.text = start_tag + sel.text + end_tag;
			}
			else
			{
				var startPos = field.selectionStart;
				var endPos = field.selectionEnd;
				field.value = field.value.substring(0, startPos) + start_tag + field.value.substring(startPos, endPos) + end_tag + field.value.substring(endPos, field.value.length);
				if (what == 'BR' || what == '13')
				{
					field.selectionStart = endPos + start_tag.length + end_tag.length; field.selectionEnd = endPos + start_tag.length + end_tag.length;
				}
				else
				{
					field.selectionStart = endPos + start_tag.length; field.selectionEnd = endPos + start_tag.length;
				}
			}

			return false;
		},

		preview: function(id)
		{
			pc = ft.ref(id+'PreviewContainer');

			if (pc.style.display == 'block') {
				ft.ref(id+'TextContainer').className = 'inputContainerFull';
				ft.hide(id+'PreviewContainer');
				ft.ref('toggle'+id+'preview').innerHTML = 'show preview &raquo; ';
			} else {
				ft.ref(id+'TextContainer').className = 'inputContainer';
				ft.show(id+'PreviewContainer');
				ft.ref('toggle'+id+'preview').innerHTML = '&laquo; hide preview';
			}
			ft.ref(id).focus();
		},

		generatePreview: function(id)
		{
			t = ft.ref(id).value;

			t = t.replace (/\<(.*)?>(.*)?<\/(.*)?>/ig, '$2');
			t = t.replace (/\<(.*)>/ig, '$1');

			find = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','\\n');
			repl = new Array('<b>','</b>','<i>','</i>','<u>','</u>','<br />');
			for (i = 0; i < find.length; i++)
			{
				while (t.indexOf (find[i]) != -1)
				{
					t = t.replace (find[i], repl[i]);
				}
			}
			t = t.replace (/\[quote\]([^\[]+)\[\/quote\]/ig, 				'<fieldset class="fquote"><legend>quote:</legend>$1</fieldset>');
			t = t.replace (/\[quote[\=]?(.+)?\]([^\[]+)\[\/quote\]/ig,		'<fieldset class="fquote"><legend>quote $1:</legend>$2</fieldset>');
			t = t.replace (/\[indent\]([^\[]+)\[\/indent\]/ig, 				'<blockquote>$1</blockquote>');
			t = t.replace (/\[left\]([^\[]+)\[\/left\]/ig, 					'<p align="left">$1</p>');
			t = t.replace (/\[center\]([^\[]+)\[\/center\]/ig, 				'<p align="center">$1</p>');
			t = t.replace (/\[right\]([^\[]+)\[\/right\]/ig, 				'<p align="right">$1</p>');
			t = t.replace (/\[large\]([^\[]+)\[\/large\]/ig, 				'<span style="font-size:large\;">$1</span>');
			t = t.replace (/\[small\]([^\[]+)\[\/small\]/ig, 				'<span style="font-size:smaller\;">$1</span>');
			t = t.replace (/\[img\]([^\]]+)\[\/img\]/ig, 					'<img src="$1" />');
			t = t.replace (/\[url\=http:\/\/([^\]]+)\]([^\[]+)\[\/url\]/gi,	'<a href="http://$1">$2</a>');
			t = t.replace (/\[color\=([^\]]*)\]([^\[]*)\[\/color\]/ig,		'<span style="color:$1;">$2</span>');

			ft.ref(id+'Preview').innerHTML = t;
		},

		keyPress: function (e, id)
		{
			k = null;
			if (window.event) 	{ k = window.event.keyCode;	a = window.event.altKey;	c = window.event.ctrlKey;	s = window.event.shiftKey;}
			else if (e) 		{ k = e.which;				a = e.altKey;				c = e.ctrlKey; 				s = e.shiftKey; }
			else 				{ k = null; }

			if (k != null)
			{
				if 		(k == 66 && a) 	{ this.setTag(id, "B"); }
				else if (k == 73 && a)	{ this.setTag(id, "I"); }
				else if (k == 66 && c)	{ this.setTag(id, "B"); }
			}
			this.preview(id);
			return false;
		},

		init: function()
		{
		}

	},

	limited: {
		limitInput: function (id, chars)
		{
			var l = ft.ref(id).value.length;
			var v = ft.ref(id).value;
			if (l >= chars)
			{
				ft.ref(id).value = ft.ref(id).value.substr(0, chars);
			}
			ft.ref(id+'_limit').innerHTML = ft.ref(id).value.length + " / " + chars;
		}
	},

	attach: function (id, type, callback)
	{
		elem = this.ref(id);
		try {
			if (elem.addEventListener)
			{
				elem.addEventListener(type, callback, false);
			}
			else if (elem.attachEvent)
			{
				elem.attachEvent("on" + type, callback);
			}
		} catch(e) { alert(e); }
	},

	init: function ()
	{
	}

};

