/*
 * Macquarie MGL/AU Utilities JavaScript for Advisers portal
 *
 * http://macquarie.com.au
 *
 * Copyright (c) 2010 - Macquarie Bank
 */

/* Link for "email to a friend" dialog box */
pageToolsEmailLink="/mgl/au/advisers/email-page?height=500&amp;width=560&amp;modal=true";



/* JQuery utility functions */
function showFormErrorCount(errorMap, errorList) {
	if (this.numberOfInvalids() > 0) {
		$("#errorSummary").html("Please correct the " + this.numberOfInvalids() + " errors described above");
	} else {
		$("#errorSummary").html("&nbsp;");
	}
	this.defaultShowErrors();
}

function validateQuizAnswerCallback(value, element, answer) {
	var selectedAnswer = $("input:radio[name='" + element.name + "']:checked").val();
	return this.optional(element) || (selectedAnswer == answer);
}
jQuery.validator.addMethod("quiz", validateQuizAnswerCallback, "This answer is wrong");

function validateReplaceDefaultValue(value, element, defaultText) {
	return this.optional(element) || (defaultText != element.value);
}
jQuery.validator.addMethod("nodefault", validateReplaceDefaultValue, "The default value needs to be updated");



/* Set JQuery defaults */
jQuery.validator.setDefaults({
	/* Summary error message */
	showErrors: showFormErrorCount
	/* --- Disabled error placement ---
	success: function(label) {
		label.text("&nbsp;").addClass("success");
	},
	errorPlacement: function(error,element) {
		if (element.parents("td").find(".errorWrapper").size() == 0) {
			if (element.parents("td").size() > 1) {
				element.parents("td:first").parents("td:first").wrapInner('<li class="labelElement" />').wrapInner('<ul class="errorWrapper" />');
			} else {
				if (element.parents("td:first").children(":first").is("div")) {
					element.parents("td:first").children(":first").wrapInner('<li class="labelElement" />').wrapInner('<ul class="errorWrapper" />');
				} else {
					element.parents("td:first").wrapInner('<li class="labelElement" />').wrapInner('<ul class="errorWrapper" />');
				}
			}
			element.parents("ul:first").append('<li class="errorElement"></li>');
		}
		element.parents("ul:first").children("li.errorElement").html(error.html());
	}
	--- */
});


/* Standard contact form */
var contactFormValidator;

function initialiseContactForm() {
    contactFormValidator = $("#contactForm").validate({
        rules: {
			/* Standard captcha fields */
            captchaID: "required",
			
			/* Standard contact details fields (abbreviated) */
            name: "required",
			
            phoneNumber: "required",
            emailAddress: {
                required: true,
                email: true
            },
			
			/* Message */
            subject: "required",
            message: "required"
        },
        messages: {
			/* Standard captcha fields */
            captchaID: "required",
			
			/* Standard contact details fields (abbreviated) */
            name: "Please enter your name",
			
            phoneNumber: "Please enter your phone number",
            emailAddress: {
                required: "Please enter your email address",
                minlength: "Please enter a valid email address"
            },
			
			/* Message */
			subject: "Please briefly describe the subject of your inquiry",
			message: "Please include a message detailing your inquiry"
        }
    });
}

function resetContactForm(){
	document.getElementById('contactForm').reset();
	if (contactFormValidator) {
		contactFormValidator.resetForm();
	}
	$("#errorSummary").html("&nbsp;");
}

function validateContactForm(){
	// We need to lazily load the validator, since onload wont work with a lightbox
	if (! contactFormValidator) {
		initialiseContactForm();
	}
	
	if (contactFormValidator.form()) {
		// This will submit the form once the captcha has been validated
		formValidationUtils.ajaxValidateCaptcha("contactForm","captchaImage","captchaID");
	}
}



/* Form to email page to friend */
var emailPageFormValidator;

function initialiseEmailPageForm() {
	/* Initialise validator */
    emailPageFormValidator = $("#emailPageForm").validate({
        rules: {
			/* Standard captcha fields */
            captchaID: "required",
			
			/* Message details fields */
			userName: {
				required: true,
				nodefault: "Your name"
			},
			userEmail: {
                required: true,
				nodefault: "Your email address",
                email: true
            },
			recipientName: {
				nodefault: "Recipient's name",
				required: true
			},
			recipientEmail: {
                required: true,
				nodefault: "Recipient's email address",
                email: true
            },
			// message: "optional",
			
			/* Legal check fields */
			termsOfUse: "required",
			recipientConsent: "required"
			
        },
        messages: {
			/* Standard captcha fields */
            captchaID: "required",
			
			/* Message details fields */
			userName: {
				required: "Please enter your name",
				nodefault: "Please enter your name"
			},
			userEmail: {
                required: "Please enter your email address",
				nodefault: "Please enter your email address",
                minlength: "Please enter a valid email address"
            },
			recipientName: {
				required: "Please enter the recipient's name",
				nodefault: "Please enter the recipient's name"
			},
			recipientEmail: {
                required: "Please enter the recipient's email address",
                nodefault: "Please enter the recipient's email address",
                minlength: "Please enter a valid email address"
            },
			
			/* Legal check fields */
			recipientConsent: "You must obtain consent from the recipient in order to use this service"
 		},
		errorPlacement: function(error, element) {
			error.appendTo(element.parent().parent());
        }
    });
	
	/* Set page information */
	var pageUrl = location.href;
	var pageTitle = $("title").html();
	if (pageTitle && jQuery.trim(pageTitle) != "") {
		pageTitle = jQuery.trim(pageTitle);
	} else {
		pageTitle = "Macquarie Adviser Services";
	}
	
	$("#pageTitle").val(pageTitle);
	$("#pageUrl").val(pageUrl);
}

function resetEmailPageForm(){
	document.getElementById('emailPageForm').reset();
	if (emailPageFormValidator) {
		emailPageFormValidator.resetForm();
	}
	$("#errorSummary").html("&nbsp;");
}

function validateEmailPageForm(){
	// We need to lazily load the validator, since onload wont work with a lightbox
	if (! emailPageFormValidator) {
		initialiseEmailPageForm();
	}
	
	if (emailPageFormValidator.form()) {
		// This will submit the form once the captcha has been validated
		formValidationUtils.ajaxValidateCaptcha("emailPageForm","captchaImage","captchaID");
	}
}

function initPageTools(){$(".VC-pagetools").html('<a class="emailPage thickbox" href="' + pageToolsEmailLink + '"><img class="tooltip" src="/dafiles/Internet/mgl/au/furniture/images/icons/pagetool_email_icon.gif" alt="Email icon" title="Email page"/></a><a class="printPage" href=""><img class="tooltip" src="/dafiles/Internet/mgl/au/furniture/images/icons/pagetool_print_icon.gif"alt="Printer icon"title="Print page"/></a>');}

