c24 = {
	vars : [],
	delays : [],
	isIE6 : $.browser.msie && $.browser.version.substr(0,3)=='6.0',
	isIE7 : $.browser.msie && $.browser.version.substr(0,3)=='7.0',
	isIE8 : $.browser.msie && $.browser.version.substr(0,3)=='8.0',
	isFF2 : $.browser.mozilla && (parseFloat($.browser.version) < 1.9),
	
	fade : function(fade_in, names, instant) {
		if (!$.isArray(names)) {
			names = new Array(names);
		}
		$.each(names, function(i, name) {
			if (fade_in) {
				c24.fadeIn(name, instant)
			} else {
				c24.fadeOut(name, instant)
			}			
		});
	},
	
	fadeIn : function(name, instant) {
		if(this.isIE6 || instant){
			$(name).show();
		}
		else{
			$(name).fadeIn();
		}
	},

	fadeOut : function(name, instant) {
		if(this.isIE6 || instant){
			$(name).hide();
		}
		else{
			$(name).fadeOut();
		}
	},
	
	set : function(name, value) {
		this.vars[name] = value;
	},
	
	get : function(name) {
		return this.vars[name];
	},	
		
	delaySet : function(id, ms, func) {
		this.delayClear(id);
		this.delays[id] = setTimeout(func +'()' , ms);
	},
	
	delayClear : function(id) {
		if (this.delays[id]) {
			clearTimeout(this.delays[id]);
			this.delays[id] = false;
		}
	},	
	
	include : function(src, id) {
		var script = document.createElement('script');
		script.src = src;
		script.type = 'text/javascript';
		script.defer = true;
		if (id) script.id = id;

		var head = document.getElementsByTagName('head').item(0);
		head.appendChild(script);
	},
	
	popup : function() {
		var w = arguments[0] ? arguments[0] : '580';
		var h = arguments[1] ? arguments[1] : '360';
		var popup = window.open('', 'popup', 'width='+w+',height='+h+',scrollbars=yes, toolbar=no,status=no,resizable=yes,menubar=no,location=no,directories=no,top=10,left=10');
		popup.focus();
	},
	
	preventMultiSubmit : function(form) {
		if ($(form).attr('preventsubmit') == '1') {
			return false;
		} else {
			$(form).attr('preventsubmit', '1');
			$('.c24-button-submitting').show();
			c24.delaySet('resetMultiSubmit', 8000, 'c24.resetMultiSubmit');
			return true;
		}
	},
	
	resetMultiSubmit : function() {
		$('form[preventsubmit=1]').attr('preventsubmit', '0');
		$('.c24-button-submitting').hide();
	},
	
	formBackSubmit : function(node) {
		var form = $(node).closest('form');
		var action = form.find('input[name=formBackAction]').val();
		form.attr('action', action);
		// Added for Satander Clickouts auf der TarifDetail Page (Credit)
		form.removeAttr('target');
		
		return true;
	},
	
	setThousandPoint : function(evt,obj) {
		evt = (evt) ? evt : ((event) ? event : null);
		
		var strOldVal = obj.value;
		
		if(typeof obj.selectionStart != 'undefined')
			var iCursorPos = obj.selectionStart;
		else if(typeof document.selection != 'undefined') {
			var range = document.selection.createRange();
			var iCursorPos = Math.abs(document.selection.createRange().moveStart("character", -20));
		} else
			return;
		
		if (evt) {
			var key=0;
			
			if (evt.keyCode)
				key = evt.keyCode;
			
			if (evt.charCode)
				key = evt.charCode;
			
			if ((key>34 && key<41) || key==9 || (key>15 && key<19))
				return true;
			
			var strVal = obj.value.replace(/\./g,"");
			var bNegative = 0;
			if (strVal.match(/-/)) {
				strVal = strVal.replace(/-/g,"");
				bNegative = 1;
			}
			
			var iLen = strVal.length;
			
			if (iLen<4 && obj.value.match(/\./g))
				obj.value = ((bNegative==1)?"-":"")+strVal;
			else if (iLen > 3) {
				var strNewVal = ((bNegative==1)?"-":"")+strVal.substr(0,iLen%3);
				for (i=iLen%3;i<iLen;i+=3)
					strNewVal += ((i!=0)?".":"")+strVal.substr(i,3);
				
				obj.value = strNewVal;
			}
			
			var iOffset = 0;
			if (obj.value.length != strOldVal.length)
				iOffset += obj.value.length - strOldVal.length;
			
			if(typeof obj.selectionStart != 'undefined') {
				obj.selectionStart = iCursorPos+iOffset;
				obj.selectionEnd = iCursorPos+iOffset;
			} else if(typeof document.selection != 'undefined') {
				range.move('character', iCursorPos+iOffset);
				range.select();
			}
		}
	},
	
	check : function(evt, obj, bAllowNegative) {
		evt = (evt) ? evt : ((event) ? event : null);
		
		if (evt) {
			var key=0;
			
			if (evt.keyCode)
				key = evt.keyCode;
			
			if (evt.charCode)
				key = evt.charCode;
			
			if ((evt.shiftKey || evt.altKey) && (key != 9 && key != 46 && (key<35 || key > 42))) 
				return false;
			
			if (bAllowNegative == 1 && (key == 109 || key == 189)) {
				if (obj.value.indexOf("-") > -1)
					bAllowNegative = 0;
				
				if (typeof obj.selectionStart != 'undefined') {
					if (obj.selectionStart > 0)
						return false;
				}
			}
			
			if (!isNaN(String.fromCharCode(key)))
				return true;
			
			if (key == 8 || key == 9 || key == 13 || key == 46 || (key>34 && key<41) || (key>47 && key<58) || (bAllowNegative == 1 && (key == 109 || key == 189)))
				return true;
			
			if (!window.opera && (key>95 && key<106))
				return true;
			
			return false;
			
		}
	}
};

