var FIELD_EMAIL = 0;
var FIELD_CLIENT_DATA = 1;
var FIELD_SERVICE = 2;
var FIELD_ARTICLE_SIZE_X = 3;
var FIELD_ARTICLE_SIZE_Y = 4;
var FIELD_ARTICLE_CNT = 5;
var FIELD_WHOLES_CNT = 6;
var FIELD_COLORS_CNT = 7;
var FIELD_WINDOW_GLOSS = 8;
var FIELD_BUTTON_CNT = 9;
var FIELD_EMBOSS_CNT = 10;
var FIELD_UNIQUE_EMBOSS_CNT = 11;
var FIELD_BUTTON_TYPE = 12;
var FIELD_RIBBON_SIZE_X = 13;
var FIELD_RIBBON_SIZE_Y = 14;
var FIELD_CIRCUIT_LAYERS_CNT = 15;
var FIELD_GLUE = 16;
var FIELD_CUT = 17;
var FIELD_SOLDER = 18;
var FIELD_DESCRIPTION_TOP = 19;
var FIELD_DESCRIPTION_BOTTOM = 20;
var FIELD_TINNING = 21;
var FIELD_DRILLING = 22;
var FIELD_MILLING_CNT = 23;
var FIELD_ENGRAVED_GROUP_SIZE = 24;
var FIELD_NOTES = 25;
var FIELD_PROJECT_FILE = 26;
var FIELD_INCISION = 27;
var FIELD_CNT = 28;

var fieldsSpec = new Array();

