//#!/usr/bin/perl
Xoffset=13;
Yoffset=17;
var isNS4=document.layers?true:false;
var isIE=document.all?true:false;
var isNS6=!isIE&&document.getElementById?true:false;
var old=!isNS4&&!isNS6&&!isIE;

var skn=0;
function initThis()
{
    if(isNS4)skn=document.d11;
    if(isIE)skn=document.all.d11.style;
    if(isNS6)skn=document.getElementById("d11").style;
}

function popup(_a,_b,_c,_d,_e,_f,_g,_h,_i,_j,_k,_l,_m,_n,_o,_p,_q)
{
	var content="<table class=sipopup border=0 cellspacing=0 cellpadding=0><tr><td colspan=2 style='border-bottom: 1px solid #434623;padding:3px;'>"+_k+" <b>"+_a+"</b> ("+_n+""+_o+" "+_l+")"+_p+"</td></tr><tr><td class=popupplayers><div style='color:#5f5f5e;font-weight:bold;padding-bottom:2px;text-align:left'>PLAYERS</div><b>" +_m+ "</b></td><td class=popupinfo><b><table border=0 cellspacing=0 cellpadding=0 style='width:100%;color:#5f5f5e;font-weight:bold;padding-bottom:2px;margin-bottom:5px'><tr><td valign=top>SERVER SETTINGS<br><font color=#c3c4be>Map: "+_b+"<br>Time Limit: "+_c+"<br>Frag Limit: "+_d+"<br>Team Damage: "+_e+"<br>si_fps: "+_f+"<br>Pure: "+_g+"<br>Allow Spectators: "+_h+"<br>OS: "+_j+"</b></td><td valign=top align=center><img src="+_i+" width=80 height=75 style='border:1px solid #414624;'><br>"+_q+"</td></tr></table>Click on the server's name to spy, or click anywhere else to select it.</font></td></tr></table>";

  if(old)
  {
      alert("You have an old web browser:"+_m);
      return;
  }
  else
  {
      if(isNS4)
      {
	  skn.document.open();
	  skn.document.write(content);
	  skn.document.close();
	  skn.visibility="visible";
      }
      if(isNS6)
      {
	  document.getElementById("d11").style.position="absolute";
	  document.getElementById("d11").style.left=x;
	  document.getElementById("d11").style.top=y;
	  document.getElementById("d11").innerHTML=content;
	  skn.visibility="visible";
      }
      if(isIE)
      {
	  document.all("d11").innerHTML=content;
	  skn.visibility="visible";
      }
  }
}

var x;
var y;
function get_mouse(e)
{
    x=(isNS4||isNS6)?e.pageX:event.clientX+document.body.scrollLeft; 
    y=(isNS4||isNS6)?e.pageY:event.clientY+document.body.scrollLeft; 
    if(isIE&&navigator.appVersion.indexOf("MSIE 4")==-1)
    y+=document.body.scrollTop;
    skn.left=x+Xoffset;
    skn.top=y+Yoffset;
}


function removeBox()
{
  if(!old)
  {
      document.getElementById("d11").innerHTML="";
      skn.visibility="hidden";
  }
}

if(isNS4)
    document.captureEvents(Event.MOUSEMOVE); 
if(isNS6)
    document.addEventListener("mousemove", get_mouse, true);
if(isNS4||isIE)
    document.onmousemove=get_mouse;


