// CPEP extensions to the jQuery validation plugin
// http://docs.jquery.com/Plugins/Validation/

$(document).ready(function(){
	$(".trim").change(function(event){
		$(this).val($.trim($(this).val()));
	    });


	var validator = $("form:not(.noValidate)").validate({ // Validate all forms by default
		errorClass: 'inputValidationError',

		success: function(label) {
		    // IE needs a space to render the label
		    label.addClass('inputValidationSuccess').html(' <br/>');
		},

		errorPlacement: function(error, element) {
		    if (element.is(".xinha_textarea")) {
			error.insertBefore(element.parent().parent().parent().parent());
			error.attr('style', 'margin-left: 0;');
			element.parent().parent().parent().parent().attr('style', 'margin-top: 2px;');
		    } else if (element.is(".errorBefore")) {
			error.insertBefore(element);
			error.attr('style', 'margin-left: 0;');
		    } else if (element.is(".errorBeforeParent")) {
			error.insertBefore(element.parent());
			error.attr('style', 'margin-left: 0;');
		    } else {
			error.insertAfter(element);
		    }
		}
	    }); 

	if (typeof validator != 'undefined') validator.focusInvalid = function() {
		    // put focus on xinha on submit validation

		    if(this.settings.focusInvalid) {
			try {
			    var toFocus = $(this.findLastActive() || this.errorList.length && this.errorList[0].element || []);
			    if (toFocus.is("textarea")) {
				if (toFocus.is(".xinha_textarea")) {
				    $('#'+toFocus.attr('id')).attr('style', 'display: block;');
				    $('#'+toFocus.attr('id')).focus();
				    $('#'+toFocus.attr('id')).attr('style', 'display: none;');
				    iLabelTop = parseInt($('#'+toFocus.attr('id')).parent().parent().parent().parent().prev('label').position().top);
				} else {
				    $('#'+toFocus.attr('id')).focus();
				    iLabelTop = parseInt($('#'+toFocus.attr('id')).parent().prev('label').position().top);
				}

				window.scrollTo(0, iLabelTop-36);
			    } else {
				$('#'+toFocus.attr('id')).focus();
			    }
			} catch(e) {
			    // ignore IE throwing errors when focusing hidden elements
			}
		    }
		}

   });

///////////////////////////////////////////////////////////////////////////////
// Validator methods

// Override default 'required' method to cope with xinha_textareas
$.validator.methods._required = $.validator.methods.required;

$.validator.addMethod("required", function(value, element, param) {
	regexp = new RegExp("xinha_textarea");

        if (!regexp.test(element.className)) return $.validator.methods._required.call(this, value, element, param);

	// Special case for xinha_textareas
	element.value = $.trim(value);

	sContent = element.value;
	if (xinhaIsInitialised) {
	    sContent = $("#"+element.id).prev().contents().find('body').html();
	    sContent = sContent.replace(/<(.|\n)*?>/g, '');
	    sContent = sContent.replace(/&nbsp;/g, ' ');
	    sContent = $.trim(sContent);
	}

	return sContent.length > 0;

    }, jQuery.validator.messages.required+"<br />"); // use default message

// Validate strict reference
$.validator.addMethod("strict-reference", function(value, element) {
	value = $.trim(value);
 	return this.optional(element) || /^[\w-]*$/.test(value);
    }, 'The reference may only contain letters, numbers, underscores and hyphens');

// Validate APR
$.validator.addMethod("apr", function(value, element){
	value = $.trim(value);
	return this.optional(element) || /^\d*\.\d$/.test(value);
    }, 'This value should a number entered to 1 decimal place');

// Validate currency
$.validator.addMethod("currency", function(value, element){
	value = $.trim(value);
	return this.optional(element) || /^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)(\.[0-9]{2})?$/.test(value);
    }, 'This value should a number entered to 2 decimal places');

// Validate mileage
$.validator.addMethod("mileage", function(value, element){
	value = $.trim(value);
	value = value.replace(/[^\d^\.]/g, '');

	if (value.length !=5) return false;

	return this.optional(element) || /^([0-9]+|[0-9]{1,3}(,[0-9]{3})*)?$/.test(value);
    }, 'Mileage should be 5 figures, with no decimal places');

$.validator.addMethod("gt0", function(value, element) {
    return this.optional(element) || (parseFloat(value) > 0);
}, "This must be greater than zero");

// Unique value check
$.validator.addMethod("unique", function(value, element){
	// This function checks if a field value has already been used by 
	// comparing it to the aUsed/aUsed[element.name] array
	// Also works with arrays of objects (checking the textNode property)

	value = $.trim(value);
	value = value.toLowerCase();

	if(typeof aUsed === 'undefined' || !aUsed.length) { return true; }

        aThisUsed = (typeof aUsed[0] == 'undefined') ? aUsed[element.name] : aUsed;

	if (typeof sCurrent == 'object') {
	    sThisCurrent = sCurrent[element.name];
	} else if (typeof sCurrent == 'string') {
	    sThisCurrent = sCurrent;
	} else {
	    sThisCurrent = '';
	}

	// We need to replace &quot; with \" to do our testing, as the 
	// sCurrent uses HTML character entities not javascript escaping.
	if (sThisCurrent) {
	    rExpression = /&quot;/gi;
	    sThisCurrent = sThisCurrent.replace(rExpression,'\"').toLowerCase();
	    if (value == sThisCurrent) return true;
	}

	//  Now test to see if the reference has been used.
	checkJSON = (typeof aThisUsed[0].textNode == 'undefined') ?  0 : 1;
	for (var i=0; i<aThisUsed.length; i++) {
	    if(checkJSON) {
		if($.trim(aThisUsed[i].textNode.toLowerCase()) == value) {
		    return false;
		}
	    } else {
		if( $.trim(aThisUsed[i][1].toLowerCase()) == value){
		    return false;
		}
	    }
	}
	return true;
    
    }, 'This value has already been used. Please choose another.');

///////////////////////////////////////////////////////////////////////////////// Formatting


// Format mileage
(function ($) {
  $.fn.formatMileage = function () {
      return this.each(function () {
	      $(this).formatCurrency({ 
		      symbol: '',
		      roundToDecimalPlace: 0,
		      negativeFormat: '-%s%n'
		  });
	  });
  };
})(jQuery);

