//check for IE 6...
var browserVersion = jQuery.browser.version;

//homepage random images
function home_randomization() {
	var ranNum = Math.floor(Math.random()*4)+1;
	var homeImage = "/img/home/img_home_"+ranNum+"a.jpg";
	var homeBG = "url(/img/home/img_home_"+ranNum+"b.jpg)";
	$("#homepageImg").attr({ src:homeImage,alt:"welcome"});
	$("#homeBgText").css({backgroundImage:homeBG});
}


//menu functionality

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

function ft_open()
{  ft_canceltimer();
   ft_close();
   ddmenuitem = $(this).find('ul').css('visibility', 'visible');}

function ft_close()
{  if(ddmenuitem) ddmenuitem.css('visibility', 'hidden');}

function ft_timer()
{  closetimer = window.setTimeout(ft_close, timeout);}

function ft_canceltimer()
{  if(closetimer)
   {  window.clearTimeout(closetimer);
      closetimer = null;}}

//page load function calls

$(document).ready(function()
{  $('#topNavItems > li').bind('mouseover', ft_open)
   $('#topNavItems > li').bind('mouseout',  ft_timer)
   $('.mainNavList > li').bind('mouseover', ft_open)
   $('.mainNavList > li').bind('mouseout',  ft_timer)     
   $('#mainSearchTxt').bind('click', search_empty);  
   $('.getInTouchTxt').bind('click',show_contact); 
   $('#closeWindow').bind('click',hide_contact);
   $('.brochure').bind('click', show_brochure);  
});


document.onclick = ft_close;

function search_empty() {
	document.getElementById('mainSearchTxt').value = "";
}

//show contact modal
//global var to keep track of the main height, sorry...
var currentMainHeight;

function show_contact() {
	$("#contactWindow").fadeIn("fast");
	//these fix stupid things IE6 can't deal with.
	$("#personSearchForm").hide();
	currentMainHeight = $(".main").height();
	if(currentMainHeight < 800) { $(".main").height(800) };
	//close the brochure window
	$("#brochureWindow").hide();
	return false
}

function hide_contact() {
	$("#contactWindow").fadeOut("fast");
	$("#personSearchForm").show();
	return false
}

//show brochure modal

function show_brochure(state) {
	if(state === 1) {
		$("#brochureWindow").fadeOut("fast");
	} else {
		$("#brochureWindow").fadeIn("fast");
		$("#contactWindow").hide();
	}
	//return false
}
//show email modal

function show_email(state) {
	if(state === 1) {
		$("#emailWindow").fadeIn("fast");

	} else {
		$("#emailWindow").fadeOut("fast");
	
	}
	return false
}

//lengthen the homepage borders
function home_borders() {
	var leftCol = $(".homeLeftColumn").height();
	var partnersCol = $(".partnerWithUs").height();
	if(leftCol < partnersCol) {
		var longestCol = partnersCol;
	} else {
		var longestCol = leftCol;
	}
	//set the heights
	$(".homeLeftColumn").height(longestCol);
	$(".partnerWithUs").height(longestCol);
}
//lengthen the homepage borders
$(document).ready(function() { home_borders() })



function process_contact(formLocation) {

	var errorString="";
	$(".requiredField").removeClass("requiredField");
	$(".errorResponse").empty();
	
	if($("#"+formLocation+" .senderName").val()=='')
	{
		$("#"+formLocation+" .senderName").addClass("requiredField");	
		errorString+="<li>Your name is a required field.</li>";
	}
	
	if($("#"+formLocation+" .senderEmail").val()=='' && $("#"+formLocation+" .senderPhone").val()=='')
	{
		$("#"+formLocation+" .senderEmail").addClass("requiredField");
		$("#"+formLocation+" .senderPhone").addClass("requiredField");
		errorString+="<li>You must complete either email address or phone number.</li>";	
	}
	
	if($("#"+formLocation+" .senderEmail").val()=='' && $("#"+formLocation+" .senderPhone").val()=='' && $("#"+formLocation+" .senderStreet").val()=='')
	{
		$("#"+formLocation+" .senderEmail").addClass("requiredField");
		$("#"+formLocation+" .senderPhone").addClass("requiredField");
		$("#"+formLocation+" .senderStreet").addClass("requiredField");
		errorString+="<li>You must complete either email address, street address or phone number.</li>";
	}
	
	if($("#"+formLocation+" .senderBrochure").attr("checked")==true && $("#"+formLocation+" .senderStreet").val()=='')
	{
		$("#"+formLocation+" .senderStreet").addClass("requiredField");
		errorString+="<li>If requesting a brochure, you must complete your address.</li>";
	}
	
	if(errorString!='')
	{
		if ( formLocation == 'become_client_contact' )
		{
			$("#"+formLocation+" .errorResponse").html("Please complete the fields highlighted below:");
		}
		else {
			$("#"+formLocation+" .errorResponse").html("Please correct the following errors:<br\>"+"<ul>"+errorString+"</ul>");
		}
		return false;
	}
	
	
	var allInputs=$("#"+formLocation+" :input");
		
	$.ajax({
		type: "GET",
		url: "/cgi-bin/utilities/get_in_touch.cgi",
		data: allInputs.serialize(),
		success: function(html) {
			if(html.match("^200.*"))
			{
				show_thankyou(formLocation);
			}
			else
			{
				$("#"+formLocation+" .errorResponse").html("The following error occurred, please try again: "+html);
			}
		}
	});
	
}