c24_formtoggle = {
	
	register : function() {
		$('.c24-toggle').each(function () {
			var toggle = this;
			var id = c24_formtoggle.getid(toggle);
			if (id.length > 0) {
				c24_formtoggle.check(toggle, id);
				
				$(toggle).click(function() {
					if ($(this).hasClass('c24-close')) {
						$(this).removeClass('c24-close');
						$('#'+ id).hide('blind');			
					} else {
						$(this).addClass('c24-close');
						$('#'+ id).show('blind');
					}
					return false;
				})
			}
		});
	},
	
	getid : function(obj) {
		var id = $(obj).attr('toggleid');
		if (id == null) {
			id = $(obj).attr('id');
			id = id + '_content';
		}
		return id;
	},
	
	check : function(toggle, id) {
		if($('#' + id).css('display') != 'none' && $(toggle).hasClass('c24-close')==false) {
			$(toggle).addClass('c24-close');
		} 
	}
}


c24_helptip = {
	active : false,
	
	//register tooltips


	register : function() {
		
		$('.c24-helptip').each(function () {
			var helptip = $(this);
			var helptip_icon = $('.c24-helptip-icon', helptip);
			var helptip_content = $('.c24-helptip-content', helptip);
			
			if(helptip.hasClass('c24-iframe')){
				helptip_content.bgiframe();
			}
			
			helptip_icon.click(function(e) {
				helptip.addClass('c24-maximize-zindex');
				
				if(c24.isIE6){
					if(c24_helptip.active!=helptip_content){
						c24_helptip.close();
						
						c24_helptip.active = helptip_content;
						e.stopPropagation();
					}
					helptip_content.show();
				}
				else{
					helptip_content.fadeIn('fast', function() {
						c24_helptip.active = helptip_content;
					});
				}
			});
			
			if(c24.isFF2){
				// Fix Positioning Bug
				helptip_content.css('left', '-273px');
				// Use FF2 inline-block
				helptip.css('display', '-moz-inline-box');
			}
		});
		
		$(document).click(function() {
			c24_helptip.close();
		});
	},
	
	close : function() {
		if (c24_helptip.active) {
			
			c24_helptip.active.closest('.c24-helptip').removeClass('c24-maximize-zindex');

			if(c24.isIE6){
				c24_helptip.active.hide();
			}
			else{
				c24_helptip.active.fadeOut('fast');
			}	
			c24_helptip.active = false;
		}				
	}
};

