// scripts copyright by iWave P/L and Phil Barnard
// version:  OnPixII  290802

//-------------------------/ testing
var objList;
var nodeList;
var theParent;
function dump(obj) {
	objList = new Array(); nodeList = new Array();
	var result = "<div style='font-family:helvetica,arial; font-size:8pt'>", str="", cnt=0;
   	if(obj.parentNode){
		theParent = obj.parentNode;
		result += "<a style='color:blue; text-decoration:none' href=\"javascript:window.opener.dump(window.opener.theParent)\">Dump parent node: </a>&lt;" + theParent.nodeName + "&gt;<br><br>";
	}
	for (var i in obj) {
   		str = "<span style='color:darkgreen'>" + obj[i] + "</span>";  // by default we see the name of the child object
   		type = typeof obj[i];  // "object"
   		if(type == "object" && obj[i]){  // we have an object that is not null
   			objList[cnt] = obj[i];
   			str = "<a style='color:red; text-decoration:none' href=\"javascript:window.opener.dump(window.opener.objList[" + cnt + "])\">" + obj[i] + "</a>";
   			if(obj[i].parentNode){ // this object is a node
   				nodeList[cnt] = obj[i].parentNode;
   				str += " &nbsp; parent node: &lt;<a style='color:blue; text-decoration:none' href=\"javascript:window.opener.dump(window.opener.nodeList[" + cnt + "])\">" + obj[i].parentNode.nodeName + "</a>&gt;";
   			}
   			++cnt;
   		}
   		result += "." + i + " = " + str + "<br>";
   }
	var dumpWin = window.open('','Dump','width=550,height=400,scrollbars=1,status=1,resizable=1')
	dumpWin.document.write(result + "</div>");
	dumpWin.document.close();
}

//-------------------------/ globals

var W3C = document.getElementById;
var IE = /Explorer/.test(navigator.appName);
var NN = /Netscape/.test(navigator.appName);
var isMac = /mac/i.test(navigator.platform);
var isOwner = getCookKey("ONPIX","USR").substring(1);  // leftmost value = 1 for owner and 0 for non-owner
var isFilmEditor = getCookKey("ONPIX","OWN") && !getCookKey("ONPIX","USR").substring(1);
var isAlb = /album/i.test(document.title);// are we called by viewAlbum?
var isDemo = /demo/i.test(getCookKey("ONPIX","OWN"));
var isEditor = /edit/i.test(document.title);

var demoComment = "<br>Sorry, this is a demonstration film and we can\'t save your changes."
var nonMember = "This feature is only available to Members, why not join now?<br>";

var popScrollHelpWinObj;
function goHelp(opt,where){
	if(!opt && !where){ popWin("help.asp",434,295); return;}  // if no params, default to showing help.asp
	if(popScrollHelpWinObj && !popScrollHelpWinObj.closed) popScrollHelpWinObj.close();
	var w = 556;
	var h = (opt=="credits") ? 360 : 480;
	var features = "left=20,top=20,width="+w+",height="+h+",scrollbars=1,resizable=1";
	opt += ".asp";
	if (where) opt+="#" + where;
	popScrollHelpWinObj = window.open(opt,"onpixiihelp",features);
}

function writeFooter() {
	var str = "<span class=txt1><a class=buttons href='javascript:goHelp(\"privacy\");'>PRIVACY STATEMENT</a> | ";
	str += "<a class=buttons href='javascript:goHelp(\"terms\");'>TERMS</a> | ";
	str += "<a class=buttons href='javascript:goHelp(\"faq\");'>HELP</a> | ";
	str += "<a class=buttons href='javascript:goHelp(\"credits\");'>SITE CREDITS</a></span>";
	document.write(str);
}

//-------------------------/ layer utilities


function move(id,t,l){
	document.getElementById(id).style.top = t; document.getElementById(id).style.left = l;
}

function show(id){
	document.getElementById(id).style.visibility="visible";
}

function hide(id){
	document.getElementById(id).style.visibility="hidden";
}

function dw(id,s){
	document.getElementById(id).innerHTML = s;
}

//------------------------/ menu utilities

// create obj with menu colouring
// var menuStyle =  {overitem:'navy',outitem:'#D4D0C8',overtag:'#FFFFFF',outtag:'black',tagdown:'gold'}
var menuStyle =  {overitem:'white',outitem:'#D4D0C8',overtag:'#990033',outtag:'black',tagdown:'orange'}
var currentMenu;
var menuOK;

function itemOver(elem){
	elem.style.backgroundColor = menuStyle.overitem;
	elem.children.tags('A')[0].style.color = menuStyle.overtag;
}

function itemOut(elem){
	elem.style.backgroundColor = menuStyle.outitem;
	elem.children.tags('A')[0].style.color = menuStyle.outtag;
}

function itemDown(elem){
	document.onmousedown = null;
	elem.children.tags('A')[0].style.color = menuStyle.tagdown;
}

function itemUp(my){
	hidemenu();
	location.href = my.children.tags('A')[0].href;
}

function showmenu(menuname){
	if(!menuOK) return;
	if(currentMenu) hidemenu();
	document.getElementById(menuname).style.visibility = "visible";
	currentMenu = menuname; document.onmousedown = hidemenu;
}

