// Support for deleting records with Ajax

function v2(a) {
	thermoff = true;
	if (!a) { 
	} else if (a instanceof Array) { 
		try {
		while (flag = a.shift()) {
			errdata = flag;
			switch (flag) {
			case 'J': 
				errdata = a.shift();
				eval(errdata); 
				break;
			case 'JX': 
				errdata = a.shift();
				if (eval(errdata)) {
					errdata = a.shift();
					eval(errdata);
				} else {
					errdata = a.shift();
				} 
				break;
			case 'C': 
				divname = errdata = a.shift();
				elem = document.getElementById(divname);
				if (!elem) {
					errdata = a.shift();
					alert('Location not found on screen: ' + divname + " = " + dumb);
				} else {
					if (divname == "bar") thermoff = false;
					elem.innerHTML = a.shift();
				}
				break;
			case 'CX': 
				divname = errdata = a.shift();
				elem = document.getElementById(divname);
				if (elem) {
					if (divname == "bar") thermoff = false;
					errdata = a.shift();
					elem.innerHTML = errdata;
				}
				break;
			}
		}
		} catch (err) {
			txt = "The response from the server is invalid (" + err + ")\n";
			if (divname)
				flag += ": " + divname;
			txt += flag + ": " + errdata;
			alert(txt);
		}
	} else {
		alert("Problem in v2: " + a);
	}
	if (thermoff)
		popdelayoff();
}

// Loading
function popdelay() {
	scrollx = (document.all)?document.body.scrollLeft:window.pageXOffset;
	scrolly = (document.all)?document.body.scrollTop:window.pageYOffset;
	screenx = (document.all)?document.body.offsetWidth:window.innerWidth;
	screeny = (document.all)?document.body.offsetHeight:window.innerHeight;
	// txt = "scrollx = " + scrollx + "<br/>";
	// txt += "scrolly = " + scrolly + "<br/>";
	// txt += "availWidth = " + screenx + "<br/>";
	// txt += "availHeight = " + screeny + "<br/>";
	leftpadding = ((screenx - 64) / 2) + scrollx;
	toppadding = ((screeny - 14) / 2) + scrolly;
	// txt += "left = " + leftpadding + "<br/>";
	// txt += "top = " + toppadding + "<br/>";
	mb = document.getElementById('delay');
	if (mb) {
		mb.className = "delaydialog";
		mb.style.left = leftpadding + "px";
		mb.style.top = toppadding + "px";
		mb.innerHTML = "<img src='images/loading_loading.gif' border='0'/>";
	    // alert("top = " + toppadding + " left = " + leftpadding);
	}
	msg = document.getElementById('message');
	if (msg)
		msg.innerHTML = ""; 
}
// Loading
function popdelayoff() {
	mb = document.getElementById('delay');
	if (mb) {
		mb.className = "dialoghide";
		mb.innerHTML = "";
	}
}

// Popup HTML
function popupdesc_relative(event,popcnt) {
	if (popcnt > 0) {
		x=event.clientX - 180;
		y=event.clientY + 5;
		scrollx = (document.all)?document.body.scrollLeft:window.pageXOffset;
		scrolly = (document.all)?document.body.scrollTop:window.pageYOffset;
		mb = document.getElementById("popcnt_" + popcnt);
		if (mb) {
			//mb.style.left = scrollx + event.clientX - 180;
			//mb.style.top = scrolly + event.clientY + 5;
			mb.style.position = "relative";
			mb.style.visibility = "visible";
			mb.style.display = "block";
		} else {
			alert("Error: Not found popcnt_" + popcnt);
		}
	}
}

function popupdesc(event,popcnt) {
	if (popcnt > 0) {
		divid = "popcnt_" + popcnt;
		mb = document.getElementById(divid);
		if (mb) {
			image = document.getElementById("image");
			image.innerHTML = mb.innerHTML;
			image.style.position = "absolute";
			image.style.visibility = "visible";
			image.style.display = "block";
			popbox(event,"image",image.clientHeight,image.clientWidth);
		} else {
			alert("Error: Not found popcnt_" + popcnt);
		}
	}
}
function popuphide(popcnt) {
	if (popcnt > 0) {
		mb = document.getElementById("image");
		if (mb) {
			mb.style.visibility = "hidden";
			mb.style.display = "none";
		}
	}
}

