// JavaScript Document

function openwin(sLocation, winTitleCaption, nWidth, nHeight)
{
	var winFeatures = "menubar=no,toolbar=no,directories=no,personalbar=no,location=no,resizable=no,scrollbars=no,status=yes,width="+nWidth+",height="+nHeight;
	
	newWindow	= window.open("", "winPhotoView", winFeatures);
	winMargins	= "leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\"";
	winImage	= "\n<img src=\""+sLocation+"\" title=\""+winTitleCaption+"\" style=\"cursor:hand;cursor:pointer;\" onClick=\"window.close()\" border=\"0\">";
	newWindow.document.write("<html>\n<head><title>"+winTitleCaption+"</title><\/head>\n<body "+winMargins+">"+winImage+"\n<\/body>\n<\/html>")
	
	newWindow.focus()
}

function trimText(strTarget)
{
	// Remove leading spaces and carriage returns
	while ((strTarget.substring(0,1) == ' ') || (strTarget.substring(0,1) == '\n') || (strTarget.substring(0,1) == '\r'))
	strTarget = strTarget.substring(1,strTarget.length);
	
	// Remove trailing spaces and carriage returns
	while((strTarget.substring(strTarget.length-1,strTarget.length) == ' ') || (strTarget.substring(strTarget.length-1,strTarget.length) == '\n') || (strTarget.substring(strTarget.length-1,strTarget.length) == '\r'))
		strTarget = strTarget.substring(0,strTarget.length-1);
	
	return strTarget;
}

function select_all(checkStatus, chkBoxes)
{
	if( !chkBoxes[0] )
	{
		if( !chkBoxes.disabled )
		{
			chkBoxes.checked = checkStatus;
			change_background(chkBoxes.value, checkStatus);
		}
	}
	else
	{
		for( var i=0; i < chkBoxes.length; i++ )
		{
			if( !chkBoxes[i].disabled )
			{
				chkBoxes[i].checked = checkStatus;
				change_background(chkBoxes[i].value, checkStatus);
			}
		}
	}
	document.getElementById('btnDelete').disabled = !checkStatus;
}

function enable_delete_button(checkStatus, chkBoxes)
{
	if( !chkBoxes[0] )
	{
		document.getElementById('btnDelete').disabled = !chkBoxes.checked;
		document.getElementById('chkCheckAll').checked = chkBoxes.checked
		
	}
	else
	{
		var nCheckedCount	= 0;
		var nDisabledCount	= 0;
		
		for( var i=0; i < chkBoxes.length; i++ )
		{
			if( chkBoxes[i].disabled )
				nDisabledCount++;
			if( chkBoxes[i].checked )
				nCheckedCount++;
		}
		// delete button
		if( nCheckedCount > 0 )
			document.getElementById('btnDelete').disabled = false;
		else
			document.getElementById('btnDelete').disabled = true;
		
		// select all check box
		if( (nCheckedCount + nDisabledCount) == chkBoxes.length )
			document.getElementById('chkCheckAll').checked = true;
		else
			document.getElementById('chkCheckAll').checked = false;
	}		
}

function confirm_delete(confirmMessage)
{
	if( confirm(confirmMessage) )
		document.formList.submit();
	else return false;
}

function change_background(id, checked)
{
	var bgcolor = "#D8F8C4";
	if( !checked )
		bgcolor = "#FFFFFF";
	
	document.getElementById('tr_'+id).style.backgroundColor = bgcolor;
}

function preloadImages() { //v3.0
  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 swapImgRestore() { //v3.0
  var i,x,a=document.sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function swapImage() { //v3.0
  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];}
}