function MM_findObj(n, d) {
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_setTextOfLayer(objName,x,newText) {
    if ((obj=MM_findObj(objName))!=null) with (obj)
	if (document.layers) {document.write(unescape(newText)); document.close();}
    else innerHTML = unescape(newText);
}

function MM_preloadImages() { 
    var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
				  var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
				      if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() {
    var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) {
    var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
    if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
    for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
    if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() {
    var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
	if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}


function IsEmpty(aTextField) {
   if ((aTextField.value.length==0) ||
       (aTextField.value==null)) {
       return true;
   }
   else { return false; }
}

function ValidateForm(form)
{
   if(IsEmpty(form.quake4servers)) 
   { 
      alert('You must enter an IP or hostname') 
	  form.quake4servers.focus(); 
      return false; 
  } 
 
   return true;
}

function joinserver(src,serveraddress,trafurl)
{

    if (src == 'spy') {
	// set ip and laucher if called for by q4mon-spy;
	serverip=serveraddress;
	launcher=document.getElementById("q_launcher_"+trafurl).value;
	if (document.getElementById("instantconnect_"+trafurl).checked) {
	    instantconnect="1";
	} else {
	    instantconnect="0";
	}
    } else {
	// set ip and laucher if called for by q4mon-browser;
	serverip=document.resultsform.selectedserverip.value;
	launcher=document.getElementById("q_launcher").value;
	if (document.getElementById("instantconnect").checked) {
	    instantconnect="1";
	} else {
            instantconnect="0";
	}
    }

    if (serverip == 0) {
	alert('You need to select the server that you want to join. Do this by clicking on its row.');
	return false;
    }

    if (launcher == 'Qtracker')
    {
	// Qtracker;
	if (instantconnect==1) {
	    top.location.href = "qtracker://"+serverip+"?game=quake4&action=connect";
	} else {
	    top.location.href = "qtracker://"+serverip+"?game=quake4";
	}
    } else if (launcher == 'HLSW') {
	// HLSW;
	if (instantconnect==1) {
	    top.location.href = "hlsw://"+serverip+"/?Connect=1";
	} else {
	    top.location.href = "hlsw://"+serverip+"/";
	}
    } else if (launcher == 'KQUERY') {
	// kquery;
	if (instantconnect==1) {
	    top.location.href = "kquery://join-"+serverip+"/";
	} else {
	    top.location.href = "kquery://"+serverip+"/";
	}
    } else if (launcher == 'XFIRE') {
	// xfire;
	top.location.href = "xfire:join?game=quake4&server="+serverip;
    } else {
	alert('You must select a launcher before you can join the server');
	return false;
    }
}

function setCookie(name, expires, path, domain, secure) {
    if (name == 'q4favs') {
	// Make sure a server is selected
	    serverip=document.resultsform.selectedserverip.value;
	if (serverip == 0) {
	    alert('You need to select a server that you want to add to your favorites. Do this by clicking on its row.');
	    return false;
	}
	
	// Get existing favorites, if any, and update the hidden value containing favorites
	    favorites=document.cookienfo.favorites.value;
	if (favorites) {
	    var newfav_array = favorites.split("::");
	    var cnt=0;
	    var found=0;
	    newfav=null;
	    while (cnt < newfav_array.length) {
		if (newfav_array[cnt] == serverip) {
		    alert(serverip + ' has already been added in your favorites. You may need to refresh to see any changes.');
		    return false;
		}
		cnt+=1;
	    }
	    
	    value=favorites + "::" + document.resultsform.selectedserverip.value;
	} else {
	    value=document.resultsform.selectedserverip.value;
	}
	// Update hidden field in case they decide to add more than one favorite before reloading cookies;
	document.cookienfo.favorites.value = value;	
	// Write cookie to client;
	alert(serverip + ' has been added to your favorites.  You will need to refresh before noticing any changes.');

    } else if (name == 'q4instantconnect') {
	// Saves whether the Instant Connect checkbox will be checked
	if (document.getElementById("instantconnect").checked) {
	    value="1";
	} else {
	    value="0";
	}
    }	

    document.cookie= name + "=" + value +
	((expires) ? "; expires=" + expires : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function setCookieSpy(name, ip, expires, path, domain, secure) {
    if (name == 'q4favs') {
	serverip=ip;
	// Get existing favorites, if any, and update the hidden value containing favorites;
	favorites=document.cookienfo.favorites.value;
	if (favorites) {
	    var newfav_array = favorites.split("::");
	    var cnt=0;
	    var found=0;
	    newfav=null;
	    while (cnt < newfav_array.length) {
		if (newfav_array[cnt] == serverip) {
		    alert(serverip + ' has already been added in your favorites. You may need to refresh to see any changes.');
		    return false;
		}
		cnt+=1;
	    }
	    value=favorites + "::" + serverip;
	} else {
	    value=serverip;
	}
	// Update hidden field in case they decide to add more than one favorite before reloading cookies;
	document.cookienfo.favorites.value = value;	
	// Write cookie to client;
	alert(serverip + ' has been added to your favorites.  You will need to refresh before noticing any changes.');
    }
    document.cookie= name + "=" + value +
	((expires) ? "; expires=" + expires : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "");
}

function removeFromCookie(name, expires, path, domain, secure) {
    favorites=document.cookienfo.favorites.value;
    serverip=document.resultsform.selectedserverip.value;

    if (serverip == 0) {
        alert('You need to select the server that you wish to remove. Do this by clicking on its row.');
        return false;
    }
    
    var newfav_array = favorites.split("::");
    var cnt=0;
    var found=0;
    var newfav;
    while (cnt < newfav_array.length) {
	if (newfav_array[cnt] != serverip) {
	    // if ip in cookie fav is not the one you want to remove, load it in newfav
	    if (!newfav) {
		newfav=newfav_array[cnt];
	    } else {
		newfav=newfav + "::" + newfav_array[cnt];
	    }
	} else {
	    found="1";
	}
	cnt+=1;
    }

    // Update hidden field;
    document.cookienfo.favorites.value = newfav;

    if (found == 0) {
	alert('This server is not in your favorites. You may need to refresh to see any changes.');
	return false;
    } else {
	alert(serverip + ' has been removed from your favorites. You will need to refresh before noticing any changes.');
    }

    // set new cookie;
    document.cookie= name + "=" +
	((newfav) ? newfav : "") +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}




function removeFromCookieSpy(name, ip, expires, path, domain, secure) {
    favorites=document.cookienfo.favorites.value;
    serverip=ip;

    var newfav_array = favorites.split("::");
    var cnt=0;
    var found=0;
    var newfav;
    while (cnt < newfav_array.length) {
	if (newfav_array[cnt] != serverip) {
	    // if ip in cookie fav is not the one you want to remove, load it in newfav
	    if (!newfav) {
		newfav=newfav_array[cnt];
	    } else {
		newfav=newfav + "::" + newfav_array[cnt];
	    }
	} else {
	    found="1";
	}
	cnt+=1;
    }

    // Update hidden field;
    document.cookienfo.favorites.value = newfav;

    if (found == 0) {
	alert('This server is not in your favorites. You may need to refresh to see any changes.');
	return false;
    } else {
	alert(serverip + ' has been removed from your favorites. You will need to refresh before noticing any changes.');
    }

    // set new cookie;
    document.cookie= name + "=" +
	((newfav) ? newfav : "") +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    var answer = confirm ("Are you sure you want to delete all your favorites?")
	if (! answer) {
	    return false;
	}
    
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

function writeFavButton(action, ImageURL, cookie_expire, cookie_path, cookie_domain) {
    favorites=document.cookienfo.favorites.value;
    serverip=document.resultsform.selectedserverip.value;
    
    remove_favorite_button='<input type=\"image\" name=\"browser_func\" value=\"REMOVE FAVORITE\" src=\"' + ImageURL + '/buttons/remove_fav.gif\" id=\"removefav\" onMouseOver=\"MM_swapImage(\'removefav\',\'\',\'' + ImageURL + '/buttons/remove_fav_mover.gif\',1)\" onMouseOut=\"MM_swapImgRestore()\" onClick=\"javascript: removeFromCookie(\'q4favs\',\'' + cookie_expire + '\',\'' + cookie_path + '\',\'' + cookie_domain + '\')\" style=\"margin-top:3px\" \/>';
    add_favorite_button='<input type=\"image\" name=\"browser_func\" value=\"ADD TO FAVORITES\" src=\"' + ImageURL + '/buttons/add_to_fav.gif\" id=\"addtofavs\" onMouseOver=\"MM_swapImage(\'addtofavs\',\'\',\'' + ImageURL + '/buttons/add_to_fav_mover.gif\',1)\" onMouseOut=\"MM_swapImgRestore()\" onClick=\"javascript: setCookie(\'q4favs\',\'' + cookie_expire + '\',\'' + cookie_path + '\',\'' + cookie_domain + '\')\" style=\"margin-top:3px\" \/>';

    if (action == 'selected') {
	// Find out if the selected server is a favorite;
	var fav_array = favorites.split("::");
	var cnt=0;
	var found=0;
	while (cnt < fav_array.length) {
	    if (fav_array[cnt] == serverip) {
		found="1";
	    }
	    cnt+=1;
	}

	if (found == 1) {
	    favorite_button = remove_favorite_button;
	} else {
	    favorite_button = add_favorite_button;
	}

	// Set the proper favorite button
	MM_setTextOfLayer('favoritebutton','',favorite_button);

    } else {
	// deselcted row, set default button;
	MM_setTextOfLayer('favoritebutton','',add_favorite_button);
    }
}


function validategetnewlist()
{
    // validate gametype
    gametype=document.getElementById("q_gametype").value;
    if (gametype == '------------------')
    {
	alert('Invalid GameType Selected');
	return false;
    }
}
