/*
	
	function: setupExternalLinks()

	Enables external links in HTML 4.01 strict mode pages.
	
	Technique found here: http://www.sitepoint.com/article/standards-compliant-world/1
	Optimized by dsmith
	
	Arguments:
		none
		
	Returns:
		void
		
*/

function setupExternalLinks(){

	if(!document.getElementsByTagName) return;
	
	var anchors = document.getElementsByTagName('a');
	
	var anchorIndex = anchors.length;
	
	while(--anchorIndex > -1){

		var thisAnchor = anchors[anchorIndex];
		if(thisAnchor.getAttribute('href') && thisAnchor.getAttribute('rel') == 'external') thisAnchor.target = '_blank';

	}

}

/*
	
	function: addBookmark()

	Adds a bookmark for a page to the browser.
	
	Arguments:
		pgTitle - The title of the bookmark.
		pgUrl - The url of the bookmark.
		
	Returns:
		Boolean
		
*/
function addBookmark(pgTitle, pgUrl){
	
	if(window.sidebar){ 
	
		window.sidebar.addPanel(pgTitle, pgUrl, ''); 
		
	} else if(document.all){

		window.external.AddFavorite(pgUrl, pgTitle);
	
	}

	return true;
	
}

/*
	
	function: checkTAreaDefault()

	Checks a textarea for a default text string. Clears the textarea if it finds it. 
	
	Arguments:
		tAreaID - The id of the textarea.
		dtext - The default text of the textarea.
		
	Returns:
		void
		
*/
function checkTAreaDefault(tAreaID, dtext){

	var tArea =  document.getElementById(tAreaID);
	
	if (tArea.value == dtext) {
	
		tArea.value = '';
	
	}

}

/*
	
	function: updateImageInfo()

	Handles submission of image info update fields in photo gallery
	
	Arguments:
		tAreaID - The id of the textarea.
		dtext - The default text of the textarea.
		
	Returns:
		void
		
*/
function updateImageInfo(imgAction, imgID){

	var actForm =  document.getElementById('actionForm_imageAdmin');
	
	if(actForm){

		actForm.img_id.value = imgID;

		switch(imgAction){
	
			case 'saveCaption':
				
				actForm.update_caption.value = 1;
				
				// get new caption
				var newCaption = document.getElementById('caption_' + imgID).value;
				actForm.caption.value = newCaption;

				break;
				
			case 'useAsMain':
				
				actForm.set_as_primary_image.value = 1;
				break;
				
			case 'deleteImg':
				
				actForm.delete_image.value = 1;
				break;
	
		}
		
		actForm.submit();
		
	}
	
}

function updateEmailDisplays(strLimit, srcField){

	var d = document;
	var newText = srcField.value;
	
	var updateIDs = toArray(arguments).slice(2);
	var updateIndex = updateIDs.length;
	
	while(--updateIndex > -1){

		d.getElementById(updateIDs[updateIndex]).innerHTML = newText;

	}

}

function toArray(srcArray) {
    
	var result = [];

    for (var i = 0; i < srcArray.length; i++) result.push(srcArray[i]);

    return result;
	
}

/*
	
	function: changeLNavSrchMode()

	Changes whats search form is displayed in the left nav
	
	Arguments:
		modeName - The name of the search mode.
		
	Returns:
		void
		
*/

function changeLNavSrchMode(modeName){

	var d = document;

	var currMode;
	var trgtMode;

	switch(modeName){

		case 'byName':
			
			currMode = d.getElementById('leftCol-searchByStats');
			trgtMode = d.getElementById('leftCol-searchByName');
			break;
			
		case 'byStats':
			
			currMode = d.getElementById('leftCol-searchByName');
			trgtMode = d.getElementById('leftCol-searchByStats');
			break;

	}
	
	currMode.style.display = 'none';
	trgtMode.style.display = 'block';

}

