﻿// JScript File - Images

var PrdSml=768;
var PrdLrg=1025;
var DefSml=768;
var DefLrg=1025;
var InsSml=0;
var InsLrg=0;
var PrdWid=0;
var PrdHgt=0;
var PrdOrn="N";
var PrdSid=0;

var DspWid=300;
var DspHgt=0;
var FulWid=0;
var FulHgt=0;
var ZomWid=0;
var ZomHgt=0;
var ActWid=300;
var ActHgt=0;
var ImgSpc="";
var DsnSpc="";
var TxtSpc="";
var MgmSpc="";

var DecWid=new Array();
var DecTWd=0;
var DecBWd=0;
var DecLWd=0;
var DecRWd=0;
var MrgWid=0;
var MrgAWd=0;

var AlwMlt=true;
var ImgSel=false;
var ImgCnt=0
var ImgIdx=0;
var ImgLst=new Array();
var ILdPnd=false;

var PanLst=new Array();
var PanSel=0;
var PanOvr=0;
var OrgImg=null;
var WrkImg=null;
var CfmImg=null;

var ImgXPs=0;
var ImgYPs=0;

var DspMod=0;
var CmpMod=0;
var SavMod=0;

var BckCol=0;

var ReqCnt=0;
var ZPrTOt=0;

function IniImg()
{
	OrgImg=document.getElementById("imgDspOrg");
	WrkImg=document.getElementById("imgDspWrk");
	CfmImg=document.getElementById("imgDspCfm");
}

function SetSiz()
{
  var Ret=false;
	var Ctl=document.getElementById("imgDspWrk");
	ImgXPs=0;
	ImgYPs=0;
  ActWid=Ctl.width;
	ActHgt=Ctl.height;
	while (Ctl)
	{
		ImgXPs+=Ctl.offsetLeft;
		ImgYPs+=Ctl.offsetTop;
		Ctl=Ctl.offsetParent;
	}
	if (ImgXPs>0 && ImgYPs>0) Ret=true;
	return Ret;
}

function ImgULd()
{
	var Msg=document.getElementById("frmImgULd").contentWindow.document.getElementById("txtULdMsg").value;
	var INm=document.getElementById("frmImgULd").contentWindow.document.getElementById("txtImgFNm").value;
	var Lst;
	var IWd;
	var IHt;
	var DPI;
	document.getElementById("frmImgULd").contentWindow.document.getElementById("cmdLodImg").src=PLbPth+"StrULd.jpg";
	if (Msg>"")
	{
	  Lst=Msg.split("|");
	  if (Lst[0]=="OK")
	  {
			IWd=1*Lst[1]; 
			IHt=1*Lst[2];
			DPI=1*Lst[3];
			RStDef=true;
			ImgSel=false;
	    AddILs(INm,IWd,IHt,DPI,"N");
	    PrpImg();
	  }
		else
		{
			alert("Error Uploading Image: "+Msg);
		}
	}
	return false;
}

function PrpImg()
{
	document.getElementById("imgDspWrk").style.display="none";
	document.getElementById("txtPrdDsc").innerHTML="Loading - Please Wait . . .";
	GetSpc(CerNum,ImgLst[ImgIdx].INm,ImgLst[ImgIdx].Orn)
}

function ImgBck(result, eventArgs)
{
	var Buf;
	var Msg;
	Buf=result.split('|');
	Msg=Buf[0]
	if (Msg=="OK")
	{
		ImgLst[ImgIdx].IWd=1*Buf[1]; 
		ImgLst[ImgIdx].IHt=1*Buf[2];
		ImgLst[ImgIdx].DPI=1*Buf[3];
		ImgLst[ImgIdx].Gry=false;
		ImgLst[ImgIdx].PIx=99;
		if (ImgLst[ImgIdx].Orn=="N") ImgLst[ImgIdx].Orn=Buf[4];
		LodImg(ImgIdx);
		SelILs(ImgIdx);
		SetUpd(1);
	}
	else
	{
		document.getElementById("txtImgDsc").innerHTML=Msg;		
	}
}

function DspImg(IIx)
{
  var Wid=DspWid;
  var Hgt;
  if (Wid==0)
  {
		if (ImgLst[IIx].Orn=="V")  Wid=300;
		if (ImgLst[IIx].Orn=="H")  Wid=375;
	}
	Hgt=Wid*(ImgLst[IIx].IHt/ImgLst[IIx].IWd);
	if (Hgt>350)
	{
		Wid=350*(ImgLst[IIx].IWd/ImgLst[IIx].IHt);
	}
	OrgImg=document.getElementById("imgDspOrg");
	OrgImg.width=Wid;
	OrgImg.src=ImgLst[IIx].BIm.src;
	WrkImg=document.getElementById("imgDspWrk");
	WrkImg.width=Wid;
	CfmImg=document.getElementById("imgDspCfm");
	CfmImg.width=Wid;
	return false;
}

