function loadJS(js)
{
	var split = js.split(',');

	for (var i=0; i<split.length; i++)
	{
		var e  = document.createElement('script');

		e.type = 'text/javascript';
		e.src  = split[i];

		document.getElementsByTagName('head')[0].appendChild(e);
	}
}


function loadCSS(css)
{
	var split = css.split(',');

	for (var i=0; i<split.length; i++)
	{
		var e  = document.createElement('link');

		e.type = 'text/css';
		e.rel  = 'stylesheet';
		e.href = split[i];

		document.getElementsByTagName('head')[0].appendChild(e);
	}
}


function dumpObj(obj)
{
	var dump = '';

	for(i in obj) {
		dump += i + ' = ' + obj[i] + "\n";
	}

	alert(dump);
}


function hideAjaxLoader(placeholder)
{
	if (!placeholder) {
		placeholder = '#ajaxLoader';
	}

	$(placeholder).html('').hide();
}


function showAjaxLoader(placeholder)
{
	if (!placeholder) {
		placeholder = '#ajaxLoader';
	}

	$(placeholder).html('').show().append('<img src="/shared/gfx/ajax-loader-bar.gif" alt="" />');
}


function ask(message)
{
	return confirm(message);
}


function reLoad(url, target)
{
    if (typeof target != 'undefined') {
        parent.frames[target].location.href = url;
    }
	else {
        location.href = url;
    }

	return false;
}


function resetForm()
{
	$('*[id^=frm_]').val('');
}


function fillForm(obj)
{
	for (var key in obj) {
		$('#frm_' + key).val(obj[key]);
	}
}


function markForm(check)
{
	if (typeof check == 'string') {
		check = new Array(check);
	}

	if (typeof check == 'object')
	{
		for (var i=0; i<check.length; i++)
		{
			$('#'+check[i]).addClass('error');
			$('label[for='+check[i]+']').addClass('red');
		}
	}
}


/**
 * 2011-12-19
 */
function removeFormError(label)
{
	/*** Twitter Bootstrap ***/
	$('#'+label).parent('.clearfix').removeClass('error');
	$('#'+label).parent().parent('.clearfix').removeClass('error');

	$('#'+label).removeClass('error');
	$('label[for='+label+']').removeClass('red');
}


/**
 * 2011-12-19
 */
function setFormError(label)
{
	/*** Twitter Bootstrap ***/
	$('#'+label).parent('.clearfix').addClass('error');
	$('#'+label).parent().parent('.clearfix').addClass('error');

	$('#'+label).addClass('error');
	$('label[for='+label+']').addClass('red');
}


/**
 * 2011-12-19
 */
function setFormSuccess(label)
{
	/*** Twitter Bootstrap ***/
	$('#'+label).parent('.clearfix').addClass('success');
	$('#'+label).parent().parent('.clearfix').addClass('success');

	$('#'+label).addClass('green');
	$('label[for='+label+']').addClass('green');
}


/**
 * Formlarfelder validieren
 */
function checkForm(check)
{
	var focus = false;
	var hasError = false;
	var errors = new Array();
	var d = new xDate();

	if (typeof check == 'object')
	{
		if (check.length == 0) {
			hasError = true;
		}
		else
		{
			for (var i=0; i<check.length; i++)
			{
				var formTyp = $('#'+check[i]).attr('type');
				var formValue = $.trim($('#'+check[i]).val());

				if ($('#'+check[i]).attr('disabled') === true) {
					continue;
				}

				formTyp == 'checkbox'
					? formValue = $.trim($('#'+check[i]).attr('checked'))
					: formValue = $.trim($('#'+check[i]).val())
				;

				if ((formTyp == 'checkbox' && formValue == false) || formValue == '')
				{
					hasError = true;
					errors.push(check[i]);
					setFormError(check[i]);

					if (focus === false) {
						$('#'+check[i]).focus();
						focus = true;
					}
				}
				else if (check[i].indexOf('email') !== -1 && !checkEmail(formValue))
				{
					hasError = true;
					errors.push(check[i]);
					setFormError(check[i]);

					if (focus === false) {
						$('#'+check[i]).focus();
						focus = true;
					}
				}
				/*else if (check[i].match(/birthday/) && d.isDate(formValue) === false) {
					$("#"+check[i]).addClass("error");
					$("label[for='"+check[i]+"']").addClass("red");
					hasError = true;
				}*/
				else if (check[i].match(/date/) && d.isDate(formValue) === false)
				{
					hasError = true;
					errors.push(check[i]);
					setFormError(check[i]);

					if (focus === false) {
						$('#'+check[i]).focus();
						focus = true;
					}
				}
				else {
					removeFormError(check[i]);
				}
			}
		}
	}
	else {
		hasError = true;
	}

	return !hasError;
}


