﻿// Global Variables
var curQty;
var Confirm = '0';
var userInput = '';

// Global Functions
function goto(page) {
  window.location = page;
}
function scrollTop() {
  $("html, body").animate({ scrollTop: 0 }, "slow");
  return false;
}
function ValidatePage() {
  var valid = Page_IsValid;
  if (!valid) return false;
  valid = ASPxClientEdit.ValidateGroup("");
  if (!valid) return false;
  return true;
}

// Parts Search Functions
function performSearch(query) {
  if (!(query.length > 0)) return;
  if (cbPanel.InCallback())
    postponedCallbackValue = query;
  else
    cbPanel.PerformCallback(query);
  clearSearchBoxes();
}
function performSearchl(query) {
  if (!(query.length > 0)) return;
  if (query.toLowerCase().indexOf(" ") >= 0) return searchError();
  if (query.toLowerCase().indexOf(".") >= 0) return searchError();
  if (query.toLowerCase().indexOf("-") >= 0) return searchError();
  if (query.toLowerCase().indexOf("/") >= 0) return searchError();
  if (query.toLowerCase().indexOf("\\") >= 0) return searchError();
  if (query.toLowerCase().indexOf(",") >= 0) return searchError();
  query=query.replace('/', '');
  //query.replace('\\', '');
  //goto('Results.aspx?id=' + query);
  goto('Search-' + query);
}
function performSearchp(query) {
  if (!(query.length > 0)) return;
  if (query.toLowerCase().indexOf(" ") >= 0) return searchErrorp();
  if (query.toLowerCase().indexOf(".") >= 0) return searchErrorp();
  if (query.toLowerCase().indexOf("-") >= 0) return searchErrorp();
  if (query.toLowerCase().indexOf("/") >= 0) return searchErrorp();
  if (query.toLowerCase().indexOf("\\") >= 0) return searchErrorp();
  if (query.toLowerCase().indexOf(",") >= 0) return searchErrorp();
  query=query.replace('/', '');
  //query.replace('\\', '');
  //goto('Results.aspx?id=' + query);
  ppcAddCart.Hide();
  goto('Search-' + query);
}
function validateRequestPartNumbers() {
  var isValid = true;
  var part;
  for (i = 1; i <= 5; i++) {
    part = $('#ctl00_ctl00_cphMain_cphMain_cbPanel2_txtPart' + i);
    if (i == 1 && part.val().length == 0) { ShowAlertWithCallback('<b>Please enter a value for Part#1.</b>', part); return false; };
    if (part.val().length > 0) {
      if (!validatePartNumber(part.val())) isValid = false;
    }
  }
  if (!ASPxClientEdit.ValidateGroup("")) { ShowAlert('<b>Must enter a valid phone number.</b>'); return false; }
  if (!Page_ClientValidate("Parts")) {
    ShowAlert('<b>Please input a part number.</b>');
    return false;
  };
  //return false;
  if (isValid) GetQuote();
}
function validatePartNumber(query) {
  if (!(query.length > 0)) return true;
  if (query.toLowerCase().indexOf(" ") >= 0) return searchError();
  if (query.toLowerCase().indexOf(".") >= 0) return searchError();
  if (query.toLowerCase().indexOf("-") >= 0) return searchError();
  if (query.toLowerCase().indexOf("/") >= 0) return searchError();
  if (query.toLowerCase().indexOf("\\") >= 0) return searchError();
  if (query.toLowerCase().indexOf(",") >= 0) return searchError();
  return true;
}
function validatePartNumberWithCallback(query,element) {
  if (!(query.length > 0)) return true;
  if (query.toLowerCase().indexOf(" ") >= 0) return searchError2(element);
  if (query.toLowerCase().indexOf(".") >= 0) return searchError2(element);
  if (query.toLowerCase().indexOf("-") >= 0) return searchError2(element);
  if (query.toLowerCase().indexOf("/") >= 0) return searchError2(element);
  if (query.toLowerCase().indexOf("\\") >= 0) return searchError2(element);
  if (query.toLowerCase().indexOf(",") >= 0) return searchError2(element);
  return true;
}
function searchError() {
  ShowAlert('<b>Please only enter the part number without any spaces, dashes, dots or slashes.</b>', 2)
  return false;
}
function searchError2(element) {
  ShowAlertWithCallback('<b>Please only enter the part number without any spaces, dashes, dots or slashes.</b>', element)
  return false;
}
function searchErrorp() {
  alert('Please only enter the part number without any spaces, dashes, dots or slashes.')
  return false;
}
function quotePop(prodID, mID, t) {
  //goto('RequestQuote-' + prodID + '-' + mID);
  //goto('requestaquote.aspx?id=' + prodID + '&m=' + mID);
  //return;
  if (prodID.length > 0) {
    ppcAddCart.SetSize(715, 640);
  }
  else {
    ppcAddCart.SetSize(715, 640);
  }
  ppcAddCart.SetHeaderText('Request a Quote');
  ppcAddCart.SetContentHtml('');
  ppcAddCart.SetContentUrl('RequestQuote.aspx?id=' + prodID + '&m=' + mID);
  if (t.length > 0) {
    (t == '1') ? ppcAddCart.SetHeaderText('Interchange Specialist') : ppcAddCart.SetHeaderText('Request a Quote');
    ppcAddCart.SetContentUrl('RequestQuote.aspx?id=' + prodID + '&m=' + mID + '&t=' + t);
  }
  ppcAddCart.Show();
}
function endCallBack() {

  if (postponedCallbackValue != null) {
    cbPanel.PerformCallback(postponedCallbackValue);
    postponedCallbackValue = null;
  }
}
function clearSearchBoxes() {
  $('#partno').val('');
}
function getDetails(productID) {
  window.location = 'ProductDetails.aspx?id=' + productID;
}

