// ----------------------------------
// cross-browser functions

var IE_all_cache = new Object();
function IE_getElementById(id) {
  if (IE_all_cache[id] == null) {
    IE_all_cache[id] = document.all[id];
  }
  return IE_all_cache[id];
}

if (document.all) {
  document.getElementById = IE_getElementById;
  IS_IE = 1;
  var isIE = 1;
} else {
  IS_IE = 0;
  var isMozilla = 1;
}

function writeLayer(layer_name,contents) {
  // IE or Mozilla
  document.getElementById(layer_name).innerHTML = contents;
}

function getFrameById(frame_name) {
  if (IS_IE) {
    frameset = document.frames[frame_name];
  } else { 
    frameset = document.getElementById(frame_name);
  }
  return frameset;

}

// -------------------------------
// String functions 
//

function String_elements(str,sep) {
  var myString = str;
  var count = 1;
  var index = myString.indexOf(sep);

  while (index != -1) {
    count++;
  //  alert(index);
    index = myString.indexOf(sep, index + 1); 
    // start search after last match found
  }

  return count;
}

function String_elementAt(str,sep,elemAt) {
  var myString = str;
  var count = 0;
  var last_index = 0;
  var si;
  var index = myString.indexOf(sep);

  if (index == -1) {
    // alert("part = " + str);
    return str;
  }

  while (index != -1) {

    if (count == elemAt) {
     if (index == -1) {   
       si = myString.length;
     } else {
       si = index;
     }
//     alert("part(" + elemAt + ") = " + last_index + "," + si + ": " + str.substring(last_index,si));
       return str.substring(last_index,si);
    }

    count++;
    last_index = index + 1;
    index = myString.indexOf(sep, index + 1); 
    // start search after last match found

  }

  if (count == elemAt) {
     if (index == -1) {   
       si = myString.length;
     } else {
       si = index;
     }
//     alert("part(" + elemAt + ") = " + last_index + "," + si + ": " + str.substring(last_index,si));
     return str.substring(last_index,si);
  }


  return null;
}


// ===================================================================
// Author: Matt Kruse <matt@mattkruse.com>
// WWW: http://www.mattkruse.com/
// ===================================================================

// getAnchorPosition(anchorname)
//   This function returns an object having .x and .y properties which
//   are the coordinates of the named anchor, relative to the page.
function getAnchorPosition(anchorname) {
	// This function will return an Object with x and y properties
	var useWindow=false;
	var coordinates=new Object();
	var x=0,y=0;
	// Browser capability sniffing
	var use_gebi=false, use_css=false, use_layers=false;
	if (document.getElementById) { use_gebi=true; }
	else if (document.all) { use_css=true; }
	else if (document.layers) { use_layers=true; }
	// Logic to find position
 	if (IS_IE) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_gebi) {
		var o=document.getElementById(anchorname);
		x=AnchorPosition_getPageOffsetLeft(o);
		y=AnchorPosition_getPageOffsetTop(o);
		}
 	else if (use_css) {
		x=AnchorPosition_getPageOffsetLeft(document.all[anchorname]);
		y=AnchorPosition_getPageOffsetTop(document.all[anchorname]);
		}
	else if (use_layers) {
		var found=0;
		for (var i=0; i<document.anchors.length; i++) {
			if (document.anchors[i].name==anchorname) { found=1; break; }
			}
		if (found==0) {
			coordinates.x=0; coordinates.y=0; return coordinates;
			}
		x=document.anchors[i].x;
		y=document.anchors[i].y;
		}
	else {
		coordinates.x=0; coordinates.y=0; return coordinates;
		}
	coordinates.x=x;
	coordinates.y=y;
	return coordinates;
	}

// getAnchorWindowPosition(anchorname)
//   This function returns an object having .x and .y properties which are the coordinates
//   of the named anchor, relative to the window
function getAnchorWindowPosition(anchorname) {
  var coordinates=getAnchorPosition(anchorname);
  var x=0;
  var y=0;
  if (document.getElementById) {
    if (isNaN(window.screenX)) {
      x=coordinates.x-document.body.scrollLeft+window.screenLeft;
      y=coordinates.y-document.body.scrollTop+window.screenTop;
    }
    else {
      x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
      y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
    }
  }
  else if (document.all) {
    x=coordinates.x-document.body.scrollLeft+window.screenLeft;
    y=coordinates.y-document.body.scrollTop+window.screenTop;
  }
  else if (document.layers) {
    x=coordinates.x+window.screenX+(window.outerWidth-window.innerWidth)-window.pageXOffset;
    y=coordinates.y+window.screenY+(window.outerHeight-24-window.innerHeight)-window.pageYOffset;
  }
  coordinates.x=x;
  coordinates.y=y;
  return coordinates;
}

