﻿var _absX = 0;
var _absY = 0;

function forceInt(x, y)
{
	return isNaN(y = parseInt(x))? 0 : y;
}

function getViewportWidth()
{
    return self.innerWidth || (document.documentElement.clientWidth || document.body.clientWidth);
}

function getViewportHeight()
{
    return self.innerHeight || (document.documentElement.clientHeight || document.body.clientHeight);
}

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)
	{
        if (el.style.overflow == 'auto')
            break;

        if (el.style.position == 'absolute' || el.style.position == 'relative')
            break;
	
		_absX += forceInt(el.offsetLeft);
		_absY += forceInt(el.offsetTop);
		
		el = el.offsetParent || null;
	}
}

// MENUS
//

function Launch(page) { 
OpenWin = this.open(page, "KirupaStyle", "toolbar=no,menubar=no,location=no,scrollbars=no,resizable=yes,width=628,height=400"); 
} 

function MM_findObj(n, d) { //v4.01
  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_showHideLayers() { //v6.0
  var i,p,v,obj,args=MM_showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function MM_swapImgRestore() { //v3.0
  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_preloadImages() { //v3.0
  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_swapImage() { //v3.0
  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 swapLink(e,c){
    var imageLink = document.getElementById("BigLink");
    imageLink.href = e;

    var caption = document.getElementById("BigCaption");
    caption.innerHTML = c;
}

// 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 refreshPage()
{
    window.focus();
    window.location.replace(window.location.href);
}

function divChange(e,d)
{
    var closeDiv = document.getElementById(e)
    var openDiv  = document.getElementById(d)
    
    closeDiv.style.display = 'none';
    openDiv.style.display  = '';
}
// 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);
}

function resizeIframe()
{
  var flyoutCart = document.getElementById('flyoutCart');
  var iFrame = document.getElementById('iFrame');
  alert(flyoutCart);
  alert(iFrame);
  
  var height = getDivH(flyoutCart);
  
  iFrame.style.height = height;
}

function goToLink(pSelect)
{
  if(pSelect[pSelect.selectedIndex].value.length == 0)
  {
    return;
  }
  else if (pSelect[pSelect.selectedIndex].value.substring(0, 4) == "http")
  {
    window.open(pSelect[pSelect.selectedIndex].value,'_blank');
  }
  else if (pSelect[pSelect.selectedIndex].value.substring(0, 3) == "www")
  {
    window.open("http://" + pSelect[pSelect.selectedIndex].value, '_blank');
  }
  else
  {
    window.open(pSelect[pSelect.selectedIndex].value, '_blank');
  }
}


// IMAGE TIPS
//

var _curImageTip = null;

function imageTipOver(id, e)
{
    if (_curImageTip)
        imageTipOut(_curImageTip.id);

    _curImageTip = document.getElementById(id);
    if (!_curImageTip)
        return;
    
    // Show tip
    imageTipMove(e);
    _curImageTip.style.visibility = 'visible';
}

function imageTipOut(s)
{
    if (!_curImageTip)
        return;

    // Hide tip
    _curImageTip.style.visibility = 'hidden';
    _curImageTip = null;
}

function imageTipMove(e)
{
    if (!_curImageTip)
        return;

    if (window.event) 
        e = window.event;
    
    // Move tip near cursor
    getAbsolutePosition(_curImageTip.offsetParent);
    var tipWidth = getDivW(_curImageTip);
    var tipHeight = getDivH(_curImageTip);
    var windowHeight = getViewportHeight();
    
    var x = forceInt(e.clientX - tipWidth - 20);
    var y = forceInt(e.clientY - (tipHeight / 2));
    
    var testY;
    testY = forceInt(e.clientY - (tipHeight / 2));
    if (testY < 0)
    {
        y -= testY;    
    }
    else
    {
        testY = forceInt(windowHeight - e.clientY - (tipHeight / 2));
        if (testY < 0)
            y += testY;
    }

    _curImageTip.style.left = (x - _absX) + 'px';
    _curImageTip.style.top = (y - _absY) + 'px';
}