function htmlvalid(frm, fld) {
	if (frm.save_cancel.value != "cancel") {
		var oEditor = FCKeditorAPI.GetInstance(fld) ;
		html = oEditor.GetHTML(true);
		msg = "";
		if (html.indexOf("http://") > 0)
			msg = "You are not permitted to include links to external websites";
		else if (html.indexOf("https://") > 0)
			msg = "You are not permitted to include links to external websites";
		else if (html.indexOf("ftp://") > 0)
			msg = "You are not permitted to include links to external websites";
		else if (html.indexOf("news://") > 0)
			msg = "You are not permitted to include links to external websites";
		else if (html.indexOf("<" + "?") > 0)
			msg = "You are not permitted to include any scripting language";
		else if (html.indexOf("?" + ">") > 0)
			msg = "You are not permitted to include any scripting language";
		else if (html.indexOf("<" + "%") > 0)
			msg = "You are not permitted to include any scripting language";
		else if (html.indexOf("%" + ">") > 0)
			msg = "You are not permitted to include any scripting language";
		else if (html.indexOf("<" + "asp:") > 0)
			msg = "You are not permitted to include any scripting language";
		if (msg != "") {
			alert(msg);
			return false;
		}
	}
	return true;
}

function htmlreqd(frm, fld, flddisplay) {
	if (frm.save_cancel.value != "cancel") {
		var oEditor = FCKeditorAPI.GetInstance(fld) ;
		xhtml = oEditor.GetXHTML();
		if(xhtml==""||xhtml==null) {
			alert("You must enter a value for " + flddisplay);
			oEditor.Focus();
			return false;
		}
	}
	return true;
}


// Image popups
function popimage(event,divid,iname,picheight,picwidth) {
	if (iname.length == 0) {
		alert("Image name not found");
		return;
	}
	mb = document.getElementById("image");
	mb.innerHTML = "<img src='" + iname + "' border='1'/>";
	popbox(event,"image",picheight,picwidth);
}

function popbox(event,divid,picheight,picwidth) {
	mb = document.getElementById(divid);
	mb.style.position = "absolute";
	mb.style.display = "block";

	mywidth = myheight = 0;
	scrollx = scrolly = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
    	//Netscape/Firefox compliant
		scrolly = window.pageYOffset;
		scrollx = window.pageXOffset;
		mywidth = window.innerWidth;
		myheight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.offsetWidth ) ) {
		//IE6+ standards compliant mode
		scrolly = document.documentElement.scrollTop;
		scrollx = document.documentElement.scrollLeft;
		mywidth = document.documentElement.offsetWidth;
		myheight = document.documentElement.offsetHeight;
	} else if( document.body && ( document.body.scrollLeft || document.body.offsetWidth ) ) {
		//DOM compliant
		scrolly = document.body.scrollTop;
		scrollx = document.body.scrollLeft;
		mywidth = document.body.offsetWidth;
		myheight = document.body.offsetHeight;
	}
	// alert("scrollx,y " + scrollx + "," + scrolly + "  windowx,y " + mywidth + "," + myheight);
	// alert("pic h,w " + picheight + "," + picwidth + "  clientx,y " + event.clientX + "," + event.clientY);

	picmargin = 20; // Popup margin to window
	picoffset = 15; // Popup offset to cursor
	cx = event.clientX;
	cy = event.clientY;
	if ((cx + picoffset + picwidth + picmargin) < mywidth)
		xx = cx + picoffset;
	else
		xx = cx - picwidth - picoffset;
	if (xx < picmargin)
		left = scrollx + picmargin;
	else
		left = scrollx + xx;
	mb.style.left = left + "px";

	// alert("scrollx=" + scrollx + " clientX=" + cx + " left=" + left);
	
	yy = cy - (picheight / 3);
	if ((yy + picheight + picmargin) > myheight)
		yy = myheight - picheight - picmargin;
	if (yy < picmargin) 
		ytop = scrolly + picmargin;
	else
		ytop = scrolly + yy;
	// alert("scrolly=" + scrolly + " myheight=" + myheight + " document.all=false");
	mb.style.top = ytop + "px";
	mb.style.visibility = "visible";
}
function hideimage(divid) {
	mb = document.getElementById("image");
	if (mb) {
		mb.style.visibility = "hidden";
		mb.style.display = "none";
		mb.innerHTML = "";
	}
}

// Set a subcatid and go to the psc page
function psc(scid, utt) {
	agent.call('ajax_item.php','psc','v2', scid, unescape(utt));
}
function pde(designerid, utt) {
	agent.call('ajax_item.php','pde','v2', designerid, unescape(utt));
}
function pr(obj) {
	ft = document.searchform.quick.value;
	// de = document.searchform.designer.value;
	de = "";
	agent.call('ajax_item.php','pr','v2', ft, de);
}
function itemstaging(sellerid) {
	agent.call('ajax_item.php','itemstaging','v2', sellerid);
}
function itemcurrent(sellerid) {
	agent.call('ajax_item.php','itemcurrent','v2', sellerid);
}
function itemexpired(sellerid) {
	agent.call('ajax_item.php','itemexpired','v2', sellerid);
}
function itemsold(sellerid) {
	agent.call('ajax_item.php','itemsold','v2', sellerid);
}
function itemsoldall(sellerid) {
	agent.call('ajax_item.php','itemsoldall','v2', sellerid);
}