function isUrl(s)
{
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	return regexp.test(s);
}

function checkURL(s)
{
	return true;
}


function checkEmail(s)
{
	var match = new RegExp("^([a-zA-Z0-9\\-\\.\\_]+)(\\@)([a-zA-Z0-9\\-\\.]+)(\\.)([a-zA-Z]{2,10})$");
	var result = (match.test(s));

	return(result);
}


function checkBirthday(s)
{
	var match = new RegExp('(\d{4})-(\d{2})-(\d{2})');
	var result = (match.test(s));

	return(result);
}


function xDate()
{
	var minYear = 1900;
	var maxYear = 9999;

	this.daysInFebruary = function(year) {
		return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
	}

	this.toTimestamp = function(year, month, day)
	{
		month -= 1;

		if (month < 1) {
			month = 12;
			year -= 1;
		}

		var date = new Date(year, month, day);
		var time = parseInt(date.getTime() / 1000);

		return time;
	}

	this.daysArray = function(n)
	{
		for (var i = 1; i <= n; i++)
		{
			this[i] = 31;

			if (i==4 || i==6 || i==9 || i==11) {
				this[i] = 30;
			}

			if (i==2) {
				this[i] = 29;
			}
	   }

	   return this;
	}

	/**
	 * @param date1 = YYYY-MM-DD
	 * @param date2 = YYYY-MM-DD
	 */
	this.isGreater = function(date1, date2)
	{
		// Start- und Enddatum prüfen
		if (date1.match(/\d{4}-\d{1,2}-\d{1,2}/) && date2.match(/\d{4}-\d{1,2}-\d{1,2}/))
		{
			var split1 = date1.split('-');
			var split2 = date2.split('-');

			var testDate1 = new Date(split1[0], split1[1], split1[2]);
			var testDate2 = new Date(split2[0], split2[1], split2[2]);

			if (parseInt(testDate1.getTime()/1000) <= parseInt(testDate2.getTime()/1000)) {
				return true;
			}
		}

		return false;
	}

	this.isDate = function(date)
	{
		if (date.length != 10 && date.length != 19) {
			return false;
		}

		if (date.match(/(\d{2})\.(\d{2})\.(\d{4})/))
		{
			var year  = date.substring(10,6);
			var month = date.substring(5,3);
			var day   = date.substring(0,2);
		}

		if (date.match(/(\d{4})-(\d{2})-(\d{2})/))
		{
			var year  = date.substring(0,4);
			var month = date.substring(5,7);
			var day   = date.substring(10,8);
		}

		if (year == '0000' && month == '00' && day == '00') {
			return false;
		}

		if (!year || !month || !day) {
			return false;
		}

		var daysInMonth = this.daysArray(12);

		if (month.length<1 || month<1 || month>12){
			return false;
		}

		if (day.length<1 || day<1 || day>31 || (month==2 && day>this.daysInFebruary(year)) || day > daysInMonth[month]) {
			return false;
		}

		if (year.length != 4 || year == 0 || year<minYear || year>maxYear){
			return false;
		}

		return true;
	}
}

