/*
  Класс для работы с Cookie через javascript.
  Этот класс является базовым для многих javascript - классов
*/



function shCookieClass(){

	// Проверить, поддерживает ли броузер работу с cookies
        // через javascript
	function isCookieSupport(){
		if(typeof(document.cookie) == 'undefined'){
			return false;
		}else{
			return true;
		}
	}
	
       	// Установить значение флага поддержки броузером работы с cookie
        // через javascript.
	var jsCookieSupport = isCookieSupport();
	this.jsCookieSupport = jsCookieSupport;

	// Получить значение ccokie, которое записано по заданному
        // смещению
	function getCookieVal(offset){
	
		var endstr = document.cookie.indexOf(';',offset);
		if(endstr==-1) {endstr=document.cookie.length;};
		return unescape(document.cookie.substring(offset,endstr));
	}


	// Получить значение cookie по имени
	function getCookie(name){

		if(name==''){return null;};
		if(!jsCookieSupport){return null;};
		var arg = name + '=';
		var alen = arg.length;
		var clen = document.cookie.length;
		var i = 0;
		var j;

		while(i < clen){
			j = i + alen;
			if(document.cookie.substring(i,j) == arg){
				return getCookieVal(j);
			}
			i = document.cookie.indexOf(' ',i) + 1;
			if(i == 0) break;
			}
		return null;
	}
	this.getCookie = getCookie;


	// Удалить cookie по имени, path, domain
	function deleteCookie(name,path,domain){
		if(!jsCookieSupport){return false;};	
		if(getCookie(name)){
			expires = new Date(0);
			document.cookie = name + '=' +
			((path) ? '; path=' + path : '') +
			((domain) ? '; domain=' + domain : '') +
			'; expires=' + expires.toGMTString();
			return true;
		}
		return true;
	}
	this.deleteCookie = deleteCookie;


	// Удалить cookie только по имени
	function simpleDeleteCookie(name){
		return deleteCookie(name,'','');
	}
	this.simpleDeleteCookie = simpleDeleteCookie;


	// Сохранить cookie
	// !!! expires - время в секундах, начиная с 1 января 1970.
	function setCookie(name,value,expires,path,domain,secure){
		if(!jsCookieSupport){return false;};
		expires = new Date(expires);
		document.cookie = name + '=' + escape(value) + 
		((expires) ? '; expires=' + expires.toGMTString() : '') +
		((path) ? '; path=' + path : '') + 
		((domain) ? '; domain=' + domain : '') + 
		((secure) ? '; secure' : '');
		return true;
	}
	this.setCookie = setCookie;

	// Установить cookie по заданному имени и значению
        // (в текущем домене. Установить на год)
	function simpleSetCookie(name,value){
		today = new Date();
		expiry = today.getTime() + 365*24*60*60*1000;
		return setCookie(name,value,expiry,'','','');
	}
	this.simpleSetCookie = simpleSetCookie;

} // End of the class
