<!-- // JavaScript Document

var d = document;

//GET THE SERVER NAME TO USE ABSOLUTE URLS WHERE NEEDED
function serverName()
{
	var server; var url = location.href;
	if(url.indexOf("localhost") != -1)		{ server = "http://localhost/friendsoftheelderly.ie"; }
	else if(url.indexOf("https://") != -1)	{ server = "https://www.friendsoftheelderly.ie"; }
	else if(url.indexOf("klstudios") != -1)	{ server = "http://www.foe.klstudios.ie"; }
	else									{ server = "http://www.friendsoftheelderly.ie"; }
	if(url.indexOf("_office") != -1) { server += "/_office"; } return server;
}

function getSelectedIndex(cellID) //GET THE SELECTED INDEX VALUE OF A SELECT BOX
{
	var selectedItem = d.getElementById(cellID).selectedIndex; var selectedvalue = d.getElementById(cellID).options[selectedItem].value; return selectedvalue;
}

function windowSize(type)
{
	var w = 0; var h = 0; if(!window.innerWidth) { 
		if(!(document.documentElement.clientWidth == 0)) { w = d.documentElement.clientWidth; h= d.documentElement.clientHeight; } //IE STRICT
		else 											 { w = d.body.clientWidth; h = document.body.clientHeight; }} //QUIRKS MODE
	else 												 { w = window.innerWidth; h = window.innerHeight; } // W3C
	if(type=="width") { return w; } else { return h; }
}

function reloadWindow()
{
	window.location = window.location;
}

function logout()
{
	window.location = serverName()+"/_lib/ajax.php?logout";
}

// START SLIDESHOW FUNCTIONS
var slideShowTimer, lastSlide, thisSlide = 1, slideShowTimerCount = 0;

$(function() { 
	$('.slideContainer').hide(); 
	$('.galleryImageName').css({ 'opacity':0.8 }); 
	$('#slideshow').mouseover(function()	{ clearTimeout(slideShowTimer); slideShowTimerCount = 0; });
	$('#slideshow').mouseleave(function()	{ startSlideshowTimer(); });
	//IF HOMEPAGE FLASH PRESENT
	if(d.getElementById('slideFlash'))		{ setTimeout('startSlideshowTimer()',19000);  $('#slideFlash').show(); } else { startSlideshowTimer(); $('#slideContainer1').show(); }
});

function autoSlideshow()
{
	runSlideShow(); slideShowTimer=setTimeout("autoSlideshow()",3000);	
}

function startSlideshowTimer()
{
	if(d.getElementById('slideFlash')) { $('#slideFlash').fadeOut(); }
	if (slideShowTimerCount == 0) { slideShowTimerCount=1; autoSlideshow(); }
}

function manualShow()
{
	if(slideShowTimerCount == 1) { clearTimeout(slideShowTimer); slideShowTimerCount = 0; } runSlideShow();
}

function runSlideShow()
{
	var totalSlides = ($('#slideshow').children().size())-2;
	if(thisSlide == totalSlides) { lastSlide = totalSlides; thisSlide = 1; } else { thisSlide += 1; lastSlide = thisSlide-1; }
	$('#slideContainer'+lastSlide).fadeOut(); $('#slideContainer'+thisSlide).fadeIn();
}
// END SLIDESHOW FUNCTIONS

// FILE STRIP FUUNCTIONS
var  position = 0, rightPosition, thisFileWidth, thisGalleryImage = 1; lastVisibleImage = 1;

function slideGallery(direction)
{
	var totalImagesWidth = 0;
	var b = $('#galleryPanel img').size();
	var galleryContainer = parseInt($('#galleryContainer').outerWidth());
	for(var a = 1; a <= b; a++) { totalImagesWidth += parseInt($('#imgContainer_'+a).outerWidth()); }
	rightPosition = totalImagesWidth + position;
	if(direction == 'left') { if(rightPosition > galleryContainer)  {
			thisFileWidth = parseInt($('#imgContainer_'+thisGalleryImage).outerWidth()); position -= thisFileWidth;
			$('#galleryPanel').animate({ left : '-='+thisFileWidth+'' }); thisGalleryImage +=1; } }
	else { if(position < 0)  {
			thisGalleryImage -=1; thisFileWidth = parseInt($('#imgContainer_'+thisGalleryImage).outerWidth());
			position += thisFileWidth; $('#galleryPanel').animate({ left : '+='+thisFileWidth+'' }); } }
}

