// rigo 9/01/1999 for Netgravity
// tan 889
// modified by nlee

// define frequency limit (1-9)
var frequency = 2;

// read cookie 
var pop_ad_cookie_string = unescape(document.cookie);

// init cookie name and value to null
var pop_ad_cookie_name = "";
var pop_ad_cookie_value = "0";

do 
    {
    // define indices to parse pop_ad_cookie_string into name=value substrings
    end_index = pop_ad_cookie_string.length;
    start_index = pop_ad_cookie_string.lastIndexOf(";");
    
    // get name=value substring
    cookie_sub_string = pop_ad_cookie_string.substring((start_index + 1), end_index);
    
    // get indices to parse cookie_sub_string into name and value
    sub_end_index = cookie_sub_string.length;
    sub_start_index = cookie_sub_string.lastIndexOf("=") + 1;

    // get name and value
    pop_ad_cookie_value = cookie_sub_string.substring(sub_start_index, sub_end_index);
    pop_ad_cookie_name = cookie_sub_string.substring(0, (sub_start_index - 1));
    // document.write("pop_ad_cookie_name =" + pop_ad_cookie_name + "<br>pop_ad_cookie_value = " + pop_ad_cookie_value + "<br><br>");

    // set cookie string to remainder
    pop_ad_cookie_string = pop_ad_cookie_string.substring(0, start_index);

    } while ((start_index > 0) && (pop_ad_cookie_name != " ViewNum"))

// force pop_ad_cookie_value to 1 if unexpected length
// catches undefined condition except where there is another cookie of length=1
// if set should have value (1-9)
if (pop_ad_cookie_value.length != 1) pop_ad_cookie_value = "1";

//convert value to number and increment
pop_ad_numeric_cookie_value = parseInt(pop_ad_cookie_value);
popup_viewed = pop_ad_numeric_cookie_value;
pop_ad_numeric_cookie_value++;

// define cookie
pop_ad_cookie_name = "ViewNum";

// set it to expire in 24 hours
var exp = new Date(); 
exp.setTime(exp.getTime()  + (1 * 24 * 60 * 60 * 1000 * 1));

// set cookie only if needed so expiration date wont be pushed forward each time
if (popup_viewed <= frequency) {
  document.cookie = pop_ad_cookie_name + "=" + pop_ad_numeric_cookie_value + "; expires=" + exp.toGMTString();
}
