function uaff(id) {
    document.getElementById('pimage').src='/aff_clicks.php?id='+id;
    return true;
}
//Functions for getting the locations on the create "activity_where_attributes" Form
function get_location_list_js() {
    var input = document.forms[0].loc_name.value;
    // Check for arguments.  If there is an argument, assume
    // that it is coming from an edit page and call the make_location_chooser differently
    var argv = get_location_list_js.arguments;
    var argc = argv.length;
    if( argc ) {
      frames['mapframe'].location.href = 'make_location_chooser.php?is_edit=1&query='+ URLEncode(input);
    }
    else {
      frames['mapframe'].location.href = 'make_location_chooser.php?query='+ URLEncode(input);
    }
}
//Functions for getting the messages on the user_pages
function fill_div(result) {
    document.getElementById(result[0]).innerHTML = result[1];
}
function get_activities_attend_js(userId, page_num, el1) {

  var elem1 = document.getElementById(el1);
  var cook = readCookie('messages_activity_attend_' + userId);

  if ( elem1.style.display == "none" && page_num <=0 ) {
    page_num = cook;
    elem1.style.display = "block";
    document.getElementById(el1).innerHTML ='<table class=ajax_messages_table><tr><td valign="top" colspan=3>Loading...' + '</td></tr><tr><td align="left" valign="top"><span class=pagertextgray>Prev</span></td><td>&nbsp;</td><td align="right" valign="top"><span class=pagertextgray>Next</span></td></table>';
    x_get_activities_attend_ajax(userId, page_num, el1, fill_div);
    var c_type="messages_activity_attend_" + userId;
    createCookie(c_type, page_num, 10);
  }
  else if( page_num >=0 ) {
    document.getElementById(el1).innerHTML ='<table class=ajax_messages_table><tr><td valign="top" colspan=3>Loading...' + '</td></tr><tr><td align="left" height=40 valign="top"><span class=pagertextgray>Prev</span></td><td>&nbsp;</td><td align="right" valign="top"><span class=pagertextgray>Next</span></td></table>';
    x_get_activities_attend_ajax(userId, page_num, el1, fill_div);
    var c_type="messages_activity_attend_" + userId;
    createCookie(c_type, page_num, 10);
  }
  else {
    elem1.style.display = "none";
    var c_type="messages_activity_attend_" + userId;
    createCookie(c_type, page_num, 0);
  }
}
function get_activity_messages_js(userId, page_num, el1) {

  var elem1 = document.getElementById(el1);
  var cook = readCookie('messages_activity_' + userId);

  if ( elem1.style.display == "none" && page_num <=0 ) {
    page_num = cook;
    elem1.style.display = "block";
    document.getElementById(el1).innerHTML ='<table class=ajax_messages_table><tr><td valign="top" colspan=3>Loading...' + '</td></tr><tr><td align="left" valign="top"><span class=pagertextgray>Prev</span></td><td>&nbsp;</td><td align="right" valign="top"><span class=pagertextgray>Next</span></td></table>';
    x_get_activity_messages_ajax(userId, page_num, el1, fill_div);
    var c_type="messages_activity_" + userId;
    createCookie(c_type, page_num, 10);
  }
  else if( page_num >=0 ) {
    document.getElementById(el1).innerHTML ='<table class=ajax_messages_table><tr><td valign="top" colspan=3>Loading...' + '</td></tr><tr><td align="left" height=40 valign="top"><span class=pagertextgray>Prev</span></td><td>&nbsp;</td><td align="right" valign="top"><span class=pagertextgray>Next</span></td></table>';
    x_get_activity_messages_ajax(userId, page_num, el1, fill_div);
    var c_type="messages_activity_" + userId;
    createCookie(c_type, page_num, 10);
  }
  else {
    elem1.style.display = "none";
    var c_type="messages_activity_" + userId;
    createCookie(c_type, page_num, 0);
  }
}
function get_help_js(divId, helpId){
    var elem = document.getElementById(divId);

    if ( elem.style.display == "none" ) {
       elem.innerHTML = '<center>Loading....</center>';
       elem.style.display = "block";
       relocate_elem(divId);
    }

    x_get_help(divId, helpId, check_handle_help);
}
function check_handle_help(result) {
alert("YES");
/*    var elem = document.getElementById( result[0] );
    var str = result[1];
    if( result[0] == 333 ) {
      str += "<br><center><span class='ajax_link' style='font-weight: bold;' onClick='javascript:close_div(222)'>Close</span></center>";
    }
    else if( result[0] == 555 ) {
      str += "<br><center><span class='ajax_link' style='font-weight: bold;' onClick='document.getElementById(\"inline_search_div\").style.display = \"none\"; return false;'>Close</span></center>";
    }
    else {
      str += "<br><center><span class='ajax_link' style='font-weight: bold;' onClick='javascript:close_div("+result[0]+")'>Close</span></center>";
    }
    if( result[0] == 111171263) { // THIS IS A SPECIAL CASE FOR THE POSTING GUIDLINES BECAUSE THEY ARE AT THE BOTTOM
      str += "<a name='bottom'></a><img src='/images/layout/clearpixel.gif' onLoad='drop_down();' width='10' height='10' />";
    }
    elem.innerHTML = str;*/
}
function drop_down(){
  document.location.href='#bottom';
}
//Functions for getting the user names on the object_admin_form Priviliges
function get_user_list_js(objectId, page) {
    document.getElementById("user_block").style.display = "block";
    var input = document.mainform.user_name.value;
    x_get_user_list(input, page, check_users);
}
function check_users(result){
    document.getElementById("user_block").innerHTML = result;
}
function add_username(userName) {
    var currentInput = document.mainform.username.value;
    var currentDivInput = document.getElementById("added_list").innerHTML;
    var reg = /^$/i;
    if( currentInput.match(reg) ) {
      document.mainform.username.value = userName;
      document.getElementById("added_list").innerHTML = "To List: "+userName;
    }
    else {
      document.mainform.username.value = currentInput + ", " + userName;
      document.getElementById("added_list").innerHTML = currentInput + ", " + userName;
    }
}
function close_div(theDiv){
    del = document.getElementById('shade');
    if( del) {
        del.style.display = "none";
    }
    var elem1 = document.getElementById(theDiv);
    elem1.style.display = "none";
}

