var mInitClipartList=1;
var mPrinting;

function UpdateProductPriceOnEnter(e)
{
	var iKey;
	
	if (window.event) // IE
		iKey=e.keyCode;
	else if(e.which) // Netscape/Firefox/Opera
		iKey=e.which;

	if (iKey==13)
		{
		UpdateProductPrice();
		return false;
		}
	else
		return true;
}

function UpdateProductPrice()
{
	
	var x,obj,sRibbon,sCard,iQty,blColor;
	var sVal,sMsg,sText,sDeposit,sLayout,sDiscountSchedule;
	var cCost, cCostBase, cCostFirst, cColorCost, cDiscountPercent;
	var iQtySoldIn,iQtyFirst,iQtyMin,cAdd,cPrice,cDisp;
	var cSatinSheerRibbonCost=.20;
	var cSpecialtyRibbonCost=.28;
	var cMetallicCardCost=.16;
	var cFoldingCardCost = .40;
	var cMiniCardCost = -.50;
	
	var td=document.getElementById("tdYourPrice");
	if (td!=null)
		{
		iQtySoldIn=parseFloat(document.getElementById("QtySoldIn").value);
		iQtyFirst=parseFloat(document.getElementById("QtyFirst").value);
		iQtyMin=parseFloat(document.getElementById("QtyMin").value);
		cCostFirst=parseFloat(document.getElementById("ProductCostFirst").value);
		cCost=parseFloat(document.getElementById("ProductCost").value);
		cColorCost=parseFloat(document.getElementById("ColorCost").value);
		sDeposit=document.getElementById("DepositItem").value;
		
		obj=document.getElementById("Quantity");
		iQty=ValidateQty(obj,iQtyMin);
		cCostBase = 0;
		
		if (iQtySoldIn==1) {
		   sDiscountSchedule = document.getElementById("DiscountSchedule").value;
		   cDiscountPercent = document.getElementById("DiscountPercent").value;
		   cCost = GetSchedulePrice(iQty, cCost, "");
		   if (cDiscountPercent != "" && cDiscountPercent != "0") {
		      cCostBase = cCost;
		      cCost = cCost - (cCost * cDiscountPercent);
		      if (cCost.toFixed)
		         cCost = cCost.toFixed(2);
		   }
      }
			
		sMsg="";
		blColor=false;
		obj=document.getElementsByName("Printing");
		if (obj.length>0)
			blColor=obj[1].checked;
		
		if (cCostFirst>0)
			{
			cAdd=cCostFirst;
			cPrice=cAdd;
			if (cAdd.toFixed)
				cAdd=cAdd.toFixed(2);
			sMsg=sMsg + "<br>- $" + cAdd + " first " + iQtyFirst;
			if (iQty>iQtyFirst)
				{
				cAdd=(((iQty-iQtySoldIn)/iQtySoldIn)*cCost);
				cPrice=cPrice+cAdd;
				if (cAdd.toFixed)
					cAdd=cAdd.toFixed(2);
				sMsg=sMsg + "<br>- $" + cAdd + " next " + (iQty-iQtySoldIn);
				}
			}
		else {
			cPrice=(iQty/iQtySoldIn)*cCost;
			if (cPrice.toFixed)
				cDisp=cPrice.toFixed(2);
	      if (iQtySoldIn == 1) {
	         if (cCostBase==0)
	            sMsg = sMsg + "<br>- $" + cDisp + " (" + cCost + " ea)";
	         else
	            sMsg = sMsg + "<br>- $" + cDisp + " (<s>" + +cCostBase + "</s>" + "&nbsp;" + cCost + " ea)";
	      }
	      else
	         sMsg = sMsg + "<br>- $" + cCost + " per " + iQtySoldIn;
		}
		
		if (blColor && cColorCost>0)
			{
			cAdd=(iQty*cColorCost);
			cPrice=cPrice+cAdd;
			if (cAdd.toFixed)
				cAdd=cAdd.toFixed(2);
			sMsg=sMsg + "<br>- $" + cAdd + " color printing upgrade";
			}
		
		obj=document.getElementById("Layout");
		if (obj!=null) {
			sLayout=obj.value;
			if (sLayout=="ifolding") {
				cAdd=(iQty*cFoldingCardCost);
				cPrice=cPrice+cAdd;
				if (cAdd.toFixed)
					cAdd=cAdd.toFixed(2);
				sMsg=sMsg + "<br>- $" + cAdd + " folding card upgrade";
	      }
         else if (sLayout == "iflatmini") {
            cAdd = (iQty * cMiniCardCost);
            cPrice = cPrice + cAdd;
            if (cAdd.toFixed)
               cAdd = cAdd.toFixed(2);
            sMsg = sMsg + "<br>- $" + cAdd + " mini credit";
         }
		}
			
		obj=document.getElementById("Color2");
		if (obj!=null)
			{
			sRibbon=obj.value;
			x=sRibbon.indexOf("sheer");
			if (x>0)
				{
				cAdd=(iQty*cSatinSheerRibbonCost);
				cPrice=cPrice+cAdd;
				if (cAdd.toFixed)
					cAdd=cAdd.toFixed(2);
				sMsg=sMsg + "<br>- $" + cAdd + " ribbon upgrade";
				}
			else
				{
				if (sRibbon=='holidayplaid' || sRibbon=='metallicchevron')
					{
					cAdd=(iQty*cSpecialtyRibbonCost);
					cPrice=cPrice+cAdd;
					if (cAdd.toFixed)
						cAdd=cAdd.toFixed(2);
					sMsg=sMsg + "<br>- $" + cAdd + " ribbon upgrade";
					}
				}
			}
		
		obj=document.getElementById("Color1");
		if (obj!=null)
			{
			sCard=obj.value;
			if (sCard=='msilver' || sCard=='mgold')
				{
				cAdd=(iQty*cMetallicCardCost);
				cPrice=cPrice+cAdd;
				if (cAdd.toFixed)
					cAdd=cAdd.toFixed(2);
				sMsg=sMsg + "<br>- $" + cAdd + " card upgrade";
				}
			}
		
		if (cPrice.toFixed)
			cPrice=cPrice.toFixed(2);
		sText="$" + cPrice;
		if (sDeposit=="True")
			sText=sText+"<font style='font-size:7pt;color:#808080'> deposit</font>";
		else
			{
			if (sMsg!="")
				sText=sText+"<font style='font-size:7pt;color:#808080'><br>" + sMsg + "</font>";
			}
		td.innerHTML=sText;
		}
}

