/**
 *	Commonly used functions.
 *	
 *	@author	Dan Joergensen, GOLFvenner.dk
 *	
 */

function updateImage_old()
{
	this.data.preview.src = 'gfx/article/' + this.data.img_url.value;
}

function updateImage( path )
{
	this.data.preview.src = path + this.data.img_url.value;
}

function setHeader( page )
{
	parent.groupheader.location.replace( page );
}

function refresh()
{
	parent.topmenu.location.replace( "topmenu.php" );
}

function setMenu ( id ) {
	return;
	parent.groupheader.location.replace( 'header.php?id=' + id );
	parent.menu.location.replace( 'a-menu.php?mod=' + id );
	parent.submenu.location.replace( 'submenu.php?id=' + id );
}

function setSubmenu( page )
{
	parent.submenu.location.replace( page );
}

function redirect( page )
{
	setTimeout('location.replace ( "' + page + '" )', 2000 );
}

function redirectNoDelay( page )
{
	setTimeout('location.replace ( "' + page + '" )', 1 );
}

function go ( hole )
{
	location.replace ( "holbaek-" + hole + ".html" );
}

function showMatch( round_id )
{
	location.replace ( "matches/match-" + round_id + ".html" );

}

function showMatchByPlayer( player_id )
{
	location.replace ( "playerstatistics-" + player_id + ".html" );
}

function calculateScore()
{
	checkScratch ( this.scorecard.hole1, par1, extra1 );
	checkScratch ( this.scorecard.hole2, par2, extra2 );
	checkScratch ( this.scorecard.hole3, par3, extra3 );
	checkScratch ( this.scorecard.hole4, par4, extra4 );
	checkScratch ( this.scorecard.hole5, par5, extra5 );
	checkScratch ( this.scorecard.hole6, par6, extra6 );
	checkScratch ( this.scorecard.hole7, par7, extra7 );
	checkScratch ( this.scorecard.hole8, par8, extra8 );
	checkScratch ( this.scorecard.hole9, par9, extra9 );
	checkScratch ( this.scorecard.hole10, par10, extra10 );
	checkScratch ( this.scorecard.hole11, par11, extra11 );
	checkScratch ( this.scorecard.hole12, par12, extra12 );
	checkScratch ( this.scorecard.hole13, par13, extra13 );
	checkScratch ( this.scorecard.hole14, par14, extra14 );
	checkScratch ( this.scorecard.hole15, par15, extra15 );
	checkScratch ( this.scorecard.hole16, par16, extra16 );
	checkScratch ( this.scorecard.hole17, par17, extra17 );
	checkScratch ( this.scorecard.hole18, par18, extra18 );

	strokes = 0;
//	strokes = parseInt( this.scorecard.hole1.value );

	if ( this.scorecard.hole1.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole1.value );
	}
	if ( this.scorecard.hole2.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole2.value );
	}
	if ( this.scorecard.hole3.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole3.value );
	}
	if ( this.scorecard.hole4.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole4.value );
	}
	if ( this.scorecard.hole5.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole5.value );
	}
	if ( this.scorecard.hole6.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole6.value );
	}
	if ( this.scorecard.hole7.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole7.value );
	}
	if ( this.scorecard.hole8.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole8.value );
	}
	if ( this.scorecard.hole9.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole9.value );
	}

	if ( isNaN( strokes ) ) {
		strokes = 0;
	}

    front_strokes.innerText = parseInt( strokes );
	i = strokes;

	strokes = 0;