// Shopping Cart Functions
function Add2Cart() {
  if (parseInt(txtQty.GetText()) < 1) { lblMessage.SetText('Purchase quantity cannot be zero.'); return false; }
  cbpMain.PerformCallback();
  return false;
}
function Added2Cart(s,e) {
  if (s.cpStatus == '1') { closeAddCartPopup();window.parent.refreshHeaderCart(); return false; }
  lblMessage.SetText('Error adding product to shopping cart.'); return false;
}
function closeAddCartPopup() {
  window.parent.closeAddCart();
  return false;
}
function add(prodID, prodName, QtyBox, mID, price) {
  var Qty = QtyBox.val();
  if ((!Qty) || (!parseInt(Qty) > 0)) {
    ShowAlert('Please input quantity first.');
    return false;
  }
  curQty = QtyBox;
  cbPanel.PerformCallback('addCart|' + prodID + '|' + Qty + '|' + mID + '|' + price + '|' + prodName);
  /*ppcAddCart.SetSize(500, 255);
  ppcAddCart.SetHeaderText('Add to Cart');
  ppcAddCart.SetContentHtml('');
  ppcAddCart.SetContentUrl('AddtoCart.aspx?id=' + prodID + '&q=' + Qty + '&m=' + mID.html());
  ppcAddCart.Show();*/
  return false;
}
function closeAddCart() {
  ppcAddCart.Hide();
  curQty.val('');
}
function refreshHeaderCart() {
  cbCart.PerformCallback();
}
function EmptyCart() {
  if (Confirm != '0') {
    var response = Confirm;
    Confirm = '0';
    if (response == 'Yes') {
      $.get('Notifications.aspx?t=13721', function (data) {
        if (data.split('|')[0] == 'OK') {
          try { cbPanel.PerformCallback('clear'); } catch (e) { }
          cbCart.PerformCallback('clear');
        }
        else {
          ShowAlert('There was an error in emptying shopping cart contents.');
        }
      });
    }
  }
  else{
    ShowConfirm("Are you sure you want to remove all selected products from your shopping cart?","EmptyCart");
  }
}
function EmptyCartFinal() {
  if (Confirm != '0') {
    var response = Confirm;
    Confirm = '0';
    if (response == 'Yes') {
      $.get('Notifications.aspx?t=13721', function (data) {
        if (data.split('|')[0] == 'OK') {
          cbPanel.PerformCallback('clear');
          cbCart.PerformCallback('clear');
          goto('Default.aspx');
        }
        else {
          ShowAlert('There was an error in emptying shopping cart contents.');
        }
      });
    }
  }
  else{
    ShowConfirm("Are you sure you want to remove all selected products from your shopping cart?","EmptyCartFinal");
  }
}
function UpdateCart() {
  cbPanel.PerformCallback('update');
}
function CheckoutCart() {
  if ($('#ctl00_cphMain_cbPanel_divNoData').length > 0) {
    ShowAlert("There are no items in your shopping cart.");
  }
  else {
    cbPanel.PerformCallback('checkout');
  }
}
function CheckoutCartFromShipping() {
  if (Cart.length > 0) {
    ShowAlert("There are no items in your shopping cart.");
  }
  else {
    var valid = Page_IsValid;
    if (!valid) return false;
    valid = ASPxClientEdit.ValidateGroup("");
    if (!valid) return false;    
    cbPanel.PerformCallback('checkout');
  }
}
function CheckoutCartHeader() {
  if (parseInt($('.cartQty').html().replace(/^.*\((.*)\).*$/m, '$1')) > 0) {
    cbCart.PerformCallback('checkout');    
  }
  else {
    ShowAlert("There are no items in your shopping cart.");
  }
}
function updatePrice(s) {
	var qty = s.GetText();
	var unit = 0.00;
	unit = parseFloat(lblUnitPrice.GetText().substring(1));
	lblPrice.SetText('$' + (qty * unit).toFixed(2));
}
function CheckoutCartFinal(s) {
  if (s.GetSelectedIndex() == '0') {
    var valid = Page_IsValid;
    if (!valid) return false;
    valid = ASPxClientEdit.ValidateGroup("");
    if (!valid) return false;
  }
  cbPanel.PerformCallback('checkout');
}
function clearShippingForm() {
  if (Confirm != '0') {
    var response = Confirm;
    Confirm = '0';
    if (response == 'Yes') {
      txtFirstName.SetText('');
      txtLastName.SetText('');
      txtInitials.SetText('');
      txtEmail.SetText('');
      txtPhone.SetText('');
      txtPhoneExt.SetText('')
      txtCompany.SetText('');
      txtAddress1.SetText('');
      txtAddress2.SetText('');
      txtState.SetText('');
      txtZip.SetText('');
      txtCity.SetText('');
      cmbCountry.SetSelectedIndex(-1);
    }
  }
  else {
    ShowConfirm("Are you sure you want to clear all fields?", "clearShippingForm");
  }
}
function changePaymentMethod(s) {
  if (s.GetSelectedIndex() == '2') {
    ShowAlert('This method is not implemented yet.', 1);
    s.SetSelectedIndex(0);
    return false;
  }
  else {
    if (s.GetSelectedIndex() == '0') {
      $('#CCDetails').show();
      $('#CVCDetails').show();
    }
    else if (s.GetSelectedIndex() == '1') {
      $('#CCDetails').hide();
      $('#CVCDetails').hide();
    }
  }
}

