UnisysV2.enableTooltips = function() {
    $("div.help:has(span)").hover(function(){
        $(this).css("z-index","10");  /* fixes z-index context stacking problems */
        $(this).children("div.tooltip").show();
    },
    function() {
        $(this).css("z-index","1");  /* fixes z-index context stacking problems */
        $(this).children("div.tooltip").hide();
    });
};

UnisysV2.enablePasswordChange = function() {
    /* change password button reveals change pw div */
    $("#changepasswordbutton").click(function(){  
        $("#newpasswordrow").addClass("shown").removeClass("hidden");
        $("#passwordrow > .success").hide();
    });
	
    /* cancel button should clear pw fields and hide pw div */
    $("#cancelpassword").click(function() {  
        $("#newPassword").val('');
        $("#verifyNewPassword").val('');
        $("#newpasswordrow").addClass("hidden").removeClass("shown");
        $("#passwordrow > .success").hide();
        $("#newPassword ~ p.error").hide();
		$("#chgpassword").val('false');
    });
	
    /* submit change pw button should validate passwords, compare them,*/
    /* hide pw div, and show "Successful" div */
    $("#savepassword").click(function() {
        if ($("#newPassword").val() == $("#verifyNewPassword").val()) {  /* they match */
            //var newvalue = $("#newPassword").val();
            //$("#password").val(newvalue);
            //$("#newPassword").val('');
            //$("#verifyNewPassword").val('');
            $("#newpasswordrow").addClass("hidden").removeClass("shown");
            $("#passwordrow > .success").show();
            $("#newPassword ~ p.error").hide();
			$("#chgpassword").val('true');
			
        } else { /* they don't match, show error */
            $("#newPassword ~ p.error").html("Please make sure the passwords match.").show();
        }
    });
	
    /* Make sure that the password row is hidden on page load */
    $("#newpasswordrow").addClass("hidden");
};

UnisysV2.enableIndustryOther = function() {
	/* Make sure that the industry-other row is hidden on page load if it's blank */
    if ((($("#otherindustry").val() == null) || ($("#otherindustry").val() == "")) && ($("#industry").val() != 2)) {
    	$("#industry-other").addClass("hidden");
    } else {
    	$("#industry-other").addClass("shown");
    };
    
    /* Industry dropdown reveals other div if "other" selected */
    $("#industry").change(function(){ 
        if ($("#industry").val() == 100016) {
        	if ($("#industry-other").hasClass("hidden")) {
        		$("#industry-other").addClass("shown").removeClass("hidden");
        	};
        } else { /* and nulls and hides it if they change their mind and select something else */
        	if (($("#otherindustry").val() != null) && ($("#otherindustry").val() != "")) {
        		$("#otherindustry").val('');
        	};
        	if ($("#industry-other").hasClass("shown")) {
        		$("#industry-other").addClass("hidden").removeClass("shown");
        	};
        };
    });
};

