// JavaScript Document
var ie7 = (document.all && !window.opera && window.XMLHttpRequest) ? true : false;

var numMenus = 5; // Set the number of menus in the top navigation

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function MM_validateContactForm() { //v4.0
  var i,p,q,nm,nd,test,num,min,max,errors='',args=MM_validateContactForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; nd=val.id; if (nm == "cFirstName" && val.value == 'First Name') { val.value = "";} else { if (nm == "cLastName" && val.value == 'Last Name') { val.value = "";}  else { if (nm == "cPhone" && val.value == 'Phone') { val.value = "";}}}if ((vVal=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=vVal.indexOf('@');
        if (p<1 || p==(vVal.length-1)) errors+='- '+nd+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(vVal);
        if (isNaN(vVal)) errors+='- '+nd+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nd+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nd+' is required.\n'; 
	if ( nm == "cFirstName" && val.value == '') {val.value = "First Name";} if (nm == "cLastName" && val.value == '') { val.value = "Last Name";} if (nm == "cPhone" && val.value == '') { val.value = "Phone";} }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
 	document.MM_returnValue = (errors == '');
  	document.cForm.action = "http://www.cdhgenetics.com/contact.cfm?sID=1";
}

function MM_validateDonationForm() { //v4.0
  var i,p,q,nm,nd,test,num,min,max,errors='',args=MM_validateDonationForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; nd=val.id; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nd+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nd+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nd+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nd+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
  document.dForm.action = "https://www5839.ssldomain.com/rllchurch/submit-a-charitable-contribution.cfm?sID=1";
}

function MM_validateForm() { //v4.0
  var i,p,q,nm,nd,test,num,min,max,errors='',args=MM_validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
    if (val) { nm=val.name; nd=val.id; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nd+' must contain an e-mail address.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nd+' must contain a number.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nd+' must contain a number between '+min+' and '+max+'.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nd+' is required.\n'; }
  } if (errors) alert('The following error(s) occurred:\n'+errors);
  document.MM_returnValue = (errors == '');
}



function playHeaderImages() {
    var $active = $('#fsHeaderImage DIV.active');
		
		if ( $active.length == 0 ) $active = $('#fsHeaderImage DIV:last');
	
		var $next =  $active.next().length ? $active.next()
			: $('#fsHeaderImage DIV:first');
	
		$active.addClass('last-active');
	
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 1000, function() {
				$active.removeClass('active last-active');
			});

}

var bOn = 0;
var nCount = 0;
var pp = 1;
var iBoxes = new Array("fsbriOne","fsbriTwo","fsbriThree");
var nBoxes = new Array("nOne","nTwo","nThree");

function playInfoBox(b) {
    var $active = $('#fsbrInfo DIV.active');
	if(b >= 0) {
		pp = 1;
		startIB();
		$active.addClass('last-active');
		$next = $('#fsbrInfo DIV#'+iBoxes[b]);
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 500, function() {
				$active.removeClass('active last-active');
			});
		
		for(i=0;i < nBoxes.length;i++) {
			if (i == b) {
				$btn = $('#fsbriNav DIV#'+nBoxes[i]);
				$btn.css({opacity: 0.3})
				.animate({opacity: 1.0}, 500, function() {
				});
			} else { 
				if (i == bOn) {
					$btn2 = $('#fsbriNav DIV#'+nBoxes[i]);
					$btn2.css({opacity: 1.0})
					.animate({opacity: 0.3}, 500, function() {
					});
				}
			}
		}
		bOn = b;
	} else {
		
		if ( $active.length == 0 ) $active = $('#fsbrInfo DIV:last');
	
		var $next =  $active.next().length ? $active.next()
			: $('#fsbrInfo DIV:first');
	
		$active.addClass('last-active');
	
		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 500, function() {
				$active.removeClass('active last-active');
			});
		if(nCount == 2) { nCount = 0; } else { nCount++; }
		for(i=0;i < nBoxes.length;i++) {
			if (i == nCount) {
				$btn = $('#fsbriNav DIV#'+nBoxes[i]);
				$btn.css({opacity: 0.3})
				.animate({opacity: 1.0}, 500, function() {
				});
			} else { 
				if (i == bOn) {
					$btn2 = $('#fsbriNav DIV#'+nBoxes[i]);
					$btn2.css({opacity: 1.0})
					.animate({opacity: 0.3}, 500, function() {
					});
				}
			}
		}
		bOn = nCount;
			
		if(pp == 1) {
			$('#fsbrInfo').hover(function() {
				clearInterval(playIBox);
				pp = 0;
				return false;
			},function() {
				clearInterval(playIBox);
				playIBox = setInterval( "playInfoBox(-1)", 3000 );
				pp = 1;
				return false;
			});
		}
	}
}