function DspPrd()
{
	var Cmd=""
	var Idx=0;
	CfgOrn(PrdOrn);
	if (PrdWid>0 && PrdHgt>0 && WrkImg!=null && CfmImg!=null)
	{
		if ((ActSec!=8 && UpdPnd) || (ActSec==8 && CmpPnd))
		{
			if (ActSec==8) document.getElementById("WrkFrm").style.cursor="wait"
		  ReqCnt++;
			WrkImg.width=DspWid;
			CfmImg.width=DspWid;
			ImgSpc=NumRow + "|" + NumCol + "|";
			for (Idx=0; Idx<PanLst.length; Idx++)	BldSpc(Idx,PanLst[Idx].IIx)
			OrgImg.style.display="none";
			WrkImg.style.display="none";
			CfmImg.style.display="none";	
			if (ActSec==6) ShwTxt(false);
			Cmd="BldImg.ashx"
			Cmd+="?OrdIDN=" + OrdIDN + "&CerNum=" + CerNum + "&DspMod=" + DspMod + "&CmpMod=" + CmpMod;
			Cmd+="&SavMod=0"         + "&ImgSpc=" + ImgSpc + "&StkIDN=" + StkIDN + "&SelImg=" + PrdSid;
			Cmd+="&PrdWid=" + PrdWid + "&PrdHgt=" + PrdHgt + "&PrdOrn=" + PrdOrn + "&BrdOrn=" + BrdOrn;
			Cmd+="&SizCod=" + SizCod + "&BrdIDN=" + BrdIDN + "&BrdTBd=" + BrdTBd + "&BrdIPt=" + BrdIPt;
			Cmd+="&DecTyp=" + DecTyp + "&DspWid=" + DspWid + "&BrdSID=" + BrdSID + "&ReqCnt=" + ReqCnt;
			document.getElementById("txtImgDsc").innerHTML="Loading Product Image . . ."; 
			if (ActSec==8)	
			{
				CmpPnd=false;
				CfmImg.src=Cmd;
			}
			else						
			{
			  UpdPnd=false;
				WrkImg.src=Cmd;
			}
		}
		else UpdDsp();
	}
	else
	{
		document.getElementById("txtImgDsc").innerHTML="";
	}
}

function BldZom()
{
	var Cmd=""
	CalZom();
	ZomPnd=true;
	ZPrTOt=setTimeout("ZomTOt()",60000);
	document.getElementById("divZomCmd").style.display="none";
	document.getElementById("divZomMsg").style.display="";
	document.getElementById("divZomMsg").innerHTML="Please wait - Building Zoom Image . . .";
	Cmd="BldImg.ashx"
	Cmd+="?OrdIDN=" + OrdIDN + "&CerNum=" + CerNum + "&DspMod=" + DspMod + "&CmpMod=" + CmpMod;
	Cmd+="&SavMod=" + SavMod + "&ImgSpc=" + ImgSpc + "&StkIDN=" + StkIDN + "&SelImg=" + PrdSid;
	Cmd+="&PrdWid=" + PrdWid + "&PrdHgt=" + PrdHgt + "&PrdOrn=" + PrdOrn + "&BrdOrn=" + BrdOrn;
	Cmd+="&SizCod=" + SizCod + "&BrdIDN=" + BrdIDN + "&BrdTBd=" + BrdTBd + "&BrdIPt=" + BrdIPt;
	Cmd+="&DecTyp=" + DecTyp + "&DspWid=" + ZomWid + "&BrdSID=" + BrdSID + "&ReqCnt=" + ReqCnt;
	document.getElementById("imgZomImg").src=Cmd;
}

function OrgCmp()
{
	UpdDsp();
}

function WrkCmp()
{
  UpdDsp();
  SndDsn();
}

function CfmCmp()
{
  UpdDsp();
  SndDsn();
  BldZom();
}

function ZomCmp()
{
  ZomPnd=false;
  document.getElementById("WrkFrm").style.cursor="default"
  document.getElementById("divZomCmd").style.display="";
	document.getElementById("divZomMsg").style.display="none";
	clearTimeout(ZPrTOt);
}

function ZomTOt()
{
	document.getElementById("divZomMsg").innerHTML="Time out building Zoom Image";
	ZomPnd=false;
	CmpPnd=true;
}

function UpdDsp()
{
	var PSz="";
	document.getElementById("divDspOrg").style.display="none";
	document.getElementById("divDspWrk").style.display="none";
	document.getElementById("divDspCfm").style.display="none";
	if (PrdOrn=="V") PSz=InsSml + '" x ' + InsLrg + '"'
	if (PrdOrn=="H") PSz=InsLrg + '" x ' + InsSml + '"'
	document.getElementById("txtImgDsc").innerHTML="";
	document.getElementById("txtPrdDsc").innerHTML=PrdNam + "  " + PSz
	if (ActSec==2)
	{
		document.getElementById("divDspOrg").style.display="";
		if (OrgImg!=null) OrgImg.style.display="";
	}
	if (ActSec==4 || ActSec==5 || ActSec==6 || ActSec==7) 
	{
	  document.getElementById("divDspWrk").style.display="";
		if (WrkImg!=null) WrkImg.style.display="";
		RStPos();
		if (ActSec==6 || ActSec==7) ResPos(true); else ResPos(false);
		if (ActSec==6) ShwTxt(true);
	}
	if (ActSec==8)
	{
		document.getElementById("divDspCfm").style.display="";
		if (CfmImg!=null) CfmImg.style.display="";
		DspDsn();
		document.getElementById("cmdZomVue").disabled=false;
		document.getElementById("cmdSumNxt").disabled=false;
		document.getElementById("cmdSumTNx").disabled=false;
		if (PanLst[0].IIx!=99 && ImgLst[PanLst[0].IIx])	document.getElementById("imgOrgImg").src=ImgLst[PanLst[0].IIx].BIm.src;
	}
}

