
var BF_TYPE_PUBLIC = 1;				// 00000001
var BF_TYPE_SYSTEM = 2;				// 00000010
var BF_TYPE_FOR_SIGNING = 4;		// 00000100
var BF_TYPE_FOR_HASHING = 8;		// 00001000
var BF_TYPE_VALUE_ENC_BASE_64 = 16;	// 00010000
var BF_TYPE_NAME_ENC_BASE_64 = 32;	// 00100000
var BF_TYPE_UNNAMED = 64;			// 01000000

/**
 * @desc JustaWebSignObject extends JustaAppletObject adding functionality
 *		to sign user data using user certificate.
 **/
function JustaWebSignObject() {
	this.setCodeBase( "https://justa.ssc.lt/" );
	this.init();
}

JustaWebSignObject.prototype = new JustaAppletObject();
JustaWebSignObject.prototype.constructor = JustaWebSignObject;
JustaWebSignObject.superclass = JustaAppletObject.prototype;

JustaWebSignObject.prototype.init = function () {
	JustaWebSignObject.superclass.init.call( this
	 , "res/Justa_Wizard_v2.2.6.jar,"
	  + "res/Justa_Utils_v1.5.1.jar,"
	  + "res/Justa_Security_v1.3.4.jar,"
	  + "res/Justa_MSCAPI_v1.6.8.jar,"
	  + "res/Justa_JSDC_v1.0.4.jar,"
	  + "web/sign/res/Justa_WEB-Sign_v1.5.0.jar"
	 , "lt.ssc.justa.web.sign.SignApplet" );
}

JustaWebSignObject.prototype.SIGN_SUCCESSFULL = "sign_successfull";
JustaWebSignObject.prototype.SIGN_FAILED = "sign_failed";

JustaWebSignObject.prototype.dataToSign = null;

JustaWebSignObject.prototype.initFieldsToSign = function ( ) {
	this.dataToSign = new DataToSign();
}

JustaWebSignObject.prototype.getFieldsToSign = function () {

	if( null == this.dataToSign
	 || !( this.dataToSign instanceof DataToSign ) 
	 || this.dataToSign.size() < 1 ) {
		alert( 'There is no data provided to sign!' );
		this.setCallResult( "false" );
		return;
	}
	var helper = document.getElementById( this.ID );
	var fields = this.dataToSign.getFields();

	for( var i=0; i<fields.length; i++ ) {
		if(fields[i].getType() == this.dataToSign.FIELD_TYPE_TEXT) {
			helper.addText( fields[i].getName(), fields[i].getValue()
			 , (BF_TYPE_PUBLIC | BF_TYPE_FOR_SIGNING | BF_TYPE_VALUE_ENC_BASE_64).toString() );
		} 
		else if(fields[i].getType() == this.dataToSign.FIELD_TYPE_FILE) {
			helper.addFile( fields[i].getValue() );
		}
	}
	this.dataToSign = null;
	this.setCallResult( "true" );
	return;
}

JustaWebSignObject.prototype.appletLoaded = function () {

}

JustaWebSignObject.prototype.putSignResult = function () {
	var signResult = this.getCallResult();
	alert( signResult );
}

JustaWebSignObject.prototype.endSign = function ( reason ) {
	alert( reason );
}


// ============================= //
/**
 * @desc Object representing one field to be signed pair
 *	
 **/
function FieldToSign( t, n, v ) {
	this.type = t;
	this.name = n;
	this.value = v;
}

FieldToSign.prototype.includeFileBody = false;
FieldToSign.prototype.getType = function () { return this.type };
FieldToSign.prototype.getName = function () { return this.name };
FieldToSign.prototype.getValue = function () { return this.value };

// ============================= //
/**
 * @desc Object representing collection of fields to be signed
 *		
 **/
function DataToSign() {
	this.fields = new Array();
}

DataToSign.prototype.FIELD_TYPE_TEXT = "text";
DataToSign.prototype.FIELD_TYPE_FILE = "file";

DataToSign.prototype.fields = null;

DataToSign.prototype.addField = function ( t, n, v ) { 
	this.fields[ this.fields.length ] 
	  = new FieldToSign( t, n, v );
}

DataToSign.prototype.addTextField = function ( n, v ) { 
	this.addField( this.FIELD_TYPE_TEXT, n, v ); 
}

DataToSign.prototype.addFileField = function ( n, v, incBody ) { 
	this.fields[ this.fields.length ] 
	 = new FieldToSign( this.FIELD_TYPE_FILE, n, v );
	this.fields[ this.fields.length-1]
	 .includeFileBody = ( true == incBody ) ? "true" : "false";
}

DataToSign.prototype.getFields = function () { 
	return this.fields;
}

DataToSign.prototype.size = function () { 
	return this.fields.length;
}