function startIB () {
	if(pp == 1) {
		$('#ppIndicator').css('backgroundImage','url(images/play.gif)');
		pp = 0;
		clearInterval(playIBox);
	} else {
		$('#ppIndicator').css('backgroundImage','url(images/pause.gif)');
		pp = 1;
		playIBox = setInterval( "playInfoBox(-1)", 3000 );
	}
}

function changeNewsletter() {
	var v=MM_findObj("viewer"); 
	var n = document.nForm.nFile.options[document.nForm.nFile.selectedIndex].value;
	v.src = n;
}

function setSides() {
		Nifty("div#fccclbcLeft,div#fccclbcRight", "none same-height");
		Nifty("div#fccFrameLeft,div#fccCenterBody,div#fccFrameRight", "none same-height");
}

function changeDate(x) {
	var datesArray = new Array();
	datesArray = x.split(",");
	$('#calendar').fullCalendar('gotoDate',datesArray[0],datesArray[1],datesArray[2]);	
}

function setPrice(r,t) {
	if (thisPage == "make-a-charitable-contribution" || thisPage == "submit-a-charitable-contribution") {
		$("#ff").val($("#Fund :selected").text());
		if($("#Fund").val().substring($("#Fund").val().lastIndexOf(",")+1)) {
			$("#ff").val($("#ff").val()+": "+$("#DonorSpecified").val());	
		}
		$("#fda").val($("#Amount").val());
	}
}

function setConf() {
	var confBText = '<div class="cHeader">Billing Details</div>' +
	MM_findObj('First Name').value + ' ' + MM_findObj('Last Name').value + '<br />' +
	MM_findObj('Address') .value+ '<br />' +
	MM_findObj('City').value + ', ';
	
	confBText += MM_findObj('State').value + ' ' + MM_findObj('Zip').value + '<br />';
	confBText += 'Phone: ' + MM_findObj('Phone').value + '<br />' + 'Email: ' + MM_findObj('Email').value + '<br />';
	
	var confCText = '<div class="cHeader">Credit Card Details</div>' +
	 MM_findObj('ccType').value.toUpperCase() + ' ending in: ' + MM_findObj('ccNumber').value.substring(MM_findObj('ccNumber').value.length-4) + '<br />' +
	'Expiration Date: ' + MM_findObj('ccMonth').value + '/' + MM_findObj('ccYear').value + '<br />';

	MM_findObj('confB').innerHTML = confBText;
	MM_findObj('confC').innerHTML = confCText;

	Nifty("div#confB,div#confC", "none same-height");
	
	setPrice(1,7);
	
}

function testCreditCard () {
  var myCardNo = MM_findObj('ccNumber').value;
  var myCardType = MM_findObj('ccType').value;
  if (checkCreditCard(myCardNo,myCardType)) {
    $(MM_findObj('ccNumber')).removeClass("error");
  } 
  else {
	 alert (ccErrors[ccErrorNo]);
  	$(MM_findObj('ccNumber')).addClass("error");
  }
}

function checkNI(i) {
	thisFund = $("#Fund").val();
	thisCheck = thisFund.substring(thisFund.lastIndexOf(",")+1);
	if(thisCheck == 1) {
		$("#otherInput").css("display","block");
	} else {
		$("#otherInput").css("display","none");	
	}
}
var formCheck = 1;


// mini jQuery plugin that formats to two decimal places
    (function($) {
        $.fn.currencyFormat = function() {
            this.each( function( i ) {
                    if( isNaN( parseFloat( this.value ) ) ) return;
                    this.value = parseFloat(this.value).toFixed(2);
            });
            return this; //for chaining
        }
    })( jQuery );
	
function callFailFunction() {
	//alert("oops!");	
}

var thisPage = document.URL.substring(document.URL.lastIndexOf("/")+1 ,document.URL.length);
thisPage = thisPage.substring(0 ,thisPage.lastIndexOf("."));
if(thisPage  == "index" || thisPage == '') {
	//var playIBox = setInterval( "playInfoBox(-1)", 3000 );
	//var playHI = setInterval( "playHeaderImages()", 6000 );
}