//Root Login and Cart Header Functions

/*function addLoginHover() {
  $("#loginBtnWrap").hover(function () { $('#loginForm').css({ visibility: "visible", display: "none" }).show(268); $('ctl00_cbLogin_txtUsername').focus(); },
      function () { $('#loginForm').css({ visibility: "hidden" }); });
}*/
function DoLogin() {
  if (ValidatePage()) {
    cbPanel.PerformCallback();
  }
}
function addCartHover(){
  $("#headerCart #cart_inner").css({ display: "none" }); // Opera Fix 
  $("#headerCart").hover(function () { $(this).find('#cart_inner').css({ visibility: "visible", display: "none" }).show(268); },
      function () { $(this).find('#cart_inner').css({ visibility: "hidden" }); });
}
function login(e) {
  if (e.html() == 'Logout') {
    cbCart.PerformCallback('Logout'); 
  }
  else {
    goto('Login.aspx');
  };
  //return false;
  //$('#loginForm').toggle();
}

//Messagebox Functions
function ShowAlert(msg,type) {
  if (type == null) { type = 0 };
  switch (type) {
    case 0:
      $.msgbox(msg);
      break;
    case 1:
      $.msgbox(msg, { type: "info" });
      break;
    case 2:
      $.msgbox(msg, { type: "error" });
      break;
    default:
      $.msgbox(msg);
      break;
  }
}
function ShowAlertWithCallback(msg, callback) {
  $.msgbox(msg, { type: "error" }, function () { callback.focus(); });
}
function ShowConfirm(msg, name) {
  Confirm = '0';
  $.msgbox(msg, {
    type: "confirm",
    buttons: [
    { type: "submit", value: "Yes" },
    { type: "submit", value: "No" }//,
    //{ type: "cancel", value: "Cancel" }
  ]
  }, function (result) { Confirm = result; window[name](); });
}
function ShowInput(msg, name) {
  $.msgbox(msg, {
    type: "prompt"
  }, function (result) { userInput = result; window[name](); });
}
function WaitforAction() {
  if (Confirm !='') return;
  while (Confirm == '0') {setTimeout(function () { var a = 1; }, 1000) }
}
/* Customer Registration */
function DoRegistration() {
  var valid = Page_IsValid;
  if (!valid) return false;
  valid = ASPxClientEdit.ValidateGroup("");
  if (!valid) return false;
  cbPanel.PerformCallback();
 }