//AUTO GALLERY TIMER
var galleryTimer, galleryShowTimerCount = 0;

$(function() { 
	$('#gallery').mouseover(function()	{ clearTimeout(galleryTimer); galleryShowTimerCount = 0; });
	$('#gallery').mouseleave(function()	{ startGalleryTimer(); });	
	 startGalleryTimer();
});

function startGalleryTimer()
{
	if (galleryShowTimerCount == 0) { galleryShowTimerCount=1; autoGalleryShow(); }
}

function autoGalleryShow()
{
	runGallerySlideshow(); galleryTimer=setTimeout("autoGalleryShow()",5000);	
}

function runGallerySlideshow()
{
	var currentPosition = $('#galleryPanel').position(); var totalImagesWidth = 0; var b = $('#galleryPanel img').size();
	var galleryContainer = parseInt($('#galleryContainer').outerWidth());
	for(var a = 1; a <= b; a++) { totalImagesWidth += parseInt($('#imgContainer_'+a).outerWidth()); }
	rightPosition = totalImagesWidth + position;
	if(rightPosition > galleryContainer) { 
		thisFileWidth = parseInt($('#imgContainer_'+thisGalleryImage).outerWidth()); position -= thisFileWidth;
		$('#galleryPanel').animate({ left : '-='+thisFileWidth+'' },2000); thisGalleryImage +=1; }
	else {
		thisFileWidth = parseInt($('#imgContainer_1').outerWidth()); position = 0;
		$('#galleryPanel').animate({ left : '0px' },2500); thisGalleryImage =1; }
}

function manualGalleryShow()
{
	if(galleryShowTimerCount == 1) { clearTimeout(startGalleryTimer); galleryShowTimerCount = 0; } runGallerySlideshow();
}
//END IMAGE STRIP FUNCTIONS

$(document).keypress(function(e) { if(e.keyCode == 27) { doBackground('hide'); } });

function doBackground(action,panelId,formHeight)
{
	if(action == "show") {
		scrollTo(0,0); var posLeft = ((windowSize("width"))-700)/2; var panelHeight = windowSize("height");
		d.getElementById("popupContainer").style.left = posLeft+"px"; 
		d.getElementById("popupBackground").style.height = panelHeight+"px";
		$('#popupBackground').show().animate({"opacity": .5 }); $('#popupContainer').fadeIn(); 
		$('.donateNow').hide(); // if(d.getElementById('donateNow')) { d.getElementById('donateNow').style.display = 'none'; }
		$('.indexMovie').hide(); // if(d.getElementById('indexMovie')) { d.getElementById('indexMovie').style.display = 'none'; }
		if(panelId == "processingDonation")
			{
				$('#popupContent').css({height:formHeight+'px'}); $('#'+panelId).show();
			}
		else
			{
				$('#popupContent').animate({height:formHeight+'px'}); setTimeout('$(\'#'+panelId+'\').fadeIn()',1000); 
			} 
		}
	else {
		$('#popupContent').animate({height:'0px'}); 
		$('.panelForm').fadeOut(); 
		$('.panelFormInput').val('');
		$('.formInfo').css("visibility", "hidden");
		$('.runningForm').hide();
		$('#imageBlowUp').val('<img src="'+serverName()+'/_images/icons/loadingFull.gif" alt="loadingImage" />');
		$('#imageBlowUp').val('<img src="'+serverName()+'/_images/icons/loadingFull.gif" alt="loadingImage" />');
		$('.ajaxResponse').html(''); 
		setTimeout('closeBackground()',500); }
}

function closeBackground()
{
	$('#popupBackground').fadeOut(); 
	$('#popupContainer').hide();
	$('#popupContent').animate({height:'0px'});
	$('.donateNow').show(); $('.indexMovie').show();
//	if(d.getElementById('donateNow')) { d.getElementById('donateNow').style.display = 'block'; }
//	if(d.getElementById('indexMovie')) { d.getElementById('indexMovie').style.display = 'block'; }
}

function closeWindow(type)
{
	doBackground('close')
}

function doButton(buttonId,strId,action)
{
	var folder; var url = location.href;
	var newButton = serverName()+"/_images/buttons/"+buttonId; if(action) { newButton += action; } newButton += ".gif";
	d.getElementById(buttonId+strId).src = newButton;
}

