var cJSBase = jQuery.Class.create({
	
	sAjaxUrl	: '',
	
	init : function()
	{
		this.sAjaxUrl = document.location.href;
	},
	
	ajaxRequest : function( oParameters, fResponse )
	{
		oParameters['is_ajax'] = 1;

		jQuery.post( 
			this.sAjaxUrl,
			oParameters,
			function( script )
			{
				if( script == '' ) return;
				try { eval( "var oRes = " + script + ";" ); }catch( err ){ return; }
				if( typeof( oRes ) != 'object' ) return;
				if(!fResponse)
					fResponse = null
				if( typeof( fResponse ) == 'function' ) fResponse( oRes );
				else
				{
					try { eval( "oRes;" ); }catch( err ){ return; }
				}
			}
		);
	},

	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]+$/ ) == -1 ) return false;
		else return true;
	},	
	
	userLogout : function()
	{
		var redirect = sBase;
		this.ajaxRequest( { 'action' : 'logout' }, function(){ document.location = redirect; } );
	},
	
	restorePassword : function()
	{
		userMail = $("#login_email").val();
		if(this.checkMail(userMail))
			this.ajaxRequest( { 'action' : 'restore','mail' : userMail }, function(bRes)
																			{  
																				if("true" == bRes["success"])
																				{
																					alert("Password sent to your email");
																					$('[id*=\'login_section_\']').toggle();
																				}
																				else
																				{
																					alert("Email is not present in our database");
																				}
																			} );
		else
			alert("Email is incorrect");
	}
} );

function userLogin( username, password )
{
	oMain.ajaxRequest( { 'action' : 'login', 'actionType' : 'users', 'username' : username, 'password' : password }, userLoginRespond );
}
function userLoginRespond( oResult )
{
	if( oResult.success == 'true' )
	{
		j( '#topLogoutBlock' ).show();
		j( '#topLoginBlock' ).hide();
		j( '#systemUserName' ).attr( 'innerHTML', oResult.name );
		j( '#userErrorField' ).attr( 'innerHTML', '' );
	}else{
		j( '#topLogoutBlock' ).hide();
		j( '#topLoginBlock' ).show();
		j( '#systemUserName' ).attr( 'innerHTML', '' );
		j( '#userErrorField' ).attr( 'innerHTML', oResult.msg );
	}
}
function userLogout()
{
	oMain.ajaxRequest( { 'action' : 'logout', 'actionType' : 'users' }, userLogoutRespond );
}
function userLogoutRespond( oResult )
{
	if( oResult.success == 'true' )
	{
		document.location = document.location;
	}
}



function parseText( 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;
}

function CheckEmail(semail) {
	if(document.layers || document.all || document.getElementById) {
		return (semail.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1);
	}
	return true;
}


function newImage() 
{
	var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
	var i,j=d.MM_p.length,a=newImage.arguments; for(i=0; i<a.length; i++)
	if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function LeftTrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
function RightTrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
function FullTrim(str) {
	return LeftTrim(RightTrim(str));
}

function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}

function Reload()
{
	window.location.reload();
}
