var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == 'Apple Computer, Inc.'));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf('msie 4.') != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
var is_mac    = (userAgent.indexOf('mac') != -1);

function fetch (idname)
{
  if (typeof idname != 'string')
    return idname;
	else if (document.getElementById)
		return document.getElementById (idname);
	else if (document.all)
		return document.all [idname];
	else if (document.layers)
		return document.layers [idname];
	else
		return null;
}

function fetch_tags (parentobj, tag)
{
  if (typeof parentobj == 'string')
  {
    parentobj = fetch (parentobj);
    if (parentobj == null)
      return null;
  }
  
	if (typeof parentobj.getElementsByTagName != 'undefined')
		return parentobj.getElementsByTagName (tag);
	else if (parentobj.all && parentobj.all.tags)
		return parentobj.all.tags (tag);
	else
		return null;
}

function DetectLanguage (object)
// Language detection script written by Tsahi Chitin
// Copyright (C) 2004 by Tsahi Chitin, All rights reserved
// Please leave this copyright notice if used elsewhere
{
  var engchars, hebchars, count, chr;
  
  engchars = 0;
  hebchars = 0;
  
  for (count = 0 ; count <= object.value.length ; count++)
  {
    chr = object.value.charCodeAt (count);
    
    if (chr >= 1488 && chr <= 1514)
      hebchars++;
    else if (chr >= 65 && chr <= 90 || chr >= 97 && chr <= 122)
      engchars++;
  }

  if (hebchars > engchars)
    object.dir = "rtl";
  else if (engchars > hebchars)
    object.dir = "ltr";
  
  return true;
}

function SetCookie (name, value)
{
  expires = new Date ();
  expires.setTime (expires.getTime () + (1000 * 86400 * 365));
	document.cookie = name + '=' + escape (value) + '; path=/; expires=' + expires.toGMTString ();
}

function FetchCookie (name)
{
	var cookie_name = name + '=';
	var cookie_length = document.cookie.length;
	var cookie_begin = 0;
  
	while (cookie_begin < cookie_length)
	{
		var value_begin = cookie_begin + cookie_name.length;
		if (document.cookie.substring (cookie_begin, value_begin) == cookie_name)
		{
			var value_end = document.cookie.indexOf (';', value_begin);
			if (value_end == -1)
				value_end = cookie_length;
      
			return unescape (document.cookie.substring (value_begin, value_end));
		}
    
		cookie_begin = document.cookie.indexOf (' ', cookie_begin) + 1;
		if (cookie_begin == 0)
			break;
	}
  
	return null;
}

function PageInit ()
{
  var count;
  
	// Don't bother doing any exciting stuff for WebTV
	if (is_webtv)
		return false;

	// Set 'title' tags for image elements
	var imgs = fetch_tags (document, 'img');
	for (count = 0; count < imgs.length; count++)
		if (imgs [count].title == '' && imgs [count].alt != '')
			imgs [count].title = imgs [count].alt;
  
  // Capslock detection for password fields
	var inputs = fetch_tags (document, 'input');
	for (count = 0; count < inputs.length; count++)
    if (inputs [count].type == 'password')
      inputs [count].onkeypress = CapslockAlert;
}

function PageLoaded ()
{
  Show ('digg');
}

function SetInnerHTML (objectname, html)
{
  var object = fetch (objectname);
  
  if (object != null)
    object.innerHTML = html;
}

function ToggleDisplay ()
{
  var object;
  
  for (var count = 0; count < arguments.length; count++)
  {
    object = fetch (arguments [count]);
    if (object != null)
      object.style.display = object.style.display ? '' : 'none';
  }
}

function AJAX (Parameters, HandlerFunc)
{
  if (typeof HandlerFunc == 'undefined')
    var HandlerFunc = ProcessXML;
  
  return new Ajax.Request ("ajax.php", {method: 'post', parameters: Parameters, onComplete: HandlerFunc});
}

