// Load jQuery
google.load("jquery", "1.3.2");
google.setOnLoadCallback(function() {

	$('body').removeClass('no_JS');

	var cFname = $('input[name=lname]'),
		cFtitle = $('input[name=title]'),
		cFcompany = $('input[name=company]'),
		cFaddress = $('input[name=address]'),
		cFcity = $('input[name=city]'),
		cFstate = $('select[name=state]'),
		cFzip = $('input[name=zip]'),
		cFemail = $('input[name=email]'),
		cFphone = $('input[name=phone]'),
		cFcomments = $('textarea[name=comments]'),
		cFfindout = $('select[name=findout]'),
		cFmeeting = $('input[name=meeting]'),
		cFac = $('input[name=ac]'),
		cFfl = $('input[name=fl]'),
		cFgb = $('input[name=gb]'),
		cFvn = $('input[name=vn]'),
		cFsubmit = $('input[name=cfsubmit]'),
		cFfields = $([]).add(cFname).add(cFtitle).add(cFcompany).add(cFaddress).add(cFcity).add(cFzip).add(cFemail).add(cFphone).add(cFcomments).add(cFfindout).add(cFmeeting).add(cFac).add(cFfl).add(cFgb).add(cFvn),
		tips = $('#cf-validate'),
		domcphr = window.location.hostname;
	
	cFsubmit.val('');
	
	function eval_cb(obj){
		if($(obj).is(':checked')){
			return true;
		}
		else{
			return false;
		}
	}
	
	function updateTips(t) {
		tips.text(t);
	}

	function checkLength(o,n,min,max) {
		if ( o.val().length > max || o.val().length < min ) {
			o.addClass('ui-state-error');
			updateTips("Length of " + n + " must be between "+min+" and "+max+".");
			return false;
		} else {
			updateTips(" ");
			return true;
		}
	}

	function checkRegexp(o,regexp,n) {
		if ( !( regexp.test( o.val() ) ) ) {
			o.addClass('ui-state-error');
			updateTips(n);
			return false;
		} else {
			updateTips(" ");
			return true;
		}
	}
	
	$('#pgmap').click(function(){
		$('div.contact-map').show();
	});
	
	$('input.send').click(function(){
		var isvalid = true;
		cFfields.removeClass('ui-state-error');
		isvalid = isvalid && checkLength(cFname,"Name",2,100);
		isvalid = isvalid && checkLength(cFemail,"Email",6,320);
		isvalid = isvalid && checkRegexp(cFemail,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"Email must be in format: email@domain.com");
		isvalid = isvalid && checkLength(cFcomments,"Message",2,5000);
		if (isvalid) {
			$(this).attr('disabled','disabled');
			$('div.contactform form').fadeTo('normal', 0.2);
			$('div.contactform').append('<div class="cf-loading">&nbsp;&nbsp;<strong>Sending...</strong><br /><img src="/wp-content/themes/perisho/images/loading.gif" /></div>');
			$.post("/wp-content/themes/perisho/includes/php/send.php",{
				sendDom: domcphr,
				senderName: cFname.val(),
				senderEmail: cFemail.val(),
				senderMesg: 'Name: '+cFname.val()+'\nCompany: '+cFcompany.val()+'\nTitle: '+cFtitle.val()+'\nAddress: '+cFaddress.val()+'\nCity: '+cFcity.val()+'\nState: '+cFstate.val()+'\nZip: '+cFzip.val()+'\nEmail: '+cFemail.val()+'\nPhone: '+cFphone.val()+'\nComments/Questions: '+cFcomments.val()+'\nFind Us: '+cFfindout.val()+'\nSetup Meeting: '+cFmeeting.val()+'\nSubscribe to All Clients: '+eval_cb(cFac)+'\nSubscribe to Focus on Lenders: '+eval_cb(cFfl)+'\nSubscribe to Golden Bullets: '+eval_cb(cFgb)+'\nSubscribe to Valuation: '+eval_cb(cFvn),
				senderReply: 'Hello '+cFname.val()+',\n\nThanks for contacting Perisho Tombor Ramirez Filler & Brown. We have received your email and will get back to you as soon as possible.\n\nPerisho Tombor Ramirez Filler & Brown\ninfo@perisho.com',
				senderCompany: cFcompany.val(),
				senderTitle: cFtitle.val(),
				senderAddress: cFaddress.val(),
				senderCity: cFcity.val(),
				senderState: cFstate.val(),
				senderZIP: cFzip.val(),
				senderPhone: cFphone.val(),
				subAllClients: eval_cb(cFac),
				subFocusLenders: eval_cb(cFfl),
				subGoldenBullets: eval_cb(cFgb),
				subValuation: eval_cb(cFvn)
				},
				function(data){
					$('div.contactform').html('<p><strong>Success!</strong><br />Your email was sent. Thank you for contacting Perisho Tombor Ramirez Filler &amp; Brown. We&rsquo;ll get back to you as soon as we can.</p>').fadeTo('fast',1);
				}
			);
		}
		return false;
	});

});