function forceInt(x, y)
{
	return isNaN(y = parseInt(x))? 0 : y;
}

function getDivW(el)
{
	return forceInt(
		el ? (el.offsetWidth || el.style.pixelWidth || el.style.width || 0)
		: 0
	);
}

function getDivH(el)
{
	return forceInt(
		el ? (el.offsetHeight || el.style.pixelHeight || el.style.height || 0)
		: 0
	);
}

function getAbsolutePosition(el)
{
	_absX = 0;
	_absY = 0;
	
	while (el)
	{
		_absX += forceInt(el.offsetLeft);
		_absX -= forceInt(el.scrollLeft);

		_absY += forceInt(el.offsetTop);
		_absY -= forceInt(el.scrollTop);
		
		el = el.offsetParent || null;
	}
}

function getOffsetPosition(el)
{
	_absX = 0;
	_absY = 0;
	
	while (el)
	{
		_absX += forceInt(el.offsetLeft);
		_absY += forceInt(el.offsetTop);
		
		el = el.offsetParent || null;
	}
}

function popup(url, width, height, winId, scroll)
{
  if (winId == null) {
  	time = new Date();
    winId = time.getTime().toString();
  }
  if (!winId.substr(0, 3) == 'meisel_')
    winId = 'meisel_' + winId;
	var wtop = (screen.height - height) / 2 - 75;
	var wleft = (screen.width - width) / 2;
	var ref = window.open(url,winId,'width='+width+',height='+height+',top='+wtop+',left='+wleft+
              ',resizable=yes,scrollbars=' + (scroll ? 'yes' : 'no') + ',toolbar=no,location=no,directories=no,status=no,menubar=no,copyhistory=no');
  ref.focus();
}

// CALENDAR
//

function calendarFocus(divID)
{
    // Mark as focused
    eval('window.focus_' + divID + ' = true;');

    // Show popup
    showDiv(divID);
}

function calendarBlur(divID)
{
    // Mark as blurred
    eval('window.focus_' + divID + ' = false;');

    // If not moused over, hide popup
    eval('var over = window.over_' + divID + ';');
    if (!over)
        hideDiv(divID);
}

function calendarOver(divID)
{
    // Mark as moused over
    eval('window.over_' + divID + ' = true;');
}

function calendarOut(divID, e)
{
    if (window.event)
        e = window.event;
        
    var div = document.getElementById(divID);
    if (!div)
        return;

    var eventX = e.clientX + document.body.scrollLeft;
    var eventY = e.clientY + document.body.scrollTop;
    getAbsolutePosition(div);
    var divW = getDivW(div);
    var divH = getDivH(div);

    // Ignore this event if the cursor is inside the div
    if (eventX > _absX && eventX < _absX + divW && eventY > _absY && eventY < _absY + divH)
        return;
    
    // Mark as moused out
    eval('window.over_' + divID + ' = false;');

    // If not focused, hide popup
    eval('var focus = window.focus_' + divID + ';');
    if (!focus)
        hideDiv(divID);
}

// HOVER
//

function hoverOver(o)
{
    _hoverColor = o.style.backgroundColor;
    o.style.backgroundColor = '#CCCCCC';
}

function hoverOut(o)
{
    o.style.backgroundColor = _hoverColor;
}

function hoverClick(o)
{
    _hoverColor = '#EEEEEE';
}

// SCROLL BAR
//

function hScrollMove(div, pagingID, e)
{
    if (window.event)
        e = window.event;

	var input = document.forms['aspnetForm'].elements[pagingID + '_PW'];
	if (input != null)
	{
        var eventX = e.clientX + document.body.scrollLeft;
        getAbsolutePosition(div);
        var divL = eventX - _absX;
        var divW = getDivW(div);
        
        var l = 0;
        if (divW > 0)
            l = divL / divW;

        var w = input.value;

        var index = 1;
        if (w > 0)
            index = forceInt(l / w) + 1;

        div.title = 'Page ' + index;
    }    
}

function hScrollClick(div, pagingID, e)
{
    if (window.event)
        e = window.event;
    
    var eventX = e.clientX + document.body.scrollLeft;
    getAbsolutePosition(div);
    var divL = eventX - _absX;
    var divW = getDivW(div);

	var input = document.forms['aspnetForm'].elements[pagingID + '_SB'];
	if (input != null)
	{
        var sb = input.value;
        
        var i = sb.indexOf('^');
        if (i >= 0)
        {
            sb = sb.substring(0,i) + divL + ':' + divW + sb.substring(i+1);
        }
        
		eval(sb);
	}
	
    //alert('eventX:' + eventX);
    //alert('absX:' + _absX);
    //alert('divW:' + divW);
}

