function getRootWindow() {
	var owner = opener;
	
	while (owner.opener != null) {
		owner = owner.opener;
	}
		
	return owner;
}

function AddOneToLightbox(id) {	
	var rootWin = getRootWindow();
	
	if (rootWin.parent.frames[1].defaultLB != null) {	
		rootWin.parent.frames[1].defaultLB.Images.add(id);
	}
	else {
		rootWin.document.iframeaddform.add.value = "1";
		rootWin.document.iframeaddform.image.value = id;
		rootWin.document.iframeaddform.submit();
	}
}

function ChangeLightbox(id) {
	var frame = window.opener? window.opener.parent.frames[1]: parent.frames[1];
	
	try {
		var pbList = document.formlstpasteboards.lstpasteboards;
		
		if (id) {
			if (pbList.options.length > 0 && pbList.options[pbList.selectedIndex].value == id)
				return;
			else if (pbList.options.length > 0)
				pbList.options[pbList.selectedIndex].value = id;
			else
				pbList.options[pbList.options.length] = new Option('Loading...', id, true);
				
			pbList.onSelect = null;
				
		} else if (!id && frame.defaultLB)
			frame.defaultLB.setActive(pbList.options[pbList.selectedIndex].value);
		else {
			var lowerFrame = window.parent.parent.frames["bottomFrame"];
			var myForm = lowerFrame.document.forms["myform"];
			myForm.pb.value = pbList.options[pbList.selectedIndex].text;
		}
    } catch(e) { 
		//alert(e.message); 
	}

    document.formlstpasteboards.submit();
}

function CreateLightbox(isRegistered) {
	if (isRegistered) {
		var frame = window.opener? window.opener.parent.frames[1]: parent.frames[1];
		
		if (frame.defaultLB) {
			frame.defaultLB.add();
			return;
		}
		
		var w = (screen.width / 2) - 150;
		var h = (screen.height / 2) - 100;
		
		window.open(
			'lightboxcreate.asp?filename=results.asp'
			, 'PopupDialog'
			, 'resizable=yes,scrollbars=no,toolbar=no,menubar=no,directories=no,height=200,width=300,top='+h+',left='+w
		);
	} else {
		alert("You are not signed in, so you will not be able to create lightboxes.");
	}
}

function RecoverLightbox(isRegistered) {
	if (isRegistered) {
		var frame = window.opener? window.opener.parent.frames[1]: parent.frames[1];
		
		if (frame.defaultLB) {
			//frame.defaultLB.add();
			return;
		}
		
		var w = (screen.width / 2) - 150;
		var h = (screen.height / 2) - 100;
		
		window.open(
			'lightboxrecover.asp?filename=results.asp'
			, 'PopupDialog'
			, 'resizable=yes,scrollbars=no,toolbar=no,menubar=no,directories=no,height=200,width=300,top='+h+',left='+w
		);
    } else {
        alert("You are not signed in, so you will not be able to recover lightboxes.");
    }
}

function DeleteLightbox(isRegistered) {
	if (isRegistered) {
		var frame = window.opener? window.opener.parent.frames[1]: parent.frames[1];
		
		if (frame.defaultLB) {
			frame.defaultLB.remove();
			return;
		}
		
        var i, LightboxID, LightboxID2, a
        i = document.formlstpasteboards.lstpasteboards.selectedIndex;
        LightboxID = document.formlstpasteboards.lstpasteboards.options[i].value;
        LightboxName = document.formlstpasteboards.lstpasteboards.options[i].text;
        a = confirm("Do you want to delete the lightbox '" + LightboxName + "'?")

        if (a) {
            if (i == 0) 
				LightboxID2 = document.formlstpasteboards.lstpasteboards.options[1].value;
            else
				LightboxID2 = document.formlstpasteboards.lstpasteboards.options[0].value;

            document.location = 'results.asp?btnshow=1&btndelete.x=1&LightboxID=' + 
				LightboxID + '&LightboxID2=' + LightboxID2;
        }
    } else {
        alert("You are not signed in, so you will not be able to delete lightboxes.");
    }
}