fieldsSpec[ FIELD_EMAIL ] = new Array( "email" );
fieldsSpec[ FIELD_CLIENT_DATA ] = new Array( "client_data" );
fieldsSpec[ FIELD_SERVICE ] = new Array( "service" );
fieldsSpec[ FIELD_ARTICLE_SIZE_X ] = new Array( "article_size_x" );
fieldsSpec[ FIELD_ARTICLE_SIZE_Y ] = new Array( "article_size_y" );
fieldsSpec[ FIELD_ARTICLE_CNT ] = new Array( "article_cnt" );
fieldsSpec[ FIELD_WHOLES_CNT ] = new Array( "wholes_cnt", "+service", "elewacja klawiatury foliowej", "0", "+service", "klawiatura foliowa", "0", "+service", "naklejka samoprzylepna", "0", "+service", "nadruk na detalach", "0", "-service", "obwód drukowany", null, "-service", "szablon SMD", null );
fieldsSpec[ FIELD_COLORS_CNT ] = new Array( "colors_cnt", "+service", "elewacja klawiatury foliowej", "0", "+service", "klawiatura foliowa", "0", "+service", "naklejka samoprzylepna", "0", "+service", "nadruk na detalach", "0", "-service", "obwód drukowany", null, "-service", "szablon SMD", null );
fieldsSpec[ FIELD_WINDOW_GLOSS ] = new Array( "window_gloss", "+service", "elewacja klawiatury foliowej", "false", "+service", "klawiatura foliowa", "false", "-service", "!elewacja klawiatury foliowej", null, "-service", "!klawiatura foliowa", null );
fieldsSpec[ FIELD_BUTTON_CNT ] = new Array( "button_cnt", "+service", "elewacja klawiatury foliowej", "0", "+service", "klawiatura foliowa", "0", "-service", "!elewacja klawiatury foliowej", null, "-service", "!klawiatura foliowa", null );
fieldsSpec[ FIELD_EMBOSS_CNT ] = new Array( "emboss_cnt", "+service", "elewacja klawiatury foliowej", "@button_cnt", "+service", "klawiatura foliowa", "@button_cnt", "-service", "!elewacja klawiatury foliowej", null, "-service", "!klawiatura foliowa", null );
fieldsSpec[ FIELD_UNIQUE_EMBOSS_CNT ] = new Array( "unique_emboss_cnt", "+service", "elewacja klawiatury foliowej", "0", "+service", "klawiatura foliowa", "0", "-service", "!elewacja klawiatury foliowej", null, "-service", "!klawiatura foliowa", null );
fieldsSpec[ FIELD_BUTTON_TYPE ] = new Array( "button_type", "+service", "klawiatura foliowa", "Wybierz", "-service", "!klawiatura foliowa", null );
fieldsSpec[ FIELD_RIBBON_SIZE_X ] = new Array( "ribbon_size_x", "+button_type", "blaszka", null, "+button_type", "folia", null, "-button_type", "microswitch", null, "-service", "!klawiatura foliowa", null );
fieldsSpec[ FIELD_RIBBON_SIZE_Y ] = new Array( "ribbon_size_y", "+button_type", "blaszka", null, "+button_type", "folia", null, "-button_type", "microswitch", null, "-service", "!klawiatura foliowa", null );
fieldsSpec[ FIELD_CIRCUIT_LAYERS_CNT ] = new Array( "circuit_layers_cnt", "+service", "klawiatura foliowa", "1", "+service", "obwód drukowany", "1", "-service", "!klawiatura foliowa", null, "-service", "!obwód drukowany", null );
fieldsSpec[ FIELD_INCISION ] = new Array( "incision", "+service", "naklejka samoprzylepna", "false", "-service", "!naklejka samoprzylepna", null );
fieldsSpec[ FIELD_GLUE ] = new Array( "glue", "+service", "elewacja klawiatury foliowej", "false", "+service", "klawiatura foliowa", ":true", "+button_type", "microswitch", ":false", "+button_type", "!microswitch", ":true", "-service", "!elewacja klawiatury foliowej", null, "-service", "!klawiatura foliowa", null );
fieldsSpec[ FIELD_CUT ] = new Array( "cut", "+service", "elewacja klawiatury foliowej", "false", "+service", "klawiatura foliowa", ":true", "+service", "naklejka samoprzylepna", "false", "-service", "!elewacja klawiatury foliowej", null, "-service", "!klawiatura foliowa", null, "-service", "!naklejka samoprzylepna", null );
fieldsSpec[ FIELD_SOLDER ] = new Array( "solder", "+service", "obwód drukowany", "true", "-service", "!obwód drukowany", null );
fieldsSpec[ FIELD_DESCRIPTION_TOP ] = new Array( "description_top", "+service", "obwód drukowany", null, "-service", "!obwód drukowany", null );
fieldsSpec[ FIELD_DESCRIPTION_BOTTOM ] = new Array( "description_bottom", "+service", "obwód drukowany", "true", "-service", "!obwód drukowany", null );
fieldsSpec[ FIELD_TINNING ] = new Array( "tinning", "+service", "obwód drukowany", "true", "-service", "!obwód drukowany", null );
fieldsSpec[ FIELD_DRILLING ] = new Array( "drilling", "+service", "obwód drukowany", "true", "-service", "!obwód drukowany", null );
fieldsSpec[ FIELD_MILLING_CNT ] = new Array( "milling_cnt", "+service", "obwód drukowany", "0", "-service", "!obwód drukowany", null );
fieldsSpec[ FIELD_ENGRAVED_GROUP_SIZE ] = new Array( "engraved_group_size", "+service", "obwód drukowany", "0", "-service", "!obwód drukowany", null );
fieldsSpec[ FIELD_NOTES ] = new Array( "notes" );
fieldsSpec[ FIELD_PROJECT_FILE ] = new Array( "project_file" );



function debug( msg )
{
	alert( msg );
}


function toUnsignedInt( string )
{
	if( string == null )
		return -1;

	var multiplier = 1;
	var value = 0;
	var i;

	for( i = string.length - 1; i >= 0; i-- )
	{
		if( string.charAt(i) < '0' && string.charAt(i) > '9' )
			return -1;

		var digit = string.charAt(i) - '0';
		value = value + digit * multiplier;
		multiplier = multiplier * 10;
	}
	return value;
}

