var finalSubmit = "";

function submitForm(paramFormId)
{
	var fid = (paramFormId != "") ? paramFormId : formId;
	
	var f = $("#"+fid);
	var paramString = "";
//	f.find("input[@checked], input[@type='text'], input[@type='hidden'], input[@type='password'], input[@type='file'], input[@type='submit'], option[@selected], textarea")
	//f.find("input[type='checkbox'][checked], input[type='text'], input[type='hidden'], input[type='password'], input[type='file'], input[type='submit'], option[selected], textarea")
	
	//f.find("input, select, textarea")
	//f.find("input[type='radio'][checked], input[type='checkbox'][checked], input[type='text'], input[type='hidden'], input[type='password'], input[type='file'], input[type='submit'], select[selected], textarea")
	f.find("input[type='radio'][checked], input[type='checkbox'][checked], input[type='text'], input[type='hidden'], input[type='password'], input[type='file'], input[type='submit'], select, textarea")
	.each(function()
	{
		paramString += this.name || this.id || this.parentNode.name || this.parentNode.id;
		paramString += "=" + proofValue(this.value) +"&";
	});

	sendRequest(paramString);
	
	return false;
}

function proofValue(val)
{
	val = val.replace("&", "_und_");
	val = val.replace("?", "_frag_");
	
	return val;
}



function sendRequest(paramString)
{
	$('.errorHint').remove();
	$('#errorsDiv').hide();
	
	var err = false;
	
	$.ajax({
		type: "POST",
		url: validationURL,
		data: paramString,
		success: function(responseText)
		{
			responseArray = responseText.split("|");
			if(responseArray[1] == "ok")
			{
				cookieSave();
				$('#'+formId).submit();
				err = true;
			}
			else
			{
				if(responseArray[0] != "" && responseArray[0] != "<ul></ul>")
				{
					window.scrollTo(0, 0);
					$('#errorsDiv').html(responseArray[0]).show();
				}
				
				if(responseArray.length > 1)
				{
					selectivesArray = responseArray[1].split("@@");				

					for(i=0; i<selectivesArray.length; i++)
					{
						var arr = selectivesArray[i].split("=");
						if(arr[0] != "" && $('#'+arr[0]).length > 0)
						{
							var code = '<div class="errorHint"><div class="hintText">'+ arr[1] +'</div></div>'; 
							$('#'+arr[0]).after(code);
						}
					}
				}
				
			}
			
			if(responseText.indexOf("showTripleOpt") != -1)
			{
				if($('#divTripleOpt').length > 0)
				{
					document.getElementById('divTripleOpt').style.border="1px dashed red";
					document.getElementById('divTripleOpt').style.borderBottom="0";
					document.getElementById('divTripleOptMailChk').style.border="1px dashed red";
					document.getElementById('divTripleOptMailChk').style.borderTop="0";
				}
			}
			else
			{
				if($('#divTripleOpt').length > 0)
				{
					document.getElementById('divTripleOpt').style.border="1px dashed #ccc";
					document.getElementById('divTripleOpt').style.borderBottom="0";
					document.getElementById('divTripleOptMailChk').style.border="1px dashed #ccc";
					document.getElementById('divTripleOptMailChk').style.borderTop="0";
				}
			}
		}
	});		
	
	//return err;
}

function cookieSave()
{
	if(formId == 'Login')
	{
		if($('#save_pwd').attr('checked'))
		{
			$.cookie('username', $('#username').val(), { expires: 30 });
			$.cookie('pwd', $('#pwd').val(), { expires: 30 });
		}
		else
		{
			$.cookie('username', '', { expires: -1 });
			$.cookie('pwd', '', { expires: -1 });
		}
	}
}

/**
 * FormHints: von users functions hier her, weil es alle formulare benutzen könn(t)en
 */
//'slow'
function showHint(field)
{
	$('.errorHint').remove();
	$('#errorsDiv').hide();	
	$('.inputHint').hide();
	$('#hint_'+field).show('normal');
}

function hideHint(field)
{
	$('.inputHint').hide('normal');
}


function reloader()
{
	location.reload(true);
}


$(document).ready(function()
{
	$('#captcha').remove();
	$('.inputHint').wrapInner("<div class='hintText'></div>");
	
	if(formId == 'Login')
	{
		$('#username').val( $.cookie('username') );
		$('#pwd').val( $.cookie('pwd') );
	}
	
	if( $('#captcha-id').length > 0)
	{
		window.setTimeout("reloader()", 300000);
		
		$("#divCaptcha").prepend('<div class="captchaTxt">Grafische Sicherheitsprüfung:<br />'
				+'Bitte gib die 6 Zeichen, die in der nebenstehenden Grafik dargestellt sind, in das Textfeld.');
		
		//$("#captcha-id").before('<div class="captchaTxt">Grafische Sicherheitsprüfung:<br />'
//				+'Bitte geben Sie die 6 Zeichen, die in der nebenstehenden Grafik dargestellt sind, in das Textfeld.');
		$("#divCaptcha").append('</div><div class="clearer"></div>');
		
	}
	
});


var datepickerOptions = {
		dateFormat: 'dd.mm.yy', 
		dayNamesShort:['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
		dayNamesMin:['So', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa'],
		dayNames:['Sonntag', 'Montag', 'Dienstag', 'Mittwoch', 'Donnerstag', 'Freitag', 'Samstag'],
		monthNames:['Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember']
    };

$(function() {
	$("#calendar_date").datepicker(datepickerOptions);
	$("#alt_calendar_date").datepicker(datepickerOptions);
});


