/*
	Waterton Lakes National Park Functions page
	Date: 5/15/2008
*/
function findPosX(obj){
	var curleft = 0;
	if(obj.offsetParent)
		while(1) 
		{
		  curleft += obj.offsetLeft;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.x)
		curleft += obj.x;
	return curleft;
}

function findPosY(obj){
	var curtop = 0;
	if(obj.offsetParent)
		while(1)
		{
		  curtop += obj.offsetTop;
		  if(!obj.offsetParent)
			break;
		  obj = obj.offsetParent;
		}
	else if(obj.y)
		curtop += obj.y;
	return curtop;
}

//Start of drop down script
var subMnus="";
var mnuTop=128;
//This function is to show the subs in the parent dropdown
function showMenu(idx,li){
	//var subleftoffset=((document.body.offsetWidth-954)/2)+25;
	var subleftoffset=findPosX(document.getElementById("mnuAboutImg"));
	/*if(document.body.offsetWidth<954)
		subleftoffset=0;*/
	var sublefts=new Array(6);
	sublefts[0]=subleftoffset;
	sublefts[1]=sublefts[0]+162;
	sublefts[2]=sublefts[1]+140;
	sublefts[3]=sublefts[2]+131;
	sublefts[4]=sublefts[3]+144;
	sublefts[5]=sublefts[4]+106;
	try{
		hideAllMenu();
		var dv=document.getElementById("sub"+idx);
		dv.style.left=sublefts[li];
		dv.style.top=mnuTop;
		dv.style.visibility="visible";
	}
	catch(e){}
}
//This function is to hide the dropdown menu
function hideAllMenu(){
	try{
		var ar=subMnus.split(',');
		for(i=0;i<ar.length;i++){
			var dv=document.getElementById("sub"+ar[i]);
			dv.style.visibility="hidden";
		}
	}
	catch(e){}
}
//End of drop down script

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
//This function checks to see if the email is valid
function isValidEmail(thisEmail){
	var emailexp = /.*\@.*\..*/;
	if (!emailexp.test(thisEmail)) {
		return false;
	} 
	return true;
}


//This function checks to make sure a check box has been marked
function cbxSelectionMade(cbx){
	var isGood=false;
	if(cbx.checked){
		isGood=true;
	}
	for(i=0;i<cbx.length && !isGood;i++){
		if(cbx[i].checked){
			isGood=true;
		}
	}
	return isGood;
}

//This Function checks to make sure a select box has been selected
function cboSelectionMade(cbo){
	var isGood=false;
	
	if(cbo.options[cbo.selectedIndex].value!="")
		isGood=true;
		
	return isGood;
}

//Sets the gem picture to the main picture
function setPic(thisPic){
	document.getElementById('mainPicture').src='images/gems/' + thisPic;
}

//Sets the trail picture to the main picture
function setTPic(thisPic){
	document.getElementById('mainPicture').src='images/trails/' + thisPic;
}
//Sets the gem picture to the main picture
function setBDirPic(thisPic){
	document.getElementById('mainPicture').src='busdir/../images/' + thisPic;
}
//This function writes the swf for the videos
function popPic(pic,gemTitleName){
	scroll(0,0);
	var str='<img src="'+pic+'" width="600" height="392" >';
	
	document.getElementById('divBg').style.display="inline";
	document.getElementById('divImgPlayer').style.display="inline";
	document.getElementById('divImg').innerHTML=str;
	document.getElementById('gemTitle').innerHTML=gemTitleName;
}

function closeMe(ele, ele2){
	document.getElementById(ele).style.display="none";
	document.getElementById(ele2).style.display="none";
}

//this function writes the swf for index
function writeSWF(divName){	
	var str='<object id="homeswf" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" width="884" height="495">';
	str+='<param name="movie" value="flash/mainImg.swf">';
	str+='<param name="quality" value="high">';
	str+='<param name="wmode" value="opaque">';
	str+='<param name="menu" value="false">';
	str+='<embed name="homeswf" src="flash/mainImg.swf" quality="high" wmode="opaque" menu="false" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" width="884" height="495"></embed>';
	str+='</object>';
	document.getElementById(divName).innerHTML=str;
}

