
var searching = false;
function searchArticles(){
	if( searching ){ return; }
	var o = document.getElementById( "suche" );
	if( !o ){ throw "[jscript::searchArticles] missing input field"; }
	if( o.value == "" ){ return; }
	$.ajax({
		type: "POST",
		url: "ctrl.php",
		dataType: "json",
		data: { c : "search", term : o.value },
		async: true,
		error: function( request ){
			throw "sending request to server failed";
			//alert( request.responseText );
		},
		success: function( reply, status, request ){
			if( reply ){
				if( reply.error ){
					if( reply.errFields ){ for( var i=0; i<reply.errFields.length; i++ ){ $( "#" + reply.errFields[i] ).addClass( "input_error" ); } }
					err_alert( reply.frameTitle, reply.errmsg );
				}else{
					location.href = "index.php?c=suche";
				}
			}else{
				//alert( request.responseText );
			}
		}
	});
}



function registerUser( btn ){
	if( btn.clicked ){ return; }
	btn.clicked = true;
	if( !btn.oldLabel ){ btn.oldLabel = btn.innerHTML; }
	if( !btn.oldTextDecoration ){ btn.oldTextDecoration = btn.style.textDecoration; }
	btn.innerHTML = "bitte warten...";
	btn.style.textDecoration = "blink";

	$( "select,input,textarea" ).removeClass( "input_error" );
	var data = { c : "register" };
	$( "select,input,textarea" ).each(function(){ data[this.name] = $( this ).val(); });
	var cb = document.getElementById( "agb_accept" );
	data.agb_accept = "no";
	if( cb ){ data.agb_accept = (cb.checked===true) ? "yes" : "no"; }

	$.ajax({
		type: "POST",
		url: "ctrl.php",
		dataType: "json",
		data: data,
		async: true,
		error: function( request ){
			btn.innerHTML = btn.oldLabel;
			btn.style.textDecoration = btn.oldTextDecoration;
			btn.clicked = false;
			throw "sending request to server failed";
			//alert( request.responseText );
		},
		success: function( reply, status, request ){
			btn.innerHTML = btn.oldLabel;
			btn.style.textDecoration = btn.oldTextDecoration;
			btn.clicked = false;
			if( reply ){
				if( reply.error ){
					if( reply.errFields ){ for( var i=0; i<reply.errFields.length; i++ ){ $( "#" + reply.errFields[i] ).addClass( "input_error" ); } }
					err_alert( reply.frameTitle, reply.errmsg );
				}else{
					location.href = "index.php?c=registrierungserfolg";
				}
			}else{
				//alert( request.responseText );
			}
		}
	});
}


function resetPwd( btn ){
	if( !btn ){ btn = document.getElementById( "btn_login" ); }
	if( !btn ){ throw "[jscript::alogin] missing login button"; }
	if( btn.clicked ){ return; }
	btn.clicked = true;
	if( !btn.oldLabel ){ btn.oldLabel = btn.innerHTML; }
	if( !btn.oldTextDecoration ){ btn.oldTextDecoration = btn.style.textDecoration; }
	btn.innerHTML = "bitte warten...";
	btn.style.textDecoration = "blink";

	$( "select,input,textarea" ).removeClass( "input_error" );
	var data = { c : "reset_pwd", email : $("#email").val() };
	if( data.username == "" ){
		btn.innerHTML = btn.oldLabel;
		btn.style.textDecoration = btn.oldTextDecoration;
		btn.clicked = false;
		$( "#email" ).addClass( "input_error" );
		err_alert( "FEHLER", "Bitte geben Sie Ihren Benutzernamen an." );
		return;
	}
	$.ajax({
		type: "POST",
		url: "ctrl.php",
		dataType: "json",
		data: data,
		async: true,
		error: function( request ){
			btn.innerHTML = btn.oldLabel;
			btn.style.textDecoration = btn.oldTextDecoration;
			btn.clicked = false;
			throw "sending request to server failed";
			//alert( request.responseText );
		},
		success: function( reply, status, request ){
			btn.innerHTML = btn.oldLabel;
			btn.style.textDecoration = btn.oldTextDecoration;
			btn.clicked = false;
			if( reply ){
				if( reply.error ){
					if( reply.errFields ){
						for( var i=0; i<reply.errFields.length; i++ ){ $( "#" + reply.errFields[i] ).addClass( "input_error" ); }
					}
					err_alert( reply.frameTitle, reply.errmsg );
				}else{
					location.href = "index.php?c=kennwort_reset_erfolg";
				}
			}else{
				//alert( request.responseText );
			}
		}
	});
}




