( function($) {
	$( function() {
		// search form, hide it, search labels to modify, filter classes nocmx and error
		// $('#callUser').hide().find( 'p>label:not(.nocmx):not(.error)' ).each(
		// function() {
		// var $this = $(this);
		// var labelContent = $this.html();
		// var labelWidth ="200px";
		// // create block element with width of label
		// var labelSpan = $("<span>")
		// .css("display", "block")
		// .width(labelWidth)
		// .html(labelContent);
		// // change display to mozilla specific inline-box
		// $this.css("display", "-moz-inline-box")
		// // remove children
		// .empty()
		// // add span element
		// .append(labelSpan);
		// // show form again
		// }).end().show();
	});
	
	$( function() {
		$().ready( function() {
			// validate the comment form when it is submitted
			// validate signup form on keyup and submit
			jQuery.validator.addMethod("alphaNumericCheck", function(value, element) {
				var reg = /^[a-zA-Z0-9]+$/;
				return this.optional(element) || reg.test(value);
			}, " the format is not valid");
			
			jQuery.validator.addMethod("nameCheck", function(value, element) {
				var reg = /^[a-zA-Z '-]+$/;
				return this.optional(element) || reg.test(value);
			}, " the format is not valid");
			
			// avoid submitting from form directly with incorrect parameters
			$("#searchRap").submit(function() {
				return false;
			});
			var validator = $("#searchRap").validate( {
				errorClass: "red",
				onfocusin: function(element) {
					this.lastActive = element;
					// this.settings.unhighlight &&
					// this.settings.unhighlight.call( this, element,
					// this.settings.errorClass );
				  // this.errorsFor(element).hide();
				},
				onfocusout: function(element) {
				},
				onkeyup: function(element) {
				},
				onclick: function(element) {
					//this.settings.unhighlight && this.settings.unhighlight.call( this, element, this.settings.errorClass );
					// this.errorsFor(element).hide();
				},
				highlight: function(element, errorClass) {
					$("#searchRap").find("label[for=" + element.id + "]").css('color', '#F00');
					$("#searchRap").find("label[for=" + element.id + "]").css('display', 'block');
	
					// set bg yellow
					$("#searchRap table").addClass("borderSpacing");
					$("#searchRap").find("label[for=" + element.id + "]").parent().css( {
						background: "#FEF7D0"
					});
				},
				unhighlight: function(element, errorClass) {
					$("#searchRap").find("label[for=" + element.id + "]").css('color', '#746C61');
					$("#searchRap").find("label[for=" + element.id + "]").css('display', 'block');
	
					// reset bg yellow
					$("#searchRap").find("label[for=" + element.id + "]").parent().css( {
						background: "none"
					});
				},
				check: function(element) {
					$("#searchRap").find("label[for=" + element.id + "]").css('display', 'block');
				},
				rules: {
					"firstName": {
						required: true,
						maxlength: 24,
						nameCheck: true
					},
					"lastName": {
						required: true,
						maxlength: 24,
						nameCheck: true
					},
					"zipCode": {
						required: true,
						rangelength: [5, 5],
						digits: true
					}
				},
				messages: {
					"firstName": {
						required: "Please fill in First Name.",
						maxlength: "Please enter valid First Name using letters only.",
						nameCheck: "Please enter valid First Name using letters only."
					},
					"lastName": {
						required: "Please fill in Last Name.",
						maxlength: "Please enter valid Last Name using letters only.",
						nameCheck: "Please enter valid Last Name using letters only."
					},
					"zipCode": {
						required: "Please fill in Zip code.",
						digits: "Please use a 5-digit Zip code.",
						rangelength: "Please use a 5-digit Zip code."
					// phoneCheck :"Phone must be input complete"
					}
				}
			});
			
			function initConnectInnerButton() {
				$("#success-connect-laydown").toggle( function() {
					$("#success-layout-content").css("display", "block");
				}, function() {
					$("#success-layout-content").css("display", "none");
				});
				$("#no-rep-connect-laydown").toggle( function() {
					$("#no-rep-layout-content").css("display", "block");
				}, function() {
					$("#no-rep-layout-content").css("display", "none");
				});
				$("#not-available-connect-laydown").toggle( function() {
					$("#not-available-layout-content").css("display", "block");
				}, function() {
					$("#not-available-layout-content").css("display", "none");
				});
			}
			
			function openRepForm(id) {
				$("#connect .container[id^='rap']").css("display", "none");
				$("#connect #" + id).css("display", "block");
			}
			
			function findRep() {
				var options = {
					success: function(result) {
						if ($("#searchRap #isCall").val() == "false") {
							$("#connect #cancelConnect").css("display", "block");
							$("#connect #callConnect").css("display", "none");
						} else {
							if (result.userCall == true) {
								$("#connect #cancelConnect").css("display", "block");
								$("#connect #callConnect").css("display", "none");
							} else {
								$("#connect #cancelConnect").css("display", "none");
								$("#connect #callConnect").css("display", "block");
							}
						}
			
						if (result.success == false) {
							
							// clear data
							$("#rap-result-zip").html("");
							$("#rap-result-username").html("");
							$("#rap-result-pic").html("");
							$("#rap-result-phone").html("");
							
							if (result.searchSpecialistFormFlag == null || result.searchSpecialistFormFlag == "") {
								
								// target prospect does not exist or service is not available
								openRepForm("rap-not-available");
							} else {
								
								// not found a rep
								openRepForm("rap-no-result");
							}
							return;
						} else {
							
							// find a rep
							var resultData = result.data;
							if (resultData != null) {
								$("#rap-result-zip").html(result.searchSpecialInfo.zipCode);
								$("#rap-result-username").html(resultData.firstName + "&nbsp;" + resultData.lastName);
								$("#rap-result-pic").html("<img src='" + ctx + "/images/specialist/" + resultData.employeeId.string + ".jpg' onerror=\"onImgError(this, '" + ctx + "/images/specialist/no-image-54px.jpg')\" />");
								$("#rap-result-phone").html(resultData.contactInformation.phoneNumber);
							}
							openRepForm("rap-result");
						}

						// reset search rep form
						$("#searchRap").resetForm();
					},
					error: function(xhr, status) {
						if (xhr.status > 200 && xhr.status <= 500) {
							alert("error comminucation.");
						}
					},
					complete: function(xhr, status) {
					},
					dataType: "json"
				};

				$("#searchRap").ajaxSubmit(options);
			}

			// do initialize work
			
			$("#connectShow").click( function() {
				$('#searchRap').find("label").each( function() {
					if ($(this).attr("for") != "") {
						$(this).css("color", "#746C61");
						$(this).css("display", "block");
						if ($(this).attr("class") == "red") {
							$(this).css("display", "none");
						}
					}
				});
				
				// $("#searchRap #zipCode").attr("value","");
				// $("#searchRap #lastName").attr("value","");
				// $("#searchRap #firstName").attr("value","");
				// $("#connectInput").css("display","block");

				// $("#callUserAjax #phoneOne").attr("value","");
				// $("#callUserAjax #phoneSecond").attr("value","");
				// $("#callUserAjax #phoneThird").attr("value","");
				// $("#callUserAjax #firstName").attr("value","");
				// $("#callUserAjax #lastName").attr("value","");
				
				$('#callUserAjax').find("label").each( function() {
					if ($(this).attr("for") != "") {
						$(this).css("color", "#746C61");
						$(this).css("display", "block");
						if ($(this).attr("class") == "red") {
							$(this).css("display", "none");
						}
					}
				});
			});

			$("#searchRapBtn").click( function() {
				if (validator.form()) {
					findRep();
				}
			});
			
			$(".callGoback a[id$='callGoback']").click( function() {
				var options = {
					success: function(result) {
						if (result.success == true) {
							//$("#searchRap").resetForm();
							openRepForm("rap-input");
						}
					},
					error: function(xhr, status) {
						if (xhr.status > 200 && xhr.status <= 500) {
							alert("error comminucation.");
						}
					},
					complete: function(xhr, status) {
					},
					dataType: "json"
				};
				$("#resetSearchSpecialist").ajaxSubmit(options);
			});

			// for enter keyboard submit
			$("#searchRap :input").each( function() {
				$(this).keypress( function(e) {
					var key = window.event ? e.keyCode : e.which;
					if (key.toString() == "13") {
						$("#searchRap #searchRapBtn").click();
					}
				});
			});

			$("#about-laydown-list").toggle( function() {
				$("#personal-layout-list").css("display", "block");
				$("#layout-content-list").css("display", "block");
			}, function() {
				$("#personal-layout-list").css("display", "none");
				$("#layout-content-list").css("display", "none");
			});
			
			initConnectInnerButton();
			
		});
	});
	
})(jQuery);