function goBack() {
	window.history.go(-1);
}

function goToURL(url) {
	window.document.location.href = url;
}

function callOnLoad() {
	showErrorMsg();
	showNormMsg();
	if (window.onLoader) {
		window.onLoader();
	}
}

function callOnFocus() {
	if (window.document.onFocusAction) {
		window.document.onFocusAction();
	}
}

function callOnBeforeUnload() {
	if (getElement('blocker')) {
		getElement('blocker').style.width=window.document.body.clientWidth;
		getElement('blocker').style.height=window.document.body.clientHeight;
		getElement('blocker').style.visibility='visible';
	}
}

function hideAllMsg() {
	hideErrorMsg();
	hideNormMsg();
}

function hideErrorMsg() {
	if (getElement('validationError')) {
		getElement('validationError').style.display = 'none';
		getElement('error_message').innerHTML = '';
	}
}

function showErrorMsg() {
	if (getElement('validationError') && window.error_msg != '') {
		getElement('validationError').style.display = 'block';
		getElement('error_message').innerHTML = window.error_msg;
	}
}

function hideNormMsg() {
	if (getElement('normalMsg')) {
		getElement('normalMsg').style.display = 'none';
		getElement('norm_message').innerHTML = '';
	}
}

function showNormMsg() {
	if (getElement('normalMsg') && window.norm_msg != '') {
		getElement('normalMsg').style.display = 'block';
		getElement('norm_message').innerHTML = window.norm_msg;
	}
}

var timeoutid = 0;
var timeout = 3000;
var allowslideshow = true;
function browserPause() {
	if (timeoutid != 0) {
    	clearTimeout(timeoutid);
     	timeoutid = 0;
    }
	if (getElement('browserButtonPlay')) {
		getElement('browserButtonPlay').src = '../images/pb_play.gif';
		getElement('browserButtonPause').src = '../images/pb_pause_on.gif';
	}
}
function browserClickPlay(newtimeout) {
	browserPause();
	allowslideshow = true;
    if (newtimeout) {
     	timeout = newtimeout;
    }
	if (allowslideshow) {
		if (getElement('browserButtonPlay')) {
			getElement('browserButtonPlay').src = '../images/pb_play_on.gif';
			getElement('browserButtonPause').src = '../images/pb_pause.gif';
		}
		timeoutid = setTimeout( "browserLoop()", timeout);
	} else  {
		browserPause();
	}
}
function browserPlay(newtimeout) {
	browserPause();
    if (newtimeout) {
     	timeout = newtimeout;
    }
	if (allowslideshow) {
   		timeoutid = setTimeout( "browserLoop()", timeout);
	} else  {
		browserPause();
	}
	getElement('browserButtonPlay').src = '../images/pb_play_on.gif';
	getElement('browserButtonPause').src = '../images/pb_pause.gif';
}
function browserLoop() {
	showNextPhoto(null,siteidvar);
    browserPlay();
}
function browserSlower() {
	if (timeout < 8000) {
		timeout = timeout + 1000;
		browserSetSpeedDisplay();
	}
}
function browserFaster() {
	if (timeout > 2000) {
		timeout = timeout - 1000;
		browserSetSpeedDisplay();
	}
}
function browserSetSpeedDisplay() {
	if (getElement('speedDisplay')) {
		getElement('speedDisplay').innerHTML = (10-timeout/1000);
	}
}

var tintLevel = 0;
function tintWindow() {
	if (getElement('tinter')) {
		getElement('tinter').style.width=window.document.body.clientWidth+"px";
		getElement('tinter').style.height=window.document.body.clientHeight+"px";
		getElement('tinter').style.visibility='visible';
		tintMore('tinter');
	}
}
function untintWindow() {
	if (getElement('tinter')) {
		tintLess('tinter');
	}
}
function tintMoreLoop(objName) {
	tintMore(objName);
}
function tintMore(objName) {
	var obj = getElement(objName);
	obj.style["filter"] = "alpha(opacity=" + tintLevel + ")";
	obj.style["opacity"] = "0." + tintLevel;
	obj.style["-moz-opacity"] = "0." + tintLevel;
	if (tintLevel<90) {
		tintLevel = tintLevel + 15;
		setTimeout("tintMoreLoop('"+objName+"')", 1);
	}
}
function tintLessLoop(objName) {
	tintLess(objName);
}
function tintLess(objName) {
	var obj = getElement(objName);
	obj.style["filter"] = "alpha(opacity=" + tintLevel + ")";
	obj.style["opacity"] = "0." + tintLevel;
	obj.style["-moz-opacity"] = "0." + tintLevel;
	if (tintLevel>0) {
		tintLevel = tintLevel - 15;
		setTimeout("tintLessLoop('"+objName+"')", 1);
	} else {
		getElement(objName).style.width='50px';
		getElement(objName).style.height='50px';
		getElement(objName).style.visibility='hidden';
	}
}

