/**
 * Joints the parts of an email address
 * @param name {string}
 * @param server {string}
 * @param domain {string}
 * @param text {mixed}
 * @returns {string} jointed eMail address
 */
function createMail (name, server, domain, text) {
    
    var email = name+'@'+server+'.'+domain;
    var output = email;
    if (text === true) {
        output = '<a href="mailto:'+email+'">'+email+'</a>';
    } else if (text !== '')  {
        output = '<a href="mailto:'+email+'">'+text+'</a>';
    }
    document.write(output);
}

/**
 * Creates the local time string for the main.html template and outputs it.
 * 
 * @returns {void}
 */
function output_local_time (time_offset, dst) {
    // create campus specific date
    var date = new Date();
    var campus_date = new Date();
    campus_date.setTime(date.getTime() + time_offset * 1000);
    
    var hourse = campus_date.getHours();
    if (hourse < 10) hourse = " " + hourse;
    var minutes = campus_date.getMinutes();
    if (minutes < 10) minutes = "0" + minutes;
    var seconds = campus_date.getSeconds();
    if (seconds < 10) seconds = "0" + seconds;

    var output = hourse +':'+ minutes +':'+ seconds + ((dst == '1') ? ' DST' : '');
    
    if (document.getElementById("localtime")) document.getElementById("localtime").innerHTML = output;
    setTimeout('output_local_time('+time_offset+', '+dst+')', 1000);
}


// alpha numeric check function  -> see http://www.itgroup.com.ph/alphanumeric
(function($){

	$.fn.alphanumeric = function(p) { 

		p = $.extend({
			ichars: "!@#$%^&*()+=[]\\\';,/{}|\":<>?~`.- ",
			nchars: "",
			allow: ""
		  }, p);	

		return this.each
			(
				function() 
				{

					if (p.nocaps) p.nchars += "ABCDEFGHIJKLMNOPQRSTUVWXYZÄÖÜß";
					if (p.allcaps) p.nchars += "abcdefghijklmnopqrstuvwxyzäöüß";
					
					s = p.allow.split('');
					for ( i=0;i<s.length;i++) if (p.ichars.indexOf(s[i]) != -1) s[i] = "\\" + s[i];
					p.allow = s.join('|');
					
					var reg = new RegExp(p.allow,'gi');
					var ch = p.ichars + p.nchars;
					ch = ch.replace(reg,'');

					$(this).keypress
						(
							function (e) {
								
									if (!e.charCode) k = String.fromCharCode(e.which);
										else k = String.fromCharCode(e.charCode);
										
									if (ch.indexOf(k) != -1) e.preventDefault();
									if (e.ctrlKey&&k=='v') e.preventDefault();
									
								}
								
						);
						
					$(this).bind('contextmenu',function () {return false});
									
				}
			);

	};

	$.fn.numeric = function(p) {
	
		var az = "abcdefghijklmnopqrstuvwxyzäöüß";
		az += az.toUpperCase();

		p = $.extend({
			nchars: az
		  }, p);	
		  	
		return this.each (function() {
				$(this).alphanumeric(p);
			}
		);
			
	};
	
	$.fn.alpha = function(p) {

		var nm = "1234567890";

		p = $.extend({
			nchars: nm
		  }, p);	

		return this.each (function() {
				$(this).alphanumeric(p);
			}
		);
			
	};	

})(jQuery);

