function check_petition() {
    var is_valid = false;
    is_valid = valid('First_Name','Please enter your First Name.', 'First Name', 2);    
    if( !is_valid ) { return false; }

    is_valid = valid('Last_Name','Please enter your Last Name.', 'Last Name', 2);
    if( !is_valid ) { return false; }

    is_valid = check_email(document.getElementById('Fax_Email'));
    if( !is_valid ) { return false; }

    is_valid = valid('Street','Please enter your Street Address.', 'Street Address', 2);
    if( !is_valid ) { return false; }

    is_valid = valid('City','Please enter your City.', 'City', 2);
    if( !is_valid ) { return false; }

    is_valid = valid('State','Please enter your State.', '', 2);
    if( !is_valid ) { return false; }
    
    is_valid = valid('Zip','Please enter a valid ZIP Code.', 'ZIP', 5);
    if( !is_valid ) { return false; }

    /*
    is_valid = valid('cell1','Please enter a valid Mobile Phone number.', '', 3);
    if( !is_valid ) { return false; }

    is_valid = valid('cell2','Please enter a valid Mobile Phone number.', '', 3);
    if( !is_valid ) { return false; }

    is_valid = valid('cell3','Please enter a valid Mobile Phone number.', '', 4);
    if( !is_valid ) { return false; }
    */
    
    if( document.getElementById('Cell_Phone') && document.getElementById('cell1') && document.getElementById('cell2') && document.getElementById('cell3') ) { 
        var el = document.getElementById('Cell_Phone');
        el.value = document.getElementById('cell1').value + document.getElementById('cell2').value + document.getElementById('cell3').value;
    }    
    if(typeof set_tell_cookie == 'function') { 
        set_tell_cookie();
    }

    return true;
}

function check_taf() {
    var is_valid = false;
    is_valid = check_email(document.getElementById('Email'));
    if( !is_valid ) { return false; }

    is_valid = valid('subject','Please enter a subject for your message.', '', 2);
    if( !is_valid ) { return false; }

    is_valid = valid('content','Please enter a message.', '', 2);
    if( !is_valid ) { return false; }

    is_valid = valid('emails','Please enter recipients.', '', 2);
    if( !is_valid ) { return false; }
    
    return true;
}

function valid(id, error_msg, the_default, min_length) {
    var el = document.getElementById(id);
    if( !el ) 
        return true;
    
    if( el.value.length < min_length || el.value == the_default ) {
        alert(error_msg);
        el.focus();
        if( el.value == the_default )
            el.value = '';
        return false;
    }
    
    return true;
}
function myfocus(el, the_default) {
    if( el.value == the_default )
        el.value = '';    
}
function myblur(el, the_default) {
    if( el.value == '' )
        el.value = the_default;    
}
function check_email(el) {
    el.value = el.value.toString().trim();
    var Regex = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;                   
    if( el.value.length <= 1 || !Regex.test(el.value) ) {
        alert('Please enter a valid Email Address.');
        el.focus(); 
        el.value = '';
        return false;
    }

    return true;
}
function append_site(id) {
    var el = document.getElementById('q');
    if( el ) {
        el.value = el.value + ' site:deltavisionfoundation.org';
    }
    return true;
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/,"");
}

function check_cell_phone(id_full_phone) {
    var is_valid = valid('cell1','Please enter a valid cell phone number.', '', 3);
    if( !is_valid ) { return false; }
  
    is_valid = valid('cell2','Please enter a valid cell phone number.', '', 3);
    if( !is_valid ) { return false; }

    is_valid = valid('cell3','Please enter a valid cell phone number.', '', 4);
    if( !is_valid ) { return false; }

    set_cell_phone( id_full_phone );

    return true;
}

function set_cell_phone(id_full_phone) {
    var el = document.getElementById(id_full_phone);
    el.value = document.getElementById('cell1').value + document.getElementById('cell2').value + document.getElementById('cell3').value;
}


/*
-FIX BUG: in CCC dia templates: clicking off to a different page and then returning looses the partner "p"
    1) Store the "p" parameter if present in a cookie with the code Yongho provided.
    2) Set the value of the "Tracking_Code" hidden parameter from the cookie's value. The input doesn't have an id, so I'll have to iterate through the form fields to find the correct one.  http://salsa.wiredforchange.com/o/5691/campaign.jsp?campaign_KEY=2819&p=OCA
*/
function getQueryValue( name ) {
    var regex = new RegExp( "[\?&]"+name+"=([^&#]*)" );
    var results = regex.exec( window.location.href );
    if( results == null )
        return "";
    else
        return results[1];
}

function read_cookie(name)
{
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    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) return c.substring(nameEQ.length,c.length);
    }
    return "";
}
function set_cookie(cookieName,cookieValue,nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays==null || nDays==0) nDays=1;
    expire.setTime(today.getTime() + 3600000*24*nDays);
    document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString()
                 + ";path=/";
}

function set_tracking_code_from_cookie() {
    var partner = read_cookie('p');
    if( partner.length == 0 ) return;

    for( var j=0; j < document.forms.length; j++ ) {    
        var form = document.forms[j];
        for( var i=0; i < form.elements.length; i++ ) {
            var el = form.elements[i];
            if( el.name == 'Tracking_Code' && el.value == '' ) {
                el.value = partner;
            }
            if( el.name == 'VARCHAR9' && el.value == '' ) {
                el.value = partner;
            }
        }
    }
}

var value = getQueryValue('p');
if ( value !== null && value != '' ) { set_cookie('p',value,30); }
setTimeout('set_tracking_code_from_cookie()',50);
