function validate()
{

	var fee = 0.00;
	document.forms.registration.currentregistration.value = "0.00";
	document.forms.registration.submitregistration.style.visibility = "hidden";
	document.forms.registration.submitregistration2.style.visibility = "hidden";
	document.forms.registration.currentregistration.style.visibility = "hidden";
	
//alert( document.forms.registration.coachingweeklycost[0].checked );

	if( document.forms.registration.coachingweeklycost[0].checked == true )
		document.forms.registration.coachinglevel.value = "Bronze Level Coaching";
	else if( document.forms.registration.coachingweeklycost[1].checked == true )
		document.forms.registration.coachinglevel.value = "Silver Level Coaching";
	else if( document.forms.registration.coachingweeklycost[2].checked == true  )
		document.forms.registration.coachinglevel.value = "Gold Level Coaching";
	else if( document.forms.registration.coachingweeklycost[3].checked  == true )
		document.forms.registration.coachinglevel.value = "E-Coaching";
	else if( document.forms.registration.coachingweeklycost[4].checked  == true )
		document.forms.registration.coachinglevel.value = "E-Coaching Plus";
	else 
		document.forms.registration.coachinglevel.value = "";


	if( document.forms.registration.discount[0].checked  == true )
		document.forms.registration.discountdescription.value = "Repeat Training Plan with no coaching";
	else if( document.forms.registration.discount[1].checked  == true )
		document.forms.registration.discountdescription.value = "Repeat Training Plan plus Coaching with no previous Coaching";
	else if( document.forms.registration.discount[2].checked  == true )
		document.forms.registration.discountdescription.value = "Repeat Training Plan plus Coaching with previous Coaching";
	else 	
		document.forms.registration.discountdescription.value = "";
	
	if( document.forms.registration.coachingweeklycost[5].checked==false )
	{
		if( document.forms.registration.coachinglocationother.value != 'yes' ) 
		{
		if( document.forms.registration.coachinglocation0full.value != 'full' )
			document.forms.registration.coachinglocation[0].disabled=false;
		if( document.forms.registration.coachinglocation1full.value != 'full' )
			document.forms.registration.coachinglocation[1].disabled=false;
		if( document.forms.registration.coachinglocation2full.value != 'full' )
			document.forms.registration.coachinglocation[2].disabled=false;
		if( document.forms.registration.coachinglocation3full.value != 'full' )
			document.forms.registration.coachinglocation[3].disabled=false;
		if( document.forms.registration.coachinglocation4full.value != 'full' )
			document.forms.registration.coachinglocation[4].disabled=false;
		if( document.forms.registration.coachinglocation5full.value != 'full' )
			document.forms.registration.coachinglocation[5].disabled=false;
        }

		
		var edf=document.forms.registration.enddate;
		edf.disabled = false;
		
		
		var c=document.forms.registration.weeksabsent;
		if( c.value == "" )
			c.value = "0";
		c.style.borderColor="#BA9BBA";
		c.disabled = false;
		
		var da=document.forms.registration.datesabsent;
		da.disabled = false;
		
		
		
		var sdf=document.forms.registration.startdate;
		sdf.disabled = false;
		
		 
		sdf.style.borderColor="#BA9BBA";
		if (isDate(sdf.value)==false )
		{
			sdf.style.borderColor="red";
			return false;
		}
		edf.style.borderColor="#BA9BBA";
		if (isDate(edf.value)==false )
		{
			edf.style.borderColor="red";
			return false
		}
		
		
		
		
		
		if( isInteger( c.value )== false )
		{
			c.style.borderColor="red";
			return false;
		}
		
		
		if( !( sdf.value == ""           || edf.value == "" ) )
		{


			var sd = new Date( myGetYear(sdf.value) , myGetMonth(sdf.value) , myGetDay(sdf.value ));
			
			var ed = new Date( myGetYear(edf.value) , myGetMonth(edf.value) , myGetDay(edf.value ));
			
			var diff = ed.getTime() - sd.getTime();
			
			diff = diff / (86400000*7);
			if( diff < 0 ) diff = diff * -1;
			
			diff = diff - parseInt(c.value);
			if( diff < 0 ) diff = 0;
			
			document.forms.registration.coachingweeks.value = diff.toFixed(1);
			diff = parseFloat(document.forms.registration.coachingweeks.value) ;
			
			
			if( document.forms.registration.coachingweeks.value == "NaN" )
				document.forms.registration.coachingweeks.value = "0";
			
			var weeklyfee = 0.00;
			for( var i = document.forms.registration.coachingweeklycost.length-1 ; i > -1 ; i-- )
			{
				if( document.forms.registration.coachingweeklycost[i].checked==true )
				{
					weeklyfee = weeklyfee + 
					parseFloat(document.forms.registration.coachingweeklycost[i].value) ;
					break;
				}
			}
			
			fee = diff * weeklyfee;
			document.forms.registration.currentregistration.value = fee.toFixed(2);
		}
		
		
		document.forms.registration.discount[0].disabled=true;
		document.forms.registration.discount[1].disabled=true;
		document.forms.registration.discount[2].disabled=true;		
		
		if( document.forms.registration.customizedtrainingplan.checked == true )
		{
			document.forms.registration.discount[0].disabled=false;
			if( parseFloat(document.forms.registration.coachingweeks.value) >= 10 )
			{
				document.forms.registration.discount[1].disabled=false;
				document.forms.registration.discount[2].disabled=false;
			}
		}		

		
	}
	else
	{
		
		
		
		var sdf=document.forms.registration.startdate;
		sdf.style.borderColor="#BA9BBA";
		sdf.disabled = true;
		 
		var edf=document.forms.registration.enddate;
		edf.style.borderColor="#BA9BBA";
		edf.disabled = true;
		
		var c=document.forms.registration.weeksabsent;
		c.style.borderColor="#BA9BBA";
		c.disabled = true;
		
		var da=document.forms.registration.datesabsent;
		da.disabled = true;
		
		document.forms.registration.coachinglocation[0].disabled=true;
		document.forms.registration.coachinglocation[1].disabled=true;
		document.forms.registration.coachinglocation[2].disabled=true;
		document.forms.registration.coachinglocation[3].disabled=true;
		document.forms.registration.coachinglocation[4].disabled=true;
		document.forms.registration.coachinglocation[5].disabled=true;
		

		fee = 0.00; 

		if( document.forms.registration.customizedtrainingplan.checked == true )
		{
			document.forms.registration.discount[0].disabled=false;
		}
		else
		{
			document.forms.registration.discount[0].disabled=true;
			
		}
		document.forms.registration.discount[1].disabled=true;
		document.forms.registration.discount[2].disabled=true;
		
	}
/*	
	c=document.forms.registration.racepacerun1;
	if( c.checked == true )
	{
		fee += parseFloat(c.value);
	}
	c=document.forms.registration.racepacerun2;
	if( c.checked == true )
	{
		fee += parseFloat(c.value);
	}
	c=document.forms.registration.racepacerun3;
	if( c.checked == true )
	{
		fee += parseFloat(c.value);
	}
*/	
	
	c=document.forms.registration.racedaycoaching;
	if( c.value == "" ) c.value = "0.00";
	
	c.style.borderColor="#BA9BBA";
	if( isCurrency( c.value ,false)== false )
	{
		c.style.borderColor="red";
		return false;
	}
	fee += parseFloat(c.value);
	
	c=document.forms.registration.privatecoaching;
	if( c.value == "" ) c.value = "0.00";
	c.style.borderColor="#BA9BBA";
	if( isCurrency( c.value , false )== false )
	{
		c.style.borderColor="red";
		return false;
	}
	fee += parseFloat(c.value);
	
	
	c=document.forms.registration.otherservice;
	if( c.value == "" ) c.value = "0.00";
	c.style.borderColor="#BA9BBA";
	
	if( isCurrency( c.value , true )== false )
	{
		c.style.borderColor="red";
		return false;
	}
	fee += parseFloat(c.value);

	if( document.forms.registration.customizedtrainingplan.checked == true )
	{
		fee += 80.00;
	}
	if( document.forms.registration.discount[0].checked == true && fee > 10 )
		fee -= 10.00;
	else if( document.forms.registration.discount[1].checked == true && fee > 20 )
		fee -= 20.00;
	else if( document.forms.registration.discount[2].checked == true && fee > 30 )
		fee -= 30.00;
	
	if( document.forms.registration.registrationdate.value <= '2010-11-19' )
	{
		fee *= 1.05; // GST
	}
	else
	{
		fee *= 1.13; // HST
		
	}

	document.forms.registration.currentregistration.value = fee.toFixed(2);
	
	if(document.forms.registration.coachingweeklycost[5].checked==false && 
	parseFloat(document.forms.registration.coachingweeks.value) < 10 )
	{
		document.forms.registration.coachingweeks.style.borderColor="RED";
		
	}
	else
	{
		document.forms.registration.coachingweeks.style.borderColor="#BA9BBA";
		
	}
		
	if( fee > 0 && 
	(document.forms.registration.waiver.checked == true ||
	document.forms.registration.waivertop.checked == true )
	&&
	(document.forms.registration.coachingweeklycost[5].checked==true || 
	parseFloat(document.forms.registration.coachingweeks.value) >= 10 ) )
	{
		document.forms.registration.submitregistration.style.visibility = "visible";
		document.forms.registration.submitregistration2.style.visibility = "visible";
		document.forms.registration.currentregistration.style.visibility = "visible";
	}
	else	
	{
		document.forms.registration.submitregistration.style.visibility = "hidden";
		document.forms.registration.submitregistration2.style.visibility = "hidden";
		document.forms.registration.currentregistration.style.visibility = "hidden";
	}
		
	
	return true;
}


