$(document).ready(function() {	
	$("#signupform").submit(function() {
		$("#signupformessage").html("<p style='color: #ffffff; font-size: 11px; padding-top: 4px;'>Saving Data.  Please wait.</p>");
		/*
		$("#signupform input[name=submit]").hide();
		$("#signupform input").attr("disabled", "disabled");
		*/
		// Perform your validation error checking here
		var formdata = $("#signupform").serialize();
		//alert(formdata);
		$.ajax({
			type: "POST",
			url: "/ajax_signupcurl.php",
			data: formdata,
			timeout: 8000,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				signupformError(XMLHttpRequest, textStatus, errorThrown);
			},
			success: function (data) {
				signupformSuccess(data);
			}
		});
		return false;
	});
	
	/*
	Ask an expert form
	*/
	$("#formaskexpert").submit(function() {
		var formisvalid = false;
		formisvalid = $("#formaskexpert").validate().form();
		if(formisvalid) {
			$("#formaskexpertmessage").html("Sending email.  Please wait.</p>");
			$("#formaskexpert").hide('fast');
			var formdata = $("#formaskexpert").serialize();
			$.ajax({
				type: "POST",
				url: "/ajax_askexpert.php",
				data: formdata,
				timeout: 2000,
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					askexpertformError(XMLHttpRequest, textStatus, errorThrown);
				},
				success: function (data) {
					askexpertSuccess(data);
				}
			});
		}
		return false;
	});
	
	$("#poll_questions").find("a").click(function(e) {
		//alert($(this).attr('id'));
		document.getElementById('form_poll_answerid').value = $(this).attr('id');
		$(this).addClass("on");
		e.preventDefault();

			$("#poll_feedback").html("Sending results. Please wait.</p>");
			$("#poll_questions").hide('fast');
			var formdata = $("#form_poll").serialize();
			$.ajax({
				type: "POST",
				url: "/ajax_poll.php",
				data: formdata,
				timeout: 4000,
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					pollError(XMLHttpRequest, textStatus, errorThrown);
				},
				success: function (data) {
					pollSuccess(data);
				}
			});
	});
	
	$("#poll_viewresults").click(function(e) {
		$("#poll_results_graph").show('fast');
		$("#poll_results_questions").hide()
		$("#poll_viewresults").hide();
	});
	
	/*
	Tooltip
	*/
	/*
	$("a.doubleul").hover(function(e) {
		//alert($(this).text());
		thistmpid = $(this).attr("id");
		//alert(thistmpid);
		e.preventDefault();
		$.ajax({
			type: "GET",
			url: "/ajax_tooltip.php",
			data: "tooltip=" + $(this).text(),
			timeout: 4000,
			error: function (XMLHttpRequest, textStatus, errorThrown) {
				toolTipError(XMLHttpRequest, textStatus, errorThrown, thistmpid);
			},
			success: function (data) {
				toolTipSuccess(data, thistmpid);
				//alert($(this).text());
			}
		});
	});
	*/
	/*
	Tooltip
	*/
	var currentlyShowing = false;
	$(".highlight").hover(
		function() {
			if(currentlyShowing == false) {
				currentlyShowing = true;
				//alert($(this).text());
				thistmpid = $(this).attr("id");
				//alert(thistmpid);
				//e.preventDefault();
				toolTipShow(thistmpid, currentlyShowing);
			}
		},
		function() {
			currentlyShowing = false;
		}
	);
	/*
	Seminar form.
	*/
	$("#registernowlink a").click(function(e) {
		$("#registernowform").toggle("slow");
	});
	
	
	/*
	Seminars form
	*/
	$("#formseminars").submit(function() {
		var formisvalid = false;
		formisvalid = $("#formseminars").validate().form();
		if(formisvalid) {
			$("#formseminarsmessage").html("Submitting registration.  Please wait.</p>");
			$("#formseminars").hide('fast');
			var formdata = $("#formseminars").serialize();
			$.ajax({
				type: "POST",
				url: "/ajax_seminar.php",
				data: formdata,
				timeout: 2000,
				error: function (XMLHttpRequest, textStatus, errorThrown) {
					formseminarsError(XMLHttpRequest, textStatus, errorThrown);
				},
				success: function (data) {
					formseminarsSuccess(data);
				}
			});
		}
		return false;
	});

});

/*
Signup Form
*/
function signupformError(XMLHttpRequest, textStatus, errorThrown) {
	//alert(XMLHttpRequest);
	//alert(textStatus);
	//alert(errorThrown);
	$("#signupformessage").html("<p class='save_error'>We were unable to save your data.  Please retry.</p>");
	// Send error details in a support email
	/*
	$("#signupform input").removeAttr("disabled");
	$("#signupform input[name=submit]").show();
	*/
}

function signupformSuccess(data) {
	if(data.indexOf("Error") >= 0) {
		$("#signupformessage").addClass("error_white");
		$("#signupformessage").html("ERROR: Cannot save data. Please retry.");
	}
	else if(data.indexOf("ERROR:") >= 0) {
		$("#signupformessage").addClass("error_white");
		$("#signupformessage").html(data);
	}
	else {
		$("#signupformessage").removeClass("error_white");
		$("#signupformessage").html("You have been subscribed to our updates.");
		$("#signupform").remove();
	}
}

