var url_prefix="http://causehive.com";

var url_php_prefix="http://causehive.com";

var useCDN=true;
if (useCDN){
    url_prefix = 'http://i.causehive.com';
}

var petition_div_html='<div style="margin-top: 20px">\
<div id="actiontag_outer_div" style="z-index:-1;position:absolute;top:0;left:0;width:100%;height:3000px;background-color:#000;filter:alpha(opacity=90);-moz-opacity:0.9;opacity:0.9;"><\/div>\
<div id="actiontag_form_div" style="position:relative;background-image:url(\'_URL_\/static\/bg.png\');height:687px;width:702px;margin-left:auto;margin-right:auto;color:#808080;">\
<div style="float:right;clear:both;cursor:pointer;height:70px;width:70px;" onclick="closePetition();"><\/div>\
<form style="clear:both;" action="">\
<div style="font-family:Arial;font-size:16px;margin-top:-15px;">\
<div style="float:left;width:310px;height:600px;margin:-30px 20px 10px 20px;font-family:Helvetica,Arial,sans-serif;font-weight:bold;">E-mail your Representative*:<br\/>\
<div style="margin:0;padding:0;"><div id="petition_letter_greeting" style="background:white;border:0;margin:0;padding:0;width:320px;font-size:14px;font-family:Lucida Grande;color:black;">Dear Representative,<\/div><\/div>\
<div><textarea id="petition_letter" name="petition_letter" style="border:0;width:320px;height:550px;font-size:13px; white-space:pre; overflow:auto;"><\/textarea><\/div>\
<div><img style="cursor:pointer;" onclick="resetLetter();" title="Revert changes" src="_URL_\/static\/revertChanges.jpg"\/><\/div><\/div>\
<div style="float:left;width:310px;height:600px;margin:-30px 20px 10px 20px;font-family:Helvetica,Arial,sans-serif;font-weight:bold;padding:0;">\
TITLE:&nbsp;<select id="seiu_title"><option>Mr<\/option><option>Mrs<\/option><option>Ms<\/option><option>Jr<\/option><\/select><br\/><br\/>\
FIRST NAME*:<br\/>\
<div><input id="seiu_first_name" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" type="text" \/><\/div>\
LAST NAME:<br\/>\
<div><input id="seiu_last_name" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" type="text" \/><\/div>\
EMAIL*:<br\/>\
<div><input id="email1" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" name="email1" id="email1" type="text" \/><\/div>\
ZIP*:<br\/>\
<div><div style="float:left;"><input onchange="updateReprName()" id="zip_code" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:60px;margin-bottom:0px; float:left;" type="text" \/><\/div><div style="float:left;" id="zip4_select"><\/div><\/div>\
<div style="clear:both;">I accept the <a href="_URL_/static/terms.xhtml" target="_blank">term and conditions<\/a>*:&nbsp;<input id="terms_conditions" type="checkbox"\/><br\/>\
Remember me:&nbsp;<input id="seiu_remember" type="checkbox" \/><\/div>\
<img style="cursor:pointer;" onclick="return doSubmit();" title="Send now" src="_URL_\/static\/send_now_button.gif" \/>&nbsp;\
<img style="cursor:pointer;" onclick="return closePetition();" title="Send later" src="_URL_\/static\/send_later_button.gif" \/>\
<img style="margin: 0px 0px 0px 0px;" src="_URL_\/static\/sponsor_button-kp_v3.png" \/>\
<\/div><div style="text-align: right; margin-right: 30px; font-size: 14px;">Pressflex LLC service sponsored by the SEIU.<\/div>\
<\/div><\/form>\
<\/div><\/div>';