c24_tooltip = {
	active : false,
	
	//register tooltips


	register : function() {
		
		$('.c24-tooltip').each(function () {
			var tooltip = $(this);
			var tooltip_icon = $('.c24-tooltip-icon', tooltip);
			var tooltip_content = $('.c24-tooltip-content', tooltip);
			
			if(tooltip.hasClass('c24-iframe')){
				tooltip_content.bgiframe();
			}
			
			tooltip_icon.mouseover(function(e) {
				tooltip.addClass('c24-maximize-zindex');
				
				if(c24.isIE6){
					if(c24_tooltip.active!=tooltip_content){
						c24_tooltip.close();
						
						c24_tooltip.active = tooltip_content;
						e.stopPropagation();
					}
					tooltip_content.show();
				}
				else{
					tooltip_content.fadeIn('fast', function() {
						c24_tooltip.active = tooltip_content;
					});
				}
			});
			
			tooltip_icon.mouseout(function(e) {
				c24_tooltip.close();
			});
			
			tooltip_icon.click(function(e) {
				c24_tooltip.close();
			});
			
			if(c24.isFF2){
				// Fix Positioning Bug
				tooltip_content.css('left', '-273px');
				// Use FF2 inline-block
				tooltip.css('display', '-moz-inline-box');
			}
		});
		
		$(document).click(function() {
			c24_tooltip.close();
		});
	},
	
	close : function() {
		if (c24_tooltip.active) {
			
			c24_tooltip.active.closest('.c24-tooltip').removeClass('c24-maximize-zindex');

			if(c24.isIE6){
				c24_tooltip.active.hide();
			}
			else{
				c24_tooltip.active.fadeOut('fast');
			}	
			c24_tooltip.active = false;
		}				
	}
};

c24_hinttip = {
	active : false,
	
	//register hinttips
	register : function() {
		
		$('.c24-hinttip').each(function () {
			var hinttip = $(this);
			var hinttip_content = hinttip.find('.c24-hinttip-content');

			if(hinttip.hasClass('c24-visible')){
				return;
			}
			
			hinttip_content.html(
				'<div class="c24-hinttip-inner">' +
					hinttip_content.html() +					
					'<div class="c24-hinttip-corner">&nbsp;</div>' +
					'<div class="c24-hinttip-close">&nbsp;</div>' +
				'</div>'
			);
			if(!hinttip.hasClass('c24-faqtip')){
				hinttip_content.bgiframe();
			}
			
			hinttip.show = function(e){
				hinttip.addClass('c24-maximize-zindex');
				
				if(c24.isIE6){	
					if(c24_hinttip.active!=hinttip_content){
						c24_hinttip.close();
						
						c24_hinttip.active = hinttip_content;
						if(e){
							e.stopPropagation();
						}
					}
					hinttip_content.show();
				}
				else{
					hinttip_content.fadeIn('fast', function() {
						c24_hinttip.active = hinttip_content;
					});
				}
			}
			
			
			hinttip.click(function(e) {
				hinttip.show(e);
			});
			
			// Avoid IE Glitch
			if(hinttip.hasClass('c24-faqtip')){
				hinttip.find('.c24-hinttip-title').hover(
					function() {
						$(this).addClass('c24-hinttip-hover');
					},
					function() {
						$(this).removeClass('c24-hinttip-hover');
					}
				);	
			}
					
		});
		
		$(document).click(function() {
			c24_hinttip.close();
		});
	},
	
	close : function() {
		if (c24_hinttip.active) {
			var t=c24_hinttip.active.closest('.c24-hinttip');
			t.removeClass('c24-maximize-zindex');
			
			if(c24.isIE6){
				c24_hinttip.active.hide();
			}
			else{
				c24_hinttip.active.fadeOut('fast');
			}
			c24_hinttip.active=false;
		}
	}



};