var currentPhotoIdx = 1;
function showPhotoBrowser() {
	tintWindow();
	if (getElement('photoBrowser')) {
		getElement('photoBrowser').style.display = 'block';
		hideChooseGallery();
	}
}
function hidePhotoBrowser(type) {
	untintWindow();
	if (getElement('photoBrowser')) {
		getElement('photoBrowser').style.display = 'none';
		if (getElement('currentPhoto') && type == null) {
			getElement('currentPhoto').src = '../images/photo_blank_450.gif';
		} else if (getElement('currentPhoto') && type == 'xlist') {
			getElement('currentPhoto').src = '../images/photo_blank_xlist.gif';
		}
		showChooseGallery();
	}
	browserPause();
}
function showPhoto(photoIdx,type) {
	if (getElement('photoBrowser')) {
		currentPhotoIdx = photoIdx;
		if (getElement('currentPhoto') && type == null) {
			getElement('currentPhoto').src = '../images/photo_blank_450.gif';
			getElement('currentPhoto').src = imageArray[photoIdx];
		} else if (getElement('currentPhoto') && type == 'xlist') {
			getElement('currentPhoto').src = '../images/photo_blank_xlist.gif';
			getElement('currentPhoto').src = imageArray[photoIdx];
		} else if (getElement('currentPhoto') && type == 'exclusive') {
			getElement('currentPhoto').src = '../images/photo_blank_450.gif';
			getElement('currentPhoto').src = imageArray[photoIdx][0];
			if (getElement('currentPhotoDate')) {
				getElement('currentPhotoDate').innerHTML = imageArray[photoIdx][1];
			}
			if (getElement('currentPhotoCaption')) {
				getElement('currentPhotoCaption').innerHTML = imageArray[photoIdx][2];
			}
		}
		showPhotoBrowser();
		if (typeof displayCaption != 'undefined') {
			displayCaption(photoIdx);
		}
		if (typeof displayEventname != 'undefined') {
			displayEventname(photoIdx);
		}
		browserPause();
		browserSetSpeedDisplay();
	}
}
function showPrevPhotoClick(type,siteid) {
	showPrevPhoto(type,siteid);
	browserPause();
}
function showPrevPhoto(type,siteid) {
	if (getElement('currentPhoto')) {
		var maxIdx = window.imageArray.length;
		var photoIdx = currentPhotoIdx-1;
		if (photoIdx==0) {
			photoIdx = maxIdx-1;
		}
		showPhoto(photoIdx,type);
		if (siteid != null) {
			var data = imageArray[photoIdx];
			addPhotoBrowserViewCount(siteid,data);
		}
		if (window.rotateEventAd) {
			rotateEventAd();
		}
	}
}
function showNextPhotoClick(type,siteid) {
	showNextPhoto(type,siteid);
	browserPause();
}
function showNextPhoto(type,siteid) {
	if (getElement('currentPhoto')) {
		var maxIdx = window.imageArray.length;
		var photoIdx = currentPhotoIdx+1;
		if (photoIdx==maxIdx) {
			photoIdx = 1;
		}
		showPhoto(photoIdx,type);
		if (siteid != null) {
			siteidvar = siteid;
			var data = imageArray[photoIdx];
			addPhotoBrowserViewCount(siteid,data);
		}
		if (window.rotateEventAd) {
			rotateEventAd();
		}
	}
}
function addPhotoBrowserViewCount(siteid,data){
	var tokens = data.tokenize("/", " ", true);
	if ( tokens.length < 4 ) {
		var date_dir = tokens[1];
		var filename = tokens[2];
		var filenamelen = filename.length;
		var filename = filename.substring(0,(filenamelen-5));
		addPhotoViewCount(siteid,date_dir,filename);
	}
}
function hideChooseGallery() {
	if (getElement('choose_gallery')) {
		getElement('choose_gallery').style.display = 'none';
	}
}
function showChooseGallery() {
	if (getElement('choose_gallery')) {
		getElement('choose_gallery').style.display = 'block';
	}
}

function loginFocus() {
	if (getElement('login_email_addr')) {
		getElement('login_email_addr').focus();
	}
}

function nonMember(index) {
	if (eval("getElement('nonmember"+index+"')")) {
		eval("getElement('nonmember"+index+"').style.display = 'block';");
	}
}

function getElement(psID) {
	if (eval("document."+psID)) {
		return eval("document."+psID);
	} else if (document.getElementById(psID)) {
		return document.getElementById(psID);
	} else if (document.all) {
   		return document.all[psID];
   	} else {
		return null;
	}
}