var real_petition_div_html='<div style="margin-top: 20px">\
<div id="actiontag_outer_div" style="z-index:-1;position:absolute;top:0;left:0;width:100%;height:3000px;background-color:#000;filter:alpha(opacity=90);-moz-opacity:0.9;opacity:0.9;"><\/div>\
<div id="actiontag_form_div" style="position:relative;background-image:url(\'_URL_\/static\/bg.png\');height:687px;width:702px;margin-left:auto;margin-right:auto;color:#808080;">\
<div style="float:right;clear:both;cursor:pointer;height:70px;width:70px;" onclick="closePetition();"><\/div>\
<form style="clear:both;" action="">\
<div style="float:left;width:310px;height:600px;margin:-30px 20px 10px 20px;font-family:Helvetica,Arial,sans-serif;font-weight:bold;"><div style="font-family:Arial;font-size:16px;margin-top:-15px;" id="header"><br\/></div>\
<div style="margin:0;padding:0;"><div id="petition_letter_greeting" style="background:white;border:0;margin:0;padding:0;width:320px;font-size:14px;font-family:Lucida Grande;color:black;"><\/div><\/div>\
<div><textarea id="petition_letter" name="petition_letter" style="border:0;width:320px;height:550px;font-size:13px; white-space:pre; overflow:auto;"><\/textarea><\/div>\
<div><img style="cursor:pointer;" onclick="resetLetter();" title="Revert changes" src="_URL_\/static\/revertChanges.jpg"\/><\/div><\/div>\
<div style="float:left;width:310px;height:600px;margin:-30px 20px 10px 20px;font-family:Helvetica,Arial,sans-serif;font-weight:bold;padding:0;">\
TITLE:&nbsp;<select id="seiu_title"><option>Mr<\/option><option>Mrs<\/option><option>Ms<\/option><option>Jr<\/option><\/select><br\/><br\/>\
FIRST NAME*:<br\/>\
<div><input id="seiu_first_name" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" type="text" \/><\/div>\
LAST NAME:<br\/>\
<div><input id="seiu_last_name" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" type="text" \/><\/div>\
EMAIL*:<br\/>\
<div><input id="email1" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" name="email1" id="email1" type="text" \/><\/div>\
ZIP*:<br\/>\
<div><div style="float:left;"><input onchange="updateReprName()" id="zip_code" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:60px;margin-bottom:0px; float:left;" type="text" \/><\/div><div style="float:left;" id="zip4_select"><\/div><\/div>\
<div style="clear:both;">I accept the <a href="_URL_/static/terms.xhtml" target="_blank">term and conditions<\/a>*:&nbsp;<input id="terms_conditions" type="checkbox"\/><br\/>\
Remember me:&nbsp;<input id="seiu_remember" type="checkbox" \/><\/div>\
<img style="cursor:pointer;" onclick="return doSubmit();" title="Send now" src="_URL_\/static\/send_now_button.gif" \/>&nbsp;\
<img style="cursor:pointer;" onclick="return closePetition();" title="Send later" src="_URL_\/static\/send_later_button.gif" \/>\
<img style="margin: 0px 0px 0px 0px;" src="_URL_\/static\/sponsor_button-kp_v3.png" \/>\
<\/div><div style="text-align: right; margin-right: 30px; font-size: 14px;">Pressflex LLC service sponsored by the SEIU.<\/div>\
<\/div><\/form>\
<\/div><\/div>';



var checkbox_div_html='<div style="margin-top: 20px">\
<div id="actiontag_outer_div" style="z-index:-1;position:absolute;top:0;left:0;width:100%;height:3000px;background-color:#000;filter:alpha(opacity=90);-moz-opacity:0.9;opacity:0.9;"><\/div>\
<div id="actiontag_form_div" style="position:relative;background-image:url(\'_URL_\/static\/bg.png\');height:687px;width:702px;margin-left:auto;margin-right:auto;color:#808080;">\
<div style="float:right;clear:both;cursor:pointer;height:70px;width:70px;" onclick="closePetition();"><\/div>\
<form style="clear:both;" action="">\
<div style="font-family:Arial;font-size:16px;margin-top:-15px;">\
<div style="float:left;width:310px;height:600px;margin:-30px 20px 10px 20px;font-family:Helvetica,Arial,sans-serif;font-weight:bold;">\
<div style="margin:0;padding:0;"><\/div>\
<div id="petition_letter" name="petition_letter" style="overflow:auto;border:0;width:320px;height:620px;font-size:34px; line-height:1;"><\/div>\
<\/div>\
<div style="float:left;width:310px;height:600px;margin:-30px 20px 10px 20px;font-family:Helvetica,Arial,sans-serif;font-weight:bold;padding:0;">\
TITLE:&nbsp;<select id="seiu_title"><option>Mr<\/option><option>Mrs<\/option><option>Ms<\/option><option>Jr<\/option><\/select><br\/><br\/>\
FIRST NAME*:<br\/>\
<div><input id="seiu_first_name" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" type="text" \/><\/div>\
LAST NAME:<br\/>\
<div><input id="seiu_last_name" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" type="text" \/><\/div>\
EMAIL*:<br\/>\
<div><input id="email1" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" name="email1" id="email1" type="text" \/><\/div>\
ZIP*:<br\/>\
<div><div style="float:left;"><input onchange="updateReprName()" id="zip_code" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:60px;margin-bottom:0px; float:left;" type="text" \/><\/div><div style="float:left;" id="zip4_select"><\/div><\/div>\
<div style="clear:both;">I accept the <a href="_URL_/static/terms.xhtml" target="_blank">term and conditions<\/a>*:&nbsp;<input id="terms_conditions" type="checkbox"\/><br\/>\
Remember me:&nbsp;<input id="seiu_remember" type="checkbox" \/><\/div>\
<img style="cursor:pointer;" onclick="return doSubmit();" title="Send now" src="_URL_\/static\/sign_me_up_button.gif" \/>&nbsp;\
<img style="cursor:pointer;" onclick="return closePetition();" title="Send later" src="_URL_\/static\/cancel_button.gif" \/>\
<img style="margin: 0px 0px 0px 0px;" src="_URL_\/static\/sponsor_button-kp_v3.png" \/>\
<\/div><div style="text-align: right; margin-right: 30px; font-size: 14px;">Pressflex LLC service sponsored by the SEIU.<\/div>\
<\/div><\/form>\
<\/div><\/div>';



