function scrollTo(selector) {
	var targetOffset = $(selector).offset().top;
	$('html,body').animate({scrollTop: targetOffset}, 500);
}

jQuery.fn.defuscate = function( settings ) {
    settings = jQuery.extend({
        link: true
    }, settings);
    var regex = /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi;
    return this.each(function() {
        if ( $(this).is('a[@href]') ) {
            // If it's an <a> element, defuscate the href attribute
            $(this).attr('href', $(this).attr('href').replace(regex, '$1@$2'));
            // Make sure that the element's contents is not made into a link
            var is_link = true;
            //alert($(this).attr('href'));
        }
        // Defuscate the element's contents
        $(this).html($(this).html().replace(regex, (settings.link && !is_link ? '<a href="mailto:$1@$2">$1@$2</a>' : '$1@$2')));
  });
}


$(document).ready(function(){

	$(".eml").defuscate();

	$("#informationrequest").validate({
		debug: true,
		onkeyup: false,
		rules: {
			recaptcha_response_field: {
				required: true
			}
		},
		submitHandler: function(form) {
			//$("#ajaxloader").show();
			$("input#submit").after(' <img id="ajaxloader" src="images/interface/ajax-loader.gif" />');
			$(form).ajaxSubmit({
				method: "POST",
				url: "processform.php",
				success: function(html) {
					$("#ajaxloader").remove();
					if (html==1) {
						// successfully submitted
						scrollTo("html");
						$("#informationrequest").hide();
						$("#message").html("<h2>Your request has been submitted</h2>")
						.append("<p>Thank you, we will be in touch soon.</p><img id='checkmark' src='images/interface/check.png' />")
						.hide()
						.fadeIn("slow");
					} else if (html=='captcha_valid') {
						// was not submitted
						//alert(html);
						scrollTo("#recaptcha_widget_div");
						Recaptcha.reload();
						$("#message").html('');
						$("#formwrapper form").show();
						$("#recaptcha_widget_div").append('<label class="error">Please check the verification code and try again.</label>');
					} else {
						// was not submitted
						//alert(html);
						scrollTo("html");
						$("#message").html('');
						$("#error").html('<p>' + html + '</p>');
						$("#formwrapper form").show();
					}
				}
			});
		}
	});
	
	$("#reportrequest").validate({
		debug: true,
		onkeyup: false,
		rules: {
			recaptcha_response_field: {
				required: true
			}
		},
		submitHandler: function(form) {
			//$("#ajaxloader").show();
			$("input#submit").after(' <img id="ajaxloader" src="images/interface/ajax-loader.gif" />');
			$(form).ajaxSubmit({
				method: "POST",
				url: "processrequest.php",
				success: function(html) {
					$("#ajaxloader").remove();
					if (html==1) {
						// successfully submitted
						scrollTo("html");
						$("#reportrequest, #intro").hide();
						//$("#reportrequest, #intro").css({display:"none"});
						$("#message").html("<h2>Your request has been submitted</h2>")
						.append("<p>Thank you, we will be in touch soon.</p><img id='checkmark' src='images/interface/check.png' />")
						.hide()
						.fadeIn("slow");
					} else if (html=='captcha_valid') {
						// was not submitted
						//alert(html);
						scrollTo("#recaptcha_widget_div");
						Recaptcha.reload();
						$("#message").html('');
						$("#formwrapper form").show();
						$("#recaptcha_widget_div").append('<label class="error">Please check the verification code and try again.</label>');
					} else {
						// was not submitted
						//alert(html);
						scrollTo("html");
						$("#message").html('');
						$("#error").html('<p>' + html + '</p>');
						$("#formwrapper form").show();
					}
				}
			});
		}
	});
	

	$("#add_account").click(function(){
		var lastcount = $("#account_count").val();
		var newcount = Number(lastcount) + 1;
		if (newcount < 10) {
			newcounttext = String('0' + newcount);
		} else {
			newcounttext = String(newcount);
		}
		$(this).before('<div>' + newcounttext + '. <input type="text" class="textfield" name="accounts[]" id="account' + newcount + '" value="" /></div>');
		$("#account_count").val(newcount);
		$("#account" + newcount).focus();
		return false;
	});
	
	$("#addhotel").click(function(){
		var lastid = $("#hotellist tbody tr:last-child").attr('id');
		var lastcount = Number(lastid.replace('hotel', ''));
		var newcount = lastcount + 1;
		if (newcount < 10) {
			nextnum = '0' + String(newcount);
		} else {
			nextnum = String(newcount);
		}
		$("#hotellist tbody").append('<tr id="hotel' + newcount + '"><td>' + nextnum + '. </td><td><input type="text" class="textfield small" name="hotel_' + newcount + '_name" id="hotel_' + newcount + '_name" value="" /></td><td><input type="text" class="textfield small" name="hotel_' + newcount + '_phone" id="hotel_' + newcount + '_phone" value="" /></td></tr>');
		$("input[name=hotel_count]").val(newcount);
		$("#hotel_" + newcount + "_name").focus();
		return false;
	});
	
	$("#addguestroom").click(function(){
		var lastid = $("#guestrooms tbody tr:last-child").attr('id');
		var lastcount = Number(lastid.replace('guestroom', ''));
		var newcount = lastcount + 1;
		if (newcount < 10) {
			nextnum = '0' + String(newcount);
		} else {
			nextnum = String(newcount);
		}
		$("#guestrooms tbody").append('<tr id="guestroom' + newcount + '"><td>' + nextnum + '</td><td><input type="text" class="textfield" name="guestroom_' + newcount + '_day" id="guestroom_' + newcount + '_day" value="" /></td><td><input type="text" class="textfield" name="guestroom_' + newcount + '_date" id="guestroom_' + newcount + '_date" value="" /></td><td><input type="text" class="textfield" name="guestroom_' + newcount + '_number" id="guestroom_' + newcount + '_number" value="" /></td></tr>');
		$("input[name=guestroomcount]").val(newcount);
		$("#guestroom_" + newcount + "_day").focus();
		return false;
	});
	
	$("#addfunctionspace").click(function(){
		var lastid = $("#functionspace tbody tr:last-child").attr('id');
		var lastcount = Number(lastid.replace('functionspace', ''));
		var newcount = lastcount + 1;
		if (newcount < 10) {
			nextnum = '0' + String(newcount);
		} else {
			nextnum = String(newcount);
		}
		$("#functionspace tbody").append('<tr id="functionspace' + newcount + '"><td>' + nextnum + '</td><td><input type="text" class="textfield" name="functionspace_' + newcount + '_day" id="functionspace_' + newcount + '_day" value="" /></td><td><input type="text" class="textfield" name="functionspace_' + newcount + '_date" id="functionspace_' + newcount + '_date" value="" /></td><td><input type="text" class="textfield" name="functionspace_' + newcount + '_time" id="functionspace_' + newcount + '_time" value="" /></td><td><input type="text" class="textfield" name="functionspace_' + newcount + '_function" id="functionspace_' + newcount + '_function" value="" /></td><td><input type="text" class="textfield" name="functionspace_' + newcount + '_setup" id="functionspace_' + newcount + '_setup" value="" /></td><td><input type="text" class="textfield" name="functionspace_' + newcount + '_number" id="functionspace_' + newcount + '_number" value="" /></td></tr>');
		$("input[name=functionspacecount]").val(newcount);
		$("#functionspace_" + newcount + "_day").focus();
		return false;
	});
	
	
});