function submit_bac_contact(formLocation) {

	var errorString="";
	$(".requiredField").removeClass("requiredField");
	$(".errorResponse").empty();
	
	if($("#"+formLocation+" .senderName").val()=='')
	{
		$("#"+formLocation+" .senderName").addClass("requiredField");	
		errorString+="<li>Your name is a required field.</li>";
	}
	
	if($("#"+formLocation+" .senderEmail").val()=='' && $("#"+formLocation+" .senderPhone").val()=='')
	{
		$("#"+formLocation+" .senderEmail").addClass("requiredField");
		$("#"+formLocation+" .senderPhone").addClass("requiredField");
		errorString+="<li>You must complete either email address or phone number.</li>";	
	}
	
	if($("#"+formLocation+" .senderEmail").val()=='' && $("#"+formLocation+" .senderPhone").val()=='' && $("#"+formLocation+" .senderStreet").val()=='')
	{
		$("#"+formLocation+" .senderEmail").addClass("requiredField");
		$("#"+formLocation+" .senderPhone").addClass("requiredField");
		$("#"+formLocation+" .senderStreet").addClass("requiredField");
		errorString+="<li>You must complete either email address, street address or phone number.</li>";
	}
	
	if($("#"+formLocation+" .senderBrochure").attr("checked")==true && $("#"+formLocation+" .senderStreet").val()=='')
	{
		$("#"+formLocation+" .senderStreet").addClass("requiredField");
		errorString+="<li>If requesting a brochure, you must complete your address.</li>";
	}
	
	if(errorString!='')
	{
		if ( formLocation == 'become_client_getintouch' )
		{
			$("#contact_intro").html("Please complete the fields highlighted below:");
			$("#contact_intro").addClass("errorResponse");
		}
		else {
			$("#"+formLocation+" .errorResponse").html("Please correct the following errors:<br\>"+"<ul>"+errorString+"</ul>");
		}
		return false;
	}
	
	
	var allInputs=$("#"+formLocation+" :input");
		
	$.ajax({
		type: "GET",
		url: "/cgi-bin/utilities/get_in_touch.cgi",
		data: allInputs.serialize(),
		success: function(html) {
			if(html.match("^200.*"))
			{
				$('#'+formLocation).hide();
				$('#contact_footer').hide();
				$('#contact_intro').html("<center><strong>Thank you.</strong><br /> Your request has been received.</center>");				
			}
			else
			{
				$("#contact_intro").html("The following error occurred, please try again: "+html);
						$("#contact_intro").addClass("errorResponse");
			}
		}
	});
	
}

function process_email() {
	var errorString="";
	$(".requiredField").removeClass("requiredField");
	$(".errorResponse").empty();
	
	if($("#articleSender").val()=='')
	{
		$("#articleSender").addClass("requiredField");	
		errorString+="<li>Your email is a required field.</li>";
	}

	if($("#articleRecipient").val()=='')
	{
		$("#articleRecipient").addClass("requiredField");	
		errorString+="<li>Recipient's email is a required field.</li>";
	}
	
	if(errorString!='')
	{
		$(".errorResponse").html("Please correct the following errors:<br\>"+"<ul>"+errorString+"</ul>");
		return false;
	}
	
	
	var allInputs=$("#emailWindow :input");
		
	$.ajax({
		type: "GET",
		url: "/cgi-bin/utilities/email_article.cgi",
		data: allInputs.serialize(),
		success: function(html) {
			if(html.match("^200.*"))
			{
				show_email();
			}
			else
			{
				$(".errorResponse").html("The following error occurred, please try again: "+html);
			}
		}
	});

}