var mix_div_html='<div style="margin-top: 20px">\
<div id="actiontag_outer_div" style="z-index:-1;position:absolute;top:0;left:0;width:100%;height:3000px;background-color:#000;filter:alpha(opacity=90);-moz-opacity:0.9;opacity:0.9;"><\/div>\
<div id="actiontag_form_div" style="position:relative;background-image:url(\'_URL_\/static\/bg.png\');height:687px;width:702px;margin-left:auto;margin-right:auto;color:#808080;">\
<div style="float:right;clear:both;cursor:pointer;height:70px;width:70px;" onclick="closePetition();"><\/div>\
<form style="clear:both;" action="">\
<div style="font-family:Arial;font-size:16px;margin-top:-15px;">\
<div style="float:left;width:310px;height:600px;margin:-30px 20px 10px 20px;font-family:Helvetica,Arial,sans-serif;font-weight:bold;">\
<div style="margin:0;padding:0;"><\/div>\
<div id="static_text" name="static_text" style="overflow:auto;border:0;width:320px;height:620px;font-size:34px; line-height:1;"><\/div>\
<\/div>\
<div style="float:left;width:310px;height:600px;margin:-30px 20px 25px 20px;font-family:Helvetica,Arial,sans-serif;font-weight:bold;padding:0;">\
<div style="padding-top:20px"><textarea id="petition_letter" name="petition_letter" style="border:0;width:320px;height:220px;font-size:13px; white-space:pre; overflow:auto;"><\/textarea><\/div>\
TITLE:&nbsp;<select id="seiu_title"><option>Mr<\/option><option>Mrs<\/option><option>Ms<\/option><option>Jr<\/option><\/select><br\/><br\/>\
FIRST NAME*:<br\/>\
<div><input id="seiu_first_name" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" type="text" \/><\/div>\
LAST NAME:<br\/>\
<div><input id="seiu_last_name" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" type="text" \/><\/div>\
EMAIL*:<br\/>\
<div><input id="email1" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" name="email1" id="email1" type="text" \/><\/div>\
ZIP*:<br\/>\
<div><div style="float:left;"><input onchange="updateReprName()" id="zip_code" style="background:white url(_URL_\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:60px;margin-bottom:0px; float:left;" type="text" \/><\/div><div style="float:left;" id="zip4_select"><\/div><\/div>\
<div style="clear:both;">I accept the <a href="_URL_/static/terms.xhtml" target="_blank">term and conditions<\/a>*:&nbsp;<input id="terms_conditions" type="checkbox"\/><br\/>\
Remember me:&nbsp;<input id="seiu_remember" type="checkbox" \/><\/div>\
<img style="cursor:pointer;" onclick="return doSubmit();" title="Send now" src="_URL_\/static\/sign_me_up_button.gif" \/>&nbsp;\
<img style="cursor:pointer;" onclick="return closePetition();" title="Send later" src="_URL_\/static\/cancel_button.gif" \/>\
<img style="margin:-6px 0px -6px 77px;" src="_URL_\/static\/sponsor_button-small2.png" \/>\
<\/div><div style="text-align: right; margin-right: 30px; font-size: 14px;">Pressflex LLC service sponsored by the SEIU.<\/div>\
<\/div><\/form>\
<\/div><\/div>';

var ty='<div style="margin-top: 20px">\
<div id="actiontag_outer_div" style="z-index:-1;position:absolute;top:0;left:0;width:100%;height:3000px;background-color:#000;filter:alpha(opacity=90);-moz-opacity:0.9;opacity:0.9;"><\/div>\
<div id="actiontag_form_div" style="position:relative; background:transparent url(\'_URL_\/static\/ty_bg.png\') no-repeat scroll center top; height:206px; margin: 0 auto; width:406px;">\
<div id="thankYouClose" style="height:47px; left:346px; position:relative; top:-10px;width:47px;">\
<div onClick="close_thankyou()" style="cursor: pointer; background: transparent url(\'_URL_\/static\/ty_close.png\') no-repeat top center; width: 47px; height:47px; display: block;"><\/div><\/div>\
<div id="pop_content" style="margin:0px 15px; font-family:Arial, Helvetica, sans-serif; font-size:12px; color: #808080; text-align: center;"><h2 style="font-size: 14pt ; font-weight: bold; ">Thank you for getting involved!</h2><br/> Visit <a href="www.seiu.org">www.seiu.org</a> for more information.<br/>Or close this window to return DailyKos.</div>\
<\/div><\/div>';


var new_sub = checkbox_div_html.replace(/_URL_/g, url_prefix);
checkbox_div_html = new_sub;

var new_sub = petition_div_html.replace(/_URL_/g, url_prefix);
petition_div_html = new_sub;

var new_sub = mix_div_html.replace(/_URL_/g, url_prefix);
mix_div_html = new_sub;

var new_sub = real_petition_div_html.replace(/_URL_/g, url_prefix);
real_petition_div_html = new_sub;

var new_sub = ty.replace(/_URL_/g, url_prefix);
ty = new_sub;

