// Common js functions

function setInput( field, action )
{
	switch ( action )
	{
		case "clear":
			if ( field.defaultValue == field.value )
			{
				field.value = "";
			}
			break;
		case "reset":
			if ( field.defaultValue == "" )
			{
				field.value = field.defaultValue;
			}
			break;
		default:
			// No action
    }
}

function popup( url, name, height, width, menubar )
{
	var config = "height=" + height + ", width=" + width + ", toolbar=no, menubar=" + menubar + ", location=no, directories=no, status=yes, resizable=yes, scrollbars=yes";
	var win = window.open( url, name, config );
}

function closeWindow( url, name )
{
	window.close();
	if ( url != "" )
	{
		if ( !window.opener || window.opener.closed )
		{
			if ( name == "" )
			{
				var name = "Opener";
			}
			var win = window.open( url, name, "height=600, width=600, toolbar=yes, menubar=yes, location=yes, directories=yes, status=yes, resizable=yes, scrollbars=yes" );
		}
	}
}

function changeField( selectedField, changingField, changingValue )
{
	var selectedValue = selectedField.options[selectedField.selectedIndex].value;
	var selfCall = false;
	var firstChangingField = changingField;
	var firstChangingValue = changingValue;
	if ( changingField.length > 1 )
	{
		selfCall = true;
		var firstChangingField = changingField.splice( 1, 1 );
		var firstChangingValue = changingValue.splice( 1, 1 );
	}
	if ( selectedValue == "" )
	{
		while ( firstChangingField[0].options.length > 0 )
		{
			firstChangingField[0].options[( firstChangingField[0].options.length - 1 )] = null;
		}
		firstChangingField[0].options[0] = new Option( "-", "" );
	}
	else
	{
		var changingArray = eval( selectedField.name + "_" + selectedValue );
		while ( changingArray.length < firstChangingField[0].options.length )
		{
			firstChangingField[0].options[( firstChangingField[0].options.length - 1 )] = null;
		}
		for ( var i = 0; i < changingArray.length; i++ )
		{
			firstChangingField[0].options[i] = new Option( changingArray[i][0], changingArray[i][1] );
			if ( firstChangingValue[0] == changingArray[i][1] )
			{
				firstChangingField[0].options[i].selected = true;
			}
		}
	}

	if ( selfCall  )
	{
		changeField( firstChangingField[0], changingField, changingValue );
	}
}

function checkValueBeforeSubmit( form, checkingField, alertText )
{ 
	if ( checkingField.value == "" )
	{
		 alert( alertText );
		 return false;
	}
	else
	{
		form.submit();
		return true;
	}	
}

// Font switchers functions

function setCookie( name, value, days, path, domain, secure )
{
	var expires, date;
	if ( typeof days == "number")
	{
		date = new Date();
		date.setTime( date.getTime() + ( days*24*60*60*1000 ) );
		expires = date.toGMTString();
	}
	document.cookie = name + "=" + escape( value ) +
	( ( expires ) ? "; expires=" + expires : "" ) +
	( ( path ) ? "; path=" + path : "" ) +
	( ( domain ) ? "; domain=" + domain : "" ) +
	( ( secure ) ? "; secure" : "" );
}

function getCookie( name )
{
	var nameq = name + "=";
	var c_ar = document.cookie.split( ';' );
	for ( var i=0; i < c_ar.length; i++ )
	{
    	var c = c_ar[i];
    	while ( c.charAt( 0 ) == ' ' )
    	{
    		c = c.substring( 1, c.length );
    	}
		if ( c.indexOf( nameq ) == 0 )
		{
			return unescape( c.substring( nameq.length, c.length ) );
		}
	}
	return '';
}

function deleteCookie( name, path, domain )
{
	if ( getCookie( name ) )
	{
		document.cookie = name + "=" +
		( ( path ) ? "; path=" + path : "") +
		( ( domain ) ? "; domain=" + domain : "" ) +
		"; expires=Thu, 01-Jan-70 00:00:01 GMT";
	}
}

var sizeBody = 1;

function fixSize( font_size )
{
 	setCookie( "sizeFont", font_size, 100, "/" );
 	sizeText = font_size + 'em';
	document.body.style.fontSize = sizeText;
}

function increaseSize()
{
	var font_size = getCookie( "sizeFont" );
	if ( font_size == "" )
	{
		font_size = sizeBody + 0.2;
	}
	else if ( font_size < 1.4 )
	{
		font_size = eval( font_size ) + 0.2;
	}
	fixSize( font_size );
}

function decreaseSize()
{
	var font_size = getCookie( "sizeFont" );
 	if ( font_size == "" )
 	{
		font_size = sizeBody - 0.2;
	}	
	else if ( font_size > 0.8 )
	{
		font_size = eval( font_size ) - 0.2;
	}	
	fixSize( font_size );
}

function restartSize()
{
	var font_size = sizeBody;
	fixSize( font_size );
}

function startSize()
{
	var font_size = getCookie( "sizeFont" );
	if (font_size == "" )
	{
		font_size = sizeBody;
	}
	fixSize( font_size );
}

