/*
 *	upload.js
 *		unobtrusive javascript to enable
 *		asynchronous form submission
 *	
 *	by Travis Parker for Red Hill Studios
 *	
 *	This script creates an IFrame element with the CSS style
 *	display: none to act as the 'target' of the form. This
 *	means that the main window never needs to navigate away
 *	from it's location, creating the illusion that the
 *	submission is asynchronous.
 */

var com;
if (!com) com = {};
if (!com.redhillstudios) com.redhillstudios = {};
if (!com.redhillstudios.timeweb) com.redhillstudios.timeweb = {};

com.redhillstudios.timeweb.upload = {
	
	activateForm: function(xsFormId) {
		
		var voFrame = document.createElement("iframe");
		var voForm = document.getElementById(xsFormId);
		
//		voFrame.setAttribute("id", "hiddeniframe");
//		voFrame.setAttribute("name", "hiddeniframe");
//		voFrame.setAttribute("style", "display: none; visibility: hidden; width: 1px; height: 1px; position: absolute; left: 0; top: 0; z-index: 9999;");
//		voFrame.setAttribute("width", 0);
//		voFrame.setAttribute("height", 0);
//		document.body.appendChild(voFrame);
		
//		voForm.setAttribute("target", "hiddeniframe");
		voForm.setAttribute("id", "activeForm");
		voForm.onsubmit = com.redhillstudios.timeweb.upload._validateForm;
		voForm.action = voForm.action + "?noreload=" + String(Math.round(Math.random()*10000));
		
	},
	
	_displayFormErrors: function() {
		
		for (var vsFieldId in com.redhillstudios.timeweb.upload._formErrors) {
			var voInputField = document.getElementById(vsFieldId);
			var voLabel = document.createElement("label");
			
			voLabel.htmlFor = vsFieldId;
			voLabel.className = "errors";
			voLabel.innerHTML = com.redhillstudios.timeweb.upload._formErrors[vsFieldId];
			
			voInputField.parentNode.insertBefore(voLabel, voInputField.parentNode.firstChild);
		}
		
	},
	
	_removeFormErrors: function() {
		
		var vlLabels = document.getElementsByTagName("label");
		var vlRemoveUs = Array();
		
		for (var i = 0; i < vlLabels.length; i++) {
			if (vlLabels[i].className == "errors") {
				vlRemoveUs.push(vlLabels[i]);
			}
		}
		
		for (var i = 0; i < vlRemoveUs.length; i++) {
			var voRemoveMe = vlRemoveUs[i];
			
			voRemoveMe.parentNode.removeChild(voRemoveMe);
		}
		
		com.redhillstudios.timeweb.upload._formErrors = {};
		
	},
	
	_validateForm: function() {
		
		var voForm = document.getElementById('activeForm');
		var vlInputs = voForm.getElementsByTagName("input");
		var vlTextAreas = voForm.getElementsByTagName("textarea");
		var vlSelects = voForm.getElementsByTagName("select");
		var voAgree = document.getElementById('agree');
		var vsValid = true // innocent until proven guilty
		
		com.redhillstudios.timeweb.upload._removeFormErrors();
		
		for (var i = 0; i < vlInputs.length; i++) {
			var voInput = vlInputs[i];
			
			if (voInput.className.indexOf("email") != -1) {
				if (!/^[\w-\.]+@[\w-]+[\.][\w]{2,4}$/.test(voInput.value)) {
					com.redhillstudios.timeweb.upload._formErrors[voInput.id] = "Please enter a valid email.";
					vsValid = false;
				}
			} else if (voInput.id == "psContactName") {
				if (voInput.value.length == 0) {
					com.redhillstudios.timeweb.upload._formErrors[voInput.id] = "Please enter your name.";
					vsValid = false;
				}
			} else if (voInput.id == "psVisName") {
				if (voInput.value.length == 0) {
					com.redhillstudios.timeweb.upload._formErrors[voInput.id] = "Please enter a clip name.";
					vsValid = false;
				}
			}  else if (voInput.id == "poClip") {
				if (voInput.value.length == 0) {
					com.redhillstudios.timeweb.upload._formErrors[voInput.id] = "Please select a video file.";
					vsValid = false;
				}
			} // add 'else if' clauses here for more validation tests on other <input> nodes.
		}
		
		for (var i = 0; i < vlTextAreas.length; i++) {
			var voTextArea = vlTextAreas[i];
			
			if (voTextArea.id == "psSource") {
				if (voTextArea.value.length == 0) {
					com.redhillstudios.timeweb.upload._formErrors[voTextArea.id] = "Please describe the source.";
					vsValid = false;
				}
			} // add 'else if' clauses here for more validation tests on other <textarea> nodes.
		}
		
		for (var i = 0; i < vlSelects.length; i++) {
			var voSelect = vlSelects[i];
			
			if (voSelect.id == "piType") {
				if (voSelect.value == "") {
					com.redhillstudios.timeweb.upload._formErrors[voSelect.id] = "Please select a type.";
					vsValid = false;
				}
			} else if (voSelect.id == "piTopic") {
				if (voSelect.value == "") {
					com.redhillstudios.timeweb.upload._formErrors[voSelect.id] = "Please select a topic.";
					vsValid = false;
				}
			} else if (voSelect.id == "piTimeScale") {
				if (voSelect.value == "") {
					com.redhillstudios.timeweb.upload._formErrors[voSelect.id] = "Please select a timescale.";
					vsValid = false;
				}
			}
		}
		
		if (!voAgree.checked) {
			com.redhillstudios.timeweb.upload._formErrors[voAgree.id] = "You must agree to the terms to submit.";
			vsValid = false;
		}
		
		com.redhillstudios.timeweb.upload._displayFormErrors();
		
		var a = voForm.action.split('=');
		voForm.action = voForm.action.substring(0, voForm.action.indexOf(a[a.length-1])) + String(Math.round(Math.random()*10000));
		
		return vsValid;
		
	},
	
	_formErrors: {} // starts empty, it will be populated by the _validateForm function (or not)
	
};