function CalZom()
{
	var IWd=0;
  var IHt=0;
	IWd=FulWid;
	IHt=FulHgt;
	if (IWd>WinWid) ZomWid=WinWid; else ZomWid=IWd;
	ZomHgt=ZomWid*(IHt/IWd);
	if (ZomHgt>(WinHgt-20)) ZomHgt=WinHgt-20;
	ZomWid=ZomHgt*(IWd/IHt);
}

function DspZom(Sel)
{
  if (Sel==0)
  {
		document.getElementById("ZomVue").style.display="none";  
  }
  if (Sel==1)
  {
		document.getElementById("ZomVue").style.display="";
		document.getElementById("imgZomImg").width=ZomWid;
		document.getElementById("imgZomImg").height=ZomHgt;
		document.getElementById("ZomVue").style.width=ZomWid+"px";
		document.getElementById("ZomVue").style.height=(ZomHgt+20)+"px";
		document.getElementById("ZomVue").style.left="0px";
		document.getElementById("ZomVue").style.top="0px";
		document.getElementById("colZomHdr").style.width=(ZomWid-20)+"px";	
	}
	return false;
}

function BldSpc(PIx,IIx)
{
  var CSp=1;
  var RSp=1;
  var BCl=0;
  var XPs=0;
  var YPs=0;
  var WWd=0;
  var WHt=0;
  var Scl=0;
  var Rot=0;
  var INm=""
  if (IIx!=99)	
  {
		if (PIx!=99)
		{
		  if (PanLst[PIx].WWd>0)
		  {
		    BCl=PanLst[PIx].BCl;
		  	CSp=PanLst[PIx].CSp;
		  	RSp=PanLst[PIx].RSp;
		  	Scl=ImgLst[IIx].IWd/ImgLst[IIx].DWd; 
		  	XPs=Math.round(ImgLst[IIx].XPs*Scl);
		  	WWd=Math.round(PanLst[PIx].WWd*Scl); 
		    Scl=ImgLst[IIx].IHt/ImgLst[IIx].DHt; 
		  	YPs=Math.round(ImgLst[IIx].YPs*Scl);
				WHt=Math.round(PanLst[PIx].WHt*Scl); 
			}
		}
		Rot=ImgLst[IIx].Rot;  if (ImgLst[IIx].Gry) Rot+=10;
		INm=ImgLst[IIx].INm;
    ImgSpc += INm + "|" + IIx + "|" + CSp + "|" + RSp + "|" + BCl + "|" + Rot + "|" + XPs + "|" + YPs + "|" + WWd + "|" + WHt + "|"			
  }
  else 
  {
		if (PIx!=99)
		{
			BCl=PanLst[PIx].BCl;
			CSp=PanLst[PIx].CSp;
		  RSp=PanLst[PIx].RSp;
			ImgSpc += "|0|" + CSp + "|" + RSp + "|" + BCl + "|0|0|0|0|0|";	
		}
		else 
		{
			ImgSpc += "|0|0|0|0|0|0|0|0|0|";
		}
	}
}

function ParSpc()
{	
  var Ret=false;
	var Lst;
	var NRw=0;
	var NCl=0;
	var Cnt=0;
	var IIx=0;
	var PIx=0;
	var Scl=0;
	var INm="";
	var Sel=true;
	Lst=DsnSpc.split(",");
	if (Lst.length>0)
	{
		NRw=1*Lst[0];
		NCl=1*Lst[1];
		Cnt=NRw*NCl;
		if (Cnt>0)
		{
			for (PIx=0; PIx<Cnt; PIx++)
			{
				INm=Lst[2+10*PIx]; 
				if (INm>"")
				{
					IIx=0; while (IIx<ImgCnt && ImgLst[IIx].INm!=INm) IIx++;
					if (IIx<ImgCnt)
					{
						Ret=true;
						if (Sel) { SelILs(IIx); Sel=false; }
						PanLst[PIx].IIx=IIx; 
						PanLst[PIx].CSp=1*Lst[ 4+10*PIx];
						PanLst[PIx].RSp=1*Lst[ 5+10*PIx];
						PanLst[PIx].BCl=1*Lst[ 6+10*PIx];
						ImgLst[IIx].Rot=1*Lst[ 7+10*PIx];  
						if (ImgLst[IIx].Rot>=10) { ImgLst[IIx].Rot-=10; ImgLst[IIx].Gry=true; } else ImgLst[IIx].Gry=false;
						if (ImgLst[IIx].Gry) GryRdo(1); 
						ImgLst[IIx].PIx=PIx;
						ImgLst[IIx].INm=INm;
						ImgLst[IIx].DWd=PanLst[PIx].WWd*ImgLst[IIx].IWd/(1*Lst[10+10*PIx]);
						ImgLst[IIx].DHt=PanLst[PIx].WHt*ImgLst[IIx].IHt/(1*Lst[11+10*PIx]);
						Scl=ImgLst[IIx].IWd/ImgLst[IIx].DWd;
						ImgLst[IIx].XPs=(1*Lst[ 8+10*PIx])/Scl;
						ImgLst[IIx].YPs=(1*Lst[ 9+10*PIx])/Scl;
					}
				}
				else
				{
				  PanLst[PIx].RLd=true;
					PanLst[PIx].BCl=1*Lst[ 6+10*PIx];				
				}
			}		
			DspArg();
		}
		ClsLod();
	}
	return Ret;
}

