// JScript File
var discountCodes = new Array( 
					new Array("CODE01", "Percent", "10"),
					new Array("CODE02", "Amount", "50")
					);

function recalcCost(SizeID)
{
    var totalCost;
    var itemCost;
    itemCost = document.getElementById("txtFormat" + SizeID).value;
//  alert("Item Cost is " + itemCost);
//  alert("Freight Cost is " + freightCost);
    totalCost = parseFloat(itemCost);
    // Convert this to a currency style
    s = new String(totalCost);
    if(s.indexOf('.') < 0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
//  alert("Total cost is " + s);
    document.getElementById("txtPrice").innerHTML = s;
}
function changeSize(sizeID)
{
    
    // display the  total cost for the selected item
    recalcCost(sizeID);
}
function changeFormat(formatID)
{
    
    // display the  total cost for the selected item
    recalcCost(formatID);
}
function getSelectedSizeOption()
{
    var sizeID = 0;
    
    for (i=0; i<document.getElementsByName("Sizes").length; i++)
    {
        if (document.getElementsByName("Sizes")[i].checked)
        {
            var sOptionName = document.getElementsByName("Sizes")[i].id
            var sObjectName = "SizeOption";
            var sizeID = sOptionName.substring(sObjectName.length, sOptionName.length);
            //alert("ID is " + formatID);
            // Terminate the loop
            break;
        }
    }
    
    return sizeID;
}
function getSelectedFormatOption(pnlFormatID)
{
    var formatID = 0;
    
    for (i=0; i<document.getElementsByName("Format" + pnlFormatID).length; i++)
    {
        if (document.getElementsByName("Format" + pnlFormatID)[i].checked)
        {
            // get the ID so we know which option this is
            var sname = document.getElementsByName("Format" + pnlFormatID)[i].id;
            var sObjectName = "FormatOption";
            //alert("Option is " + sname);
            // the option is the last digit
            formatID = sname.substring(sObjectName.length, sname.length);
            //alert("ID is " + formatID);
            // Terminate the loop
            break;
        }
    }
    
    return formatID;
}
function showCorrectFormat(sizeID)
{
    // HIde all the format panels
    var noFormatPanels = document.getElementById("NoFormat").value;
    for (i=0; i<noFormatPanels; i++)
    {
        document.getElementById("pnlFormat" + i).style.display = "none";
    }
    // Display the panel that matches the size ID
    document.getElementById("pnlFormat" + sizeID).style.display = "block";
}
function showCorrectPrice()
{
    var iSize;
    iSize = getSelectedSizeOption();
    //var iFormat;
    //iFormat = getSelectedFormatOption(iSize);
    recalcCost(iSize);
}
function buyNow()
{

    // Get selected price
    document.getElementById("amount").value = document.getElementById("txtPrice").innerHTML;

    // Get selected size and format
    var iSize;
    iSize = getSelectedSizeOption();
//    document.getElementById("os0").value = document.getElementsByName("Sizes")[iSize].value;
    document.getElementById("os0").value = document.getElementById("SizeOption" + iSize).value;
    
    // Check for a discount code
    if (document.getElementById("txtVoucher").value != "" && document.getElementById("txtVoucher").value != "Voucher Code")
    {
        var i;
        
        for (i =0; i<discountCodes.length; i++)
        {
            // Look for a matching code in our look up list at the top of the file
            if (discountCodes[i][0]== document.getElementById("txtVoucher").value)
            {
				document.getElementById("os1").value = discountCodes[i][0];
				document.getElementById("on1").value = "Discount Voucher";
                // Check if this is an amount or a percentage
                if (discountCodes[i][1] == "Percent")
                {
                    // Percentage discount applies
					var iAmount = document.getElementById("amount").value;
					var iDiscount = discountCodes[i][2];
					var iTheDiscount = (iAmount * iDiscount ) / 100;
                    // Discount is calculated and added to the amount to discount as cant use both methods
                    document.getElementById("discount_amount").value = iTheDiscount;
                    
                }
                else
                {
                    // Fixed amount discount applies
                    document.getElementById("discount_amount").value = discountCodes[i][2];
                }
                // Exit the for loop
                break;
            }
			else
			{
				document.getElementById("on1").value = "";
				document.getElementById("os1").value = "";
			}
        }
        if (i == discountCodes.length)
        {
            alert("Your discount code is invalid please check the code you entered and try again");
            return false;
        }
    }

    return true;
    
}
function addToOrderPage()
{
    // Get the selected image name
    var selectedImage = document.getElementById("txtSelectedImage").value;
    
    // Get the size text
    var sizeID = getSelectedSizeOption();
    var selectedSize = document.getElementById("SizeOption" + sizeID).value;
    
    // Get the format text
    var formatID = getSelectedFormatOption(sizeID);
    var selectedFormat = document.getElementById("FormatOption" + formatID).value;
    
    // Get the total cost
    var totalCost = document.getElementById("txtPrice").innerHTML;
    
    // Create the URL
    var newURL;
   newURL = createURL("online-gallery2-process.aspx");
   
   var timeNow = new Date();
    
    // call the order page with the required parameters
    window.location.href = newURL + "&img=" + selectedImage + "&size=" + selectedSize + "&format=" + selectedFormat + "&cost=" + totalCost + "&id=" + timeNow.getTime() + "&ret=" + document.getElementById("txtReturnPage").value + "&cmd=add";

}
function showOrderPage()
{
    // Create the URL
    var newURL;
   newURL = createURL("online-gallery2-order.aspx");
   
   var timeNow = new Date();
    
    // call the order page with the required parameters
    window.location.href = newURL;
}
function showFinalPage()
{
    // Get the selected image name
    var selectedImage = document.getElementById("txtSelectedImage").value;
    
    // Get the size text
    var sizeID = getSelectedSizeOption();
    var selectedSize = document.getElementById("SizeOption" + sizeID).value;
    
    // Get the format text
    var formatID = getSelectedFormatOption(sizeID);
    var selectedFormat = document.getElementById("FormatOption" + formatID).value;
    
    // Get the total cost
    var totalCost = document.getElementById("txtPrice").innerHTML;
    
    // call the order page with the required parameters
    window.location.href="online-gallery2-final.aspx?img=" + selectedImage + "&size=" + selectedSize + "&format=" + selectedFormat + "&cost=" + totalCost;
}
function changeShipping()
{
   if (document.getElementById("drpDelivery").value <= 0)
   {
      document.getElementById("txtSelectShipping").value = "0.00";
   }
   else if (document.getElementById("drpDelivery").value == 1)
   {
      document.getElementById("txtSelectShipping").value = "20.00";
   }
   else if (document.getElementById("drpDelivery").value == 2)
   {
      document.getElementById("txtSelectShipping").value = "25.00";
   }
   else if (document.getElementById("drpDelivery").value == 3)
   {
      document.getElementById("txtSelectShipping").value = "25.00";
   }
   else if (document.getElementById("drpDelivery").value == 4)
   {
      document.getElementById("txtSelectShipping").value = "25.00";
   }
   /* Update the total cost */
   var noImages;
   var costs = 0;
   noImages = document.getElementById("txtNoImages").value;
   
   for (var i = 1; i <= noImages; i++)
   {
      costs += parseFloat(document.getElementById("txtImage" + i).value) * parseFloat(document.getElementById("txtQty" + i).value);
   }
   costs += parseFloat(document.getElementById("txtSelectShipping").value);
    s = new String(costs);
    if(s.indexOf('.') < 0) { s += '.00'; }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
   document.getElementById("txtTotal").value = s;
   costs = costs / 11;
    s = new String(costs);
    if(s.indexOf('.') < 0) { s += '.00'; }
    if(s.length - s.indexOf('.') > 2) {s = s.substring(0, s.indexOf('.') + 3); }
    if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
   document.getElementById("txtGST").value = s;
}
function updateOrder()
{
   /* Update the quantities */
   if (updateQty())
   {
      /* store the shipping selected */
      getSelectedShipping();
       // Create the URL
       var newURL;
      newURL = createURL("online-gallery2-process.aspx");
      
      var timeNow = new Date();
    
       // call the order page with the required parameters
       window.location.href = newURL + "&qty=" + document.getElementById("txtImageQty").value + "&ship=" + document.getElementById("txtShipping").value + "&shipcost=" + document.getElementById("txtShippingCost").value + "&ret=" + document.getElementById("txtReturnPage").value + "&cmd=update";
    }
    else
    {
       alert("Quantities entered must be numeric, please check your order and click update again");
    }

}
function updateQty()
{
   /* Update the selected qtys */
   var noImages;
   var s = "";
   noImages = document.getElementById("txtNoImages").value;
   /* alert("No images is " + noImages); */
   
   for (var i = 1; i <= noImages; i++)
   {
   /* alert(document.getElementById("txtQty" + i).value); */
      /* Check this is a valid number, if not return false */
      if (isNumber(document.getElementById("txtQty" + i).value))
      {
         s = s + document.getElementById("txtQty" + i).value + ",";
      }
      else
      {
         return false;
      }
   }
   /* alert(s); */
   document.getElementById("txtImageQty").value = s;
   
   return true;
}
function checkQty()
{
   /* Update the selected qtys */
   var noImages;
   var s = "";
   noImages = document.getElementById("txtNoImages").value;
   /* alert("No images is " + noImages); */
   
   for (var i = 1; i <= noImages; i++)
   {
   /* alert(document.getElementById("txtQty" + i).value); */
      s = s + document.getElementById("txtQty" + i).value + ",";
   }
   /* alert ("S=|" + s + "| and txt has |" + document.getElementById("txtImageQty").value + "|"); */
   if (s != document.getElementById("txtImageQty").value)
   {
      return false;
   }
   else
   {
      return true;
   }
}
function removeItem(itemNo)
{
   /* set the number of items of the selected item to 0 */
   document.getElementById("txtQty" + itemNo).value = "0";
   updateOrder();
}
function placeOrder()
{   if (document.getElementById("drpDelivery").selectedIndex == 0)
   {
      alert("You must select a shipping method");
   }
   else
   {
      document.getElementById("txtCommand").value = "Process";
      getSelectedShipping();
      /* Update the quantities */
      if (checkQty())
      {
         document.form1.submit();
      }
      else
      {
         alert("Quantities ordered does not match total cost. Please check your order and then click on the Update button before submitting your order"); 
      }
   }
}
function submitOrder()
{
   document.getElementById("txtCommand").value = "Submit";
   document.form1.submit();
}
function getSelectedShipping()
{
    document.getElementById("txtShipping").value = document.getElementById("drpDelivery").selectedIndex - 1;
    /* get the cost */
    document.getElementById("txtShippingCost").value = document.getElementById("txtSelectShipping").value;
}
function isNumber (InString)  { 
    if(InString.length==0) return (false); 
    var RefString="1234567890"; 
    for (Count=0; Count < InString.length; Count++)  { 
        TempChar= InString.substring (Count, Count+1); 
        if (RefString.indexOf (TempChar, 0)==-1)   
            return (false); 
    } 
    return (true); 
} 

function galleryShippingDetails ()
{ 
   /* Disable all the shipping details textboxes if box ticked and enable them if not */
   if (document.getElementById("chkShippingAddress").checked)
   {
      document.getElementById("txtFirstName2").disabled = true;
      document.getElementById("txtFirstName2").value = "";
      document.getElementById("txtSurname2").disabled = true;
      document.getElementById("txtSurname2").value = "";
      document.getElementById("txtAddress2").disabled = true;
      document.getElementById("txtAddress2").value = "";
      document.getElementById("txtSuburb2").disabled = true;
      document.getElementById("txtSuburb2").value = "";
      document.getElementById("txtPostcode2").disabled = true;
      document.getElementById("txtPostcode2").value = "";
      document.getElementById("txtState2").disabled = true;
      document.getElementById("txtState2").value = "";
      document.getElementById("txtCountry2").disabled = true;
      document.getElementById("txtTelephone2").disabled = true;
      document.getElementById("txtTelephone2").value = "";
      document.getElementById("txtEmail2").disabled = true;
      document.getElementById("txtEmail2").value = "";
   }
   else
   {
      document.getElementById("txtFirstName2").disabled = false;
      document.getElementById("txtSurname2").disabled = false;
      document.getElementById("txtAddress2").disabled = false;
      document.getElementById("txtSuburb2").disabled = false;
      document.getElementById("txtPostcode2").disabled = false;
      document.getElementById("txtState2").disabled = false;
      document.getElementById("txtCountry2").disabled = false;
      document.getElementById("txtTelephone2").disabled = false;
      document.getElementById("txtEmail2").disabled = false;
   }

} 

function getQuerystring(key, default_)
{
  if (default_==null) default_="";
  key = key.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regex = new RegExp("[\\?&]"+key+"=([^&#]*)");
  var qs = regex.exec(window.location.href);
  if(qs == null)
    return default_;
  else
    return qs[1];
}

var theImage;
var theTitle;
var theCode;
function outputImage()
{
    var sOutputImage;
    // Get the image to display and its title and alt
    theImage = getQuerystring('img', '');
    theTitle = getQuerystring('title', '');
    theCode = getQuerystring('code', '');
    sOutputImage = '<a id="galleryImageBuy0" class="galleryImageBuy" rel="group" href="onlinegallery/images/large/';
    sOutputImage = sOutputImage + theImage;
    sOutputImage = sOutputImage + '.jpg" title="' + theTitle + '" ';
    sOutputImage = sOutputImage + 'alt="' + theTitle + '" ';
    sOuputImage = sOutputImage + ' ><br />';
    sOutputImage = sOutputImage + '<div class="galleryImageBuyText"><br />';
    sOuputImage = sOutputImage + theTitle + '<br />';
    sOutputImage = sOutputImage + '</div><br />';
    sOutputImage = sOutputImage + '<img class="galleryImageBuySmall" id="galleryImageBuySmall" src="onlinegallery/images/medium/' + theImage + '.jpg" ';
    sOutputImage = sOutputImage + 'title="' + theTitle + '" alt="' + theTitle + '" border="0" align="middle" width="320px" height="480px" ><br />';
    sOuputImage = sOutputImage + ' <div class="galleryImageBuyText2">Catalogue No.: ' + theCode + '</div>';
    document.write(sOutputImage);
    
}

function outputMattedPrintButtons(collection)
{
    var sOutputButton;
    
    sOutputButton = '<span id="pnlSize">Artist - $160.00 ';
    sOutputButton = sOutputButton + '<a href="https://www.paypal.com/cgibin/webscr?';
    sOutputButton = sOutputButton + 'undefined_quantity=1&amp;';
    sOutputButton = sOutputButton + 'business=dave@madcat.com.au&amp;';
    sOutputButton = sOutputButton + 'image_url=&amp;';
    sOutputButton = sOutputButton + 'return=' + window.location.href +'&amp;';
    sOutputButton = sOutputButton + 'cancel_return=' + window.location.href +'&amp;';
    sOutputButton = sOutputButton + 'item_name=' + theTitle + '&amp;';
    sOutputButton = sOutputButton + 'amount=160&amp;';
    sOutputButton = sOutputButton + 'shipping=0&amp;';
    sOutputButton = sOutputButton + 'currency_code=AUD&amp;';
    sOutputButton = sOutputButton + 'item_number=' + theCode + '&amp;';
    sOutputButton = sOutputButton + 'on0=size&amp;';
    sOutputButton = sOutputButton + 'os0=artist&amp;';
    sOutputButton = sOutputButton + 'on1=media&amp;';
    sOutputButton = sOutputButton + 'os1=matted print&amp;';
    sOutputButton = sOutputButton + 'cmd=_cart&amp;add=1" shape="rect" target="_blank">';
    sOutputButton = sOutputButton + '<img alt="Buy Now" src="https://www.paypal.com/en_US/i/btn/x-click-but3.gif" border="0"></a><br />';
    sOutputButton = sOutputButton + '</span>'
    document.write(sOutputButton);

}
function outputPrintButtons(collection)
{
    var sOutputButton;
    
    
    sOutputButton = '<span id="pnlSize">Studio - $260.00 ';
    sOutputButton = sOutputButton + '<a href="https://www.paypal.com/cgibin/webscr?';
    sOutputButton = sOutputButton + 'undefined_quantity=1&amp;';
    sOutputButton = sOutputButton + 'business=dave@madcat.com.au&amp;';
    sOutputButton = sOutputButton + 'image_url=&amp;';
    sOutputButton = sOutputButton + 'return=' + window.location.href +'&amp;';
    sOutputButton = sOutputButton + 'cancel_return=' + window.location.href +'&amp;';
    sOutputButton = sOutputButton + 'item_name=' + theTitle + '&amp;';
    sOutputButton = sOutputButton + 'amount=260&amp;';
    sOutputButton = sOutputButton + 'shipping=0&amp;';
    sOutputButton = sOutputButton + 'currency_code=AUD&amp;';
    sOutputButton = sOutputButton + 'item_number=' + theCode + '&amp;';
    sOutputButton = sOutputButton + 'on0=size&amp;';
    sOutputButton = sOutputButton + 'os0=studio&amp;';
    sOutputButton = sOutputButton + 'on1=media&amp;';
    sOutputButton = sOutputButton + 'os1=print&amp;';
    sOutputButton = sOutputButton + 'cmd=_cart&amp;add=1" shape="rect" target="_blank">';
    sOutputButton = sOutputButton + '<img alt="Buy Now" src="https://www.paypal.com/en_US/i/btn/x-click-but3.gif" border="0"></a><br />';
    sOutputButton = sOutputButton + '</span><br />'
    sOutputButton = sOutputButton + '<span id="pnlSize">Gallery - $425.00 ';
    sOutputButton = sOutputButton + '<a href="https://www.paypal.com/cgibin/webscr?';
    sOutputButton = sOutputButton + 'undefined_quantity=1&amp;';
    sOutputButton = sOutputButton + 'business=dave@madcat.com.au&amp;';
    sOutputButton = sOutputButton + 'image_url=&amp;';
    sOutputButton = sOutputButton + 'return=' + window.location.href +'&amp;';
    sOutputButton = sOutputButton + 'cancel_return=' + window.location.href +'&amp;';
    sOutputButton = sOutputButton + 'item_name=' + theTitle + '&amp;';
    sOutputButton = sOutputButton + 'amount=425&amp;';
    sOutputButton = sOutputButton + 'shipping=0&amp;';
    sOutputButton = sOutputButton + 'currency_code=AUD&amp;';
    sOutputButton = sOutputButton + 'item_number=' + theCode + '&amp;';
    sOutputButton = sOutputButton + 'on0=size&amp;';
    sOutputButton = sOutputButton + 'os0=gallery&amp;';
    sOutputButton = sOutputButton + 'on1=media&amp;';
    sOutputButton = sOutputButton + 'os1=print&amp;';
    sOutputButton = sOutputButton + 'cmd=_cart&amp;add=1" shape="rect" target="_blank">';
    sOutputButton = sOutputButton + '<img alt="Buy Now" src="https://www.paypal.com/en_US/i/btn/x-click-but3.gif" border="0"></a><br />';
    sOutputButton = sOutputButton + '</span>'
    document.write(sOutputButton);

}
function outputCanvasButtons(collection)
{
    var sOutputButton;
    
    
    sOutputButton = '<span id="pnlSize">Studio - $260.00 ';
    sOutputButton = sOutputButton + '<a href="https://www.paypal.com/cgibin/webscr?';
    sOutputButton = sOutputButton + 'undefined_quantity=1&amp;';
    sOutputButton = sOutputButton + 'business=dave@madcat.com.au&amp;';
    sOutputButton = sOutputButton + 'image_url=&amp;';
    sOutputButton = sOutputButton + 'return=' + window.location.href +'&amp;';
    sOutputButton = sOutputButton + 'cancel_return=' + window.location.href +'&amp;';
    sOutputButton = sOutputButton + 'item_name=' + theTitle + '&amp;';
    sOutputButton = sOutputButton + 'amount=285&amp;';
    sOutputButton = sOutputButton + 'shipping=0&amp;';
    sOutputButton = sOutputButton + 'currency_code=AUD&amp;';
    sOutputButton = sOutputButton + 'item_number=' + theCode + '&amp;';
    sOutputButton = sOutputButton + 'on0=size&amp;';
    sOutputButton = sOutputButton + 'os0=studio&amp;';
    sOutputButton = sOutputButton + 'on1=media&amp;';
    sOutputButton = sOutputButton + 'os1=canvas&amp;';
    sOutputButton = sOutputButton + 'cmd=_cart&amp;add=1" shape="rect" target="_blank">';
    sOutputButton = sOutputButton + '<img alt="Buy Now" src="https://www.paypal.com/en_US/i/btn/x-click-but3.gif" border="0"></a><br />';
    sOutputButton = sOutputButton + '</span><br />'
    sOutputButton = sOutputButton + '<span id="pnlSize">Gallery - $425.00 ';
    sOutputButton = sOutputButton + '<a href="https://www.paypal.com/cgibin/webscr?';
    sOutputButton = sOutputButton + 'undefined_quantity=1&amp;';
    sOutputButton = sOutputButton + 'business=dave@madcat.com.au&amp;';
    sOutputButton = sOutputButton + 'image_url=&amp;';
    sOutputButton = sOutputButton + 'return=' + window.location.href +'&amp;';
    sOutputButton = sOutputButton + 'cancel_return=' + window.location.href +'&amp;';
    sOutputButton = sOutputButton + 'item_name=' + theTitle + '&amp;';
    sOutputButton = sOutputButton + 'amount=470&amp;';
    sOutputButton = sOutputButton + 'shipping=0&amp;';
    sOutputButton = sOutputButton + 'currency_code=AUD&amp;';
    sOutputButton = sOutputButton + 'item_number=' + theCode + '&amp;';
    sOutputButton = sOutputButton + 'on0=size&amp;';
    sOutputButton = sOutputButton + 'os0=gallery&amp;';
    sOutputButton = sOutputButton + 'on1=media&amp;';
    sOutputButton = sOutputButton + 'os1=canvas&amp;';
    sOutputButton = sOutputButton + 'cmd=_cart&amp;add=1" shape="rect" target="_blank">';
    sOutputButton = sOutputButton + '<img alt="Buy Now" src="https://www.paypal.com/en_US/i/btn/x-click-but3.gif" border="0"></a><br />';
    sOutputButton = sOutputButton + '</span>'
    document.write(sOutputButton);
}


