function findObj ( theObj, theDoc )
{
	var p, i, foundObj;

	if(!theDoc) theDoc = document;
	if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
	{
		theDoc = parent.frames[theObj.substring(p+1)].document;
		theObj = theObj.substring(0,p);
	}
	if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
	for (i=0; !foundObj && i < theDoc.forms.length; i++) 
		foundObj = theDoc.forms[i][theObj];
	for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
		foundObj = findObj(theObj,theDoc.layers[i].document);
	if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);

	return foundObj;
}

function validateForm ()
{
	var i, p, q, nm, test, num, min, max, errors='', args = validateForm.arguments;
  
	for ( i = 0; i < ( args.length - 2 ); i += 3 )
	{
		test = args [i + 2];
		val = findObj ( args [i] );
		if ( val )
		{
			nm = val.name;
			if ( ( val = val.value ) != "" )
			{
				if ( test.indexOf ( "isEmail" ) != -1 )
				{
					p = val.indexOf ('@');
					if ( p < 1 || p == ( val.length-1 ) )
						errors += "- " + args [i+1] + " must be an e-mail address.\n";
				} else
				if ( test != "R" )
				{
					num = parseFloat ( val );
        			if ( isNaN ( val ) )
						errors += "- " + args [i+1] + " must be a number.\n";
			        if ( test.indexOf ( 'inRange' ) != -1 )
					{
						p = test.indexOf ( ":" );
						min = test.substring ( 8, p );
						max = test.substring ( p + 1 );
						if ( num < min || max < num )
							errors += "- " + args [i+1] + " must be a number between " + min + " and " + max + ".\n";
					}
				}
			} else
			if ( test.charAt (0) == "R" )
				errors += "- " + args [i+1] + " must be entered.\n";
		}
	}
	
	if ( errors )
		alert ( "The following errors were found:\n" + errors );
	return ( errors == '' );
}

function swapImageRestore ()
{
	var i, x, a = document.sr;
	for ( i = 0; a && i < a.length && ( x = a [i] ) && x.oSrc; i++ )
		x.src=x.oSrc;
}

function preloadImages ()
{
	var d = document;
	if ( d.images )
	{
		if ( !d.p ) d.p = new Array ();
		var i, j = d.p.length, a = preloadImages.arguments;
		for ( i = 0; i < a.length; i++ )
		{
			if ( a [i].indexOf ( "#" ) != 0 )
			{
				d.p [j] = new Image;
				d.p [j++].src = a [i];
			}
		}
	}
}

function swapImage ()
{
	var i, j = 0, x, a = swapImage.arguments;
	document.sr = new Array;
	for ( i = 0; i < ( a.length - 2 ); i += 3 )
	{
		if ( ( x = findObj ( a [i] ) ) != null )
		{
			document.sr [j++] = x;
			if ( !x.oSrc ) x.oSrc = x.src;
			x.src= a [i+2];
		}
	}
}

function changeadmincat ( ss )
{
	window.location.replace ( "index.php?page=admin/items&cid=" + ss.value );
}

function changecat ( ss, page )
{
	var f = findObj ('additemsform');
	f.elements ['cid'].value = ss;
	f.elements ['page'].value = page;
	f.submit ();
}

var itemdescpopup = 0;

function itemdescription ( code )
{
	if ( itemdescpopup )
	{
		if ( !itemdescpopup.closed ) itemdescpopup.close ();
	}
	var url = "itemdesc.php?code=" + code;
	itemdescpopup = window.open ( url, "itemdescpopup", "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=no,copyhistory=yes,width=500,height=600,left=100,top=100,screenY=100"); 
}