function GetSchedulePrice(Qty,Cost,DiscountSchedule)
{
	var obj;
	var sSchedule,sVals,sKV;
	var i,iQty;
	var cCost,cRet;
	var blFound;
	
	cRet=Cost;
	blFound=false;
	obj=document.getElementById("DiscountSchedule");
	if (obj!=null)
		{
		sSchedule=obj.value;
		sVals=sSchedule.split(",");
		
		for(i=1;i<=sVals.length;i++)
			{
			sKV=sVals[i-1].split("-");
			
			iQty=parseFloat(sKV[0]);
			
			if (sKV[1]=="*")
				cCost=Cost;
			else
				cCost=parseFloat(sKV[1]);
				
			if (Qty<=iQty)
				{
				if (blFound==false)
					{
					cRet=cCost;
					blFound=true;
					}
				}
			}
			
		}
	
	return(cRet);
}

function ValidateQty(obj,QtyMin)
{
	var iQty,iQtyMin;
	
	iQty=obj.value;
	iQtyMin=QtyMin;
	
	if (isNaN(iQty))
		{
		iQty=iQtyMin;
		obj.value=iQty;
		alert("You must enter a numeric value for Quantity.");
		}
	if (iQtyMin>0)
	{
		if (iQty<iQtyMin)
			{
			iQty=iQtyMin;
			obj.value=iQty;
			alert("The minimum quantity you can order for this item is " + iQtyMin + ".");
			}
	}
	
	return(iQty);
}

function ValidateMatchQuantity(callObj,QtyMin)
{

	var iKey;
	var iQty=parseFloat(callObj.value);
	
	if (isNaN(iQty))
		{
		callObj.value="0";
		alert("You must enter a numeric value for Quantity.");
		}
	else if (iQty!=0 && iQty<QtyMin)
		{
		callObj.value="0";
		alert("The minimum quantity you can order for this item is " + QtyMin + ".");
		}
}

function ShowStyle(URL,cbo)
{
	var sURL=URL + "style=" + cbo.value;
	
	window.location=sURL;
}

function ShowDesign(URL,cbo)
{
	var sURL=URL + "productcode=" + cbo.value;
	
	window.location=sURL;
}

function ShowPrintingType(URL,cbo)
{
	var sURL=URL + "&printing=" + cbo.value;
	
	window.location=sURL;
}

function ShowDesignNav()
{

	var tbl=document.getElementById("tblDesignNav");
	
	if (tbl.style.display=="")
		{
		tbl.style.display="none";
		}
	else
		{
		tbl.style.display="";
		}

}

