var IE = document.all?true:false;
//if (IE) alert('IE'); else alert('FF');
if (!IE) document.onmousemove = event_getMouseXY;
var ff_mousex=0;
var ff_mousey=0;

//var tzo=(new Date().gettimezoneOffset()/60)*(-1); 
//new Date() -> Sun Apr 6 08:25:33 UTC+0300 2008
//(new Date()).getTimezoneOffset() -> 180 minutes
//(new Date()).getTimezoneOffset() / 60; -> -3
//var tzo=(new Date()).getTimezoneOffset() / 60; 
setCookie("timezone", (new Date()).getTimezoneOffset() );
//var d=new Date();
//setCookie("client_time_utc", d);
//setCookie("client_time_s",  FormatDateTime_full_noms(d) );

function SetValue(control1, value1) {
  if (control1==undefined) {
    alert("SetValue called for an unknown control");
    return;
  }
  
  //this works for editboxes, and comboboxes <select><option>
  //for radiobuttons the value is set, but the comboboxes are not checked
  control1.value = value1; //k 

  //control1.checked -> editboxes->false
  //control1.checked -> 1 radiobuttons->true/false
  //control1.checked -> 2 radiobuttons->undefined
  //control1.checked -> comboboxes->undefined
 
  //alert(control1.checked);
  //if (control1.checked != undefined)
  SetRadioValue11(control1, value1);
}

function GetRadioValue(btn) {
  if (btn==undefined) { //e.g. 0 radiobuttons
    return "";
  }
  var cnt = -1;
  //if there is only one radiobutton btn.length is undefined (both ie and firefox)
  //alert(btn.length);
  if (btn.length == undefined) {    
    if (btn.checked) return btn.value;
    else return "";
  }
  for (var i=btn.length-1; i > -1; i--) {
    if (btn[i].checked) { cnt = i; i = -1; }
  }
  if (cnt > -1) 
    return btn[cnt].value;
  else 
    return '';
}

function SetRadioValue11(radioObj, newValue) {
  var len = radioObj.length;    
  //alert(radioObj.checked);
  if (len == undefined) { //when there is only one radiobutton in the group
    radioObj.checked = (radioObj.value == newValue.toString());
    return;
  }
  for (var i = 0; i < len; i++) {
    //if (radioObj[i].value != newValue.toString()) continue;
    //will also uncheck
    radioObj[i].checked = false;
    radioObj[i].checked = (radioObj[i].value == newValue.toString());
  }
}
/*function SetRadioValue(radioObj, newValue) {
  if(!radioObj)
    return;
  var radioLength = radioObj.length;
  if(radioLength == undefined) {
    radioObj.checked = (radioObj.value == newValue.toString());
    return;
  }
  for(var i = 0; i < radioLength; i++) {
    radioObj[i].checked = false;
    if(radioObj[i].value == newValue.toString()) {
      radioObj[i].checked = true;
    }
  }
}*/

function IsValidAlphaAndDigitName(s) {
  var checkOk = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-";
  var checkStr = s;  
  //var validGroups = true;  
  for (var i = 0;  i < checkStr.length;  i++) {
    var ch = checkStr.charAt(i);
    for (var j = 0;  j < checkOk.length;  j++)
      if (ch == checkOk.charAt(j))
        break;    
    if (j == checkOk.length) 
      return false;    
  }
  return true;
}

function IsValidForumName(s) {
  var checkOk = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-";
  var checkStr = s;  
  //var validGroups = true;  
  for (var i = 0;  i < checkStr.length;  i++) {
    var ch = checkStr.charAt(i);
    for (var j = 0;  j < checkOk.length;  j++)
      if (ch == checkOk.charAt(j))
        break;
    if (j == checkOk.length) 
      return false;    
  }
  return true;
}

function IsValidInteger(s) {
  var checkOk = "0123456789";
  var checkStr = s;    
  for (var i = 0;  i < checkStr.length;  i++) {
    var ch = checkStr.charAt(i);
    for (var j = 0;  j < checkOk.length;  j++)
      if (ch == checkOk.charAt(j))
        break;
    if (j == checkOk.length)         
      return false;    
  }
  return true;
}

  //var expdate = new Date ();
  //expdate.setTime (expdate.getTime() + (1000 * 60 * 60 * 24 * 31));