// END Functions for getting the user names on the mainform in the create activity process

function toggle_show(theMenu, theImage){
    var elem1 = document.getElementById(theMenu);
    if ( elem1.style.display == "none" ) {
      theImage.src = "/images/layout/collapse.gif";
      elem1.style.display = "block";
    }
    else {
      elem1.style.display = "none";
      theImage.src = "/images/layout/expand.gif";
    }
}

function readCookie(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 null;
}

function createCookie(name, value, minutes)
{
    if (minutes)
    {
        var date = new Date();
        date.setTime(date.getTime()+(minutes*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else{ var expires = "; expires=Thu, 01-Jan-1970 00:00:01 GMT"; var value="";}
    document.cookie = name+"="+value+expires+"; path=/";
}
// GLOBAL FUNCTION FOR GREYING OUT STUFF
function grayOut(mode) {
    del = document.getElementById('shade');
    if (!del) return;
        var px=10;

    if (mode == 'block') {
        del.style.width=document.body.scrollWidth+'px';
        del.style.height=document.body.scrollHeight+'px';
    }
    del.style.display = mode;
}

// Used for making sure that the drop down divs don't run off the page
function relocate_elem(divId) {

  var winWidth = getWinWidth();
  var elem = document.getElementById(divId);
  var currPos = getElementPos(elem);
  var elemWidth = elem.offsetWidth;
  //alert("currPos.x = " + currPos.x);
  var rightSide = elemWidth + currPos.x;
  var newPos = {x: 0, y: 0};
  if( rightSide > winWidth ) {
    newPos.x = winWidth - elemWidth;
  }
  else {
    newPos.x = currPos.x;
  }
  newPos.y = currPos.y;
  //alert("newPos.x = " + newPos.x);
  //alert("newPos.y = " + newPos.y);
  setElementPos(elem, newPos);
}
function getWinWidth()
{
    if (window.innerWidth) return window.innerWidth - 18;
    else if (document.documentElement && document.documentElement.clientWidth)
        return document.documentElement.clientWidth;
    else if (document.body && document.body.clientWidth)
        return document.body.clientWidth;
    else if (document.body && document.body.parentNode && document.body.parentNode.clientWidth)
        return document.body.parentNode.clientWidth;
}
function getElementPos(element)
{
  var coords = {x: 0, y: 0};
  do {
    coords.x += element.offsetLeft;
    coords.y += element.offsetTop;
  }
  while ((element = element.offsetParent));
  return coords;
}
function setElementPos(element, coords)
{
    element.style.left = coords.x+"px";
    element.style.top = coords.y+"px";
}

function URLEncode( plaintext )
{
    // The Javascript escape and unescape functions do not correspond
    // with what browsers actually do...
    var SAFECHARS = "0123456789" +                  // Numeric
                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +  // Alphabetic
                    "abcdefghijklmnopqrstuvwxyz" +
                    "-_.!~*'()";                    // RFC2396 Mark characters
    var HEX = "0123456789ABCDEF";

    var encoded = "";
    for (var i = 0; i < plaintext.length; i++ ) {
        var ch = plaintext.charAt(i);
        if (ch == " ") {
            encoded += "+";             // x-www-urlencoded, rather than %20
        } else if (SAFECHARS.indexOf(ch) != -1) {
            encoded += ch;
        } else {
            var charCode = ch.charCodeAt(0);
            if (charCode > 255) {
                alert( "Unicode Character '"
                        + ch
                        + "' cannot be encoded using standard URL encoding.\n" +
                          "(URL encoding only supports 8-bit characters.)\n" +
                          "A space (+) will be substituted." );
                encoded += "+";
            } else {
                encoded += "%";
                encoded += HEX.charAt((charCode >> 4) & 0xF);
                encoded += HEX.charAt(charCode & 0xF);
            }
        }
    } // for

    return encoded;
};

function URLDecode( encoded )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef";

   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
       if (ch == "+") {
           plaintext += " ";
           i++;
       } else if (ch == "%") {
            if (i < (encoded.length-2)
                    && HEXCHARS.indexOf(encoded.charAt(i+1)) != -1
                    && HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
                plaintext += unescape( encoded.substr(i,3) );
                i += 3;
            } else {
                alert( 'Bad escape combination near ...' + encoded.substr(i) );
                plaintext += "%[ERROR]";
                i++;
            }
        } else {
           plaintext += ch;
           i++;
        }
    } // while

   return plaintext;
};