function alogin( btn ){
	if( !btn ){ btn = document.getElementById( "btn_login" ); }
	if( !btn ){ throw "[jscript::alogin] missing login button"; }
	if( btn.clicked ){ return; }
	btn.clicked = true;
	//$( btn ).hide();

	$( "select,input,textarea" ).removeClass( "input_error" );
	var data = { c : "login", username : $("#username").val(), password : $("#password").val() };

	if( data.username == "" && data.password == "" ){
		$( btn ).show();
		btn.clicked = false;
		return;
	}

	$.ajax({
		type: "POST",
		url: "ctrl.php",
		dataType: "json",
		data: data,
		async: true,
		error: function( request ){
			$( btn ).show();
			btn.clicked = false;
//			alert( request.responseText );
			throw "sending request to server failed";
		},
		success: function( reply, status, request ){
			$( btn ).show();
			btn.clicked = false;
			if( reply ){
				if( reply.error ){
					/*if( reply.errFields ){
						for( var i=0; i<reply.errFields.length; i++ ){ $( "#" + reply.errFields[i] ).addClass( "input_error" ); }
					}*/
					err_alert( reply.frameTitle, reply.errmsg );
				}else{
					location.href = "index.php?c=bereiche";
				}
			}else{
				//alert( request.responseText );
			}
		}
	});
}



var scriptData = [];
$(function(){
	$( "#suche" ).keypress(function(e){ if( Acu.detectEnterKeyPressed(e) ){ searchArticles(); } });
	$( "#password" ).keypress(function(e){ if( Acu.detectEnterKeyPressed(e) ){ alogin( null ); } });
	$( "#agb_accept" ).click(function(e){
		if( this.checked === true ){
			$( this ).addClass( "form_checkcont_off" ).removeClass( "form_checkcont_on" );
			this.checked = false;
		}else{
			$( this ).addClass( "form_checkcont_on" ).removeClass( "form_checkcont_off" );
			this.checked = true;
		}
	});
	$( "select,input,textarea" ).focus(function(e){ $( this ).removeClass( "input_error" ); });

	if( $("#alert").length > 0 ){
		$( "#alert" ).draggable({ handle: "#alert_handle" });
		$( "#alert_desc" ).disableSelection();
	}
	for( var i=0; i<scriptData.length; i++ ){
		var data = scriptData[i];
		var o = document.getElementById( data.target );
		if( !o ){ throw "[jscript::init] unable to connect data to DOM: no element found with id " + data.target; }
		o.data = data;
	}

	$( ".color" ).blur(function(e){
		var hexcode = this.value;
		if( hexcode && hexcode != "" ){
			if( hexcode.charAt(0) == "#" ){ hexcode = hexcode.substr( 1 ); }
			hexcode = hexcode.toLowerCase();
			if( hexcode.match(/^[a-f0-9]{6}$/) ){
				$( this ).removeClass( "input_error" );
				this.value = "#" + hexcode;
				$( ".farbfleck", this.parentNode ).css( "background-color", this.value );
			}else{
				$( this ).addClass( "input_error" );
			}
		}
	});

	$( "#agb_link" ).click(function(e){
		window.open('agbpop.php','agb','width=718,height=600,menubar=no,status=no,location=no,toolbar=no,scrollbars=yes');
		return false;
	});
});




function err_alert( title, msg ){
	if( $( "#alert" ).length > 0 ){
		showAlert( {title:title, text:msg, type:"info"} );
	}else{
		alert( msg );
	}
}

var alertDefaultOptions = {
	text			: "",
	title			: "",
	type			: "info",
	onClose		: null,
	onConfirm	: null,
	onCancel		: null
};
var alertOptions = {};
for( var p in alertDefaultOptions ){ alertOptions[p] = alertDefaultOptions[p]; }
function confirmAlert(){
	if( alertOptions.onConfirm ){ alertOptions.onConfirm(); }
	$( "#alert" ).hide();
}
function cancelAlert(){
	if( alertOptions.onCancel ){ alertOptions.onCancel(); }
	$( "#alert" ).hide();
}
function closeAlert(){
	if( alertOptions.onClose ){ alertOptions.onClose(); }
	$( "#alert" ).hide();
}
function showAlert( options ){
	for( var p in alertDefaultOptions ){
		if( typeof options[p] == "undefined" ){
			alertOptions[p] = alertDefaultOptions[p];
		}else{
			alertOptions[p] = options[p];
		}
	}
	$( "#alert_desc" ).html( alertOptions.title );
	$( "#alert_content" ).html( alertOptions.text );

	if( alertOptions.type == "confirm" ){
		$( "#btn_cancel_alert" ).show();
	}else{
		$( "#btn_cancel_alert" ).hide();
	}

	$( "#alert" ).center().show();
}



function mergeOptions( dflt, custom ){
	var opts = {};
	for( var p in dflt ){ opts[p] = dflt[p]; }
	if( custom ){
		for( var p in custom ){ opts[p] = custom[p]; }
	}
	return opts;
};

function getExtension( file ){
	file = new String( file );
	var p = file.lastIndexOf( "." );
	if( p < 0 ){ return ""; }
	return file.substr( p+1 );
}

