/***************************************
* function newWindow
* IN: page URL, window height, window width, scrollbars (yes/no)
*
* Calls a new popup window through a page
* URL passed into function
****************************************/
function newWindow(myURL,h,w,scrollBars) {
	var winl = (screen.width - w) / 2;
	var wint = ((screen.height - h) / 2) - 20;
	winprops = 'height=' + h + ',width=' + w +',top=' + wint + ',left= ' + winl + ', scrollbars=' + scrollBars;
	testWindow = window.open(myURL,"baldweb",winprops);
	testWindow.focus();
}


/***************************************
* function newWindow
* IN: page URL, window height, window width, scrollbars (yes/no)
*
* Calls a new popup window through a page
* URL passed into function
****************************************/
function newWindow2(myURL) {
	var w = 800;
	var h = 600;
	var winl = (screen.width - w) / 2;
	var wint = ((screen.height - h) / 2) - 20;
	winprops = 'height=' + h + ',width=' + w +',top=' + wint + ',left= ' + winl + ',resizable=yes';
	testWindow2 = window.open(myURL,"baldweb2",winprops);
	testWindow2.focus();
}


/***************************************
* function newWindow
* IN: page URL, window height, window width, scrollbars (yes/no)
*
* Calls a new popup window through a page
* URL passed into function
****************************************/
function newWindow3(myURL,h,w,scrollBars) {
	var winl = (screen.width - w) / 2;
	var wint = ((screen.height - h) / 2) - 20;
	winprops = 'height=' + h + ',width=' + w +',top=' + wint + ',left= ' + winl + ', scrollbars=' + scrollBars;
	testWindow3 = window.open(myURL,"baldweb3",winprops);
	testWindow3.focus();
}


/*********************************
* function showHide
* IN: object ID (or an array of IDs)
*
* this function is used to toggle
* the visibility of a page element
**********************************/
function showHide(objIds,dispType) {
	if(isArray(objIds)){
		for(var i=0; i<objIds.length; i++){
			var objRef=document.getElementById(objIds[i]);
			if (objRef.style.display=='none') {
				objRef.style.display=dispType;
			} else {
				objRef.style.display='none';
			}
		}
	}
	else {	
		var objRef=document.getElementById(objIds);
		if (objRef.style.display=='none') {
			objRef.style.display=dispType;
		} else {
			objRef.style.display='none';
		}
	}
	
}

/****************************
* function isArray
* IN: any variable
* OUT: bool
*
* This function returns true if the variable
* that was passed in is an array 
*******************************/
function isArray(myVar) {	
	var tempType = typeof myVar;
	//alert(tempType);

	if (tempType == "string")
		return false;
	else
		return true;
}

/*******************************
* function fixBlogLinks
*
* This function spilts links apart
* to wrap properly in the blog comments section
*******************************/
function fixBlogLinks() {
	blogComments = document.getElementById("commentsBlock");
	blogLinks = blogComments.getElementsByTagName("A");
	var breakLength = 50;
	var output = "";
	
	for(var i=0; i<blogLinks.length; i++){
		for(var j=0; j<blogLinks[i].innerHTML.length; j=j+breakLength){
			if(j+breakLength > blogLinks[i].innerHTML.length)
				output += blogLinks[i].innerHTML.substring(j+1); //get the rest of the string
			else
				output += blogLinks[i].innerHTML.substring(j,breakLength) + " "; //take a chunk of the string and split it
		}		
		blogLinks[i].innerHTML = output;		
		output = ""; //reset output
	}
}


