var cAdmin = 
{
	/***********************************************************************/
	/**************************** T E M P L A T E  ****************************/
	/***********************************************************************/
	/** Very simple js templating engine, templates consist of normal html , with        ***/
	/** values to be replaced between '|' characters   eg "<b>|bold_value|</b>"          ***/
	/**********************************************************************/
	template:function()
	{
		this.original="";
		this.manipulated="";
		this.loaded=false;
		this.fromUrl=function(_url)
		{/*loads  the template from an external source*/
			var response=jQuery.ajax({url:_url,async:false}).responseText;
			var error=error_request(response);
			if(error!==false) 
			{
				alert(error.response);
				this.loaded=false;
				return false;
			}
			this.original=response;
			this.manipulated=response;
		}
		/***********/
		this.data=function(html)
		{/*The data is given as a html-string*/
			
			this.original=html;
			this.manipulated=html;
		}
		/************/
		this.set=function(search,replace)//Very simple template function
		{
			exp= new RegExp("\\|" + search + "\\|","g");
			this.manipulated = this.manipulated.replace(exp,replace);
		}
		/************/
		this.getResult=function()
		{//Returns the parsed string
			return this.manipulated;
		}
		/************/
		this.rollBack=function()
		{//Rolls back the changes
			this.manipulated=this.original;
		}
	}//template
	/**********************************************************************************************************/
}

function ajaxObj()
{
var obj;
	if(window.XMLHttpRequest)
	{
		obj = new XMLHttpRequest();
	}
	else//IE6
	{
		obj = new ActiveXObject("Microsoft.XMLHTTP");
	
	}
	
return obj;

}
/**************************************************************/
function runAjaxCode(url)
{
	jQuery.getScript(url);
}
/**************************************************************/
function addEvent(obj,type,func)
{
	if(obj.attachEvent)
	{
		obj.attachEvent('on' + type,func);
	}
	else obj.addEventListener(type,func,false);
	
}
/*********************************************************/

function unescapeHTML(html)
{
return html.split("&amp;").join("&").split("&lt;").join("<").split("&gt;").join(">");

}
/***********************************************************************/
function getElement(id)
{
	if(document.getElementById) {return document.getElementById(id);}
	else if(document.all){ return document.all[id];}
	return false;

}
/***********************************************************************************/
function make_visible(id)
{
	var element=document.getElementById(id);
	if(element.style.visibility=='visible')
	{
		element.style.visibility='hidden';
		element.style.display='none';
		return 0;
	}
	element.style.visibility='visible';
	element.style.display='block';
}
/****************************************************************************************/

/***Wrapper-functions for the Sdialog-class********/


function dialogGetWidth()
{
	return Sdialog.dBox.width();
}
/********************************************************************************************/
function dialogGetHeight()
{
	return Sdialog.dBox.height();
}
/********************************************************************************************/
function dialogFixWH()//fixes width and height for dialogs opened with openDialog, useful if the Dialog has eg. lot's of images and no set w/h
{						//making the w/h change as the images getting loaded, thus destroying the neat centeronpage , run this function onload to fix this
	Sdialog.dBox.fixWH();
}
/**********************************************************************************************/
function dialogFixWHonLoad()
{
	var images=Sdialog.dBox.element.getElementsByTagName('img');
	for(var i=0;i<images.length;i++)
	{
		var temp=images[i];
		addEvent(temp,'load',dialogFixWH);
	}
	
}//end
/******************************************************************************************************/

function parseInput(instring,tag)//parses strings with bb-style tags returning an array of strings containing everything  between [tag] and [/tag]
{
	exp= new RegExp("\\[" + tag + "\\](.+?)\\[\\/" + tag + "\\]","g");

	regArr=instring.match(exp);
	if(regArr==null) return false;
	for(var i=0;i<regArr.length;i++)
	{
		exp= new RegExp("\\[" + tag + "\\]","g");
		
		temp=regArr[i].replace(exp,"");
		regArr[i]=temp;
		exp= new RegExp("\\[\\/" + tag + "\\]","g");
		temp=regArr[i].replace(exp,"");
		regArr[i]=temp;
		
		
	}//end of for
	return regArr;

}//end of parseInput
/***************************************************************************************************/


function ajaxDo(url)
{
	Ajax=ajaxObj();
	Ajax.open('GET',url,false);
	Ajax.send(null);
	var response=Ajax.responseText;
	if(response==="false") return false;
	var error=request_success(response);
	if(error!==false) alert(error.response);
	return true;
}

/**********************************************************************************************************/


function URLarguments(url)
{
	exp=/\?/g;
	if(url.search(exp)<0) return false;
	return true;
		
}//end of checkURL
/*************************************************************************************/
function getMessage(phrase)
{
	return admGetMessage(phrase);
}
/******************************************************************************/

function getCanvasWidth()
{
	de=document.documentElement;

	return window.innerWidth || self.innerWidth || de.clientWidth || document.body.clientWidth;

}
/*******************************************************************************/

function getCanvasHeight()
{
	de=document.documentElement;
	return window.innerHeight || self.innerHeight || de.clientHeight || document.body.clientHeight;

}

/*******************************************************************************/

function getScrollHeight()
{
	de=document.documentElement;
	return window.pageYOffset ||  de.scrollTop || document.body.scrollTop;

}
/*******************************************************************************/

function getScrollWidth()
{
	de=document.documentElement;
	return window.pageXOffset ||  de.scrollLeft || document.body.scrollLeft;


}
/*******************************************************************************/