function toBool( string )
{
	if( string == null )
		return false;

	if( string.toLowerCase() == "true" || string == "1" )
		return true;

	if( string.toLowerCase() != "false" && string != "0" )
		alert( string + " unconvertable to boolean!" );

	return false;
}

/*
function powerOf10( exponent )
{
	if( exponent < 0 )
	{
		alert( "Negative exponent not supported by power function" );
		return -1;
	}
	var power = 1;
	var i=0;
	for( i=0; i<exponent; i++ )
		power = power * 10;
	return power;
}

function toFloatConverter( string )
{
	this.isCorrect = false;
	
	if( string == null )
		return;
	
	val dotPos = string.indexOf( "." );
	if( dotPos == -1 )
		dotPos = string.length;
	
	var multiplier = powerOf10( string.length - 1 );
	var pos = 0;
	for( pos=0; pos < string.length; pos++ )
	{		
		var digit = string 
	}
}


function StringConverterToFloat()
{
}
*/

function disableField( field )
{
	if( field == null )
		return false;

	field.disabled = true;
	field.style.backgroundColor = "dimgray";
	field.style.visibility = 'hidden';

	var label = window.document.getElementById( "label_" + field.id );
	if( label != null )
	{
		label.style.color = "dimgray";
		label.style.visibility = 'hidden';
	}
	var extraLabel = window.document.getElementById( "label_" + field.id + "_" );
	if( extraLabel != null )
	{
		extraLabel.style.color = "dimgray";
		extraLabel.style.visibility = 'hidden';
	}
}

function enableField( field )
{
	if( field == null )
		return false;

	field.disabled = false;
	field.style.backgroundColor = "white";
	field.style.visibility = 'visible';

	var label = window.document.getElementById( "label_" + field.id );
	if( label != null )
	{
		label.style.color = "white";
		label.style.visibility = 'visible';
	}
	var extraLabel = window.document.getElementById( "label_" + field.id + "_" );
	if( extraLabel != null )
	{
		extraLabel.style.color = "white";
		extraLabel.style.visibility = 'visible';
	}
}


function lockFormField( field )
{
	if( field == null )
	{
		alert( "Fatal error, null field!" );
		return false;
	}
	//field.disabled = true;
	field.readOnly = true;
	return true;
}

function lockFormFieldById( fieldId )
{
	lockFormField( getFormField( fieldId ));
}

function unlockFormField( field )
{
	if( field == null )
	{
		alert( "Fatal error, null field!" );
		return false;
	}
	//field.disabled = false;
	field.readOnly = false;
	return true;
}

function unlockFormFieldById( fieldId )
{
	unlockFormField( getFormField( fieldId ));
}

function disableFormField( field )
{
	if( field == null )
	{
		alert( "Fatal error, null field!" );
		return false;
	}
	field.disabled = true;
	return true;
}

function disableFormFieldById( fieldId )
{
	disableFormField( getFormField( fieldId ));
}

function enableFormField( field )
{
	if( field == null )
	{
		alert( "Fatal error, null field!" );
		return false;
	}
	field.disabled = false;
	return true;
}

function enableFormFieldById( fieldId )
{
	enableFormField( getFormField( fieldId ));
}

function setFormFieldLockFlagById( fieldId, status )
{
	if( !status )
	{
		lockFormField( getFormField( fieldId ));
	}
	else
	{
		unlockFormField( getFormField( fieldId ));
	}
}

function setFormFieldEnableFlagById( fieldId, status )
{
	if( !status )
	{
		disableFormField( getFormField( fieldId ));
	}
	else
	{
		enableFormField( getFormField( fieldId ));
	}
}

function getFormFieldEnableFlag( field )
{
	if( field == null )
	{
		alert( "Fatal error, null field!" );
		return false;
	}
	return field.disabled;
}

function getFormFieldEnableFlagById( fieldId )
{
	return getFormFieldEnableFlag( getFormField( fieldId ));
}