/*******************************
* function processForgot
* IN: form reference
* OUT: bool, result message written to page
*
* AJAX function that triggers password
* reset for user with specified username
********************************/
function processForgot(formRef) {
	var queryString = "util/resetPass.php?u=" + formRef.username.value;
	var myResult = "";
	
	xmlhttp = initAjax();
	xmlhttp.open("GET", queryString, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.status==404){
				formRef.innerHTML = "<p class=\"error\">The username you entered was not found in our database.</p>";
			}
			else {
				myResult = xmlhttp.responseText;
				if(myResult == "error" || myResult == "undefined" || myResult == "")
					formRef.innerHTML = "<p class=\"medium error\">The username you entered was not found in our database.</p>";
				else {
					formRef.innerHTML = "<p class=\"medium\">Your new password has been sent to <strong>" + myResult + "</strong>.</p>";
					resetPasswd(formRef.username.value,myResult);
				}
			}
		}
	}
	xmlhttp.send(null);
		
	return false;
}


/******************************
* function reserPasswd
* IN: username, email address
* OUT: none
*
* This function is used to invoke a password
* reset for a specified user
******************************/
function resetPasswd(user,email) {
	var queryString = "util/resetPass.php?u=" + user + "&e=" + email;
	
	xmlhttp = initAjax();
	xmlhttp.open("GET", queryString, true);
	xmlhttp.send(null);
}


/*********************************
* function verifyPW
*
* This function is used to check
* that a user has entered their password
* correctly
********************************/
function verifyPW(formRef) {
	if(formRef.newPW1.value != formRef.newPW2.value) {
		alert("The passwords you entered do not match.\nPlease re-enter them.");
		formRef.newPW1.value = "";
		formRef.newPW2.value = "";
		formRef.newPW1.focus();
		return false;
	}
	else
		return true;
}


/*********************************
* function sendPhotoEmail
* IN: photoID
* OUT: email sent to user
*
* This function sends a custom email
* to the specified email address
* with a link to a particular photo
**********************************/
function sendPhotoEmail(photoID) {
	if(document.sendEmail.sendTo.value == ""){
		alert("You must enter an email address to send this message to!");
	}
	else{
		var queryString = "util/email.php?p=" + photoID;
		
		var s = document.sendEmail.sendTo.value;
		var m = document.getElementById("myMessage").value;
		var f = document.getElementById("from").value;
		
		queryString = queryString + "&s=" + s + "&m=" + m + "&f=" + f;
		
		//alert(queryString);
		
		xmlhttp = initAjax();
		xmlhttp.open("POST", queryString, true);
		xmlhttp.send(null);
		
		document.getElementById("sendFriend").innerHTML = "<p class=\"normal\" style=\"height: 60px;\"><strong>Your message has been sent!</strong></p>\n";
	}
}

/*********************************
* function sendPhotoEmail
* IN: photoID
* OUT: email sent to user
*
* This function sends a custom email
* to the specified email address
* with a link to a particular photo
**********************************/
function sendVideoEmail(videoID) {
	if(document.sendEmail.sendTo.value == ""){
		alert("You must enter an email address to send this message to!");
	}
	else{
		var queryString = "util/email.php?v=" + videoID;
		
		var s = document.sendEmail.sendTo.value;
		var m = document.getElementById("myMessage").value;
		var f = document.getElementById("from").value;
		
		queryString = queryString + "&s=" + s + "&m=" + m + "&f=" + f;
		
		//alert(queryString);
		
		xmlhttp = initAjax();
		xmlhttp.open("POST", queryString, true);
		xmlhttp.send(null);
		
		document.getElementById("sendFriend").innerHTML = "<p class=\"normal\" style=\"height: 60px;\"><strong>Your message has been sent!</strong></p>\n";
	}
}


/*******************************
* function highlightRow
*
* This function applies a class to a
* table row on rollover
*******************************/
function highlightRow(rowRef) {
	var highlighted;
	
	if(rowRef.className == "")
		highlighted = "highlighted";
	else
		highlighted = " highlighted";
	
	rowRef.className = rowRef.className + highlighted;
}