function process_people_search() {
	var errorString="";
	$(".requiredField").removeClass("requiredField");
	$(".errorResponse").empty();
	
	if($("#fullName").val()=='' && $("#officeLocation").val()=='' )
	{
		errorString+="<li>You must complete at least one field.</li>";
	}
	
	if(errorString!='')
	{
		$(".errorResponse").html("Please correct the following errors:<br\>"+"<ul>"+errorString+"</ul>");
		return false;
	}
	
	
	var allInputs=$("#personSearchForm :input");
		
	$.ajax({
		type: "GET",
		url: "/cgi-bin/utilities/people_search.cgi",
		data: allInputs.serialize(),
		success: function(html) {
			$("#personSearchResult").html(html);
		}
	});

}

//this is where you would edit the form submission code, add a line with the submission method
function submit_contact(formLocation) {
	process_contact(formLocation);
}

function submit_email() {
	process_email();
}

function submit_people_search() {
	process_people_search();
}

function show_thankyou(formLocation) {
		$('#'+formLocation+' .contactForm').hide();
		$('#contactFooter').hide();
		$('#contact_footer').hide();
		$('#'+formLocation+' .instructionsThankyou').html("<strong>Thank you.</strong><br /> Your request has been received.");
}

//leaving site confirmations

function leave_site(msg) {
	var externalLink;
	if(msg === 1) {
		//msg txt
		alertMsg = 'You are now leaving the Fiduciary Trust website and entering an interdependent website operated by FISERV Solutions, Inc. ("FISERV"), which is not an affiliate of Fiduciary Trust, Franklin Resources, Inc. or any of their respective affiliates (collectively, "Franklin"). Franklin does not review information provided on the FISERV website; privacy and security policies, terms, conditions and other legal information for the FISERV website may differ from those applicable to any Franklin website.\n\n© Copyright 2009, Fiduciary Trust Company International. All rights reserved.';
		//external link
		//externalLink = "https://ns.bankbyweb.net/PBI_PBI1961/pbi1961.asp?Rt=026007922&LogonBy=Connect3&PRMAccess=Account";
		externalLink = "https://ebank3.secureaps.com/PBI1961.asp?Rt=026007922&LogonBy=Connect3&PRMAccess=Account&URL=Start.htm";
	}
	if(msg === 2) {
		//msg txt #2
		alertMsg = 'You are about to leave the Fiduciary Trust Company International ("Fiduciary Trust") website. You have requested online access to the NetExchange Client website operated by Pershing LLC ("Pershing"), executing broker for your Franklin Templeton Financial Services Corp. ("FTFS") brokerage account in which FTFS acts as introducing broker. FTFS is an affiliate of Fiduciary Trust and a wholly-owned subsidiary of Franklin Resources, Inc. ("Franklin"). FTFS is a member of the NASD, NASDAQ, SIPC, AMEX and MSRB. FTFS and Pershing have entered into a service agreement for the purpose of providing you with clearing brokerage services. Neither Pershing nor NetExchange Client is affiliated with Franklin, Fiduciary Trust or FTFS. Information provided on this independent site are not reviewed by Franklin, Fiduciary Trust, FTFS or its affiliates. Please bear in mind that the NetExchange website\'s terms and conditions, privacy and security policies and other legal information may be different from those of Fiduciary Trust, FTFS or its affiliates.\n\nPast performance is not indicative of future returns.\n\nINVESTMENTS ARE:\n*NOT BANK DEPOSITS\n*NOT BANK GUARANTEED\n*NOT FDIC INSURED\n*SUBJECT TO RISK\n\n© Copyright 2009, Fiduciary Trust Company International. All rights reserved.';
		//external link
		externalLink = "https://www2.netxselect.com";
	}
	var answer = confirm(alertMsg);
	if(answer) {
		window.open(externalLink);
	} else {
		//whatever you might want to say here 
	}		
	
}

function check_search() {
	if($("#mainSearchTxt").val() != '' ) {
		return true;
	}
	
	return false;
}
function leave_site_msg(sitelink)
{

alertMsg="You are about to leave the Fiduciary Trust Website\n\nClicking OK below will take you to an independent site. Information and services provided on this independent site are not reviewed by, guaranteed by, or endorsed by Fiduciary Trust or its affiliates. Please keep in mind that this independent site\'s terms and conditions, privacy and security policies, or other legal information may be different from those of Fiduciary Trust\'s site. Fiduciary Trust is not liable for any direct or indirect technical or system issues, consequences, or damages arising from your use of this independent website.\n\n© Copyright 2009, Fiduciary Trust Company International. All rights reserved.";
var answer = confirm(alertMsg);
 if(answer) {
externalLink =sitelink;
window.open(externalLink);
} else {
//whatever you might want to say here
 }

}