// SHOW/HIDE

function showDiv(divID)
{
    var div = document.getElementById(divID);
    if (!div)
        return;

    div.style.visibility = 'visible';
}

function hideDiv(divID)
{
    var div = document.getElementById(divID);
    if (!div)
        return;

    div.style.visibility = 'hidden';
}

function hideDivTimeout(divID, time)
{
    setTimeout("hideDiv('" + divID + "')", time);
}

// LOADING
//

function showLoader(divID)
{
    var div = document.getElementById(divID);
    if (!div)
        return;

    var width = getDivW(div) + 'px';
    var height = getDivH(div) + 'px';
    getOffsetPosition(div);

    var image = document.createElement('div');
    image.className = 'loaderImage';
    image.style.top = _absY + 'px';
    image.style.left = _absX + 'px';
    image.style.width = width;
    image.style.height = height;
    div.appendChild(image);

    var mask = document.createElement('div');
    mask.className = 'loaderMask';
    mask.style.top = _absY + 'px';
    mask.style.left = _absX + 'px';
    mask.style.width = width;
    mask.style.height = height;
    div.appendChild(mask);
}

// UPLOAD
//
function UpdateFake()
{
  document.forms[0].elements['FakeInput'].value = document.forms[0].elements['<%= Upload.ClientID %>'].value;
}
  
function preUpload(id)
{
    var form = document.forms['aspnetForm'];
    var action = form.elements[id + '_Action'];
    var target = form.target;

    // Show loader
    showLoader(id);

    // Set values
    action.value = 'upload';    
    form.target = id + '_Target';
    
    // Submit form
    form.submit();

    // Restore values
    action.value = '';
    form.target = target;   
}

function postUpload(id, filename)
{
    var hidden = document.getElementById(id + '_SlickBack');
    var slickback = hidden.value.replace("__FILE__", filename);

    eval(slickback);
}

// EXPRESSION EDITOR
//

function insertText(fieldName, value) 
{
	var field = document.forms['aspnetForm'].elements[fieldName];
	if (field == null)
		return;

	field.focus();
	textFocus(field);

	//IE support
	if (document.selection) 
	{
		sel = document.selection.createRange();
		sel.text = value;
	}
	//MOZILLA/NETSCAPE support
	else if (field.selectionStart || field.selectionStart == '0') 
	{
		var startPos = field.selectionStart;
		var endPos = field.selectionEnd;
		field.value = field.value.substring(0, startPos)
								+ value
								+ field.value.substring(endPos, field.value.length);
	} else {
		field.value += value;
	}
}

function insertSelect(fieldName, selectName) 
{
	var select = document.forms['aspnetForm'].elements[selectName];
	if (selectName == null)
		return;
		
	var value = select.options[select.selectedIndex].value;
	
	insertText(fieldName, value);
}

function textFocus(field)
{		
	//IE support
	if (document.selection) 
	{
		if (typeof(field._selection) == "undefined")
			return;

		field._selection.select();
	} 
	//MOZILLA/NETSCAPE support
	else if (field.selectionStart || field.selectionStart == '0')
	{
		//if (typeof(field._selectionStart) == "undefined")
			//return;

		//field.selectionStart = field._selectionStart;
		//field.selectionEnd = field._selectionEnd;
	}
}

function textChange(field)
{
	//IE support
	if (document.selection) 
	{
		field._selection = document.selection.createRange();
	} 
	//MOZILLA/NETSCAPE support
	else if (field.selectionStart || field.selectionStart == '0')
	{
		//field._selectionStart = field.selectionStart;
		//field._selectionEnd = field.selectionEnd;
	}
}

// SLICKBACK
//