function DspArg()
{
  var PIx=0;
  for (PIx=0; PIx<PanLst.length; PIx++) DspPan(PIx,false);
  ResPos();
  if (SupArg==3) SelSid(PrdSid);
  return false;
}

function DspPan(PIx,Sav)
{
	var Cmd=""
	var IIx=0;
	var Wid=0;
	var Hgt=0;
	var BCl=2;
	if (PanLst[PIx].OWd>0 && PanLst[PIx].OHt>0)
	{
	  IIx=PanLst[PIx].IIx; 
	  if (PanLst[PIx].IIx==99)
		{
		  if (PanLst[PIx].BCl!=0) BCl=PanLst[PIx].BCl; else BCl=2;
		  Cmd+="BldTmb.ashx?OrdIDN=" + OrdIDN + "&CerNum=" + CerNum;
			Cmd+="&ImgWid=" + PanLst[PIx].OWd + "&ImgHgt=" + PanLst[PIx].OHt;
			Cmd+="&BlkCol="+BCl+"&BlkGrd=1&TmbMod=3&DspWid=0"
			if (BrwNam=="IE")
			{
				try
				{
					PanLst[PIx].Img.style.position="absolute";
					PanLst[PIx].Img.style.left  ="0px";
					PanLst[PIx].Img.style.top   ="0px";
					PanLst[PIx].Img.style.width =PanLst[PIx].OWd + "px";
					PanLst[PIx].Img.style.height=PanLst[PIx].OHt + "px";
					if (PanLst[PIx].RLd || RStDef)
					{
						PanLst[PIx].Img.onload=new Function("PImLod("+PIx+")");
						PanLst[PIx].Img.src=Cmd;
					}
					else
					{
						PImLod(PIx);
					}
				}
				catch (err)
				{
				}			
			}
			else
			{
				try
				{
					PanLst[PIx].Cvs.style.position="absolute";
					PanLst[PIx].Cvs.style.left  ="0px";
					PanLst[PIx].Cvs.style.top   ="0px";
					PanLst[PIx].Cvs.setAttribute("width",PanLst[PIx].OWd);
					PanLst[PIx].Cvs.setAttribute("height",PanLst[PIx].OHt);
					if (PanLst[PIx].RLd || RStDef)
					{
						PanLst[PIx].Img.onload=new Function("PImLod("+PIx+")");
						PanLst[PIx].Img.src=Cmd;
					}
					else
					{
						PImLod(PIx);
					}
				}
				catch (err)
				{
				}			
			}
		}
		else
		{
		  if (BrwNam=="IE")
		  {
				try
				{
					if (ImgLst[IIx].Gry) PanLst[PIx].Img.style.filter="progid:DXImageTransform.Microsoft.BasicImage(rotation="+ImgLst[IIx].Rot+",grayScale=1)";
					else								 PanLst[PIx].Img.style.filter="progid:DXImageTransform.Microsoft.BasicImage(rotation="+ImgLst[IIx].Rot+")";
					PanLst[PIx].Img.style.position="absolute";
					PanLst[PIx].Img.style.left  =-ImgLst[IIx].XPs + "px";
					PanLst[PIx].Img.style.top   =-ImgLst[IIx].YPs + "px";
					PanLst[PIx].Img.style.width =ImgLst[IIx].DWd + "px";
					PanLst[PIx].Img.style.height=ImgLst[IIx].DHt + "px";
					PanLst[PIx].Img.src=ImgLst[PanLst[PIx].IIx].BIm.src;
				}
				catch (err)
				{
				}
			}
			else
			{
				switch (ImgLst[IIx].Rot)
				{
					case 0: 
						Wid=ImgLst[IIx].DWd;
						Hgt=ImgLst[IIx].DHt;
						break;
					case 1: 
						Wid=ImgLst[IIx].DHt;
						Hgt=ImgLst[IIx].DWd;
						break;
					case 2: 
						Wid=ImgLst[IIx].DWd;
						Hgt=ImgLst[IIx].DHt;
						break;
					case 3: 
						Wid=ImgLst[IIx].DHt;
						Hgt=ImgLst[IIx].DWd;
						break;
				}	
				try
				{
					PanLst[PIx].Cvs.style.position="absolute";
					PanLst[PIx].Cvs.style.left  =-ImgLst[IIx].XPs + "px";
					PanLst[PIx].Cvs.style.top   =-ImgLst[IIx].YPs + "px";
					PanLst[PIx].Cvs.setAttribute("width",Wid);
					PanLst[PIx].Cvs.setAttribute("height",Hgt);
					if (PanLst[PIx].RLd || RStDef)
					{
						PanLst[PIx].Img.onload=new Function("PImLod("+PIx+")");
						PanLst[PIx].Img.src=ImgLst[PanLst[PIx].IIx].BIm.src+"&ReqCnt=" + ReqCnt++;
					}
					else
					{
						if (!PImLod(PIx))
						{
							PanLst[PIx].Img.onload=new Function("PImLod("+PIx+")");
							PanLst[PIx].Img.src=ImgLst[PanLst[PIx].IIx].BIm.src+"&ReqCnt=" + ReqCnt++;
						}
					}
				}
				catch (err)
				{
				  alert(err);
				}
			}
		}
	}
}

