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 saveIframeUrl(iframe)
{
  var savedUrl = window.frames[iframe].location;
  createCookie("url", savedUrl, "");
}

function setIframeUrl(iframe)
{
  var savedUrl = readCookie("url");
  if(savedUrl == null)
  {
    return;
  }
  else
  {
    window.frames[iframe].location = savedUrl;
  }    
  
  eraseCookie("url");
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}



function setIframeHeight2(el)
{
  setTimeout("changeSize('" + el + "')", 10);
}

function changeSize(el)
{
  var iFrame = document.getElementById(el);
  var height = iFrame.contentWindow.document.body.scrollHeight;
  iFrame.height = height; 
  window.top.scrollTo(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;
	}
}

// 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);
}

// UPLOAD
//

function preUpload(id)
{
    var form = document.forms['aspnetForm'];
    var action = form.elements[id + '_Action'];
    var target = form.target;

    // 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);
}

// POPUPS
//

function popup(url, width, height, winId, scroll) {
  if (winId == null) {
  	time = new Date();
    winId = time.getTime().toString();
  }
  if (!winId.substr(0, 3) == 'rn_')
    winId = 'rn_' + winId;
	var wtop = (screen.height - height) / 2 - 30;
	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=yes,menubar=no,copyhistory=no');
  ref.focus();
}

function returnFromPopup()
{
    window.opener.refreshPage();
    window.close();
}

function returnFromProductPicker(editorID, productID, productName)
{
    var editor = window.opener.document.getElementById(editorID);
    editor.PasteHTML("<table id=\"CARTBOX:" + productID + "\"><tr><td class=\"cute_cart_box\">" + productName + " (ID=" + productID + ")</td></tr><tr><td class=\"cute_cart_description\">Insert description here</td></tr></table>");

    setTimeout('window.close();', 1);
}

function chooseReplacementProduct(editorID, productID, productName)
{
    var editor = window.opener.document.getElementById(editorID);
    editor.value = productID + ':' + productName;
    
    setTimeout('window.close();', 1);
}

function returnFromEmailDropIn(editorID, emailID, emailAddress)
{
    var editor = window.opener.document.getElementById(editorID);
    editor.PasteHTML("<table id=\"EMAILDROPIN:" + emailID + "\"><tr><td class=\"cute_cart_box\">" + emailAddress + " (ID=" + emailID + ")</td></tr></table>");

    setTimeout('window.close();', 1);
}

function returnFromEventPicker(editorID, groupID, groupName)
{
    var editor = window.opener.document.getElementById(editorID);
    editor.PasteHTML("<table id=\"EVENTBOX:" + groupID + "\"><tr><td class=\"cute_event_box\">" + groupName + "</td></tr></table>");

    setTimeout('window.close();', 1);
}

function refreshPage()
{
    window.focus();
    window.location.replace(window.location.href);
}

function ShippingWindow(url){
    remote = window.open (url, "ShippingWindow", "scrollbars=no,height=425,width=475,resizable=yes")
  }
  
function ScrollDiv(divID, count)
{
  var div = document.getElementById(divID);
  var scrollDistance = (count * 24) + 84;
  div.scrollTop = scrollDistance;
}