//	strokes = parseInt( this.scorecard.hole10.value );

	if ( this.scorecard.hole10.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole10.value );
	}
	if ( this.scorecard.hole11.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole11.value );
	}
	if ( this.scorecard.hole12.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole12.value );
	}
	if ( this.scorecard.hole13.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole13.value );
	}
	if ( this.scorecard.hole14.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole14.value );
	}
	if ( this.scorecard.hole15.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole15.value );
	}
	if ( this.scorecard.hole16.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole16.value );
	}
	if ( this.scorecard.hole17.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole17.value );
	}
	if ( this.scorecard.hole18.value != '' ) {
		strokes =parseInt(strokes) + parseInt( this.scorecard.hole18.value );
	}

	if ( isNaN( strokes ) ) {
		strokes = 0;
	}
	
    back_strokes.innerText = parseInt( strokes );
	total_strokes.innerText = parseInt(strokes) + parseInt( i );

	point1.innerText = calcHole( par1.innerText, extra1.innerText, this.scorecard.hole1.value );
	point2.innerText = calcHole( par2.innerText, extra2.innerText, this.scorecard.hole2.value );
	point3.innerText = calcHole( par3.innerText, extra3.innerText, this.scorecard.hole3.value );
	point4.innerText = calcHole( par4.innerText, extra4.innerText, this.scorecard.hole4.value );
	point5.innerText = calcHole( par5.innerText, extra5.innerText, this.scorecard.hole5.value );
	point6.innerText = calcHole( par6.innerText, extra6.innerText, this.scorecard.hole6.value );
	point7.innerText = calcHole( par7.innerText, extra7.innerText, this.scorecard.hole7.value );
	point8.innerText = calcHole( par8.innerText, extra8.innerText, this.scorecard.hole8.value );
	point9.innerText = calcHole( par9.innerText, extra9.innerText, this.scorecard.hole9.value );
	point10.innerText = calcHole( par10.innerText, extra10.innerText, this.scorecard.hole10.value );
	point11.innerText = calcHole( par11.innerText, extra11.innerText, this.scorecard.hole11.value );
	point12.innerText = calcHole( par12.innerText, extra12.innerText, this.scorecard.hole12.value );
	point13.innerText = calcHole( par13.innerText, extra13.innerText, this.scorecard.hole13.value );
	point14.innerText = calcHole( par14.innerText, extra14.innerText, this.scorecard.hole14.value );
	point15.innerText = calcHole( par15.innerText, extra15.innerText, this.scorecard.hole15.value );
	point16.innerText = calcHole( par16.innerText, extra16.innerText, this.scorecard.hole16.value );
	point17.innerText = calcHole( par17.innerText, extra17.innerText, this.scorecard.hole17.value );
	point18.innerText = calcHole( par18.innerText, extra18.innerText, this.scorecard.hole18.value );

	points = parseInt(point1.innerText);
	points = parseInt(points) + parseInt(point2.innerText);
	points = parseInt(points) + parseInt(point3.innerText);
	points = parseInt(points) + parseInt(point4.innerText);
	points = parseInt(points) + parseInt(point5.innerText);
	points = parseInt(points) + parseInt(point6.innerText);
	points = parseInt(points) + parseInt(point7.innerText);
	points = parseInt(points) + parseInt(point8.innerText);
	points = parseInt(points) + parseInt(point9.innerText);
	front_point.innerText = points;
	
	points = parseInt(points) + parseInt(point10.innerText);
	points = parseInt(points) + parseInt(point11.innerText);
	points = parseInt(points) + parseInt(point12.innerText);
	points = parseInt(points) + parseInt(point13.innerText);
	points = parseInt(points) + parseInt(point14.innerText);
	points = parseInt(points) + parseInt(point15.innerText);
	points = parseInt(points) + parseInt(point16.innerText);
	points = parseInt(points) + parseInt(point17.innerText);
	points = parseInt(points) + parseInt(point18.innerText);
	
	back_point.innerText = parseInt(points)-parseInt(front_point.innerText);
	total_point.innerText = points;
}

function checkScratch( hole, par, extra )
{
	if ( isNaN(hole.value) ) {
		hole.value = parseInt( par.innerText ) + parseInt( extra.innerText ) + 2;
	}
}

function calcHole( par, extra, strokes )
{
    score = parseInt(par) + parseInt(extra) - parseInt(strokes) + 2;
    
	if ( isNaN(score) )  {
		score = 0;
	}

	if ( score < 0 )  {
		score = 0;
	}
		
	return score;	
}