c24_toptip = {
	active : false,
	
	close : function() {
		if (c24_toptip.active) {
			if(c24.isIE6){
				c24_toptip.active.hide();
			}
			else{
				c24_toptip.active.fadeOut('fast');
			}
			c24_toptip.active=false;
		}
	},
	
	//register toptips
	register : function() {
		
		$('.c24-toptip').each(function () {
			var toptip = $(this);
			var toptip_content = toptip.find('.c24-toptip-content');
			
			toptip_content.html(
				'<div class="c24-toptip-inner">' +
					toptip_content.html() +					
					'<div class="c24-toptip-corner">&nbsp;</div>' +
					'<div class="c24-toptip-close">&nbsp;</div>' +
				'</div>'
			);
			toptip_content.bgiframe();
			
			if (toptip_content.hasClass('c24-toptip-active')) {
				c24_toptip.active = toptip_content;
				toptip_content.show();
			}
			
			toptip.click(function(e) {
				if(c24.isIE6){
					toptip_content.show();
					if(c24_toptip.active!=toptip_content){
						if (c24_toptip.active) {
							c24_toptip.active.hide();
						}
						c24_toptip.active = toptip_content;
						e.stopPropagation();
					}
				}
				else{
					toptip_content.fadeIn('fast', function() {
						c24_toptip.active = toptip_content;
					});
				}
			});
		});
		
		$('.c24-toptip-close').click(function (e) {
			c24_toptip.close();
			e.stopPropagation();
		});
	}
};


c24_datepicker = {
	//datepicker array
	dps : [],
	//callback mapping
	callbacks : [],
	
	//add date picker
	add : function(dp) {
	
		this.dps.push(dp);
	},	
	
	// register date pickers
	register : function() {
		
		$.each(this.dps, function(key, dp) {
			
			var dp_default = {
				yearRange: '1900:2100',
				constrainInput: false,
				buttonImage: '/einsurance/media/credit/default/imgs/global/calendar.png',
				buttonImageOnly: true,
				duration: 0,
				showOn: 'button',
				buttonText: 'Kalender',
				changeMonth: true,
				changeYear: true,
				onSelect: function() {
					c24_datepicker.success(this.id);
				}
			};
			
			var dp_settings = $.extend(dp_default, dp);
			
			$('#'+ dp.id).
				after('<em id="'+ dp.id +'_info">TT.MM.JJJJ</em>').
				datepicker(dp_settings);
			
			if(dp.onSuccess) {
				c24_datepicker.callbacks[dp.id] = key;			
			}
			
			$('#'+ dp.id).keyup(function() {
				c24_datepicker.val(this.id);
			});
		});
		
		$.each(this.dps, function() {
			c24_datepicker.val(this.id);
		});
	},
	
	// set info
	success : function(id) {
		
		var date = $('#'+ id).datepicker('getDate');
		$('#'+ id + '_info').html(
			$.datepicker.formatDate(
				'DD, dd. MM yy',
				date
			)
		);
		
		try { //try to execute callback function
			this.dps[this.callbacks[id]].onSuccess(id, date);
		}
		catch(e) { //no callback
			
		}
	},
	
	val : function(id) {
		
		var regex = /^[0-9]{2}\.{1}[0-9]{2}\.{1}[0-9]{4}$/;
		var node = $('#'+ id);
		
		var udate = node.attr('value').replace(/[,-/]/g, '.');
		if (udate.match(/^[0-9]{8}$/)) {
			udate = udate.substr(0, 2) + '.' + 	udate.substr(2, 2) + '.' + udate.substr(4);
		}
		
		var udatestandard = udate.replace(/^([1-9]{1})\./, '0$1.').replace(/\.([1-9]{1})\./, '.0$1.');
		
		//console.log(udatestandard);
		
		if (udatestandard.match(regex) && (node.attr('value') != udate))
		{
			node.attr('value', udate);
		}
		
		var dpdate = $.datepicker.formatDate(
			'dd.mm.yy',
			node.datepicker('getDate')
		);

		if (dpdate.match(regex) && (udatestandard == dpdate))
		{
			this.success(id);
		}
		else
		{
			$('#'+ id +'_info').html('TT.MM.JJJJ');
		}		
	}
};