function doPanel(panelId,panelHeight)
{
	if(panelHeight > 0) { doBackground("show",panelId,panelHeight); } else { doBackground("hide",panelId) }
	
}

function swapPanel(panel1,panel2,intHeight)
{
	$('#'+panel1).fadeOut(); setTimeout('$(\'#'+panel2+'\').fadeIn()',500);
}

//process forms
$(function() { 
	//LOGIN FORM
	$('#loginLogin').click(function()				{	$('#loginLoading').show(); setTimeout('processForm(\'login\')',500); return false; });
	$('#retreivePasswordPassword').click(function() {	$('#retreivePasswordLoading').show(); setTimeout('processForm(\'retreivePassword\')',500); return false; });
	$('#newsletterLogin').click(function() 			{	$('#newsletterLoading').show(); setTimeout('processForm(\'newsletter\')',500); return false; });
	$('#recommendLogin').click(function() 			{	$('#recommendLoading').show(); setTimeout('processForm(\'recommend\')',500); return false; });
	$('#signPetitionSubmit').click(function()		{	$('#signUpLoading').show(); setTimeout('processForm(\'signUp\')',500); return false; });
});

function processForm(formName)
{
	var arrCells, strResult; var sendForm = true;  var data = Array();  var query = "formname="+formName;
	if(formName == 'login') 			{ arrCells = Array('username','password'); strResult = "You are now logged in";   }
	if(formName == 'retreivePassword')	{ arrCells = Array('usernamepassword'); strResult = "Your password been reset and has been sent to your email account.";  }
	if(formName == 'newsletter') 		{ arrCells = Array('nlFirstname','nlSurname','nlEmail'); strResult = "Thank you, your details have been added to our mailing list"; }
	if(formName == 'recommend') 		{ arrCells = Array('rYourName','rYourEmail','rFriendName','rFriendEmail'); strResult = "Thank you, your recommendation has been sent"; }
	if(formName == 'signUp')			{ arrCells = Array('petitionSurname','petitionFirstname','petitionEmail','petitionAddress1','petitionCity','petitionPostcode'); strResult = "Thank you for your support and for being a friend of the elderly"; }

	for(var i = 0; i < arrCells.length; i++) { data[i] = $('#'+arrCells[i]).val(); if(data[i] == "") {
			d.getElementById(arrCells[i]+'Info').style.visibility = 'visible'; sendForm = false; } 
		else { query += "&"+arrCells[i]+"="+data[i]; }
		if(formName == "newsletter") { var company = $('#nlCompany').val(); if(company != "") { query += "&nlCompany="+company; } } }
	if(formName == "recommend")	{ query += "&rUrl="+$('#rUrl').val()+"&rTitle="+$('#rTitle').val(); }
	if(formName == "signUp") 	
		{ 
			query += "&petitionCountry="+$('#petitionCountry').val();
			if($('#petitionAddress2').val() != "") { query += "&petitionAddress2="+$('#petitionAddress2').val(); } 
			if(d.getElementById("getNewsletter").checked == true) { query += "&getNewsletter=true"; } 
		}
	if(sendForm == true)  {
		$.ajax ({	url: serverName()+"/_lib/ajax.php", type: 'POST', data: query,
		success: function(result) 
			{ 
				$('#'+formName+'FormResponse').html(result); 
				if(result == strResult) 
					{ 
						if(formName == "retreivePassword") 
							{ 
								setTimeout('swapPanel(\'retreivePassword\',\'login\',200)',3000);  
							}
						else if(formName == "signUp") 
							{
								return false;
							}
						else 
							{ 
								setTimeout('doBackground(\'close\')',3000); 
								alert("here2");
							} 
					} 
				} }); 
		}
	$('#'+formName+'Loading').hide();
}

function doArchive(yearId)
{
	if(d.getElementById('newsGroup'+yearId).style.display == 'none') {
		$('#newsGroup'+yearId).fadeIn();
		d.getElementById('newsArchiveH3'+yearId).style.background = "url("+serverName()+"/_images/icons/arrowDown.gif) no-repeat left center"; }
	else {
		$('#newsGroup'+yearId).fadeOut();
		d.getElementById('newsArchiveH3'+yearId).style.background = "url("+serverName()+"/_images/icons/arrowRight.gif) no-repeat left center"; }			
}