/*
Ask export
*/
function askexpertformError(XMLHttpRequest, textStatus, errorThrown) {
	$("#formaskexpertmessage").html("<p class='save_error'>We were unable to save your data.  Please try again.</p>");
	// Send error details in a support email
	$("#formaskexpert input").removeAttr("disabled");
	$("#formaskexpert input[name=submit]").show('fast');
}

function askexpertSuccess(data) {
	if(data.indexOf("ERROR:") >= 0) {
		$("#formaskexpert").show('fast');
		$("#formaskexpertmessage").addClass("askexpert_error");
		$("#formaskexpertmessage").html(data);
		$("#formaskexpert input").removeAttr("disabled");
		$("#formaskexpert input[name=submit]").show('fast');
	}
	else {
		$("#formaskexpertmessage").removeClass("askexpert_error");
		$("#formaskexpertmessage").html(data);
		$("#formaskexpert").remove();
		$("#askexpert_introtext").remove();
	}
}
/*
Poll
*/
function pollError(XMLHttpRequest, textStatus, errorThrown) {
	$("#formaskexpertmessage").html("<p class='save_error'>We were unable to save your data.  Please try again.</p>");
	// Send error details in a support email
	$("#formaskexpert input").removeAttr("disabled");
	$("#formaskexpert input[name=submit]").show('fast');
}

function pollSuccess(data) {
	if(data.indexOf("ERROR:") >= 0) {
		$("#poll_feedback").html(data);
		$("#poll_viewresults").hide();
	}
	else {
		$("#poll_feedback").removeClass("askexpert_error");
		$("#poll_feedback").html(data);
		$("#poll_viewresults").hide();
		//$("#poll_questions").remove();
	}
}
/*
Poll
*/
function toolTipError(XMLHttpRequest, textStatus, errorThrown, elementid) {
	alert("ERROR");
}

function toolTipSuccess(data, elementid) {
	//$("#" + elementid).attr("title", data);
	//alert($("#" + elementid).attr("title"));
	//$("#" + elementid).title = "AGTESTING";
	$("#" + elementid).simpletip({
		onBeforeShow: function(){
			// Note this refers to the API in the callback function
			if(currentlyShowing == false) {
				this.load('ajax_tooltip.php?tooltip=Amortization');
			}
		}
	}); 
}
function rPosition(elementID, mouseX, mouseY) {
  var offset = $('#'+elementID).offset();
  var x = mouseX - offset.left;
  var y = mouseY - offset.top;

  return {'x': x, 'y': y};
}
var mouseX = 0;
var mouseY = 0;
$().mousemove( function(e) {
   mouseX = e.pageX; 
   mouseY = e.pageY;
 });
function toolTipShow(elementid, tmpShow) {
	//$("#" + elementid).attr("title", data);
	//alert($("#" + elementid).attr("title"));
	//$("#" + elementid).title = "AGTESTING";
	var thistext = "";
	thistext = $("#" + elementid).text();
	//alert(thistext);
	var pos = $("#highlight-plugin").offset();  
  	var width = $("#highlight-plugin").width();

	$("#" + elementid).simpletip({
		content: "",
		onBeforeShow: function(){
			// Note this refers to the API in the callback function
			//alert(tmpShow);
			if(tmpShow) {
				this.load('/ajax_tooltip.php?tooltip=' + escape(thistext) + "&x=" + (mouseX - pos.left) + "&y=" + (mouseY - pos.top));
				return true;
			}
		}
	});
}

function viewcalculator(thisurl) {
	var thiswindow;
	thiswindow = window.open(thisurl, 'viewpage', 'width=650,height=500,scrollbars,resizable');
	thiswindow.focus();
}




/*
Poll
*/
function formseminarsError(XMLHttpRequest, textStatus, errorThrown) {
	$("#formseminarsmessage").html("<p class='save_error'>We were unable to save your data.  Please try again.</p>");
	// Send error details in a support email
	$("#formseminars input").removeAttr("disabled");
	$("#formseminars input[name=submit]").show('fast');
}

function formseminarsSuccess(data) {
	//alert(data);
	if(data.indexOf("ERROR:") >= 0) {
		$("#formseminars").show('fast');
		$("#formseminarsmessage").addClass("error");
		$("#formseminarsmessage").html(data);
		$("#formseminars input").removeAttr("disabled");
		$("#formseminars input[name=submit]").show('fast');
	}
	else {
		$("#formseminarsmessage").removeClass("error");
		$("#formseminarsmessage").html(data);
		$("#formseminars").remove();
	}
}

/*
 * 
 * Textarea Word Count Jquery Plugin 
 * Version 1.0
 * 
 * Copyright (c) 2008 Roshan Bhattarai
 * website : http://roshanbh.com.np
 * 
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
*/

jQuery.fn.wordCount = function(params){
	var p = {
		counterElement:"display_count",
		maxWords: 100
	};
	var total_words;
	
	if(params) {
		jQuery.extend(p, params);
	}
	
	//for each keypress function on text areas
	this.keypress(function(evt, obj)
	{
		var charCode = (evt.which) ? evt.which : evt.keyCode;
		total_words=this.value.split(/[\s\.\?]+/).length;
		if (total_words > p.maxWords) {
			if ((charCode != "8") && (charCode != "46")) {
				return false;
			}
		}
		jQuery('#'+p.counterElement).html(total_words);
	});	
};
