// Settings
var tell_path = "/tell-a-friend/tell/";
var captcha_path = "/tell-a-friend/captcha/";
var tell_html = "<div class=\"subTitleBar\"><h1>Tell A Friend</h1></div>" + 
				"<div class=\"subPageTextBox\">" + 
				"We're glad you want to tell a friend about Hotel Magician. Just fill out the form below (we will pre-populate some fields but you may change them if you wish) and we'll tell your friend about the page you are currently on.<br /><br />" +
				"<img src=\"/images/main/spacer_blue.gif\" width=\"386\" height=\"1\" alt=\"\" /><br />" + 
				"<form name=\"tell_form\" id=\"tell_form\" action=\"tell.php\" target=\"_blank\" method=\"post\">" + 
				"<label for=\"tell_from\" id=\"tell_from-label\">Your E-mail:</label>" + 
				"<input type=\"text\" name=\"email\" id=\"tell_from\" class=\"tell_required tell_email\" style=\"width:280px\" /><br />" + 
				"<label for=\"tell_name\" id=\"tell_name-label\">Your Name:</label>" + 
				"<input type=\"text\" name=\"name\" id=\"tell_name\" class=\"tell_required\" style=\"width:280px\" /><br />" + 
				"<label for=\"tell_to\" id=\"tell_to-label\">Friend's Email:</label>" + 
				"<input type=\"text\" name=\"recipient\" id=\"tell_to\" class=\"tell_required tell_email\" style=\"width:280px\" /><br />" + 
				"<label for=\"tell_subject\" id=\"tell_subject-label\">Subject:</label>" + 
				"<input type=\"text\" name=\"subject\" id=\"tell_subject\" class=\"tell_required\" style=\"width:280px\" /><br />" + 
				"<label for=\"tell_body\" id=\"tell_body-label\">Message:</label>" + 
				"<textarea name=\"message\" id=\"tell_body\" class=\"tell_required\" style=\"width:280px; margin-bottom: 5px;\" rows=\"5\"></textarea><br />" + 
				"<div id=\"captcha\"></div>" + 
				"<input type=\"image\" src=\"/images/main/button_submit.gif\" alt=\"Submit\" align=\"right\" id=\"tell_submit\" />" + 
				"</form><img src=\"/images/main/spacer.gif\" width=\"1\" height=\"29\" alt=\"\" />" + 
				"</div>" + 
				"<div class=\"tell_close\"><a href=\"javascript:window.close();\">Close Window</a></div><br />";

var tell_resp = "<div class=\"subTitleBar\"><h1>Thank You For Telling A Friend!</h1></div>" + 
				"<div class=\"subPageTextBox\">" + 
				"Your message has been sent and you can close this window<br /><br /><b>Thank you!</b><br />" + 
				"<a href=\"javascript:window.close();\"><img src=\"/images/main/button_close.gif\" alt=\"Close Window\" width=\"82\" height=\"29\" border=\"0\" align=\"right\" /></a>" + 
				"<img src=\"/images/main/spacer.gif\" width=\"1\" height=\"29\" alt=\"\" />" + 
				"</div>";

// Create containers and function calls after page loads
$(function(){
	$("<div id=\"tell_box\">" + tell_html + "</div>").appendTo("body");
	// $("<div id=\"tell_blocker\"><!--[if lte IE 6.5]><iframe></iframe><![endif]--></div>").appendTo("body");
	/*
	$("a.tellafriend").attr({"href": "javascript:void(0);", "target": "_self"}).click(function(){
		tell_launch();
	});
	
	$("input.tellafriend").click(function(){
		tell_launch();
	});
	*/
	tell_launch();
});

function tell_launch() {
	$("#tell_box").html(tell_html);
	tell_block();
	
	$("#tell_name").blur(function(){
		var my_name = ($(this).val() != "") ? $(this).val() : "I";
		$("#tell_subject").val(my_name + " wanted to tell you about HotelMagician.com");
		$("#tell_body").val(my_name + " thought you would find this page useful. You have not been added to any email lists.");
	});
	
	$("#tell_url").val(window.location);
	$("#tell_ajax").val("true");
	
	$("#tell_form").attr({"action": "javascript:void(0);", "target": "_self"}).submit(function(){
		tell_validate();
	});
	
	tell_captcha();
}

function tell_validate() {
	var e = "";
	
	$("#tell_form").find(".tell_required").each(function(){
		if ($(this).val() == "") e += tell_label($(this).attr("id")) + " is required.<br />";
	});
	
	$("#tell_form").find(".tell_email").each(function(){
		if (tell_validate_email($(this).val()) == false) e += tell_label($(this).attr("id")) + " is not valid.<br />";
	});
	
	if (e == "") {
		tell_clear_error();
		tell_process();
	} else {
		tell_error(e);
	}
}

function tell_process() {
	$("#tell_submit").attr("disabled", "disabled").val("Sending...");
	
	var params = {
		ajax: "true",
		url: $("#tell_referer").html(),
		from: $("#tell_from").val(),
		name: $("#tell_name").val(),
		recipient: $("#tell_to").val(),
		subject: $("#tell_subject").val(),
		message: $("#tell_body").val(),
		"confirmation-code": $("#confirmation-code").val(),
		captcha: $("#captcha-code").val()
	};

	$.post(tell_path, params, function(resp) {
		if ($("result", resp).text() == "true") {
			tell_result();
		} else {
			if ($("error", resp).text() != "") tell_error($("error", resp).text());
			tell_reset();
		}
	});
}

function tell_result() {
	$("#tell_box").html(tell_resp);
}

function tell_error(e) {
	if ($("#tell_message").size() == 0) $("#tell_form").prepend("<div id=\"tell_message\"></div>");
	
	$("#tell_message").html(e).slideDown();
}

function tell_clear_error() {
	$("#tell_message").html("").slideUp();
	$("#tell_submit").removeAttr("disabled").val("Submit");
}

function tell_reset() {
	tell_captcha();
	$("#tell_submit").removeAttr("disabled").val("Submit");
}

function tell_block() {

}

function tell_close() {
	//$("#tell_blocker").css("display", "none");
	//$("#tell_box").css("display", "none");
	window.close();
}

function tell_validate_email(str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	
	if ((str.indexOf(at)==-1) || 
		(str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr) || 
		(str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr) || 
		(str.indexOf(at,(lat+1))!=-1) || 
		(str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) || 
		(str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot) || 
		(str.indexOf(dot,(lat+2))==-1) || 
		(str.indexOf(" ")!=-1)) {
			return false;
	} else {
		return true;
	}
}

function tell_label(id) {
	return $("#" + id + "-label").text().replace(":", "").replace("†", "");
}

function tell_captcha() {
	var hash = "";
	
	if ($("#captcha-image").size() == 1) {
		hash = $("#captcha-image").attr("src");
		hash = hash.substring((hash.length - 32), hash.length);
	}
	
	$("#captcha").load(captcha_path + "?action=reload&hash=" + hash, null);
}
