com_givensviolins_www = new (function() {
	var Application = function() {
		
	};

	Application.toHTTPS = function(uri) {
		var securedURI = 'https://' + window.location.hostname + uri;
		return securedURI;
	}

	Application.validateForm = function(button) {
		var elm;
		var fn;
		var validator;
		var prompt;
		var label;
		var className;
		var messages = [];
		var errors = [];
		var validators = [];
		var frm = button.form

		for (var i = 0; i < frm.elements.length; i++) {
			elm = frm.elements[i];

			if (validator = document.getElementById(elm.id + '.validation')) {
				validators.push({ validation: validator.value, id: elm.id });
			}
		}

		for (var i = 0; i < validators.length; i++) {
			try {
				validator = validators[i];
				fn = new Function('return ' + validator.validation)();

				if (fn) {
					elm = document.getElementById(validator.id);
					label = document.getElementById(validator.id + '.prompt');

					if (!fn(elm)) {
						if (label) {
							prompt = label.innerHTML;
							label.className = label.className + ' invalid';
						}
						
						messages.push((prompt) ? prompt : elm.name);
						elm.className = elm.className + ' invalid';
						label.className = label.className + ' invalid';
					} else {
						elm.className = elm.className.replace(/\s*invalid/g, '');
						if (label && label.className.indexOf('invalid') >= 0) {
							className = new String(label.className);
							label.className = className.replace(/\s*invalid/g, '');
						}
					}
				}
			} catch (e) {
				errors.push(e.message + '(' + validator.validation + ')');
			}
		}

		if (errors.length > 0) {
			alert("Errors occurred during processing: \r\n" + errors.join('\r\n'));
		} else if (messages.length > 0) {
			alert("Please complete the following fields:\r\n - " + messages.join('\r\n - '));
			return false;
		}

		return true;
	}

	Validators = {
		checkPostalcode: function(elm) {
			var str;

			if (elm) {
				str = new String(elm.value);
				str = str.replace(/[^a-zA-Z0-9\- ]/g, '');

				if (str.length > 3) {
					elm.value = str;
					return true;
				}
			}
			return false;
		},
		checkPhone: function(elm) {
			var str;

			if (elm) {
				str = new String(elm.value);
				str = str.replace(/[^0-9.\-_,:;\/ ]/g, '');
				str = str.replace(/\-_,:;\/\(\) ]/g, ' ');
				str = str.replace(/^\s+/g, '');
				str = str.replace(/\s+$/g, '');
				str = str.replace(/\s\s*/g, '.');

				if (str.length > 6) {
					elm.value = str;
					return true;
				}
			}
			return false;
		},
		checkWords: function(elm) {
			var str;

			if (elm) {
				str = new String(elm.value);
				str = str.replace(/[^A-Za-z0-9.\-_,:;\/ ]/g, '');
				str = str.replace(/^\s+/g, '');
				str = str.replace(/\s+$/g, '');
				str = str.replace(/\s\s+/g, ' ');

				if (str.length > 0) {
					elm.value = str;
					return true;
				}
			}
			return false;
		},
		checkEmail: function(elm) {
			var str;
			var name;
			var octet;
			var host;
			var pos;

			if (elm) {
				str = new String(elm.value);
				pos = str.indexOf('@');
				if (pos > 0) {
					name = str.substring(0, pos);
					name = name.replace(/[^A-Za-z0-9.\-_:$]/g, '');
					if (name.length > 0) {
						host = str.substring(pos + 1);
						host = host.replace(/[^A-Za-z0-9.\-_:]/g, '');
						if (host.length > 0 && (pos = host.lastIndexOf('.')) > 0) {
							octet = host.substring(pos + 1).replace(/[^A-Za-z0-9.\-_:\/ ]/g, '');

							if (octet.length > 0) {
								elm.value = name + '@' + host;
								return true;
							}
						}
					}
				}
			}
			return false;
		},
		checkCCN: function(elm) {
			var ccn;
			var count;

			if (elm) {
				try {
					ccn = new String(elm.value);

					ccn = ccn.replace(/[^0-9]/g, ' ');
					ccn = ccn.replace(/\s+/g, ' ');
					ccn = ccn.replace(/\s+$/, '');
					ccn = ccn.replace(/^\s+/, '');

					count = 0;
					for (var i = 0; i < ccn.length; i++) {
						if ("0123456789".indexOf(ccn.charAt(i)) >= 0) {
							count++;
						}
					}

					if (count > 14) {
						elm.value = ccn;
						return true;
					}
				} catch (e) {
					alert("Error " + e.message);
				}
			}
			return false;
		},
		checkCCSecurity: function(elm) {
			var str;

			str = new String(elm.value);
			str = str.replace(/[^A-Za-z0-9]/g, '');

			if (str.length >= 3) {
				elm.value = str.toUpperCase();
				return true;
			}
			return false;
		}
	}

	this.Application = Application;
	this.Validators = Validators;
})();









