// pop-up windows
function openPopUp(PageName){
    new_window = window.open(PageName, "new_window", "width=400,height=400,location=0,menubar=0,resizable=0,scrollbars=1,status=0,titlebar=0,toolbar=0,left=150,top=50,screenx=150,screeny=50");
    if (new_window.opener == null) {
        new_window.opener = self;
    }
    new_window.focus();
}

function openSizedPopUp(PageName, Width, Height){
    new_window = window.open(PageName, "new_window", "width=" + Width + ",height=" + Height + ",location=0,menubar=0,resizable=0,scrollbars=1,status=0,titlebar=0,toolbar=0,left=150,top=50,screenx=150,screeny=50");
    if (new_window.opener == null) {
        new_window.opener = self;
    }
    new_window.focus();
}

// quick order form validation
function validateQuickOrder(form) {        
    if ((form.ProductNumber.value == "")|| (form.ProductNumber.value == "Enter Product #")){
        alert("Please enter an item number.");
        form.ProductNumber.focus();
        return false;
    }
    return true;
}

// used to generate random string that will be appended to AJAX requests to avoid getting cached response
function rnd() { 
    return String((new Date()).getTime()).replace(/\D/gi,'')
}

// used to retrieve the debug flag
function debugFlag() { 
    if (null != document.getElementsByName("debug")[0])
        return String('&dminsite=' + document.getElementsByName("debug")[0].value);
    else
        return '';
}

// Used on one-page multi-ship checkout to populate the shipping address from the billing address.
function UseBillingAsShippingMS(form){

    if (form.ship_to_bill_address.checked) {
        form.Email_SHIP.value = form.Email.value;
        form.ConfEmail_SHIP.value = form.ConfEmail.value;
        form.FirstName_SHIP.value = form.FirstName.value;
        form.LastName_SHIP.value = form.LastName.value;
        form.Address1_SHIP.value = form.Address1.value;
        form.Address2_SHIP.value = form.Address2.value;
        form.City_SHIP.value = form.City.value;
        form.State_SHIP.value = form.State.value;
        form.Country_SHIP.value = form.Country.value;
        form.PostalCode_SHIP.value = form.PostalCode.value;
        form.Phone_SHIP.value = form.Phone.value;
      
        $( '#County_SHIP' ).parent().toggle( form.State_SHIP.value == 'SC' );

        populateShippingMethods();
    }
    else {
        form.Email_SHIP.value = '';
        form.ConfEmail_SHIP.value = '';
        form.Address1_SHIP.value = '';
        form.Address2_SHIP.value = '';
        form.City_SHIP.value = '';
        form.State_SHIP.value = '';
        form.Country_SHIP.value = '';
        form.PostalCode_SHIP.value = '';
        form.FirstName_SHIP.value = '';
        form.LastName_SHIP.value = '';
        form.Phone_SHIP.value = '';
        
        $( '#County_SHIP' ).parent().hide();

        populateShippingMethods();
    }
}

// Used on one-page multi-ship checkout to apply a promo code.
function applyKeyCodeCheckout()
{
    var keycode = document.getElementsByName("keycode")[0].value;
    DmiAjaxFetch('CheckOutCartItemsContentMS', 'DmiAjaxSecure.aspx', 'request=CheckOutCartItemsContentMS&keycode=' + keycode + '&extra=', rnd());
    return false;
}

// Used on one-page multi-ship checkout to update billing state or country and order total
function updateBillingInfoMS()
{
    var state_bill = document.getElementsByName("State")[0].value;
    var country_bill = document.getElementsByName("Country")[0].value;
    var zip_bill = document.getElementsByName("PostalCode")[0].value;

    if (state_bill != " - Select - " && country_bill != " - Select - ")
    {
        DmiAjaxFetch('CheckoutCartTotalContentMS', 'DmiAjaxSecure.aspx', 'request=CheckoutCartTotalContentMS&state_bill=' + state_bill + '&country_bill=' + country_bill + '&zip_bill=' + zip_bill + debugFlag() + '&extra=', rnd());
    }
}