var dtCh= "-";
var minYear=2007;
var maxYear=2025;

function isCurrency(f,negAllowed)
{
	var nNum = 0;			// Total numbers for currency value.
	var nDollarSign = 0;	// Total times a dollar sign occurs.
	var nNegative = 0;	// Total times a negative sign occurs.
	var nDecimal = 0;		// Total times a decimal point occurs.
	var nCommas = 0;		// Total times a comma occurs.
	var txtLen;				// Length of string passed.
	var xTxt;				// Assigned object passed.
	var sDollarVal;			// Assigned dollar amount with or without commas.
	var bComma;				
	var decPos;				// Assigned value of numbers or positions after decimal point.
	var nNumCount = 0;		// Total number between commas.
	var i;					// For forloop indexing.
	var x;					// Assigned each indivual character in string.
	
	// Set the xTxt variable to the object passed to this function.
	// Assign the length of the string to txtLen.
	txtLen = f.length

	
	for(i = 0; i < txtLen; i++)
	{
		// Assign charater in substring to x.
		x = f.substr(i, 1);
		
		
		if(x == "$")
			nDollarSign = nDollarSign + 1; // Sum total times dollar sign occurs.
		else if(x == ".")
			nDecimal = nDecimal + 1; // Sum total times decimal point occurs.
		else if(x == "-")
			nNegative = nNegative + 1; // Sum total times negative sign occurs.
		else if(x == ",")
			nCommas = nCommas + 1; // Sum total times comma occurs.
		else if(parseInt(x) >= 0 || parseInt(x) <= 9)
			nNum = nNum + 1; // If the character is a number sum total times a number occurs.
		else
		{
			// Error occurs if any other character value is in the string
			// othere then the valid characters.
			return false;
		} // end else
	} // end for
	if(nDollarSign > 0 || nDecimal > 1 || nNegative > 1 )
	{
		return false;
	} // end if
		
	if(nNegative == 1 )
	{
		if( negAllowed == false )
			return false;
		else if(f.indexOf("-") != 0)
		{
			return false;
		} // end if
		
		
	}// end if

	if(nDecimal == 1)
	{
		// Get the number of numbers after the decimal point in
		// the string if there is a decimal point present
		decPos = (txtLen - 1) - f.indexOf(".");
		
		// Floating point cannot be more then two.
		// Valid format after decimal point.
		/**********************************/
		/*   $#.##, $#.#, $.#, $#., $.##  */
		/**********************************/
		if(decPos > 2)
		{
			return false;
		} // end if
	} // end if
	
	if(nCommas == 0)
	{
		// If no commas are present value is a valid US
		// currency.
		return true;
	}
	else
	{
		// Get total number of dollar number(s), removing
		// floating point numbers or cents.
		nNum = nNum - decPos;
		
		// Determine if dollar sign is in string so to be 
		// removed.
		// After determining dollar sign, assign sDollarVal
		// numbers and comma(s)
		if(f.indexOf("$", 0) == 0)
			sDollarVal = f.substr(1, (nNum + nCommas));
		else
			sDollarVal = f.substr(0, (nNum + nCommas));
		
		
		// Determine if a zero is the first number or if a
		// comma is the first or last character in the string.
		if(sDollarVal.lastIndexOf("0", 0) == 0 )
		{
			return false;
		}
		else if(sDollarVal.lastIndexOf(",", 0) == 0)
		{
			return false;
		}
		else if(sDollarVal.indexOf(",", (sDollarVal.length - 1)) == (sDollarVal.length - 1))
		{
			return false;
		}
		else
		{
			// Initialize bComma indicating a comma has not been
			// occured yet.
			bComma = false;
			for(i = 0; i < sDollarVal.length; i++)
			{
				// Assign charater in substring to x.
				x = sDollarVal.substr(i, 1);
				
				if(parseInt(x) >= 0 || parseInt(x) <= 9)
				{
					// If x is a number add one to the number counter.
					nNumCount = nNumCount + 1;
					
					// Sense comma(s) are present number counter cannot
					// be more then three before the first or next comma.
					if(nNumCount > 3)
					{
						return false;
					} // end if
				}
				else
				{
					// If the number counter is less then three and
					// the comma indicator is true the comma is either
					// mis-placed or there are not enough values.
					if(nNumCount != 3 && bComma)
					{
						return false;
					} // end if
					
					// Reset the number counter back to zero.
					nNumCount = 0;
					
					// Set the comma indicator to true indicating
					// that the first comma has been found and that
					// there now MUST be three numbers after each
					// comma until the loop hits the end.
					bComma = true;
				} // end if
			} // end for
			
			// Determine if after the loop ended that there
			// was a total of three final numbers after the
			// last comma.
			if(nNumCount != 3 && bComma)
			{
				return false;
			} // end if
		} // end if
	} // end if
	
	// Return true indicating that the value is a valid
	// currency.
	return true;
}

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)

	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	
	var strYear=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strDay=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		return false
	}
