var FiConWddx = new function(){

	this.uID;
	this.oFiConWddxCFC = new FiConWddxCFC();

	this.DisplayFields = function( uID, cFormName, cFieldPrefix ){
	
		if ( cFormName == null )
			cFormName = "D_Display";
		if ( cFieldPrefix == null )
			cFieldPrefix = "D_";
 			
		var Divs = document[ cFormName ].getElementsByTagName( "span" );
		var aDivNames = new Array();
		var n = 0;
		var cDivName; 
		var elem;
				
		for ( var i = 0; i < Divs.length; i++ ){
			cDivName = Divs[ i ].id;
			if ( cDivName.substring( 0, 2 ) == cFieldPrefix ){
				aDivNames[ n++ ] = cDivName;
			}
		}

		var sFields = this.oFiConWddxCFC.GetDisplayFields( uID, aDivNames );
		for ( var cFieldName in sFields ){
			elem = document.getElementById( cFieldName );
			elem.innerHTML = this.Leading0( sFields[ cFieldName ] );
			elem.parentNode.style.display = "block";
		}
		
	}

	this.Leading0 = function( Val ){

		var cVal = String( Val );

		if ( cVal.substring( 0, 2 ) == "@0" ){
			cVal = cVal.substring( 1 );
			//alert( cVal );
		}

		return cVal;

	}	
	
	this.Init	=	function( uID ){

		this.LoadFormVAR();
		this.uID = uID;

		var sFields = this.oFiConWddxCFC.GetData( this.uID );
		var	elem;
		
		if ( sFields.nMerknummer_CAR_AT != null ){
			AutoTelex.RequestYears( sFields.nMerknummer_CAR_AT );
			AutoTelex.RequestModel( sFields.nMerknummer_CAR_AT, sFields.nYear_CAR_AT );
			AutoTelex.RequestUitv( sFields.nModelID_CAR_AT, sFields.nYear_CAR_AT );
		}

		for ( var FieldName in sFields ){
		
			elem = document.getElementById( FieldName );

			if ( elem != null ){

				switch ( elem.type ){
				
					case "text":
						elem.value = this.Leading0( sFields[ FieldName ] );
						break;
						
					case "select-one":
						for ( var i=0; i < elem.options.length; i++ ){
							if (elem.options[i].value == sFields[ FieldName ] ) {
								elem.selectedIndex = i;
							}
						}
						break;
				
					case "checkbox":
						elem.checked = parseInt( sFields[ FieldName ] );
						break;
				
					case "hidden":
						elem.checked = parseInt( sFields[ FieldName ] );
						break;
				
					default:
						//alert( elem.type + " " + FieldName + "=" + sFields[ FieldName ] );
				}
			}	
		}
		this.InitURLVARS();
	} // 	Init

	this.GetUrlVars = function(){
	
    var vars = [], hash, FieldName;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

    for(var i = 0; i < hashes.length; i++){
			hash = hashes[i].split('=');
			FieldName = hash[0].toUpperCase();
			vars.push( FieldName );
			vars[ FieldName ] = unescape( hash[ 1 ] );
    }
    return vars;
	}	

	this.Queryizer = function( JSONQuery ){

		var Vars = new Object();
		Vars.Cols = new Object();
		Vars.RecordCount = JSONQuery.DATA.length;
		Vars.ColumnCount = JSONQuery.COLUMNS.length;
		Vars.CurrentRow = 0;
		Vars.JSONQuery = JSONQuery;

		for ( i = 0; i < Vars.ColumnCount; i++ ){
			Vars.Cols[ Vars.JSONQuery.COLUMNS[ i ] ] = i;
		}
		this.RecordCount = function(){
			return Vars.RecordCount;
		}
		this.ColumnCount = function(){
			return Vars.ColumnCount;
		}
		this.GetField = function( cCol ){
			return Vars.JSONQuery.DATA[ Vars.CurrentRow ][ Vars.Cols[ cCol ] ];
		}
		this.Next = function(){
			Vars.CurrentRow++;
		}
		this.EndOfQuery = function(){
			return Vars.CurrentRow >= Vars.RecordCount;
		}

	}

	this.InitURLVARS = function(){
	
		var UrlVars = this.GetUrlVars();
		var cVT = UrlVars[ "CVT" ];
		var cFieldName;
		var elem;
		var bFieldsFound = false;
		
		if( cVT != null ){
	
			cVT = cVT.toUpperCase();
			var jsonFields = this.oFiConWddxCFC.GetURLFields( cVT );
//alert( dump( jsonFields ) );
			var qFields = new this.Queryizer( jsonFields );
			while ( !qFields.EndOfQuery() ){
				cFieldName = qFields.GetField( "FCURLFIELDNAME" );
				if ( UrlVars[ cFieldName ] != null ){
					elem = document.getElementById( qFields.GetField( "FCFORMFIELDNAME" ) );
					if ( elem != null ){
						bFieldsFound = true;
						elem.value = UrlVars[ cFieldName ];
					}	
				}
				qFields.Next();
			}
			if ( bFieldsFound )
				this[ cVT ] = UrlVars;
		}
	}

	this.SetStep = function( nStep ){
		this.oFiConWddxCFC.SetStep( this.uID, nStep );
	}

	this.SaveForm = function( FormName ){
		this.oFiConWddxCFC.setForm( FormName );
		this.uID = this.oFiConWddxCFC.SaveForm( this.uID, this.GetCheckBoxNames( FormName ) );
//		alert( FormName + " Saved ( " + this.uID + " )" );
	}

	this.GetCheckBoxNames = function( FormName ){
		var chb = "";
		with ( document[ FormName ] ) {
			for( i = 0; i < elements.length; i++ ){
				if( elements[ i ].type == "checkbox" ){
					if ( chb != "" )
						chb += ",";
					chb += elements[ i ].name;
				}
			}
		}
		return chb;
	}

	this.SaveContract = function(){
		this.oFiConWddxCFC.SaveContract( this.uID );
		alert( "Contract Saved ( " + this.uID + " )" );
	}

	this.LoadFormVAR = function(){
//		alert( document[ "formVAR" ] == null );
	}
	
} // function FiConWddx()
/*
function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects 
		for(var item in arr) {
			var value = arr[item];
			
			if(typeof(value) == 'object') { //If it is an array,
				dumped_text += level_padding + "'" + item + "' ...\n";
				dumped_text += dump(value,level+1);
			} else {
				dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
			}
		}
	} else { //Stings/Chars/Numbers etc.
		dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
}
*/

