/* Text Box Scroll */

var b;
var lim = 1;
var margTop;
var textBox;
var D = new Array();
var U = new Array();

function bD(id,cId) {
	textBox = document.getElementById(id);
	text = document.getElementById(cId);
	text.style.display='block';
	clearInterval(U[textBox]);
	D[textBox] = setInterval("startD(\""+id+"\");",1);
}

function startD(id) {
	b = parseInt(document.getElementById(id).style.top);
	bInc = parseInt(b+10);

	if(bInc < 440) {
		document.getElementById(id).style.top = parseInt(bInc)+'px';
	} else {
		document.getElementById(id).style.top = 440+'px';
		clearInterval(D[textBox]);
	}
}

function bU(id,cId) {
	clearInterval(D[textBox]);
	U[textBox] = setInterval("startU(\""+id+"\",\""+cId+"\");",1);
}

function startU(id,cId) {
	b = parseInt(document.getElementById(id).style.top);
	bInc = parseInt(b-10);
	text = document.getElementById(cId);
	
	if(bInc > 280) {
		document.getElementById(id).style.top = parseInt(bInc)+'px';
	} else {
		document.getElementById(id).style.top = 280+'px';
		text.style.display='none';
		clearInterval(U[textBox]);
	}
}


/* Loading Page Status Functions */

	var ld=(document.all);
 	var ns4=document.layers; 
	var ns6=document.getElementById&&!document.all; 
	var ie4=document.all;  

	if (ns4) 
		ld=document.getElementById('loading'); 
	else if (ns6) 	
		ld=document.getElementById('loading').style;
	else if (ie4) 	
		ld=document.getElementById('loading').style; 

	function loading() {
		var mnCont=document.getElementById('mnCont').style;
		if(ns4){
			ld.visibility="hidden";
			mnCont.visibility="visible";
		} else if (ns6||ie4) 
			ld.display="none";
			mnCont.display="block"; }


/* Image Preloads */

	/*var images = new Array('images/body/navigator.jpg','images/body/back.png','images/body/logos.png','images/navigation/butOff.png','images/navigation/butOn.png','images/navigation/navBotBack.png','images/navigation/navLeftBack.png','images/navigation/navRightBack.png','images/navigation/navTopBack.png','images/body/cropAMPM.jpg','images/body/cropAC.jpg','images/body/cropOV.jpg','images/body/cropWH.jpg','images/navigation/bslftCorn.png','images/navigation/bsrhtCorn.png','images/navigation/bsMid.png','images/navigation/bsMidOn.png','images/body/loginBk.png','images/body/SMFoot.png','images/body/SMFootOn.png','images/navigation/darkOff.png','images/navigation/darkOn.png','images/navigation/bsrhtCornOn.png','images/navigation/bslftCornOn.png','images/navigation/navigator.jpg','images/navigation/subTop.png','images/navigation/subMid.png','images/navigation/subBot.png','images/body/popupShadBot.png','images/body/popupShadTop.png','images/body/popupShadCn.png','images/body/diaryInsShadBot.png','images/body/diaryInsShadCn.png','images/body/diaryInsShadTop.png','images/body/vtourNavBack.png','images/body/closePopup.png','images/body/closePopupOn.png');*/

function preload() {
	this.image = new Array();		

	for(var i=0; i <images.length;) {
		this.image[i] = new Image();
		this.image[i].src = images[i];
		this.image.onload=i++;
	}
	loading();
}


/* Popup */

function PopUp(ID, popup, query) {
	document.getElementById('popupContBack').style.display='block'; 

	if(query==null){
		document.getElementById('popupiFrame').src='popups/'+popup+'.php?ID='+ID;
	} else {
		document.getElementById('popupiFrame').src='popups/'+popup+'.php?'+query;
	}
}



function EscapeKeyPress() {

    if (window.event.keyCode == 27)
        document.getElementById('popupContBack').style.display='none';
}


/* Diary Instructions Popup */

function showHide(id, motion) {

	if(motion=='show') {
		document.getElementById(id).style.display='block';
	} else if(motion=='hide') {
		document.getElementById(id).style.display='none';	
	}
}


/* Login Failed */

function loginFailed() {
	document.getElementById('loginFailed').style.display='block';
	document.getElementById('login').style.display='none';
	setTimeout("loginFTimeout()",5000);
}



function loginFTimeout(){
	document.getElementById('loginFailed').style.display='none';
	document.getElementById('login').style.display='block';
}


/* IP Blacklisted */

function ipBlacklisted() {
	document.getElementById('ipBlacklisted').style.display='block';
	document.getElementById('login').style.display='none';
	setTimeout("ipBTimeout()",5000);
}

function ipBTimeout(){
	document.getElementById('ipBlacklisted').style.display='none';
	document.getElementById('login').style.display='block';
}


/* Toggle Tour */

function toggleTour(id) {
   	var e = document.getElementById('tourCont');
	e.innerHTML='<embed src="../images/vtours/vt'+id+'.mov" pluginspace="http://quicktime.apple.com" width="448" height="236">'
}