// Used on one-page multi-ship checkout to update order total after populateShippingMethods is called.
function populateCartTotalCheckout()
{
    DmiAjaxFetch('CheckoutCartTotalContentMS', 'DmiAjaxSecure.aspx', 'request=CheckoutCartTotalContentMS' + debugFlag() + '&extra=', rnd());
}

// Used on one-page multi-ship checkout 
function populateShippingMethods(recipientId)
{
    var recipient = '';
    var recipientNamePart = '';

    if ( recipientId == '' || recipientId == null || recipientId == 'undefined' ) {
        recipient = 'Default';
    } else {
        recipient = recipientId;
    }

    // if not the default, add the name part to retrieve specific recipient form elements
    if ( recipient != 'Default' && recipient != '' ) {
        recipientNamePart = '_' + recipient;
    }

    var state_ship = document.getElementsByName("State_SHIP" + recipientNamePart)[0].value;
    var country_ship = document.getElementsByName("Country_SHIP" + recipientNamePart)[0].value;
    var zip_ship = document.getElementsByName("PostalCode_SHIP" + recipientNamePart)[0].value;

    if ( state_ship != " - Select - " && country_ship != " - Select - " ) {
        
        var ship_method;
        if( document.getElementById( "shipping_method" + recipientNamePart ) != null ) {
            //shipping_method dropdown wont exist if page portion is being refreshed by AJAX call
            ship_method = document.getElementsByName("shipping_method" + recipientNamePart)[0].value;
        } else {
            ship_method = ""; // won't get set on call, but will use existing or default
        }

        // must pass recipient or else the built section will not be able to resend the recipient
        DmiAjaxFetch( 'CheckoutShippingMethods' + recipientNamePart, 'DmiAjaxSecure.aspx', 'request=' + 'CheckoutShippingMethods' + recipientNamePart + '&state_ship=' + state_ship + '&country_ship=' + country_ship + '&zip_ship=' + zip_ship + '&shipping_method=' + ship_method + '&recipient=' + recipient + debugFlag() + '&extra=', rnd() );

        setTimeout("populateCartTotalCheckout()", 2000);
    }
}



// used by product lists to remove list items without page reload
function removeVisitorProductListItem(listItemId, listName, listType, viewControl) {
   DmiAjaxFetch(viewControl, 'DmiAjax.aspx', 'request=RemoveListItem&listItemId=' + listItemId + '&listName=' + listName + '&listType=' + listType + '&viewControl=' + viewControl + '&extra=', rnd());
}

// used by product lists to add list items without page reload
function addVisitorProductListItem(productNumber, listName, listType, quantityDesired) {
   DmiAjaxFetch('ProductListView', 'DmiAjax.aspx', 'request=AddListItem&productNumber=' + productNumber + '&listName=' + listName + '&listType=' + listType + '&quantityDesired=' + quantityDesired + '&extra=', rnd());
}

// used by product lists to update list privacy without page load
function updateVisitorProductListPrivacy(listName, listType, isPublic)
{
   DmiAjaxFetch('ProductListPrivacyView', 'DmiAjax.aspx', 'request=UpdateListPrivacy&listName=' + listName + '&listType=' + listType + '&isPublic=' + isPublic + '&extra=', rnd());
}

// handle correlated attributes
function updateCorrelatedAttributes(attrId, productNumber, attrValueId, attrNumber)
{
  var attributeValue1 = document.getElementsByName("correlated_attribute1")[0].value;
  var attributeValue2 = document.getElementsByName("correlated_attribute2")[0].value;
  var attributeValue3 = document.getElementsByName("correlated_attribute3")[0].value;

  var attributeId1 = document.getElementsByName("correlated_attribute_id1")[0].value;
  var attributeId2 = document.getElementsByName("correlated_attribute_id2")[0].value;
  var attributeId3 = document.getElementsByName("correlated_attribute_id3")[0].value;

  //alert('attr id:' + attrId + ' p=' + productNumber + ' attrValId: ' + attrValueId + ' attrNum:' + attrNumber + ' attr1:' + attributeValue1 + ' attr2:' + attributeValue2 + ' attr3:' + attributeValue3 + ' attrId1:' + attributeId1 + ' attrId2:' + attributeId2 + ' attrId3:' + attributeId3);

   DmiAjaxFetch('CorrelatedAttributes', 'DmiAjax.aspx', 'request=CorrelatedAttributes&p=' + productNumber + '&attrId=' + attrId + '&attrId1=' + attributeId1 + '&attrId2=' + attributeId2 + '&attrId3=' + attributeId3 + '&attrValue1=' + attributeValue1 + '&attrValue2=' + attributeValue2 + '&attrValue3=' + attributeValue3 + '&attrNumber=' + attrNumber + '&extra=', rnd());
}

