
function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}


function popup(divId) {
	var div = $(divId);
	if(div==null) {
		alert("unable to get element:" + divId);
		return;
	}
	var bgdiv = $('popupbackground');
	if(bgdiv==null) {
		alert("unable to get element:popupbackground");
		return;
	}
	var height = f_clientHeight();
	var width = f_clientWidth();
	
	if(width < document.body.clientWidth) {
		width = document.body.clientWidth;
	}
	
	
	var pos = Position.cumulativeOffset(div);
	
	body_height = pos[1] + div.clientHeight + 10;
		
	if(height < body_height) {
		height = body_height;
		bgdiv.style.top = "0px";
	}
	pos = Position.cumulativeOffset($('body-bottom'));
		
	body_height = pos[1] + 2;
	if(height < body_height) {
		height = body_height;
		bgdiv.style.top = "0px";
	} else {
		bgdiv.style.top = f_scrollTop() + "px";
	}
	
	bgdiv.style.height = height + "px";
	bgdiv.style.width = width + "px";
	
	bgdiv.style.left = f_scrollLeft() + "px";
	bgdiv.style.zIndex = 3000;
	bgdiv.style.visibility="visible";
	bgdiv.style.display="";
	
	
	div.style.zIndex = 3001;
	div.style.visibility="hidden";
	div.style.display="";
	
	if( ((((f_clientHeight() / 2) - (div.clientHeight/2))) + f_scrollTop()) <10 ) {
		div.style.top = "10px";
	} else {
		div.style.top = (((f_clientHeight() / 2) - (div.clientHeight/2))) + f_scrollTop() + "px";
	}
	
	div.style.left = (((f_clientWidth()  / 2) - (div.clientWidth/2))) + f_scrollLeft() + "px";
	div.style.visibility="visible";
}

function closePopup(divId) {
	var div = $(divId);
	if(div==null) {
		alert("unable to get popup element");
		return;
	}
	var bgdiv = $('popupbackground');
	if(bgdiv==null) {
		alert("unable to get background element");
		return;
	}
	bgdiv.style.visibility="hidden";
	bgdiv.style.display="none";
	div.style.visibility="hidden";
	div.style.display="none";
}

function repositionPopup(divId) {
	var div = $(divId);
	
	if(div==null) {
		alert("unable to get popup element");
		return;
	}
	if(div.style.display=='none') {
		return;
	}
	
	if( ((((f_clientHeight() / 2) - (div.clientHeight/2))) + f_scrollTop()) <10 ) {
		div.style.top = "10px";
	} else {
		div.style.top = (((f_clientHeight() / 2) - (div.clientHeight/2))) + f_scrollTop() + "px";
	}
	
	div.style.left = (((f_clientWidth()  / 2) - (div.clientWidth/2))) + f_scrollLeft() + "px";
	
	
	
	var bgdiv = $('popupbackground');
	if(bgdiv==null) {
		alert("unable to get element:popupbackground");
		return;
	}
	var height = f_clientHeight();
	var width = f_clientWidth();
	
	if(width < document.body.clientWidth) {
		width = document.body.clientWidth;
	}
	
	//if(height < document.body.clientHeight) {
	//	height = document.body.clientHeight;
	//}
	

	var pos = Position.cumulativeOffset(div);
	
	body_height = pos[1] + div.clientHeight + 10;
	
		
	if(height < body_height) {
		height = body_height;
		bgdiv.style.top = "0px";
	}
	pos = Position.cumulativeOffset($('body-bottom'));
		
	body_height = pos[1] + 2;
	if(height < body_height) {
		height = body_height;
		bgdiv.style.top = "0px";
	} else {
		bgdiv.style.top = f_scrollTop() + "px";
	}
	
	bgdiv.style.height = height + "px";
	bgdiv.style.width = width + "px";
	
	bgdiv.style.left = f_scrollLeft() + "px";
}

function startAsyncAction() {
	$('status-bar').style.display='';
}

function finishAsyncAction() {
	$('status-bar').style.display='none';
}


function clipboard(obj)
{
	var copied = false;
	if (window.clipboardData) {
		window.clipboardData.setData("Text", obj.value);
		copied = true;
	} else if (window.netscape) {
		try {
			netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
			var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIclipboard);
			if (!clip) {
				alert("no clip");
				return;
			}
			var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
			if (!trans) {
				alert("no trans");
				return;
			}
			trans.addDataFlavor('text/unicode');
			var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
			var copytext=obj.value;
			str.data=copytext;
			trans.setTransferData("text/unicode",str,copytext.length*2);
			var clipid=Components.interfaces.nsIclipboard;
			clip.setData(trans,null,clipid.kGlobalclipboard);
			copied = true;
		} catch(ex) {
			return;
		}
	}
	if(copied) {
		var sb = $('cb_saved');
		var pos = Position.cumulativeOffset(obj);
		sb.style.left = (pos[0] - 65) + "px";
		sb.style.top = (pos[1] - 3) + "px";
		Effect.Appear('cb_saved', { duration: 1.0, afterFinish: function() { Effect.Fade('cb_saved', { duration: 1.0});} });
	}
	return false;
}

var sbWindow;

function goPrint(type, id) {
	//get the window if it does not exist
	if((sbWindow != null)&&(!sbWindow.closed && sbWindow.location)) {
		sbWindow.location.href = "/ps/user/designer/" + type + "?sl=1&sg=" + id;
	} else {	
		sbWindow = window.open("/ps/user/designer/" + type + "?sl=1&sg=" + id, "printer", "height=650,width=850,menubar=no,toolbar=no,scrollbars=yes");
	}
	if(sbWindow==null) {
		alert("Unable to open designer window. Check your popup blocker setings and try again");
		return;
	}
	sbWindow.focus();
}

function goPrintold(type, id) {
	//get the window if it does not exist
	if((sbWindow != null)&&(!sbWindow.closed && sbWindow.location)) {
		sbWindow.location.href = "/ppr/designer/" + type + "?sl=1&sg=" + id;
	} else {	
		sbWindow = window.open("/ppr/designer/" + type + "?sl=1&sg=" + id, "printer", "height=650,width=850,menubar=no,toolbar=no,scrollbars=yes");
	}
	if(sbWindow==null) {
		alert("Unable to open designer window. Check your popup blocker setings and try again");
		return;
	}
	sbWindow.focus();
}

function notifyCartChanged() {
	var t1 = new Ajax.Updater('cart_info' , '/ps/home/cart_info', {asynchronous:true, evalScripts:true});
}
