$(document).ready(function() {
	
	$("table.styled tr:odd td").css("background-color", "#cccccc");
	$("table.styled tr:odd td").css("color", "#cc6600");
	
	$("ul#navigation-quicklinks li:last").css('backgroundImage','none');
	
	$('.netbranch-login').submit(function(){
		setTimeout('clearMemberNumber()', 500); 
	});
	
	$('a.external').click(function(){
	  return confirm("You are leaving Riverset Credit Union's website and linking to an alternate website not operated by Riverset®. Riverset® is not responsible for the content of the alternate website and does not represent either the third party or the member if the two enter into a transaction. The privacy and security practices of the alternate site may differ from those practiced by Riverset®. You should review the privacy policy of any site before providing information.");
	});
	
	$('#q').focus(function(){
	  if($(this).val() == 'Search Our Site') {
		  $(this).attr('value','');
	  }
	});
	$('#q').blur(function(){
	  if($(this).val() == '') {
		  $(this).val('Search Our Site');
	  }
	});
	
	$('#member_number').focus(function(){
	  if($(this).val() == 'Log In ID') {
		  $(this).attr('value','');
	  }
	});
	$('#member_number').blur(function(){
	  if($(this).val() == '') {
		  $(this).val('Log In ID');
	  }
	});
	
	$('#zip_code').focus(function(){
	  if($(this).val() == 'Enter Zip Code') {
		  $(this).attr('value','');
	  }
	});
	$('#zip_code').blur(function(){
	  if($(this).val() == '') {
		  $(this).val('Enter Zip Code');
	  }
	});
	
	$('.btn-slider-toggle').click(function(){
	  var src = $(this).find('img').attr('src');
	  $("table.slider").find('thead tr td a.btn-slider-toggle img').attr('src',src.replace('-down','-up'));
	  $("table.slider").find('tbody tr td div').hide();
	  if(src.indexOf("-up") != -1) {
		  $(this).parents("table").find('tbody tr td div').fadeIn();
		  $(this).find('img').attr('src',src.replace('-up','-down'));
	  }
	});
	
	var slideSrc = $("table.slider-on").find('thead tr td a.btn-slider-toggle img').attr('src');
	if(slideSrc) {
		$("table.slider-on").find('thead tr td a.btn-slider-toggle img').attr('src',slideSrc.replace('-up','-down'));
	}
	
	$('table.accordian-toggle thead th a').click(function(){
	  var src = $(this).find('img').attr('src');
	  $('table.accordian-toggle thead th a img').attr('src',src.replace('-down','-up'));
	  $("table.accordian-toggle").next('table').hide();
	  if(src.indexOf("-up") != -1) {
		  $(this).parents("table").next('table').fadeIn();
		  $(this).find('img').attr('src',src.replace('-up','-down'));
	  }
	});
	
	var mouseInMenu = false;
	
	var navhoverconfig = {    
		 over: function() {
			mouseInMenu = false;
			$(this).next('ul').slideDown('fast');
		 },  
		 timeout: 300, 
		 out: function() {
			if(!mouseInMenu) {
				$(this).next('ul').hide();
			}
		 }
	};
	$('ul#navigation-primary > li > a').hoverIntent(navhoverconfig);
	
	$('ul#navigation-primary > li > ul').hover(
		function () {
			mouseInMenu = true;
			$(this).show();
		},
		function () {
			mouseInMenu = false;
			$(this).hide();
		}
	);
	
	$('ul#navigation-primary > li > ul > li').hover(
		function () {
			mouseInMenu = true;
			$(this).parent('ul').show();
		},
		function () {

		}
	);
	
	$('ul#navigation-primary > li > ul > li > a').hover(
		function () {
			mouseInMenu = true;
			$(this).parent('li').parent('ul').show();
		},
		function () {

		}
	);
	
	var nav_bg_img = new Image();
	nav_bg_img.src = '/images/bg-nav-primary-on.gif';
	
	$('ul#navigation-primary > li.last > a').hover(
		function () {
			$(this).next('ul').css('left','448px');
			if($('ul#navigation-primary > li.last > a.on').length == 0) {
				$(this).parent("li").parent('ul').css('backgroundImage','url(/images/bg-nav-primary-on.gif)');
			}
		},
		function () {
			if($('ul#navigation-primary > li.last > a.on').length == 0) {
				$(this).parent("li").parent('ul').css('backgroundImage','url(/images/bg-nav-primary.gif)');
			}
		}
	);
	if($('ul#navigation-primary > li.last > a.on').length > 0) {
		$('ul#navigation-primary').css('backgroundImage','url(/images/bg-nav-primary-on.gif)');
	}
	
	$('#features').cycle({
		fx: 'fade',
		timeout: 5000, 
		before:  onSlideBefore, 
		after:   onSlideAfter
	});
	$('#feature-numbers a').click(function(){
		$('#features').cycle($(this).index());
		$('#feature-numbers a').removeClass('on');
		$(this).addClass('on');
	});
	
	bindPasswordFocus();
});