function PImLod(PIx)
{
  var Ret=true;
	var IIx=0;
	var Rot=0;
	var XAd=0;
	var YAd=0;
	var IDt;
	var XPs=0;
	var YPs=0;
	var Idx=0;
	var Avg=0;
	PanLst[PIx].RLd=false;
	if (PanLst[PIx].IIx==99)
	{
		if (BrwNam=="IE")	
		{
			PanLst[PIx].Img.style.display="";
		}
		else
		{
			PanLst[PIx].CTx.drawImage(PanLst[PIx].Img,0,0,PanLst[PIx].OWd,PanLst[PIx].OHt);
			PanLst[PIx].Cvs.style.display="";
		}
	}
	else
	{
		if (BrwNam=="IE")	
		{
			PanLst[PIx].Img.style.display="";
		}
		else
		{
			IIx=PanLst[PIx].IIx;
			switch (ImgLst[IIx].Rot)
			{
				case 0: 
					Rot=0;								
					XAd=0;
					YAd=0;
					break;
				case 1: 
					Rot=90*Math.PI/180;		
					XAd=0;
					YAd=-ImgLst[IIx].DHt;
					break;
				case 2: 
					Rot=Math.PI;					
					XAd=-ImgLst[IIx].DWd;
					YAd=-ImgLst[IIx].DHt;
					break;
				case 3: 
					Rot=270*Math.PI/180;
					XAd=-ImgLst[IIx].DWd;
					YAd=0;
					break;
			}	
			try
			{
				PanLst[PIx].CTx.rotate(Rot);
				PanLst[PIx].CTx.drawImage(PanLst[PIx].Img,XAd,YAd,ImgLst[IIx].DWd,ImgLst[IIx].DHt);
				PanLst[PIx].Cvs.style.display="";
				if (ImgLst[IIx].Gry)
				{
					IDt=PanLst[PIx].CTx.getImageData(0,0,PanLst[PIx].Cvs.width,PanLst[PIx].Cvs.height);
					for(YPs=0; YPs<IDt.height; YPs++)
					{
						for(XPs=0; XPs<IDt.width; XPs++)
						{
							Idx=(YPs*4)*IDt.width+XPs*4;
							Avg=(IDt.data[Idx]+IDt.data[Idx+1]+IDt.data[Idx+2])/3;
							IDt.data[Idx]	 =Avg;
							IDt.data[Idx+1]=Avg;
							IDt.data[Idx+2]=Avg;
						}
					}
					PanLst[PIx].CTx.putImageData(IDt, 0, 0, 0, 0, IDt.width, IDt.height);
				}
			}
			catch (err)
			{
			  Ret=false;
			}
		}
	}
	return Ret;
}

function BldTxt(TIx)
{
	var Cmd="BldTxt.ashx";
	var Txt;
	var Fnt;
	var FSz;
	var TxC;
	var Aln;
	var OLn;
	var OCl;
	var TNm;
	var TSz;
	var Scl=FulWid/DspWid;
	var PWd=PrdWid;
	var Orn=PrdOrn;
	var TWd=PrdWid-(2*MrgAWd);
	var THt=PrdHgt;
	var DWd=DspWid-(DecLWd+DecRWd);
	if (TIx<TxtLst.length)
	{
		Txt=escape(TxtLst[TIx].Txt);
		Fnt=TxtLst[TIx].Fnt;
		FSz=TxtLst[TIx].FSz;
		TxC=TxtLst[TIx].Col;
		Aln=TxtLst[TIx].Aln;
		OLn=TxtLst[TIx].OLn;
		OCl=TxtLst[TIx].OCl;
		if (TxtTyp==3 && BrdTLs!=null) 
		{
			TWd=(BrdTLs[0].XP2-BrdTLs[0].XP1);
			THt=(BrdTLs[0].YP2-BrdTLs[0].YP1);
		}
		if (Txt>"" && PWd>0)
		{
		  BldTIm(TIx,ImgXPs+TxtLst[TIx].XPs,ImgYPs+TxtLst[TIx].YPs,false);
		  Cmd+="?OrdIDN=" + OrdIDN;
			Cmd+="&TxtBuf=" + Txt;
			Cmd+="&FntNam=" + Fnt + "&FntSiz=" + FSz;
			Cmd+="&TxtCol=" + TxC + "&TxtAln=" + Aln;
			Cmd+="&TxtOLn=" + OLn + "&TxtOCl=" + OCl;
			Cmd+="&TxtWid=" + TWd + "&TxtHgt=" + THt;
			Cmd+="&PrdWid=" + PWd + "&DspWid=" + DWd;
			Cmd+="&PrdOrn=" + Orn;
			TxtLst[TIx].Img.src=Cmd;
		}
		else
		{
			TxtLst[TIx].Img.style.display="none";
		}
	}
}