function __doSlickBack(eventTarget, eventArgument, contentDivIDs, loaderDivIDs) 
{
  var theForm = document.forms['aspnetForm'];
  
  // Set slickback values
  theForm.__SLICKBACKTARGET.value = eventTarget.split("$").join(":");
  theForm.__SLICKBACKARGUMENT.value = eventArgument;
  
  // Make request
  if (contentDivIDs.length > 0 && supportsXMLHttpRequest())
  {
    // Reflect loading status in div
    if (loaderDivIDs != null && loaderDivIDs.length > 0)
    {
        var divIDs = loaderDivIDs.split(',');
        
        for (i=0; i<divIDs.length; i++)
        {
            showLoader(divIDs[i]);
        }
    }

    // Use XMLHttpRequest
    theForm.__SLICKBACKDIVIDS.value = contentDivIDs;

    var body = processFormElements(theForm);
    postXMLDoc(theForm.action, body);

    // Reset values since the page isn't refreshed
    theForm.__SLICKBACKTARGET.value = '';
    theForm.__SLICKBACKARGUMENT.value = '';
    theForm.__SLICKBACKDIVIDS.value = '';
  }
  else
  {
    // Use vanilla post
    theForm.__SLICKBACKDIVIDS.value = '';
    
    theForm.submit();
  }
}

function processFormElements(theForm) 
{
  var re1 = new RegExp("&", "g");
  var re2 = new RegExp("\\x2B", "g");

  var r = '';
  var count = theForm.elements.length;

  for (i=0; i<count; i++) 
  {
    var element = theForm.elements[i];
    var elementName = element.tagName.toLowerCase();

    if (elementName == "input") 
    {
      if (element.type == "submit" || element.type == "image")
        continue;

      if (element.type == "checkbox" || element.type == "radio")
      {
        if (!element.checked)
          continue;
      }
      
      r += element.name + "=" + element.value.replace(re1, "%26").replace(re2, "%2B") + "&";
    }
    else if (elementName == "textarea")
    {
      r+= element.name + "=" + element.value.replace(re1, "%26").replace(re2, "%2B") + "&";
    }
    else if (elementName == "select") 
    {
      var selectCount = element.options.length;

      for (j=0; j<selectCount; j++) 
      {
        var option = element.options[j];

        if (option.selected == true)
          r += element.name + "=" + option.value.replace(re1, "%26").replace(re2, "%2B") + "&";
      }
    }
  }
  
  //alert(r);
  
  return r;
}

function supportsXMLHttpRequest() 
{
  if (window.XMLHttpRequest)
    return true;
    
  if (window.ActiveXObject) 
  {
    xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
    if (xmlRequest)
      return true;
  }
    
  return false;
}

function postXMLDoc(url, body) 
{
  // Native object
  if (window.XMLHttpRequest) 
  {
    xmlRequest = new XMLHttpRequest();
    xmlRequest.onreadystatechange = xmlRequestChange;
    xmlRequest.open("POST", url, true);
    xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xmlRequest.send(body);
  } 
  // ActiveX object
  else if (window.ActiveXObject) 
  {
    xmlRequest = new ActiveXObject("Microsoft.XMLHTTP");
    if (xmlRequest) 
    {
      xmlRequest.onreadystatechange = xmlRequestChange;
      xmlRequest.open("POST", url, true);
      xmlRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
      xmlRequest.send(body);
    }
  } 
  // No object?!
  else
  {
    alert('XML POST NEEDS IMPLEMENTATION FOR OTHER BROWSERS!!!!');
  }
}

function xmlRequestChange() 
{
  // Loaded
  if (xmlRequest.readyState == 4) 
  {
    if (xmlRequest.status == 200) 
    {
      xmlRequestDone();
    } 
    else 
    {
      document.body.innerHTML = xmlRequest.responseText;
      //alert("There was a problem retrieving the XML data:\n" + );
    }
  }
}

function nodeValue(node, tagName)
{
  var elements = node.getElementsByTagName(tagName);
  if (elements.length == 0)
    return null; 
  
  return elements.item(0).firstChild.nodeValue
}

function xmlRequestDone()
{
  //alert(xmlRequest.responseText);
  //return;

  var root = xmlRequest.responseXML.documentElement;  

  var redirect = nodeValue(root, 'redirect');
  if (redirect)
  {
    window.location = redirect;
    return;
  }

  var theForm = document.forms['aspnetForm'];
  var viewState = theForm.elements['__VIEWSTATE2'];
  if (viewState)
    viewState.value = nodeValue(root, 'viewState');
 
  var divContents = root.getElementsByTagName('divContent');
  for (i=0; i<divContents.length; i++)
  {
    var divContent = divContents.item(i);
    var divID = divContent.getAttribute('id');
    var div = document.getElementById(divID);
    if (div)
      div.innerHTML = divContent.firstChild.nodeValue;
  }
}