Ext.onReady( function() {
	var widthofSels = 284;
	var abteilung = new Ext.form.ComboBox( {
	    typeAhead :true,
	    triggerAction :'all',
	    transform :'abteilung',
	    width :widthofSels,
	    forceSelection :false, 
	    hideTrigger :true
	});
	var land = new Ext.form.ComboBox( {
	    typeAhead :true,
	    triggerAction :'all',
	    transform :'land',
	    width :widthofSels,
	    forceSelection :false,
	    hideTrigger :true
	});
	var position = new Ext.form.ComboBox( {
	    typeAhead :true,
	    triggerAction :'all',
	    transform :'position',
	    width :widthofSels,
	    forceSelection :false,
	    hideTrigger :true
	});
	var sels = Ext.select(".x-form-arrow-trigger");
	/*
	 * if (sels){ sels.each(function( elem ){ if (!Ext.isIE){
	 * elem.dom.style.marginTop = "0px"; } }); }
	 */
	Ext.QuickTips.interceptTitles = true;
	Ext.QuickTips.init();
});
function addnewUpload(field, ausenrum) {
	if (Ext.getDom(ausenrum).childNodes.length < 3) {
		var n = Ext.getDom(ausenrum).firstChild.cloneNode(true);
		Ext.getDom(ausenrum).appendChild(n);
	}
	return;
}
function addnewBauteil(maxteile) {
	var elem = Ext.get('weiterebauteile').select('.einzelbauteile');
	var nextitemcount = elem.getCount();
	nextitemcount = nextitemcount + 1;
	if (maxteile < nextitemcount) {
	} else {
		var jetzt = new Date();
		var Zeit = jetzt.getTime();
		var url = "?addnewbauteil=1&bauteilnummer=" + nextitemcount + "&time=" + Zeit;
		Ext.Ajax.request( {
		    method :'get',
		    url :url,
		    success : function(req) {
			    Ext.DomHelper.insertHtml('beforeEnd', Ext.get('weiterebauteile').dom, req.responseText);
			    if (Ext.isIE) {
				    decode(req.responseText);
			    }
		    }
		});
	}
}
function gwpBlendIn(id, ela) {
	var el = Ext.get(id);
	if (id == 'rfq') {
		if (Ext.get(id).dom.style.position == 'absolute') {
			Ext.get(id).dom.style.position = 'relative';
			Ext.get(id).dom.style.top = '0px';
			Ext.get(id).dom.style.left = '0px';
			ela.className = "anfrformplusminus";
		} else {
			Ext.get(id).dom.style.position = 'absolute';
			Ext.get(id).dom.style.top = '-1000px';
			Ext.get(id).dom.style.left = '-1000px';
			ela.className = "anfrformplusminusminus";
		}
	} else {
		if (el) {
			if (el.dom.style.display == 'block') {
				el.slideOut('t', {
				    easing :'easeOut',
				    duration :.5,
				    remove :false,
				    useDisplay :true,
				    callback : function(f) {
					    el.dom.style.display = "none";
					    ela.className = "anfrformplusminusminus";
				    }
				});
			} else {
				el.slideIn('t', {
				    easing :'easeOut',
				    duration :.5,
				    remove :false,
				    useDisplay :true,
				    callback : function(f) {
					    el.dom.style.display = "block";
					    ela.className = "anfrformplusminus";
				    }
				});
			}
		}
	}
};
var fileReferences = [];
var filesToUpload = 0;
var filemsgBox = false;
var totalFileSize = 0;
var maxTotalFileSize = 10485760; // 10 MB!
var askForSend = false;
var filieuploadNames = new Object();
filieuploadNames['en'] = "select file(s)"; 