return true
}

function convertDate(dtStr){

	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var strMonth=dtStr.substring(0,pos1);
	var strDay=dtStr.substring(pos1+1,pos2);
	var strYear=dtStr.substring(pos2+1);
	return strYear + "-" + strMonth + "-" + strDay;
	
}


function myGetYear(dtStr){

	var pos1=dtStr.indexOf(dtCh);
	var m = dtStr.substring(0,pos1);
	return m;
	
}
function myGetMonth(dtStr){

	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	var m = dtStr.substring(pos1+1,pos2);
	var mi = 0 + m - 1;
	return mi;	
	
}
function myGetDay(dtStr){

	var pos1=dtStr.indexOf(dtCh);
	var pos2=dtStr.indexOf(dtCh,pos1+1);
	return dtStr.substring(pos2+1);
	
}

function calctotal()
{
	var fee = 0.00;
	for( var i = document.forms.registration.coachingweeklycost.length-1 ; i > -1 ; i-- )
	{
		fee = fee + (s[i] * (document.forms.registration.coachingweeklycost[i].checked==true));
	}
		
	document.forms.registration.currentregistration.value = fee.toFixed(2);
	
	return true;
}

function submitform( action , n )
{
	var confirmed = true;
	if( action == "delete" )
	{
		confirmed = confirm( "WARNING: Are you sure you want to delete this invoice? All payments made on this invoice will also be deleted from the database." );	
	}
	if( confirmed )
	{
	document.forms.registration.action = "display.php?page=account&";
	document.forms.registration.action += action;
	document.forms.registration.action += "=yes&n=";
	document.forms.registration.action += n;
	document.forms.registration.submit();
	}
}

