(function(){

if (typeof createCookie === "undefined") {
createCookie = function (name, value, expires, domain, path) {
    var cExpires = "";
    var cDomain = "";
    var cPath = "";
    if (expires) {
        if (Object.prototype.toString.call(expires) === '[object Date]') {
            cExpires = "; expires=" + expires.toUTCString();
        }
        else {
            var date = new Date();
            date.setTime(date.getTime()+(expires*24*60*60*1000));
            cExpires = "; expires=" + date.toUTCString();
        }
    }
    if (domain) {
        cDomain = "; domain=" + domain;
    }
    if (path) {
        cPath = "; path=" + path;
    }
    else {
        cPath = "; path=/";
    }
    document.cookie = name + "=" + value + cExpires + cPath + cDomain;
};    
}

if (typeof readCookie === "undefined") {
readCookie = function (name) {
    var ca = document.cookie.split(';');
    var nameEQ = name + "=";
    for(var i=0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1, c.length); //delete spaces
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
};
}

/*
function deleteCookie( name, path, domain ) {
    if ( readCookie( name ) ) document.cookie = name + '=' +
        ( ( path ) ? ';path=' + path : '') +
        ( ( domain ) ? ';domain=' + domain : '' ) +
        ';expires=Thu, 01-Jan-1970 00:00:01 GMT';
}
*/

/**
 * createCookieArray takes a name and an Options Object and stores the options
 * inside the key 'values'. A typical options object contains the following
 * parameters:
 *
 * {
 *   'values' : {},             - An Object of the values to be saved.
 *   'days' : 1,                - Amount of days to save the cookie. Default: null
 *   'domain' : 'olx.com.ar     - Domain for wich to save the Cookie. Default: null
 * }
 */
if (typeof createCookieArray === "undefined") {
createCookieArray = function (name, options) {
    var defaults = {
        'values' : [],
        'days'   : null,
        'domain' : null
    };
    for (key in defaults) {
        options[key] = (typeof options[key] == "undefined")?defaults[key]:options[key];
    }

    values = options['values'];
    for (key in values) {
        createCookie(name+'_'+key, values[key], options['days'], options['domain']);
    }
    return true;
};
}
 
/**
 * readCookieArray takes the name of the cookie as the sole argument and returns
 * an Javascript object with the corresponding keys. It uses a regular expression
 * to cycle through the cookies and extract the right variables.
 *
 * Example, for cookies like:
 * cookiename_key1=value1
 * cookiename_key2=value2
 * cookiename_extrakey=newvalue
 *
 * It Returns:
 * {
 *   'key1': 'value1',
 *   'key2': 'value2',
 *   'extrakey': 'newvalue'
 * }
 */
if (typeof readCookieArray === "undefined") {
readCookieArray = function (name) {
    var cookies = document.cookie;
    var obj = {};
    var match = [];
    var expr = name + '_([^=]+)=([^;]+)';
    var regexp = new RegExp(expr, 'ig');
    while (match = regexp.exec(cookies)) {
        obj[match[1]] = unescape(match[2]);
    }
    return obj;
};
}

window.getUtmv = window.getUtmv || function() {
  var u = [], temp;
  if(temp = readCookie('__utmv')) {
    for (var v in (t = temp.split('|')[1].split(','))) {
      u.push(t[v].split('=').slice(1, 3));
    }
    return u;
  }
  return null;
}

})();

