var orderform=0;
var cart = new Object();
var browseframe = top.Viewer
var currentimage = 0;

//
// Called by Flash to add an item and its quantities and comment to the cart
function addToCart(item, quantity46, quantity57, quantity810, quantity1218, quantity23, comments) {
if ((quantity46 == "0" || quantity46 == "") &&
      (quantity57 == "0" || quantity57 == "") &&
      (quantity810 == "0" || quantity810 == "") &&
      (quantity1218 == "0" || quantity1218 == "") &&
      (quantity23 == "0" || quantity23 == "")) {
    alert ("Please let us know what size of prints\nand how many you would like");
    return;
  }

  cart[item] = new Object();

  cart[item].comments = comments;
  cart[item].quantity46 = quantity46;
  cart[item].quantity57 = quantity57;
  cart[item].quantity810 = quantity810;
  cart[item].quantity1218 = quantity1218;
  cart[item].quantity23 = quantity23;

  updateCartDiv();
  
  // If we are updating from a popup window, let's update the SWF if
  // we are looking at the same picture
  if (currentimage == item) updateFlash(item);

  // We probably could do an && for both conditions but I'm not sure if JS would like that when popUpWin is 0...
  if(popUpWin) {
    if(!popUpWin.closed) {
		popUpWin.close();
	} else {
		alert("Added " + item.slice(0,-4) + " to your shopping cart");
	}
  } else
  {
    alert("Added " + item.slice(0,-4) + " to your shopping cart");
  }
  
  if (orderform) {
	  if (!orderform.closed) displayCart();
  }
}

//
// Called to update the div containing the list of items in the cart
//
function updateCartDiv() {
  cartdivhtml = "<A href=\"javascript:displayCart();\"><B>Shopping Cart</B></A><BR>";
  cartdiv = document.getElementById("cartdisplay");
  for (var i in cart) {
    cartdivhtml += "<A href='javascript:viewpic(\"" + i + "\")'>" + i.slice(0,-4) + "</A>&nbsp;&nbsp;<A title=\"Remove this item from your shopping cart\" href='javascript:delCartItem(\"" + i + "\")'><img src=\"/img/trash_b.gif\"</A><BR>";
  }
  cartdiv.innerHTML = cartdivhtml;
}

//
// Function to display a web sized pic in another window
//
function viewpic(picname) {
	thewin = popUpWindow("", "", "", 710, 510);
	picwinhtml ="<HTML><HEAD><link rel=\"stylesheet\" href=\"../../dancecircle.css\" type=\"text/css\" /><BODY style=\"margin:10px;\"><TABLE align=center border=0><TR><TD valign=top>";
	picwinhtml += "<H3>" + picname + "</H3>";
	picwinhtml += "<FORM><INPUT ID=quantity46 TYPE=TEXT SIZE=2 MAXLENGTH=2 VALUE=\"" + cart[picname].quantity46 + "\"> 4x6 - $7<BR>";
	picwinhtml += "<INPUT ID=quantity57 TYPE=TEXT SIZE=2 MAXLENGTH=2 VALUE=\"" + cart[picname].quantity57 + "\"> 5x7 - $10<BR>";
	picwinhtml += "<INPUT ID=quantity810 TYPE=TEXT SIZE=2 MAXLENGTH=2 VALUE=\"" + cart[picname].quantity810 + "\"> 8x10 - $25<BR>";
	picwinhtml += "<INPUT ID=quantity1218 TYPE=TEXT SIZE=2 MAXLENGTH=2 VALUE=\"" + cart[picname].quantity1218 + "\"> 12x18 - $50<BR>";
	picwinhtml += "<INPUT ID=quantity23 TYPE=TEXT SIZE=2 MAXLENGTH=2 VALUE=\"" + cart[picname].quantity23 + "\"> 2x3 - $25<BR>";
	picwinhtml += "Order Comments<BR><TEXTAREA ID=comments rows=4 cols=20>" + cart[picname].comments + "</TEXTAREA><BR>";
	picwinhtml += "<INPUT width=150 TYPE=BUTTON VALUE=\"Update Cart\" onClick=\"window.opener.addToCart(\'" + picname + "\', this.form.quantity46.value, this.form.quantity57.value, this.form.quantity810.value, this.form.quantity1218.value, this.form.quantity23.value, this.form.comments.value);\"><BR><INPUT width=150 TYPE=BUTTON VALUE=\"Remove From Cart\" onClick=\"window.opener.delCartItem(\'" + picname + "\');\"></FORM></TD>";	picwinhtml += "<TD width=10><IMG width=10 height=10 src=\"/images/px.gif\"></TD>";
	picwinhtml += "<TD align=center valign=middle bgcolor=\"#333333\"><IMG src=\"/photography/images/web/" + picname.slice(0,8) + "/" + picname + "\"></TD><TD><IMG height=480 width=1 src=\"/images/px.gif\"></TD></TR>";
	picwinhtml += "<TR><TD></TD><TD></TD><TD><IMG width=480 height=1 src=\"/images/px.gif\"></TD></TR>";
	picwinhtml += "</TABLE></BODY></HTML>";

	thewin.document.write(picwinhtml);
//	thewin.document.write("<HTML><BODY style=\"text-align:center\" bgcolor=\"#000000\"><IMG src=\"/dancecircle/photography/images/web/" + picname.slice(0,8) + "/" + picname + "\"></BODY></HTML>");
}

