class cFormular
{
//----------------------------------------------
// Vars
//----------------------------------------------
private var ScriptPath:String;
private var ItemName:Array;
private var ItemValue:Array;
private var Enter:Array;
private var Tab:Array;
private var InfoText:TextField;
public var checkEmail:Boolean;
//----------------------------------------------
// Constructor
//----------------------------------------------
public function cFormular()
{
if(arguments[0].toString()!=undefined){
this.ScriptPath = arguments[0].toString();
} else {
this.ScriptPath = "formular.php";
}
this.ItemName = new Array();
this.ItemValue = new Array();
this.Enter = new Array();
this.Tab = new Array();
this.checkEmail = true;
}
//----------------------------------------------
// Set Path to php script
//----------------------------------------------
public function setScriptPath(ScriptPath:String):Void
{
this.ScriptPath = ScriptPath;
}
//----------------------------------------------
// add new Item
//----------------------------------------------
public function addItem(Name:String, Value:TextField, Enter:Boolean, Tab:Number):Void
{
this.ItemName.push(Name);
this.ItemValue.push(Value);
this.Enter.push(Enter);
this.Tab.push(Tab);
this.allocateTab();
}
//----------------------------------------------
// allocate Tabindex
//----------------------------------------------
private function allocateTab():Void
{
for(var i=0;i<this.ItemName.length;i++){
if(this.Tab[i]!=null){
this.ItemValue[i].tabEnabled = true;
this.ItemValue[i].tabIndex = this.Tab[i];
this.selectItem(1);
}
}
}
//----------------------------------------------
// Selection.setFocus
//----------------------------------------------
public function selectItem(Tab:Number):Void
{
for(var i=0;i<this.ItemName.length;i++){
if(this.Tab[i]!=null){
if(this.Tab[i]==Tab) Selection.setFocus(this.ItemValue[i]);
}
}
}
//----------------------------------------------
// add InfoTextField
//----------------------------------------------
public function addInfoTextField(InfoText:TextField):Void
{
this.InfoText = InfoText;
}
//----------------------------------------------
// send formular
//----------------------------------------------
public function send(To:String, Subject:String):Void
{
if(!this.check()) return;
var Send:LoadVars = new LoadVars();
var Load:LoadVars = new LoadVars();
Send.To = escape(To);
Send.Subject = escape(Subject);
if(arguments[2].toString()!=undefined) Send.Caption = escape(arguments[2].toString());
else Send.Caption = escape(Subject);
for(var i=0;i<this.ItemName.length;i++){
Send["name"+i] = escape(this.ItemName[i]);
Send["value"+i] = escape(this.ItemValue[i].text);
}
var scope = this;
Load.onLoad = Delegate.createPacked(this, onLoad, Load);
Send.sendAndLoad(this.ScriptPath, Load, "_POST");
if(this.InfoText!=undefined) this.InfoText.text = "Daten werden gesendet!";
}
//----------------------------------------------
// check all Items and start error event
//----------------------------------------------
private function check():Boolean
{
var errorCode:Number = 0;
var errorEmail:Number = 0;
for(var i=0;i<this.ItemName.length;i++){
// all fields filled?
if(this.Enter[i]){
if(this.ItemValue[i].text==""){
errorCode = 101;
}
}
// email correct?
if(this.checkEmail){
if(this.ItemName[i].toString().toUpperCase()=="EMAIL"){
if(this.ItemValue[i].text.indexOf('@')<3||this.ItemValue[i].text.indexOf('.')<6){
errorEmail = 102;
}
}
}
}
if(errorCode!=0){
this.onError(errorCode);
return false;
} else if(errorEmail!=0){
this.onError(errorEmail);
return false;
}
return true;
}
//----------------------------------------------
// onLoad Event
//----------------------------------------------
public function clearItems():Void
{
for(var i=0;i<this.ItemName.length;i++){
this.ItemValue[i].text = "";
}
}
//----------------------------------------------
// onLoad Event
//----------------------------------------------
public function onLoad(evt):Void
{
if(evt.back){
this.clearItems();
this.selectItem(1);
this.InfoText.text = "Daten wurden erfolgreich gesendet";
} else this.InfoText.text = "Daten konnten nicht gesendet werden";
}
//----------------------------------------------
// error event
//----------------------------------------------
public function onError(target:Number):Void
{
if(this.InfoText!=undefined){
switch(target){
case 101:
this.InfoText.text = "Sie haben nicht alle felder Ausgefüllt!";
break;
case 102:
this.InfoText.text = "Sie müssen eine gültige Email eingeben!";
break;
}
}
}
/*###################################################
# error Code
# 101 = fields not filled
# 102 = invalid email address
###################################################*/
}