function GetQuote() {
  var valid;
  //if (!valid) return false;
  valid = ASPxClientEdit.ValidateGroup("");
  if (!valid) return false;
  cbPanel2.PerformCallback();
}
function quoteSuccess() {
  ppcAddCart.Hide();
  goto('BallBearingThankYou.html');
}
 function ChangeAddress(OID) {
	ppcAddress.SetContentHtml('');
 	ppcAddress.SetContentUrl('EditAddress.aspx?id=' + OID);
 	ppcAddress.Show();
}
function showPop(url) {
  ppc.SetContentHtml('');
  ppc.SetContentUrl(url);
  ppc.Show();
}
function closePop() {
  ppc.Hide();
 }
 function closeAddressPop() {
   ppcAddCart.Hide();
   cbPanel.PerformCallback();
 }

/* Popups */
function editAddress(id,type) {
  ppcAddCart.SetSize(560, 275);
  ppcAddCart.SetHeaderText('Edit Address');
  ppcAddCart.SetContentHtml('');
  ppcAddCart.SetContentUrl('EditAddress.aspx');
  ppcAddCart.Show();
 }
 function showHomePop() {
  ppcAddCart.SetSize(335, 542);
  ppcAddCart.SetHeaderText('Your request has been submitted');
  ppcAddCart.SetContentHtml('');
  ppcAddCart.SetContentUrl('SearchPop.aspx');
  ppcAddCart.Show();  
 }
 function editShopingCart() {
 	goto('Default.aspx');
 }