// Functions for IE to get position of an object
function AnchorPosition_getPageOffsetLeft (el) {
  var ol=el.offsetLeft;
  while ((el=el.offsetParent) != null) { ol += el.offsetLeft; }
  return ol;
}
function AnchorPosition_getWindowOffsetLeft (el) {
  return AnchorPosition_getPageOffsetLeft(el)-document.body.scrollLeft;
}	
function AnchorPosition_getPageOffsetTop (el) {
  var ot=el.offsetTop;
  while((el=el.offsetParent) != null) { ot += el.offsetTop; }
  return ot;
}
function AnchorPosition_getWindowOffsetTop (el) {
  return AnchorPosition_getPageOffsetTop(el)-document.body.scrollTop;
}

// Generic dialog

function openGenericDialog(window_id,url,
   /* optional params */
       open_location,newWidth,newHeight,winTop,winLeft) {
  
  
  newWidth = getCookie(window_id + "_size_w",newWidth);
  newHeight = getCookie(window_id + "_size_h",newHeight);
  if (!newWidth || newWidth < 0 || newWidth > 1000) {
    newWidth=600;
    newHeight=700;
  }

  if (open_location == "rel") {
    if (IS_IE) {
      var ev = window.event;
    } else {
      var ev = window.Event;
    }
      winLeft = ev.screenX - newWidth;
      winTop = ev.screenY;
   

  } else {

   winLeft = getCookie(window_id + "_pos_x");
   winTop = getCookie(window_id + "_pos_y");

   if (!winTop || winTop < 0 || winTop > 1000) {
     winTop = 10;
     winLeft = 10;
   }
  }

  var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=" + newWidth + ",height=" + newHeight;
  if (winTop) {
     options = options + ",left=" + winLeft + ",top=" + winTop;
  }

  var win3 = window.open(url,"",options)
}


// This script is free to use as long as long as these lines remain untouched.
// Copyright Jojoxx 2001 / johan@jojoxx.net - http://www.jojoxx.net
function getCookie(Name,default_value){
        if (default_value == null) { default_value = ""; }
	var search = Name + "=";
	if (document.cookie.length > 0){
		offset = document.cookie.indexOf(search);
		if (offset != -1){
			offset += search.length;
			end = document.cookie.indexOf(";", offset);
			if (end == -1) {
				end = document.cookie.length;
			}
			return unescape(document.cookie.substring(offset, end));
		} else {
			return default_value;
		}
	} else {
		return default_value;
	}
}
function setCookie(name, value, expire){
  if (expire == null) {
    var d = new Date();
    expire = d;
    expire.setYear(expire.getYear() + 1);
  }

  document.cookie = name + "=" + escape(value) + ((expire ==null) ? "" : ("; expires=" + expire.toGMTString())) + "; path=/";
}



function verify_floater() {
  openFloater();
}

function openFloater() {
  var newWidth = 200;
  var newHeight= 200;
  var winTop = 10;
  var winLeft = 10;

  newWidth = getCookie("floater_size_w");
  newHeight = getCookie("floater_size_h");
  if (!newWidth || newWidth < 0 || newWidth > 1000) {
    newWidth=200;
    newHeight=200;
  }
 winLeft = getCookie("floater_pos_x");
 winTop = getCookie("floater_pos_y");

 if (!winTop || winTop < 0 || winTop > 1000) {
   winTop = 10;
   winLeft = 10;
 }

  var options = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=" + newWidth + ",height=" + newHeight;
  if (winTop) {
     options = options + ",left=" + winLeft + ",top=" + winTop;
  }

  var url = "/chatop/fl_frm.cs";
  var win3 = window.open("","trakken_floater",options)
  win3.blur();
  if (win3.location.href == "about:blank") {
    win3.location.href = url;
  }

}

// Floater window state save 


function onPageLoadSave(window_id) {
  onMoveWin(window_id);
}

function onPageLoadMove(window_id) {
 winW = getCookie(window_id + "_size_w");
 winH = getCookie(window_id + "_size_h");
 
 if (winW) {
   window.resizeTo(winW,winH);
 }

 winX = getCookie(window_id + "_pos_x");
 winY = getCookie(window_id + "_pos_y");

 if (winX > 0 && winX < 1000) {
 //  alert(winX + " " + winY);
   window.moveTo(winX,winY);
 }

 setTimeout("timeoutHandler('" + window_id + "')",200);
}

function timeoutHandler(window_id) {
  onMoveWin(window_id);
  setTimeout("timeoutHandler('" + window_id + "')",300);
}

function onMoveWin(window_id,w) {
  if (!w) {
      w = window;
  }

  if (navigator.appName.indexOf("Microsoft")!=-1) {
      winX = w.screenLeft;
      winY = w.screenTop;
  } else {
      winX = -1;
      winY = -1;
  }
  setCookie(window_id + "_pos_x",winX);
  setCookie(window_id + "_pos_y",winY);
}

function onPageResize(window_id) {
  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;
     }
  }

  setCookie(window_id + "_size_w",winW);
  setCookie(window_id + "_size_h",winH);
  onMoveWin(window_id);
}

