// JavaScript Document
$(function() {
	var phoneMask = "(999) 999-9999? x99999"; 

	$("#txtPhone").mask(phoneMask);

	$("#cboCountry").change(function(){
		var country = $(this).val();

		if (country=="United States of America") {
			$("#txtPhone").mask(phoneMask);
		} else {
			$("#txtPhone").unmask(phoneMask);
		}
	});

	// add * to required field labels
	$('label.required').append('&nbsp;<font color="#990000"><b>*</b></font>&nbsp;');

	$.tools.validator.fn("[data-equals]", "Value not equal with the $1 field", function(input) {
		var name = input.attr("data-equals"),
			field = this.getInputs().filter("[name=" + name + "]"); 
		return input.val() == field.val() ? true : [name]; 
	});

	$.tools.validator.fn("[minlength]", function(input, value) {
		var min = input.attr("minlength");

		return value.length >= min ? true : {     
			en: "Please provide at least " +min+ " character" + (min > 1 ? "s" : ""),
			fi: "Kentän minimipituus on " +min+ " merkkiä" 
		};
	});

	$.tools.validator.fn("[doctype]", "Please choose a file with an allowed extensions", function(input, value) {
		var option = input.attr("doctype");

		if($(":file").val() != ""){
			/* if the input is not empty, then we need to check the extension check */
			return /.csv$|.doc$|.pdf$|.pps$|.ppt$|.txt$|.xls$/.test(value);
		}else{
			return option.toLowerCase() == 'required' ? false : true; 
		}
	});

	$.tools.validator.fn("[imgtype]", "Please choose a file with an allowed extensions", function(input, value) {
		var option = input.attr("imgtype");

		if($(":file").val() != ""){
			/* if the input is not empty, then we need to check the extension check */
			return /.gif$|.jpg$|.jpeg$|.png$|.wbmp$/.test(value);
		}else{
			return option.toLowerCase() == 'required' ? false : true; 
		}
	});

	$('input[type=radio][required]').click(function(){
	  var req = $(this).attr('name') + '_required';
	  $('#' + req).attr('checked', true);
	  $('#' + req).change();
	});

	$("#entryForm").bind("onFail", function(e, errors)  {
		// we are only doing stuff when the form is submitted
		if (e.originalEvent.type == 'submit') {
			// loop through Error objects and add the border color
			$.each(errors, function()  {
				var input = this.input;
				input.css({borderColor: 'red'}).focus(function()  {
					input.css({borderColor: '#444'});
				});
			});
		}
	});

	// adds an effect called "wall" to the validator
	$.tools.validator.addEffect("wall", function(errors, event) {
		// get the message wall
		var wall = $(this.getConf().container).fadeIn();
		// remove all existing messages
		wall.find("p").remove();
		// add new ones
		$.each(errors, function(index, error) {
			wall.append(
				"<p><strong>" +error.input.attr("title")+ "</strong>: &nbsp; <em>" +error.messages[0]+ "</em></p>"
			);		
		});
	// the effect does nothing when all inputs are valid	
	}, function(inputs)  {
		
	});

	$("#entryForm").validator({ 
		effect: 'wall', 
		container: '#errorz',
		// do not validate inputs when they are edited
		errorInputEvent: null

	}).submit(function(e) {
		var form = $(this);
		// client-side validation NOT OK.
		if (e.isDefaultPrevented()) {
			$('html,body').animate({scrollTop: 0},'slow');
		}
	});

});