c24_date = {
	now : new Date(),
	isToday : function(date) {
		if (
			date.getFullYear() == this.now.getFullYear() &&
			date.getMonth() == this.now.getMonth() &&
			date.getDate() == this.now.getDate()
		) {
			return true;
		}
		
		return false;
	},
	
	isFutureDay : function(date) {
		if (date.getFullYear() > this.now.getFullYear()) {
			return true;
		} else if (
			date.getFullYear() == this.now.getFullYear() &&
			date.getMonth() > this.now.getMonth()
		) {
			return true;
		} else if (
			date.getFullYear() == this.now.getFullYear() &&
			date.getMonth() == this.now.getMonth() &&
			date.getDate() > this.now.getDate()
		) {
			return true;
		}	
				
		return false;
				
	},
	
	isPastDay : function(date) {
		return (!this.isToday() && !this.isFutureDay());
	},
	
	isLeapYear : function(year) {
		return (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
	}
}

c24_dialog = {
	active : false,
		
	register: function() {
		$('.c24-dialog-onclick').each(function () {
			$(this).click(function() {
				if (!c24_dialog.active) c24_dialog.show();
			});
		});

		$(document).keypress(function(e){
			if(c24_dialog.active && e.keyCode==27){
				c24_dialog.hide();
			}
		});		
	},
		
	show: function() {
		c24_dialog.active = true;
		$('#c24_dialog_bg').css({
			'opacity': '0.25'
		});
		$('#c24_dialog_bg').fadeIn('slow');
		
		this.center();
		$('#c24_dialog_container').fadeIn();
	},
	
	hide: function() {
		c24_dialog.active = false;
		$('#c24_dialog_bg').fadeOut('slow');
		$('#c24_dialog_container').fadeOut();
	},
	
	center: function() {
		var documentWidth = $(document).width();
		var documentHeight = $(document).height();
		var windowWidth = $(window).width();
		var windowHeight = $(window).height();
		var scrollTop = $(window).scrollTop();
		
		var dialogHeight = $('#c24_dialog_container').height();
		var dialogWidth = $('#c24_dialog_container').width();
		
		var top = Math.round(windowHeight/2 - dialogHeight/2);
		var left = Math.round(windowWidth/2 - dialogWidth/2);
		
		if (c24.isIE6) top += scrollTop;
		
	
		//centering
		$('#c24_dialog_container').css({
			'top': top +'px',
			'left': left +'px'
		});
		
		//only need force for IE6
		if (c24.isIE6) $('#c24_dialog_bg').css({
			'height': documentHeight +'px'
		});		
	}
}

function c24_browserSetup() {
	 if ($.browser.msie) {
	 	$('#c24').addClass('ie').addClass('ie'+$.browser.version.substr(0,1));
	 }
	 if ($.browser.safari) {
		 $('#c24').addClass('sa');
	 }
	 if ($.browser.opera) {
		 $('#c24').addClass('op');
	 }
	 //if ($.browser.mozilla) {
	 //	 $('#c2<4').addClass('mo');
	 //} 
}


/* deprecated! use c24.popup() instead */
function c24_popup()
{
	var w = arguments[0] ? arguments[0] : '580';
	var h = arguments[1] ? arguments[1] : '360';
	var popup = window.open('', 'popup', 'width='+w+',height='+h+',scrollbars=yes, toolbar=no,status=no,resizable=yes,menubar=no,location=no,directories=no,top=10,left=10');
	popup.focus();
}

function c24_alt_submit(name,url){
	var form=$('form[name="'+name+'"]');
	if(form){
		form.attr("action",url);
		form.submit();
	}
}

/* startup pre render*/
function c24_base_prepare() {
}


/* startup post render*/
$(function() {
	c24_dialog.register();
	c24_helptip.register();
	c24_tooltip.register();
	c24_datepicker.register();
	c24_hinttip.register();
	c24_toptip.register();
	c24_formtoggle.register();
	
	c24_browserSetup();
	

	$(".c24-input-numeric").numeric();
	$(".c24-input-float").numeric({allow:".,"});
	$(".c24-input-alphanumeric").alphanumeric();
	$(".c24-input-alpha").alpha();
	//$(".c24-input-uppercase").aplphanumeric(); Handled via css text-transform-uppercase
	
	$('input[role=focus],select[role=focus]').focus();
});

var c24_allowedKeyCodes = new Array(8,9,13,35,36,37,38,39,40,46);
var c24_currentTimestamp; 

function c24_setCurrentTimestamp(timestamp) {
    c24_currentTimestamp = timestamp;
}

function c24_setThousendPoint(e) {
	var iKeyCode = window.event ? window.event.keyCode : e.which;
	var bShift = window.event ? window.event.shiftKey : e.shiftKey;
	var obj = e.target;
	
	if ((((iKeyCode > 47 && iKeyCode < 58) || (iKeyCode > 95 && iKeyCode < 106)) && !bShift) || (c24_allowedKeyCodes.indexOf(iKeyCode) != -1)) {
		c24_insertThousendPoint(obj);
	}
} 
function c24_removeThousendPoint(e) {
	var obj = e.target;
	$(obj).val($(obj).val().replace(/\./g,''));
}
function c24_updateThousendPoint(e){
	c24_insertThousendPoint(e.target);
}

function c24_convertThousendPoint(val){
	val=''+val;
	var append='';
	var posComma=val.lastIndexOf(',');
	if(posComma>=0){
		append=val.substring(posComma);
		// Correct input for user 1,000 -> 1.000
		if(append.length>=4 && val.indexOf('.')<0){
			append='';
			val = val.replace(/,/g,"");
		}
		else{
			val=val.substring(0,posComma);
		}
	}
	
	var strVal = val.replace(/\./g,"");
	var bNegative = 0;
	
	if (strVal.match(/-/)){
		strVal = strVal.replace(/-/g,"");
		bNegative = 1;
	}

	var iLen = strVal.length;
	
	if (iLen<4 && val.match(/\./g)){
		strVal = ((bNegative==1)?"-":"")+strVal;
	}
	else if (iLen > 3){
		var strNewVal = ((bNegative==1)?"-":"")+strVal.substr(0,iLen%3);
		for (i=iLen%3;i<iLen;i+=3)
		strNewVal += ((i!=0)?".":"")+strVal.substr(i,3);
		
		strVal = strNewVal;
	}
	
	return strVal+append; 
	
}


function c24_insertThousendPoint(obj){
	var strVal =c24_convertThousendPoint(obj.value);
	obj.value = strVal;
}



$(function() {
	
	$.each($(".c24-input-currency"), function(key, input) {
			$(input).numeric({allow:".,"});
			c24_insertThousendPoint(input);
			$(input).focus(function(e) {
				c24_removeThousendPoint(e);
				this.select();
			});
			$(input).blur(c24_updateThousendPoint);
			$(input).change(c24_updateThousendPoint);
	});	
	
	
});