function SetImageSRC(Key,Value)
{
	var sVals,sKV,sK,sV,sVal,sURL,sParms,sNewURL;
	var i,x,blFound;
	var img=document.getElementById("imgProductSample");
	var sSRC=img.src;

	AjaxShowLoader(img, 0, 0);
	
	x=sSRC.indexOf("?");
	sURL=sSRC.substr(0,x);
	sParms=sSRC.substr(x+1);
	sVals=sParms.split("&");
	
	sNewURL=sURL;
	blFound=false;
	
	for(i=1;i<=sVals.length;i++)
		{
		sKV=sVals[i-1].split("=");
		sK=sKV[0];
		sV=sKV[1];
		
		if (sK==Key)
			{
			sV=Value;
			blFound=true;
			}
			
		if (i==1)
			sNewURL=sNewURL + "?";
		else
			sNewURL=sNewURL + "&";
			
		sNewURL=sNewURL + sK + "=" + sV;
      }
      
   if (!blFound)
		sNewURL=sNewURL + "&" + Key + "=" + Value;
			
	if (img.style.filter)
		{
		img.filters(0).Apply();
		img.src=sNewURL;
		img.filters(0).Play();
		}
	else
		{
		img.src=sNewURL;
		}
		
	UpdateProductPrice();
}
function ClickSample(ColorCode)
{
	var img;
	var sSRC,sID;
	var x;
	var sID="txtSample" + ColorCode;
	var txt=document.getElementById(sID);
	
	if (txt==null)
		{
		if (ColorCode=="customphotocolors")
			{
			ShowAjax(document.getElementById('imgDDColor1'),500,1,'/product.asp?ajax=1&callout=lt&id=586');
			tmrAjax=setTimeout("HideAjax(null)",15000);
			}
		else
			{
			img=document.getElementById("imgProductSample");
			sSRC=img.src;
			
			x=sSRC.indexOf("cardcolor=" + ColorCode);
			if (x==-1)
				{
				ShowAjax(document.getElementById('imgDDColor1'), 300, 1, 'ajaxdata.asp?type=cardcolornotfound');
				tmrAjax=setTimeout("HideAjax(null)",15000);
				}
			}
		}
	else
		{
		SetImageSRC("id", txt.value)
		}
}

function ClickColorList(ClickType,ColorItemID,ColorCode,ColorName,ColorValue,TagID)
{
   var sText, sRet, sKV;
   var td, txt,el;

   txt = document.getElementById(TagID);
   td = document.getElementById("td" + TagID.toString());

   if (TagID == "ColorItemID1" || TagID == "ColorItemID2")
      txt.value = ColorItemID;
   else
      txt.value = ColorCode;

   sText = "<table border=0 cellpadding=0 cellspacing=1>";
   sText += "<tr>";
   sText += "<td style='border:1px solid #c0c0c0;width:15px;"
   if (ColorValue.indexOf('.gif')>0)
      sText += "background-image:url(/images/swatches/" + ColorValue + ")";
   else
      sText += "background-color:" + ColorValue;
   sText += "'>&nbsp;</td>";
   sText += "<td style='font-size:8pt'>" + ColorName + "</td>";
   sText += "</tr>";
   sText += "</table>";

   td.innerHTML = sText;

   HideAjax(null);

   if (ClickType == "a") {
      ClickSample(ColorCode);
   }
   else if (ClickType == "s") {
      if (TagID == "Color1")
         SetImageSRC("cardcolor", ColorCode);
      else
         SetImageSRC("ribboncolor", ColorCode);
   }
   else if (ClickType == "t") {
      el = document.getElementById("DesignMode");
      if (el)
         TemplateSetCardColor(ColorItemID); // function in template.js
      else
         SetImageSRC("coloritemid1", ColorItemID);
   }
   
}

function SetColorBox(ColorList,ColorCode,ColorName,ColorValue,ProductSampleID)
{
	var sText,sRet,sKV;
	var td,txt;
	var sField;
	
	if (ColorList==1)
		sField="Color1";
	else
		sField="Color2";
	
	txt=document.getElementById(sField);
	td=document.getElementById("tdColor" + ColorList.toString());
	
	txt.value=ColorCode;
	
	if (ColorName=="" && ProductSampleID!="")
		{
		if (window.XMLHttpRequest)
			var obj=new XMLHttpRequest();
		else if (window.ActiveXObject)
			var obj=new ActiveXObject("Microsoft.XMLHTTP");
			
		obj.open("GET","ajaxdata.asp?type=colornamevalue&colorlist=" + ColorList + "&productsampleid=" + ProductSampleID + "&code=" + ColorCode,false);
		obj.send(null);
		sRet=obj.responseText;
		sKV=sRet.split("-");
		ColorName=sKV[0];
		ColorValue=sKV[1];
		}
		
	sText="<table border=0 cellpadding=0 cellspacing=1>";
	sText+="<tr>";
	sText+="<td style='border:1px solid #c0c0c0;width:15px;background-color:" + ColorValue + "'>&nbsp;</td>";
	sText+="<td style='font-size:8pt'>" + ColorName + "</td>";
	sText+="</tr>";
	sText+="</table>";

	td.innerHTML=sText;
}

function ClickFontList(TagID,FontName)
{
	SetFontBox(TagID,FontName);
	HideAjax(null);
}

