var Contact = {
    
	init: function(options){
		//added htmlWidth and htmlHeight which are the default sizes for html content blocks
		this.options = Object.extend({
		    formID: 'formmail',
			recipientID: 'recipient',
			artistID: 'artist',
			titleID: 'artTitle',
			subjectID: 'subject',
			pageRefID: 'pageRef',			
			reqIDs: ['realname','email','subject','body'],
			emailID: 'email',			
		    action: '/cgi-bin/formmail/formmail.cgi',	
			brdOK: '#000',
			brdDefault: null,
			brdBAD: '#F00',		
			artist: 'Jana Parkin',
			address: 'janaparkin@gmail.com' //'rbanning@gmail.com'
		}, options || {});

		var ctrl;
		this.theForm = $(this.options.formID);
		if (this.theForm) {
			this.theForm.setProperty('action',this.options.action);
			ctrl = $(this.options.recipientID);
			if (ctrl) {
				ctrl.setProperty('value',this.options.address);
			} else {
				alert('Error setting up contact form (missing input field - ' + this.options.recipientID + ')\nPlease email ' + this.AddressToString());
			}
			ctrl = $(this.options.artistID);
			if (ctrl) {
				ctrl.setProperty('value',this.options.artist + ' <' + this.options.address + '>');
			}
		} else {
			alert('Error setting up contact form (missing form - ' + this.options.formID + ')\nPlease email ' + this.AddressToString());
		}
		
		//check querystring
		var q = this._parseQuerystring(location.search);
		ctrl = $(this.options.titleID);
		if (ctrl && q) {
			if (q['title']) {
				ctrl.setProperty('value',q['title']);				
				ctrl = $(this.options.subjectID);
				if (ctrl) { ctrl.setProperty('value', 'INQUIRY: ' + q['title']); }
			} else {
				ctrl.setProperty('value','n/a');
			}
		}
		ctrl = $(this.options.pageRefID);
		if (ctrl && q) {
			if (q['ref']) {
				ctrl.setProperty('value',q['ref']);
			} else {
				ctrl.setProperty('value','n/a');
			}
		}				
	},
	
	AddressToString: function() {
		var ret = this.options.address.replace('@',' {AT} ');
		return ret.replace('.',' {DOT} ');
	},
	
	OnFocus: function(target) {
		if (target) {
			Contact._inputStyle(target, true);	
		}
	},

	OnBlur: function(target) {
		if (target) {
			Contact._inputStyle(target, null, this.options.brdDefault);	
		}
	},

	SubmitForm: function() {
		//validate required fields
		var txt = '';
		this.options.reqIDs.each( function(id) {
			if (!Contact._requiredField(id)) {
				if (txt.length == 0) { txt = 'The following fields are required:'; }
				txt += '\n' + id;
			}
		});
		if (txt.length > 0) {
			alert(txt);
			return false;
		}
		
		//validate email
		if (!Contact._validateEmail(this.options.emailID)) {
			alert('Invalid email address');
			return false;
		}
		
		//OK so submit
		this.theForm.submit();
		
	},
	
	_inputStyle: function (ctrl, isOK, override) {
		ctrl = $(ctrl);
		if (ctrl) {
			if (isOK == null) {
				ctrl.setStyle('border-color', override);
			} else if (isOK) {
				ctrl.setStyle('border-color', this.options.brdOK);
			} else {
				ctrl.setStyle('border-color', this.options.brdBAD);
			}
		}
	},
	
	_validateEmail: function(target) {
		var ret = true;
		target = $(target);
		if (target) {
			var value = target.getProperty('value');
			
			var reEmail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (value && reEmail.test(value)) {
				Contact._inputStyle(target, true);
			} else {
				ret = false;
				Contact._inputStyle(target, false);
			}
		}
		return ret;
	},
	
	_requiredField: function(target) {
		var ret = true;
		target = $(target);
		if (target) {
			var value = target.getProperty('value');			
			if (value && value.length > 0) {
				Contact._inputStyle(target, true);
			} else {
				ret = false;
				Contact._inputStyle(target, false);
			}
		}
		return ret;
	},
	
	_parseQuerystring: function (url) {
		var ret = new Object;
		if (url) {
			url = url.replace(/^\?/,'');  //remove question mark
			var temp = null;
			$A(url.split('&')).each( function (el) {
				temp = el.split('=');
				if (temp.length == 1) {
					ret[temp[0]] = '';
				} else {
					ret[temp[0]] = unescape(temp[1]).replace(/\+/g,' ');
				}
			});
		}
		return ret;
	}
	
};

window.addEvent('domready', Contact.init.bind(Contact));


