aCookADS = new Array();
String.prototype.trim = function() {
    s = this;
    var charset = ' \n\r';
    while (charset.indexOf(s.substr(0, 1)) > -1) {
        s = s.substr(1);
    }
    while (charset.indexOf(s.substr(s.length-1, 1)) > -1) {
        s = s.slice(0, -1);
    }
    return s;
}


function readCookie(name) {
    var cookies = document.cookie.split(';');
    var cookie;
    for (var  i = 0; i < cookies.length; i++) {
        cookie = cookies[i].split('=');
        if (cookie[0].trim() == name) {if (cookie[1]!=undefined) return unescape(cookie[1]);}
    }
    return null;
}
/*function readCookie(name) {
    var xname = name + "=";
    var xlen = xname.length;
    var dlen = document.cookie.length;
    var i = 0;
    while(i < dlen) {
        var j = i + xlen;
        if (document.cookie.substring(i, j) == xname)
return getCookieVal(j);
        i = document.cookie.indexOf(" ", j)+1;
        if (i == 0) break;
    }
    return null;
} */

function getCookieVal(n){
    var endstr = document.cookie.indexOf(";", n);
    if (endstr == -1)
        endstr = document.cookie.length;
    return unescape(document.cookie.substring(n, endstr));
}

function deleteAds(name2, value2) {
    if (readCookie(name2) != null) {
        adsCook = readCookie(name2);
        adsCookLen = readCookie(name2).length;
        deleteCookie(name2, '');
	for(i=0; i<adsCookLen; i++) {
		if (adsCook.indexOf(",")!=-1) 
			k = adsCook.indexOf(",");
		else 
			k = adsCookLen;
		adsID = adsCook.substring(0, k);
		if (adsID != value2) {
		 if (adsID !=',')     
		 if (adsID !='')
		    writeCookie(name2, adsID);
		}
		adsCook = adsCook.substring(k+1, adsCookLen);
		adsCookLen = adsCook.length;
		i = adsCook.indexOf(",");
        }
    }
}

function writeCookie(name, value) {
    aCookADS.push(value);
    var expdate = new Date();
    var monthFromNow = expdate.getTime() + (30*24*60*60*1000);
    expdate.setTime(monthFromNow);
    value2 = value; 
    if (readCookie(name) != null) value2 = readCookie(name) + "," + value; 
    var curCookie = name + "=" + escape(value2) + "; path=/ ;expires=" + expdate.toGMTString(); 
//    var curCookie = name + "=" + escape(aCookADS.join(',')) + "; path=/ ;expires=" + expdate.toGMTString(); 
    document.cookie = curCookie;
}

function deleteCookie(name, domain) {
    if (readCookie(name)) {
    document.cookie = name + " = " +
        "; path=/" +
        ((domain)   ? "; domain=" + domain    : "" ) +
        "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function favInc(id_ads) {
	writeCookie('cookAds', id_ads);document.getElementById('div_'+id_ads).className='one_advert blue_back';
	document.getElementById('div_a'+id_ads).style.display='none';
	document.getElementById('div_d'+id_ads).style.display='block';
	document.getElementById('countFav').value++;
	document.getElementById('showCountFav').innerHTML = document.getElementById('countFav').value;
}
function favDec(id_ads) {
	deleteAds('cookAds', id_ads);document.getElementById('div_'+id_ads).className='one_advert';
	document.getElementById('div_a'+id_ads).style.display='block';
	document.getElementById('div_d'+id_ads).style.display='none';
	document.getElementById('countFav').value--;
	document.getElementById('showCountFav').innerHTML = document.getElementById('countFav').value;
}
function favFInc(id_ads) {
	writeCookie('cookAds', id_ads);document.getElementById('div_'+id_ads).className='one_advert_fav';
	document.getElementById('div_a'+id_ads).style.display='none';
	document.getElementById('div_d'+id_ads).style.display='block';
	document.getElementById('countFav').value++;
	document.getElementById('showCountFav').innerHTML = document.getElementById('countFav').value;
}
function favFDec(id_ads) {
	deleteAds('cookAds', id_ads);document.getElementById('div_'+id_ads).className='one_advert_fav gray_back';
	document.getElementById('div_a'+id_ads).style.display='block';
	document.getElementById('div_d'+id_ads).style.display='none';
	document.getElementById('countFav').value--;
	document.getElementById('showCountFav').innerHTML = document.getElementById('countFav').value;
}
function favSInc(id_ads) {
	writeCookie('cookAds', id_ads);document.getElementById('div_'+id_ads).className='single_advert blue_back';
	document.getElementById('div_a'+id_ads).style.display='none';
	document.getElementById('div_d'+id_ads).style.display='block';
	document.getElementById('countFav').value++;
	document.getElementById('showCountFav').innerHTML = document.getElementById('countFav').value;
        document.getElementById('div_'+id_ads+'_c').className='dark_blue_back single_adv_cont';
}
function favSDec(id_ads) {
	deleteAds('cookAds', id_ads);document.getElementById('div_'+id_ads).className='single_advert';
	document.getElementById('div_a'+id_ads).style.display='block';
	document.getElementById('div_d'+id_ads).style.display='none';
	document.getElementById('countFav').value--;
	document.getElementById('showCountFav').innerHTML = document.getElementById('countFav').value;
        document.getElementById('div_'+id_ads+'_c').className='dark_gray_back single_adv_cont';
}