/* Virtual Tour Navigation */

function vTourNavig(id) {
	var vTourPos = parseInt(document.getElementById('vTourPos').value);
	var vTourSize = parseInt(document.getElementById('vTourSize').value);

	if(id=='next'){
		var newVTourPos = parseInt(vTourPos+1);
	}else if(id=='prev'){
		var newVTourPos = parseInt(vTourPos-1);
	}else{
		var newVTourPos = parseInt(vTourPos);
	}

	document.getElementById('vTourPos').value=newVTourPos;

	if(newVTourPos==1 && vTourSize>3){
		disableVTBut('prev');
	}else{
		enableVTBut('prev');
	}

	if(newVTourPos==vTourSize-2 && vTourSize>3){
		disableVTBut('next');
	}else{
		enableVTBut('next');
	}

	if(vTourSize<=3){
		disableVTBut('next');
		disableVTBut('prev');
	}

	var i;

	for (i=1;i<=vTourSize;i++) {
		document.getElementById('vt'+i).style.display='none';
	}

	placeVThumbs(newVTourPos);

}

function disableVTBut(id){
	var img = document.getElementById(id+'Img');
	img.style.opacity=0.2;
	img.style.MozOpacity=0.2;
	img.style.filter = 'alpha(opacity=20)';
	img.onclick=""
	img.style.cursor="default";
}

function enableVTBut(id){
	var img = document.getElementById(id+'Img');
	img.style.opacity=1;
	img.style.MozOpacity=1;
	img.style.filter = 'alpha(opacity=100)';
	img.onclick=function() {vTourNavig(id);};
	img.style.cursor="pointer";
}

function placeVThumbs(position) {
	var t=parseInt(position+2);
	var i;

	for (i=position;i<=t;i++) {
		document.getElementById('vt'+i).style.display='block';	
	}
}


/* Toggle Visibility of Editing Divs */

function toggleVis(id, show, hide) {
	var div = document.getElementById(id).style.height;

	if(div=='auto'){
		document.getElementById(id).style.height='10px';
		document.getElementById(hide).style.display='none';
		document.getElementById(show).style.display='block';
	} else if(div=='10px') {
		document.getElementById(id).style.height='auto';
		document.getElementById(hide).style.display='block';
		document.getElementById(show).style.display='none';
	}
}


/* Textarea Text Formatting */

function formatText(el,tag){
	var txt = document.getElementById(el);

	if(tag!='URL') {
        if(document.selection) {
        	txt.focus();
        	sel = document.selection.createRange();
        	sel.text = '[' + tag + ']' + sel.text + '[/' + tag + ']';
        } else if(txt.selectionStart || txt.selectionStart == '0') {	
        	txt.value = (txt.value).substring(0, txt.selectionStart) + "["+tag+"]" + (txt.value).substring(txt.selectionStart, txt.selectionEnd) + "[/"+tag+"]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
        } else {
            txt.value = '[' + tag + '][/' + tag + ']';
        }

        return;

	} else {
		var url=prompt('Please enter the URL of your link:', 'http://');

		if(url!=null && url!="") {
			if(document.selection) {
        		txt.focus();
        		sel = document.selection.createRange();
        		sel.text = '[' + tag + '='+url+']' + sel.text + '[/' + tag + ']';
        	} else if(txt.selectionStart || txt.selectionStart == '0') {	
        		txt.value = (txt.value).substring(0, txt.selectionStart) + "["+tag+'='+url+']' + (txt.value).substring(txt.selectionStart, txt.selectionEnd) + "[/"+tag+"]" + (txt.value).substring(txt.selectionEnd, txt.textLength);
            } else {
            	txt.value = '[' + tag + '='+url+'][/' + tag + ']';
            }

		}

        return;

  	}
}


/* File Upload Validation */

function fileValidation(elemValue, id) {
	var fileChange = document.getElementById('fileChange'+id);

	if(elemValue.lastIndexOf(".jpg")!=-1 || elemValue.lastIndexOf(".gif")!=-1 || elemValue.lastIndexOf(".JPG")!=-1 || elemValue.lastIndexOf(".jpeg")!=-1 || elemValue.lastIndexOf(".JPEG")!=-1)	{
		fileChange.style.display="block";
	} else {
		alert("Please choose an image file, either a '.gif' or a '.jpg'");
		fileChange.style.display="none";	
	}
}


/* Delete Row */

function deleteRow(id, page, msg, query, table, pageID) {
	del = confirm(msg);

	if(del==true) {
		document.location=page+'?'+query+'='+id+'&table='+table+'&ID='+pageID;
	} else return;
}


/* Validating Year */

function isValidYear(value, id) {
	var regex=/(^-?\d\d*$)/;
	var elem = document.getElementById(id);

	if(regex.test(value) && value.length==4) {
		document.dateSelect.submit.disabled=false;	
	} else {
		elem.style.color='#CCC';
		elem.value='YYYY';
		alert('Please enter a valid year');	
	}
}