function BldMgm()
{
	var Cmd="BldMgm.ashx";
	var PWd=PrdWid;
	var DWd=DspWid-(DecLWd+DecRWd);
	if (MgmTxt>"" && BrdTLs!=null)
	{
		MgmCol=BrdTLs[1].Bgd;
		Cmd+= "?MgmTxt=" + MgmTxt;
		Cmd+= "&MgmCol=" + MgmCol;
		Cmd+= "&PrdWid=" + PWd + "&DspWid=" + DWd;
		document.getElementById("imgMgmImg").src=Cmd;
	}
}

function BldDte(DTp)
{
	var Cmd="BldDte.ashx";
	var PWd=PrdWid;
	var DWd=DspWid-(DecLWd+DecRWd);
	Cmd+= "?DteTyp=" + DTp;
	Cmd+= "&DteTxt=" + DteTxt;
	Cmd+= "&DteCol=" + DteCol;
	Cmd+= "&PrdWid=" + PWd + "&DspWid=" + DWd;
	if (DTp=="M") document.getElementById("imgMonImg").src=Cmd; 
	if (DTp=="D") document.getElementById("imgDayImg").src=Cmd;
	if (DTp=="Y") document.getElementById("imgYerImg").src=Cmd;
	if (DTp=="C") document.getElementById("imgClkImg").src=Cmd;
}

function AddILs(INm,IWd,IHt,DPI,Orn)
{
  if (AlwMlt)
  {
		ImgIdx=ImgCnt;
		ImgCnt++;
	}
	else
	{
	  ImgLst.length=0
	  ImgCnt=1;
		ImgIdx=0;
	}
	if (Orn=="N")
	{
	  if (IWd>IHt) Orn="H"; else Orn="V";
	}
	ImgLst[ImgIdx]=new Object();
	ImgLst[ImgIdx].INm=INm;
	ImgLst[ImgIdx].CDf=1;
	ImgLst[ImgIdx].IWd=IWd; 
	ImgLst[ImgIdx].IHt=IHt;
	ImgLst[ImgIdx].DPI=DPI;
	ImgLst[ImgIdx].Orn=Orn;
	ImgLst[ImgIdx].XPs=0;
	ImgLst[ImgIdx].YPs=0;
	ImgLst[ImgIdx].XP1=0;
	ImgLst[ImgIdx].YP1=0;
	ImgLst[ImgIdx].XP2=0;
	ImgLst[ImgIdx].YP2=0;
	ImgLst[ImgIdx].RWd=0;
	ImgLst[ImgIdx].Rot=0;
	ImgLst[ImgIdx].Gry=false;
	ImgLst[ImgIdx].PIx=99;
	ImgLst[ImgIdx].Img=new Image();
	ImgLst[ImgIdx].BIm=new Image();
	DspCtl();
}

function DelImg()
{
  var PIx;
  if (ImgCnt>0)
  {
		if (confirm("Are you sure you want to remove the selected image from your collection?"))
	  {
			document.getElementById("txtImgWrn").innerHTML="";
			for (PIx=0; PIx<PanLst.length; PIx++)
			{
				if (PanLst[PIx].IIx==ImgIdx) 
				{
					PanLst[PIx].IIx=99;
					PanLst[PIx].BCl=0;
					DspPan(PIx,false);
				}
			}
			RmvImg(ImgLst[ImgIdx].INm);
			ImgLst.splice(ImgIdx,1);
			ImgCnt--;
			DspILs();
			if (ImgIdx>=ImgCnt) ImgIdx=0;
			if (ImgCnt>0)	SelILs(ImgIdx);
			else					document.getElementById("divDspOrg").style.display="none";
			DspCtl();
		}
	}
	return false;
}

function ClrILs()
{
  var PIx=0;
	if (ImgCnt>0)
	{
		for (PIx=0; PIx<PanLst.length; PIx++)
		{
			PanLst[PIx].IIx=99;
			PanLst[PIx].BCl=0;
			DspPan(PIx,false);
		}
		ImgCnt=0;
		ImgLst.length=0
		DspILs();
	}
}

function RshImg()
{
  ILdPnd=true;
	GetILs(SesIDN);
	return false;
}

function DImBck(result, eventArgs)
{
	var Buf
	Buf=result.split('|');
	if (Buf[0]!="OK")
	{
		alert("Error Getting Image Definition: "+Buf[1]);		
	}
}