function ApplyGiftCertificateToCart(certificateNumber1, certificateNumber2, certificateNumber3, certificateNumber4, certificateNumber5)
{
  var giftCertificateQueryString = "&giftcert1=" + certificateNumber1 + "&giftcert2=" + certificateNumber2 + "&giftcert3=" + certificateNumber3 + "&giftcert4=" + certificateNumber4 + "&giftcert5=" + certificateNumber5;

  DmiAjaxFetch('GiftCertificateMessageContent','DmiAjaxSecure.aspx','request=ApplyGiftCertificate'+giftCertificateQueryString+'&extra=',rnd());

  PauseScript(2000);

  try 
  {
    populateCartTotalCheckout();
  }
  catch(err){}

  return false;
}

function ApplyGiftCertificateToCartClick(certificateNumber1, certificateNumber2, certificateNumber3, certificateNumber4, certificateNumber5)
{
  // try 
  // { 
    //alert('apply gc: ' + certificateNumber);
    document.getElementById('GiftCertificateMessageContent').innerHTML='Applying Gift Card';

    ApplyGiftCertificateToCart(certificateNumber1, certificateNumber2, certificateNumber3, certificateNumber4, certificateNumber5);

    document.getElementById('giftCert1').value='';
    document.getElementById('giftCert2').value='';
    document.getElementById('giftCert3').value='';
    document.getElementById('giftCert4').value='';
    document.getElementById('giftCert5').value='';
  /* }
  catch(err)
  {
    try 
    {
      document.getElementById('GiftCertificateMessageContent').innerHTML='An error occurred. Please try again.';
    }
    catch(err){}
  } */

}

function PauseScript(millis) 
{
  var date = new Date();
  var curDate = null;
  do { curDate = new Date(); } 
  while(curDate-date < millis);
} 

// retrieve product inventory based on trait selection 
function getProductInventory(productNumber, attribute1, attributeValue1, attribute2, attributeValue2, attribute3, attributeValue3, attribute4, attributeValue4, attribute5, attributeValue5, attribute6, attributeValue6, attribute7, attributeValue7, attribute8, attributeValue8)
{
    //alert('productNumber=' + productNumber+ '&attribute1=' + attribute1 + '&attributeValue1=' + attributeValue1 + '&attribute2=' + attribute2 + '&attributeValue2=' + attributeValue2 + '&attribute3=' + attribute3 + '&attributeValue3=' + attributeValue3 + '&attribute4=' + attribute4 + '&attributeValue4=' + attributeValue4 + '&attribute5=' + attribute5 + '&attributeValue5=' + attributeValue5 + '&attribute6=' + attribute6 + '&attributeValue6=' + attributeValue6 + '&attribute7=' + attribute7 + '&attributeValue7=' + attributeValue7 + '&attribute8=' + attribute8 + '&attributeValue8=' + attributeValue8);

    DmiAjaxFetch( 'ProductInventoryStockStatus', 'DmiAjax.aspx', 'request=' + 'ProductInventoryStockStatus' + '&productNumber=' + productNumber+ '&attribute1=' + attribute1 + '&attributeValue1=' + attributeValue1 + '&attribute2=' + attribute2 + '&attributeValue2=' + attributeValue2 + '&attribute3=' + attribute3 + '&attributeValue3=' + attributeValue3 + '&attribute4=' + attribute4 + '&attributeValue4=' + attributeValue4 + '&attribute5=' + attribute5 + '&attributeValue5=' + attributeValue5 + '&attribute6=' + attribute6 + '&attributeValue6=' + attributeValue6 + '&attribute7=' + attribute7 + '&attributeValue7=' + attributeValue7 + '&attribute8=' + attribute8 + '&attributeValue8=' + attributeValue8 + '&extra=', rnd() );

}

