var Class = {
   create: function() {
     return function() {
//       if (this.initialize) {
//         this.initialize();
//       }
return this;
     }
   }
 }  


var site = Class.create();
site.prototype = {
	
	sysDiv : 'system__div',
	ajaxUrl : '/',
	domain : null,
	browser : null,
	
	initialize : function( domain, ajaxUrl )
	{
		if( !ajaxUrl ) ajaxUrl = '';
		this.domain = domain;
		this.ajaxUrl = domain+ajaxUrl;
		this.browser = (function x(){})[-5]=='x' ? 'FF3' : (function x(){})[-6]=='x' ? 'FF2' : /a/[-1]=='a' ? 'FF' : /msie|MSIE 6/.test(navigator.userAgent) ? 'IE6' : /msie|MSIE 7/.test(navigator.userAgent) ? 'IE7' : /msie|MSIE 8/.test(navigator.userAgent) ? 'IE8' : /a/.__proto__=='//' ? 'Saf' : /s/.test(/a/.toString) ? 'Chr' : /^function \(/.test([].sort) ? 'Op' : 'Unknown';
	},
	

	updater : function( action, parameters )
	{
		var params = '';
		for( i in parameters )
		{
			params += '&V[' + i + ']=' + parameters[i];
		}
		
//		new Ajax.Updater( this.sysDiv, this.ajaxUrl, { parameters : 'action=' + action + params } );
	},
	
	
	login : function( username, password )
	{
		$.ajax({
			type : "POST",
   			url : this.domain,
   			data : "action=usersLoginLogout&V[action]=login&V[username]=" + this.parseText( username ) + "&V[password]=" + this.parseText( password ) + "&V[host]=" + this.parseText( this.domain ),
   			success: function( msg )
   			{
   	 			eval( 'var res = ' + msg + ';' );
   	 			
   	 			if( res['is_error'] && res['is_error'] == '1' )
   	 			{
   	 				eval( 'tAlert.message( "' + res['error_msg'] + '" );' );
   	 			}else{
   	 				document.getElementById( 'loginForm' ).submit();
   	 				document.location = res['host'] + 'extranet/';
   	 			}
			}
 		});
	},
	
	fogotPassword : function( email )
	{
		$( '#remindAnswer' ).attr( 'innerHTML', '' );
		$.ajax({
			type : "POST",
   			url : this.domain,
   			data : "action=usersLoginLogout&V[action]=fogotPassword&V[email]=" + this.parseText( email ) + "&V[host]=" + this.parseText( this.domain ),
   			success: function( msg )
   			{
   	 			eval( 'var res = ' + msg + ';' );
   	 			
   	 			tAlert.message( '' );
   	 			$( '#remindAnswer' ).attr( 'innerHTML', res['error_msg'] );
   	 			
   	 			if( res['is_error'] && res['is_error'] == '1' )
   	 			{
   	 				return;
   	 			}else{
   	 				
//   	 				$( '#fogotPassDiv' ).hide();
   	 			}
			}
 		});
	},
	
	logout : function()
	{
		$.ajax({
			type : "POST",
   			url : this.domain,
   			data : "action=usersLoginLogout&V[action]=logout",
   			success: function( msg )
   			{
   	 			eval( 'var res = ' + msg + ';' );
   	 			
   	 			if( res['is_error'] && res['is_error'] == '1' )
   	 			{
   	 				eval( 'tAlert.message( "' + res['error_msg'] + '" );' );
   	 			}else{
   	 				document.location = document.location;
   	 			}
			}
 		});
		
	},

	
	checkMail : function ( userMail, noempty )
	{
		if( ( noempty == true && userMail == '' ) || userMail != '' && userMail.search( /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{2,4}$/ ) == -1 ) return false;
		else return true;
	},

	
	checkEmpty : function( check, message )
	{
		if( check != '' ) return true;
		
		if( message ) alert( message );
		return false;
	},

	
	parseText : function( text )
	{
		text = text.replace(/%/gi, "%25");
		text = text.replace(/ /gi, "%20");
		text = text.replace(/!/gi, "%21");
		text = text.replace(/\"/gi, "%22"); //"
		text = text.replace(/#/gi, "%23");
		text = text.replace(/\$/gi, "%24");
		text = text.replace(/&/gi, "%26");
		text = text.replace(/\'/gi, "%27"); //'
		text = text.replace(/\(/gi, "%28");
		text = text.replace(/\)/gi, "%29");
		text = text.replace(/\*/gi, "%2a");
		text = text.replace(/\+/gi, "%2b");
		text = text.replace(/\,/gi, "%2c");
		text = text.replace(/-/gi, "%2d");
		text = text.replace(/\./gi, "%2e");
		text = text.replace(/\//gi, "%2f");
		text = text.replace(/:/gi, "%3a");
		text = text.replace(/;/gi, "%3b");
		text = text.replace(/</gi, "%3c");
		text = text.replace(/=/gi, "%3d");
		text = text.replace(/>/gi, "%3e");
		text = text.replace(/\?/gi, "%3f");
		text = text.replace(/@/gi, "%40");
		text = text.replace(/\[/gi, "%5b");
		text = text.replace(/\\/gi, "%5c");
		text = text.replace(/\]/gi, "%5d");
		text = text.replace(/\^/gi, "%5e");
		text = text.replace(/_/gi, "%5f");
		text = text.replace(/`/gi, "%60");
		text = text.replace(/\{/gi, "%7b");
		text = text.replace(/\|/gi, "%7c");
		text = text.replace(/\}/gi, "%7d");
		text = text.replace(/~/gi, "%7e");
		return text;
	}	
	
	
}
