// functions.js

//constants
var DELETE_USER_WARNING = 'Do you really want to delete these user(s)??';
var DELETE_IMAGE_WARNING = 'WARNING: Are you sure you want to delete this image?  By clicking OK you will PERMANENTLY delete this image and all data associated with this image, including this image\'s comments.';
var DELETE_ALBUM_WARNING = 'WARNING: Are you sure you want to delete this album?  By clicking OK you will PERMANENTLY delete this album.  Note: you can edit and delete individual images by clicking cancel and then clicking on the image you want to edit';
var CANCEL_IMAGE_WARNING = 'Are you sure you want to cancel?  Any changes you have made since last clicking Save will be lost.';

var COOKIES_NOT_ENABLED_WARNING = 'Cookies are not enabled on you computer.  Cookies must be enabled to login to this site.  To enable cookies in Internet Explorer go to Tools->Internet Options, click privacy and choose a less restrictive setting';

//file index counter
var imageFileIndex = 1;

//view image more info counter
var textIsExpanded = false;


// Table functions
function rowHover(obj) {
	obj.style.backgroundColor='#FFE6CC';
	obj.style.cursor='pointer';
}

function rowUnhover(obj) {
	obj.style.backgroundColor='';
}

function thumbHover(obj) {
	obj.style.border='1px solid #FF9933';
	obj.style.cursor='pointer';
}

function thumbUnhover(obj) {
	obj.style.border='1px solid #CCCCCC';
}

function rowOnClick(rowUrl) {
	window.location=rowUrl;
}


//open a new window
//params - (url to open, width, height, vertical coords, horizontal coords)
function newWindow(url, width, height, left, top) {
	paramString = 'width=' + width + 
				  ',height=' + height + 
				  ',left=' + left + 
				  ',top=' + top +
				  'screenX=' + left +  //for nescape
				  'screenY=' + top;
	window.open(url, '', paramString);
}

function sendWarningMessage(message) {
	if (confirm(message)) {
		return true;
	} else {
		return false;
	}
}

//if this site is in a frame break out of it
function breakOutOfFrames() {
	if (top != self) {
        top.location=self.location;
    }
}

function getObj(id) {
	var i,x;
	var d=document; 
	if(!(x=d[id])&&d.all) x=d.all[id]; 
	for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][id];
	if(!x && document.getElementById) x=document.getElementById(id); 
	return x;
}

//add another file input field
function addFileInput() {
	imageFileIndex++;
	getObj("imageFile" + imageFileIndex).style.display = "block";
	if(imageFileIndex >= 20) {
		getObj("moreImagesLink").style.display = "none";
		getObj("noMoreImagesText").style.display = "block";
	}
}

//display the Please Wait message
function activatePleaseWaitMessage() {
	
	//after adding compression to upload tool uncomment if stmt
	getObj("pleaseWaitMessage").style.display = "block";
	//if (hasEnteredImages()) {
		//getObj("pleaseWaitMessage").style.display = "block";
	//}
	
	
	
	getObj("moreImagesLink").style.display = "none";
	//disable forms
	for (i=1; i<=50; i++) {
		getObj("imageFile" + i).readOnly = true;
	}
	return true;
}

//has client entered images to be uploaded
function hasEnteredImages() {
	for (i=1; i<=50; i++) {
		if (getObj("imageFile" + i).value != "") {
			return true;
		}
	}
	return false;
}

//display the upload didn't work instructions
function uploadDidntWork() {
	getObj("uploadDidntWorkLink").style.display = "none";
	getObj("uploadDidntWorkText").style.display = "block";
}

//toggle moreInfoText on viewImage
function toggleMoreInfoText() {
	//get the elements we need to modify
	//note- document.getElementsByName() doesn't work very well in ie
	var moreInfoText = getObj("moreInfoText");
	var lessInfoText = getObj("lessInfoText");
	var moreInfoTable = getObj("moreInfoTable");
	
	if (textIsExpanded) {
		moreInfoText.style.display = "block";
		moreInfoTable.style.display = "none";
		lessInfoText.style.display = "none";
		textIsExpanded = false;
	} else {
		moreInfoText.style.display = "none";
		moreInfoTable.style.display = "block";
		lessInfoText.style.display = "block";
		textIsExpanded = true;
	}
}

//Returns true if the user has cookies enabled, false otherwise.
function checkForCookes() {
	var cookieEnabled=(navigator.cookieEnabled) ? true : false
	
	//if not IE4+ nor NS6+
	if (typeof navigator.cookieEnabled=="undefined" && !cookieEnabled){
		document.cookie="testcookie"
		cookieEnabled=(document.cookie.indexOf("testcookie")!=-1)? true : false
	}

	if (cookieEnabled) {
		return true;
	} else {
		alert(COOKIES_NOT_ENABLED_WARNING);
		return false;
	}
}

//Change order functions
function postNewOrder() {
	//get all list elements and set in select
	var currSelect = getObj("elementsArray");
	var ul = getObj("sortableList");
	var lis = ul.childNodes;
	for (i=0; i<lis.length; i++) {
		var currId = lis[i].getAttribute('id');
		//set all elements in form
		var currOpt = new Option(currId);
		currOpt.selected = true;
		currSelect.options[i] = currOpt;
	}
}