/*******************************
* function removeHighlight
*
* This function removes the highlight
* class attribute on a table row
********************************/
function removeHighlight(rowRef) {
	if(rowRef.className.indexOf(" highlighted") == -1)
		rowRef.className = rowRef.className.replace("highlighted","");
	else
		rowRef.className = rowRef.className.replace(" highlighted","");
}


/***********************************
* function readMessage
*
* This function is used to invoke a popup
* in the My Account area to read a message
* sent by another user
************************************/
function readMessage(myURL) {
	var scrollBars = "no";
	var h = 410;
	var w = 600;
	var winl = (screen.width - w) / 2;
	var wint = ((screen.height - h) / 2) - 20;
	winprops = 'height=' + h + ',width=' + w +',top=' + wint + ',left= ' + winl + ', scrollbars=' + scrollBars;
	testWindow = window.open(myURL,"baldweb",winprops);
	testWindow.focus();
}


/***********************************
* function composeMessage
*
* This function calls the popup window
* in the My Account area to compose
* a message to another user
***********************************/
function composeMessage() {
	var scrollBars = "no";
	var myURL = "composeMessage.php";
	var h = 400;
	var w = 600;
	var winl = (screen.width - w) / 2;
	var wint = ((screen.height - h) / 2) - 20;
	winprops = 'height=' + h + ',width=' + w +',top=' + wint + ',left= ' + winl + ', scrollbars=' + scrollBars;
	testWindow = window.open(myURL,"baldweb",winprops);
	testWindow.focus();
}


/**********************************
* function addTraining
*
* This function invokes a popup to allow
* a user to enter/modify their training record
***********************************/
function addTraining(trainID) {
	var scrollBars = "no";
	var myURL = "community/addWorkout.php?trainID="+trainID;
	var h = 480;
	var w = 500;
	var winl = (screen.width - w) / 2;
	var wint = ((screen.height - h) / 2) - 20;
	winprops = 'height=' + h + ',width=' + w +',top=' + wint + ',left= ' + winl + ', scrollbars=' + scrollBars;
	testWindow = window.open(myURL,"baldweb",winprops);
	testWindow.focus();
}


/**********************************
* function manageRoutes
*
* This function invokes a popup to allow
* a user to enter/modify their route
***********************************/
function manageRoutes() {
	var scrollBars = "no";
	var myURL = "community/manageRoutes.php";
	var h = 450;
	var w = 500;
	var winl = (screen.width - w) / 2;
	var wint = ((screen.height - h) / 2) - 20;
	winprops = 'height=' + h + ',width=' + w +',top=' + wint + ',left= ' + winl + ', scrollbars=' + scrollBars;
	testWindow = window.open(myURL,"baldweb",winprops);
	testWindow.focus();
}


/***************************
* deleteTraining
*
* This function deletes a training activity for the specified ID
***************************/
function deleteTraining(trainID) {
	if(confirm("Are you sure you want to delete this training record?")){
		window.location.href = "index.php?p=account&action=deleteTraining&del="+trainID;	
	}	
}


/***************************
* removeChallenge
*
* This function removes the Bike Challenge app
***************************/
function removeChallenge() {
	if(confirm("Are you sure you want to remove the Bike Challenge '08 app?")){
		window.location.href = "index.php?p=account&action=removeChallenge";
	}	
}


/***************************
* previewRoute
*
* This function opens a popup window based on the specified routeID
****************************/
function previewRoute(routeID) {
	var queryString = "../util/previewRoute.php?routeID=" + routeID;
	var myResult = "";
	
	xmlhttp = initAjax();
	xmlhttp.open("GET", queryString, true);
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState==4) {
			if (xmlhttp.status==404){
				//error message
				alert("Unable to retreive your route information at this time.\nPlease be sure you have selected a route.");
			}
			else {
				myResult = xmlhttp.responseText;
				if(myResult == "error" || myResult == "undefined" || myResult == "")
					alert("Unable to retreive your route information at this time.\nPlease be sure you have selected a route.");
				else {
					newWindow3(myResult,600,800,'yes');
				}
			}
		}
	}
	xmlhttp.send(null);
		
	return false;
		
}