//
// Called by Flash to remove an item from the cart
//
function delCartItem(item) {
  //Make sure there is an item to remove
  if (cart[item]) {
    delete cart[item];
  }
  updateCartDiv();
  
    // We probably could do an && for both conditions but I'm not sure if JS would like that when popUpWin is 0...
  if(popUpWin) {
    if(!popUpWin.closed) {
		popUpWin.close();
	} else {
		alert("Removed " + item.slice(0,-4) + " from your shopping cart");
	}
  } else
  {
    alert("Removed " + item.slice(0,-4) + " from your shopping cart");
  }
  
  if (orderform) {
	  if (!orderform.closed) displayCart();
  }
}

//
// Called by Flash to update its own quantity display variables from the cart
//
function updateFlash(item) {
 currentimage = item;
  flasher = document.getElementById("viewer");
  //Make sure the item is in the cart first
  if (cart[item]) {
    // If so, call the setVariable functions to set the quantities and comment
    flasher.SetVariable("mcCaption.q46.text", cart[item].quantity46);
    flasher.SetVariable("mcCaption.q57.text", cart[item].quantity57);
    flasher.SetVariable("mcCaption.q810.text", cart[item].quantity810);
    flasher.SetVariable("mcCaption.q1218.text", cart[item].quantity1218);
    flasher.SetVariable("mcCaption.q23.text", cart[item].quantity23);
    flasher.SetVariable("mcCaption.ordercomments.text", cart[item].comments);
  } else {
    // We can send in 0's for all the values or have Flash set the default value to 0 ahead of time
    flasher.SetVariable("mcCaption.q46.text", "");
    flasher.SetVariable("mcCaption.q57.text", "");
    flasher.SetVariable("mcCaption.q810.text", "");
    flasher.SetVariable("mcCaption.q1218.text", "");
    flasher.SetVariable("mcCaption.q23.text", "");
    flasher.SetVariable("mcCaption.ordercomments.text", "");
  }
} 

//
// Called by Flash (or Javascript) to display the contents of the cart in the Viewer frame
//
function displayCart() {
  // Replace the content of the Viewer frame with a display of what's in the cart.
sendFeedback("test");
}