/*
	
	function: checkAgeFormReqs()

	Verifies that all required form fields are correctly filled in.
	
	Arguments:
		theForm - The form being submitted.
		nextPg - The page that will be redirected to on success.
		
	Returns:
		void
		
*/
function checkAgeFormReqs(theForm, nextPg) {

	var oput='';

	if(theForm.f_month.selectedIndex == 0) oput += '- month\n';
	if(theForm.f_day.selectedIndex == 0) oput += '- day\n';
	if(theForm.f_year.selectedIndex == 0) oput += '- year\n';

	if(oput != ''){

		alert('We require you to select values for the following:\n' + oput);
		return false;

	}	else {
		
		theForm.next.value = unescape(nextPg);
		
		return true;

	}

}

/*
	
	function: selectAllChks()

	Changes whats search form is displayed in the left nav
	
	Arguments:
		formID - The id of the form
		
	Returns:
		void
		
*/

function setAllChkBxs(formID, cValue){

	var d = document;
	var theForm = d.getElementById(formID);
	if(cValue == undefined) cValue = true;
	
	for(i = 0; i < theForm.elements.length; i++){
		
		if(theForm.elements[i].type == 'checkbox') theForm.elements[i].checked = cValue;
		
	}
	
}

/*
	Group: Show/Hide Div Controls
*/

dsShowHideDiv = {

	d: document,
	divList: new Array(),

	regGroup: function(btnID, trgtDivID, divOnBtn, divOffBtn){

		this.divList[trgtDivID] = {

			trgtBtn: this.d.getElementById(btnID),
			trgtDiv: this.d.getElementById(trgtDivID),
			onGrfx: divOnBtn,
			offGrfx: divOffBtn

		}

	},

	tglGroup: function(divID){

		var thisSet = this.divList[divID];

		// get the current div state
		switch(thisSet.trgtDiv.style.display){

			case 'none':

				thisSet.trgtBtn.src = thisSet.onGrfx;
				thisSet.trgtDiv.style.display = 'block';
				break;

			case 'block':

				thisSet.trgtBtn.src = thisSet.offGrfx;
				thisSet.trgtDiv.style.display = 'none';
				break;

		}

	},

	openGroup: function(divID){

		var thisSet = this.divList[divID];

		thisSet.trgtBtn.src = thisSet.onGrfx;
		thisSet.trgtDiv.style.display = 'block';

	},

	closeGroup: function(divID){

		var thisSet = this.divList[divID];

		thisSet.trgtBtn.src = thisSet.offGrfx;
		thisSet.trgtDiv.style.display = 'none';

	},

	openAll: function(){

		for(divGroup in this.divList){

			this.openGroup(divGroup);

		}

	},

	closeAll: function(){

		for(divGroup in this.divList){

			this.closeGroup(divGroup);

		}

	}

}

function dsFauxProgressive(){
	
	this.sections = new Array();
	
	this.addSection = function(divID){
		
		var divRef = document.getElementById(divID);
		
		if(divRef != undefined){
			
			this.sections[divID] = {
				
				divRef: divRef
				
			}
			
		}
		
	};
	
	this.showSection = function(showDivID){
		
		for(divID in this.sections){
			
			var thisDiv = this.sections[divID].divRef;
			
			if(divID == showDivID){
				
				thisDiv.style.display = 'block';
				
			} else {
				
				thisDiv.style.display = 'none';
				
			}
			
		}
		
	}
	
}

function dsHashWatcher(){

	var dshw = this;

	this.watches = new Array();
	this.currentHash;
	this.watchIntvl;
	this.noHash;

	this.addNoHashAction = function(){
		
		
		
	};

	this.addWatch = function(watchName, trigAction){

		this.watches[watchName] = trigAction;
		this.watchCount = this.watches.length;

	};

	this.removeWatch = function(watchName){

		delete this.watches[watchName];

	};

	this.startWatch = function(intvl){

		this.watchIntvl = setInterval(this.checkWatches, intvl);

	};

	this.stopWatch = function(){

		clearInterval(this.watchIntvl);

	};

	this.checkWatches = function(){

		var i = dshw.watches.length;
		var cHash = document.location.hash.slice(1);

		if(cHash != dshw.currentHash){

			dshw.currentHash = cHash;
			
			for(i in dshw.watches){
				
				if(i == dshw.currentHash){
					
					eval(dshw.watches[i]);
					
				}
			}

		}

	};

}