$(document).ready(function(){
	//thisPage = document.URL.substring(document.URL.lastIndexOf("/")+1 ,document.URL.length);
	//thisPage = thisPage.substring(0 ,thisPage.lastIndexOf("."));

	if(thisPage  == "index" || thisPage == '') {
		Nifty("div#fcccLeft,div#fcccRight", "none same-height");
		Nifty("div#fccFrameLeft,div#fccCenter,div#fccFrameRight", "none same-height");
	}


	if(thisPage == "contact" || thisPage == "camp-information") {
		setTimeout("initialize()",500);
	}
	
	
	Nifty("div.fhcrBtn,div.fhcrBtn2","bottom big transparent");
	
	if(thisPage == "make-a-charitable-contribution" || thisPage == "submit-a-charitable-contribution") {
		if(formCheck) {
		if(!ie7) {
			Nifty("ul#status li", "transparent");
		}
		
		var root = $("#orderForm").scrollable();
		
		// some variables that we need
		var api = root.scrollable(), drawer = $("#drawer");
		
		// validation logic is done inside the onBeforeSeek callback
		api.onBeforeSeek(function(event, i) {
		
			// we are going 1 step backwards so no need for validation
			if (api.getIndex() < i) {
		
				// 1. get current page
				var page = root.find(".page").eq(api.getIndex()),
		
					 // 2. .. and all required fields inside the page
					 inputs = page.find(".required :input").removeClass("error"),
		
					 // 3. .. which are empty
					 empty = inputs.filter(function() {
						return $(this).val().replace(/\s*/g, '') == '';
					 });
		
				 // if there are empty fields, then
				if (empty.length) {
		
					// slide down the drawer
					drawer.slideDown(function()  {
		
						// colored flash effect
						drawer.css("backgroundColor", "#900");
						setTimeout(function() { drawer.css("backgroundColor", "#fff"); }, 1000);
					});
		
					// add a CSS class name "error" for empty & required fields
					empty.addClass("error");
		
					// cancel seeking of the scrollable by returning false
					return false;
		
				// everything is good
				} else {
		
					// hide the drawer
					drawer.slideUp();
				}
		
			}
		
			// update status bar
			$("#status li").removeClass("active").eq(i).addClass("active");
		
		});
		
		
		
		// if tab is pressed on the next button seek to next page
		root.find("button.next").keydown(function(e) {
			if (e.keyCode == 9) {
		
				// seeks to next tab by executing our validation routine
				api.next();
				e.preventDefault();
			}
		});
		}
		
	}
	
	if(thisPage == "calendar") {
		$('#calendar').fullCalendar({
				header: {
				left: 'prev,next today',
				center: 'title',
				right: 'month,agendaWeek,agendaDay'
				},
				editable: false,
				
				events: "getEvents.cfm",
				
				eventRender: function(event, element) {
					if(event.url != undefined) {
						element.colorbox({iframe:true, innerWidth:460, innerHeight:560, href:event.url});
					}
				},
				
				loading: function(bool) {
					if (bool) {
						$('#loading').show();
					} else {
						$('#fccclbcLeft').css({'height' : ''});
						$('#fccclbcRight').css({'height' : ''});
						Nifty("div#fccclbcLeft,div#fccclbcRight", "none same-height");
						Nifty("div#fccFrameLeft,div#fccCenterBody,div#fccFrameRight", "none same-height");
						$('#loading').hide();
					}
				}
				
		});
		
		$('#membercalendar').fullCalendar({
				header: {
				left: 'prev,next today',
				center: 'title',
				right: 'month,agendaWeek,agendaDay'
				},
				editable: false,
				
				events: "members/getEvents.cfm",
				
				eventRender: function(event, element) {
					if(event.url != undefined) {
						element.colorbox({iframe:true, innerWidth:460, innerHeight:560, href:event.url});
					}
				},
				
				loading: function(bool) {
					if (bool) {
						$('#loading').show();
					} else {
						$('#fccclbcLeft').css({'height' : ''});
						$('#fccclbcRight').css({'height' : ''});
						Nifty("div#fccclbcLeft,div#fccclbcRight", "none same-height");
						Nifty("div#fccFrameLeft,div#fccCenterBody,div#fccFrameRight", "none same-height");
						$('#loading').hide();
					}
				}
				
		});
	}
	
	
	if(thisPage  !== "index" || thisPage !== '' || thisPage !== "calendar") {
		setSides();
	}
	
		
	$("ul.topnav li span").hover(function() { //When trigger is hovered...
	
		//Following events are applied to the subnav itself (moving subnav up and down)
		$(this).parent().find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on hover
	
		$(this).parent().hover(function() {
		}, function(){
			$(this).parent().find("ul.subnav").slideUp('slow'); //When the mouse hovers out of the subnav, move it back up
		});
	
		//Following events are applied to the trigger (Hover events for the trigger)
		}).hover(function() {
			$(this).addClass("subhover"); //On hover over, add class "subhover"
		}, function(){	//On Hover Out
			$(this).removeClass("subhover"); //On hover out, remove class "subhover"
	});
	
	MM_preloadImages('images/spacer.gif');
	
	$(".elNav:first").tabs("#eventsList:first > .eBox", { effect: 'fade', fadeOutSpeed: "slow", rotate: true }).slideshow({ autoplay: true, interval: 5000 });
	
	$(".memberBox").colorbox({iframe:true, innerWidth:350, innerHeight:250});
	$(".eventBox").colorbox({iframe:true, innerWidth:460, innerHeight:560});
	$(".archiveBox").colorbox({iframe:true, innerWidth:550, innerHeight:600});
	
	if(thisPage  == "committees" || thisPage == "resources") {
		Nifty("div.crRight", "transparent");
	}
	
	if(thisPage  == "links") {
		Nifty("a.lLink", "transparent");
	}
	
});

