﻿function PriceAlertCookie_Detail(data) 
{
    _arr = data.split(":");
    this.ad_id = !_arr[0]?'':_arr[0];
    this.price = !_arr[1]?'':_arr[1];
    this.date = !_arr[2]?'':new Date(_arr[2].substring(0,4),_arr[2].substring(4,6),_arr[2].substring(6,8),_arr[2].substring(8,10),_arr[2].substring(10,12));
    this.isfavorite = !_arr[3]?'':_arr[3];
}

function PriceAlertCookie() 
{
    //init
    this.email = '';
    this.phone = '';
    this.ads = new Array();

    var cookieArray = init_array();
    get_array('PriceAlertForm_' + _userId, cookieArray);
    if (cookieArray.length >= 2)
    {
      this.email = cookieArray[1];      
      this.phone = cookieArray[2]
      this.ads = new Array();
      for(i=3;i<cookieArray.length;i++)
         this.ads[i-3] = new PriceAlertCookie_Detail(cookieArray[i])
      
    }
    
    // getAd
    this.getAd = function (adId) {
        for(i=0;i<this.ads.length;i++)
            if (this.ads[i].ad_id == adId) return this.ads[i];
        return new PriceAlertCookie_Detail('');
    } ;
    
    // setAd
    this.setAd = function (adId,price,myDate,isfavorite) {
        pos = this.ads.length
        for(i=0;i<this.ads.length;i++)
            if (this.ads[i].ad_id == adId) pos = i;
        if (pos == this.ads.length) this.ads[pos] = new PriceAlertCookie_Detail('');
        this.ads[pos].ad_id = adId;
        this.ads[pos].price = price;
        this.ads[pos].date = myDate;
        this.ads[pos].isfavorite = isfavorite;
    } ;
    
    // saveCookie
    this.saveCookie = function (myDate) {
    
        var cookieArray = init_array();
        var maxstoredAds;
        if (navigator.userAgent.indexOf("MSIE")!=-1)
        {
            maxstoredAds = 40;
        } else 
        {
            maxstoredAds = 95;
        }
        get_array('PriceAlertForm_' + _userId, cookieArray);
        cookieArray[1] = $('#frm_priceAlert_Email').val();
        cookieArray[2] = $('#frm_priceAlert_Phone').val();
        var ini = this.ads.length - maxstoredAds < 0?0:this.ads.length - maxstoredAds;
        var fin = this.ads.length;
        for(i=ini;i<fin;i++)
        {
            if (this.ads[i])
            {
                cookieArray[i-ini+3] = this.ads[i].ad_id + ':';
                cookieArray[i-ini+3] += this.ads[i].price + ':' ;
                cookieArray[i-ini+3] += this.ads[i].date.getFullYear();
                cookieArray[i-ini+3] += Right("0" + this.ads[i].date.getMonth(),2);
                cookieArray[i-ini+3] += Right("0" + this.ads[i].date.getDate(),2);
                cookieArray[i-ini+3] += Right("0" + this.ads[i].date.getHours(),2);
                cookieArray[i-ini+3] += Right("0" + this.ads[i].date.getMinutes(),2) + ':';
                cookieArray[i-ini+3] += this.ads[i].isfavorite;
            }
        }
        set_array('PriceAlertForm_' + _userId, cookieArray, myDate);
    } ;
    
}


function SetPriceAlertCookie(price)
{
    var expirationDate = new Date();
    var isfavorite;
    if (_userId == 0) 
    {
        expirationDate.setMinutes(expirationDate.getMinutes() + 30);
        isfavorite = 0;
    }
    else
    {
        expirationDate.setFullYear(expirationDate.getFullYear() + 1);
        isfavorite = 1;
    }
    myPriceAlertCookie.setAd(_adId,price,new Date(),isfavorite);
    myPriceAlertCookie.saveCookie(expirationDate);
}

function GetPriceAlertCookie()
{
    if (myPriceAlertCookie.email != '') $('#frm_priceAlert_Email').val(myPriceAlertCookie.email);
    if (myPriceAlertCookie.phone != '') $('#frm_priceAlert_Phone').val(myPriceAlertCookie.phone);
    var pAd=new PriceAlertCookie_Detail('');
    pAd = myPriceAlertCookie.getAd(_adId);
    if (pAd.ad_id)
    {
        $('#price-alert-result').html($('#price-alert-result').html().replace('$PRICE$',addSeparators(pAd.price)));
        $('#price-alert-result').html($('#price-alert-result').html().replace('$DAY$', Right("0" + pAd.date.getDate(), 2)));
        $('#price-alert-result').html($('#price-alert-result').html().replace('$MONTH$', Right("0" + (pAd.date.getMonth() + 1), 2)));
        $('#price-alert-result').html($('#price-alert-result').html().replace('$YEAR$', pAd.date.getFullYear()));
        $('#price-alert-result').html($('#price-alert-result').html().replace('$HOUR$', Right("0" + pAd.date.getHours(), 2)));
        $('#price-alert-result').html($('#price-alert-result').html().replace('$MINUTE$', Right("0" + pAd.date.getMinutes(), 2)));            
        if (pAd.isfavorite==0) $('#price-alert-savedfavorites').css("display", 'none');
        $('#price-alert-title-modify').css("display", 'block');
        $('#price-alert-title').css("display", 'none');
        $('#price-alert-result').css("display", 'block');
        //$('price-alert-space').css("display", 'block');
        if (_isProfessional=='True') 
        {
            $('#price-alert-request').css("display", 'none'); 
            //$('price-alert-space').css("display", 'none');
        }
        $('#frm_priceAlert_DesiredPrice').val(pAd.price);
        $('#price-alert-money-info').css("display", 'none');
    }
}

function addSeparators(val)
{
    val = reverse(val);
    val = val.replace(/(\d{3})/g, "$1.");
    val = reverse(val);
    val = val.replace(/^(-)?\./, "$1");
    return val
}

function reverse(str)
{
    var res = "";
    for (var i = str.length; i > 0; --i) {
        res += str.charAt(i - 1);
    }
    return res;
}    


