var domainPath = window.location.protocol + "//" + window.location.host;
var pathNameArray = window.location.pathname.split("/");
if(pathNameArray[1] && pathNameArray[1].match(/^[a-z0-9\-\.][a-z0-9\-\.]{1,75}\.(com|net|org|us|biz|edu|tv|mobi|co.uk|ca|info|ws|me|co|cc|asia|localhost)$/i))
	domainPath += "/" + pathNameArray[1];

window.addEvent("domready", function() {
	$$('form').addEvent("submit", function() {
		if(this.querySelector('#checkField'))
			this.querySelector('#checkField').value = '';

		return checkRequired(this);
	});

	$$("input.overText").each(function(ele) {
		if(ele.name.toLowerCase().indexOf("password")==-1) {
			if(ele.value=="" || ele.value==ele.get("title")) {
				ele.value = ele.get("title");
				ele.addClass("overText");
				ele.addEvent("focus", function() {
					this.removeClass("overText");
					if(this.value == this.get("title"))
						this.value = "";
				});
			}
			ele.addEvent("blur", function() {
				if(this.value == "") {
					this.addClass("overText");
					this.value = this.get("title");
				}
			});
		} else {
			if(ele.value=="") {
				// If loginMenuPassword has no value, we clone it and create a text field with "Password:" inside it. We then replace
				// the password field with this new one.
				// When it gains focus, we switch out the text field to a password field.
				eleID = ele.get("id");
				if(ele.get("maxlength") < 0)
					ele.set("maxlength",100);
				var newInput = new Element("input", {"type":ele.get("type"),"value":ele.get("title"),"class":ele.get("class"),"name":ele.get("name"),"maxlength":ele.get("maxlength"),"title":ele.get("title")});
				newInput.addEvent("focus", function() {
					eleID = this.get("id");
					var newInput2 = new Element("input", {"type":"password","class":this.get("class"),"name":this.get("name"),"maxlength":this.get("maxlength"),"title":this.get("title")});
					newInput2.addEvent("focus", function() {
						this.removeClass("overText");
					});
					this.blur();
					this.parentNode.replaceChild(newInput2,this);
					newInput2.set("id",eleID);
					setTimeout('$(\'' + eleID + '\').focus()',100);	// If I don't set a timeout, than it won't have anything to focus on.
				});
				ele.parentNode.replaceChild(newInput,ele);
				newInput.set("id",eleID);
			}
		}
	});
});

function checkRequired(formEle) {
	var formElements = formEle.elements;
	for(i=0;i<formElements.length;i++) {
		if(formElements[i].hasClass("required")) {
			if(formElements[i].type == "text" || formElements[i].type == "textarea" || formElements[i].type == "password") {
				if(formElements[i].value == "" || (formElements[i].hasClass("overText") && formElements[i].value == formElements[i].title)) {
					alert("Please provide all required information.");
					if(!formElements[i].hasClass("alert")) {
						formElements[i].addClass("alert");
					}
					formElements[i].focus();
					return false;
				}
			}
			if(formElements[i].type.indexOf("select") != -1) {
				if(formElements[i].options[formElements[i].selectedIndex].value == "") {
					alert("Please provide all required information.");
					if(!formElements[i].hasClass("alert")) {
						formElements[i].addClass("alert");
					}
					formElements[i].focus();
					return false;
				}
			}
		}
	}
	return true;
}

// Detect if device is a touch device
function is_touch_device() {
	// Adapted from Modernizr
	return (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch);
};

LoadFiles = new Class({
	Implements: [Options, Events],
	
	options: {
		"onComplete": function(){}
	},
	
	initialize: function(fileArr,options) {
		this.fileArr = fileArr;
		this.fileIdx = 0;
		this.setOptions(options);
		this.start();
	},
	
	start: function() {
		if(this.fileIdx < this.fileArr.length) {
			if(/.js$/.test(this.fileArr[this.fileIdx])) {
				Asset.javascript(this.fileArr[this.fileIdx], function() {
									 this.fileIdx++;
									 this.start();
								 }.bind(this));
			} else if(/.css$/.test(this.fileArr[this.fileIdx])) {
				Asset.css(this.fileArr[this.fileIdx], function() {
							 this.fileIdx++;
							 this.start();
						 }.bind(this));
			}
		} else {
			this.fireEvent("complete");
		}
	}
	
});

// Rewrite of Asset.css and Asset.javascript for crossbrowser onLoad support.
// Modified from below link.
// http://dev.enekoalonso.com/2010/01/27/detecting-when-css-gets-loaded-by-the-browser-with-mootools-and-asset-css/

Asset.css = function(source, onLoad) {
	var link = new Element('link', {
		rel: 'stylesheet',
		media: 'screen',
		type: 'text/css',
		href: source
	});
	
	if (("onload" in link) && !Browser.chrome && !Browser.safari) {
		if (onLoad) link.onload = onLoad;
	} else {
		(function() {
			try {
				link.sheet.cssRules;
			} catch (e) {
				setTimeout(arguments.callee, 100);
				return;
			};
			if (onLoad) onLoad();
		})();
	}
	
	link.inject(document.head);
	return link;
}

Asset.javascript = function(source, onLoad) {
	var script = new Element('script', {
		type: 'text/javascript',
		src: source
	});
	
	if (("onload" in script)) {
		if (onLoad) script.onload = onLoad;
	} else {
		(function(script) {
			function isLoaded(script) {
				try {
					if (/loaded|complete/.test(script.readyState) == false)
						throw "Not ready...";
					return true;
				} catch (e) {
					setTimeout(function() { isLoaded(script); }, 100);
				};
			}
			if(isLoaded(script)) {
				if (onLoad) 
					setTimeout(onLoad, 100);
			}
		})(script);
	}
	script.inject(document.head);
	
	return script;
}