function getCurrentFormFieldVal( field )
{
	if( field == null )
	{
		alert( "Trying to get value of NULL form field!" );
		return null;
	}
	if( field.type == 'select-one' )
	{
		return field.options[ field.selectedIndex ].value;
	}
	else if( field.type == 'checkbox' || field.type == 'radio' )
	{
		return field.checked;
	}
	return field.value;
}

function getCurrentFormFieldValById( id )
{
	return getCurrentFormFieldVal( getFormField( id ));
}

function setFormFieldVal( field, value )
{
	if( field == null )
	{
		alert( "Trying to get value of NULL form field!" );
		return;
	}
	if( field.type == 'select-one' )
	{
		var i=0;
		for( i=0; i<field.length; i++ )
			if( field.options[ i ].text == value || field.options[ i ].value == value)
			{
				field.selectedIndex = i;
				return;
			}
		alert( value + " is not an option of " + field.id );
		return;
	}
	else if( field.type == 'checkbox' || field.type == 'radio' )
	{
		return field.checked = toBool( value );
	}
	field.value = value;
}

function isConditionForFieldUpdateFulfilled( field, changedField, condition )
{
	if( ( changedField.type == 'checkbox' || changedField.type == 'radio' ) && changedField.checked != toBool( condition ) )
		return false;
	if( condition.charAt(0) == '#' )
	{
		if( condition.length == 1 )
		{
			alert( "corrupted condition!" );
			return false;
		}
		if( field.type != 'text' )
		{
			alert( "type mismatch" );
			return false;
		}
		var fieldVal = toUnsignedInt( getCurrentFormFieldVal( changedField ));
		if( fieldVal == -1 )
		{
			alert( field.name + " unconvertable to number!" );
			return false;
		}

		var lowerRangeString = condition.slice( 1 );
		var lowerRangeVal = toUnsignedInt( lowerRangeString );
		if( lowerRangeVal == -1 )
		{
			alert( "string " + lowerRangeString + " unconvertable!" );
			return false;
		}
		if( fieldVal <= lowerRangeVal )
			return false;
	}
	else if( condition.charAt(0) == '!' )
	{
		if( getCurrentFormFieldVal( changedField ) == condition.split(1) )
			return false;
	}
	else if( getCurrentFormFieldVal( changedField ) != condition )
	{
		return false;
	}

	return true;
}


function setFieldValue( field, expr )
{
	var isToBeLocked = false;
	if( expr != null && expr.length > 0 && expr.charAt(0) == ':' ) // readonly
	{
		//field.disabled = true;
		isToBeLocked = true;
		expr = expr.slice(1);
	}

	if( expr != null && expr.length > 0 && expr.charAt(0) == '@' )
	{
		if( expr.length == 1 )
		{
			alert( "corrupted expression!" );
			return false;
		}
		var fieldToCopyFrom = window.document.getElementById( expr.slice( 1 ) );
		if( fieldToCopyFrom == null )
		{
			alert( "Field " + fieldToCopyFrom + " not found!" );
			return false;
		}
		setFormFieldVal( field, getCurrentFormFieldVal( fieldToCopyFrom ))
	}
	setFormFieldVal( field, expr );
	if( isToBeLocked )
	{
		field.disabled = true;
	}
	return true;
}

function setFormFieldValById( id, value )
{
	setFieldValue( window.document.getElementById( id ), value );
}


function getFormField( fieldId )
{
	var elem = document.getElementById( fieldId );
	if( elem == null )
	{
		alert( fieldId + " does not exist!" );
		return null;
	}
	return elem;
}



/**
 * changedField == null forces default state of the form incl. form reset
 */
