/*
 * file: sosignForm.js
 * -
 * What: Sosign form builder
 * When: 
 * Why: 
 */
 
/* Constructor */
function sosignForm(form, scriptPath) {

	this.form = form;
	this.scriptPath = scriptPath;
}

/*
 * prototype: sosignForm
 * -
 * What: sosignForm JSON object containing base functions
 * When: On form submit
 * Why: 
 */
sosignForm.prototype = {
	/*
	 * function: sosignForm.disableForm()
	 * -
	 * What: Disable form
	 * When: On send data
	 * Why: 
	 */
	disableForm: function() {
	
		var controllers = $("input", this.form).get().concat($("select", this.form).get());
		$(controllers).each(function(controller) { this.disabled = true; });
		
		if (!jQuery.browser.msie) {
			$(this.form).css({opacity: " .3", filter: "alpha(opacity=30"});
		}
		
		return true;
	},
	/*
	 * function: sosignForm.enableForm()
	 * -
	 * What: Enable form
	 * When: After data check and error found
	 * Why: 
	 */
	enableForm: function(form) {
	
		var controllers = $("input", this.form).get().concat($("select", this.form).get());
		$(controllers).each(function(controller) { this.disabled = false; });

		if (!jQuery.browser.msie) {
			$(this.form).css({opacity: "", filter: ""});
		}
		
		return true;
	},
	/*
	 * function: sosignForm.displayMessage()
	 * -
	 * What: Display message on top of form
	 * When: On ajax callback
	 * Why: 
	 */
	displayMessage: function(message, error) {
	
		/* Create message wrapper */
		var messageBox = $("<div></div>");
		var messageContent = $("<p></p>");
		
		/* Set class */
		if(error) {
			$(messageBox).attr("class", "formError");
			
		} else {
			$(messageBox).attr("class", "formConfirm");
		}
		
		/* Append content */
		$(messageBox).append(
			$(messageContent).prepend(message.toString())
		)
		.insertBefore(this.form)
		.fadeIn("slow");
		
	},
	/*
	 * function: sosignForm.send()
	 * -
	 * What: Send form data
	 * When: On form submit
	 * Why: 
	 */
	send: function(formSuffix) {
		var self = this;
		try {
			if(typeof(formSuffix) != "string") {
				formSuffix = "";
			}
			/* Disable form */
			self.disableForm();
			
			var error = false;
	
			/* Test email field */
			if(typeof(self.form["email"+formSuffix]) != "undefined") {
				if (self.form["email"+formSuffix].value.indexOf("@") == -1  
					|| self.form["email"+formSuffix].value.indexOf(".") == -1
					|| self.form["email"+formSuffix].value == "") {
					
					/* Highlight field that caused error */
					$(self.form["email"+formSuffix]).css({
						borderStyle:"solid", 
						borderColor:"#FF0000", 
						color:"#FF0000"
						});
					
					/* Enable form */
					self.enableForm();
					
					error = true;
		
				} else {
					$(self.form["email"+formSuffix]).attr("style", "");
				}
			}
			
			/* Test name field */
			if(typeof(self.form["name"+formSuffix]) != "undefined") {
			
				if(self.form["name"+formSuffix].value == "") {
				
					/* Highlight field that caused error */
					$(self.form["name"+formSuffix]).css({
						borderStyle:"solid", 
						borderColor:"#FF0000", 
						color:"#FF0000"
					});
					
					/* Enable form */
					self.enableForm();
					error = true;
					
				} else {
					$(self.form["name"+formSuffix]).attr("style", "");
				}
			}
			
			/* Test contact field */
			if(typeof(self.form["contact"+formSuffix]) != "undefined") {
			
				if(self.form["contact"+formSuffix].value == "") {
				
					/* Highlight field that caused error */
					$(self.form["contact"+formSuffix]).css({
						borderStyle:"solid", 
						borderColor:"#FF0000", 
						color:"#FF0000"
					});
					
					/* Enable form */
					self.enableForm();
					
					error = true;
					
				} else {
					$(self.form["contact"+formSuffix]).attr("style", "");
				}
			}
			
			/* Test website field */
			if(typeof(self.form["website"+formSuffix]) != "undefined") {
	
				if(self.form["website"+formSuffix].value == "") {
				
					/* Highlight field that caused error */
					$(self.form["website"+formSuffix]).css({
						borderStyle:"solid", 
						borderColor:"#FF0000", 
						color:"#FF0000"
					});
					
					/* Enable form */
					self.enableForm();
					
					error = true;
					
				} else {
					$(self.form["website"+formSuffix]).attr("style", "");
				}
			}
			/* Stop script if error */
			if(error == true) {
				self.enableForm();
				return false;
			}
			
			/* collect form data and convert it to stringVar */
			var formFields = new Object();
			
			for(var field in self.form) {
				if(self.form[field] && self.form[field].type) {
					if(self.form[field].type != "submit" 
						&& self.form[field].type != "button" 
						&& self.form[field].type != "reset") {
						formFields[self.form[field].name] = self.form[field].value;
					}
				}
			}
			
			var stringVars = self.jsonToStringVars(formFields);
			
			/* Ajax call */
			$.ajax({
				url: self.scriptPath,
				type: "post",
				data: stringVars,
				dataType: "json",
				success: function(json) {
					
					/* Test message type */
					if(json.type == "error") {
						self.displayMessage(json.message, true);
						
					} else {
						self.displayMessage(json.message);
					}
				}
			});
		} catch(e) {
			alert(e);
			return false;
		}
		return false;
	},
	
	/*
	 * function: jsonToStringVars()
	 * -
	 * What: Convert a simple json object into a string like "?variable1=value1&variable2=value2&..."
	 * When:
	 * Why: 
	 */
	jsonToStringVars: function(jsonObject) {
		var stringVars = "";
		
		$.each(jsonObject, function(item, value) {
			stringVars += "&"+item+"="+value;
		});
		
		return stringVars.replace(/^\&/, "");
	}
};
/*
 * function: preOrder()
 * -
 * What: send pre-order form
 * When:
 * Why: 
 */
function preOrder(formEl, formSuffix) {
	var senderObject = new sosignForm(formEl, 'php/send_preorder_form.php');
	senderObject.send(formSuffix);
	return false;
}

/*
 * function: order()
 * -
 * What: send order form
 * When:
 * Why: 
 */
function order(formEl, formSuffix) {
	var senderObject = new sosignForm(formEl, 'php/send_order_form.php');
	senderObject.send(formSuffix);
	return false;
}

/*
 * function: project()
 * -
 * What: send project form
 * When:
 * Why: 
 */
function project(formEl, formSuffix) {
	var senderObject = new sosignForm(formEl, 'php/send_project_form.php');
	senderObject.send(formSuffix);
	return false;
}