function hidemenu(){
	if(!currentMenu) return;
	document.getElementById(currentMenu).style.visibility = "hidden";
	document.onclick = null;  currentMenu = null;
}

//------------------------/ cookie utilities

// return key value in named cookie, or ""
function getCookKey(nm,ke){
	var re = new RegExp ("^"+nm+"=([^;]*).*|.+;\\s?"+nm+"=([^;]*).*|.*","i");
	var c = document.cookie.replace(re,"$1$2");  // extract the cookie
	if(!c) return "";
	re = new RegExp ("^"+ke+"=([^&]*).*|.+&\\s?"+ke+"=([^&]*).*|.*","i");
	return unescape(c.replace(re,"$1$2"));  // extract the key value
}

// get key value from search args 
function getSearchKey(ke){
	ke = ke.toLowerCase();
	start = location.search.toLowerCase().indexOf(ke+"=");
	if(start == -1) return "";
	end = location.search.toLowerCase().indexOf("&", start);
	if(end == -1) end = location.search.length;
	return unescape(location.search.substring(start+ke.length+1, end))
}

//------------------------/ string utilities

function badEmail(str){
	if(!str || /[\s#&^+]/.test(str)) return true;  // empty str or bad chars
	var parts = str.split("@");  // check for x@x structure
	if(parts.length != 2 || parts[0].length == 0 || parts[1].length == 0) return true;
	var dom = parts[1].split("."); // check domain for minimum of x.x structure
	if(dom.length < 2) return true;
	for(i in dom) if(dom[i].length < 1) return true;
	return false;  // false means the address is valid
}

function toCurrency(n){
	n = (Math.round(n * 100) / 100).toString();
	n += /\./.test(n) ? "00" : ".00";
	return n.replace(/(\d*\.\d\d).*/,"$1");  // truncate to 2 chars after .
}

// mirrors the clean() function in DBA.inc for corresponding client-side cleaning
var cleanJS = new RegExp("[\\\\<>\'\"]","g");
var cleanPath = new RegExp("[<>/\'\\\\:*?|\"]","g");
var cleanSQL = new RegExp("[%_*?]","g");
var cleanPass = new RegExp("[\'\\s]","g");
var cleanTXT = new RegExp("[\\\\<>\'\"\r\n\t]","g");

function check(str,re){
	return re.test(str); // return true if nasties found, else false
}

function clean(str,re){
	return str.replace(re,""); // replace nasties with empty string
}

function escapeQuotes(str){
	return str.replace(/([\'\"])/g,"\\$1");
}

//----------------------------/  other utilities

function isProAlbum(stl){  // return true or false based on the album style number
	return stl > 99 && stl < 200;
}

function preload(){  // preload images;  preload("img/one.gif","two.gif","three.gif")
	var imgs = new Array();
	for (var i=0; i < preload.arguments; i++) {
		imgs[i] = new Image(); imgs[i].src = preload.arguments[i];
	}
}

// stores an image's top left co-ords in global vars T, L
var T,L;
function imgTL(imgName){
	var imgObj = document.images[imgName];
	if (document.all) {
		L = imgObj.offsetLeft;
		T = imgObj.offsetTop;
		leftEdge = imgObj.offsetParent;
		rightEdge = imgObj.offsetParent;
  		while (leftEdge) {
			L += leftEdge.offsetLeft;
			leftEdge = leftEdge.offsetParent;
		}
		while (rightEdge) {
			T += rightEdge.offsetTop;
			rightEdge = rightEdge.offsetParent;
		}
		if (/Mac/i.test(navigator.userAgent)) {
			T += 15;
			L += 10;
		} 
	}
	if (!document.all) {
		T=imgObj.y; L=imgObj.x;
	}
}

function getSel(){
	var f = document.forms[0], selList = "";
	if(!f.sel) return selList;  // no checkbox elements on page
	if(!f.sel.length){ return (f.sel.checked ? refs[0] : "") } // one single checkbox
	for(var i=0; i<f.sel.length; i++){  // more than one checkbox so sel is now an array
		if(f.sel[i].checked) selList += "|" + refs[f.sel[i].value];
	}
	return selList.substring(1);  // remove leading pipe, let VB take care of it's own lack of trailing pipe
}

var popWinObj;
function popWin(url,w,h){
	if(popWinObj && !popWinObj.closed) popWinObj.close();
	if(!w) w=750; if(!h) h=480;
	var features = "left=10,top=10,width="+w+",height="+h+",scrollbars=0,resizable=0";
	popWinObj = window.open(url,"onpix",features);
}

var popScrollWinObj;
function popScrollWin(url,w,h){
	if(popScrollWinObj && !popScrollWinObj.closed) popScrollWinObj.close();
	if(!w) w=750; if(!h) h=480;
	var features = "left=20,top=20,width="+w+",height="+h+",scrollbars=1,resizable=1";
	popScrollWinObj = window.open(url,"onpixii",features);
}

//-------------------/ statistics

function stats(){
	new Image().src = "/writestats.asp?" + escape(document.title + "\t" + document.referrer);
}

function sendPageStats(){
if(logging) stats();	
	//	setTimeout("stats()",6000);
}

