// service-centers.js

contactUs = ({
	valid: true,
	msg: "",
	initialize: function(){
		// remove faces inline js for validation purposes
		var inp = $(".promo-fill input")[0];
		var rep = "<input id=\"_id32:saveAndSendMail\" class=\"search-button\" type=\"submit\" value=\"Submit\" name=\"_id32:saveAndSendMail\" />"
		var oldClick = "onclick=\"if(typeof window.clearFormHiddenParams__id32!='undefined'){clearFormHiddenParams__id32('_id32');}\"";
		
		$(".promo-fill input").replaceWith(rep);
		
		$(".promo-fill input").mousedown(function(ev){
			ev.preventDefault();
			contactUs.validate();
		});
	},
	validate: function(){
		var form = $("#_id32");
		var submit = $(".promo-fill input");
		var error = $("#error");
		
		var required = $(".required-field");
		var labels = $(".required");
		
		$(submit).parent().parent().css({opacity:0.5});
		$(submit).attr("disabled",true);
		
		for(i=0;i<required.length;i++){
			contactUs.valid = true;
			switch(required[i].type){
				case "text":
					if($(required[i]).hasClass("email")){
						contactUs.valid = contactUs.validateEmail(required[i]);
						if(contactUs.valid != true){
							contactUs.showError();
							break;
						}
						
					}
					
					if(contactUs.msg.length == 0){
						contactUs.valid = contactUs.validateEmpty(required[i]);
						if(contactUs.valid == false){
							for(j=0;j<required.length;j++){
								if(required[i] == required[j]){
									var pos = j;
								}
							}
							
							contactUs.msg = "<strong>Oops!</strong> Please enter a value for " + $(labels[pos]).text();
							contactUs.msg = contactUs.msg.substring(0,contactUs.msg.length-2);
							contactUs.msg += ".";
							contactUs.showError();
						}
					
					}
					
				break;
				case "select-one":
					contactUs.valid = contactUs.validateSelect(required[i]);
					if(contactUs.valid != true){
						contactUs.showError();
					}
				break;
				case "textarea":
					contactUs.valid = contactUs.validateTextArea(required[i]);
					if(contactUs.valid != true){
						contactUs.showError();
					}
				break;
				case "checkbox":
					contactUs.valid = contactUs.validateCheckbox(required[i]);
					if(contactUs.valid != true){
						contactUs.showError();
					}
				break;
				default:
				
				break;
			}
			
			if(contactUs.valid != true){
				$(required[i]).focus();

      		$(submit).parent().parent().css({opacity:1.0});
      		$(submit).attr("disabled",false);

				return false;
				break;
			}
			
		}


		if(contactUs.valid == true){
			// restore faces inline js so we can submit the form
   		$(submit).attr("disabled",false);
			$(".promo-fill input").click(function(){
				if(typeof window.clearFormHiddenParams__id32!='undefined'){clearFormHiddenParams__id32('_id32');}
			});
			$(".promo-fill input").trigger("click");
		}
		
	},
	validateEmpty: function(el){
		if($(el).attr("value").length > 0){
			contactUs.msg = "";
			return true;
		}else{
			return false;
		}
		
	},
	validateEmail: function(el){
		var emails = $(".email");
		var validEmails = true;
		
		var email1 = $(".mEmail");
		var email2 = $(".cEmail");
		
		var isEmail_re = /^\s*[\w\-\+_]+(\.[\w\-\+_]+)*\@[\w\-\+_]+\.[\w\-\+_]+(\.[\w\-\+_]+)*\s*$/;
		if($(el).attr("value").search(isEmail_re) == -1){
			validEmails = false;
			contactUs.msg = "<strong>Oops!</strong> That doesn't look like a valid email address - can you check it for any typos?"; 
		}

		if(validEmails == true){
			if(email1.attr("value") != email2.attr("value")){
				validEmails = false;
				contactUs.msg = "<strong>Oops!</strong> Those two email addresses don't appear to match - can you check to see that the same email address is in both fields?"; 
			}

		}
		return validEmails;
		
	},
	validateSelect: function(el){
		if($("option:selected",el).attr("value") == "Select"){
			contactUs.msg = "<strong>Oops!</strong> Can you please select a feedback type?";
			return false;
		}else{
			return true;
		}
		
	},
	validateTextArea: function(el){
		if($(el).attr("value").length == 0){
			contactUs.msg = "<strong>Oops!</strong> It doesn't look like you've entered any comments - can you please enter one?"; 
			return false;
		}else{
			return true;
		}
		
	},
	validateCheckbox: function(el){
		if($(el).attr("checked") == false){
			contactUs.msg = "<strong>Oops!</strong> We need to know if you're at least 13 years old."; 
			return false;
		}else{
			return true;
		}
	},
	showError: function(){
		var error = $("#error");
		$('#error span').html(contactUs.msg);
		error.animate({
			opacity: 1
		});
		contactUs.msg = "";
		
	}
});