var numLinks = 1;
var emailFilter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9_\.\-])+\.)+([a-zA-Z0-9])+$/;
var telFilter  = /^([0-9 ]{9,20})$/; 

function createAlbum() {
    var msg = "";
    if (document.form1.albumName.value=="") {
        msg+="Please enter a name for this album.\n";
    }
    if (msg.length==0) {
        document.form1.submit();
    } else {
        alert(msg);
    }    
}

function addPerson(maxLinks) {
    
    if (numLinks < Number(maxLinks)) {
        document.getElementById('person' + numLinks).style.display="";
        numLinks++;
    } else {
        alert("Number of links maxxed out!");
    }
}

function confirmDelete(targetPage) {
    if (confirm("Are you sure you want to delete?")) {
        window.location.href=targetPage;
    }
}

function showSection(s) {
//alert(s);
	if (document.getElementById('section0') != null) {
        document.getElementById('section0').style.display="none";
		if (s!=3) {
			document.getElementById('tab0').style.background="#999999";
			document.getElementById('tabx0').style.background="#999999";
		}
	}
    if (document.getElementById('section1') != null) {
        document.getElementById('section1').style.display="none";
		document.getElementById('tab1').style.background="#999999";
		document.getElementById('tabx1').style.background="#999999";
    }
    if (document.getElementById('section2') != null) {
        document.getElementById('section2').style.display="none";
		document.getElementById('tab2').style.background="#999999";
		document.getElementById('tabx2').style.background="#999999";
    }
    if (document.getElementById('section3') != null) {
        document.getElementById('section3').style.display="none";
    }
	document.getElementById('section' + s).style.display="";
	if (s != 3) {
	    document.getElementById('tab' + s).style.background="#FF6600";
	    document.getElementById('tabx' + s).style.background="#FF6600";
	}
}

function showInnerSection(s) {
//alert(s);
	if (document.getElementById('section0') != null) {
        document.getElementById('section0').style.display="none";
	}
	document.getElementById('section' + s).style.display="";
}

