


/*
 * All java script logic for the Demanware reference application.
 *
 * The code relies on the prototype.js and scriptaculous.js libraries to
 * be also loaded.
 */

function submitWithValue(formid,name,value)
{
	var form = $(formid);
	
	try{
		element = document.createElement("<input name='"+name+"' type='hidden' value='"+value+"' />");
	}catch(e)
	{
	        element = document.createElement("input");
	        element.setAttribute("name", ""+name);
	        element.setAttribute("type", "hidden");
	        element.setAttribute("value", ""+value);
	}
	form.appendChild(element);
	form.submit();
}

/*
 * Register more initializations here
 */
window.onload = function()
{
}


/*
    Opens a new window with the provided url and dimension. Used
    for Scene7 and other situations.

    @param url the url to open
    @param width the window width
    @param height the window height
*/
function openPopup( url, width, height )
{
    if (url != null)
    {
        if (width != null && height != null)
        {
            window.open(url, "", "width=" + width +", height=" + height +", scrollbars=yes, resizable=yes");
        }
        else
        {
            window.open(url, "", "scrollbars=yes, resizable=yes, location=yes, statusbar=yes, menubar=yes");
        }
    }
}

Effect.Appear2 = function(ele) {
  element = $(ele);
  var options = Object.extend(
  {
  	from: 0.0,
  	to:   element.getOpacity(),
  	// force Safari to render floated elements properly
  	afterFinishInternal: function(effect) 
  	{
    	effect.element.forceRerendering();
  	},
  	beforeSetup: function(effect) 
  	{
    	effect.element.setOpacity(effect.options.from);
    	effect.element.show(); 
  	}
  }, arguments[1] || {});
  return new Effect.Opacity(element,options);
}


var PopupDiv = {
	rti: null,

	show: function(arg)
	{
		$('popupbody').style.top=getTop()+'px';
		$('popupbody').style.left=getLeft()+'px';
		$('popupbody').style.height=(getHeight()-1)+'px';
		$('popupbody').style.width=(getWidth()-scrollerWidth)+'px';
		Effect.Appear2('popupbody');
		window.onresize=resizeHandler;
		window.onscroll=resizeHandler;
		PopupDiv.rti = arg;
		var popup = $(PopupDiv.rti);
		centerComponent(popup);
		popup.scrollTop=0;
		Effect.Appear(PopupDiv.rti);
	},

	close: function()
	{
		Effect.Fade(PopupDiv.rti);
		Effect.Fade('popupbody');
	},
	
	
	showByUrl: function( url, arg, closeTimer, returnUrl )
	{
		var index=0;
		PopupDiv.rti = arg;
		$('popupbody').style.top=getTop()+'px';
		$('popupbody').style.left=getLeft()+'px';
		$('popupbody').style.height=(getHeight()-1)+'px';
		$('popupbody').style.width=(getWidth()-scrollerWidth)+'px';
		$('popupbody').style.top=getTop()+'px';
		window.onresize=resizeHandler;
		window.onscroll=resizeHandler;
		Effect.Appear2('popupbody');
		var popup = $(PopupDiv.rti);
		popup.src = url;
		centerComponent(popup);
		Element.hide(PopupDiv.rti);
		Effect.Appear(PopupDiv.rti);
	}
}

function resizeHandler()
{
	centerVisibleComponent($(PopupDiv.rti));
	$('popupbody').style.top=getTop()+'px';
	$('popupbody').style.left=getLeft()+'px';
	$('popupbody').style.height=getHeight()+'px';
	$('popupbody').style.width=getWidth()+'px';
	$('popupbody').style.top=getTop()+'px';
}

function centerVisibleVPComponent(compo)
{
	var ch=getTop()-compo.clientHeight/2;
	var cw=getLeft()-compo.clientWidth/2;
	compo.style.marginTop =ch + 'px';
	compo.style.marginLeft =cw + 'px';
}

function centerVisibleComponent(compo)
{
	var ch=getTop()-compo.clientHeight;
	var cw=getLeft()-compo.clientWidth;
	compo.style.marginTop =ch + 'px';
	compo.style.marginLeft =cw + 'px';
}

function centerComponent(compo)
{
	compo.style.visibility='hidden';
	compo.style.display='block';
	var ch=getTop()-compo.clientHeight/2;
	var cw=getLeft()-compo.clientWidth/2;
	compo.style.display='none';
	compo.style.visibility='';
	compo.style.marginTop =ch + 'px';
	compo.style.marginLeft =cw + 'px';
}