var selectFilesBtn = "Datei(en) auswählen"; 
function makeMyUploadButton(btn, nr) {

if (filieuploadNames[window.location.pathname.split("/")[1]]){
	selectFilesBtn  =filieuploadNames[window.location.pathname.split("/")[1]];
	
}
	

	var infFeld = Ext.get('fileinformation' + nr);
	var internNr = nr;
	var settings = {
	    flash_url :"/__/scripts/swfupload/swfupload.swf",
	    upload_url :"/fileupload/upload.php?UploadUniqueID=" + UploadUniqueID+"&bauteil="+nr, // URL
	    // anpassen
	    // auf
	    // irgendein
	    // part
	    // (DA
	    // CMS
	    // dafür
	    // WIRKLICH
	    // NICHT
	    // gebraucht
	    // wird
	    post_params : {
		    "PHPSESSID" :""
	    },
	    file_size_limit :"2 MB",
	    file_types :"*.*",
	    file_types_description :"All Files",
	    file_upload_limit :5,
	    file_queue_limit :5,
	    custom_settings : {
		    cancelButtonId :"btnCancel"
	    },
	    debug :false,
	    // Button settings
	    button_image_url :"/__/images/bg110.gif",
	    button_width :"152",
	    button_height :"23",
	    button_placeholder_id :btn,
	    button_text :'<span class="button1">'+selectFilesBtn+'</span>',
	    button_text_left_padding :6,
	    button_text_top_padding :4,
	    button_text_style :".button1 { display: block; margin-top: 5px; color: #FFFFFF;  font-size: 12px; line-height: 16px; font-family: Arial; background: #FF0000; }",
	    button_cursor :SWFUpload.CURSOR.HAND,
	    button_window_mode :SWFUpload.WINDOW_MODE.TRANSPARENT,
	    button_text_top_padding :0,
	    file_types_description :'alle Dateien',
	    file_queued_handler : function(file) {
		    totalFileSize = totalFileSize + file.size;
		    if (totalFileSize < maxTotalFileSize) {
			    infFeld.dom.innerHTML += "<li id='" + file.id + "_li' ><div id='" + file.id + "'>" + file.name + " " + "(" + (Math.ceil(file.size / 1024)) + " KB) <a href=\"#\" onClick=\"removeFromQue('" + file.id + "'); return false; \"><img src=\"/__/images/minus_klein.png\" border=\"0\"  alt=\"Datei entfernen\"></a></div></li>";
		    } else {
			    Ext.Msg.alert('Fehler', 'Alle Dateien dürfen Max. 10 MB betragen, Datei wird entfernt...');
			    removeFromQue(file.id);
		    }
		    filesToUpload = filesToUpload + 1;
	    },
	    file_queue_error_handler : function(file, error_code, message) {
		    switch (error_code) {
			    case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
				    Ext.Msg.alert('Fehler', 'Maximale Anzahl erreicht');
				    break;
			    case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
				    Ext.Msg.alert('Fehler', 'Datei zu groß');
				    break;
			    case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
				    Ext.Msg.alert('Fehler', 'Datei zu klein');
				    break;
			    case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
				    Ext.Msg.alert('Fehler', 'Datei nicht erlaubt');
				    break;
		    }
	    },
	    file_dialog_complete_handler : function(numFilesSelected, numFilesQueued) {
	    },
	    upload_start_handler : function(file) {
	    },
	    upload_progress_handler : function(file, bytesLoaded, bytesTotal) {
		    var p = bytesLoaded / bytesTotal;
		    var pt = Math.floor(bytesLoaded / bytesTotal * 100);
		    Ext.get(file.id).dom.innerHTML = '<img src="/_/pics/formwait.gif">';
		    Ext.MessageBox.show( {
		        title :'Upload...',
		        msg :'Initializing...',
		        width :240,
		        progress :true,
		        closable :false,
		        modal: false 
		    });
		    Ext.MessageBox.updateProgress(p, file.name + ' ' + pt + '%');
	    },
	    upload_error_handler : function(file, error_code, message) {
		    switch (error_code) {
			    case SWFUpload.UPLOAD_ERROR.HTTP_ERROR:
				    break;
			    case SWFUpload.UPLOAD_ERROR.MISSING_UPLOAD_URL:
				    Ext.Msg.alert("Fehler", "Configuration Error");
				    break;
			    case SWFUpload.UPLOAD_ERROR.UPLOAD_FAILED:
				    Ext.Msg.alert("Fehler", "Upload Failed.");
				    break;
			    case SWFUpload.UPLOAD_ERROR.IO_ERROR:
				    Ext.Msg.alert("Fehler", "Server (IO) Error");
				    break;
			    case SWFUpload.UPLOAD_ERROR.SECURITY_ERROR:
				    Ext.Msg.alert("Fehler", "Security Error");
				    break;
			    case SWFUpload.UPLOAD_ERROR.UPLOAD_LIMIT_EXCEEDED:
				    Ext.Msg.alert("Fehler", "Upload limit exceeded.");
				    break;
			    case SWFUpload.UPLOAD_ERROR.SPECIFIED_FILE_ID_NOT_FOUND:
				    Ext.Msg.alert("Fehler", "File not found.");
				    break;
			    case SWFUpload.UPLOAD_ERROR.FILE_VALIDATION_FAILED:
				    Ext.Msg.alert("Fehler", "Failed Validation. Upload skipped.");
				    break;
			    case SWFUpload.UPLOAD_ERROR.FILE_CANCELLED:
				    break;
			    case SWFUpload.UPLOAD_ERROR.UPLOAD_STOPPED:
				    Ext.Msg.alert("Fehler", "Stopped");
				    break;
			    default:
				    Ext.Msg.alert("Fehler", "Unhandled Error: " + error_code);
				    break;
		    }
		    Ext.get('hauptcontainer').unmask();
	    },
	    upload_success_handler : function(file, serverData) {
	    },
	    upload_complete_handler : function(file) {
		    filesToUpload = filesToUpload - 1;
		    Ext.get(file.id).dom.innerHTML = '<img src="/_/pics/yes.gif">';
		    if (filesToUpload == 0) {
			    Ext.MessageBox.hide();
			    cmsForms.anfrageformular.un('submit', processFileUploads);
			    Ext.getDom('anfrageformular').submit();
		    }
	    },
	    queue_complete_handler : function(numFilesUploaded) {
	    } // Queue plugin event
	};
	fileReferences.push(new SWFUpload(settings));
};
runOnLoad( function() {
	
	cmsForms.anfrageformular.on('submit', processFileUploads);
});