function LodImg(IIx)
{
	var Cmd;
	ImgSpc=ImgSpc="1|1|";
	BldSpc(99,IIx)
	Cmd="BldTmb.ashx?OrdIDN=" + OrdIDN + "&CerNum=" + CerNum + "&ImgSpc=" + ImgSpc + "&TmbMod=1&DspWid=0"
	ImgLst[IIx].Img.onload=new Function("ImgLod("+IIx+")");
	ImgLst[IIx].Img.src=Cmd;
	Cmd="BldTmb.ashx?OrdIDN=" + OrdIDN + "&CerNum=" + CerNum + "&ImgSpc=" + ImgSpc + "&TmbMod=2&DspWid=" + DspWid;
	ImgLst[IIx].BIm.onload=new Function("BImLod("+IIx+")");
	ImgLst[IIx].BIm.src=Cmd;
}

function ImgLod(IIx)
{
	if (IIx+1==ImgCnt) { DspILs(); BldPan(NumRow,NumCol,SplCod); }
}

function BImLod(IIx)
{
	if (IIx+1==ImgCnt)
	{
		if (ILdPnd)
		{
			ILdPnd=false;
			if (!ParSpc()) setTimeout("SelILs("+IIx+")",100);
		}
		else 
		{
			setTimeout("SelILs("+IIx+")",100);
		}
	}
}

function DspILs()
{
	var Tbl=document.getElementById("tblImgSel");
	var Idx=0;
	if (ImgCnt==0) document.getElementById("txtILsHdr").innerHTML="No Images";
	if (ImgCnt==1) document.getElementById("txtILsHdr").innerHTML=ImgCnt+" Image";
	if (ImgCnt>1)  document.getElementById("txtILsHdr").innerHTML=ImgCnt+" Images";
	while (Tbl.rows.length>0) Tbl.deleteRow(0);
	for (Idx=0; Idx<ImgCnt; Idx++) DspIIm(Tbl,Idx);
	return false;
}

function RStILs()
{
  var Idx=0;
	for (Idx=0; Idx<ImgLst.length; Idx++)
  {
		ImgLst[Idx].CDf=1;  
  }
}

function DspIIm(Tbl,IIx)
{
	var Bdy=document.createElement('tbody'); 
	var Row;
	var Hdr;
	var Img;
	var Thb;
	var Idx=0;
	var Buf="";
	var Wid=0;
	var Hgt=0;
	if (BrwNam=="IE" && (BrwVer==6 || BrwVer==7))
	{
		Row=document.createElement('<tr onclick="SelILs('+IIx+')" onmousedown="SetDrg('+IIx+')">');
	}
	else
	{
		Row=document.createElement("tr")
		Row.setAttribute("onclick","SelILs("+IIx+")");
		Row.setAttribute("onmousedown","SetDrg("+IIx+")");
	}
	if (BrwNam=="IE" && (BrwVer==6 || BrwVer==7))
	{
		Hdr=document.createElement('<td class="ISlHdr">');
		Img=document.createElement('<td class="ISlImg">');
	}
	else
	{
		Hdr=document.createElement("td");
		Img=document.createElement("td");
		Hdr.setAttribute("class","ISlHdr");
		Img.setAttribute("class","ISlImg");
	}
	Hdr.innerHTML=String.fromCharCode(IIx+65);
	Wid=60;
	Hgt=Wid*(ImgLst[IIx].IHt/ImgLst[IIx].IWd)
	Thb=document.createElement("img");
	Thb.src=ImgLst[IIx].Img.src;
	Thb.width=Wid;
	Thb.height=Hgt;
	Img.appendChild(Thb);
	Row.appendChild(Hdr);
	Row.appendChild(Img);
	Bdy.appendChild(Row);
	Tbl.appendChild(Bdy);
	return false;
}

function SelILs(RIx)
{
 	var Tbl=document.getElementById("tblImgSel");
 	var Row;
  var Grd;
  var Buf;
  var Chg=false;
  var Idx;
  var IIx;
 	if (Tbl.rows.length>0)
 	{
 	  SetUpd(1);
 		for (Idx=0; Idx<Tbl.rows.length; Idx++) 
		{
			Row=Tbl.rows[Idx]; 
			if (Idx==RIx)	Row.style.backgroundColor="#b22222"; 
			else					Row.style.backgroundColor="#d6c7a0";
		}
		if (ImgIdx!=RIx) Chg=true;
		ImgIdx=RIx; 
		if (BrdIDN==0 && NumRow==1 && NumCol==1 && (PrdOrn=="N" || !AlwArg))
		{
			if (SupOrn=="B") CfgOrn(ImgLst[ImgIdx].Orn); else CfgOrn(SupOrn);
			Chg=true;
		}
		if (ActSec==2)
		{
		  if (Chg || !ImgSel) CalDef(0,ImgIdx);
			ImgSel=true;
		  Grd=EvlImg(0,0,ImgIdx);	  
			document.getElementById("txtEdtSpc").innerHTML=ImgLst[ImgIdx].IWd + " x " + ImgLst[ImgIdx].IHt + "  " + ImgLst[ImgIdx].DPI + " DPI";
			Buf=Grd.toFixed(0) + "%"
			if (Grd>=40) 
			{
				document.getElementById("txtEdtGrd").style.color="Green";
				document.getElementById("txtImgWrn").innerHTML="";
				Buf+=" Good Image"
			}
			else
			{
				document.getElementById("txtEdtGrd").style.color="Red";
				if (MatTyp=="WOV") document.getElementById("txtImgWrn").innerHTML="This will NOT weave a high quality product.\nPlease rescan at a higher resolution or choose a different photograph.";
				else							 document.getElementById("txtImgWrn").innerHTML="This will NOT produce a high quality product.\nPlease rescan at a higher resolution or choose a different photograph.";
				Buf+=" Too Small"
			}
			document.getElementById("txtEdtGrd").innerHTML=Buf;
			if (PrdOrn=="V") document.getElementById("txtEdtReq").innerHTML=PrdSml + " x " + PrdLrg
			if (PrdOrn=="H") document.getElementById("txtEdtReq").innerHTML=PrdLrg + " x " + PrdSml
			DspImg(ImgIdx);
			if (NumRow==1 && NumCol==1)
			{
			  BldPan(NumRow,NumCol,SplCod);
				for (IIx=0; IIx<ImgCnt; IIx++) if (ImgLst[IIx].PIx==0) ImgLst[IIx].PIx=99;
				PanLst[0].IIx=ImgIdx;
				ImgLst[ImgIdx].PIx=0;
				ImgLst[ImgIdx].Rot=0;
				CalDef(0,ImgIdx,true);
				DspPan(0,true);
			}
			else
			{
				DefFlp();			
			}
		}
	}
}