function setCookie(name, value) {
  document.cookie = name + "=" + escape (value) +  ";";
  //e.g. document.cookie = 'blockadsense=yes; expires=31-DEC-2023 00:00:00 GMT; path=/'
}

function setCookieEx(name, value, expires) {
  if (!expires) expires = new Date();
  document.cookie = name + "=" + escape (value) + "; expires=" + expires.toGMTString() +  "; path=/";
}

function setLongCookie(name, value) {
  var expDays = 3000;
  var exp1 = new Date();
  exp1.setTime(exp1.getTime() + (expDays*24*60*60*1000));

  //exp1 = new Date;
  document.cookie = name + "=" + escape (value) +  "; expires="+exp1.toGMTString();
}

function update_inbox_msgsperpage() {
  //alert(inbox_msgsperpage.value);
  setLongCookie("inbox_msgsperpage", inbox_msgsperpage.value);
    window.location = window.location; //reload
}

var selcount = 0;

function CheckAll() {
  if (document.forminbox == null) { return; }
  for (var i=0;i<document.forminbox.elements.length;i++) {
    var e = document.forminbox.elements[i];
    //e.checked = document.forminbox.allbox.checked;
    e.checked = true;
  }
}

function CheckNone() {
  if (document.forminbox == null) { return; }
  for (var i=0;i<document.forminbox.elements.length;i++) {
    var e = document.forminbox.elements[i];
    //e.checked = document.forminbox.allbox.checked;
    e.checked=false;
  }
}

function CheckString() {
  selcount = 0;
  var s = "";

  if (document.forminbox == null) {
    //alert("No Messages Here"); 
    return("");
  }
  for (var i=0;i<document.forminbox.elements.length;i++) {
    var e = document.forminbox.elements[i];
    if (e.checked) {
      s = s + e.name + ",";
      selcount += 1;
    }
    //e.checked = document.forminbox.allbox.checked;
    //e.checked=true;    
  }
  //alert(s);
  return(s);
}

function DeleteSelected() {
  var s = CheckString();
  if (s=="") { alert("No messages are selected"); return;
  }  
  if (confirm('WARNING: You are about to delete the ' + selcount +' selected messages. ')) { 
    window.location="?inbox_op_del&msg="+s; 
  }
}

function DeleteSelected_S() {
  //alert('Deleting Messages from the Sent Messages Folder has not been implemented yet! Coming Soon!');
  //return;
  var s = CheckString();
  if (s=="") { alert("No messages are selected"); return;
  }  
  if (confirm('WARNING: You are about to delete the ' + selcount +' selected messages. ')) {
    window.location="?inbox_op_del_S&msg="+s; 
  }
}
function ArchiveSelected() {
  var s = CheckString();
  if (s=="") { alert("No messages are selected"); return;
  }
  if (confirm('You have selected to archive the ' + selcount +' selected messages. ')) { 
    window.location="?inbox_op_arc&msg="+s; 
  }
}

//-------------------------------
//userpopup

var username = "";
var UserId   = "";
var hideframe_timeoutid = 0;
var showframe_timeoutid = 0;

var pass_obj = "";
var pass_this_username = "";
var pass_this_user_id = "";
var pass_this_str = "";
    
function setUserFrame(obj, thisUsername, thisUserId, thisStr) {
  //setUserFrame_now(obj, this_username,this_user_id,this_str);
  //return;
  
  pass_obj = obj;
  pass_this_username = thisUsername;
  pass_this_user_id = thisUserId;
  pass_this_str = thisStr;
  setUserFrame_clear_showtimer();
  showframe_timeoutid = setTimeout("setUserFrame_pass()", 100);
  //setUserFrame_clear_hidetimer();
}

function setUserFrame_pass() {
  setUserFrame_now(pass_obj, pass_this_username, pass_this_user_id, pass_this_str);
  //setUserFrame_clear_hidetimer();
}

