/*<![CDATA[*/
		   
/*****************************************************************************
//-> START GENERAL FUNCTIONS
*****************************************************************************/
// --> START CAPTCHA
var flag = false;
var a = Math.ceil(Math.random() * 10);
var b = Math.ceil(Math.random() * 10);
var c = a+b;

function verify(){

	if(!flag){
		var c = a + b;
		var d = $("#verify:input").val();
		
		$('label.verify').append("what is "+ a + " + " + b +"? ");
		flag = true;
		return false;
	}
	
	var c = parseInt($("#aVal:input").val()) + parseInt($("#bVal:input").val());
	var d = $("#verify:input").val();

	if (flag && c == d) {
		return true; 
	}else{
		return false;
	}
	return false;	
}

// --> START EXTEND VALIDATION FOR CAPTCHA
$.validator.methods.equal = function(value, element, param) {
	return value == param;
};

// --> FORM pre-submit callback
function showRequest2(formData, jqForm, options) {
	$('.msg2').fadeIn(500).html("<img src='images/loadinfo.gif' width='16' height='16' alt='' /> Please wait, sending suggestion...");
	$('#tell-a-friend').slideUp("normal", function(){
			$.fn.colorbox.resize();
	});
}

// --> FORM post-submit callback
function showResponse2()  {
	$('.msg2').fadeIn(500).html('<img class="png_bg" src="images/success.png" width="16" height="16" alt="" /> <strong>Thank You!</strong><br />Your suggestion has been sent successfully.<br /><a href="javascript:initForm();" style="color:blue;">Click here</a> to send to another friend.');
	$.fn.colorbox.resize();
}

// --> FORM Grab page data
function grabData()  {
	//Adding page information to the email
	var page_link = $("div#tell-a-friend-wrapper div.curURL").text();
	var page_name = $("div.header-wrap h1").text();
	
	$("input#tell_link_name").val(page_name + " - Island, Luxurious suites hotel");
	$("input#tell_link").val(page_link);
}

// --> FORM show form again
function initForm()  {
	$('.msg2').slideUp("fast", function(){
		$('#tell-a-friend').slideDown("normal", function(){
			grabData();
			$.fn.colorbox.resize();
		});
	});
}

/*****************************************************************************
//-> END GENERAL FUNCTIONS
*****************************************************************************/

/*****************************************************************************
//-> START Window Load
*****************************************************************************/

$(window).load(function () {
// --> Start ColorBox
	$("a[rel=form]").colorbox({
		opacity:0.3,
		inline:true,
		scrolling:false,
		title: true,
		width: 720,
		href:"#tell-a-friend-holder"
	});
// --> START FORM VALIDATION
	
	// Init Form Validation
    $("#tell-a-friend").validate({
		errorElement: "em",
		errorClass: "error",
		meta: "validate",
		onkeyup: false,
		onfocusout: false,
		onclick: true,
		errorPlacement: function(error, element) {
			 if (element.attr("id") == "tell_verify" )
			   error.insertAfter(element);
			 else
			   error.insertBefore(element.next("div.seperation"));
		},
        showErrors: function(errorMap, errorList) {
			$.fn.colorbox.resize();
        	this.defaultShowErrors();
        },
		submitHandler: function(form) {			
			// Send the form via ajax form
			jQuery(form).ajaxSubmit({
        		url:              "/sites/islandtower/process-tell-a-friend.asp",
                //target:           ".msg2",        // Get the server response here
                beforeSubmit:     showRequest2,   // pre-submit callback
                success:          showResponse2, // post-submit callback                    
        		clearForm:        true,        // clear all form fields after successful submit
        		resetForm:        true        // reset the form after successful submit
			});
		}
	});
	//Add the verify rule
	$("input#tell_verify").rules("add", {
		equal: c
	});

	//Adding page information to the email
	grabData();

// --> Init CAPTCHA
	$('#tell-a-friend').find("fieldset").append('<input type="hidden" name="aVal" id="aVal" value="" /><input type="hidden" name="bVal" id="bVal" value="" />');
	$("#aVal:input").val(a);
	$("#bVal:input").val(b);

	verify();
});

/*****************************************************************************
//-> END Window Load
*****************************************************************************/

/*]]>*/