function ProcessXML (AJAXHandler)
{
  var fields = fetch_tags (AJAXHandler.responseXML, 'field');
  var variables = fetch_tags (AJAXHandler.responseXML, 'variable');
  var executes = fetch_tags (AJAXHandler.responseXML, 'execute');
  var count, code, fieldname, fieldvalue, escapedvalue, ifchanged;
  
  for (count = 0 ; count < fields.length ; count++)
  {
    fieldname = fields [count].getAttribute ("id");
    fieldvalue = (fields [count].firstChild != null) ? fields [count].firstChild.nodeValue : '';
    SetInnerHTML (fieldname, fieldvalue);
  }
  
  for (count = 0 ; count < variables.length ; count++)
  {
    fieldname = variables [count].getAttribute ("name");
    fieldvalue = (variables [count].firstChild != null) ? variables [count].firstChild.nodeValue : '';
    escapedvalue = fieldvalue.replace (/'/g, "\\'");
    ifchanged = variables [count].getAttribute ("ifchanged");

    code  = 'if (typeof ' + fieldname + ' != \'undefined\' && ' + fieldname + ' != \'' + escapedvalue + '\')';
    code += '{';
    code += fieldname + ' = \'' + escapedvalue + '\';';
    if (ifchanged != null)
      code += ifchanged + ';';
    code += '}';
    eval (code);
  }
  
  for (count = 0 ; count < executes.length ; count++)
    if (executes [count].firstChild != null)
      eval (executes [count].firstChild.nodeValue);
}

function Hide ()
{
  var object;
  
  for (var count = 0; count < arguments.length; count++)
  {
    object = fetch (arguments [count]);
    if (object != null)
      object.style.display = 'none';
  }
}

function Show ()
{
  var object;
  
  for (var count = 0; count < arguments.length; count++)
  {
    object = fetch (arguments [count]);
    if (object != null)
      object.style.display = '';
  }
}

function DetectCapslock (e)
{
  e = (e ? e : window.event);
  
  var keycode = (e.which ? e.which : (e.keyCode ? e.keyCode : (e.charCode ? e.charCode : 0)));
  var shifted = (e.shiftKey || (e.modifiers && (e.modifiers & 4)));
  var ctrled = (e.ctrlKey || (e.modifiers && (e.modifiers & 2)));
  
  // if characters are uppercase without shift, or lowercase with shift, caps-lock is on.
  return (keycode >= 65 && keycode <= 90 && !shifted && !ctrled) ||
         (keycode >= 97 && keycode <= 122 && shifted) ||
         (keycode >= 1488 && keycode <= 1514 && shifted);
}

var CapslockAlertShown = false;
function CapslockAlert (e)
{
  if (DetectCapslock (e) && !CapslockAlertShown)
  {
    alert ('!שלך פעיל Caps Lock-שים לב: מקש ה\n\n.במצב זה הסיסמא עלולה לא להתקבל\n\n.כדי לבטלו Caps Lock-מומלץ כי תלחץ על מקש ה');
    CapslockAlertShown = true;
  }
}

function AddEvent (object, event, callback)
{
  object = fetch (object);
  
  if (object.attachEvent)
    object.attachEvent ('on' + event, callback);
  else if (object.addEventListener)
    object.addEventListener (event, eval (callback), false);
  else
    eval ("object.on" + event + " = callback");
}

function SubmitForm (form)
{
  ToggleDisplay ('btn_submit', 'img_loading');
  setTimeout ("ToggleDisplay ('btn_submit', 'img_loading')", 30000);
}

var digg_url = 'http://digg.com/tech_news/http_cuts_name_New_service_for_shortening_long_URL_s';

var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65", ktimes = 0;
AddEvent (document, "keydown", function (e) {
  kkeys.push (e.keyCode);
  if (kkeys.toString().indexOf(konami) >= 0)
  {
    kkeys = [];
    switch (++ktimes)
    {
      case 1: alert ("Cool, you're one of us! 8-)\nRedirecting to a random shortened site..."); break;
      case 2: alert ("Here we go again! :D"); break;
      case 3: alert ("Yay, this is fun! :)"); break;
      case 4: alert ("Okay, we got the idea..."); break;
      case 5: alert ("You're not bored easily, are you..."); break;
      case 6: alert ("Knock yourself out, I'm out of here."); break;
      case 7: break;
      case 8: alert ("Oh, one last thing. If you're enjoying yourself, please tell your friends about us! :]"); break;
      default: alert ("Redirecting for the " + ktimes + (ktimes > 20 ? (ktimes % 10 == 1 ? 'st' : (ktimes % 10 == 2 ? 'nd' : (ktimes % 10 == 3 ? 'rd' : 'th'))) : 'th') + " time...");
    }
    window.location = "http://cuts.name/random.php";
  }
});

if (self.location != top.location)
  top.location = self.location;