var now = new Date();
var defaultDay = now.getDate();
var defaultMonth = now.getMonth()+1; // numbered from 0
var defaultYear = now.getYear();
if (defaultYear < 1900) defaultYear += 1900;
var MONTH_NAMES = ["???","Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
/**
 * Function to set the values of the Date selectors
 * @param theForm The form bearing the date selectors
 */
function setDrops(day,month,year,endingDay,endingMonth,endingYear) {
    if (day=="" || month == "" || year == "") {
        day = defaultDay;
        month = MONTH_NAMES[defaultMonth];
        endingMonth = MONTH_NAMES[defaultMonth];
        year = defaultYear;
        endingDay = defaultDay + 1;
        endingYear = year;
    }
    
    
    for (i = 0; i < document.formSearchUsersByDate.month.length; i++) {
        if (document.formSearchUsersByDate.month.options[i].value == month) {
            document.formSearchUsersByDate.month.selectedIndex = i;
            break;
        }
    }

    for (i = 0; i < document.formSearchUsersByDate.endingMonth.length; i++) {
        if (document.formSearchUsersByDate.endingMonth.options[i].value == endingMonth) {
            document.formSearchUsersByDate.endingMonth.selectedIndex = i;
            break;
        }
    }

    for (i = 0; i < document.formSearchUsersByDate.year.length; i++) {
        if (document.formSearchUsersByDate.year.options[i].value == year) {
            document.formSearchUsersByDate.year.selectedIndex = i;
            break;
        }
    }

    for (i = 0; i < document.formSearchUsersByDate.endingYear.length; i++) {
        if (document.formSearchUsersByDate.endingYear.options[i].value == endingYear) {
            document.formSearchUsersByDate.endingYear.selectedIndex = i;
            break;
        }
    }

    document.formSearchUsersByDate.day.selectedIndex = day-1;
    document.formSearchUsersByDate.endingDay.selectedIndex = endingDay-1;
}

var currentPhotoIndex = 0;
var totalPhotos = 1;

function updatePhotoIndicdes () {
    currentPhotoIndex = document.getElementById("imageIndex").innerHTML-1;
    totalPhotos = document.getElementById("photoCount").innerHTML-1;
}
function prevPhoto() {
    updatePhotoIndicdes();
    if (currentPhotoIndex > 0) {
        currentPhotoIndex--;
    } else {
        currentPhotoIndex = totalPhotos;
    }
    updateImageDisplay();
    
}

function nextPhoto() {
    updatePhotoIndicdes();
    if (currentPhotoIndex < totalPhotos) {
        currentPhotoIndex++;
    } else {
        currentPhotoIndex = 0;
    }
    updateImageDisplay();
}

function expandThumb(f) {
    updatePhotoIndicdes();
    currentPhotoIndex = f;
    updateImageDisplay();
}

function updateImageDisplay() {
    
/*
    if (currentPhotoIndex == totalPhotos) {
        document.getElementById("btnNext").src="pics/btnNextOff.gif";
    } else {
        document.getElementById("btnNext").src="pics/btnNext.gif";
    }
	if (currentPhotoIndex == 0) {
        document.getElementById("btnPrev").src="pics/btnPrevOff.gif";
    } else {
        document.getElementById("btnPrev").src="pics/btnPrev.gif";
    }
*/
    document.getElementById("mainImage").src = thumbs[currentPhotoIndex].src;
	document.getElementById("caption").innerHTML = thumbs[currentPhotoIndex].caption;
	if(thumbs[currentPhotoIndex].comments != "") {
	    document.getElementById("comments").innerHTML = thumbs[currentPhotoIndex].comments;
	} else {
	    document.getElementById("comments").innerHTML = "<br>&nbsp;";
	}
	document.getElementById("imageIndex").innerHTML = currentPhotoIndex+1;
}

function deleteImage() {
    if (confirm("Are you sure you want to delete?")) {
        alert("PLEASE NOTE - the deleted image may still appear in your browser.\n\nIf this occurs, it means that your browser has cached the image.\n\nYou can solve this by reloading the page (Ctrl+F5 in Internet Explorer).\n\nIf this does not work, you can clear your cache.");
//        alert("gallery.asp?action=deletePhoto&album=" + thumbs[currentPhotoIndex].album + "&photo=" + thumbs[currentPhotoIndex].ID + "&filename=" + thumbs[currentPhotoIndex].filename);
        window.location.href="gallery.asp?action=deletePhoto&album=" + thumbs[currentPhotoIndex].album + "&photo=" + thumbs[currentPhotoIndex].ID + "&filename=" + thumbs[currentPhotoIndex].filename;
    }
}

function confirmDelete(url) {
    if (confirm("Are you sure?")) {
        window.location.href=url;
    }
}

function openpopup(popurl){
	winpops=window.open(popurl,"","width=400,height=338,scrollbars,resizable,");
}


function renameAlbum (ID) {
    var newName;
    var accepted = false;
    while(!accepted) {
        newName = prompt("Please rename the gallery");
                
        // make sure that the page name is not null (i.e. user cancelled)
        if (newName==null) {
            return;
        }
    
        var albumNameFilter  = /^([a-zA-Z0-9_\- ])+$/;

        if (!albumNameFilter.test(newName)) {
            alert("Please use only letters, spaces and numbers in your album name.");
        } else {
            accepted = true;
        }
    }

    var loc = "gallery.asp?ID=" + ID + "&action=rename&newName=" + newName
//    alert(loc);
    document.location=loc;
}
function setAsThumb(album) {    window.location.href="gallery.asp?action=setIcon&album=" + album + "&photo=" + thumbs[currentPhotoIndex].ID;
}
function move(index,to) {
	var list = document.form1.order;
	var total = list.options.length-1;
	
	if (index == -1) return false;
	if (to == +1 && index == total) return false;
	if (to == -1 && index == 0) return false;

    var bufferV = list.options[index].value;
    var bufferT = list.options[index].text;
    
    var bufferVN = list.options[index+to].value;
    var bufferTN = list.options[index+to].text;

    list.options[index].value = bufferVN;
    list.options[index].text = bufferTN;
    
    list.options[index+to].value = bufferV;
    list.options[index+to].text = bufferT;    
    list.selectedIndex += to;

	list.focus();

}

function submitOrder() {
	document.form1.orderOutput.value = "";
	var c = 0;
	for (var i = document.form1.order.options.length-1; i >= 0 ; i--) { 
        document.form1.orderOutput.value += c + "=" + document.form1.order.options[i].value;
        c++;
        if (i > 0) {
            document.form1.orderOutput.value += "|";
	    }
	}
	document.form1.submit();
	
}