function doReferee(panelId,action)
{
	var i = panelId-1; if(action == "add") {
		$('#addReferee'+i).fadeOut(); $('#referee'+panelId).fadeIn(); }
	else {
		$('#addReferee'+i).fadeIn(); $('#referee'+panelId).fadeOut(); $('#referee'+panelId).children().val(''); }
}

function doImage(fileID,nextPrev)
{
	if(nextPrev) { 
		d.getElementById('imageShownCover').src = serverName()+"/_images/icons/imageCover.gif"; 
		d.getElementById('imageShown').src = serverName()+"/_images/icons/loadingFull.gif"; }
	else {
		scrollTo(0,0); doBackground('show','imageBlowUp',465); }
	setTimeout('callImage('+fileID+')',1000);
}

function callImage(fileID)
{
	var prevFile, nextFile; 
	var totalImages = $('#totalFiles').val(); var thisFile = $('#mediaId_'+fileID).val();
	if(fileID == 1)				{ prevFile = thisFile; } 
	else 						{ prevFile = d.getElementById("mediaId_"+(parseInt(fileID)-1)).value; }
	if(fileID == totalImages) 	{ nextFile = thisFile; } 
	else 						{ nextFile = d.getElementById("mediaId_"+(parseInt(fileID)+1)).value; } 
	$.ajax({ url: serverName()+"/_lib/ajax.php", type: 'GET',  
		data: 'viewfile='+thisFile+'&fileid='+fileID+'&prev='+prevFile+'&next='+nextFile+'&totalimages='+totalImages,
		success: function(result) { d.getElementById('imageBlowUp').innerHTML = result; } });
}

function doDocument(recordId)
{
	window.open(serverName()+'/document.php?id='+recordId,'','width=700,height=700,location=0,scrollbars=1');
}

function copyEmail()
{
	var email = $('#newsLetterEmail').val(); $('#nlEmail').val(email); doPanel('newsletter',300); return false;
}


//DONATION PAGE
function verifyNow() // WARNING JS POPUP THAT T&C HAS NOT BEED CHECKED AND CARD DETAILS ARE INCORRECT
{
	var pass = true; var msg = "Please complete the following data\r\n_____________________________________\r\n";
	if(d.getElementById("verifyOrder").checked != true) { 
		alert("Please confirm that you accept our terms and conditions"); pass = false; } 
	else { 
		var ccType 	= getSelectedIndex('credit_card_type'); var ccMonth = getSelectedIndex('credit_card_month')-1;
		var ccYear 	= parseInt(getSelectedIndex('credit_card_year')) + 2000; var ccCvv 	= d.getElementById('credit_card_cvv').value;
		var ccNo	= d.getElementById('credit_card_number').value; var today = new Date(); 
		var cardDate=new Date(); cardDate.setFullYear(ccYear,ccMonth,daysInMonth(ccMonth,ccYear));		
		if(d.getElementById('credit_card_name').value == ""){ msg = msg + "Please complete the name on the payment card\r\n"; pass = false; }
		if((ccNo == "")	|| (ccNo.length < 16))				{ msg = msg + "Please complete your payment card number\r\n"; pass = false; }
		if(cardDate < today) 								{ msg = msg + "The card expiriation date is invalid\r\n"; pass = false; }
		if(((ccType != 'laser') && (ccCvv.length != 3)) || (isNaN(ccCvv)==true)) { msg = msg + "Please complete your CVV number\r\n"; pass = false; }
		if(pass == false) { alert(msg); } }
	if(pass == true) { 
		doBackground('show','processingDonation',200); }	
	return pass;
}

function daysInMonth(iMonth, iYear) //number of days in a month for verifyNow()
{
	return 32 - new Date(iYear, iMonth, 32).getDate();
}

function ccType() //ACVTIVE CVV BOX BY SELECTED PAYMENT CARD TYPE
{
	var i = getSelectedIndex('credit_card_type'); var j = d.getElementById('credit_card_cvv'); 
	if(i == "laser") { j.value = ""; j.disabled = true; } else { j.disabled = false; }
}

function doDiv(divId,action)
{
	if(action==1) { $('#'+divId).show(); } else { $('#'+divId).hide(); }
}

function doDownload(documentId)
{
	window.location = serverName()+"/download.php?document="+documentId;
}
