//var emailIsValid = false;

function checkFormDatas(formName){
	objForm = getFormObject(formName);
	
	// AddUserForm
	if(objForm.id == 'AddUser'){
		i_fn = chkStr(objForm.firstname.value, 'Vorname');
		i_ln = chkStr(objForm.lastname.value, 'Nachname');
		i_un = chkStr(objForm.userName.value, 'Benutzername');
		i_em = chkEmail(objForm, true);	
		errorController(objForm.firstname, i_fn);
		errorController(objForm.lastname, i_ln);
		errorController(objForm.userName, i_un);
		errorController(objForm.email, i_em);
		if(i_fn && i_ln && i_un){
			chkEmail(objForm);	
		}
	}
	
	// UpdateUserForm
	if(objForm.id == 'UpdateUser'){
		alert('UpdateUser');
	}
	
	// AddPartnerUser
	if(objForm.id == 'AddPartnerUser'){
		i_fn = chkStr(objForm.firstname.value, 'Vorname');
		i_ln = chkStr(objForm.lastname.value, 'Nachname');
		i_un = chkStr(objForm.userName.value, 'Benutzername');
		i_em = chkEmail(objForm, true);	
		errorController(objForm.firstname, i_fn);
		errorController(objForm.lastname, i_ln);
		errorController(objForm.userName, i_un);
		errorController(objForm.email, i_em);
		if(i_fn && i_ln && i_un){
			chkEmail(objForm);	
		}
	}
	
	// offer_form
	if(objForm.id == 'offer_form'){
		//alert(objForm.id);
		o_head = chkStr(objForm.offerName.value, '');
		o_desc = chkStr(objForm.offerDescription.value, '');
		errorController(objForm.offerName, o_head);
		errorController(objForm.offerDescription, o_desc);
		if(o_head && o_desc){
			tb_show('','/Loading/?height=150&width=350', null);		
			objForm.submit();
		}
	}
	
}

function getFormObject(formName){
	return document.forms[formName];
}

// check string
function chkStr(str, stdInput){
	return (str.length > 1 && str != '' && str !== stdInput) ? true : false;
}

// check email
function chkEmail(objForm, strCheckOnly){
	strCheckOnly = strCheckOnly ? true : false;
	reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)' + '(\\@)([a-zA-Z0-9\\-\\.]+)' + '(\\.)([a-zA-Z]{2,4})$');
   
  if(strCheckOnly){
  	return reg.test(objForm.email.value);
  }
   
  //alert(objForm.email.value);
  if(reg.test(objForm.email.value)){
  		jQuery.post('/HTTPRequest/checkIfEmailExists/' + Math.random(), {'emailString':objForm.email.value}, function(text){
																																																		//alert(text);
																																																		if(text == '0'){
																																																			errorController(objForm.email, true);
																																																			objForm.submit();
																																																		} else {
																																																			alert('Diese Emailadresse ist bereits registriert.\nJede Emailadresse darf nur ein mal im System vorhanden sein!');
																																																			errorController(objForm.email, false);
																																																		}
																																																	});
		} else {
			return false;	
		}
}

function errorController(objField, state){
	if(state){
		objField.className = 'xt_form';
	} else {
		objField.className = 'xt_form_error';
	}
}

function sleep(delay){
    var start = new Date().getTime();
    while (new Date().getTime() < start + delay);
}

function docFormControlle(){
	fileField = document.getElementById('docUserFile');
	if(fileField.value.length > 0){
		tb_show('','/Loading/?height=150&width=350', null);
		return true;
	} else {
		alert('Es wurde keine Datei ausgwählt');
		return false;
	}
}