/*globals window, document, navigator, $ */
var W3CDOM = (document.createElement && document.getElementsByTagName);

var CJQuForms = {

	vars: {
		highlight_array: []
	},

	clearSafariRadios: function() {
		$.each(CJQuForms.vars.highlight_array, function(i, n) {
			if (n.parentNode) {
				$(this).parents('li').removeClass('focused');
			}
		});
	},

	validateRange: function(ColumnId, RangeType) {
		if ($('#rangeUsedMsg' + ColumnId)) {
			var field = $('#Field' + ColumnId);
			var msg = $('#rangeUsedMsg' + ColumnId);

			switch (RangeType) {
			case 'character':
				msg.innerHTML = field.value.length;
				break;
			case 'word':
				var words = field.value.split(" ");
				var used = 0;
				$.each(words, function(i, n) {
					if (n.replace(/\s+$/, "") !== "") {
						used++;
					}
				});
				msg.innerHTML = used;
				break;
			case 'digit':
				msg.innerHTML = field.value.length;
				break;
			}
		}
	},

	checkMechanicalTurk: function() {
		if ($('#mechanicalTurk').length > 0 && $('#merchantMessage').length > 0 && $('#merchantButton').length > 0) {
			$('#merchantMessage').innerHTML = 'Your submission is being processed. You will be redirected shortly.';
			$('#merchantButton').style.display = 'none';
			$('#mechanicalTurk').submit();
		}
	},

	checkPaypal: function() {
		if ($('#merchant').length > 0 && $('#merchantMessage').length > 0 && $('#merchantButton').length > 0) {
			$('#merchantMessage').innerHTML = 'Your order is being processed. Please wait while the page redirects to the merchant checkout.';
			$('#merchantButton').style.display = 'none';
			$('#merchant').submit();
		}
	},

	initAutoResize: function() {
	},

	showRangeCounters: function() {
		var counters = $('em.currently');
		$.each(counters, function(i, n) {
			n.style.display = 'inline';
		});
	},

	ifInstructs: function() {
		var container = $('#public');
		if (container.length > 0) {
			$('#public').removeClass('noI');
			var instructs = $('.currently');
			if (instructs === '') {
				$(container).addClass('noI');
			}
			if (container.offsetWidth <= 450) {
				$(container).addClass('altInstruct');
			}
		}
	},

	// for radio and checkboxes, they have to be cleared manually, so they are added to the
	// global array CJQuForms.vars.highlight_array so we dont have to loop through the dom every time.
	initializeFocus: function() {
		$('.field').each(function(i, n) {
			if (n.type === 'radio' || n.type === 'checkbox' || n.type === 'file') {
				$(n).click(function() {
					CJQuForms.clearSafariRadios();
					$(this).parents('li').addClass('click');
				});
				$(n).focus(function() {
					CJQuForms.clearSafariRadios();
					$(this).parents('li').addClass('focused');
				});
				CJQuForms.vars.highlight_array.splice(CJQuForms.vars.highlight_array.length, 0, n);
			}
			$(n).focus(function() {
				CJQuForms.clearSafariRadios();
				$(this).parents('li').addClass('focused');
			});
			$(n).blur(function() {
				$(this).parents('li').removeClass('focused');
			});
		});
	},
	
	/* http://www.quirksmode.org/dom/inputfile.html */
	fixFileUpload: function() {
		
		
		var x = document.getElementsByTagName('input');
		$('input[type=file]').each(function(i,n) {
			if (n.parentNode.className === 'input_file_wrapper') {
				var f = document.createElement('div');
				$(f).addClass('input_file_div');
				var inp = document.createElement('input');
				inp.type = "text";
				inp.value = "";
				$(inp).addClass('input_file_fake');
				f.appendChild(inp);
				var btn = document.createElement('button');
				$(btn).addClass('linkButton');
				$(btn).html('Browse File');
				$(btn).click(function() {
					$(n).trigger('click');
					return false;
				});
				f.appendChild(btn);
				$(n).css({position:"relative", textAlign:"right", mozOpacity:"0", filter:"alpha(opacity: 0)", opacity:"0", zIndex:"2", marginBottom:"7px"});
				n.parentNode.appendChild(f);
				$(n).change(function() {
					inp.value = this.value;
				});
				$(n).mousedown(function() {
					inp.value = this.value;
				});
				$(n).mouseover(function() {
					$(btn).addClass('linkButtonHover');
				});
				$(n).mouseout(function() {
					$(btn).removeClass('linkButtonHover');
				});
			}
		});
	},
	
	updateTimeField: function(elm) {
		$("input#"+elm).val(
			$("select#"+elm+"_HOUR").val() + ":" +
			$("select#"+elm+"_MIN").val() + " " +
			$("select#"+elm+"_ZONE2").val()
		);
	},
	
	browserDetect: function() {
		var detect = navigator.userAgent.toLowerCase();
		var container = $('html');
		if (detect.indexOf('safari') + 1) {
			$(container[0]).addClass('safari');
		}
		if (detect.indexOf('firefox') + 1) {
			$(container[0]).addClass('firefox');
		}
	},

	initForm: function() {
		if (!document.getElementById || !document.createElement) {
			return;
		}
		CJQuForms.browserDetect();
		CJQuForms.fixFileUpload();
		CJQuForms.initializeFocus();
		CJQuForms.ifInstructs();
		CJQuForms.showRangeCounters();
		CJQuForms.initAutoResize();
		CJQuForms.checkPaypal();
		CJQuForms.checkMechanicalTurk();
	}

};

$(document).ready(function() {
	CJQuForms.initForm();
});