//This function is to validate login
function validateLogin(thisForm){
	if(!isValidEmail(thisForm.userEmailAddress.value)){
		alert("Please enter in a valid email.");	
		thisForm.userEmailAddress.focus();
		return false;
	}
	if(thisForm.userPassword.value==""){
		alert("Please enter in your password.");	
		thisForm.userPassword.focus();
		return false;
	}
	return true;
}

//This function is to validate creating a new user
function validateCreateUser(thisForm,section){
	// validates password change
	if(section == 0){
		if(thisForm.userPassword.value==""){
			alert("Please enter in a password.");
			thisForm.userPassword.focus();
			return false;
		}
		if(thisForm.userPassword2.value==""){
			alert("Please enter in a confirmation password.");
			thisForm.userPassword2.focus();
			return false;
		}
		if(thisForm.userPassword.value != thisForm.userPassword2.value){
			alert("Your two passwords did not match.");
			thisForm.userPassword.focus();
			return false;
		}
		if(thisForm.imageVerify.value==""){
			alert("Please enter in the verification code.");
			thisForm.imageVerify.focus();
			return false;
		}
		return true;
	}
	//This is the acutal user registration form
	else if(section == 1)
	{
		if(thisForm.userFullName.value==""){
			alert("Please enter in your name.");
			thisForm.userFullName.focus();
			return false;
		}
		if(thisForm.displayName.value==""){
			alert("Please enter a name to be shown on the website.");
			thisForm.displayName.focus();
			return false;
		}
		if(!isValidEmail(thisForm.userEmailAddress.value)){
			alert("Please enter in a valid email.");	
			thisForm.userEmailAddress.focus();
			return false;
		}
		if(thisForm.userPassword.value==""){
			alert("Please enter in a password.");
			thisForm.userPassword.focus();
			return false;
		}
		if(thisForm.userPassword2.value==""){
			alert("Please enter in a confirmation password.");
			thisForm.userPassword2.focus();
			return false;
		}
		if(thisForm.userPassword.value != thisForm.userPassword2.value){
			alert("Your two passwords did not match.");
			thisForm.userPassword.focus();
			return false;
		}
		if(thisForm.imageVerify.value==""){
			alert("Please enter in the verification code.");
			thisForm.imageVerify.focus();
			return false;
		}
		return true;
	}
	// This is the my profile submission
	else if(section == 2)
	{
		if(thisForm.userFullName.value==""){
			alert("Please enter in your name.");
			thisForm.userFullName.focus();
			return false;
		}
		if(thisForm.displayName.value==""){
			alert("Please enter a name to be shown on the website.");
			thisForm.displayName.focus();
			return false;
		}
		if(!isValidEmail(thisForm.userEmailAddress.value)){
			alert("Please enter in a valid email.");	
			thisForm.userEmailAddress.focus();
			return false;
		}
		if(thisForm.imageVerify.value==""){
			alert("Please enter in the verification code.");
			thisForm.imageVerify.focus();
			return false;
		}
		return true;
	}
}
//This function is to validate user add note
function validateAddNote(thisForm){
	if(thisForm.mynote.value==""){
		alert("You must provide content for your note.");
		thisForm.mynote.focus();
		return false;
	}
	
	return true;	
}
//This function is to validate user adding an experience
function validateAddExperience(thisForm){
	if(thisForm.expTitle.value==""){
		alert("You must provide a title for yoru experience.");
		thisForm.expTitle.focus();
		return false;
	}
	if(thisForm.expDescription.value==""){
		alert("You must provide a title for yoru experience.");
		thisForm.expDescription.focus();
		return false;
	}

	return true;
}