function setUserFrame_now(obj, thisUsername, thisUserId, thisStr) {
//alert('obj = ' + obj);
//alert('obj top = ' + getObjTop(obj) );
  setUserFrame_clear_hidetimer();

  UserId = thisUserId;
  username = thisUsername;  
    
  var edUserStr = getobject("idUserStr");  
  edUserStr.value = thisStr;
  //alert(getObjLeft(idUserStr));
  //alert(getObjTop(idUserStr));
  
  //MouseX = getMouseX() + 40;
  //MouseY = getMouseY();
  var mouseX = getObjLeft(obj) + 60;
  var mouseY = getObjTop(obj);  
  
  var tabUserMenu = getobject("id_tabUserMenu");
  //alert(tabUserMenu);
  tabUserMenu.style.display  = "";

  //screenClientWidth = screen.width - 40;
  var screenClientWidth = document.body.clientWidth + document.body.scrollLeft;

  var x2=0;
  var y2=0;
  if ( parseInt(tabUserMenu.width)+parseInt(mouseX) < screenClientWidth ) {
    x2 = mouseX;
    y2 = mouseY;
  } else {
    x2 = screenClientWidth - parseInt(tabUserMenu.width);
    y2 = mouseY + 15;
  }
  tabUserMenu.style.top = y2 + 'px'; //px needed for FF, IE is OK without it
  tabUserMenu.style.left = x2 + 'px';
  
  //alert(parseInt(tabUserMenu.width));
  //  alert(screen.width);
  //frm.subject.focus();
}

function setUserFrame_clear_hidetimer() {
  if (hideframe_timeoutid > 0) clearTimeout(hideframe_timeoutid);
}

function setUserFrame_clear_showtimer() {
  if (showframe_timeoutid > 0) clearTimeout(showframe_timeoutid);
}

function hideUserFrame() {   
  var tabUserMenu = getobject("id_tabUserMenu");
  tabUserMenu.style.display  = "none";
}

function hideUserFrame_2s() {
  if (showframe_timeoutid > 0) clearTimeout(showframe_timeoutid);
  
  //timer_active = 1;
  setUserFrame_clear_hidetimer();
  hideframe_timeoutid = setTimeout('hideUserFrame()', 1000);
}

function useraction(action) {
  var url = "";
  
  //for use in both forums and discussion 

  if (action == "view") { url = "/Program.aspx?id=" + UserId; }
  else if (action == "bing") { url = "http://www.bing.com/search?q=" + username; }
  else if (action=="profile") { url = "./?public_profile&UserId="+UserId; };

  //window.location = url;
  window.open(url, 'updater', '', '');
}

/*function DocPos(oElement, nLeft, nTop) {
  //var oElement = document.all.elementId;
  var nLeft = oElement.offsetLeft;
  var nTop = oElement.offsetTop;
alert(oElement.parent);
  while ( oElement.parentOffset != document.body ) {
    oElement = oElement.parentElement;
    nLeft += oElement.offsetLeft;
    nTop += oElement.offsetTop;
  }
}*/

function getObjLeft(myObject) {
  //return(myObject.offsetParent ? (getObjLeft(myObject.offsetParent) + myObject.offsetLeft) : myObject.offsetLeft);
  return getXY(myObject).x;
}

function getObjTop(myObject) {
  //return(myObject.offsetParent ? (getObjTop(myObject.offsetParent) + myObject.offsetTop) : myObject.offsetTop);
  return getXY(myObject).y;
}

var menu_timeoutid = 0;
function setMenu(i) {
  var tabMenu = getobject("id_tabMenu");
  var mainmenu = getobject("id_mainmenu");

  //alert('x: ' + getXY(mainmenu).x + ' y: '+ getXY(mainmenu).y );  
  
  if (i==1) {    
    tabMenu.style.display  = "";
    //alert(getObjLeft(id_mainmenu));
    //alert(getobject('id_mainmenu').width);
    tabMenu.style.left = (getObjLeft(mainmenu) - 20) + 'px';
    tabMenu.style.top  = (getObjTop(mainmenu) + 1) + 'px';
    setMenu_cleartimer();
    //alert(getobject('id_mainmenu').width);
    //alert(document.all[id_mainmenu].offsetLeft);
  } else {
    tabMenu.style.display  = "none";    
  }
}