function closeEditAddressPop(status) {
  ppcAddCart.Hide();
  if (status) { cbpAccount.PerformCallback() };
}
function printLabel(orderID) {
 	ppcPrint.SetSize(500, 400)
 	ppcPrint.SetHeaderText('Print Shipping Label');
 	ppcPrint.SetContentHtml('');
 	ppcPrint.SetContentUrl('PrintShippingLabel.aspx?id=' + orderID);
 	ppcPrint.Show();
 	return false;
}
function showOrder(id) {
  ppc.SetSize(800, 500);
  ppc.SetHeaderText('Order Details');
  ppc.SetContentHtml('');
  ppc.SetContentUrl('Order.aspx?id=' + id);
  ppc.Show();
}
function sendOrderDetail(orderID) {
	ppcPrint.SetSize(600, 400)
	ppcPrint.SetHeaderText('Send Order detail');
	ppcPrint.SetContentHtml('');
	ppcPrint.SetContentUrl('SendOrderDetail.aspx?id=' + orderID);
	ppcPrint.Show();
	return false;
}
/* Gallary */
function slideShow() {
  $('#gallery a').css({ opacity: 0.0 });
  $('#gallery a:first').css({ opacity: 1.0 });
  $('#gallery .caption').css({ opacity: 1.0 });
  $('#gallery .caption').css({ width: $('#gallery a').find('img').css('width') });
  $('#gallery .content').html($('#gallery a:first').find('img').attr('rel')).animate({ opacity: 1.0 }, 400);
  setInterval('gallery()', 6000);
}
function gallery() {
  var current = ($('#gallery a.show') ? $('#gallery a.show') : $('#gallery a:first'));
  var next = ((current.next().length) ? ((current.next().hasClass('caption')) ? $('#gallery a:first') : current.next()) : $('#gallery a:first'));
  var caption = next.find('img').attr('rel');
  next.css({ opacity: 0.0 }).addClass('show').animate({ opacity: 1.0 }, 1000);
  current.animate({ opacity: 0.0 }, 1000).removeClass('show');
  $('#gallery .caption').animate({ opacity: 0.0 }, { queue: false, duration: 0 }).animate({ height: '1px' }, { queue: true, duration: 300 });
  $('#gallery .caption').animate({ opacity: 1.0 }, 100).animate({ height: '30px' }, 500);
  $('#gallery .content').html(caption);
}
/* Registration Page */
function setShippingAddress() {
  var enabled = chkSameAddress.GetChecked();
  txtSFirstName.SetEnabled(!enabled);
  txtSLastName.SetEnabled(!enabled);
  txtSInitials.SetEnabled(!enabled);
  txtSCompany.SetEnabled(!enabled);
  txtSAddress1.SetEnabled(!enabled);
  txtSAddress2.SetEnabled(!enabled);
  txtSState.SetEnabled(!enabled);
  txtSZip.SetEnabled(!enabled);
  txtSCity.SetEnabled(!enabled);
  cmbSCountry.SetEnabled(!enabled);
  if (enabled) {
    txtSFirstName.SetText(txtBFirstName.GetText());
    txtSLastName.SetText(txtBLastName.GetText());
    txtSInitials.SetText(txtBInitials.GetText());
    txtSCompany.SetText(txtBCompany.GetText());
    txtSAddress1.SetText(txtBAddress1.GetText());
    txtSAddress2.SetText(txtBAddress2.GetText());
    txtSState.SetText(txtBState.GetText());
    txtSZip.SetText(txtBZip.GetText());
    txtSCity.SetText(txtBCity.GetText());
    cmbSCountry.SetValue(cmbBCountry.GetValue());
  }
  else {
    txtSFirstName.SetText('');
    txtSLastName.SetText('');
    txtSInitials.SetText('');
    txtSCompany.SetText('');
    txtSAddress1.SetText('');
    txtSAddress2.SetText('');
    txtSState.SetText('');
    txtSZip.SetText('');
    txtSCity.SetText('');
    cmbSCountry.SetSelectedIndex(-1);
  }
}
function SendSurplus() {
  var valid = Page_IsValid;
  if (!valid) return false;
  valid = ASPxClientEdit.ValidateGroup("");
  if (!valid) return false;
  cbPanel2.PerformCallback();
}