function getLeft()
{
	var scrollPos;
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		if(typeof window.pageXOffset != 'undefined')
		{
			scrollPos=window.pageXOffset;
		}
		else if(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
		{
			scrollPos=document.documentElement.scrollLeft;
		}
		else if(typeof document.body != 'undefined')
		{
			scrollPos=document.body.scrollLeft;
		}
	} 
	else
	{
		scrollPos=window.pageXOffset;	
	}
//	return 0;
	return scrollPos;
}

function getHeight()
{
	var scrollPos;
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		if(typeof window.innerHeight != 'undefined')
		{
			scrollPos=window.innerHeight;
		}
		else if(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
		{
			scrollPos=document.documentElement.clientHeight;
		}
		else if(typeof document.body != 'undefined')
		{
			scrollPos=document.body.clientHeight;
		}
	} 
	else
	{
		scrollPos=window.innerHeight;	
	}
//	return 10000;
	return scrollPos;
}

function getWidth()
{
	var scrollPos;
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		if(typeof window.innerWidth != 'undefined')
		{
			scrollPos=window.innerWidth;
		}
		else if(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
		{
			scrollPos=document.documentElement.clientWidth;
		}
		else if(typeof document.body != 'undefined')
		{
			scrollPos=document.body.clientWidth;
		}
	} 
	else
	{
		scrollPos=window.innerWidth;	
	}
//	return 2000;
	return scrollPos;
}

function getTop()
{
	var scrollPos;
	if(navigator.appName == "Microsoft Internet Explorer")
	{
		if(typeof window.pageYOffset != 'undefined')
		{
			scrollPos=window.pageYOffset;
		}
		else if(typeof document.compatMode != 'undefined' && document.compatMode != 'BackCompat')
		{
			scrollPos=document.documentElement.scrollTop;
		}
		else if(typeof document.body != 'undefined')
		{
			scrollPos=document.body.scrollTop;
		}
	} 
	else
	{
		scrollPos=window.pageYOffset;	
	}
//	return 0;
	return scrollPos;
}

var EMail = {

	replacementTagId: null,
	url: null,

	login : function(url,replacementTagId)
	{
		EMail.replacementTagId = replacementTagId;
		EMail.url = url;
		// Please insert Cheetah Mail Login-URL
		var loginUrl='';
		var handlerFunc = function(req)
		{
			var text = req.responseText;
			if(text=='OK')
			{
				EMail.signUp(EMail.url,EMail.replacementTagId);
			}

		}

		var errFunc = function(req)
		{
		}

		// add the product
		new Ajax.Request( loginUrl, {method:'get', postBody:null, onSuccess:handlerFunc, onFailure:errFunc});
	},
	
	signUp : function( url,replacementTag)
	{
		var handlerFunc = function(req)
		{
			var popup = $(EMail.replacementTagId);
			popup.innerHTML = req.responseText;
			Element.hide(EMail.replacementTagId);
			Effect.Appear(EMail.replacementTagId);
		}

		var errFunc = function(req)
		{
		}

		// add the product
		new Ajax.Request( url, {method:'post', postBody:postdata, onSuccess:handlerFunc, onFailure:errFunc});
	}
}



/************************************************
 * Returns the width of the windows scrollers 
 ************************************************/
function getScrollerWidth() {
    var scr = null;
    var inn = null;
    var wNoScroll = 0;
    var wScroll = 0;

    // Outer scrolling div
    scr = document.createElement('div');
    scr.style.position = 'absolute';
    scr.style.top = '-1000px';
    scr.style.left = '-1000px';
    scr.style.width = '100px';
    scr.style.height = '50px';
    // Start with no scrollbar
    scr.style.overflow = 'hidden';

    // Inner content div
    inn = document.createElement('div');
    inn.style.width = '100%';
    inn.style.height = '200px';

    // Put the inner div in the scrolling div
    scr.appendChild(inn);
    // Append the scrolling div to the doc
    document.body.appendChild(scr);

    // Width of the inner div sans scrollbar
    wNoScroll = inn.offsetWidth;
    // Add the scrollbar
    scr.style.overflow = 'auto';
    // Width of the inner div width scrollbar
    wScroll = inn.offsetWidth;

    // Remove the scrolling div from the doc
    document.body.removeChild(
        document.body.lastChild);

    // Pixel width of the scroller
    return (wNoScroll - wScroll);
}

