// JavaScript Document
var displayForm = false;
var refreshCaptcha = false;

// Captcha
var borderColor = ""; /* border color hex or left it null if you don't want to change border color*/
var captchaDir = "/jquery/captcha" /* path to captcha files (if you use domain www.example.com path should present all subfolders after that, start with "/") */
var url = captchaDir + "/process2.php" /* this is name of form action */
var formId = "investForm" /* id of your form */
var items = Array("pencil", "scissors", "clock", "heart", "note"); 

jQuery(document).ready(function(){
	var options = { 
    	target: '#resultTarget', // target identifies the element(s) to update with the server response 
		clearForm: false,		 // clear all form fields after successful submit
		url: 'process2.php',		 // override for form's 'action' attribute  
		success: showResponse	 // post-submit callback 
	}; 

	jQuery('#resultTarget').hide();
	jQuery("#txtPhone").mask("(999) 999-9999");
	jQuery("#txtFax").mask("(999) 999-9999");
	jQuery('#capcha').captcha();

	// add * to required field labels
	jQuery('label.required').append('&nbsp;<font color="#990000"><b>*</b></font>&nbsp;');
	
	jQuery.validator.addMethod("pageRequired", function(value, element) {
		var $element = jQuery(element)
		return !this.optional(element);
	}, jQuery.validator.messages.required)

	// show a simple loading indicator
	var loader = jQuery('<div id="loader" style="background-color:#000000;color:#ffffff;padding:10;border: 1px solid #000;z-index: 99;">&nbsp;&nbsp;Processing...<br/><img src="/VPanel/images/loader.gif" alt="loading..." /></div>')
		.css({position: "absolute", top: "25em", left: "35em"})
		.appendTo("body")
		.hide();
	jQuery().ajaxStart(function() {
		loader.show();
	}).ajaxStop(function() {
		loader.hide();
	}).ajaxError(function(a, b, e) {
		throw e;
	});

	var v = jQuery("#investForm").validate({
		errorClass: "warning",
		onkeyup: false,
		onblur: false,
		submitHandler: function(form) {
			if (verifyCriteria()) {
				jQuery(form).ajaxSubmit(options);
			}
		},
		rules: {
			txtCompany: {
				required: true,
				minlength: 2
			},
			txtAddress: {
				required: true,
				minlength: 2
			},
			txtCity: {
				required: true,
				minlength: 2
			},
			cboState: {
				required: true
			},
			txtZip: {
				required: true,
				minlength: 2
			},
			txtPhone: {
				required: true,
				minlength: 10
			},
			txtFax: {
				required: false,
				minlength: 10
			},
			txtEmail: {
				required: true,
				email: true
			},
			txtContact: {
				required: true,
				minlength: 2
			},
			optBilling: "required"
		},
		messages: {
			txtCompany: {
				required: "** Required",
				minlength: "** Minimum 2 characters"
			},
			txtAddress: {
				required: "** Required",
				minlength: "** Minimum 2 characters"
			},
			txtCity: {
				required: "** Required",
				minlength: "** Minimum 2 characters"
			},
			cboState: {
				required: "** Required"
			},
			txtZip: {
				required: "** Required",
				minlength: "** Minimum 2 characters"
			},
			txtPhone: {
				required: "** Required",
				minlength: "** Invalid Phone Number"
			},
			txtFax: {
				minlength: "** Invalid Fax Number"
			},
			txtEmail: {
				required: "** Required",
				email: "** Invalid E-Mail"
			},
			txtContact: {
				required: "** Required",
				minlength: "** Minimum 2 characters"
			},
			optBilling: "** Please specify"
		}
	});

}); 

// post-submit callback 
function showResponse(responseText, statusText)  { 
	if(!displayForm) {
		jQuery('#htmlForm').hide();
		//jQuery('html, body').animate({scrollTop:0}, 'slow');
	}

	jQuery('#resultTarget').fadeIn('slow'); 
} 

function verifyCriteria() {
	if(jQuery("input[name='captcha']").is(":checked")){
		// Do Nothing
	} else {
		alert("Please select security image to continue...\n");
		return false;
	}

	return true;
}