//This function is to set the ratings for experience back to nothing
function offRating(){
	rating=document.experience.expRating.value;
	for(i=1;i<6;i++){
		document.getElementById('rating'+i).src='images/gem'+(i<=rating?'Full':'Empty')+'.gif';
	}
}//End of function

//This function is to set the ratings for experience
function setRating(rating,clik){
	for(i=1;i<6;i++){
		document.getElementById('rating'+i).src='images/gem'+(i<=rating?'Full':'Empty')+'.gif';
	}
	if(clik==1){
		document.experience.expRating.value = rating;
	}
}//End of function

function validateForgot(thisForm){

	if(!isValidEmail(thisForm.userEmailAddress.value)){
			alert("Please enter in a valid email.");	
			thisForm.userEmailAddress.focus();
			return false;
		}
	return true;	
}

//This function is to validate emails for sending trip planner
function validateTheEmails(thisForm){
	if(!isValidEmail(thisForm.email1.value)&&thisForm.email1.value!=""){
		alert("Email #1 is not a valid email address.");	
		thisForm.email1.focus();
		return false;
	}
	if(!isValidEmail(thisForm.email2.value)&&thisForm.email2.value!=""){
		alert("Email #2 is not a valid email address.");	
		thisForm.email2.focus();
		return false;
	}
	if(!isValidEmail(thisForm.email3.value)&&thisForm.email3.value!=""){
		alert("Email #3 is not a valid email address.");	
		thisForm.email3.focus();
		return false;
	}
	if(!isValidEmail(thisForm.email4.value)&&thisForm.email4.value!=""){
		alert("Email #4 is not a valid email address.");	
		thisForm.email4.focus();
		return false;
	}
	if(!isValidEmail(thisForm.email5.value)&&thisForm.email5.value!=""){
		alert("Email #5 is not a valid email address.");	
		thisForm.email5.focus();
		return false;
	}
	
	return true;
}

// this function validates trip planner pdf selections
function validatePDFitems(thisForm){
	if(!cbxSelectionMade(thisForm.stuffids)){
		alert("You must select an item to add to your booklet.");
		return false;
	}
	
	return true;
}

function hideCreateDiv(){
	try{
		document.getElementById('dvLogCreate').style.display='none';
	}
	catch(e){}
}


/*       MAIN SLIDE SHOW FUNCTIONS     */
var mainSlides=null;
var mainSlideCurrent=0;
var mainSlideChanging=false;
var mainSlideTimer;
var mainSlideInterval=5000;
var mainSlideFadeInterval=500;

function nextMainSlide(){
	if(mainSlideChanging){
		return false;
	}
	if(mainSlides){
		if(mainSlideCurrent>=(mainSlides.length-1)){
			gotoMainSlide(0);
		}
		else{
			gotoMainSlide(mainSlideCurrent+1);
		}		
	}
}

function gotoMainSlide(idx){
	if(mainSlideChanging){
		return false;
	}
	if(idx<mainSlides.length && idx>(-1)){
		// stop timer
		mainSlideTimerStop();
		// set changing flag to true
		mainSlideChanging=true;
		// set current slide to passed index
		mainSlideCurrent=idx;
		// fade out old slide image
		JQ("#imgHomeSlide").fadeOut(mainSlideFadeInterval*2,function() {
			// change the image src to the new slide
			JQ("#imgHomeSlide").attr("src",mainSlides[mainSlideCurrent][0].src);
			JQ("#tdSlideCap").html(mainSlides[mainSlideCurrent][1]);
			JQ("#aSlideLink").attr("href",mainSlides[mainSlideCurrent][2]);
			// set the main image alpha
			JQ("#imgHomeSlide").fadeIn();
			mainSlideChanging=false;
			mainSlideTimerStart();
		});
	}
}

function mainSlideTimerReset(){
	mainSlideTimerStop();
	mainSlideTimerStart();
}

function mainSlideTimerStart(){
	mainSlideTimer=setTimeout(nextMainSlide,mainSlideInterval);
}

function mainSlideTimerStop(){
	clearTimeout(mainSlideTimer);
}
