// create Login object for the login form
var Login = {
	
	form:null, // holds the form element
	inputs:null, // holds the input elements (as an array)
	warning:null, // holds the warning element, if any
	ajax:null, // holds the ajax request
	
	// setup events for login form
	setup:function() {
		
		// get form, and continue if it exists
		this.form = $(document.forms["login"]);
		if(!this.form) { return; }
		
		var p = this; // acts as parent
		this.warning = this.form.getElement("span.warning");
		GOOP.extend(new Array(this.form.username, this.form.password),["events"]);
		
		// set up request
		this.ajax = new Ajax({
			url:"users/login",
			complete:function(html) {
				if(html == "ok") {
					window.location.href = "./backend";
				} else if(html == "bad") {
					if(p.warning) {
						p.warning.set("html","Invalid Username and/or Password");
					} else {
						Validate.alert(p.form.username,"Invalidate Username and/or Password");
					}
				} else if(html == "inactive") {
					if(p.warning) {
						p.warning.set("html","Account is Inactive");
					} else {
						Validate.alert(p.form.username,"Account is Inactive");
					}
				}
			}
		});
		
		// username
		this.form.username.addEvent("keypress",function(e) {
			if(new Event(e).code == 13) {
				p.check();
			}
		});
		
		// password
		this.form.password.addEvent("keypress",function(e) {
			if(new Event(e).code == 13) {
				p.check();
			}
		});
		
	},
	
	// check login
	check:function() {

		if(Validate.empty(this.form.username)) {
			return;
		}
		if(Validate.empty(this.form.password)) {
			return;
		}
		this.ajax.send("username=" + this.form.username.value.trim() + "&password=" + this.form.password.value.trim());
	}
	
};


var Contact = {
	
	form:null, // holds the form element
	
	// get form element
	setup:function() {
		this.form = $(document.forms["contact"]);
	},
	
	// checks if the user filled out the form
	check:function() {
		
		if(!Validate.letters(this.form.first_name, "Invalid First Name")) {
			return false;
		}
		if(!Validate.letters(this.form.last_name, "Invalid Last Name")) {
			return false;
		}
		if(!Validate.email(this.form.email)) {
			return false;
		}
		if(Validate.empty(this.form.city)) {
			return false;
		}
		if(Validate.empty(this.form.state, "Invalid State")) {
			return false;
		}
		
		var phone = "(" + this.form.phone_area.value + ") " + this.form.phone_3.value + "-" + this.form.phone_4.value;
		if(!Validate.phone(phone)) {
			Validate.alert(this.form.phone_4,"Invalid Phone");
			return false;
		}
		
		this.form.action = "./frontend/contact_form";
		this.form.submit();
		
	}
	
};

// lets begin
DOM.ready(function() {
	
	Login.setup();
	Contact.setup();
	
	if($("anagram_home") != null) {
		$("anagram_home").addEvents({
			click:function() {
				if(this.get("html").indexOf(".swf") == -1) {
					new SWF("assets/swfs/email_square.swf", {
						container:this,
						width:720,
						height:360
					});
				}
			},
			mouseover:function() {
				this.setStyle("background","transparent url(assets/images/anagram_home_over.jpg) no-repeat center center");
			},
			mouseout:function() {
				this.setStyle("background","transparent url(assets/images/anagram_home_out.jpg) no-repeat center center");
			}
		}).setStyles({
			"background":"transparent url(assets/images/anagram_home_out.jpg) no-repeat center center",
			"width":720,
			"height":360,
			"cursor":"pointer"
		});
	}
	
	if($("marketing_video") != null) {
		$("marketing_video").addEvents({
			click:function() {
				if(this.get("html").indexOf(".swf") == -1) {
					new SWF("assets/swfs/Tab 1.swf",{
						container:this,
						width:720,
						height:360
					});
				}
			},
			mouseover:function() {
				this.setStyle("background","transparent url(assets/images/marketing_video_over.jpg) no-repeat center center");
			},
			mouseout:function() {
				this.setStyle("background","transparent url(assets/images/marketing_video_out.jpg) no-repeat center center");
			}
		}).setStyles({
			"background":"transparent url(assets/images/marketing_video_out.jpg) no-repeat center center",
			"width":720,
			"height":360,
			"cursor":"pointer"
		});
	}
	
	if($("demo") != null) {
		
		new SWF("assets/swfs/Demo_Video.swf", {
			container:$("demo"),
			width:640,
			height:499
		});
		
	}
	
});