UnisysV2.enableValidation = function() {
	// fix Google Toolbar issues
	$("input.remove-title").attr("title", "");
	
	$.validator.setDefaults({ 
		errorPlacement: function(error, element) {
			error.appendTo(element.parent("div"));   
		},
		highlight: function(element, errorClass) {
			// we're going to use "focus" instead of "error" in this case
			$(element).parent("div").addClass("focus");
		},
		unhighlight: function(element, errorClass) {
			$(element).parent("div").removeClass("focus");
		},
		rules: {
			firstname: {
                required: true,
                maxlength: 50
            },
            lastname: {
                required: true,
                maxlength: 50
            },
			jobfunction: "required",
			organization: {
				required: true,
				maxlength: 254
			},
			jobTitle: { 
				maxlength: 100
			},
			addressone: "required",
			city: "required",
			state: "required",
			zipcode: "required",
			country: "required",
			mysubject: "required",
			industry: "required",
            salutation: {
                maxlength: 50
            },
			emailaddress: {
                required: true,
                maxlength: 50
            },
			confirmemailaddress: {
				required: true,
				equalTo: "#emailaddress"
			},
			loginemailaddress: "required",
			password: {
				required: true,
				minlength: 6
			},
			loginpassword: {
				required: true,
				minlength: 6
			},
			confirmpassword: {
		    equalTo: "#password",
				required: true,
				minlength: 6
			},
			oldpassword: {
				required: true,
				minlength: 6
			},
			newpassword: {
				required: true,
				minlength: 6
			},
			confirmnewpassword: {
				equalTo: "#newpassword",
				required: true,
				minlength: 6
			},
			phonenumber: {
				required: true
			},
			confirmphonenumber: {
				equalTo: "#phonenumber",
				required: true
			},
			comments: {
				required: true,
				maxlength: 1000
			},
			modeofcontact: {
				required: true
			},
			yourname: {
				required: true,
				maxlength: 50
			},
			youremail: {
				required: true,
				maxlength: 50
			},
			receiversemail: {
				required: true,
				maxlength: 50
			}
		},
		messages: {
			firstname: {
                required: "Please enter your first name.",
                maxlength: "Please limit to 50 characters."
            },
			lastname: {
                required: "Please enter your last name.",
                maxlength: "Please limit to 50 characters."
            },
            salutation: "Please limit to 50 characters.",
			jobfunction: "Please select a job function.",
			organization: {
				required: "Please enter your organization.",
                maxlength: "Please limit to 254 characters."
			},
			addressone: "Please enter your address.",
			city: "Please enter your city.",
			state: "Please enter your state.",
			zipcode: "Please enter your postal code.",
			country: "Please select a country.",
			mysubject: "Please enter a subject.",
			subject: "Please enter a subject.",
			industry: "Please select an industry.",
			emailaddress: {
                required: "Please enter your email address.",
                maxlength: "Please limit to 50 characters."
            },
			confirmemailaddress: {
				required: "Please enter your email address.",
				equalTo: "Email addresses entered do not match."
			},
			loginemailaddress: "Please enter your email address.",
			password: {
				required: "Please enter a password.",
				minlength: "Password must be at least 6 characters."
			},
			loginpassword: {
				required: "Please enter your password.",
				minlength: "Password must be at least 6 characters."
			},
			confirmpassword: {
				required: "Please enter your password.",
				equalTo: "Passwords entered do not match."
			},
			oldpassword: {
				required: "Please enter your temporary password.",
				minlength: "Password must be at least 6 characters."
			},
			newpassword: {
				required: "Please enter your new password.",
				minlength: "Password must be at least 6 characters."
			},
			confirmnewpassword: {
				equalTo: "Passwords entered do not match.",
				required: "Please verify your new password.",
				minlength: "Password must be at least 6 characters."
			},
			phonenumber: "Please enter your phone number.",
			confirmphonenumber: {
				equalTo: "Phone numbers entered do not match.",
				required: "Please enter your phone number."
			},
			comments: {
				required: "Please enter your comment.",
				maxlength: "Comments must be 1000 characters or less."
			},
			modeofcontact: {
				required: "Please select a mode of contact."
			},
			yourname: {
				required: "Please enter your name."
			},
			youremail: {
				required: "Please enter your email address."
			},
			receiversemail: {
				required: "Please enter the recipient email address."
			},
			jobTitle: {
				maxlength: "Job titles are limited to 100 characters."
			},
			contactreason: "Please select a reason for contact."
		},
		groups: {
			contactType: "emailcontact phonecontact"
		},
		success: "form-success",
		ignoreTitle: true//,
		//debug: true
	});
};

UnisysV2.enableCollapsibleTree = function() {
	//Common variables
	var sub_list = $('ul.collapsible-tree li > ul');
	var parent_li = $(sub_list).parent('li');
	var interest_divs = $(".interests-detail-div");
	
	//Hiding functions
	function collapseAllTreeNodes() {
		parent_li.addClass('collapsed-list');
		sub_list.hide();
	}
	
	function hideAllDetails() {
		interest_divs.hide();
	}

	// Prep the list, collapse everything in it 
    parent_li.addClass('parent-node');
	parent_li.addClass('collapsed-list');
	sub_list.hide();

	
	//Reveal the first item in the list
	$('ul.collapsible-tree li:first-child > ul').each(function(i) {
		$(this).parent('li').removeClass('collapsed-list');
		$(this).show();
	});

	// When clicking on the tree nodes...
	$('ul.collapsible-tree li').click(function() {
		
		//first, check to make sure it's not already expanded
		if ($(this).hasClass("collapsed-list")) {
		
			var whichInterest = $(this).attr("id");

			//define interest detail divs
			var industryDetails = $("#interest-products");
			var businessDriverDetails = $("#interest-business-drivers");
			var productDetails = $("#interest-products");

			var myList = $(this).children("ul");
			collapseAllTreeNodes();
	        myList.show();
			$(this).removeClass('collapsed-list');

			hideAllDetails();

			switch (whichInterest) {
				case "industry":
					industryDetails.show();
					break;
				case "business-drivers":
					businessDriverDetails.show();
					break;
				case "products":
					productDetails.show();
					break;
				default:
					productDetails.show();
					break;
			};	
		};
    }); 
} 