function DefFlp()
{
	var PIx=0;
	var IIx=0;
	var SIx=0;
	var SIm=false;
	if (PrdSid!=0)
	{
		for (PIx=0; PIx<PanLst.length; PIx++)
		{
			if (PanLst[PIx].IIx==99 && PanLst[PIx].BCl==0)
			{
				SIx=PIx; SIm=true;
			}
			else
			{
				IIx=PanLst[PIx].IIx;
			}
		}
		if (SIm)
		{
			PanLst[SIx].IIx=IIx;
			ImgLst[IIx].PIx=SIx;
			ImgLst[IIx].Rot=0;
			CalDef(SIx,IIx,true);
			DspPan(SIx,true);
		}
	}
}

function EdtGrd(PIx,IIx)
{
  var Grd=0;
  var Buf="";
	Grd=EvlImg(1,PIx,IIx);	  
	if (Grd>0)
	{
		document.getElementById("txtEdtSpc").innerHTML=ImgLst[IIx].IWd + " x " + ImgLst[IIx].IHt + "  " + ImgLst[IIx].DPI + " DPI";
		Buf=Grd.toFixed(0) + "%"
		if (Grd>=40) 
		{
			document.getElementById("txtEdtGrd").style.color="Green";
			document.getElementById("txtEdtWrn").innerHTML="";
			Buf+=" Good Image"
		}
		else
		{
			document.getElementById("txtEdtGrd").style.color="Red";
			if (MatTyp=="WOV")	document.getElementById("txtEdtWrn").innerHTML="This will NOT weave a high quality product.\nPlease rescan at a higher resolution.";
			else								document.getElementById("txtEdtWrn").innerHTML="This will NOT produce a high quality product.\nPlease rescan at a higher resolution.";	
			Buf+=" Too Small"
		}
		document.getElementById("txtEdtGrd").innerHTML=Buf;	
		if (PrdOrn=="V") document.getElementById("txtEdtReq").innerHTML=PrdSml + " x " + PrdLrg
		if (PrdOrn=="H") document.getElementById("txtEdtReq").innerHTML=PrdLrg + " x " + PrdSml
	}
	else
	{
		document.getElementById("txtEdtSpc").innerHTML="";
		document.getElementById("txtEdtGrd").innerHTML="";
		document.getElementById("txtEdtReq").innerHTML="";
	}
}

function EvlImg(Lvl,PIx,IIx)
{
	var Scl=1;
	var DWd;
	var DHt;
	var OWd;
	var OHt;
	var Pct=0;
	if (ImgLst.length>0 && IIx<ImgLst.length)
	{
		if (Lvl==0)
		{
			if (ImgLst[IIx].IWd>ImgLst[IIx].IHt) { OWd=PrdLrg; OHt=PrdSml; } else  { OWd=PrdSml; OHt=PrdLrg; }
			if ((ImgLst[IIx].IHt/ImgLst[IIx].IWd)<(OHt/OWd))
			{
				DHt=OHt;
				DWd=OHt*(ImgLst[IIx].IWd/ImgLst[IIx].IHt)
			}
			else
			{
				DWd=OWd;
				DHt=OWd*(ImgLst[IIx].IHt/ImgLst[IIx].IWd)
			}
			Scl=ImgLst[IIx].IWd/DWd;
			DWd=OWd*Scl;
			DHt=OHt*Scl;	
			Pct=((DWd*DHt)/(PrdSml*PrdLrg))*100;
		}
		if (Lvl==1)
		{
			Scl=ImgLst[IIx].IWd/ImgLst[IIx].DWd;
			DWd=PanLst[PIx].OWd*Scl;
			DHt=PanLst[PIx].OHt*Scl;	
			if (BrdIDN==0) Pct=((DWd*DHt)/(PrdSml*PrdLrg))*100;
			else					 Pct=((DWd*DHt)/(BrdILs.Wid*BrdILs.Hgt))*100;	
		}
		if (Pct>100) Pct=100;
	}
	return Pct;
}