function request_success(response)
{	
	response= response.toString();
	var code=response.match(/^[a-z]+;/i);
	if(code==null) return false;
	var returnval=new Object();
	returnval.response=response.replace(/^[a-z]+;/i,'');
	code=code[0].split(';').join('');
	switch(code)
	{
		case 'true': 
			returnval.code=true;
		break;
		case 'false':
			returnval.code=false;
		break;
		default:
			returnval.code=code[0];
		break;
	}//end of switch
	try
	{	
		if(returnval.response=='not_logged_in')
		{
			throw "not_logged_in";
		}
	}
	catch(error)
	{
		if(error=="not_logged_in")
		{
			window.location.href="login.html";
			returnval.response="Du är inte inloggad!";
		}
		
	}
	return returnval;
}
/**************************************************************************************/
function extract_html(htmlString)
{
	var rp=new RegExp("<[^<>]+>","g");
	var returnObject=new Object();
	returnObject.tags=htmlString.match(rp);
	returnObject.stringArray=htmlString.split(rp);
	return returnObject;
}
/********************************************************************************************/
function getUniqueElementId()
{
	var id=Math.random();
	while(getElement(id) != undefined)
	{
		id=Math.random();
	}
	return id;
}
/********************************************************************************************/
function openUrl(url)
{
	window.location=decodeURIComponent(url);
}
/********************************************************************************************/
var grabNmove =
{
	xPos:0,
	yPos:0,
	run:false,
	start: function(e)
	{
		e=e || window.event;
		if(e.preventDefault) e.preventDefault();
		else e.returnValue=false;
		//document.body.onmousedown=null;

		document.body.style.cursor="move";
		addEvent(document.body,'mouseup',grabNmove.stop);
		grabNmove.xPos=e.clientX;
		grabNmove.yPos=e.clientY;
		grabNmove.run=true;
		document.onmousemove=grabNmove.move;
		
	},
	/*******************************************************************************************/
	move: function(e)
	{
		if(grabNmove.run)
		{
			e=e || window.event;
			var newX=e.clientX;
			var newY=e.clientY;
			var difX=grabNmove.xPos - newX;
			var difY=grabNmove.yPos-newY;
			grabNmove.xPos=newX;
			grabNmove.yPos=newY;
			window.scrollBy(difX,difY);
			grabNmove.run=false;
			setTimeout(function(){grabNmove.run=true;},10);
		}
	},
	
	stop : function()
	{
		document.body.style.cursor="default";
		document.onmousemove=null;
	},
	noScroll:function(element)
	{
		document.body.onmousedown=null;
		element.onmouseout=function(){document.body.onmousedown=grabNmove.start;}
	}
/*******************************************************************************************/
}

/********************************************************************************************/
function cScroller(elementId)
{
	this.elementId=elementId;
	this.interval=false;
	this.scrollTop=0;
	
	/**************************************************/
	this.scrollDown=function()
	{
		var element=getElement(this.elementId);
		var last=element.scrollTop;
		this.scrollTop+=10;
		element.scrollTop=this.scrollTop;
		if(last==element.scrollTop) this.stopScroll();
	}
	/**************************************************/
	this.scrollUp=function()
	{
		var element=getElement(this.elementId);
		var last=element.scrollTop;
		this.scrollTop-=10;
		element.scrollTop=this.scrollTop;
		if(last==element.scrollTop) this.stopScroll();
	}
	/**************************************************/
	this.startScroll=function(direction,speed)
	{
		var that=this;
		this.scrollTop=getElement(this.elementId).scrollTop;
		if(this.interval!==false) this.stopScroll();
		
		switch(direction)
		{
			case "up":
				this.interval=setInterval(function(){that.scrollUp();},speed);
			break;
			case "down":
				this.interval=setInterval(function(){that.scrollDown();},speed);
			break;
			default:
				return false;
			break;
		}
	}
	/**************************************************/
	this.stopScroll=function()
	{
		clearInterval(this.interval);
		this.interval=false;
	}
	
}

/****************************************************/
var cForm =
{
	send:function(formElement,callBack)//Posts the given form , takes it's data from the forms action and method attributes
	{
		var queryString=cForm.get_query_string(formElement);
		var url=jQuery(formElement).attr('action');
		if(URLarguments(url)) queryString = url + "&" + queryString;
		else queryString=url + "?" + queryString;
		
		if(callBack!=undefined)
			jQuery.get(queryString,callBack);
		else 
		{
			jQuery.get(queryString,function(data)
			{
				var error=request_success(data);
				if(error!==false)
				{
					alert(error.response);//Outouts an error-message in error
				}
			});//jQuery.get
		}//else
		
		
		//alert(queryString);
	},
	find:function(element)//finds the parent-form of the given input tag
	{
		if(!jQuery(cur).is('input') || !jQuery(cur).is('select')) return false;
		var cur=element.parentNode;
		while(!jQuery(cur).is('form') || cur===document.body)
		{
			cur=cur.parentNode;
		}
		if(jQuery(cur).is('form')) return cur;
		else return false;
	},
	get_query_string:function(formElement)
	{
		var inputs = jQuery(formElement).find('input,textarea,select');
		var queryString="";
		for(var i=0;i<inputs.length;i++) 
		{
			if(inputs.eq(i).attr('name')!="")
			{
				queryString += inputs.eq(i).attr('name') + "=" + encodeURIComponent(inputs.eq(i).val().replace(/€/gi,'&#8364;')) + "&";
			}
		}
		queryString=queryString.replace(/&$/,'');
		return queryString;
	}
}
/*************************************************/