// mgen.js


// omniture code:
// look familiar?
/*
bubbleSequentialEvent(" var variables = [ {name: 'pageName', value: 'B&P: Select Color'}, {name: 'events', value: 'scAdd'}, {name: 'products', value: ';" + myCar.modelName + "'}, {name: 'zip', value: '" + sessionZipCode + "'} ]")
*/
//here is the list of omniture variables that have to be reset each time:
var omniResetList = new Array();
//omniResetList[omniResetList.length] = "s.eVar1"; // for example

//a "log" for errors that can be queried for the current page:
var omniErrors = new Array();

function resetVariables() {
	//cycle through the list and reset variables
	for(var i = 0; i < omniResetList.length; i++) {
		var strRef = omniResetList[i];
		var strReset = strRef + " = '' ";
		//alert("reset string: " + strReset);
		try {
			eval(strReset);
		}
		catch(e) {
			omniErrors.push(e.message + "; *" + strReset + "*");
		}

	}
}

function bubbleMGENEvent(json) {
	//evaluate the json
	eval(json);
	for(var i=0; i < variables.length; i++) {
		//get a reference to this name/value pair object:
		var refObj = variables[i];
		var strOmniVar = CONST_OBJ_PREFIX + refObj.name;
		var strOmniVal = refObj.value;
		var strOmniExpression = strOmniVar + "= '" + strOmniVal + "'";
		//alert(strOmniExpression);
		try {
			eval(strOmniExpression);
		}
		catch(e) {
			omniErrors.push(e.message + "; *" + strOmniExpression + "*");
		}
	}
	//fire analytics event:
	try { s.t(); } catch (e) {}
}

    var regexps = {
	    "firstName" : "^[a-zA-Z\-]+$",
		"lastName" : "^[a-zA-Z\-]+$",
		"email" : "^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z]{2,4})+$",
		"zip" : "^[0-9]{5}(\-[0-9]+)*$"
	}
	
	$(document).ready ( function () {
	    $('#register input[type=text]').focus ( function () {
		    $(this).val('');
		});
	
		$('#submit').click ( function () {
		    
		    var valid = true;
			$('#register input[type=text]').each ( function (idx) {
			    if ($(this).val().search(regexps[$(this).attr('name')]) == -1) {
				    valid = false;
					return false;
				}
			});
			if (valid) {
			    $.each ($('#register input[type=text]'), function () {
					$(this).css ('backgroundColor', '');
				});
				var url = $('#register').attr('action');
				/*
				$.ajax ( {
				    url: $('#register').attr('action'), 
				    type: "POST", 
					data: $('#register').serialize(), 
				    error: function (XMLHttpRequest, textStatus, errorThrown) {
					    $('#messageDiv').html ('There was an error. Please try again later.');
						$('#messageDiv').css ('visibility', 'visible');
					}, 
				    success: function (dataBack, textStatus, XMLHttpRequest) {
						$('#messageDiv').html ('Thank you for your submission!');
						$('#messageDiv').css ('visibility', 'visible');
					}  
				});
				
				*/
				
				$.post(url, $('#register').serialize(), function (data) {	
                    
					if (data.success == 'true') {
						// display thank you
						$('#messageDiv').html ('Thank you for your submission!');
						//$('#messageDiv').css ('color', '');
						$('#messageDiv').css ('visibility', 'visible');
						$('#registerFields').css ('visibility', 'hidden');
										
					} else {
						if (data.fields != undefined) {
							// validation failed
							$.each (data.fields.split(','), function (idx, field) {
								$('#register input[name=' + field + ']').css ('backgroundColor', '#fee398');
							});
							$('#messageDiv').html ('Please check the highlighted fields above and re-submit.');
							//$('#messageDiv').css ('color', '#ff1600');
							$('#messageDiv').css ('visibility', 'visible');
						} else {
							// some kind of error
							$('#messageDiv').html (data.error);
							//$('#messageDiv').css ('color', '#ccc');
							$('#messageDiv').css ('visibility', 'visible');
						}
					}
					
				}, 'json');
				
				
			} else {
			    $.each ($('#register input[type=text]'), function () {
					$(this).css ('backgroundColor', '#fee398');
				});
				$('#messageDiv').html ('Please check the highlighted fields above and re-submit.');
			}
			$('#messageDiv').css('visibility', 'visible');
			return false;
		});
	});