function removeFromQue(fileLk) {
	fileReferences.each( function(frd) {
		frdId = frd.getFile(fileLk);
		if (frdId.id == fileLk) {
			filesToUpload = filesToUpload - 1;
			totalFileSize = totalFileSize - frdId.size;
			frd.cancelUpload(frdId.id);
			if (Ext.get(fileLk + '_li')) {
				Ext.get(fileLk + '_li').remove();
			}
		}
	});
}
function processFileUploads() {
		
	Ext.MessageBox.show( {
	    title :'Absenden?',
	    msg :'Möchten Sie die Daten jetzt senden?',
	    modal: false,
	    buttons :Ext.MessageBox.YESNO,
	    fn : function(res) {
		    if (res == "yes") {
		
		    	
		    	var fireUpload = false;
		    	fileReferences.each( function(fr) {
		    		if (fr.getStats().files_queued > 0) {
		    			fr.startUpload();
		    			fireUpload = true;
		    		}
		    	});
		    	if (fireUpload == false) {
		    		// keine Datei zum Uploaden, weg damit...
		    		cmsForms.anfrageformular.un('submit', processFileUploads);
		    		var submtrue = cmsForms.anfrageformular.fireEvent('submit');
		    		if (submtrue){
		    			cmsForms.anfrageformular.el.dom.submit();
		    		}
		    	} else {
		    		return false;
		    	}
		    	
		    } else {
		    	return false; 
		    	
		    } 
	    }
	});
	
return false; 
}
