var siteGlobal = function () {
	// private
	function isEmpty (val) {
		var testExp = /^\w+/i;
		return (testExp.test(val)) ? false : true;
	}
	
	function validateForm (f) {
		var errors = [];
		
		f.select('.req').each(function (el,i) {
			var lblEL = el.up('label') || el.previous('label') || el.up('li').down('label') || false;
			var field = (lblEL !== false) ? lblEL.innerHTML : '';
			
			switch (el.tagName) {
			case 'INPUT' :
				switch (el.type) {
				case 'text' :
					if (isEmpty(el.value)) {
						errors.push('<span>' + field + '</span> field was left empty.');
					} else {
						if (el.hasClassName('email')) {
							var format = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
							if (!format.test(el.value)) { errors.push('<span>' + field + '</span> field is formatted wrong.'); }
						}
					}
					break;
				case 'radio' :
					
					break;
				case 'checkbox' :
					
					break;
				}
				break;
			case 'SELECT' :
				if (el.value == '') { errors.push('<span>' + field + '</span>'); }
				break;
			case 'TEXTAREA' :
				if (isEmpty(el.value)) { errors.push('<span>' + field + '</span>'); }
				break;
			}
			
		});
		
		return errors;
	}
	
	function alertErrors (errEL,errs,type) {
		if (errs.length > 0) {
			var errStr = '';
			errs.each(function(err) { errStr += '<li>' + err + '</li>'; });
			errStr = '<h2>The following errors have been detected:</h2><ol>' + errStr + '</ol>';
			alertMsg(errEL,errStr);
		}
	}
	
	function alertMsg (el,msg) {
		if (msg != '') {
			var alertStr = '<div class="errContent">\
			<div class="close" onclick="javascript:siteGlobal.closeFormMsg(this);"></div>\
			\<div class="jjBox jjBBox jjTL jjTR">\
				<div class="jjT"><span></span><div></div></div>\
				<div class="jjM">\
					' + msg + '\
				</div>\
				<div class="jjB"><span></span><div></div></div>\
			</div>\
			</div>';
			
			var errMsgEL = el.next('div.errMsg');
			
			//alert(typeof errMsgEL);
			
			if (typeof errMsgEL !== 'object') {
				var newEL = new Element('div',{'class':'errMsg','style':'display:none;'});
				el.insert({'after':newEL});
				errMsgEL = el.next('div.errMsg');
				
			}
			errMsgEL.update(alertStr).blindDown({'duration':.5});
		}
	}
	
	function trackIt (tCode) {
		if (typeof pageTracker === 'object') {
			pageTracker._trackPageview(tCode);
		}
	}
	
	//public
	var pub = function () {
		
		return {
			SITE : {},
			PAGE : {},
			
			trackIt : function (str) {
				trackIt(str);
			},
			
			emailDirector : function (params) {
				if (typeof params.preTrack === 'string') { trackIt(params.preTrack); }
				var prePopText = 'I saw this white paper from Zebra Technologies and thought it would interest you. It explains why using thermal printing technology for patient wristbanding cuts down on waste and saves money.' + "\n\n";
				prePopText += 'Please take a moment to read "Patient Wristbanding: 5 Reasons Why Hospitals Should Choose a Thermal Print Solution."' + "\n\n";
				prePopText += 'Also, Zebra makes a terrific thermal printer -- the HC100. It\'s fast, accurate and easy to use. I think it could really streamline the workflow for our nurses, while reducing support calls to IT.' + "\n\n";
				prePopText += 'Thanks for your time.';

				var eForm = '<div class="jjBox jjBBox">\
				<div class="jjT"><span></span><div></div></div>\
				<div class="jjM">\
					<div class="form" id="popUpForm">\
					<h2>Tell your IT director about the benefits of thermal printing.</h2>\
					<p>Send him/her our free white paper, <em>Patient Wristbanding: 5 Reasons Why Hospitals Should Choose a Thermal Print Solution."</em> Just fill out the form below.</p>\
					<form id="emailDirectorForm" action="#" method="post" onsubmit="return siteGlobal.sendForm(this);">\
					<fieldset>\
					<div class="cols">\
						<div class="col">\
							<label for="emailDirector_ToName">IT Contact Name</label>\
							<input type="text" class="text req" id="emailDirector_ToName" name="ToName" value="" />\
							<label for="emailDirector_ToTitle">IT Contact Title</label>\
							<input type="text" class="text req" id="emailDirector_ToTitle" name="ToTitle" value="" />\
							<label for="emailDirector_ToEmail">IT Contact E-mail</label>\
							<input type="text" class="text req email" id="emailDirector_ToEmail" name="ToEmail" value="" />\
						</div>\
						<div class="col">\
							<label for="emailDirector_FromName">Your Name</label>\
							<input type="text" class="text req" id="emailDirector_FromName" name="FromName" value="" />\
							<label for="emailDirector_FromTitle">Your Title</label>\
							<input type="text" class="text req" id="emailDirector_FromTitle" name="FromTitle" value="" />\
							<label for="emailDirector_FromEmail">Your E-mail</label>\
							<input type="text" class="text req email" id="emailDirector_FromEmail" name="FromEmail" value="" />\
						</div>\
					</div>\
					<label for="emailDirector_comments">Comments</label>\
					<textarea id="emailDirector_comments" name="comments">' + prePopText + '</textarea>\
					</fieldset>\
					<fieldset class="formButtons">\
						<input type="image" src="images/submit.gif" class="submit" />\
					</fieldset>\
					<input type="hidden" name="cmd" value="sendToDirector" />';
					
					if (typeof params.postTrack === 'string') { eForm += '<input type="hidden" name="trackIt" value="' + params.postTrack + '" />'; }
					
					eForm += '</form>\
					</div>\
				</div>\
				<div class="jjB"><span></span><div></div></div>\
				</div>';
				
				Dynalicious.openOverlayWin(eForm,{closeWin:true,w:500,h:400});
			},
			
			emailUs : function (params) {
				if (typeof params.preTrack === 'string') { trackIt(params.preTrack); }
				
				var eForm = '<div class="jjBox jjBBox">\
				<div class="jjT"><span></span><div></div></div>\
				<div class="jjM">\
					<div class="form" id="popUpForm">\
					<form id="emailUsForm" action="#" method="post" onsubmit="return siteGlobal.sendForm(this);">\
					<fieldset>\
					<div class="cols">\
						<div class="col">\
							<label for="emailUs_FirstName">First Name</label>\
							<input type="text" class="text req" id="emailUs_FirstName" name="FirstName" value="" />\
							<label for="emailUs_LastName">Last Name</label>\
							<input type="text" class="text req" id="emailUs_LastName" name="LastName" value="" />\
							<label for="emailUs_Title">Title</label>\
							<input type="text" class="text req" id="emailUs_Title" name="Title" value="" />\
							<label for="emailUs_FromEmail">E-mail</label>\
							<input type="text" class="text req email" id="emailUs_FromEmail" name="FromEmail" value="" />\
							<label for="emailUs_Company">Company</label>\
							<input type="text" class="text req" id="emailUs_Company" name="Company" value="" />\
						</div>\
						<div class="col">\
							<label for="emailUs_City">City</label>\
							<input type="text" class="text req" id="emailUs_City" name="City" value="" />\
							<label for="emailUs_State">State</label>\
							<input type="text" class="text req" id="emailUs_State" name="State" value="" />\
							<label for="emailUs_Zip">Zip</label>\
							<input type="text" class="text req" id="emailUs_Zip" name="Zip" value="" />\
							<label for="emailUs_Phone">Phone</label>\
							<input type="text" class="text req" id="emailUs_Phone" name="Phone" value="" />\
						</div>\
					</div>\
					<label for="emailUs_comments">Comments</label>\
					<textarea id="emailUs_comments" name="comments" class="req"></textarea>\
					</fieldset>\
					<fieldset class="formButtons">\
						<input type="image" src="images/submit.gif" class="submit" />\
					</fieldset>\
					<input type="hidden" name="cmd" value="sendUsEmail" />';
					
					if (typeof params.postTrack === 'string') { eForm += '<input type="hidden" name="trackIt" value="' + params.postTrack + '" />'; }
					
					eForm += '</form>\
					</div>\
				</div>\
				<div class="jjB"><span></span><div></div></div>\
				</div>';
				
				Dynalicious.openOverlayWin(eForm,{closeWin:true,w:500,h:400});
			},
			
			
			sendForm : function (f) {
				f = ($(f).tagName == 'FORM') ? $(f) : $(f).down('form');
				var errors = validateForm(f);
				var fID = f.id;
				
				if (errors.length > 0) {
					alertErrors(f,errors);
				} else {
					
					var ajaxFile = 'ajax/sendForm.php';
					var ajaxParams = f.serialize(true);
					
					// ======= extended params
					switch (fID) {
					case 'emailUsForm' :
						ajaxParams.subject = eSubject;
						break;
					case 'emailDirectorForm' :
						ajaxParams.oCode = oCode;
						break;
					}
					// ======= extended params
					
					var ajaxOpts = {method:'get',
						parameters : ajaxParams,
						onSuccess : function(resp) {
							//alert(resp.responseText.strip());
							resp = resp.responseText.strip().evalJSON();
							var msg = resp.msg;
							if (siteTestMode && typeof resp.sent === 'string') { msg = resp.sent + msg; }
							alertMsg(f,msg);
							f.reset();
							
							// ======= post-tracking
							if (typeof resp.trackIt === 'string') {
								trackIt(resp.trackIt);
							}
							// ======= post-tracking
							
						}
					}
					new Ajax.Request(ajaxFile,ajaxOpts);
				}
				return false;
			},
			
			closeFormMsg : function (btnEL) {
				$(btnEL).up('.errMsg').blindUp({'duration':.5});
			},
			
			postForm : function (f) {
				f = ($(f).tagName == 'FORM') ? $(f) : $(f).down('form');
				var errors = validateForm(f);
				var fID = f.id;
				
				// ======= extended validation
				switch (fID) {
				case 'whitePaperForm' :
					if (typeof f.contactMe != 'undefined') {
						if (f.contactMe.checked && isEmpty(f.phone.value)) {
							errors.push('Please enter a phone number.');
						}
					}
					
					f.subject.value = eSubject;
					break;
				case 'warehouseMobileForm' :
					if (f.email.value !== f.email2.value) { errors.push('Email addresses do not match.'); }
					break;
				case 'dsdMobileForm' :
					if (f.email.value !== f.email2.value) { errors.push('Email addresses do not match.'); }
					break;
				case 'securityContest':
					if (f.accesscode.value == '946637') { 
						f.action = 'security-contest3a.php'; 
					} else if (f.accesscode.value == '483752') { 
						f.action = 'security-contest3b.php'; 
					} else {
						errors.push('Not a valid code.');
					}
					break;
				default:
					if (typeof f.contactMe != 'undefined') {
						if (f.contactMe.checked && isEmpty(f.phone.value)) {
							errors.push('Please enter a phone number.');
						}
					}
					
					f.subject.value = eSubject;
					break;
				}
				// ======= extended validation
				
				if (errors.length > 0) {
					alertErrors(f,errors);
					return false;
				} else {
					if (typeof f.trackIt.value == 'string') { trackIt(f.trackIt.value); }
					if (typeof oCode == 'string' && oCode != '') { f.action += '?oid=' + oCode; }
					return true;
				}
			},
			
			tog : function (el,tag) {
				var togEL = $(el).next(tag);
				if (togEL.visible()) { togEL.hide(); } else { togEL.show(); }
			}
		}
	}();
	
	return pub;
	
}();