//
// The old display cart function
//
function sendFeedback(picName) {
  // We should have Flash check to make sure that the current item is updated appropriately in the cart
  //      checkForUnSavedChanges(picName);

      var cost = 0;
      var bgc = "#ffffff";

      orderform = window.open("", "orderform", "location=no,status=no,toolbar=no,resizable=yes,scrollbars=yes");
      orderform.focus();

      orderhtml = "<HTML><HEAD><TITLE>Genesis Dance Championships 2005 - Photo Order Form</TITLE><style>\nbody{font-family: Trebuchet MS, verdana, arial;\n color: #333;\n}\ntd{font-size:11px}\n.pagetitle{font-size:16px;\nfont-weight:bold;\n}\nth{font-size:14px;\nfont-weight:bold;\n}\n</style></HEAD><BODY bgcolor='#000000' text='#000000'><FORM method=POST action=form2mail.php onsubmit=\"if(this.name.value == '' || this.c_street.value == '' || this.email.value == '' || this.c_city.value == ''){alert('Please make sure to fill in all required shipping fields');return false;}\"><TABLE width=95% border=0 align=center cellpadding=0 cellspacing=0><TR><TD align=left><img src='/images/titlebar_dc_left.jpg' height=100 width=400 border=0></TD><TD align=right><img src='/images/titlebar_right.jpg' height=100 width=175 border=0</TD></TR><TR><TH colspan=2 bgcolor='#ffffff' width='100%' class='pagetitle'>Photo Shoot Order Form Instructions</TH></TR><TR><TD colspan=2 bgcolor='#cccccc'><OL><LI>Verify your photo selection.  If you would like to make changes, click on the icon or the filename.</LI><LI>Enter your shipping and contact information.</LI><LI>Press the \"Send Order\" button (at the bottom of the page) to send your order out.</LI><LI>Send your payment by mailing a check, money order, or cash to:<B><BR>Howard Narvaez<BR>DanceCircle Photography<BR>PO Box 360954<BR>Milpitas, CA 95036-0954</B><BR>Your photos will be shipped within a day of when the payment is received.  If you are a PayPal user, you can send your payment to:  <B>howard@dancecircle.net</B></LI></OL></TD></TR></TABLE><BR><TABLE width=95% border=0 bgcolor='#ffffff' align=center cellpadding=3><TR bgcolor='#cccccc'><TH colspan=4>To change a quantity or to remove a photo from your cart, please click on its icon or filename</TH></TR><TR bgcolor='#cccccc'><TH>Photo</TH><TH>Quantity</TH><TH>Cost</TH><TH>Comments</TH></TR>";

      orderform.document.write(orderhtml);

      for (var i in cart) {
        var buy   = 0;
        var q23 = 0;
        var q46 = 0;
        var q57 = 0;
        var q810 = 0;
        var q1114 = 0;
        var q1218 = 0;

	var imageroot = i.slice(0,-4);
        var imagedir = i.slice(0,8);
        var imagepage = imageroot + ".htm";

        var note  = (cart[i].comments != "") ? true : false;

        if (cart[i].quantity23 != "") {q23=cart[i].quantity23;buy++;}
        if (cart[i].quantity46 != "") {q46=cart[i].quantity46;buy++;}
        if (cart[i].quantity57 != "") {q57=cart[i].quantity57;buy++;}
        if (cart[i].quantity810 != "") {q810=cart[i].quantity810;buy++;}
//        if (cart[i].quantity1114 != "") {q1114=cart[i].quantity1114;buy++;}
        if (cart[i].quantity1218 != "") {q1218=cart[i].quantity1218;buy++;}

        orderhtml = "";

        if (buy || note) {

          orderhtml += "<TR bgcolor='" + bgc + "'><TD nowrap valign=\"middle\" width='150' align=center><A href='javascript:window.opener.viewpic(\"" + i + "\");'><img style=\"vertical-align:middle;\" src='../images/thumbs/" + imagedir + "/" + i + "'><br/>" + i + "</A></TD>";

          (!buy) ? orderhtml += "<TD bgcolor='#ff0000'>You added a comment, but did not mark any quantities for this item.  To purchase this item, please click on the icon or filename to the left to update the quantity." : orderhtml += "<TD nowrap>"

          if (isNaN(q23)) {
            orderhtml += "<blink><B>Invalid quantity for 2x3 photos! Click icon or filename to the left to update</blink></B><BR>";
          } else {
            orderhtml += q23 ? ("<input type=hidden name='" + imageroot + "_q23' value='" + q23 + "'>(<B>" + q23 + "</B>) 2x3 sheet (16 count) @ $25 a sheet<BR>") : "";
          }

          if (isNaN(q46)) {
            orderhtml += "<blink><B>Invalid quantity for 4x6 photos! Click icon or filename to the left to update</blink></B><BR>";
          } else {
            orderhtml += q46 ? ("<input type=hidden name='" + imageroot + "_q46' value='" + q46 + "'>(<B>" + q46 + "</B>) 4x6 @ $7 each<BR>") : "";
          }

          if (isNaN(q57)) {
            orderhtml += "<blink><B>Invalid quantity for 5x7 photos! Click icon or filename to the left to update</B></blink><BR>";
          } else {
            orderhtml += q57 ? ("<input type=hidden name='" + imageroot + "_q57' value='" + q57 + "'>(<B>" + q57 + "</B>) 5x7 @ $10 each<BR>") : "";
          }

          if (isNaN(q810)) {
            orderhtml += "<blink><B>Invalid quantity for 8x10 photos! Click icon or filename to the left to update</blink></B><BR>";
          } else {
            orderhtml += q810 ? ("<input type=hidden name='" + imageroot + "_q810' value='" + q810 + "'>(<B>" + q810 + "</B>) 8x10 @ $25 each<BR>") : "";
          }

          if (isNaN(q1218)) {
            orderhtml += "<blink><B>Invalid quantity for 12x18 photos! Click icon or filename to the left to update</blink></B><BR>";
          } else {
            orderhtml += q1218 ? ("<input type=hidden name='" + imageroot + "_q1218' value='" + q1218 + "'>(<B>" + q1218 + "</B>) 12x18 @ $50 each") : "";
          }

	  if (buy) {
            orderhtml += "</TD><TD>";
	    orderhtml += q23 ? ("$" + q23 * 25 + "&nbsp;<BR>") : "";
	    orderhtml += q46 ? ("$" + q46 * 7 + "&nbsp;<BR>") : "";
	    orderhtml += q57 ? ("$" + q57 * 10 + "&nbsp;<BR>") : "";
	    orderhtml += q810 ? ("$" + q810 * 25 + "&nbsp;<BR>") : "";
	    orderhtml += q1218 ? ("$" + q1218 * 50 + "&nbsp;") : "";

	    cost += (q23*25)+(q46*7)+(q57*10)+(q810*25)+(q1218*50);
          } else {
            orderhtml += "</TD><TD>";
          }
          
orderhtml += (cart[i].comments != "") ? ("</TD><TD><INPUT TYPE=hidden name='" + imageroot + "_com' value='" + cart[i].comments + "'>" + cart[i].comments + "</TD></TR>") : "</TD><TD></TD></TR>";

          orderform.document.write(orderhtml);

          if (bgc == "#ffffff") bgc = "#cccccc"; else bgc = "#ffffff";
        }
      }

      var dcost = 0;

      orderform.document.write("</TABLE><BR><TABLE width=95% border=0 align=center cellpadding=0 cellspacing=0><TR><TD valign=top width='50%'><TABLE width='100%' border=0 bgcolor='#ffffff' align=center cellpadding=3><TR bgcolor='#cccccc'><TH align=left colspan=2>Shipping/Handling</TH></TR><TR><TD nowrap>Postal Delivery - <B style='color: #CD2027;'>FREE</B></TD><TD>Items will be shipped by US Mail.  If you would like to arrange a pickup at a local dance studio, please add some comments to your order below and we will see what we can arrange!</TD></TR></TABLE></TD><TD>&nbsp;</TD><TD valign=top width='50%'><TABLE width='100%' border=0 bgcolor='#ffffff' align=center cellpadding=3>");
      orderform.document.write("<TR bgcolor='#cccccc'><TH align=left colspan=2>Cost Total</TH></TR><TR><TD align=right>Photos Cost:</TD><TD>$" + cost + "</TD></TR><TR bgcolor='#cccccc'><TD align=right>Delivery Total:</TD><TD>$" + dcost + "</TD></TR><TR><TH align=right>Total Cost:</TH><TH align=left>$" + (cost + dcost) + "</TD></TR></TABLE></TD></TR></TABLE><BR>");

      orderform.document.write("<TABLE width=95% border=0 bgcolor='#ffffff' align=center cellpadding=3><TR><TD colspan=4 class='pagetitle'>Contact and Shipping Information</TD></TR><TR bgcolor='#cccccc'><TD align=right>* Name:</TD><TD><INPUT name='name' type=text size=30></TD><TD align=right>Phone Number:</TD><TD><INPUT name='c_phone' type=text size=10></TD></TR><TR><TD align=right>* Street Address:</TD><TD><INPUT name='c_street' type=text size=30></TD><TD align=right>* Email Address:</TD><TD><INPUT name='email' type=text size=30></TD></TR><TR bgcolor='#cccccc'><TD align=right>* City/State/Zip:</TD><TD><INPUT name='c_city' type=text size=30></TD><TD></TD><TD></TD></TR><TR><TD align=right>Order Comments:<TD><TEXTAREA name='c_cart' cols=30 rows=4></TEXTAREA></TD><TD colspan=2>* indicates a required field</TD></TR></TABLE><BR>");

      orderform.document.write("<TABLE width=95% border=0 bgcolor='#ffffff' align=center cellpadding=3><TR><TD><INPUT type=button name='Print' value='Print Page' onclick='window.print()'> - Print this page for your records or to send it with your payment</TD></TR><TR><TD><INPUT type=submit name='Submit' value='Send Order'> - Press this button to send your photo order</TD></TR></TABLE>");

      orderform.document.writeln("</FORM></BODY></HTML>");
      orderform.document.close();
    }
