
function removeWhiteSpace(str)
{ 
    str = str.replace(/ /gi, "");
    return str; 
} 

function removePunc(str)
{ 
    str = str.replace(/(,|;|-|'|")/gi, ""); 
    return str; 
} 

function getFormFields()
{ 
	// All the form fields in the start page training form. 
	var fd = getFormDefaults(); 
	var na = new Array(); 
	for(var k in fd) { 
		na[na.length] = k; 
	} 
	return na; 
} 

function getFormDefaults()
{  
	var k = new Object(); 
	k.hour = 0; 
	k.mins = 0; 
	k.secs = 0; 
	k.diff = "mod"; 
	k.slen = "16"; 
	k.rlen = "fivk"; 	
	k.rdst = "fivk"; 
	k.mpwe = "11"; 
	k.lrdy = "0"; 
	k.trainstart = "ds" + getFirstMonday().getTime(); 
	k.metr = "miles";
	
	return k; 
} 

function writeHidden(nm, val)
{ 
    document.write("<input type=\"hidden\" name=\"" + nm + "\" value=\"" + val + "\"></input>"); 
}     

function scSetCookie(name, value, nweeks)
{
	// default expiration is 16 weeks, otherwise length of schedule. 
	var d = new Date(); 
	var n = nweeks ? nweeks : 16; 
	d.setTime(d.getTime() + n*7*24*60*60*1000); 
	setCookie("smartcoach_" + name, value, d); 
} 

function scGetCookie(name)
{ return getCookie("smartcoach_" + name); } 

function setCookie(name, value, expires, path, domain, secure) {
	document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}


function getCookie(name) {
	var dc = document.cookie;
	var prefix = name + "=";
	var begin = dc.indexOf("; " + prefix);
	if (begin == -1) {
		begin = dc.indexOf(prefix);
		if (begin != 0) return null;
	} else {
		begin += 2;
	}
	var end = document.cookie.indexOf(";", begin);
	if (end == -1) {
		end = dc.length;
	}
	return unescape(dc.substring(begin + prefix.length, end));
}



/** 
 * Clones an object. An attempt is made to preserve OO properties by assigning
 * the prototype of the new object to be the old object, but this should 
 * be regarded as somewhat suspect. Copies all the data fields of the source 
 * object. 
 */ 
function mClone(o)
{ 
	var x = new Object(); 
	for(var k in o)
		{ x[k] = o[k]; } 
	return x; 
} 

function removeWhiteSpace(str)
{ 
    str = str.replace(" ", ""); 
    return str; 
}

function dEl(divId)
{ 
	if(document[divId])
	{ return document[divId]; }
	else
	{ return document.getElementById(divId); } 
} 

function clearLog()
{ 
    errLog = new Array(); 
} 

function logErr(s)
{ 
	if(s.constructor == Object)
	{ 
		logErr("Found object with fields: "); 
		for(var k in s)
		{ logErr(">>>" + k + " = " + s[k]); } 
	} 

	errLog[errLog.length] = s; 
} 

function errorList()
{	
	if(errLog.length == 0)
	{ logErr("No errors reported."); } 

	var hStr = "<ul>"; 
	for(var i = 0; i < errLog.length; i++)
	{ 
		hStr = hStr + "<li>" + errLog[i] + "</li>"; 
	} 
	hStr = hStr + "</ul>"; 
	return hStr; 
} 


function errLogPopup()
{ 
	var mw = window.open(); 
	mw.document.write("Errors: "); 
	mw.document.write(errorList()); 
	mw.document.close(); 
} 

// Log of error messages (or debugging messages)
errLog = new Array(); 

// This function sets the query string parameters directly from Javascript. 
// Should be redundant (and thus harmless) with the Perl. 
function setQSParams()
{ 
	var qs = location.search; 
	if(!qs || qs == "") { return; } 


	qs = qs.replace(/^.*\?(.+)$/,'$1');

	while ((pair = qs.match(/([^=]+)=\'?([^\&\']*)\'?\&?/)) && pair[0].length) 
	{
		qs = qs.substring( pair[0].length );

		if (/^\-?\d+$/.test(pair[2])) { 
			pair[2] = parseInt(pair[2]);
		}
		qsParams[pair[1]] = pair[2];
	}
} 

/** 
 * Returns a date string of the form MM/DD/YY
 */ 
function myDateStr(d)
{ 
	var s = d.getMonth()+1; 
	s += "\/"; 
	s += d.getDate(); 
	s += "\/"; 
	s += d.getFullYear(); 
	return s; 
} 

/** 
 * Returns the name of a day based on its index. 0=Sunday, 1=Monday, etc. 
 */ 
function getDayName(x)
{ 
	var a = new Array("Zondag", "Maandag", "Dinsdag", "Woensdag", "Donderdag", "Vrijdag", "Zaterdag"); 
	return a[x]; 
} 

function getFirstMonday()
{ 
	var d = new Date(); 
	while(d.getDay() != 1)
	{ d.setTime(d.getTime() + 24*60*60*1000); } 
	return d; 
} 

// Takes a speed in meters / minute a converts it to a string representing a pace in 
// minutes per mile or km. 	
function timeConvert(speed, met) 
{
	var dist = met ? 1000 : 1609; 
	return baseTimeConvert(speed, dist); 
}

function baseTimeConvert(speed, dist)
{ 
	var ans = dist/speed; 
	return timeString(ans); 
}

function timeString(mins)
{ 
	var m = Math.floor(mins); 
	var s = Math.floor((mins - m) * 60); 
	if(s < 10) { s = "0" + s; } 
	return m + ":" + s; 
} 

/** 
 * Returns a time string of the form "hours:mins:secs". 
 */ 
function fullTimeString(hr, min, sec)
{ 
	var a = [hr,  (min > 9 ? min : "0" + min), (sec > 9 ? sec : "0" + sec)];
	return a.join(":");
} 

function bounceTimeString(tdata)
{
	var ft = tdata[0];
	for(var i = 1; i < tdata.length; i++)
	{
		var x = tdata[i];
		ft += (":" + (x > 9 ? x : "0" + x));
	}
	return ft;
}

function smartTimeString(mins)
{
	var tdo = timeData(mins*60);
	return fullTimeFromTdo(tdo);
}

function fullTimeFromTdo(tdo)
{
	if(tdo.h > 0)
		return bounceTimeString([tdo.h, tdo.m, tdo.s]);
	
	if(tdo.m > 0)
		return bounceTimeString([tdo.m, tdo.s]);
}

function timeData(secs)
{
	var remsecs = secs;
	var tdo = new Object();

	tdo.h = Math.floor(remsecs/3600);
	remsecs -= (tdo.h*3600);

	tdo.m = Math.floor(remsecs/60);
	remsecs -= (tdo.m*60);	
	
	tdo.s = Math.floor(remsecs);
	return tdo;
}

function validateData(fd)
{ 
	// Checks a data object to see if it is valid. 
	return ((fd.startf) && (fd.startf == "123xyz" || fd.startf == "checkforward" || fd.startf == "halfcomplete"));  		
}

function scCookieData()
{ 
	var cd = new Object(); 
	var ff = getFormFields(); 
	
	for(var i = 0; i < ff.length; i++)
	{ cd[ff[i]] = scGetCookie(ff[i]); }
	cd["startf"] = scGetCookie("startf"); 
	return cd; 
} 

/** 
 * Returns a data object that should be used to display this page. 
 * If the query string has valid data in it, return qsParams. 
 * Otherwise if there is valid data in the cookies, use that. 
 * Otherwise return null. 
 */ 
function getValidData()
{ 
	var fd = null; 
	if(validateData(qsParams)) {
		fd = qsParams; 
	}
	else if(validateData(scCookieData())) { 
		fd = scCookieData(); 
	}

	return fd; 
} 

/** 
 * Argument s is a string representing an difficulty code; this function 
 * returns an associated 
 */ 
function getDiffCode(s)
{ 
	var x = s == "main" ? 0 : (s == "mod" ? 1 : (s == "hard" ? 2 : 3)); 
	return x; 
} 

/**
 * Returns true if the current configuration (prParams) is set to Marathon. 
 */ 
function isMarathon()
{ return prParams["rdst"] == "mara"; } 

/** 
 * Returns true if the current configuration (prParams) is set to Metric. 
 */ 
function isMetric()
{ return prParams["metr"] == "km"; } 

function getQueryStringFromObject(o)
{ 
	var qs = "?"; 
	for (var k in o)
	{ qs = qs + k + "=" + escape(o[k]) + "&"; } 
	return qs.substring(0, qs.length-1); 
} 



function addToFavorites(urlAddress, pageName) 
{
	if (window.sidebar) { // Mozilla Firefox Bookmark
		window.sidebar.addPanel(pageName, urlAddress,"");
	} else if( window.external ) { // IE Favorite
		window.external.AddFavorite( urlAddress, pageName); 
	}	 
}

/**
 * This object contains the key-value pairs passed in from the query string. 
 */ 
qsParams = new Object(); 

setQSParams(); 