function SetFontBox(TagID,FontName)
{
   var sText;
	
	var txt=document.getElementById(TagID);
	var td=document.getElementById("td" + TagID);
	
	txt.value=FontName;
	sText="<img border=0 src='getimage.asp?type=fontname&name=" + FontName + "'>";
	td.innerHTML=sText;
}

function ClickClipartList(ClickType,TagID, ClipartID) {
   SetClipartBox(TagID, ClipartID);

   if (ClickType == "product")
      SetImageSRC("clipart", ClipartID);
      
   HideAjax(null);
}

function SetClipartBox(TagID, ClipartID) {
   var sText;

   var txt = document.getElementById(TagID);
   var td = document.getElementById("td" + TagID);

   txt.value = ClipartID;
   if (ClipartID == "")
      sText = "(not selected)";
   else
      sText = "<img border=0 src='getimage.asp?type=table&amp;max=48&amp;table=Clipart&amp;key=ClipartID&amp;field=Image&amp;value=" + ClipartID + "'>";
   td.innerHTML = sText;
}

function LoadClipartItems(ClickType, TagID, ClipartTypeID) {

   var sURL, sHTML;
   var elList;

   elList = document.getElementById("List" + TagID);
   
   if (window.XMLHttpRequest)
      var objXMLAjax = new XMLHttpRequest();
   else if (window.ActiveXObject)
      var objXMLAjax = new ActiveXObject("Microsoft.XMLHTTP");

   sURL = "ajaxdata.asp?type=clipartitems&clicktype=" + ClickType + "&tagid=" + TagID + "&cliparttypeid=" + ClipartTypeID;
   
   objXMLAjax.open("GET", sURL, false);
   objXMLAjax.send(null);

   sHTML = objXMLAjax.responseText;
   elList.innerHTML = sHTML;
   
}

function SetSample(ProductCode,ProductSampleID,Printing,CardColor,RibbonColor,FontName,ClipartID,ImageInfo)
{
	var img;
	var sURL;
	var obj;
	var divM=document.getElementById("divProductMsg");
	
	sURL="getimage.asp?type=sample"
	sURL=sURL + "&productcode=" + ProductCode;
	sURL=sURL + "&cardcolor=" + CardColor;
	sURL=sURL + "&ribboncolor=" + RibbonColor;
	sURL=sURL + "&clipart=" + ClipartID;
	sURL=sURL + "&id=" + ProductSampleID;
	sURL=sURL + "&idtype=productsampleid";
	// sURL=sURL + "&size=420";
	sURL=sURL + "&border=5";
	sURL=sURL + "&quality=90";
	if (Printing==1)
		sURL=sURL + "&grayscale=1";
	
	img=document.getElementById("imgProductSample");
	if (img.style.filter)
		{
		img.filters(0).Apply();
		img.src=sURL;
		img.filters(0).Play();
		}
	else
		{
		img.src=sURL;
		}
		
	SetPrintingOption(Printing,0);
	
	obj=document.getElementById("Color1");
	if (obj!=null)
	   SetColorBox(1,CardColor,"","",ProductSampleID);
	
	obj=document.getElementById("Color2");
	if (obj!=null)
		SetColorBox(2,RibbonColor,"","",ProductSampleID);
	
	obj=document.getElementById("FontName");
	if (obj!=null)
		{
		if (obj.tagName=="SELECT")
			SetListIndex("FontName",FontName);
		else
			SetFontBox("FontName",FontName);
		}
	
	divM.innerHTML=ImageInfo;

	UpdateProductPrice();
	
}

function SetPrintingOption(Printing,Click)
{
	var opt=document.getElementsByName("Printing");
	if (opt.length>0)
		{
		if (Printing==1)
			{
			opt[0].checked=true;
			if (Click==1)
				opt[0].click();
			}
		else
			{
			opt[1].checked=true;
			if (Click==1)
				opt[1].click();
			}
		}

	UpdateProductPrice();
	
}

function ShowProdInfo(Tab)
{
	var a,tbl;
	
	for(i=1;i<=3;i++)
		{
		a=document.getElementById("aProdInfo" + i);
		tbl=document.getElementById("tblProdInfo" + i);
		
		if (i==Tab)
			{
			tbl.style.display='';
			a.style.fontWeight="bold";
			}
		else
			{
			tbl.style.display='none';
			a.style.fontWeight="normal";
			}
		}

}
function SetSampleImageSrc(ProductID, ColorItemID1) {

   var img = document.getElementById("imgProductSample-" + ProductID);
   var sURL;

   if (img) {
      AjaxShowLoader(img, 0, 0);
      sURL = img.src;
      sURL = UpdateQueryParam(sURL, "productid", ProductID);
      sURL = UpdateQueryParam(sURL, "coloritemid1", ColorItemID1);
      img.src = sURL;
   }
   
}