$(document).ready(function() {
	// validate signup form on keyup and submit
	var validator = $("#FormParam").validate({
		rules: {				
		UserTypeCd: {
		required: true
	},
	Nm: {
		required: true,		
		validChars:true
	},	
	Email: {
		required: true,
		email: true
	},
	ContactType: {
		required: true
	},
	Subject: {
		required: true
	},
	security_code: {
		required: true
	}			
	},
	messages: {			
		UserTypeCd: {
		required: " "
	},
	Nm: {
		required: " ",
		validChars: " "
	},	
	Email: {
		required: " ",
		email: " " 
	},
	ContactType: {
		required: " "
	},
	Subject: {
		required: " "		 
	},
    security_code: {
		required: " "
	}	 			
	},
	// the errorPlacement has to take the table layout into account
	errorPlacement: function(error, element) {
		if ( element.is(":radio") )
			error.appendTo( element.parent().next().next() );
		else if ( element.is(":checkbox") )
			error.appendTo ( element.next() );
		else
			error.appendTo( element.parent().next() );
	},
	// specifying a submitHandler prevents the default submit
	submitHandler: function() {
		if ( $("#FormParam").validate().form()) {				
		
			var errorMsg = $('#errorMsg');
			var params = 'security_code=' + $("#security_code").val();
			
			errorMsg.removeClass('error').html('<img src="/images/loading.gif" height="16" width="16" /> <span  class="clsAjax">&nbsp;Please wait...</span>');

				if (this.timer) clearTimeout(this.timer);
                
                this.timer = setTimeout(function () {
                    $.ajax({
                        url: '/captcha/check.php',
                        data: params,
                        dataType: 'json',
                        type: 'post',
                        success: function (data) {
                        	if (data.success) {
				    	$("#errorMsg").addClass('success').html(data.message).fadeIn('slow');
				     	document.FormParam.submit();                         
				} else {
		                        new_captcha();
					$("#errorMsg").addClass('error').html(data.message).fadeIn('slow');				
	                        }
                        }
                    });
                }, 250);
                
			return false;
		}	
	},
	// set this class to error-labels to indicate valid fields
	success: function(label) {
		// set &nbsp; as text for IE
		label.html("&nbsp;").addClass("checked");
	}
	});

	$.validator.addMethod('validChars', function (value) {

		var result = true;
		// unwanted characters
		var iChars = "!@#$%^&*()+=-[]\\\';,./{}|\":<>?";

		for (var i = 0; i < value.length; i++) {

			if (iChars.indexOf(value.charAt(i)) != -1) {
				return false;
			}

		}

		return result;

	}, '');

});

function Dissapear(){
	if ( $("#autoDis").length > 0 ) {
		if (jQuery.browser.msie) { 
			$("#autoDis").remove(); 
		} else {
		 $("#autoDis").animate({ backgroundColor: "#fbc7c7" }, "fast").animate({ opacity: "hide" }, "slow");
		}
	}	
}
 
function onCancelClick()
{
	window.location.href = 'index.php';
}


maxKeys = 250;
var IE = (document.all) ? 1 : 0;
var DOM = 0; 
if (parseInt(navigator.appVersion) >=5) {DOM=1};

function ShowTextCount( showCharLeft ) {
    
    // Detect Browser

    if (DOM) {
    	var viewer = document.getElementById("showCounter");
        viewer.innerHTML=showCharLeft;
    }
    else if(IE) {
        document.all["showCounter"].innerHTML=showCharLeft;
    }
}

function keyup(_textarea) 
{
var str = new String(_textarea.value);
var len = str.length;
var charsLeft = maxKeys - len + ' characters left';
if (len > maxKeys) charsLeft += '<br>characters more than ' + maxKeys + ' will be truncated.';
ShowTextCount( '&nbsp;' + charsLeft );
}