function updateForm( form, changedField )
{
	if( form == null )
	{
		alert( "Unspecified form object!" );
		return false;
	}

	if( changedField == null )
		form.reset();

	var formSpecIt = 0;
	var formIt = 0;

	for( formIt = 0; formIt < form.elements.length; formIt++ )
	{
		var field = form.elements[ formIt ];

		if( changedField != null && field.id == changedField.id )
			continue;

		for( formSpecIt = 0; formSpecIt < fieldsSpec.length; formSpecIt++ )
		{
			if( fieldsSpec[formSpecIt][0] == field.id )
			{
				if( changedField == null ) // going to default state
				{
					if( fieldsSpec[formSpecIt].length == 1 ) // the field is unconditionally enabled
						enableField( field );
					else
						disableField( field );
					break;
				}

				var isFieldEnabled = false;
				var canFieldBeDisabled = true;
				var isFieldGoingToBeDisabled = false;

				for( s=1; !isFieldEnabled && s < fieldsSpec[formSpecIt].length; s=s+3 )
				{
					if( fieldsSpec[formSpecIt][s].length == 1 || fieldsSpec[formSpecIt][s].charAt(0) != '+' && fieldsSpec[formSpecIt][s].charAt(0) != '-' )
					{
						alert( "Corrupted field specification!" );
						continue;
					}
					if( fieldsSpec[formSpecIt][s].slice(1) != changedField.id )
						continue;

					var isConditionForFieldUpdateTrue = isConditionForFieldUpdateFulfilled( field, changedField, fieldsSpec[formSpecIt][s+1] );
					if( !isConditionForFieldUpdateTrue )
					{
						if( fieldsSpec[formSpecIt][s].charAt(0) == '-' )
						{
							canFieldBeDisabled = false;
						}
						continue;
					}

					if( fieldsSpec[formSpecIt][s].charAt(0) == '-' )
					{
						isFieldGoingToBeDisabled = true;
					}
					else if( fieldsSpec[formSpecIt][s].charAt(0) == '+' )
					{
						enableField( field );
						isFieldEnabled = true;
					}

					if( fieldsSpec[formSpecIt].length > 2 )
					{
						setFieldValue( field, fieldsSpec[formSpecIt][s+2] );
					}
				}

				if( fieldsSpec[formSpecIt].length > 1 // the filed is changeable
						&& ! isFieldEnabled  // the filed is not enabled
						&& canFieldBeDisabled && isFieldGoingToBeDisabled )
				{
					disableField( field );
				}
				break;
			}
		}
	}
}

function FieldCache()
{
	this.cacheTab = new Array();
	this.get = FieldCacheGet;
}

function FormChecker( form )
{
	this.form = form;
	this.getField = FormChecker_getField;
	this.check = FormChecker_check;
	this.checkEmail = FormChecker_checkEmail;
	this.checkElevation = FormChecker_checkElevation;
	this.checkKeyboard = FormChecker_checkKeyboard;
	this.checkPCB = FormChecker_checkPCB;
	this.checkSMD = FormChecker_checkSMD;
	this.checkSticker = FormChecker_checkSticker;
	this.checkOverprint = FormChecker_checkOverprint;	
	this.checkProductName = FormChecker_checkProductName;
	this.checkProductSize = FormChecker_checkProductSize;
	this.checkProductCnt = FormChecker_checkProductCnt;
	this.checkRibbon = FormChecker_checkRibbon;
	this.checkWireConnection = FormChecker_checkWireConnection;
	this.checkCustomConnection = FormChecker_checkCustomConnection;
	this.checkColors = FormChecker_checkColors;
	this.checkEmboss = FormChecker_checkEmboss;
	this.checkButtons = FormChecker_checkButtons;
	this.checkBackSeal = FormChecker_checkBackSeal;
	this.checkCircuitLayersCntAccordingToKeyboard = FormChecker_checkCircuitLayersCntAccordingToKeyboard;
	this.checkCircuitLayersCntAccordingToPCB = FormChecker_checkCircuitLayersCntAccordingToPCB;
}


function FormChecker_getField( fieldId )
{
	return getFormField( fieldId );
}