function getSelectedRadio(buttonGroup) {
	// returns the array number of the selected radio button or -1 if no button is selected
   	if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
    	for (var i=0; i<buttonGroup.length; i++) {
         	if (buttonGroup[i].checked) {
            	return i
         	}
      	}
   	} else {
      	if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   	}
   	// if we get to this point, no radio button is selected
   	return -1;
}
function getSelectedRadioValue(buttonGroup) {
	// returns the value of the selected radio button or "" if no button is selected
	var i = getSelectedRadio(buttonGroup);
	if (i == -1) {
	  	return "";
	} else {
		if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
			return buttonGroup[i].value;
		} else { // The button group is just the one button, and it is checked
			return buttonGroup.value;
		}
	}
}
function getSelectedCheckbox(buttonGroup) {
	// Go through all the check boxes. return an array of all the ones
	// that are selected (their position numbers). if no boxes were checked,
	// returned array will be empty (length will be zero)
	var retArr = new Array();
	var lastElement = 0;
	if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
		for (var i=0; i<buttonGroup.length; i++) {
			if (buttonGroup[i].checked) {
				retArr.length = lastElement;
				retArr[lastElement] = i;
				lastElement++;
			}
		}
	} else { // There is only one check box (it's not an array)
		if (buttonGroup.checked) { // if the one check box is checked
			retArr.length = lastElement;
			retArr[lastElement] = 0; // return zero as the only array value
		}
	}
	return retArr;
}
function getSelectedCheckboxValue(buttonGroup) {
	// return an array of values selected in the check box group. if no boxes
	// were checked, returned array will be empty (length will be zero)
	var retArr = new Array(); // set up empty array for the return values
	var selectedItems = getSelectedCheckbox(buttonGroup);
	if (selectedItems.length != 0) { // if there was something selected
		retArr.length = selectedItems.length;
		for (var i=0; i<selectedItems.length; i++) {
			if (buttonGroup[selectedItems[i]]) { // Make sure it's an array
				retArr[i] = buttonGroup[selectedItems[i]].value;
			} else { // It's not an array (there's just one check box and it's selected)
				retArr[i] = buttonGroup.value;// return that value
			}
		}
	}
	return retArr;
}

var photoViewCounter = new Image();
function addPhotoViewCount(site_id,date_dir,filename) {
	var str = "index.cfm?realm=gallerycount&site_id="+site_id+"&date_dir="+date_dir+"&filename="+filename;
	photoViewCounter.src = str;
}

String.prototype.tokenize = tokenize;

function tokenize()
  {
     var input             = "";
     var separator         = " ";
     var trim              = "";
     var ignoreEmptyTokens = true;

     try {
       String(this.toLowerCase());
     }
     catch(e) {
       window.alert("Tokenizer Usage: string myTokens[] = myString.tokenize(string separator, string trim, boolean ignoreEmptyTokens);");
       return;
     }

     if(typeof(this) != "undefined")
       {
          input = String(this);
       }

     if(typeof(tokenize.arguments[0]) != "undefined")
       {
          separator = String(tokenize.arguments[0]);
       }

     if(typeof(tokenize.arguments[1]) != "undefined")
       {
          trim = String(tokenize.arguments[1]);
       }

     if(typeof(tokenize.arguments[2]) != "undefined")
       {
          if(!tokenize.arguments[2])
            ignoreEmptyTokens = false;
       }

     var array = input.split(separator);

     if(trim)
       for(var i=0; i<array.length; i++)
         {
           while(array[i].slice(0, trim.length) == trim)
             array[i] = array[i].slice(trim.length);
           while(array[i].slice(array[i].length-trim.length) == trim)
             array[i] = array[i].slice(0, array[i].length-trim.length);
         }

     var token = new Array();
     if(ignoreEmptyTokens)
       {
          for(var i=0; i<array.length; i++)
            if(array[i] != "")
              token.push(array[i]);
       }
     else
       {
          token = array;
       }

     return token;
  }
  
// Autoformats phone numbers
function formatPhoneNum( fieldObj )
{
	var phoneValue = strip(fieldObj);
	if (phoneValue.length == 0) {
		fieldObj.value = '';
	}
	else if (phoneValue.length < 4) {
		fieldObj.value = '('+phoneValue;
	}
	else if (phoneValue.length < 7) {
		fieldObj.value = '('+phoneValue.substring(0,3)+') '+phoneValue.substring(3,6);
	}
	else {
		fieldObj.value = '('+phoneValue.substring(0,3)+') '+phoneValue.substring(3,6)+'-'+phoneValue.substring(6,10);
	}
}
// Remove extra characters from field value
function strip( fieldObj ) {
	var fieldValue = fieldObj.value.replace(/\(|\)|\-|\ /gi, '');
	return fieldValue;
}