/***************************
* function filterRiders
*
***************************/
function filterRiders(mylimit) {
	//var limit = 400;
	
	for(var i=0; i<listRef.childNodes.length; i++) {
		if(listRef.childNodes[i].value != ""){
			
			if(listRef.childNodes[i].value < mylimit) {
				if(typeof listRef.childNodes[i].style == "object" && listRef.childNodes[i].style.opacity == "")
					alphaDown(listRef.childNodes[i],9);
				else if(typeof listRef.childNodes[i].style == "object" && listRef.childNodes[i].style.filter == "")
					alphaDown(listRef.childNodes[i],9);
			}
			else if(listRef.childNodes[i].value > mylimit) {
				if(typeof listRef.childNodes[i].style == "object" && listRef.childNodes[i].style.opacity == "0.2")
					alphaUp(listRef.childNodes[i],2);
				else if(typeof listRef.childNodes[i].style == "object" && listRef.childNodes[i].style.filter == "alpha(opacity=20)")
					alphaUp(listRef.childNodes[i],2);
			}
		}
	}
}


/*****************************
* function alphaUp
*
*****************************/
function alphaUp(myRef,myValue) {
	//alpha for IE
	if(navigator.appName == "Microsoft Internet Explorer"){
		myRef.style.filter = "alpha(opacity="+myValue*10+")";
		myRef.style.zoom = "1";
	}
	else
		myRef.style.opacity = "0."+myValue;

	if(myValue < 9){
		newValue = myValue+1;
		//setTimeout("alphaUp('"+myRef+"',"+newValue+")",500);
		//alphaUp(myRef,newValue);
		setTimeout(myTimer = function() { alphaUp(myRef,newValue); },50);
	}
	else {
		if(navigator.appName == "Microsoft Internet Explorer"){
			myRef.style.filter = "";
		}
		else
			myRef.style.opacity = "";
	}
}


/*****************************
* function alphaDown
*
*****************************/
function alphaDown(myRef,myValue) {
	//alpha for IE
	if(navigator.appName == "Microsoft Internet Explorer"){
		myRef.style.filter = "alpha(opacity="+myValue*10+")";
		myRef.style.zoom = "1";
	}
	else 
		myRef.style.opacity = "0."+myValue;

	if(myValue > 2){
		newValue = myValue-1;
		//setTimeout("alphaDown('"+myRef+"',"+newValue+")",200);
		//alphaDown(myRef,newValue);
		setTimeout(myTimer = function() { alphaDown(myRef,newValue); },50);
	}
}

function calcPace() {
	
	paceRef = document.getElementById("paceFor");
	
	if(paceRef.value != ""){
		var hrs = document.getElementById("hrs").value;
		var mins = document.getElementById("min").value;
		var secs = document.getElementById("sec").value;
		var output = document.getElementById("pace");
		
		var hrsInt = parseInt(hrs);
		var minsInt = parseInt(mins);
		var secsInt = parseInt(secs);
		
		if(hrs == "")
			hrsInt = 0;
		if(secs == "")
			secsInt = 0;
		var distRef = paceRef.value;
		
		if(document.getElementById(distRef).value != "" && document.getElementById(distRef).value > 0) {
			distVal = document.getElementById(distRef).value;
			
			minsInt = minsInt + (hrsInt * 60);
			totalTime = minsInt + (secsInt / 60);
			
			paceIntLong = totalTime / distVal;
			//alert(paceIntLong);
			paceInt = parseInt(paceIntLong)
			//alert(paceInt);
			remain = parseInt((paceIntLong - paceInt) * 60);

			if(remain.toString().length < 2)
				remain = "0"+remain;
			
			pace = paceInt + ":" + remain;
			output.value = pace;
		}
		else{
			output.value = "";
		}
	}
	
}