// Button mouseover colours
function menushadeon(cnt, tot) {
	for (ix=1; ix <= tot; ix++) {
		if (ix == cnt) 
			disp = 'none'; // No shade on current menu
		else
			disp = '';
		elem = document.getElementById("shade_" + ix);
		if (elem)
			elem.style.display = disp;
	}
	subcatshow(cnt);
}
function menushadeoff(cnt, tot) {
	for (ix=1; ix <= tot; ix++) {
		elem = document.getElementById("shade_" + ix);
		if (elem) 
		 	elem.style.display = elem.title;
	}
	subcathide(cnt);
}
function makecolour(elem, bg, txt) {
	elem.style.backgroundColor = bg;
	lk = document.getElementById("txt_0");
	if (lk)
		lk.style.color = txt;
}

function subcatshow(cnt) {
	elem = document.getElementById("subcatpops_" + cnt);
	if (elem) {
		placement = findPos(130, 27 + (cnt * 27));
		elem.style.left = placement[0] + "px";
		elem.style.top = placement[1] + "px";
		elem.style.visibility = "visible";
		elem.style.display = "block";
		elem.style.zIndex = 9999;
	}
}
function subcathide(cnt) {
	elem = document.getElementById("subcatpops_" + cnt);
	if (elem) {
		elem.style.visibility = "hidden";
		elem.style.display = "none";
		elem.style.zIndex = 0;
	}
}
function findPos(addleft, addtop) {
	var curleft = curtop = 0;
	obj = document.getElementById("popupanchor");
	if (obj.offsetParent) {
		curleft = obj.offsetLeft + addleft;
		curtop = obj.offsetTop + addtop;
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function unbecome() {
	popdelay();
	agent.call('ajax_join.php','unbecome','v2');
}

function pfriendly() {
	url = window.location.href;
	if (url.indexOf("?") > 0)
		url += "&";
	else
		url += "?";
	url += 'pfriendly=true';
	specs = 'directories=no,height=600,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,top=0,width=800';
	pf = window.open(url,'_pf',specs);
}


// Utilities
// Utilities
// Utilities
function form_to_array(frmname) {
	var result = new Array();
	frm = eval("document." + frmname);
	if (!frm) {
		result.push("ERR: Form " + frmname + " not found");
		return result;
	}
	for (ix = 0; ix < frm.elements.length; ix++) {
		elem = frm.elements[ix];
		if (!elem.disabled) {
			switch (elem.type) {
			case "select":
				result.push("~~" + elem.name);
				result.push(elem.options[elem.selectedIndex].value);
				break;
			case "checkbox":
				result.push("~~" + elem.name);
				if (elem.checked)
					result.push('true');
				else
					result.push('false');
				break;
			case "radio":
				if (elem.checked) {
					result.push("~~" + elem.name);
					result.push(elem.value);
				}
				break;
			default:
				result.push("~~" + elem.name);
				result.push(escape(elem.value));
				break;
			}
		}
	}
	return result;
}

function go(page) {
	window.location = page;
}
function gonw(page) {
	specs = 'directories=no,height=600,left=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no,top=0,width=800';
	somenw = window.open(page,'_gonw',specs);
}
function gopi(e,page) {
	if (!e) e = window.event;
	if (e.target) targ = e.target;
	else if (e.srcElement) targ = e.srcElement;
	if (targ.nodeType == 3) targ = targ.parentNode;
	targname = targ.getAttribute("name");
	if (targname != "sticky")
		window.location = page;
}

var currenturl = window.location.href;
function delete_record_go(clazz, pid, nurl) {
	if (confirm("Are you sure you want to delete this record?")) {
		agent.call('ajax_functions.php','delete_record','v2', clazz, pid, currenturl, nurl);
	}
}

function myreload() {
	ix = currenturl.indexOf("page=");
	if (ix > 0) 
		myurl = currenturl.substring(0,ix);
	else
		myurl = currenturl;
	window.location = myurl;
}

function sleep(milliseconds) {
  var start = new Date().getTime();
  for (var i = 0; i < 1e7; i++) {
    if ((new Date().getTime() - start) > milliseconds){
      break;
    }
  }
}