function CopyLightbox(isRegistered) {
    if (isRegistered) {
		var frame = window.opener? window.opener.parent.frames[1]: parent.frames[1];
		
		if (frame.defaultLB) {
			frame.defaultLB.copy();
			return;
		}
		
        var w = (screen.width / 2) - 150;
        var h = (screen.height / 2) - 100;
        var i = document.formlstpasteboards.lstpasteboards.selectedIndex;
        var LightboxID = document.formlstpasteboards.lstpasteboards.options[i].value;
        var OldLightboxName = document.formlstpasteboards.lstpasteboards.options[i].text;
        OldLightboxName = escape(OldLightboxName);
		
		window.open(
			'lightboxcopy.asp?filename=results.asp&LightboxID='+ LightboxID + '&OldLightboxName=' + OldLightboxName
			, 'PopupDialog'
			, 'resizable=yes,scrollbars=no,toolbar=no,menubar=no,directories=no,height=200,width=300,top='+h+',left='+w
		);
    } else {
        alert("You are not signed in, so you will not be able to copy lightboxes.");
    }
}

function RenameLightbox(isRegistered) {
    if (isRegistered) {
		var frame = window.opener? window.opener.parent.frames[1]: parent.frames[1];
		
		if (frame.defaultLB) {
			frame.defaultLB.rename();
			return;
		}
		
        var w = (screen.width / 2) - 150;
        var h = (screen.height / 2) - 100;
        var i = document.formlstpasteboards.lstpasteboards.selectedIndex;
        var LightboxID = document.formlstpasteboards.lstpasteboards.options[i].value;
        var OldLightboxName = document.formlstpasteboards.lstpasteboards.options[i].text;
        OldLightboxName = escape(OldLightboxName);
		
		window.open(
			'lightboxrename.asp?filename=results.asp&LightboxID='+ LightboxID + '&OldLightboxName=' + OldLightboxName
			, 'PopupDialog'
			, 'resizable=yes,scrollbars=no,toolbar=no,menubar=no,directories=no,height=200,width=300,top='+h+',left='+w
		);
    } else {
        alert("You are not signed in, so you will not be able to rename lightboxes.");
    }
}

function PrintLightbox() {
	var frame = window.opener? window.opener.parent.frames[1]: parent.frames[1];
	
	if (frame.defaultLB) {
		frame.defaultLB.print();
		return;
	}
	
	var w = 10;
    var h = 10;
	var i = document.formlstpasteboards.lstpasteboards.selectedIndex;
	var LightboxID = document.formlstpasteboards.lstpasteboards.options[i].value;
	
	window.open(
		'lightboxprint.asp?LightboxID='+ LightboxID
		, 'PopupDialog'
		, 'resizable=yes,scrollbars=no,toolbar=no,menubar=no,directories=no,height=560,width=760,top='+h+',left='+w
	);
}

function EmailLightbox(isRegistered) {
    if (isRegistered) {
		var frame = window.opener? window.opener.parent.frames[1]: parent.frames[1];
		
		if (frame.defaultLB) {
			frame.defaultLB.email();
			return;
		}
		
        var w = (screen.width / 2) - 300;
        var h = (screen.height / 2) - 275;
		var i = document.formlstpasteboards.lstpasteboards.selectedIndex;
		var LightboxID = document.formlstpasteboards.lstpasteboards.options[i].value;
        
		//h = 10;
		
		window.open(
			'formlbmail.asp?LightboxID='+ LightboxID
			, 'PopupDialog'
			, 'resizable=yes,scrollbars=no,toolbar=no,menubar=no,directories=no,height=550,width=600,top='+h+',left='+w
		);
    } else {
        alert("You are not signed in, so you will not be able to email lightboxes.");
    }
}

function AdvancedLightbox(isRegistered) {
	if (isRegistered) {
		var i = document.formlstpasteboards.lstpasteboards.selectedIndex;
		var pb = document.formlstpasteboards.lstpasteboards.options[i].value;
		var w = 10;
		var h = 10;
		
		pb = escape(pb);
	
		window.open(
			'lightboxiframes.asp?chknotes=1&selectbox=screenres&pb1=' + pb
			,'LightboxAdvanced'
			,'resizable=yes,scrollbars=yes,toolbar=no,menubar=no,directories=no,height=560,width=760,top='+h+',left='+w
		);
	} else {
		document.formlightboxnotregistered.submit();
	}
}

function ShowParsedQuery(SiteURL) {
	winl = (screen.width) ? parseInt((screen.width - 380)/2) : 0;
	wint = (screen.height) ? parseInt((screen.height - 380)/2) : 0;
	
	newWindow = window.open(
		SiteURL + '/Help/QueryParserResults.asp?'
		,'QueryParser'
		,'height=160,width=420,top='+wint+',left='+winl+',scrollbars=no,resizable=no,menubar=no,directories=no'
	);
	
	if (newWindow.window.focus) 
		newWindow.window.focus();
}