var checkbox_div_html='<div style="margin-top: 20px">\
<div id="actiontag_outer_div" style="z-index:-1;position:absolute;top:0;left:0;width:100%;height:3000px;background-color:#000;filter:alpha(opacity=90);-moz-opacity:0.9;opacity:0.9;"><\/div>\
<div id="actiontag_form_div" style="position:relative;background:url(\'http:\/\/causehive.com\/static\/bg.png\') no-repeat center top;height:687px;width:702px;margin-left:auto;margin-right:auto;color:#808080;">\
<div style="float:right;clear:both;cursor:pointer;height:70px;width:70px;" onclick="closePetition();"><\/div>\
<form style="clear:both;" action="">\
<div style="font-family:Arial;font-size:16px;margin-top:-15px;">\
<div style=" display: inline; float:left;width:320px !important; height:600px;margin:-30px 10px 10px 20px;font-family:Helvetica,Arial,sans-serif;font-weight:bold;">\
<div id="petition_letter" name="petition_letter" style="overflow:auto;border:0;width:320px !important; height:620px;font-size:34px; line-height:1;"><\/div>\
<\/div>\
<div style="display: inline; float:left;width:310px !important;height:600px;margin:-30px 20px 10px 20px; _margin: -30px 0px 10px 25px;font-family:Helvetica,Arial,sans-serif;font-weight:bold;padding:0; text-align: left;">\
TITLE:&nbsp;<select id="seiu_title"><option>Mr<\/option><option>Mrs<\/option><option>Ms<\/option><option>Jr<\/option><\/select><br\/><br\/>\
FIRST NAME*:<br\/>\
<div><input id="seiu_first_name" style="background:white url(http:\/\/causehive.com\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" type="text" \/><\/div>\
LAST NAME:<br\/>\
<div><input id="seiu_last_name" style="background:white url(http:\/\/causehive.com\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" type="text" \/><\/div>\
EMAIL*:<br\/>\
<div><input id="email1" style="background:white url(http:\/\/causehive.com\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:240px;margin:0 0 15px 0;" name="email1" id="email1" type="text" \/><\/div>\
ZIP*:<br\/>\
<div><div style="float:left;"><input onchange="updateReprName()" id="zip_code" style="background:white url(http:\/\/causehive.com\/static\/bg_inputTextField.gif) repeat-x scroll right top;width:60px;margin-bottom:0px; float:left;" type="text" \/><\/div><div style="float:left;" id="zip4_select"><\/div><\/div>\
<div style="clear:both;">I accept the <a href="http://causehive.com/static/terms.xhtml" target="_blank">term and conditions<\/a>*:&nbsp;<input id="terms_conditions" type="checkbox"\/><br\/>\
Remember me:&nbsp;<input id="seiu_remember" type="checkbox" \/><\/div>\
<img style="cursor:pointer;" onclick="return doSubmit();" title="Send now" src="http:\/\/causehive.com\/static\/sign_me_up_button.gif" \/>&nbsp;\
<img style="cursor:pointer;" onclick="return closePetition();" title="Send later" src="http:\/\/causehive.com\/static\/cancel_button.gif" \/>\
<img style="margin: 0px 0px 0px 0px;" src="http:\/\/causehive.com\/static\/sponsor_button-kp_v3.png" \/>\
<\/div><div style="text-align: right; margin-right: 30px; font-size: 14px;">Pressflex LLC service sponsored by the SEIU.<\/div>\
<\/div><\/form>\
<\/div><\/div>';

var defaultletter = '';
var petitionopen = false;
var petitioninitialized = false;

var camp_datas;
var completed_actions;
var ty_msg;

var seiu_form_fields = ['seiu_title','seiu_first_name','seiu_last_name','email1','zip_code','zip4_select','seiu_remember','petition_letter'];
var zip4_state_hack = '';
var mode = '';
var action_type = '';
var opened_bug;