function FormChecker_check()
{
	if( this.form == null )
	{
		alert( "Unspecified form object!" );
		return false;
	}

	if( !this.checkProductName() )
		return false;

	if( !this.checkProductCnt() )
		return false;

	if( !this.checkProductSize() )
		return false;

	var serviceField = this.getField( 'serviceFormItem' );
	if( serviceField == null )
		return false;

	switch( getCurrentFormFieldVal( serviceField ))
	{
		case "elewacja klawiatury foliowej" : 
			if( !this.checkElevation() )
				return false;
			break;
			
		case "klawiatura foliowa" : 
			if( !this.checkKeyboard() )
				return false;
			break;
			
		case "obwód drukowany" : 
			if( !this.checkPCB() )
				return false;
			break;
			
		case "szablon SMD" : 
			if( !this.checkSMD() )
				return false;
			break;
			
		case "naklejka samoprzylepna" : 
			if( !this.checkSticker() )
				return false;
			break;
			
		case "nadruk na detalach" : 
			if( !this.checkOverprint() )
				return false;
			break;
			
		default : alert( "Unrecognized name of service " + getCurrentFormFieldVal( serviceField ));
			return false;
	}

	return true;
}

function FormChecker_checkEmail()
{
	var emailField = this.getField( 'emailFormItem' );
	if( emailField == null )
		return false;
		
	if( getCurrentFormFieldVal( emailField ).length < 3 )
	{
		alert( "Niepoprawny adres email" );
		return false;
	}
	if( getCurrentFormFieldVal( emailField ).indexOf( "@" ) == -1 )
	{
		alert( "Niepoprawny adres email" );
		return false;
	}
	return true;	
}