function setMenu_cleartimer() {
  if (menu_timeoutid > 0) clearTimeout(menu_timeoutid);
}

function hideMenu_2s() {
  menu_timeoutid = setTimeout('setMenu(0);', 500);
}

function OpenWindow(url, height, width, name, parms) {
//opens centered window
   var left = Math.floor( (screen.width - width) / 2);
   var top = Math.floor( (screen.height - height) / 2);
   var winParms = "top=" + top + ",left=" + left + ",height=" + height + ",width=" + width;
   if (parms) { winParms += "," + parms; }
   var win = window.open(url, name, winParms);
   if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
   return win;
}

function ReportAbuse(itemType, messageId) {
  var s = "message";
  if (itemType==1) s = "message";
  if (itemType==2) s = "picture";
  if (itemType==3) s = "album_message";
    
  if (!confirm('Your are about to report this '+s+' to the administrator as offensive. Continue?')) return false;
  
  //var newWindow = window.open( '../scripts/report_message.php?id=' + message_id, 'myPopup', 'width=500,height=400' )
  //var newWindow = window.open( '?report_message&id=' + message_id, 
  //  'myPopup', 
  //  'width=450,height=400' )
  
  //if (myPopup) alert(myPopup);
 
  //for use in both forums and discussion 
  OpenWindow('./?report_message&item_type='+itemType+'&id=' + messageId, 450, 500, 'myPopup', '');
  return false;
}

function event_getMouseXY(e) {
  ff_mousex = e.clientX + document.body.scrollLeft;
  ff_mousey = e.clientY + document.body.scrollTop;
  //alert(mousex + ' - ' + mousey);
}

function getMouseX() {
  if (IE) 
    return event.clientX + document.body.scrollLeft;
  else
    return ff_mousex; 
}

function getMouseY() {
  //alert(document.body);
  //alert(event.clientY + ' - ' + document.body.scrollTop);
  if (IE) 
    return event.clientY + document.body.scrollTop;
  else
    return ff_mousey; 
}

function d2(v) {
  if (v<10) 
    return "0" + v;
  else 
    return v;
}

function FormatDateTime_full_noms(d) {
  //format: 2008-04-06 10:13:58
  return d.getFullYear() + '-' + d2((d.getMonth()+1)) + '-' + d2(d.getDate())
    + ' ' + d2(d.getHours()) + ':' + d2(d.getMinutes()) + ':' + d2(d.getSeconds());
}

function getXY(obj) {
/**
 http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html
 * Returns the absolute X and Y positions of an object.
 * @param {HTMLObject} obj HTML Object.
 * @return {Object} Returns an accessor with .x and .y values.
 */
  var curleft = 0;
  var curtop = obj.offsetHeight + 5;
  var border;
  if (obj.offsetParent)
  {
    do
    {
      // XXX: If the element is position: relative we have to add borderWidth
      if (getStyle(obj, 'position') == 'relative') {
        if (border = _pub.getStyle(obj, 'border-top-width')) curtop += parseInt(border);
        if (border = _pub.getStyle(obj, 'border-left-width')) curleft += parseInt(border);
      }
      curleft += obj.offsetLeft;
      curtop += obj.offsetTop;
    }
    while (obj = obj.offsetParent);
  }
  else if (obj.x)
  {
    curleft += obj.x;
    curtop += obj.y;
  }
  return {'x': curleft, 'y': curtop};
}

function getStyle(obj, styleProp) {
/**
 http://www.davidcramer.net/code/84/get-offsets-xy-for-an-object-javascript.html
 * Returns the specified computed style on an object.
 * @param {HTMLObject} obj HTML Object
 * @param {String} styleProp Property name.
 * @return {Mixed} Computed style on object.
 */
  if (obj.currentStyle)
    return obj.currentStyle[styleProp];
  else if (window.getComputedStyle)
    return document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
}

function getobject(objectId) {
  return document.getElementById(objectId);
}
function getobjectbyid(objectId) {
  return document.getElementById(objectId);
}
function getobjectbyname(objectName) {
  return document.getElementsByName(objectName)[0];
}
function getform(objectName) {
  return getobjectbyname(objectName);
}
//-------------------------------


//javascript extension methods
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+$/,"");
};