function traceDebug(str) {
	var output = $('#trace').html();
	$('#trace').html(output + "<br />" + str)
}

function clearMemberNumber() {
	$('#member_number').val('');		
}

function onSlideBefore() { 
     $('#feature-numbers a').removeClass('on');
	$('#feature-numbers a:eq(' + $(this).index() + ')').addClass('on');
} 
function onSlideAfter() { 
	
    //alert();
}

function bindPasswordFocus() {
	$('#password').focus(function(){
	  if($(this).val() == 'Password') {
		  $(this).replaceWith('<input class="textinput" type="password" id="password" name="password" value="" />');
		  $('#password').focus();
		  bindPasswordBlur();
	  }
	});	
}

function bindPasswordBlur() {
	$('#password').blur(function(){
	  if($(this).val() == '') {
		  $(this).replaceWith('<input class="textinput" type="text" id="password" name="password" value="Password" />');
		  bindPasswordFocus();
	  }
	});
}

function clear_form_elements(ele) {

    $(ele).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });

}

function validateLoanCalcForm(){
 
// This function checks for empty required fields
// With Netscape focus is placed on empty fields
// Inputs are hard coded, nothing is passed to it
// It returns a true or false depending on validity 
	var amount = document.forms['cacl'].amount.value
	var payment = document.forms['cacl'].payment.value
	var rate = document.forms['cacl'].rate.value
	var months = document.forms['cacl'].months.value
	var comma = ","
	var temparry = new Array(10)
 
	temparray = amount.split(comma)
	amount = temparray.join("")
	temparray = payment.split(comma)
	payment = temparray.join("")
	temparray = rate.split(comma)
	rate = temparray.join("")
	temparray = months.split(comma)
	months = temparray.join("")
 
 
	if (isNaN(amount)) {
		alert("Amount must be a number!");
		document.forms['cacl'].amount.focus();
		return false;
	}
	if (isNaN(payment)) {
		alert("Payment must be a number!");
		document.forms['cacl'].payment.focus();
		return false;
	}
	if (isNaN(rate)) {
		alert("Rate must be a number!");
		document.forms['cacl'].rate.focus();
		return false;
	}
	if (isNaN(months)) {
		alert("Length of Loan must be a number!");
		document.forms['cacl'].months.focus();
		return false;
	}
	
	if(((amount != 0) && (amount != "")) &&
		((payment != 0) && (payment != ""))){ 
		alert("Please clear either the loan payment or the loan amount to continue.");
		document.forms['cacl'].amount.focus();
		return false;
	}
	if(((amount == 0) || (amount == "")) &&
		((payment == 0) || (payment == ""))){ 
		alert("You must select either the loan payment or the loan amount!");
		document.forms['cacl'].amount.focus();
		return false;
	}
	if((rate == 0) || (rate == "")){ 
		alert("You must select a loan rate!");
		document.forms['cacl'].rate.focus();
		return false;
		}
	else {
		rate = rate/1200;
	}
 
	if((months == 0) || (months == "")){
		alert("You must provide the term of the loan!");
		document.forms['cacl'].months.focus();
		return false;
		}
	else {
		if(document.forms['cacl'].frequency.options[document.forms['cacl'].frequency.selectedIndex].value == '1'){
			months = months * 12;
		}
	}
 
	if(payment == 0 || payment == ""){
		document.forms['cacl'].payment.value = parseInt(100 * ((amount * ( rate / (1 - (Math.pow(1 + rate, -months))))) + .005)) / 100;
	}
	else {
		document.forms['cacl'].amount.value = parseInt(100 * ((((Math.pow(1 + rate, -months)*(-payment + (Math.pow(1 + rate, months)* payment))))/rate) + .005)) / 100;
	}
	return false;
}