function roundEx(num, dec) 
{
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


function fieldToFloat( fieldId )
{
	var field = getFormField( fieldId );
	if( field == null )
		return "NaN";
	var fieldString = getCurrentFormFieldVal( field );
	fieldString = fieldString.replace( ",", "." );
	return Number( fieldString );
}

function fieldToInt( fieldId )
{
	var generalValue = fieldToFloat( fieldId );
	if( generalValue == null )
		return "NaN";
	
	return Math.round( generalValue );
}

function fieldToBool( fieldId )
{
	var field = getFormField( fieldId );
	if( field == null )
		return false;
		
	return getCurrentFormFieldVal( field );
}

function nan2zero( value )
{
	if( isNaN( value ))
		return 0;
	return value;
}

function float2Field( id, val, precision )
{
	setFormFieldValById( id, roundEx( val, precision ));
}

function int2Field( id, val )
{
	setFormFieldValById( id, Math.round( val ));
}

function bool2Field( id, val )
{
	if( val == false )
	{
		setFormFieldValById( id, "false" );
	}
	else
	{
		setFormFieldValById( id, "true" );
	}
}

function canFieldBeInitialized( id )
{
	if( nan2zero( fieldToFloat( id )) == 0 )
		return true;
	return false;
}

function isFieldEmpty( field )
{
	var text = getCurrentFormFieldVal( field );
	if( text == "" )
		return true;

	var i=0;
	for( i=0; i<text.length; i++ )
	{
		if( text[i] != ' ' && text[i] != '\t' )
			return false;
	}
	return true;
}

function isFieldEmptyById( id )
{
	return isFieldEmpty( getFormField( id ));
}

function FormChecker_checkProductName()
{
	var nameField = this.getField( 'productNameFormField' );
	if( nameField == null )
		return false;
		
	if( getCurrentFormFieldVal( nameField ).length < 1 )
	{
		alert( "Określ nazwę produktu" );
		return false;
	}
	return true;		
}

function FormChecker_checkProductSize()
{
	var x = fieldToFloat( 'productWidthFormField' );
	var y = fieldToFloat( 'productHeightFormField' );
	
	if( x < 1 || x > 2000 || y < 1 || y > 2000 || isNaN(x) || isNaN(y) )
	{
		alert( "Niepoprawny rozmiar użytku" );
		return false;
	}
	return true;
}

function FormChecker_checkProductCnt()
{
	var articlesCntField = this.getField( 'quatityFormField' );
	if( articlesCntField == null )
		return false;
		
	var cnt = parseInt( getCurrentFormFieldVal( articlesCntField ));
	if( cnt <= 0 || isNaN( cnt ) )
	{
		alert( "Niepoprawna liczba sztuk" );
		return false;
	}	
	return true;	
}	

function FormChecker_checkBackSeal()
{
	var backSealWholesCnt = nan2zero( fieldToInt( 'transpFieldsCntFormItem' ));
	var buttonsCnt = nan2zero( fieldToInt( 'buttonsCntFormItem' ));
	var transpColorsCnt = nan2zero( fieldToInt( 'transpFieldsColorsCntFormItem' ));
	var isSuperTransp = fieldToBool( 'superTranspWindowFormField' );
	var isMatTransp = fieldToBool( 'matTranspWindowFormField' );

	var minWholesCnt = buttonsCnt + transpColorsCnt + isSuperTransp + isMatTransp;
	
	if( backSealWholesCnt < minWholesCnt )
	{
		alert( "Liczba klawiszy i/lub kolorów okien sugeruje potrzebę wykonania co najmniej " + minWholesCnt + " otworów w kleju. Uzupełnij pole 'Liczba otworów w kleju pod okna'" );
		return false;
	}
	return true;
}

function FormChecker_checkElevation()
{	
	if( !this.checkColors() )
		return false;

	if( !this.checkEmboss() )
		return false;

	if( !this.checkBackSeal() )
		return false;

	if( !this.checkEmail() )	// elevation is not priced automaticly so the email is necessary
		return false;
		
	return true;
}


function FormChecker_checkRibbon()
{	
	if( !isFieldEmpty( this.getField( 'ribbonPathsCntFormField' )))
	{
		var pathsCnt = nan2zero( fieldToFloat( 'ribbonPathsCntFormField' ));
		if( pathsCnt == 0 )
		{
			alert( "Niepoprawna liczba ścieżek" );
			return false;
		}
		
		//todo check number of paths due to number of buttons
	}	
	
	
	var x = fieldToFloat( 'productWidthFormField' );
	var y = fieldToFloat( 'productHeightFormField' );

	if( isNaN(x) || isNaN(y) )
	{
		alert( "Fatal error 69037" );
		return false;
	}

	var xRib = fieldToFloat( 'ribbonConnectionWidthFormField' );
	var yRib = fieldToFloat( 'ribbonConnectionHeightFormField' );

	if( isNaN( xRib ) || isNaN( yRib ) || xRib < x || yRib < y )
	{
		alert( "Niepoprawny rozmiar produktu wraz z tasiemkš" );
		return false;
	}
		
	return true;	
}


function FormChecker_checkWireConnection()
{
	if( !isFieldEmpty( this.getField( 'wireConnectionWidthFormField' )))
	{
		var pathsCnt = nan2zero( fieldToFloat( 'wireConnectionWidthFormField' ));
		if( pathsCnt == 0 )
		{
			alert( "Niepoprawna liczba cieżek" );
			return false;
		}
		
		//todo check number of paths due to number of buttons
	}
}

function FormChecker_checkCustomConnection()
{
	if( isFieldEmpty( this.getField( 'customConnectionFormField' )))
	{
		alert( "Określ rodzaj wyprowadzenia" );
		return false;
	}
}

function FormChecker_checkCircuitLayersCntAccordingToKeyboard()
{
	var circuitLayersCntField = this.getField( FIELD_CIRCUIT_LAYERS_CNT );
	if( circuitLayersCntField == null )
		return false;
		
	var x = parseInt( getCurrentFormFieldVal( circuitLayersCntField ));
	
	if( x <= 0 || x > 5 || isNaN(x) )
	{
		alert( "Niepoprawna liczba warstw obwodu" );
		return false;
	}
	return true;
}

function FormChecker_checkColors()
{
	colorsCnt = fieldToInt( 'colorsCntFormItem' );
	if( isNaN( colorsCnt ) || colorsCnt == 0 )
	{
		alert( "Określ liczbę kolorów" );
		return false;
	}
	return true;
}


function FormChecker_checkEmboss()
{
	if( fieldToBool( 'embossFormItem' ))
	{
		var buttonsCnt = nan2zero( fieldToInt( 'buttonsCntFormItem' ));		
		var uniqueEmbossCnt = nan2zero( fieldToInt( 'embossTypesCntFormItem' ));
		
		if( uniqueEmbossCnt > buttonsCnt )
		{
			alert( "Liczba różnych średnic do przetłoczenia przekracza liczbę klawiszy" );
			return false;
		}
	}
	return true;	
}

function FormChecker_checkButtons()
{
	var buttonsCnt = nan2zero( fieldToInt( 'buttonsCntFormItem' ));
	if( buttonsCnt == 0 )
	{
		alert( "Określ liczbę klawiszy" );
		return false;
	}
	
	return this.checkEmboss();
}

function FormChecker_checkKeyboard()
{
	var isRibbonConnection = fieldToBool( 'ribbonConnectionFormField' );
	var isWiredConnection = fieldToBool( 'wireConnectionFormField' );
	if( !isRibbonConnection && !isWiredConnection && !fieldToBool( 'customConnectionFormField' ))
	{
		alert( "Określ rodzaj wyprowadzenia" );
		return false;
	}

	if( !this.checkColors() )
		return false;

	if( !this.checkButtons() )
		return false;

	if( !this.checkBackSeal() )
		return false;

	if( isRibbonConnection && !this.checkRibbon() )
		return false;
	else if( isWiredConnection && !this.checkWireConnection() )
		return false;
	else if( isCustomConnection && !this.checkCustomConnection() )
		return false;
	
	if( !this.checkEmail() )	// elevation is not priced automaticly so the email is necessary
		return false;
				
	return true;
}

function FormChecker_checkCircuitLayersCntAccordingToPCB()
{
	var circuitLayersCntField = this.getField( FIELD_CIRCUIT_LAYERS_CNT );
	if( circuitLayersCntField == null )
		return false;
		
	var x = parseInt( getCurrentFormFieldVal( circuitLayersCntField ));
	
	if( x <= 0 || x > 2 || isNaN(x) )
	{
		alert( "Niepoprawna liczba warstw obwodu" );
		return false;
	}
	if( x == 2 )
	{
		alert( "Obwód zostanie wykonany bez matalizacji" );
	}
	return true;	
}

function FormChecker_checkPCB()
{
	if( !this.checkCircuitLayersCntAccordingToPCB() )
		return false;
	
	return true;
}

function FormChecker_checkSMD()
{
	var x = fieldToFloat( FIELD_ARTICLE_SIZE_X );
	var y = fieldToFloat( FIELD_ARTICLE_SIZE_Y );

	assert( "x * y != 0 && !isNaN(x) && !isNaN(y)", this );

	if( x <= SMD_AUTO_PRICE_MAX_WIDTH && y <= SMD_AUTO_PRICE_MAX_WIDTH || x <= SMD_AUTO_PRICE_MAX_WIDTH && y <= SMD_AUTO_PRICE_MAX_WIDTH )
		return true;

	if( !this.checkEmail() )	// size of SMD exceeds limits for automatic price calculation
		return false;

	return true;
}

function FormChecker_checkSticker()
{
	if( !this.checkEmail() )	// sticker is not priced automaticly so the email is necessary
		return false;

	return true;
}

function FormChecker_checkOverprint()
{
	if( !this.checkEmail() )	// overprint is not priced automaticly so the email is necessary
		return false;

	return true;
}

function checkForm( form )
{
	var formChecker = new FormChecker( form );
	return formChecker.check();
}