function createCookie(name,value,days) {
    if (days){
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
        document.cookie = name+"="+value+expires+"; path=/";
    }

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

function eraseCookie(name) {
createCookie(name,"",-1);
}

function saveElement(elem, value, days){
    createCookie(elem, escape(value), days);
}

function getElement(elem){
    var ret = readCookie(elem);
    if (ret.length >= 1) {
            return unescape(ret[0]);
    }
}

function getBySov(data, random)
{
    sumSov = 0;
    var intervals = new Array();
    for (var k=0; k<data.length; k++)
    {
        sumSov = sumSov + parseInt(data[k][3]);
    }
    for (var i=0; i<data.length; i++)
    {
        if (data.length == 1)
        {
            return data[0];
        }
            if ( intervals[i-1] != undefined )
            {
                if ( random >= intervals[i-1] && random <= (intervals[i-1]+Math.round(data[i][3]/sumSov*100)))
                {
                    return data[i];
                }
                else
                {
                    intervals[i]=intervals[i-1]+Math.round(data[i][3]/sumSov*100);
                }
            }
            else
            {
                if ( random <= Math.round(data[i][3]/sumSov*100))
                {
                return data[i];
                }
                else
                {
                    intervals[i] = Math.round(data[i][3]/sumSov*100);
                }
            }
    }
    return undefined;
}

function getByOrder(data, previous)
{
    if (data.length > 0){
        data.sort(function (a, b){return a[2] - b[2]});
        if (previous < 0 || previous == undefined){
            return data[0];
        }
        else{
            for (var k=0; k<data.length; k++){
                if (data[k][0] == previous){
                    var m = (k + 1) % data.length;
                    return data[m];
                }
            }
        }
    }
    return data[0];
}

if (!Array.prototype.filter)
{
    Array.prototype.filter = function(fun /*, thisp */)
    {
        var len = this.length;
        if (typeof(fun) != "function") throw new TypeError();

        var res = new Array();
        var thisp = arguments[1];

        for(var i = 0; i < len; ++i)
        {
            if (i in this)
            {
            var val = this[i];
            if (fun.call(thisp, val, i, this)) res.push(val);
            }
        }

        return res;
    };
}

function cleanCallBack(element, index, array)
{
    for (var i=0; i<this.length; i++)
    {
        if (element[0] == this[i])
        {
            return false;
        }
    }
    return true;
}

function cleanData(data, cleanOutIds)
{
    if (cleanOutIds.length > 0){
        for (var i=0; i<data.length; i++)
        {
            data[i][4] = data[i][4].filter(cleanCallBack, cleanOutIds);
            if ( data[i][4].length < 1 )
                {
                    data.splice(i, 1);
                }
        }
    }
    return data
}

function getCampaignDatas(campgroup, completed_actions, prev_actiontag, prev_action) {
    var campaign = new Array();
    var action = new Array();
    var action_tag = new Array();
    var tweet_tag = new Array();

    var random = Math.round(Math.random() * 100);

    cleanData(campgroup, completed_actions);
    if (campgroup[0] == undefined){
        return undefined;
    }
    if ( campgroup[0][3] > 0 )
    {
        campaign = getBySov(campgroup, random);
    }
    else
    {
        campaign = getByOrder(campgroup);
    }

    //if ( campaign[4].length > 0 && campaign[5].length > 0)
    if ( campaign[4].length > 0)
    {
        if ( campaign[4][0][3] > 0 )
        {
            random = Math.round(Math.random() * 100);
            action = getBySov(campaign[4], random);
        }
        else
        {
            action = getByOrder(campaign[4], prev_action);
        }
        if (action[5] == 'tweet'){
            if ( campaign[6][0][3] > 0 )
            {
                random = Math.round(Math.random() * 100);
                tweet_tag = getBySov(action[7], random);
            }
            else
            {
                tweet_tag = getByOrder(campaign[6], prev_actiontag);
            }
        }
        else{
            if ( campaign[5][0][3] > 0 )
            {
                random = Math.round(Math.random() * 100);
                action_tag = getBySov(campaign[5], random);
            }
            else
            {
                action_tag = getByOrder(campaign[5], prev_actiontag);
            }
        }
        return new Array(action[0], action[4], action_tag[0], action_tag[4], action[5], action[6], action[7], action[8], tweet_tag[0], tweet_tag[4]);
    }
    else
    {
        return undefined;
    }
}

function hideFlash(){
    var f = document.getElementsByTagName("object");
    for(var i=0; i<f.length; i++){
       f[i].style.visibility='hidden';
    }
    f = document.getElementsByTagName("iframe");
    for(var i=0; i<f.length; i++){
       f[i].style.visibility='hidden';
    }
    f = document.getElementsByTagName("embed");
    for(var i=0; i<f.length; i++){
       f[i].style.visibility='hidden';
    }
}

function showFlash(){
    var f = document.getElementsByTagName("object");
    for(var i=0; i<f.length; i++){
       f[i].style.visibility='visible';
    }
    f = document.getElementsByTagName("iframe");
    for(var i=0; i<f.length; i++){
       f[i].style.visibility='visible';
    }
    f = document.getElementsByTagName("embed");
    for(var i=0; i<f.length; i++){
       f[i].style.visibility='visible';
    }
}

function limitChars(textarea, limit, infodiv)
{
        var text = textarea.value;
        var textlength = text.length;
        var info = document.getElementById(infodiv);
        if (info == null || info == undefined){
            return false;
        }

        if(textlength > limit)
        {
            info.innerHTML = '0';
            textarea.value = text.substr(0,limit);
            return false;
    }
        else
        {
            info.innerHTML = limit - textlength ;
            return true;
    }
}


var camp_datas_container = new Array();

function init_actiontag() {
    var counter = -1;
    var spans = document.getElementsByTagName('span');
    if(spans.length==0) {
        return;
    }
    for(var j=0;j<spans.length;j=j+1) {
        if(spans[j].id.substring(0,9) == 'tagLinks-' || spans[j].id == 'tagLinks') {
            var tagelems = spans[j].getElementsByTagName('a');
            var node = null;
            var gmode = '';
            for(var i=0;i<tagelems.length;i=i+1) {
                if (gmode == ''){
                    for (var b=0; b<variable_keys.length; b++){
                        var v=eval(variable_keys[b]+'_tags').length;
                        var tgs=eval(variable_keys[b]+'_tags');
                        for (var k=0; k<v;k++){
                            if(tagelems[i].innerHTML.toLowerCase() == tgs[k].toLowerCase()){
                                if (spans[j].id.substring(0,9) == 'tagLinks-') {
                                    node = tagelems[i].parentNode;
                                }
                                else {
                                    node = tagelems[i].parentNode.parentNode;
                                }
                                gmode=variable_keys[b];
                                break;
                            }
                        }
                    }
                }
                else {
                    break;
                }
            }
            if(gmode=='') {
                continue;
            }
            completed_actions = readCookie('completed_actions');
            var cg = eval(gmode);
            camp_datas = getCampaignDatas(cg, completed_actions, parseInt(readCookie('last_action_tag')), parseInt(readCookie('last_action')));
            camp_datas_container.push(camp_datas);
            counter = counter + 1;
            if (camp_datas == undefined) {
                continue;
            }
            defaultletter = camp_datas[1];
            if(node!=null) {
                if (camp_datas[4] == 'tweet'){

                    var TAG_ID = 'tweettag-'+counter.toString();
                    var TEXT_ID = 'tweetthis-'+counter.toString();
                    var BUTTON_ID = 'tweetthisbutton-'+counter.toString();
                    var INFO_ID = 'tweetinfo-'+counter.toString();

                    TWEET_TAG=camp_datas[9]+'?tweettag=1&timestamp='+(100000000*Math.random()).toString();
                    TWEET_TEXT = camp_datas[1];
                    TWEET_BUTTON =  url_prefix + '/site_media/uploads/' + camp_datas[5] +'?tweet=1&timestamp='+(100000000*Math.random()).toString();

                    newNode = document.createElement('p');
                    newNode.id='adv-'+counter.toString();
                    newNode.innerHTML = 'Advertisement';
                    newNode.style.fontSize = '9px';
                    newNode.style.fontFamily = 'Verdana, Arial';
                    newNode.style.color = '#999';
                    newNode.style.border = '0';
                    newNode.style.margin = '0';
                    node.parentNode.insertBefore(newNode, node);
                    
                    newNode = document.createElement('div');
                    var nodeId = 'tweetdiv-' + counter.toString();
                    newNode.id = nodeId;
                    newNode.innerHTML=camp_datas[7].replace(/__TWEET_TAG__/g, TWEET_TAG).replace(/__TWEET_TEXT__/g,TWEET_TEXT).replace(/__TWEET_BUTTON__/g, TWEET_BUTTON).replace(/__TAG_ID__/g, TAG_ID).replace(/__TEXT_ID__/g, TEXT_ID).replace(/__BUTTON_ID__/g, BUTTON_ID).replace(/__INFO_ID__/g, INFO_ID);
                    node.parentNode.insertBefore(newNode, node);
                    limitChars(document.getElementById('tweetthis-'+counter.toString()), 140, INFO_ID)
                    document.getElementById(TEXT_ID).onkeyup=function(){limitChars(this, 140, 'tweetinfo-'+this.id.split('-')[1]);};
                    var nodeId = 'tweetthisbutton-' + counter.toString();
                    var n = document.getElementById(nodeId);
                    if (n == null || n == undefined){
                        continue;
                    }
                    n.onclick = function(){
                    var head = document.getElementsByTagName('head')[0];
                    var script = document.createElement('script');
                    script.type = 'text/javascript';
                    script.src = url_php_prefix+'/php/click.php?'+'tweet_action_id='+camp_datas[0];
                    head.appendChild(script);
                    var sourceNode = 'tweetthis-'+this.id.split('-')[1];
                    var str = escape(document.getElementById(sourceNode).value);
                    window.open('http://twitter.com/home?status='+str);
                    };

                    /*newNode = document.createElement('img');
                    var nodeId = 'tweettag-' + counter.toString();
                    newNode.id = nodeId;
                    newNode.src = camp_datas[9]+'?timestamp='+(100000000*Math.random()).toString();
                    node.parentNode.insertBefore(newNode, node);
                    newNode = document.createElement('textarea');
                    //var nodeId = 'bug-' + counter.toString();
                    newNode.id = 'tweetthis-'+counter.toString();
                    newNode.innerHTML = camp_datas[1];
                    node.parentNode.insertBefore(newNode, node);

                    newNode = document.createElement('img');
                    var nodeId = 'tweetthisbutton-' + counter.toString();
                    newNode.id = nodeId;
                    newNode.src = url_prefix + '/site_media/uploads/' + camp_datas[5] +'?tweet=1&timestamp='+(100000000*Math.random()).toString();
                    newNode.style.cursor = "pointer";
                    node.parentNode.insertBefore(newNode, node);
                    var n = document.getElementById(nodeId);
                    n.onclick = function(){
                    var head = document.getElementsByTagName('head')[0];
                    var script = document.createElement('script');
                    script.type = 'text/javascript';
                    script.src = url_prefix+'/php/click.php?'+'tweet_action_id='+camp_datas[0];
                    head.appendChild(script);
                    var sourceNode = 'tweetthis-'+this.id.split('-')[1];
                    var str = document.getElementById(sourceNode).value;
                    window.open('http://twitter.com/home?status='+escape(str));
                    };*/
                }
                else {
                    var newNode = document.createElement('div');
                    newNode.id = 'petition_div';
                    newNode.name = 'petition_div';
                    var bodyNode = document.getElementsByTagName('body')[0];
                    if(document.getElementById('petition_div') == null) {
                        bodyNode.appendChild(newNode);
                    }

                    newNode = document.createElement('p');
                    newNode.id='adv-'+counter.toString();
                    newNode.innerHTML = 'Advertisement';
                    newNode.style.fontSize = '9px';
                    newNode.style.fontFamily = 'Verdana, Arial';
                    newNode.style.color = '#999';
                    newNode.style.border = '0';
                    newNode.style.margin = '0';
                    node.parentNode.insertBefore(newNode, node);

                    newNode = document.createElement('img');
                    var nodeId = 'bug-' + counter.toString();
                    newNode.id = nodeId;
                    newNode.src = camp_datas[3]+'?timestamp='+(100000000*Math.random()).toString();
                    createCookie('last_action_tag', camp_datas[2], 30)
                    createCookie('last_action', camp_datas[0], 30)
                    node.parentNode.insertBefore(newNode, node);
                    var bug = document.getElementById(nodeId);
                    bug.style.cursor = "pointer";
                    bug.onclick = function(){  openPetition(this); };
                    action_type = camp_datas[4];
                }
            }
        }
    }
}

function scroll(){
    var divobj = document.getElementById('petition_div');
    var outercontainer = document.getElementById('actiontag_outer_div');
    var formdiv = document.getElementById('actiontag_form_div');
    var bodyNode = document.getElementsByTagName('body')[0];
    var scrollto = document.body.scrollTop;

    if(scrollto == 0) {
        if(window.pageYOffset!=undefined) scrollto = window.pageYOffset;
        else {
            if (document.documentElement && document.documentElement.scrollTop) scrollto = document.documentElement.scrollTop;
        }
    }

    outercontainer.style.height = "19999px"; //bodyNode.scrollHeight + 'px';
    divobj.style.position = "absolute";
    divobj.style.zIndex = "999999999";
    divobj.style.top = "0px";
    divobj.style.left = "0px";
    divobj.style.width = "100%";
    divobj.style.height = "100%";
    divobj.style.display = "block";
    formdiv.style.top = "" + scrollto + "px"
}
var r_id;

function openPetition(obj) {
    hideFlash();
    serial = obj.id.substring(4, obj.id.length);
    opened_bug = serial;
    camp_datas = camp_datas_container[serial];
    defaultletter = camp_datas[1];
    actiontag_id = camp_datas_container[serial][2];
    action_id = camp_datas_container[serial][0];
    actiontag_random = obj.src.substring(obj.src.search(/=/)+1);
    r_id=actiontag_random;
    xsHelper(url_php_prefix+'/php/click.php','action_tag_id='+actiontag_id+'&action_id='+action_id+'&random='+r_id+'&actiontag_random='+actiontag_random,'');
    type = camp_datas_container[serial][4];
    petitonletter = camp_datas_container[serial][1];

    var petition_div = document.getElementById('petition_div');
    if(false && petitioninitialized) { scroll(); petition_div.style.display = "block"; }
    else {
    if (type == "letter"){
        petition_div.innerHTML = petition_div_html;
    }
    else if ( type == "petition"){
        petition_div.innerHTML = real_petition_div_html;
        document.getElementById('header').innerHTML = camp_datas[5];
    }
    else if ( type == "mixed" ){
        petition_div.innerHTML = mix_div_html;
        document.getElementById('static_text').innerHTML = defaultletter;
        document.getElementById('petition_letter').innerHTML = camp_datas[7];
    }
    else {
        petition_div.innerHTML = checkbox_div_html;
    }
    ty_msg=camp_datas[6];
    scroll();
    action_type=type;
    resetLetter();
    petitioninitialized = true;
    }
}


function thankyou(){
    hideFlash();
    div = document.getElementById('petition_div');
    div.innerHTML = ty;

    if (ty_msg != 'null' && ty_msg != undefined && ty_msg != ""){
        document.getElementById('pop_content').innerHTML=ty_msg;
    }
    scroll();
}

function close_thankyou(){
    var divobj = document.getElementById('petition_div');
    divobj.style.display = "none";
    showFlash();
}

function closePetition() {
    var divobj = document.getElementById('petition_div');
    divobj.style.display = "none";
    petitionopen = false;
    showFlash();
}

function resetLetter() {
    if (getElement('seiu_remember') == '1') {
    document.getElementById('seiu_first_name').value = getElement('seiu_first_name');
    document.getElementById('seiu_last_name').value = getElement('seiu_last_name');
    document.getElementById('seiu_first_name').value = getElement('seiu_first_name');
    document.getElementById('seiu_title').value = getElement('seiu_title');
    document.getElementById('email1').value = getElement('email1');
    }
    else {
        eraseCookie('seiu_first_name');
        eraseCookie('seiu_last_name');
        eraseCookie('seiu_first_name');
        eraseCookie('seiu_title');
        eraseCookie('email1');
    }
    var petition_letter = document.getElementById('petition_letter');
    if (action_type == 'letter'){
            petition_letter.value  = defaultletter;
    }
    else if ( action_type == 'petition') {
        petition_letter.value = defaultletter;
    }
    else if (action_type == 'mixed'){
        defaultletter = defaultletter;
    }
    else{
        document.getElementById('petition_letter').innerHTML = defaultletter;
    }
}

function submitCheck() {
    var errors = {};
    if(document.getElementById('seiu_first_name').value == '') errors.first_name = "Please tell us your first name";
    if(document.getElementById('email1').value=='') errors.email = "Please check your email address.";
    var email_regexp = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
    if (email_regexp.test(document.getElementById('email1').value) == false) errors.valid_email = "Please type a valid email address.";
    if(document.getElementById('zip_code').value == '') errors.zip5 = "Please type your ZIP code";
    if(document.getElementById('zip4_select').value =='') errors.zip4 = "Please select your ZIP4 code";
    if(!document.getElementById('terms_conditions').checked) errors.terms_conditions = "Please accept terms and conditions";


    if (errors.first_name != null) {
        alert(errors.first_name);
    return false;
    }
    else if(errors.email!=null) {
        alert(errors.email);
        return false;
    }
    else if(errors.valid_email!=null) {
        alert(errors.valid_email);
        return false;
    }
    else if ( errors.zip5 != null) {
        alert(errors.zip5);
    return false;
    }
    else if ( errors.zip4 != null) {
        alert(errors.zip4);
    return false;
    }
    else if ( errors.terms_conditions != null) {
        alert(errors.terms_conditions);
    return false;
    }
    return true;
}

function doSubmit() {
     if(!submitCheck()) return false;
     var cburl = url_php_prefix+'/php/cb.php';
     var cbdata = '';
     var form_array =  new Array();
     for(var i=0;i<seiu_form_fields.length;i++) {
         var item = document.getElementById(seiu_form_fields[i]);
         if(seiu_form_fields[i] == 'seiu_remember') {
        if(item.checked) {
            cbdata += seiu_form_fields[i] + '=1&';
            saveElement(seiu_form_fields[i], 1, 90);
        }
        else {
                saveElement(seiu_form_fields[i], 0, 90);
        }
     }
         else if(seiu_form_fields[i] == 'seiu_title') {cbdata += seiu_form_fields[i] + '=' + escape(item.options[item.selectedIndex].text) + '&'; if (document.getElementById('seiu_remember').checked)saveElement(seiu_form_fields[i], escape(item.options[item.selectedIndex].text), 90);}
         else if(seiu_form_fields[i] == 'zip4_select' && zip4_state_hack!='') {cbdata += seiu_form_fields[i] + '=' + escape(zip4_state_hack) + '&';}
         else if(item.value!=undefined) {cbdata += seiu_form_fields[i] + '=' + escape(item.value) + '&';  if (document.getElementById('seiu_remember').checked && seiu_form_fields[i] != 'petition_letter')saveElement(seiu_form_fields[i],item.value, 90);}

     }
     cbdata += '&action_id=' + camp_datas[0].toString() + '&random='+r_id + '&actiontag_id='+camp_datas[2].toString()+'&random='+r_id;
     var cbfunc = "thankyou();";
     xsHelper(cburl, cbdata, cbfunc);
     completed_actions.push(camp_datas[0]);
     createCookie('completed_actions', completed_actions, 30);
     var bugid = 'bug-'+opened_bug;
     var advid = 'adv-'+opened_bug;
     var act = document.getElementById(bugid);
     var adv_act = document.getElementById(advid);
     act.parentNode.removeChild(act);
     adv_act.parentNode.removeChild(adv_act);
     closePetition();
     return false; //Prevent form from submitting
}

function updateReprName(value) {
     if (value == undefined) {
        document.getElementById('zip4_select').innerHTML = '';
        var zip = document.getElementById('zip_code').value;
        xsHelper(url_php_prefix+'/php/zip2rep.php', 'zipcode='+zip, 'cb_repr();');
     }
     else if (value.value == '-1' && mode =='letter'){
        document.getElementById('petition_letter_greeting').innerHTML = "Dear Representative,";
     }
     else if (mode == 'letter'){
        document.getElementById('petition_letter_greeting').innerHTML = "Dear Representative " + value.value + ',';
        zip4_state_hack = value.options[value.selectedIndex].innerHTML;
     }
}

function cb_repr() {
     if(zip4reps.length == 0 && mode == 'letter') document.getElementById('petition_letter_greeting').innerHTML = "Dear Representative,";
     if(zip4reps.length == 1 && mode == 'letter') document.getElementById('petition_letter_greeting').innerHTML = "Dear Representative" + zip4reps[0][1] + ',';
     if(zip4reps.length > 1) {
         var sel = '<select name="zip4select" id="zip4select" onchange="updateReprName(this)"><option value="-1">Please select plus-four code</option>';
         for(var i=0;i<zip4reps.length;i=i+1) {
             sel = sel + "<option value='" + zip4reps[i][1] + "'>" + zip4reps[i][0] + "</option>";
         }
         sel = sel + '</select>';
         document.getElementById('zip4_select').innerHTML = sel;
     }
}

function xsHelper(url, data, onsuccess) {
     var head = document.getElementsByTagName('head')[0];
     var script = document.createElement('script');
     script.type = 'text/javascript';
     script.src = url + '/?' + data + '&onsuccess=' + onsuccess;
     head.appendChild(script);
}

function addEvent(obj, evType, fn){
    if (obj.addEventListener){
       obj.addEventListener(evType, fn, false);
       return true;
    } else if (obj.attachEvent){
       var r = obj.attachEvent("on"+evType, fn);
       return r;
    } else {
       return false;
    }
}
if (true) addEvent(window, 'load', init_actiontag);
