// Globals
var g=new Object;
function ag(n, v){eval('g.'+n+'="'+v+'"')}

Array.prototype.contains = function(obj){
  var len = this.length;
  for (var i = 0; i < len; i++){
    if(this[i]===obj){return true;}
  }
  return false;
};

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g, ""); };
String.prototype.truncateSpaces = function() { return this.replace(/\s+/g, " "); };
String.prototype.removeProtocols = function() { return this.replace(/https?:\/\//, ""); };
String.prototype.escapeAngles = function() { return this.replace(/</g, "&lt;").replace(/>/g, "&gt;"); };
String.prototype.convertLines = function() { return this.replace(/(\r\n)|(\n)|(\r)/g, "<br>"); };

//Tools
function setOpacity(o,v){o.style.visibility = v>10?'visible':'hidden';o.style.opacity=v/100;o.style.filter='alpha(opacity='+v+')'}
function d(i){return document.getElementById(i)}
function dt(i){return document.getElementsByTagName(i)}
function ga(o,n){return o.getAttribute(n)}
function dc(t){return document.createElement(t)}
function msie(){var u=window.navigator.userAgent; var m=u.indexOf("MSIE"); if(m>0){return parseInt(u.substring(m+5,u.indexOf(".",m)))}else return 0}
function dhtmlLoadScript(url){var e = document.createElement("script");e.src = url;e.type="text/javascript";dt("head")[0].appendChild(e);}
function ajax(URL, callback, params, pData)
{
  var xmlHttp;
  try
  {
    xmlHttp=new XMLHttpRequest();
  }
  catch (e)
  {
    try
    {
        xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
    catch (e)
    {
      try
      {
        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
      catch (e)
      {
        d('cmdFeedback').innerHTML = "Your browser does not support AJAX!, " + e.message;
        return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
  {
    if(xmlHttp.readyState==4)
    {
      if(xmlHttp.status==0)
      {
        return;
      }
      if(xmlHttp.status==200)
      {
        if(callback) callback(xmlHttp.responseText, params);
      }
      else
      {
        alert('Error, status: ' + xmlHttp.status + ' - ' + URL);
        var s=xmlHttp.responseText;
        s.substring(s.indexOf('<body>') + 6, s.indexOf('</body>'));
        document.body.innerHTML = xmlHttp.responseText;
      }
      delete xmlHttp['onreadystatechange'];
      xmlHttp = null;
    }
  }
  if (pData)
  {
    xmlHttp.open("POST",URL,true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", pData.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(pData);
  }
  else
  {
    xmlHttp.open("GET", URL, true);
    xmlHttp.send(null);
  }
}

function ajaxFunction(URL, callback, params)
{
  ajax(URL, callback, params);
}

function ajaxFunctionPOST(URL, POSTData, callback, params)
{
  ajax(URL, callback, params, POSTData);
}

function addVariables(p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15, p16, p17, p18, p19, p20)
{
  var s='';
  for(var i=1;i<21;i++)
  {
    eval("if(s!=''){s+='&'}s+='p"+i+"=' + encodeURIComponent(p"+i+")"); 
  }
  return s;
}

function addFormValues()
{
  var args='';

  var objs = dt('INPUT');
  for(i=0; i<objs.length; i++)
  {
    if(String(objs[i].id)!='')
    {
      args += '&' + objs[i].id + '=' + encodeURIComponent(objs[i].value);
    }
  }

  var objs = dt('TEXTAREA');
  for(i=0; i<objs.length; i++)
  {
    if(String(objs[i].id)!='')
    {
      args += '&' + objs[i].id + '=' + encodeURIComponent(objs[i].value);
    }
  }

  return args;
}

function hide(id)
{
  var o=d(id);
  o.style.visibility = 'hidden';
  o.style.position = 'absolute';
  o.style.left = 0;
  o.style.top = 0;
}

function toggle(id, setTo)
{
  var o=d(id);
  if (setTo)
  {
    o.style.display = (setTo?"":"none");
    return;
  }
  if (o.style.display == "none")
  {
    o.style.display = "";
  }
  else
  {
    o.style.display = "none";
  }
}

function show(id)
{
  var o=d(id);
  o.style.visibility = 'visible';
  o.style.position='static';
}

function fadein(e, overwrite)
{
  if(!e.fade) {e.fade=0; e.fading='in'};
  if (overwrite)
  {
    e.fading = 'in';
  }
  if (e.fading == 'out')
  {
    return;
  }
  if(e.fade<100)
  {
    e.fade += 10;
    setOpacity(e, e.fade);
    setTimeout('fadein(d("' + e.id + '"));', 50);
  }
  else
  {
    e.fading = '';
  }
}

function keyword_key(e, o)
{
  if(!e) var e=window.event;
  if(e.keyCode==13)
  {
    searchButton();
  }
}

function AsURL(text)
{
  if(String(text)!='undefined')
  {
    text = String(text).replace(/'/g, '');
    text = String(text).replace(/"/g, '');
    text = String(text).replace(/&/g, '&amp;');
    text = String(text).replace(/ /g, '-');
  }
  return text;
}

var real=0;

function realUser()
{
  real++;
  if (real==2)
  {
    ajaxFunction('/Action/User_IsReal.asp', '', []);
  }
}

//function MouseHandler(e){if(!e)var e=window.event;var o=e.target||e.srcElement;while(!o.mouseenter)o=o.parentNode;var r=e.relatedTarget||(e.type=='mouseover'?e.fromElement:e.toElement);if(!r)r=null;else while((!r.mouseenter)&&(r.nodeName!='HTML'))r=r.parentNode;if(o!=r)e.type=='mouseover'?o.mouseenter(o):o.mouseleave(o);}

function chatKey_Up(e)
{
  if(!e) var e=window.event;
  if(e.keyCode==13)
  {
    sendChatMessage(d("message").value);d("message").value="";
  }
}

function getBrowserWindowSize() {
    var winW = 630, winH = 460;

    if (parseInt(navigator.appVersion)>3) {
        if (navigator.appName=="Netscape") {
            winW = window.innerWidth;
            winH = window.innerHeight;
        }
        if (navigator.appName.indexOf("Microsoft")!=-1) {
            winW = document.body.offsetWidth;
            winH = document.body.offsetHeight;
        }
    }

    var rval = {
        width: winW,
        height: winH
    };

    return rval;
}

function hidePopup()
{
  document.getElementsByTagName("body")[0].removeChild(d("modalDialog"));
  document.getElementsByTagName("body")[0].removeChild(d("darkenScreenObject"));
}

function extend(obj1, obj2)
{
  for (var id in obj2)
  {
    obj1[id] = obj2[id];
  }
  return obj1;
}

function showPopup(URL, options)
{
  options = extend({width:500, height:326}, options);
  scroll(0,0); 

  if( document.body && ( document.body.scrollWidth || document.body.scrollHeight ) ) {
    var pageWidth = (document.body.scrollWidth-4)+'px';
    var pageHeight = document.body.scrollHeight+'px';
   } else if( document.body.offsetWidth ) {
    var pageWidth = document.body.offsetWidth+'px';
    var pageHeight = document.body.offsetHeight+'px';
  } else {
    var pageWidth='100%';
    var pageHeight='100%';
  }   

  var tnode = document.createElement('div');
      tnode.style.position='absolute';
      tnode.style.top='0px';
      tnode.style.left='0px';
      tnode.style.overflow='hidden';
      tnode.id='darkenScreenObject';                 
      tnode.style.opacity=0.5;                      
      tnode.style.MozOpacity=0.5;                   
      tnode.style.filter='alpha(opacity=50)'; 
      tnode.style.zIndex=50;        
      tnode.style.backgroundColor='#000000';  
      tnode.style.border='1px solid black';
      tnode.style.width= pageWidth;
      tnode.style.height= pageHeight;
      tnode.style.display='block';                          
  document.getElementsByTagName("body")[0].appendChild(tnode);

  var tnode2 = document.createElement('div');
      tnode2.style.position='absolute';
      tnode2.style.top='130px';
      tnode2.style.left=((getBrowserWindowSize().width - options.width)/2) + 'px';
      tnode2.style.overflow='hidden';
      tnode2.id='modalDialog';                 
      tnode2.style.zIndex=100;        
      tnode2.style.backgroundColor='#FFFFFF';  
      tnode2.style.border='1px solid black';
      tnode2.style.width=options.width + 'px';
      tnode2.style.height=options.height + 'px';
      tnode2.style.textAlign='left';
      tnode2.style.padding='0px';
      tnode2.style.display='block';
      tnode2.innerHTML = '<div style="height:26px;background-color:#EEEEEE;"> </div>';
      tnode2.innerHTML += '<iframe src="' + URL + '" width=' + options.width + ' height=' + (options.height - 26) + ' frameborder=0>';
      tnode2.innerHTML += "<div style='position:absolute;right:0px;top:0px;margin-right:4px;margin-top:4px;'><a onclick='hidePopup();'><img src='/Img/Design/Close.PNG'></a></div>";
  document.getElementsByTagName("body")[0].appendChild(tnode2);
}

function resizeImage(img, height, width)
{
  var height_ratio = img.height / height;
  var width_ratio = img.width / width;
  if (height_ratio > width_ratio)
  {
    img.style.width = "auto";
    img.style.height = height + "px";
  }
  else
  {
    img.style.width = width + "px";
    img.style.height = "auto";
  }
}

function searchButton()
{
  window.location = "/Search/" + escape(d('searchText').value.trim().truncateSpaces().removeProtocols()) + "/";
}
