
function shoppingCartTopFocus(){
  window.location.href="#cart_steps";	
}

//generalized call to the DB using Ajax
function ajax_db_call(p_url, p_data, p_async, p_success_function) {
  $.ajax( {
    type : 'post',
    url : p_url,
    data : p_data,
    cache : false,
    async : p_async,
    success : p_success_function
  });
}

//call to profanity text filter
function profanityFilter() {
  var v_filter_text = '';
  var v_data = '';
  var v_async = false;
  //reset return variable
  personalization_return = true;

  for ( var i = 0; i < personalization_text_count; i++) {
    v_filter_text = document.getElementById(personalization_fields[i]).value;
    if (v_filter_text != '') {
      v_data = 'personalization_text=' + v_filter_text;
      ajax_db_call("/query/profanityCheck", v_data, v_async,
      filter_success);
     }

  }

  return personalization_return;

}

//success handling for profanity text filter
function filter_success(p_count) {
  if (p_count != 0) {
    alert('The personalized text you have entered contains language that is not permitted on personalized items.');
    personalization_return = false;
  } else {
     //not sure if any browsers will require a submit here...
  }
}


// Redesign Ajax Functions

function locateStore(p_screen_name, p_line_number) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/findISPStore/';
  } else {
    v_url = '/checkout/findISPStore/';
  }

  v_zipcode_text = $("#isp_zipcode").val();
  v_city = $("#isp_city").val();
  v_state = $("#isp_state").val();
  v_inventory_item_id = $("#inventory_item_id_" + p_line_number).val();
  v_quantity = $("#qty_" + p_line_number).val();

  $.ajax( {
    type : 'post',
    url : v_url,
    data : 'store_postal_code=' + v_zipcode_text + '&isp_city=' + v_city + '&isp_state=' + v_state 
                                + '&line_number=' + p_line_number 
                                + '&inventory_item_id=' + v_inventory_item_id
                                + '&quantity=' + v_quantity,
    cache : false,
    async : false,
    beforesend : function() {
	
    },
    success : function(html) {
    $("#isp_stores_" + p_line_number).empty();
    $("#isp_stores_" + p_line_number).append(html);
    }
  });
  return false;

}

function changeISPWarehouse(p_screen_name, p_line_number, p_organization_id, p_line_type) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/changeISPWarehouse/';
  } else {
    v_url = '/checkout/changeISPWarehouse/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    data : 'line_number=' + p_line_number + '&organization_id=' + p_organization_id + '&line_type=' + p_line_type,
    cache : false,
    async : false,
    beforesend : function() {

    },
    success : function(v_html) {
      $("#cart_items").empty();
      $("#cart_items").append(v_html);      
      loadCartTotals(p_screen_name);
      loadCartCheckoutBottom(p_screen_name);
      refreshCartItems(p_screen_name);
    }
  });
  return false;

}

function validateOrderQtys(p_screen_name) {
	
	if(p_screen_name != '') {
	    v_url = '/' + p_screen_name + '/checkout/validateOrderQtys/';
	  } else {
	    v_url = '/checkout/validateOrderQtys/';
	  }

	  $.ajax( {
	    type : 'post',
	    url : v_url,
	    cache : false,
	    async : false,
	    beforesend : function() {

	    },
	    success : function(v_html) {
	    	$("#mcafee_seal_top").remove();
	    	if (v_html.length > 0) {
	    		$("#popUp_box").empty();
		        $("#popUp_box").append('<div id="popUp_border"><div id="popUp_close"><a class="pointer" href="#" onclick="hidePopUpBox(); return false;" title="Close"><img src="/_site_images/_shopping_cart/popup_close.gif" alt="Close" /></a></div><p>' + v_html + '</p></div>');
		        $("#popUp_box").show();
	    	} else {
	    		$("#popUp_box").hide();
	    		fadeinout_cart(p_screen_name);
	    	}
	    }
	  });
	
	  return false;
}


function emptyCart(p_screen_name) {

  var blnConfirm = confirm('Delete all items in your shopping cart?');

  if (blnConfirm)
  {
	
	  if(p_screen_name != '') {
	    v_url = '/' + p_screen_name + '/checkout/emptyCart/';
	  } else {
	    v_url = '/checkout/emptyCart/';
	  }
	
	  $.ajax( {
	    type : 'post',
	    url : v_url,
	    cache : false,
	    async : false,
	    beforesend : function() {
	
	    },
	    success : function(v_html) {
	      $("#cart_items").empty();
	      $("#cart_items").append(v_html);
	      loadCartTotals(p_screen_name);
	      loadCartCheckoutBottom(p_screen_name);
	    }
	  });
  }
  return false;

}

function deleteItem(p_screen_name, p_line_reference, p_count, p_sku_desc) {

  var blnConfirm;
  
  if (p_count == null){
	 p_count    = 1;
	 p_sku_desc = '';
  }

  if (p_count <= 1) {
	  blnConfirm = confirm('Delete this item in your shopping cart?');
  } else {
	  blnConfirm = confirm('The custom club you want to delete is in a set and cannot be deleted individually. Would you like to delete all the ' + p_sku_desc + ' clubs from your cart?');
  }

  if (blnConfirm)
  {	
	  if(p_screen_name != '') {
	    v_url = '/' + p_screen_name + '/checkout/update/';
	  } else {
	    v_url = '/checkout/update/';
	  }
	
	  $.ajax( {
	    type : 'post',
	    url : v_url,
	    data : 'line=' + p_line_reference + '&quantity=0',
	    cache : false,
	    async : true,
	    beforesend : function() {
	
	    },
	    success : function(html) {
	      $("#cart_items").empty();
	      $("#cart_items").append(html);
	      loadCartTotals(p_screen_name);
	      loadCartCheckoutBottom(p_screen_name);
	    }
	  });
  }
  return false;

}

function updateItem(p_screen_name, p_line_number, p_line_reference) {

  v_quantity = document.getElementById('qty_' + p_line_number).value;
  v_orig_quantity = document.getElementById('orig_qty_' + p_line_number).value;
  v_atp_quantity = document.getElementById('atp_qty_' + p_line_number).value;
  v_sku_disp = document.getElementById('sku_disp_' + p_line_number).value;
  
  v_err_msg = 'The amount you\'ve entered for ' + v_sku_disp + ' is more than we have in stock. Please try entering a lesser quantity. You may also contact Golfsmith Customer Service by calling 1-800-813-6897.';

  if (parseInt(v_quantity) > parseInt(v_atp_quantity))
  {
	  document.getElementById('qty_' + p_line_number).value = parseInt(v_orig_quantity);
	  $("#popUp_box").empty();
      $("#popUp_box").append('<div id="popUp_border"><div id="popUp_close"><a class="pointer" href="#" onclick="hidePopUpBox(); return false;" title="Close"><img src="/_site_images/_shopping_cart/popup_close.gif" alt="Close" /></a></div><p>' + v_err_msg + '</p></div>');
      $("#popUp_box").show();
      
	  return false;
  }
  
  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/update/';
  } else {
    v_url = '/checkout/update/';
  }

  if(v_quantity != '') {

    $.ajax( {
      type : 'post',
      url : v_url,
      data : 'line=' + p_line_reference + '&quantity=' + v_quantity,
      cache : false,
      async : true,
      beforesend : function() {
      },
      success : function(html) {
        $("#cart_items").empty();
        $("#cart_items").append(html);
        loadCartTotals(p_screen_name);
        loadCartCheckoutBottom(p_screen_name);
      }
    });
  }

  return false;

}


function addPureing(p_screen_name, p_style, p_line_number) {

	  v_quantity = document.getElementById('qty_' + p_line_number).value;


	  if(p_screen_name != '') {
	    v_url = '/' + p_screen_name + '/checkout/addPureing/';
	  } else {
	    v_url = '/checkout/addPureing/';
	  }

	  if(v_quantity != '') {

	    $.ajax( {
	      type : 'post',
	      url : v_url,
	      data : 'line=' + p_line_number + '&quantity=' + v_quantity + '&stynum=' + p_style,
	      cache : false,
	      async : true,
	      beforesend : function() {
	      },
	      success : function(html) {
	        $("#cart_items").empty();
	        $("#cart_items").append(html);
	        loadCartTotals(p_screen_name);
	        loadCartCheckoutBottom(p_screen_name);
	      }
	    });
	  }

	  return false;

	}


function applyCartPromo(p_screen_name) {
  
	if(p_screen_name != '') {
	    v_url = '/' + p_screen_name + '/checkout/applyCartPromoCode/';
	  } else {
	    v_url = '/checkout/applyCartPromoCode/';
	  }
	
	var v_source_code = $("#promoCode_input").val();


	  $.ajax( {
	      type : 'post',
	      url : v_url,
	      data : 'source_code=' + v_source_code,
	      cache : false,
	      async : false,
	      beforesend : function() {
	      },
	      success : function(v_html) {

	    	  if (v_html == 'valid')
	    	  {
	    		  refreshCartItems(p_screen_name);
	    		  loadCartTotals(p_screen_name);
	    	  } else {
	    		  loadCartTotals(p_screen_name);
	    		  $("#promoCode_input").val('');
	    	  }
	    	  
	      }
	    });
}

function checkEnterApplyCartPromo(event, p_screen_name) {

	  if(event.keyCode == 13 || event.which == 13) {
		  applyCartPromo(p_screen_name);
	  } else {
	    return false;
	  }
}

function removeCartPromo(p_screen_name) {
	  
	if(p_screen_name != '') {
	    v_url = '/' + p_screen_name + '/checkout/applyCartPromoCode/';
	  } else {
	    v_url = '/checkout/applyCartPromoCode/';
	  }
	
	var v_source_code = "DEFWEB";
	$("#promoCode_input").val('');
  	 
	$.ajax( {
	      type : 'post',
	      url : v_url,
	      data : 'source_code=' + v_source_code,
	      cache : false,
	      async : false,
	      beforesend : function() {
	      },
	      success : function(v_html) {
	    	  if (v_html == 'valid')
	    	  {
	    		  refreshCartItems(p_screen_name);
	    		  loadCartTotals(p_screen_name);
	    	  } else {
	    		  loadCartTotals(p_screen_name);
	    		  $("#promoCode_input").val('');
	    	  }
	    	  
	      }
	    });
}

function refreshCartItems(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/cartItems/';
  } else {
    v_url = '/checkout/cartItems/';
  }

  $.ajax( {
      type : 'post',
      url : v_url,
      cache : false,
      async : false,
      beforesend : function() {
      },
      success : function(v_html) {
        $("#cart_items").empty();
        $("#cart_items").append(v_html);
      }
    });
}

function displayAddressBookForm(p_screen_name, p_address_id, p_contact_id, p_phone_id, p_address_type, p_containing_page) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/displayAddressBookForm/';
  } else {
    v_url = '/checkout/displayAddressBookForm/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    data : 'address_id=' + p_address_id + '&contact_id=' + p_contact_id + '&phone_id=' + p_phone_id + '&address_type=' + p_address_type + '&containing_page=' + p_containing_page,
    cache : false,
    async : true,
    beforesend : function() {

    },
    success : function(v_html) {
      $("#popUp_box").empty();
      $("#popUp_box").append(v_html);
      $("#popUp_box").addClass('address_change');
      $("#popUp_box").show();
   }
  });

}

function setOrderAddress(p_screen_name, p_address_id, p_contact_id, p_phone_id, p_address_type, p_containing_page) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/setSessionAddressInfo/';
  } else {
    v_url = '/checkout/setSessionAddressInfo/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    data : 'address_id=' + p_address_id + '&contact_id=' + p_contact_id + '&phone_id=' + p_phone_id + '&address_type=' + p_address_type + '&containing_page=' + p_containing_page,
    cache : false,
    async : true,
    beforesend : function() {

    },
    success : function(v_html) {
      //but we need to load a different piece for the review page
      //and possibly need to load *two* different pieces for the review page
      //address book needs to know the referring page
      if(p_containing_page == 'payment') {
        $("#popUp_box").hide();
        $("#cart_items").html(v_html);
        updatePaymentShipping(p_screen_name);
      } else {
        $("#popUp_box").hide();

        //reload all three sections to adjust GC total amounts
        refreshReviewShippingAndPayment(p_screen_name);
        refreshReviewCharges(p_screen_name);
        refreshReviewShipSection(p_screen_name);
      }
    }

  });

}


function refreshPayment(p_screen_name) {
  
  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/paymentContent/';
  } else {
    v_url = '/checkout/paymentContent/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    cache : false,
    async : true,
    beforesend : function() {

    },
    success : function(v_html) {
      $("#cart_items").html(v_html);     
    }

  });

}


function refreshPaymentCharges(p_screen_name) {

	  if(p_screen_name != '') {
	    v_url = '/' + p_screen_name + '/checkout/displayPaymentCharges/';
	  } else {
	    v_url = '/checkout/displayPaymentCharges/';
	  }

	  $.ajax( {
	    type : 'post',
	    url : v_url,
	    cache : false,
	    async : false,
	    beforesend : function() {

	    },
	    success : function(v_html) {
	  	  $("#payment_summary").html(v_html);
	    }

	  });

	}


function refreshPaymentShippingCharges(p_screen_name) {

	  if(p_screen_name != '') {
	    v_url = '/' + p_screen_name + '/checkout/displayPaymentShippingCharges/';
	  } else {
	    v_url = '/checkout/displayPaymentShippingCharges/';
	  }

	  $.ajax( {
	    type : 'post',
	    url : v_url,
	    cache : false,
	    async : false,
	    beforesend : function() {

	    },
	    success : function(v_html) {
	  	  $("#shipping_method").html(v_html);
	    }

	  });

	}
function refreshReviewCharges(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/displayReviewCharges/';
  } else {
    v_url = '/checkout/displayReviewCharges/';
  }

  var v_ship_method = $("#method_select").val();

  $.ajax( {
    type : 'post',
    url : v_url,
    data : 'method_select=' + v_ship_method,    
    cache : false,
    async : true,
    beforesend : function() {

    },
    success : function(v_html) {      
      //$("#cart_charges").html(v_html);
  	  $("#footer_summary").html(v_html);
    }

  });

}

function refreshReviewShipSection(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/displayReviewShipItemsOnly/';
  } else {
    v_url = '/checkout/displayReviewShipItemsOnly/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    cache : false,
    async : true,
    beforesend : function() {

    },
    success : function(v_html) {
      $("#cart_items").html(v_html);
    }

  });

}

function refreshReviewShippingAndPayment(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/displayReviewShipPayment/';
  } else {
    v_url = '/checkout/displayReviewShipPayment/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    cache : false,
    async : false,
    beforesend : function() {

    },
    success : function(v_html) {
      $("#footer_left").html(v_html);
    }

  });

}


function displayAddressBook(p_screen_name, p_address_type, p_containing_page, p_require_canadian_address) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/displayAddressBook/';
  } else {
    v_url = '/checkout/displayAddressBook/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    data : 'address_type=' + p_address_type + '&containing_page=' + p_containing_page + '&require_canadian_address=' + p_require_canadian_address,
    cache : false,
    async : true,
    beforesend : function() {

    },
    success : function(v_html) {
      $("#popUp_box").empty();
      $("#popUp_box").append(v_html);
      $('li:odd').addClass('end_listItem');
      $("#popUp_box").show();
      $("#popUp_border").addClass('address_book_scroll');   
      
    }
  });

}

function setAddressAsPrimary(p_screen_name, p_address_id, p_address_type, p_containing_page) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/setAddressAsPrimary/';
  } else {
    v_url = '/checkout/setAddressAsPrimary/';
  }
  
  $.ajax( {
    type : 'post',
    url : v_url,
    data : 'address_id=' + p_address_id + '&address_type=' + p_address_type + '&containing_page=' + p_containing_page,
    cache : false,
    async : true,
    beforesend : function() {

    },
    success : function(v_html) {
      $("#popUp_box").empty();
      $("#popUp_box").append(v_html);
      $("#popUp_box").show();
      $('li:odd').addClass('end_listItem');
      $("#popUp_border").addClass('address_book_scroll');
  
    }
  });

}

function deleteAddress(p_screen_name, p_address_id, p_address_type, p_containing_page) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/deleteAddress/';
  } else {
    v_url = '/checkout/deleteAddress/';
  }
  
  $.ajax( {
    type : 'post',
    url : v_url,
    data : 'address_id=' + p_address_id + '&address_type=' + p_address_type + '&containing_page=' + p_containing_page,
    cache : false,
    async : true,
    beforesend : function() {

    },
    success : function(v_html) {
      $("#popUp_box").empty();
      $("#popUp_box").append(v_html);
      $("#popUp_box").show();
      $('li:odd').addClass('end_listItem');
      $("#popUp_border").addClass('address_book_scroll');
  }
  });


}

function loadCartTotals(p_screen_name) {

  var v_ship_method = $("#method_select").val();
  var v_postal_code = $("#postal_code").val();
  var v_source_code = $("#promoCode_input").val();

  if(v_ship_method == '' || typeof(v_ship_method) == 'undefined') {
    v_ship_method = 'G';
  }

  if(!v_source_code) {
    v_source_code = '';
  }

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/cartFooter/';
  } else {
    v_url = '/checkout/cartFooter/';
  }

  $.ajax( {
      type : 'post',
      url : v_url,
      data : 'method_select=' + v_ship_method + '&postal_code=' + v_postal_code + '&source_code=' + v_source_code,
      cache : false,
      async : false,
      beforesend : function() {
      },
      success : function(v_html) {
        $("#cart_footer").empty();
        $("#cart_footer").append(v_html);
      }
    });

  return false;

}

function loadCartCheckoutBottom(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/cartCheckout/';
  } else {
    v_url = '/checkout/cartCheckout/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    cache : false,
    async : true,
    beforesend : function() {
    },
    success : function(html) {
      $("#cart_checkoutOptions").empty();
      $("#cart_checkoutOptions").append(html);
    }
  });

  return false;


}

function loadIspSelector(p_screen_name, p_line_number) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/getISPForm/';
  } else {
    v_url = '/checkout/getISPForm/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    data : 'line_number=' + p_line_number,
    cache : false,
    async : false,
    beforesend : function() {

    },
    success : function(html) {
      $("#isp_locator_" + p_line_number).empty();
      $("#isp_locator_" + p_line_number).append(html);
    }
  });

}

function checkLogin(p_screen_name) {

  v_user_name = $("#account_email_input").val();
  v_password = $("#account_password_input").val();

  var v_valid_fields = checkLoginFields(v_user_name, v_password);

  if(v_valid_fields == true) {

    if(p_screen_name != '') {
      v_url = '/' + p_screen_name + '/checkout/loginCheck/';
    } else {
      v_url = '/checkout/loginCheck/';
    }

    $.ajax( {
      type : 'post',
      url : v_url,
      data : 'user_name=' + v_user_name + '&password=' + v_password,
      cache : false,
      async : true,
      beforesend : function() {
      },
      success : function(message) {
        if(message == '') {
          //load the next page
          cmCreatePageviewTag("PS VALIDATE LOGON","CART","","");
          toggle_login_header(p_screen_name, true);
          fadeinout_login(p_screen_name);
        } else {
          //don't load the next page; just show the error message
          $("#account_validation_message").html('<p>' + message + '</p>');
          $("#account_validation_message").show();
          $("#account_password_input").val('');
        }
      }
    });
  }

  return false;

}

function checkLoginFields(p_email, p_password) {
  var v_validation = "";

  $("#email_error").empty();
  $("#password_error").empty();

  if(p_email == "") {
    $("#email_error").text(' Required');
    $("#account_email").addClass('validate');
    v_validation += "Error";
  } else if(p_email != "" && !isValidEmail(p_email)) {
    $("#email_error").text(' Invalid');
    $("#account_email").addClass('validate');
    v_validation += "Error";
  } else {
    $("#account_email").removeClass('validate');
  }

  if (p_password == "") {
    $("#password_error").text(' Required');
    $("#account_password").addClass('validate');
    v_validation += "Error";
  } else {
    $("#account_password").removeClass('validate');
  }

  if (v_validation != "") {
    $("#account_validation_message").html('<p>Please fix the errors below and try again.</p>');
    $("#account_validation_message").show();
    return false;
  } else {
    $("#account_validation_message").hide();
    return true;
  }
}

function isValidEmail(p_email) {
  if (p_email.indexOf("@") > 0) {
    var v_check = p_email.split("@");
    v_domain = v_check[1].split(".");
    if (v_domain.length > 1) {
      if (v_domain[v_domain.length-1].length > 1 && v_domain[v_domain.length-2].length >= 1) {
        return true;
      }
    }
  }
  return false;
}

//p_containing_page will be either 'payment' or 'review'
function checkAddressBookAddress(p_screen_name, p_address_type, p_containing_page) {

  if(p_address_type == 'BILL_TO') {
    v_address_type = 'bill';
  } else {
    v_address_type = 'ship';
  }

  var v_is_valid = validateAddressBookEntry();
  
  if(v_is_valid == true) {
	 
    var v_address_id = $("#address_id").val();
    var v_contact_id = $("#contact_id").val();
    var v_phone_id = $("#phone_id").val();
    var v_fname = $("#fname_ship_input").val();
    var v_lname = $("#lname_ship_input").val();
    var v_addr1 = $("#add1_ship_input").val();
    var v_addr2 = $("#add2_ship_input").val();
    var v_city = $("#city_ship_input").val();
    var v_pcode = $("#pcode_ship_input").val();
    var v_country = $("#country_ship_select").val();
    var v_phone  = $("#phone_ship_input").val();
    var v_state;
    
    if (v_country=="US"){
      v_state = $("#state_ship_select").val();
    }else if(v_country=="CA"){
    	v_state = $("#state_ship_ca_select").val();
    }else{
    	v_state =$("#state_ship_input").val();
    }
     
  
    /*
    if(v_state == '' || typeof(v_state) == 'undefined') {
      v_state = $("#state_ship_input").val();
    }
  */

  if(p_address_type=='BILL_TO'){
    var v_run_fedex_validation ='N';
  }else{
	 var v_run_fedex_validation = $("#run_fedex_validation").val();
  }
    
   var v_data = 'address_id=' + v_address_id + '&contact_id=' + v_contact_id + '&phone_id=' + v_phone_id + '&fname_ship_input=' + v_fname + '&lname_ship_input=' + v_lname + '&add1_ship_input=' + v_addr1 + '&add2_ship_input=' + v_addr2 + '&city_ship_input=' + v_city + '&state_ship_input=' + v_state + '&pcode_ship_input=' + v_pcode + '&country_ship_select=' + v_country + '&phone_ship_input=' + v_phone + '&address_type=' + p_address_type + '&referring_page=address_book' + '&containing_page=' + p_containing_page;

    if(v_run_fedex_validation == 'Y') {

      if(p_screen_name != '') {
        v_url = '/' + p_screen_name + '/checkout/fedexCheck/';
      } else {
        v_url = '/checkout/fedexCheck/';
      }

      $.ajax({
        type : 'post',
        url : v_url,
        data : v_data,
        cache : false,
        async : true,
        beforesend : function() {
        },
        success : function(v_html) {
          if(v_html == '') {
            updateAddressBookAddress(p_screen_name, v_data, p_containing_page);
          } else {
            //don't load the next page; just show the error message
            $("#popUp_box").html(v_html);
            $("#v_scr_name").val(p_containing_page);
            $("#popUp_box").show();
          }
        }
      });

    } else {
      updateAddressBookAddress(p_screen_name, v_data, p_containing_page);
    }

  }

}

function newAddressBookAddress(p_site) {
  $("#address_id").val('');
  $("#contact_id").val('');
  $("#phone_id").val('');
  $("#fname_ship_input").val('');
  $("#lname_ship_input").val('');
  $("#add1_ship_input").val('');
  $("#add2_ship_input").val('');
  $("#city_ship_input").val('');
  $("#state_ship_select").val('');
  $("#pcode_ship_input").val('');
  
  if(p_site == 'US') {
    $("#country_ship_select").val('US');
    hide_show_ca_select();
  } else {
    $("#country_ship_select").val('CA');
    hide_show_ca_select();
  }
   $("#phone_ship_input").val('');

}

function checkGuestCheckoutAddresses(p_screen_name) {

  var v_is_valid = validateAddressEntry();

  if(v_is_valid == true) {

    var v_ship_fname = $("#fname_ship_input").val();
    var v_ship_lname = $("#lname_ship_input").val();
    var v_ship_addr1 = $("#add1_ship_input").val();
    var v_ship_addr2 = $("#add2_ship_input").val();
    var v_ship_city = $("#city_ship_input").val();
    var v_ship_pcode = $("#pcode_ship_input").val();
    var v_ship_country = $("#country_ship_select").val();
    var v_ship_phone = $("#phone_ship_input").val();
    var v_ship_state;
    
    if('CA'==$("#country_ship_select").val()){
    	v_ship_state = $("#state_ship_ca_select").val();
    }else if('US'==$("#country_ship_select").val()){
    	v_ship_state = $("#state_ship_select").val();	
    }else{
    	v_ship_state = $("#state_ship_input").val();
    }

    var v_bill_fname = $("#fname_bill_input").val();
    var v_bill_lname = $("#lname_bill_input").val();
    var v_bill_addr1 = $("#add1_bill_input").val();
    var v_bill_addr2 = $("#add2_bill_input").val();
    var v_bill_city = $("#city_bill_input").val();
    var v_bill_pcode = $("#pcode_bill_input").val();
    var v_bill_country = $("#country_bill_select").val();
    var v_bill_phone = $("#phone_bill_input").val();
    var v_bill_state;
    if('CA'==$("#country_bill_select").val()){
    	v_bill_state = $("#state_bill_ca_select").val();
    }else if('US'==$("#country_bill_select").val()){
    	v_bill_state = $("#state_bill_select").val();	
    }else{
    	v_bill_state = $("#state_bill_input").val();
    }
    
    var v_email = $("#email_address_input").val();
    var v_rewards = $("#rewards_player_input").val();
    var v_email_subscribe = $("#rewards_emails_input").val();

    var v_data = 'fname_ship_input=' + v_ship_fname + '&lname_ship_input=' + v_ship_lname + '&add1_ship_input=' + v_ship_addr1 + '&add2_ship_input=' + v_ship_addr2 + '&city_ship_input=' + v_ship_city + '&state_ship_input=' + v_ship_state + '&pcode_ship_input=' + v_ship_pcode + '&country_ship_select=' + v_ship_country + '&phone_ship_input=' + v_ship_phone + '&fname_bill_input=' + v_bill_fname + '&lname_bill_input=' + v_bill_lname + '&add1_bill_input=' + v_bill_addr1 + '&add2_bill_input=' + v_bill_addr2 + '&city_bill_input=' + v_bill_city + '&state_bill_input=' + v_bill_state + '&pcode_bill_input=' + v_bill_pcode + '&country_bill_select=' + v_bill_country + '&phone_bill_input=' + v_bill_phone + '&email_address_input=' + v_email + '&rewards_player_input=' + v_rewards + '&rewards_emails_input=' + v_email_subscribe;

    var v_run_fedex_validation = $("#run_fedex_validation").val();
    $("#popUp_box").addClass('address_change');
    if(v_run_fedex_validation == 'Y') {

      if(p_screen_name != '') {
        v_url = '/' + p_screen_name + '/checkout/fedexCheck/';
      } else {
        v_url = '/checkout/fedexCheck/';
      }

      $.ajax({
        type : 'post',
        url : v_url,
        data : v_data,
        cache : false,
        async : true,
        beforesend : function() {
        },
        success : function(html) {
          if(html == '') {
            registerCustomer(p_screen_name, v_data);
          } else {
            //don't load the next page; just show the error message
            $("#popUp_box").html(html);
            $("#popUp_box").show();
            
          }
        }
      });

    } else {
      registerCustomer(p_screen_name, v_data);
    }

  }

  return false;

}

function runDefaultFedExCheck(p_screen_name, p_address_id, p_contact_id, p_phone_id, p_first_name, p_last_name, p_line1, p_line2, p_city, p_state, p_postal_code, p_country, p_phone) {
	//$("#popUp_box").removeClass('address_change');
  var v_run_fedex_validation = $("#run_fedex_check").val();

  if(v_run_fedex_validation == 'Y') {

    if(p_screen_name != '') {
      v_url = '/' + p_screen_name + '/checkout/fedexCheck/';
    } else {
      v_url = '/checkout/fedexCheck/';
    }

    var v_data = 'address_id=' + p_address_id + '&contact_id=' + p_contact_id + '&phone_id=' + p_phone_id + '&address_type=SHIP_TO&fname_ship_input=' + p_first_name + '&lname_ship_input=' + p_last_name + '&add1_ship_input=' + p_line1 + '&add2_ship_input=' + p_line2 + '&city_ship_input=' + p_city + '&state_ship_input=' + p_state + '&pcode_ship_input=' + p_postal_code + '&country_ship_select=' + p_country + '&phone_ship_input=' + p_phone + '&referring_page=address_book&containing_page=payment';

    $.ajax({
      type : 'post',
      url : v_url,
      data : v_data,
      cache : false,
      async : true,
      beforesend : function() {
      },
      success : function(v_html) {
        if(v_html != '') {
          //don't load the next page; just show the error message
          $("#popUp_box").html(v_html);
          $("#popUp_box").show();
        }
        $("#run_fedex_check").val('N');
      }
    });

  }

}

function registerCustomer(p_screen_name, p_data) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/verifyCustomer/';
    v_screen_name = '/' + p_screen_name;
  } else {
    v_url = '/checkout/verifyCustomer/';
    v_screen_name = '';
  }

  $.ajax({
    type : 'post',
    url : v_url,
    data : p_data,
    cache : false,
    async : true,
     beforesend : function() {
    },
    success : function(v_message) {
      if(v_message == '') {
       //load next page here
       fadeinout_generic(v_screen_name + '/checkout/paymentcontent'); 
      } else {
        //need an error message for this
        //for now, just separate return status messages by line breaks
        $("#popUp_box").empty();
        $("#popUp_box").append('<div id="popUp_border"><div id="popUp_close"><a class="pointer" href="#" onclick="hidePopUpBox(); return false;" title="Close"><img src="/_site_images/_shopping_cart/popup_close.gif" alt="Close" /></a></div><p>' + v_message + '</p></div>');
        $("#popUp_box").show();
      }
    }
  });

  return false;

}

function getPreviousGiftCards(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/getGiftCards/';
  } else {
    v_url = '/checkout/getGiftCards/';
  }

  $.ajax({
    type : 'post',
    url : v_url,
    cache : false,
    async : true,
     beforesend : function() {
    },
    success : function(v_html) {
      $("#giftCard_applied").html(v_html)
    }
  });

  return false;

}

function addGiftCardPayment(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/addGiftCardPayment/';
    v_screen_name = '/' + p_screen_name;
  } else {
    v_url = '/checkout/addGiftCardPayment/';
    v_screen_name = '';
  }

  v_message = validateGiftCard();

  if(v_message == '') {

    v_gc_num = $("#giftCard_number_input").val();
    v_gc_pin = $("#giftCard_pin_input").val();

    $.ajax({
      type : 'post',
      url : v_url,
      data : 'giftCard_number_input=' + v_gc_num + '&giftCard_pin_input=' + v_gc_pin,
      cache : false,
      async : true,
       beforesend : function() {
      },
      success : function(v_message) {
        if(v_message.substr(0, 5) != 'Error') {
        	$("#giftCard_number").removeClass('validate');
            $("#giftCard_pin").removeClass('validate');
          //load page here (pmt page if full order amount isn't covered, review page otherwise)
          //how do we know which page to load?
//          if(v_message == 'review') {
            //transition to review goes here
//            fadeinout_generic(v_screen_name + '/checkout/reviewcontent');
//          } else {
            //fadeinout_generic(v_screen_name + '/checkout/paymentcontent', 'payment');
            //just reload payment piece - still requires an Ajax call
            $("#giftCard_number_input").val('');
            $("#giftCard_pin_input").val('');
            getPreviousGiftCards(p_screen_name);
            refreshPayment(p_screen_name);
//          }
        } else {
          //need an error message for this
          //for now, just separate return status messages by line breaks
          $("#giftCard_number").addClass('validate');
          $("#giftCard_pin").addClass('validate');
          $("#giftCard_validation_message").html('<p>' + v_message.substr(7) + '</p>');
          $("#giftCard_validation_message").show();
        }
      }
    });
  } else {
	$("#giftCard_number").addClass('validate');
    $("#giftCard_pin").addClass('validate');
    $("#giftCard_validation_message").html('<p>' + v_message + '</p>');
    $("#giftCard_validation_message").show();
    $("#giftCard_number_input").val('');
    $("#giftCard_pin_input").val('');
  }

  return false;
}

function removeGiftCardPayment(p_screen_name, p_gc_num) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/removeGiftCardPayment/';
  } else {
    v_url = '/checkout/removeGiftCardPayment/';
  }

  $.ajax({
    type : 'post',
    url : v_url,
    data : 'giftCard_number=' + p_gc_num,
    cache : false,
    async : true,
     beforesend : function() {
    },
    success : function() {
      getPreviousGiftCards(p_screen_name);
      refreshPayment(p_screen_name);
    }
    });

}

function applyPromoCode(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/applyPromoCode/';
  } else {
    v_url = '/checkout/applyPromoCode/';
  }

  v_cashcard_num = $("#cashCard_number_input").val();

  $.ajax({
    type : 'post',
    url : v_url,
    data : 'cashCard_number=' + v_cashcard_num,
    cache : false,
    async : false,
     beforesend : function() {
    },
    success : function(v_message) {
      if ((v_message.length > 0) && (v_message.substring(0,4)!="Your")) {    	 
    	  $("#cashCard_validation_message").html('<p><br />' + v_message +'</p>');
    	  $("#cashCard_number").addClass('validate');
    	  $("#cashCard_number_input").val('');
    	  $("#cashCard_accepted").html('');
      }
      if(v_message.substring(0,4)=="Your"){
          refreshPaymentCharges(p_screen_name);
          refreshPaymentShippingCharges(p_screen_name);
          $("#cashCard_validation_message").html('');
    	  $("#cashCard_number").removeClass('validate');    	  
    	  $("#cashCard_accepted").html('<strong>Your promotion code has been accepted.</strong>');
      }
    }
    });

}

function updatePaymentShipping(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/updatePaymentShipping/';
  } else {
    v_url = '/checkout/updatePaymentShipping/';
  }

  v_ship_method = $("#method_select").val();

  $.ajax({
    type : 'post',
    url : v_url,
    data : 'method_select=' + v_ship_method,
    cache : false,
    async : true,
     beforesend : function() {
    },
    success : function(v_total) {
      //reload the payment page to show adjusted gift card amounts
      //refreshPayment(p_screen_name);
      //load the updated total
      refreshPaymentCharges(p_screen_name);	
      $("td#estTotalTop").html('<h6><strong>' + v_total + '</strong></h6>');
      
    }
    });

}

function updateReviewShipping(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/updateReviewShipping/';
  } else {
    v_url = '/checkout/updateReviewShipping/';
  }

  v_ship_method = $("#method_select").val();

  $.ajax({
    type : 'post',
    url : v_url,
    data : 'method_select=' + v_ship_method,
    cache : false,
    async : true,
     beforesend : function() {
    },
    success : function() {
      //if (v_ship_method != 'I') {
    	//refreshReviewShippingAndPayment(p_screen_name);
    	//refreshReviewCharges(p_screen_name);
        //} else {
    	refreshLineItemsReview(p_screen_name);
        //}
    }
    });
}

function refreshLineItemsReview(p_screen_name) {

	  var v_url = '';

	  if(p_screen_name != '') {
	    v_url = '/' + p_screen_name + '/checkout/reviewContent/';
	  } else {
	    v_url = '/checkout/reviewContent/';
	  }

	  v_ship_method = $("#method_select").val();

	  //we need to know if the order is all ISP
	  v_is_isp_only = isISPOnly(p_screen_name);

	  
	  	
	    $.ajax({
	      type : 'post',
	      url : v_url,
	      data : 'method_select=' + v_ship_method,
	      cache : false,
	      async : true,
	       beforesend : function() {
	    //	$("#preloader").show();
	      },
	      success : function(v_html) {
	        $("#shopping_cart").html(v_html);
	        $("#shopping_cart").fadeIn(100,function(){
	        $("#shopping_cart").addClass('isp');
	       	//$("#preloader").hide();
	        });
	        //$("#preloader").hide();
	        if(v_is_isp_only == true) {
	          //we'll re-show these if we need them
	          $("#cart_top").hide();
	          $("#footer_left").addClass('isp_only');
	          $("#cart_charges").hide();
	        }
	        
	      }
	  //$("#preloader").hide();
	    
	  });
	return false;
	}


function getDefermentOptions(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/getDefermentOptions/';
  } else {
    v_url = '/checkout/getDefermentOptions/';
  }

  $.ajax({
    type : 'post',
    url : v_url,
    cache : false,
    async : true,
     beforesend : function() {
    },
    success : function(v_options_section) {
      $("#payment_terms").html(v_options_section);
      $("#cc_expiration").hide();
      $("#cc_MM_input").val('');
      $("#cc_YY_input").val('');
      $("#cc_cvv_input").val('');
      $("#cc_cvv_help").hide();
      $("#cc_cvv").hide();
      $("#payment_save").html('<input type="hidden" name="save_cc_input" id="save_cc_input" value="N" />');
    }
    });

}

function addCreditCardPayment(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/addCreditCardPayment/';
  } else {
    v_url = '/checkout/addCreditCardPayment/';
  }

  v_payment_method = $("#ccType").val();
  v_cc_num = $("#cc_number_input").val();
  v_expiry_month = $("#cc_MM_input").val();
  v_expiry_year = $("#cc_YY_input").val();
  v_deferment_option = $("#terms_select").val();

  $.ajax({
    type : 'post',
    url : v_url,
    data : 'ccType=' + v_payment_method + '&cc_number_input=' + v_cc_num + '&cc_MM_input=' + v_expiry_month + '&cc_YY_input=' + v_expiry_year + '&terms_select=' + v_deferment_option,
    cache : false,
    async : true,
     beforesend : function() {
    },
    success : function(v_message) {
      if(v_message == '') { //load the review page
        alert('success!');
      } else {
        alert(v_message);
      }
    }
    });

}

function checkPayments(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/checkPayments/';
  } else {
    v_url = '/checkout/checkPayments/';
  }
  
  v_payment_method_hidden = $("#payment_method_input").val();
  v_promo_code = $("#cashCard_number_input").val();
  v_gc_num = $("#giftCard_number_input").val();
  v_gc_pin = $("#giftCard_pin_input").val();
  v_require_wire_transfer = $("#require_wire_transfer").val();
  v_payment_method = $("#ccType").val();
  v_cc_num = $("#cc_number_input").val();
  v_expiry_month = $("#cc_MM_input").val();
  v_expiry_year = $("#cc_YY_input").val();
  v_deferment_option = $("#terms_select").val();
  v_save_cc_info = $("#save_cc_input").val();
  v_cc_cvv=$("#cc_cvv_input").val();
  v_payment_method2 = document.getElementsByName('ccType');
  var v_card_type = 'undefined';
  for (var i=0; i < v_payment_method2.length; i++)
  {
  if (v_payment_method2[i].checked)
     {
       v_card_type = v_payment_method2[i].value;
     }
  }
  
  var v_has_err = false;
  
  
  
  //cc validations
  if ((v_payment_method_hidden != 'PAYPAL') && (v_payment_method_hidden != 'WIRETR'))	  
  {
	  if(v_cc_cvv !='undefined'){
	    var v_cc_cvv = v_cc_cvv.replace(/[^0-9]/g,''); //validating card security code
	  }
	  
	  if ((v_card_type == 'undefined') && (v_cc_num.length > 0))
	  {
		  $("#cc_validation_message").html('<p>' + 'Oops! Some of your credit card information is either missing or invalid. Please review your Card Type, Card Number, Expiration Date, and Security Code.' + '</p>');
          $("#cc_number").addClass('validate');
          $("#cc_expiration").addClass('validate');
          $("#cc_cvv").addClass('validate');
          v_has_err = true;
	  }
	  if (((v_expiry_month == '') || (v_expiry_year == '') || (v_cc_cvv == '') || (v_cc_cvv.length > 4) || (v_cc_cvv.length < 3)) && (v_card_type != 'golfsmith_cc') && (v_card_type != 'undefined'))
	  {
		  $("#cc_validation_message").html('<p>' + 'Oops! Some of your credit card information is either missing or invalid. Please review your Card Type, Card Number, Expiration Date, and Security Code.' + '</p>');
          $("#cc_number").addClass('validate');
          $("#cc_expiration").addClass('validate');
          $("#cc_cvv").addClass('validate');
          v_has_err = true;
	  }

	  if (v_card_type == 'golfsmith_cc')
	  {
		  var gs_ccnum = v_cc_num.replace(/[^0-9]/g,'');
		  if (v_deferment_option == 'undefined')
		  {
			  $("#cc_validation_message").html('<p>' + 'Please select interest deferment term.' + '</p>');
			  $("#terms_select").addClass('validate');          
			  v_has_err = true;
		  }
		  
		  if (gs_ccnum.substr(0,5) != '60205') 
		  {
			  $("#cc_validation_message").html('<p>' + 'Card number ' + gs_ccnum + ' does not appear to be a valid golfsmith card number.' + '</p>');
	          $("#terms_select").addClass('validate');	          
	          v_has_err = true;
		  }
	  }
  }
  
  if(typeof(v_save_cc_info) == 'undefined' && v_payment_method_hidden != 'PAYPAL') {

    v_save_cc_field = document.getElementById('save_cc');
    
    if(v_save_cc_field.checked == true) {
      v_save_cc_info = 'Y'
    } else {
      v_save_cc_info = 'N';
    }

  }

  if(typeof(v_gc_num) == 'undefined') {
    v_gc_num = '';
    v_gc_pin = '';
  }

  if (!v_has_err)
  {
	$("#preloader").show();
  $.ajax({
    type : 'post',
    url : v_url,
    data : 'cashCard_number=' + v_promo_code + '&giftCard_number_input=' + v_gc_num + '&giftCard_pin_input=' + v_gc_pin + '&require_wire_transfer=' + v_require_wire_transfer + '&ccType=' + v_payment_method + '&cc_number_input=' + v_cc_num + '&cc_MM_input=' + v_expiry_month + '&cc_YY_input=' + v_expiry_year + '&save_cc=' + v_save_cc_info + '&terms_select=' + v_deferment_option + '&cc_cvv=' + v_cc_cvv,
    cache : false,
    async : true,
     beforesend : function() {
	   
	},
    success : function(v_message) {
	  $("#preloader").hide();
      if(v_message == '') { //load the review page
        fadeinout_review(p_screen_name);
      
      } else {
        //determine where to display the error and display it
        if(v_message.substring(0, 9) == 'promocode') {
          $("#cashCard_validation_message").html('<p>' + v_message.substring(11) + '</p>');
          $("#cashCard_number").addClass('validate');
        } else if(v_message.substring(0, 8) == 'giftcard') {
          $("#giftCard_validation_message").html('<p>' + v_message.substring(10) + '</p>');
          $("#giftCard_number").addClass('validate');
          $("#giftCard_pin").addClass('validate');
        } else {
          $("#cc_validation_message").html('<p>' + v_message.substring(9) + '</p>');
          $("#cc_number").addClass('validate');
          $("#cc_expiration").addClass('validate');
          $("#cc_cvv").addClass('validate');
        }
      }
    }
    });
  }
return false;
}


function addWireTransferPayment(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/addWireTransferPayment/';
  } else {
    v_url = '/checkout/addWireTransferPayment/';
  }

  $.ajax({
    type : 'post',
    url : v_url,
    cache : false,
    async : true,
     beforesend : function() {
    },
    success : function(v_message) {
      if(v_message == '') { //load the review page
        alert('success!');
      } else {
        alert(v_message);
      }
    }
  });

}

function getExpiry() {
  $("#payment_terms").html('');
  $("#cc_expiration").show();
  $("#payment_save").html('<br /><p><input id="save_cc" class="no_border" name="save_cc" type="checkbox" tabindex="11"/> Save Credit Card for <strong><em>Fast</em> Checkout</strong> next time you order.</p>');
  $("#cc_cvv").show();
  $("#cc_cvv_help").show();
  //document.getElementById('save_cc').checked = true;
}

function validateGiftCard() {

  var v_gcnumber = $("#giftCard_number_input").val();
  var v_gcpin = $("#giftCard_pin_input").val();
  var v_is_valid = true;
  var v_numbers = '0123456789';
  var v_message = '';

  for (i = 0; i < v_gcnumber.length; i++) {
    if (v_numbers.indexOf(v_gcnumber.charAt(i)) == -1 && v_message == '') {
    	$("#giftCard_number").addClass('validate');
    	v_message = 'Sorry, <strong>' + v_gcnumber + '</strong> is not a valid Golfsmith Gift Card number.  If you meant to enter a Golfsmith Cash Card, please use the form below.';
    }else{
    	$("#giftCard_number").removeClass('validate');
    }
  }

  if (v_gcnumber.length > 0 && v_gcnumber.length != 16 && v_gcnumber.length != 15 && v_message == '') {
	  $("#giftCard_number").addClass('validate');
	  v_message = 'Sorry, Golfsmith does not recognized <strong>' + v_gcnumber + '</strong> as a valid card number.  Please double check that the card number and PIN number (if required) are typed correctly.  If you have entered them correctly, please try to \'apply\' it again before calling Customer Service at (800) 813-6897.';
  }else{
  	  $("#giftCard_number").removeClass('validate');
  }

  if (v_gcnumber.length > 0 && v_gcnumber.length == 16 && v_gcpin == '' && v_message == '') {
	  $("#giftCard_pin").addClass('validate');
     v_mesage = 'Gift card requires a valid pin number.';
  }else{
  	$("#giftCard_pin").removeClass('validate');
  }

  return v_message;
}

//if guest selected the FedEx address, load the form fields, set the hidden, and then run customer registration
function process_fedex_selection(p_screen_name) {
	$("#popUp_box").removeClass('address_change');	
  var v_data = '';

  $("#run_fedex_validation").val('N');

  var v_use_fedex = document.getElementById('fedex_shipto_fedex').checked;

  if(v_use_fedex == true) {
    //update shipping fields
    $("#add1_ship_input").val($("#fedex_address1").val());
    $("#add2_ship_input").val($("#fedex_address2").val());
    $("#city_ship_input"). val($("#fedex_city").val());
    $("#state_ship_input").val($("#fedex_state").val());
    $("#state_ship_select").val($("#fedex_state").val());
    $("#pcode_ship_input").val($("#fedex_pcode").val());

    autofillAddress();

  }

  $("#popUp_box").hide();
  $("#popUp_box").html('');

  //redo validation, just to make sure
  var v_is_valid = validateAddressEntry();

  if(v_is_valid == true) {

    var v_ship_fname = $("#fname_ship_input").val();
    var v_ship_lname = $("#lname_ship_input").val();
    var v_ship_addr1 = $("#add1_ship_input").val();
    var v_ship_addr2 = $("#add2_ship_input").val();
    var v_ship_city = $("#city_ship_input").val();
    var v_ship_state = $("#state_ship_select").val();
    var v_ship_pcode = $("#pcode_ship_input").val();
    var v_ship_country = $("#country_ship_select").val();    
    var v_ship_phone = $("#phone_ship_input").val()

    var v_bill_fname = $("#fname_bill_input").val();
    var v_bill_lname = $("#lname_bill_input").val();
    var v_bill_addr1 = $("#add1_bill_input").val();
    var v_bill_addr2 = $("#add2_bill_input").val();
    var v_bill_city = $("#city_bill_input").val();
    var v_bill_state = $("#state_bill_select").val();
    var v_bill_pcode = $("#pcode_bill_input").val();
    var v_bill_country = $("#country_bill_select").val();
    var v_bill_phone = $("#phone_bill_input").val();

    v_email = $("#email_address_input").val();
    var v_rewards = $("#rewards_player_input").val();
    var v_email_subscribe = $("#rewards_emails_input").val(); 

    v_data = 'fname_ship_input=' + v_ship_fname + '&lname_ship_input=' + v_ship_lname + '&add1_ship_input=' + v_ship_addr1 + '&add2_ship_input=' + v_ship_addr2 + '&city_ship_input=' + v_ship_city + '&state_ship_input=' + v_ship_state + '&pcode_ship_input=' + v_ship_pcode + '&country_ship_select=' + v_ship_country + '&phone_ship_input=' + v_ship_phone + '&fname_bill_input=' + v_bill_fname + '&lname_bill_input=' + v_bill_lname + '&add1_bill_input=' + v_bill_addr1 + '&add2_bill_input=' + v_bill_addr2 + '&city_bill_input=' + v_bill_city + '&state_bill_input=' + v_bill_state + '&pcode_bill_input=' + v_bill_pcode + '&country_bill_select=' + v_bill_country + '&phone_bill_input=' + v_bill_phone + '&email_address_input=' + v_email + '&rewards_player_input=' + v_rewards + '&rewards_emails_input=' + v_email_subscribe;

    registerCustomer(p_screen_name, v_data);
  }
}

function process_fedex_selection_payment(p_screen_name, p_containing_page) {

  var v_data = '';

  $("#run_fedex_validation").val('N');

  var v_use_fedex = document.getElementById('fedex_shipto_fedex').checked;

  if(v_use_fedex == true) {
    //update shipping fields
    $("#add1_ship_input").val($("#fedex_address1").val());
    $("#add2_ship_input").val($("#fedex_address2").val());
    $("#city_ship_input"). val($("#fedex_city").val());
    $("#state_ship_input").val($("#fedex_state").val());
    $("#state_ship_select").val($("#fedex_state").val());
    $("#pcode_ship_input").val($("#fedex_pcode").val());
  }

 
  //don't clear the HTML here -- we still need the form fields

  //redo validation, just to make sure
  var v_is_valid = validateAddressBookEntry();

  if(v_is_valid == true) {
    //address insert/update goes here
    var v_address_id = $("#address_id").val();
    var v_contact_id = $("#contact_id").val();
    var v_phone_id = $("#phone_id").val();
    var v_address_type = $("#address_type").val();
    var v_ship_fname = $("#fname_ship_input").val();
    var v_ship_lname = $("#lname_ship_input").val();
    var v_ship_addr1 = $("#add1_ship_input").val();
    var v_ship_addr2 = $("#add2_ship_input").val();
    var v_ship_city = $("#city_ship_input").val();
    var v_ship_state = $("#state_ship_select").val();
    var v_ship_pcode = $("#pcode_ship_input").val();
    var v_ship_country = $("#country_ship_select").val();
    var v_phone = $("#phone_ship_input").val();

    v_data = 'address_id=' + v_address_id + '&contact_id=' + v_contact_id + '&phone_id=' + v_phone_id + '&fname_ship_input=' + v_ship_fname + '&lname_ship_input=' + v_ship_lname + '&add1_ship_input=' + v_ship_addr1 + '&add2_ship_input=' + v_ship_addr2 + '&city_ship_input=' + v_ship_city + '&state_ship_input=' + v_ship_state + '&pcode_ship_input=' + v_ship_pcode + '&country_ship_select=' + v_ship_country + '&phone_ship_input=' + v_phone + '&address_type=' + v_address_type + '&referring_page=address_book' + '&containing_pag e=' + p_containing_page;

    updateAddressBookAddress(p_screen_name, v_data, p_containing_page);

    //this is where we need containing page
  }

}

function updateAddressBookAddress(p_screen_name, p_data, p_containing_page) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/updateAddress/';
  } else {
    v_url = '/checkout/updateAddress/';
  }

  $.ajax({
    type : 'post',
    url : v_url,
    data : p_data,
    cache : false,
    async : true,
    beforesend : function() {
    },
    success : function(v_html) {
      //this will be for payment page or review page, depending on where it was called
      //also need containing page here, to find out whether to load the totals section

    	if(v_html == '') {

        if(p_containing_page == 'payment') {
          //if containing page is payment, refresh the main content
          refreshPayment(p_screen_name);
          updatePaymentShipping(p_screen_name);
        } else {
          //if containing page is review, refresh the main content and the bottom sections
          refreshReviewCharges(p_screen_name);
          refreshReviewShipSection(p_screen_name);
          refreshReviewShippingAndPayment(p_screen_name);  
        }
        $("#popUp_box").hide();
        $("#popUp_box").html('');
      }
      else
      {
    	  $("#address_validation_message").html('<p>' + v_html + '</p>');
    	  $("#address_validation_message").show();
      }
    }
  });

}

function validateAddressEntry() {

  var v_valid = true;
  var v_is_select_field = false;
  var v_field_value = '';

  var va_required_fields = new Array("fname_ship", "lname_ship", "add1_ship", "city_ship", "state_ship", "pcode_ship", "country_ship", "phone_ship", "email_address","email_address_confirm", "fname_bill", "lname_bill", "add1_bill", "city_bill", "state_bill", "pcode_bill", "country_bill", "phone_bill");

  var va_list_names = new Array("shipping_firstName", "shipping_lastName", "shipping_address1", "shipping_city", "shipping_state", "shipping_postalCode", "shipping_country", "shipping_phone", "email_address","email_address_confirm", "billing_firstName", "billing_lastName", "billing_address1", "billing_city", "billing_state", "billing_postalCode", "billing_country", "billing_phone");

  //clear error fields
  for(i = 0; i < va_required_fields.length; i++) {
    $("#" + va_required_fields[i] + "_error").empty();
  }

  for(i = 0; i < va_required_fields.length; i++) {

    if(va_required_fields[i].substr(0, 7) == 'country') {

      v_field_value = $("#" + va_required_fields[i] + "_select").val();

      if(v_field_value.replace(/\s/g, "") == "" || v_field_value == "--") {
        $("#" + va_list_names[i]).addClass('validate');
        $("#" + va_required_fields[i] + "_error").text(' Required');
        v_valid = false;
      } else {
        $("#" + va_list_names[i]).removeClass('validate');
      }
    } else if(va_required_fields[i].substr(0, 5) == 'state') {
      if(va_list_names[i].substring(0, 8) == "shipping") {
        v_country = $("#country_ship_select").val();
      } else {
        v_country = $("#country_bill_select").val();
      }

      if(v_country == 'US') {
        v_field_value = $("#" + va_required_fields[i] + "_select").val();
        if(v_field_value.replace(/\s/g, "") == "" || v_field_value == "--") {
          $("#" + va_list_names[i]).addClass('validate');
          $("#" + va_required_fields[i] + "_error").text(' Required');
          v_valid = false;
        } else {
          $("#" + va_list_names[i]).removeClass('validate');
        }

      }else if(v_country == 'CA'){
    	  v_field_value = $("#" + va_required_fields[i] + "_ca_select").val();
          if(v_field_value.replace(/\s/g, "") == "" || v_field_value == "--") {
            $("#" + va_list_names[i]).addClass('validate');
            $("#" + va_required_fields[i] + "_error").text(' Required');
            v_valid = false;
          } else {
            $("#" + va_list_names[i]).removeClass('validate');
          }
    	  
      }//state/province is not a required field for international

    } else {

      v_field_value = $("#" + va_required_fields[i] + "_input").val();

      if(va_required_fields[i].substr(0, 5) == "pcode") {

        if(va_list_names[i].substring(0, 8) == "shipping") {
          v_country = $("#country_ship_select").val();
          if(v_country != 'US' && v_country != 'CA') {
            v_state = $("#state_ship_input").val();
          } else if(v_country=='US') {
            v_state = $("#state_ship_select").val();
          }else{
        	v_state = $("#state_ship_ca_select").val();
          }
        } else {
          v_country = $("#country_bill_select").val();
          if(v_country != 'US' && v_country != 'CA') {
            v_state = $("#state_bill_input").val();
          } else if(v_country=='US') {
            v_state = $("#state_bill_select").val();
          }else{
        	  v_state = $("#state_bill_ca_select").val();
          }
        }

        if(v_field_value.replace(/\s/g, "").replace('-', '') == "" && (v_country== 'US' || v_country == 'CA')) {

          $("#" + va_list_names[i]).addClass('validate');
          $("#" + va_required_fields[i] + "_error").text(' Required');
          v_valid = false;
        } else {
          if(v_country == 'US' || v_country == 'CA') {
            v_valid2 = validatePostalCode(v_field_value, v_country, v_state, va_required_fields[i], va_list_names[i]);
            if(v_valid2 != true) {
              v_valid = false;
            }
          }
        }

      } else if(va_required_fields[i].substr(0, 5) == "phone") {

        if(v_field_value.replace(/\s/g, "") == "") {
          $("#" + va_list_names[i]).addClass('validate');
          $("#" + va_required_fields[i] + "_error").text(' Required');
          v_valid = false;
        } else {
          if(va_list_names[i].substring(0, 8) == "shipping") {
            v_country = $("#country_ship_select").val();
          } else {
            v_country = $("#country_bill_select").val();
          }
          v_valid2 = validatePhone(v_field_value, v_country, va_required_fields[i], va_list_names[i]);
          if(v_valid2 != true) {
            v_valid = false;
          }
        }

      } else if(va_required_fields[i] == "email_address" || va_required_fields[i] == "email_address_confirm") {
    	  v_email = $("#email_address_input").val();
    	  v_confirm_email = $("#email_address_confirm_input").val();
        if(v_field_value.replace(/\s/g, "") == "") {
          $("#" + va_list_names[i]).addClass('validate');
          $("#" + va_required_fields[i] + "_error").text(' Required');
          v_valid = false;
        } else if(!isValidEmail(v_field_value)) {
          $("#" + va_list_names[i]).addClass('validate');
          $("#" + va_required_fields[i] + "_error").text(' Invalid');
          v_valid = false;
        }else if(v_email!=v_confirm_email){
        	$("#" + va_list_names[i]).addClass('validate');
            $("#" + va_required_fields[i] + "_error").text(' Mismatched');
            v_valid = false;
        } else {
          $("#" + va_list_names[i]).removeClass('validate');
        }

      } else {
        if(v_field_value.replace(/\s/g, "") == "") {
          $("#" + va_list_names[i]).addClass('validate');
          $("#" + va_required_fields[i] + "_error").text(' Required');
          v_valid = false;
        } else {
          $("#" + va_list_names[i]).removeClass('validate');
        }
      }
    }

  }
  if(v_valid!=true){
	$("#shipping_validation_message").html('<p>Please fix the errors and try again.</p>');
	$("#shipping_validation_message").show('slow');
  }else{
	$("#shipping_validation_message").hide();
  }
  return v_valid;

}

function validateAddressBookEntry() {

  var v_valid = true;
  var v_is_select_field = false;
  var v_field_value = '';

  var va_required_fields = new Array("fname_ship", "lname_ship", "add1_ship", "city_ship", "state_ship", "pcode_ship", "country_ship", "phone_ship");

  var va_list_names = new Array("shipping_firstName", "shipping_lastName", "shipping_address1", "shipping_city", "shipping_state", "shipping_postalCode", "shipping_country", "shipping_phone");

  //clear error fields
  for(i = 0; i < va_required_fields.length; i++) {
    $("#" + va_required_fields[i] + "_error").empty();
  }

  for(i = 0; i < va_required_fields.length; i++) {

    if(va_required_fields[i].substr(0, 7) == 'country') {

      v_field_value = $("#" + va_required_fields[i] + "_select").val();

      if(v_field_value.replace(/\s/g, "") == "" || v_field_value == "--") {
        $("#" + va_list_names[i]).addClass('validate');
        $("#" + va_required_fields[i] + "_error").text(' Required');
        v_valid = false;
      } else {
        $("#" + va_list_names[i]).removeClass('validate');
      }
    } else if(va_required_fields[i].substr(0, 5) == 'state') {
      if(va_list_names[i].substring(0, 8) == "shipping") {
        v_country = $("#country_ship_select").val();
      } else {
        v_country = $("#country_bill_select").val();
      }

      if(v_country == 'US') {
        v_field_value = $("#" + va_required_fields[i] + "_select").val();

        if(v_field_value.replace(/\s/g, "") == "" || v_field_value == "--") {
          $("#" + va_list_names[i]).addClass('validate');
          $("#" + va_required_fields[i] + "_error").text(' Required');
          v_valid = false;
        } else {
          $("#" + va_list_names[i]).removeClass('validate');
        }

      } else if(v_country == 'CA'){
    	  v_field_value = $("#" + va_required_fields[i] + "_ca_select").val();
          if(v_field_value.replace(/\s/g, "") == "" || v_field_value == "--") {
            $("#" + va_list_names[i]).addClass('validate');
            $("#" + va_required_fields[i] + "_error").text(' Required');
            v_valid = false;
          } else {
            $("#" + va_list_names[i]).removeClass('validate');
          }
      }//state/province is not a required field for international

    } else {

      v_field_value = $("#" + va_required_fields[i] + "_input").val();

      if(va_required_fields[i].substr(0, 5) == "pcode") {

        if(va_list_names[i].substring(0, 8) == "shipping") {
          v_country = $("#country_ship_select").val();
          if(v_country == 'CA') {
        	  v_state = $("#state_ship_ca_select").val();
          } else if(v_country == 'US') {
        	  v_state = $("#state_ship_select").val();
          }else{
        	v_state = $("#state_ship_input").val();
            
          }
        } else {
          v_country = $("#country_bill_select").val();
          if(v_country != 'US' && v_country != 'CA') {
            v_state = $("#state_bill_input").val();
          } else {
            v_state = $("#state_bill_select").val();
          }
        }

        if(v_field_value.replace(/\s/g, "").replace('-', '') == "" && (v_country== 'US' || v_country == 'CA')) {

          $("#" + va_list_names[i]).addClass('validate');
          $("#" + va_required_fields[i] + "_error").text(' Required');
          v_valid = false;
        } else {
          if(v_country == 'US' || v_country == 'CA') {
            v_valid2 = validatePostalCode(v_field_value, v_country, v_state, va_required_fields[i], va_list_names[i]);
            if(v_valid2 != true) {
              v_valid = false;
            }
          }
        }

      } else if(va_required_fields[i].substr(0, 5) == "phone") {

        if(v_field_value.replace(/\s/g, "") == "") {
          $("#" + va_list_names[i]).addClass('validate');
          $("#" + va_required_fields[i] + "_error").text(' Required');
          v_valid = false;
        } else {
          v_country = $("#country_ship_select").val();

          v_valid2 = validatePhone(v_field_value, v_country, va_required_fields[i], va_list_names[i]);
          if(v_valid2 != true) {
            v_valid = false;
          }
        }

      } else {
        if(v_field_value.replace(/\s/g, "") == "") {
          $("#" + va_list_names[i]).addClass('validate');
          $("#" + va_required_fields[i] + "_error").text(' Required');
          v_valid = false;
        } else {
          $("#" + va_list_names[i]).removeClass('validate');
        }
      }
    }

  }
  
if(v_valid!=true){
  $("#address_validation_message").html('<p>Please fix the errors below and try again.</p>');
  $("#address_validation_message").show('slow');
}else{
  $("#address_validation_message").hide();
}

  return v_valid;
  

}

function validatePostalCode(p_postal_code, p_country, p_state, p_input_name, p_list_id) {

  v_valid = true;

  if(p_country == 'US' && (p_state == 'AB' || p_state == 'BC' || p_state == 'MB' || p_state == 'NB' || p_state == 'NL' 
                        || p_state == 'NS' || p_state == 'NT' || p_state == 'NU' || p_state == 'ON' || p_state == 'PE' 
                        || p_state == 'QC' || p_state == 'SK' || p_state == 'YT')) {
    p_country = 'CA';
  }

  if(p_country == 'US') {
    v_postal_code = p_postal_code.replace(/[^0-9]/g, '');

    if(v_postal_code.length != 5 && v_postal_code.length != 9) {
      $("#" + p_list_id).addClass('validate');
      $("#" + p_input_name + "_error").text(' Invalid');
      v_valid = false;
    } else {
      if(v_postal_code.length > 5) {
        $("#" + p_input_name + "_input").val(v_postal_code.substring(0, 5) + "-" + v_postal_code.substr(5));
      } else {
        $("#" + p_input_name + "_input").val(v_postal_code);
      }
      $("#" + p_list_id).removeClass('validate');
    } 
  } else if(p_country == 'CA') {
    v_postal_code = p_postal_code.replace(/[^a-zA-Z0-9]/g, '');

    if(v_postal_code.length != 6) {
      $("#" + p_list_id).addClass('validate');
      $("#" + p_input_name + "_error").text(' Invalid');
      v_valid = false;
    } else {
      $("#" + p_input_name + "_input").val(v_postal_code.substr(0, 3) + "-" + v_postal_code.substr(3));
      $("#" + p_list_id).removeClass('validate');
    }

  }

  return v_valid;

}

function validatePhone(p_phone, p_country, p_input_name, p_list_id) {

  v_valid = true;

  if(p_country == 'US' || p_country=='CA') {
    v_phone = p_phone.replace(/[^0-9]/g, '');
    if(v_phone.length != 10) {
      $("#" + p_list_id).addClass('validate');
      $("#" + p_input_name + "_error").text(' Invalid');
      v_valid = false;
    } else {
      $("#" + p_input_name + "_input").val(v_phone.substr(0, 3) + "-" + v_phone.substr(3, 3) + "-" + v_phone.substr(6));
      $("#" + p_list_id).removeClass('validate');
    }
  }

  return v_valid;

}

function adjustEmptyCartDisplay() {
  $("#cart_checkoutOptions").remove();
  $("#cart_checkout").html('');
  $("#cart_media").html('');
}

//Ajax call to find out if customer is logged in
function checkLoggedIn(p_screen_name) {

  v_logged_in = false;

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/checkLoggedIn/';
  } else {
    v_url = '/checkout/checkLoggedIn/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    cache : false,
    async : false,
    beforesend : function() {

    },
    success : function(v_result) {

      if(v_result == 'true') {
        v_logged_in = true;
      } //else variable should already be set to false
    }
  });

  return v_logged_in;

}

function isISPOnly(p_screen_name) {

  v_is_isp_only = false;

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/isISPOnly/';
  } else {
    v_url = '/checkout/isISPOnly/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    cache : false,
    async : false,
    beforesend : function() {

    },
    success : function(v_result) {

      if(v_result == 'true') {
        v_is_isp_only = true;
      } //else variable should already be set to false
    }
  });

  return v_is_isp_only;

}

function hasPayments(p_screen_name) {

  v_has_payments = false;

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/hasPayments/';
  } else {
    v_url = '/checkout/hasPayments/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    cache : false,
    async : false,
    beforesend : function() {

    },
    success : function(v_result) {
      if(v_result == 'true') {
        v_has_payments = true;
      } //else variable should already be set to false
    }
  });

  return v_has_payments;

}

function updateSalesAssociateInfo(p_screen_name) {

  v_salesrep_number = $("#caddie_badge_input").val();
  v_store_number = $("#caddie_store_select").val();

  if(v_salesrep_number != '' && v_store_number != '' && v_store_number != 'Select Your Store') {

    if(p_screen_name != '') {
      v_url = '/' + p_screen_name + '/checkout/updateSalesAssociateInfo/';
    } else {
      v_url = '/checkout/updateSalesAssociateInfo/';
    }

    $.ajax( {
      type : 'post',
      url : v_url,
      data : 'salesrep_number=' + v_salesrep_number + '&store_number=' + v_store_number, 
      cache : false,
      async : false,
      beforesend : function() {

      },
      success : function(v_html) {
        $("#submit_order").empty();
        $("#submit_order").html(v_html);

      }
    });

  }

}

function updateAccount(p_screen_name) {

  v_password = $("#createAccount_password_input").val();
  v_password_confirm = $("#createAccount_passwordConfirm_input").val();

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/updateAccount/';
  } else {
    v_url = '/checkout/updateAccount/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    data : 'createAccount_password_input=' + v_password + '&createAccount_passwordConfirm_input=' + v_password_confirm,
    cache : false,
    async : false,
    beforesend : function() {

    },
    success : function(v_message) {
      if(v_message.substr(0, 5) == 'Error') {
        $("#createAccount_validation_message").html('<p>' + v_message.substr(7) + '</p>');
      } else {
        $("#createAccount").html(v_message);
        toggle_login_header(p_screen_name, true);
      }

    }
  });

}

function processOrder(p_screen_name) {

	
var v_ret = validate_cart_QBD(p_screen_name);	

	
	if (v_ret == true) {
				
		  if(p_screen_name != '') {
		    v_url = '/' + p_screen_name + '/checkout/processOrder/';
		  } else {
		    v_url = '/checkout/processOrder/';
		  }
		  $("#preloader").show();
		  $.ajax( {
		    type : 'post',
		    url : v_url,
		    cache : false,
		    async : false,
		    beforesend : function() {
		
		    },
		    success : function(v_html) {
		      if(v_html.substr(0, 6) == 'paypal') {
		        //show popup box with message
		        $("#popUp_box").html(v_html.substr(8));
		        $("#popUp_box").show();
		        $("#preloader").hide();
		      }else if (v_html.substr(0, 6) == 'qtyerr') {
		    	  
		    	//show popup box with message
		    	  if(p_screen_name != '') {
		    		  v_cart_url = '/' + p_screen_name + '/checkout/cart/';
		    	  } else {
		    		  v_cart_url = '/checkout/cart/';
		    	  }
		    	  
		    	  $("#popUp_box").empty();
			      $("#popUp_box").append('<div id="popUp_border"><p>' + v_html.substr(8) + '</p>Please update the quantity in the Shopping Cart.  Go to <a href="' + v_cart_url + '">Shopping Cart.</a><p></p></div>');
			      $("#popUp_box").show();
		          $("#preloader").hide();
		    	  
		      }else if (v_html.substr(0, 10) == 'restricted') {

		    	  
		    	  if(p_screen_name != '') {
		    		  v_cart_url = '/' + p_screen_name + '/checkout/cart/';
		    	  } else {
		    		  v_cart_url = '/checkout/cart/';
		    	  }
		    	  

		    	  //show popup box with message
		    	  $("#popUp_box").empty();
		    	  $("#popUp_box").html('<div id="popUp_border"><p>Some items <u>cannot ship</u> to the country you have provided. Please see the <a href="' + v_cart_url + '">shopping cart</a> for details.</p><div>');
		          $("#popUp_box").show();
		          $("#preloader").hide();
		          //return false;
		      } else if(v_html.substr(0, 7) == 'payment') {    	
		      	var start = v_html.indexOf("errStart");    	
		    	var end = v_html.indexOf("errEnd");    	
		    	alert(v_html.substr(start + 8 , (end - (start + 8))));
		    	//$("#preloader").show();
		        fadeinout_review_to_payment(p_screen_name);
		       // $("#preloader").hide();
		      } else {
		        v_html = v_html.substr(8);
		        $("#preloader").show();
		        $("#shopping_cart").fadeOut('slow', function() {
		          $("#shopping_cart").html(v_html);
		          $("#shopping_cart").addClass('thank_you');
		          $("#shopping_cart").fadeIn(100,function(){
		          $("#preloader").hide();
		        });
		        });
		
		      }
		
		    }
		  });
		return false;
		
	} else {
		
		location.reload(true);
		return false;
		
	}
}

function googleCheckout(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/getGoogleCheckoutUrl/';
  } else {
    v_url = '/checkout/getGoogleCheckoutUrl/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    cache : false,
    async : false,
    beforesend : function() {

    },
    success : function(v_result) {
      if(v_result.substr(0, 5) == 'Error') {
        $("#checkoutOptions_validation_message").html('<p>' + v_result.substr(7) + '</p>');
      } else if(v_result != '') {
        window.location = v_result;
      }

    }
  });

}

function paypalCheckout(p_screen_name) {

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/getPayPalCheckoutUrl/';
  } else {
    v_url = '/checkout/getPayPalCheckoutUrl/';
  }

  $.ajax( {
    type : 'post',
    url : v_url,
    cache : false,
    async : false,
    beforesend : function() {

    },
    success : function(v_result) {
      if(v_result.substr(0, 5) == 'Error') {
        $("#checkoutOptions_validation_message").html('<p>' + v_result.substr(7) + '</p>');
      } else if(v_result != '') {
        window.location = v_result;
      } else {
        alert('blank?');
      }

    }
  });

}

function fadeinout_cart(p_screen_name) {

  v_logged_in = checkLoggedIn(p_screen_name);
  if(p_screen_name != '') {
    v_page_name = '/' + p_screen_name;
  } else {
    v_page_name = '';
  }

  if(v_logged_in == true) {
    //check for payment
    v_has_payments = hasPayments(p_screen_name);

    if(v_has_payments == true) {
      v_page_name = 'review';
      shoppingCartTopFocus();
    } else {
      v_page_name = v_page_name + '/checkout/paymentcontent';
      shoppingCartTopFocus();
    }
  } else { //customer is not logged in
    v_page_name = v_page_name + '/checkout/loginContent/';
    shoppingCartTopFocus();
  }


  if(v_page_name != 'review') {

    $("#preloader").show();
    $("#shopping_cart").fadeOut('slow', function() {
      $("#cart_checkoutOptions").remove();
      $("#cart_checkout").html('');
      $("#cart_media").html('');
      $("#cart_top").html('');
      $("#cart_footer").remove();
    $("#cart_items").load(v_page_name, function(v_response) {
      $("#shopping_cart").fadeIn(100,function(){
      $("#preloader").hide();
      });
    });

      //$("#cart_lower_border").html('<div id="cart_bottom"><div id="bottom_left"></div></div>');
    });

  } else {

    fadeinout_review(p_screen_name);
    
  }
}

//this function is only called if login was successful
function fadeinout_login(p_screen_name) {

  //check for payment
  v_has_payments = hasPayments(p_screen_name);
  if(p_screen_name != '') {
    v_page_name = '/' + p_screen_name;
  } else {
    v_page_name = '';
  }
 
  if(v_has_payments == true) {
    v_page_name = 'review';
  } else {
    v_page_name = v_page_name + '/checkout/paymentcontent';
  }

  if(v_page_name != 'review') {
    fadeinout_generic(v_page_name);
  } else {
    fadeinout_review(p_screen_name);
  }

}

function fadeinout_generic(p_page_name) {

  $("#preloader").show();
  $("#shopping_cart").fadeOut('slow', function() {
    $("#cart_items").load(p_page_name, function(v_response) {
        $("#shopping_cart").fadeIn(100,function(){
        	 shoppingCartTopFocus();
        	$("#preloader").hide();
        });
    });
  });
}

function fadeinout_review_to_payment(p_screen_name) {

	  $("#preloader").show();
	  $("#shopping_cart").fadeOut('slow', function() {
	    $("#footer_left").html('');
	    $("#cart_charges").html('');
	    $("#cart_shippingTo_header").html('');

	    //the only thing that seems to keep the ISP section from showing up is remove() combined with hide()
	    $("#instore_pickup").remove();
	    $("#instore_pickup").hide();

	    $("#shopping_cart").removeClass('isp');

	    //$("#cart_header").html('<div id="cart_estTotal_top"><table><tr><td><h6>Estimated Total:</h6></td><td class="estTotal" id="estTotalTop"><h6><strong></strong></h6></td></tr></table></div>');
	    $("#cart_header").html('');
	    $("#cart_footer").remove();
	    
	    
	    if(p_screen_name != '') {
	      v_screen_name = '/' + p_screen_name;
	    } else {
	      v_screen_name = '';
	    }

	    $("#cart_items").load(v_screen_name + '/checkout/paymentContent/');
	    $("#cart_items").show();

	    $("#cart_lower_border").html('<div id="cart_bottom"><div id="bottom_left"></div></div></div><br class="clear" /><br /><br /><br /><br /><br />');

	    $("#submit_order").html('');

	    $("#cart_top").show();
	    $("#footer_left").removeClass('isp_only');
	    $("#cart_charges").show();
	    $("#cart_bottom").after("<div id='siteseal'></div>");
	    $("#siteseal").append('<br /><br /><br /><span id="mcafee_seal_bottom" class="float_R"><a href="https://www.mcafeesecure.com/RatingVerify?ref=www.golfsmith.com" target="_blank"><img height="32" border="0" width="115" alt="McAfee Secure sites help keep you safe from identity theft, credit card fraud, spyware, spam, viruses and online scams" src="//images.scanalert.com/meter/www.golfsmith.com/22.gif"/></a></span>');
	    $("#shopping_cart").fadeIn(3000,function(){
	    	shoppingCartTopFocus();
	    	$("#preloader").hide();
	    });

	  });

	}

function fadeinout_review(p_screen_name) {

  var v_url = '';

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/checkout/reviewContent/';
  } else {
    v_url = '/checkout/reviewContent/';
  }

  v_ship_method = $("#method_select").val();

  //we need to know if the order is all ISP
  v_is_isp_only = isISPOnly(p_screen_name);

  
   $("#shopping_cart").fadeOut('slow', function() {
	   
   });
	
    $.ajax({
      type : 'post',
      url : v_url,
      data : 'method_select=' + v_ship_method,
      cache : false,
      async : true,
       beforesend : function() {
    //	$("#preloader").show();
      },
      success : function(v_html) {
        $("#shopping_cart").html(v_html);
        $("#shopping_cart").fadeIn(100,function(){
        $("#shopping_cart").addClass('isp');
        shoppingCartTopFocus();
       	//$("#preloader").hide();
        });
        //$("#preloader").hide();
        if(v_is_isp_only == true) {
          //we'll re-show these if we need them
          $("#cart_top").hide();
          $("#footer_left").addClass('isp_only');
          $("#cart_charges").hide();
        }
        
      }
  //$("#preloader").hide();
    
  });
return false;
}


function loadHeader(p_section) {
  $("#cart_pageTitle").load('/static_pages/html/_shopping_cart/headers/header_' + p_section + '.html');
}

function loadCartImage(p_active, p_screen_name) {
	
	if(p_screen_name != '') {
	    v_url = '/' + p_screen_name + '/checkout/cart/';
	  } else {
	    v_url = '/checkout/cart/';
	  }
	
	if (p_active) {
		$("#cart_pageTitle").html('<h1 class="pageTitle_link"><a class="pointer" href="' + v_url + '" >Shopping Cart</a></h1><p>Need some help? <strong>Call (800) 813-6897</strong></p>');
	} else {
		$("#cart_pageTitle").html('<h1 class="pageTitle_cart"><a>Shopping Cart</a></h1><p>Need some help? <strong>Call (800) 813-6897</strong></p>');
	}
}

function loadCartTop(p_section) {	

	  if (p_section == 'none')
	  {
		  $("#cart_top").html('');		  
	  }else {
		  $("#cart_top").load('/static_pages/html/_shopping_cart/cart_tops/cart_top_' + p_section + '.html');
	  }		  
}

function loadNavigation(p_section) {
	  $("#cart_steps").load('/static_pages/html/_shopping_cart/navigations/navigation_' + p_section + '.html');
	}

function loadSteps(p_step) {

  var v_html = '<h5 id="step1_shipping" class="step_';
  if(p_step <= 1) {
    v_html += 'active';
  } else {
    v_html += 'done';
  }
  v_html += '">Step 1: Shipping</h5>';
  v_html += '<h5 id="step2_payment"';
  if(p_step >= 2) {
    v_html += ' class="step_';
    if(p_step == 2) {
      v_html += 'active';
    } else {
      v_html += 'done';
    }
    v_html += '"';
  }
  v_html += '>Step 2: Payment</h5>';
  v_html += '<h5 id="step3_confirm"';
  if(p_step == 3) {
    v_html += ' class="step_active"';
  }
  v_html += '>Step 3: Confirm &amp; Submit</h5>';

  $("#cart_steps").empty();
  $("#cart_steps").append(v_html);

}

function getCurrencyConversion(p_screen_name) {

  v_convert_to = $("#convertto").val();
  v_amount = $("#vamount").val();

  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/currencyconverter/convertCurrency/';
  } else {
    v_url = '/currencyconverter/convertCurrency/';
  }

  $.ajax({
    type: 'post',
    url: v_url,
    data: 'convert_to=' + v_convert_to + '&amount=' + v_amount,
    success:function(html){
      $("#currency_converter_body").empty();
      $("#currency_converter_body").append(html);
    }
  });

  return false;
}

function showCurrencyConverter() {
  $("#currency_converter").show('slow');
}

function hideCurrencyConverter() {
  $("#currency_converter").hide(800);
}

function show_hide_update(p_screen_name, p_line_number, p_line_reference) {
  $('td#item_total_' + p_line_number).addClass("update_total");
  $('td#item_total_' + p_line_number).text("");
  $('td#item_total_' + p_line_number).html('<h6><a class="pointer" onclick="updateItem(\'' + p_screen_name + '\', ' + p_line_number + ', ' + p_line_reference + ');"> UPDATE </a> </h6>');
}


function update_order_qty(event, p_screen_name, p_line_number, p_line_reference) {
	
	var charCode = (event.which) ? event.which : event.keyCode;
	var ret = true;

	if (block_alpha(event))
	{
		if (charCode == 13)
		{
			updateItem(p_screen_name, p_line_number, p_line_reference);
			ret = false;
		}
	} else {
		ret = false;
	}
	
	return ret;
}

function block_alpha(event) {
	
	var charCode = (event.which) ? event.which : event.keyCode
	
	if (charCode > 31 && (charCode < 48 || charCode > 57))
	{
		return false;
	}
	else
	{
		return true;
	}
}

function update_order_total(p_total) {
  $('td#estTotalTop').text("");
  $('td#estTotalTop').html("<h6><strong>" + p_total + "</strong></h6>");
}

function show_custom_specs(p_number) {
  $("#custom_specs_" + p_number).show('slow');
}

function hide_custom_specs(p_number) {
  $("#custom_specs_" + p_number).hide('slow');
}

function show_store_locator(p_screen_name, p_line_number){
  loadIspSelector(p_screen_name, p_line_number);
  $("#store_locator_" + p_line_number).show('slow');
}

function hide_store_locator(v_line_number) {
  $("#isp_locator_" + v_line_number).html('');
  $("#store_locator_" + v_line_number).hide('slow');
  $("#item"+v_line_number+"_ship").attr("checked","checked");
  $("#delivery_ship_"+v_line_number).addClass('delivery_selected');
  $("#delivery_isp_"+v_line_number).removeClass('delivery_selected');  
}

function user_address(){
  $("#user_add").addClass('chosen_address');
  $("#fedex_add").removeClass('chosen_address');
  $("#ship_address_compare").addClass('section_complete');
}

function fedex_address(){
  $("#fedex_add").addClass('chosen_address');
  $("#user_add").removeClass('chosen_address');
  $("#ship_address_compare").addClass('section_complete');
}

function toggle_checkout_options() {
  v_display = document.getElementById('show_google_checkout').value;

  if(v_display == 'false') {
    document.getElementById('enabled_google_checkout_1').style.display = 'none';
    document.getElementById('disabled_google_checkout_1').style.display = 'block';
  }

  v_display = document.getElementById('show_paypal_checkout').value;

  if(v_display == 'false') {
    document.getElementById('enabled_paypal_checkout_1').style.display = 'none';
  }
}

function toggle_cart_footer() {
	
	v_cart_items = document.getElementById('cart_item_count').value;
	
	if (v_cart_items <= 0)
	{
		$("#cart_checkoutOptions").html('');
	}
}

function toggle_login_header(p_screen_name, p_success) {
	if (p_success) {
		$("#siteHelp_login").html('<a href="/' + p_screen_name + '/logout.php"><strong>Log Out</strong></a>&nbsp; | &nbsp;<a href="/' + p_screen_name + '/myacct.php"><strong>My Account</strong></a>&nbsp; | &nbsp;<a href="/' + p_screen_name + '/orderstatus.php"><strong>Order Status</strong></a>');
	}else {
		$("#siteHelp_login").html('<a href="/' + p_screen_name + '/login.php"><strong>Login</strong></a><a href="/' + p_screen_name + '/orderstatus.php"><strong>Order Status</strong></a>');
	}
}

function showPopUpBox() {
  $("#popUp_box").show();
}

function hidePopUpBox() {
  $("#popUp_box").hide();
}

function checkEnterLoginPassword(event, p_screen_name) {

  if(event.keyCode == 13 || event.which == 13) {
    checkLogin(p_screen_name);
  } else {
    return false;
  }

}


function checkEnterGuestCheckoutAddresses(event, p_screen_name) {

  if(event.keyCode == 13 || event.which == 13) {
    checkGuestCheckoutAddresses(p_screen_name);
  } else {
    return false;
  }

}

function checkEnterPayments(event, p_screen_name) {
	
  if(event.keyCode == 13 || event.which == 13) {		
    checkPayments(p_screen_name);
  } else {
    return false;
  }

}


function checkEnterApplyPromoCode(event, p_screen_name) {
	
  if(event.keyCode == 13 || event.which == 13) {		
    applyPromoCode(p_screen_name);
  } else {
    return false;
  }

}


function checkEnterAddGiftCardPayment(event, p_screen_name) {
	
  if(event.keyCode == 13 || event.which == 13) {		
    addGiftCardPayment(p_screen_name);
  } else {
    return false;
  }

}


function checkEnterUpdateSalesAssociateInfo(event, p_screen_name) {
	
  if(event.keyCode == 13 || event.which == 13) {		
    updateSalesAssociateInfo(p_screen_name);
  } else {
    return false;
  }

}



function checkEnterAddressBookAddress(event, p_screen_name, p_address_type, p_containing_page) {

  if(event.keyCode == 13 || event.which == 13) {		
    checkAddressBookAddress(p_screen_name, p_address_type, p_containing_page)
  } else {
    return false;
  }

}


function checkEnterUpdateAccount(event, p_screen_name) {

  if(event.keyCode == 13 || event.which == 13) {		
    updateAccount(p_screen_name)
  } else {
    return false;
  }

}

function checkEnterISP(event, p_screen_name, p_line_number) {
  if(event.keyCode == 13 || event.which == 13) {
    locateStore(p_screen_name, p_line_number);
  } else {
    return false;
  }

}



function close_fedex_popup(){
  $("#popUp_box").hide();
}

function check_item_radio(p_line_no){
  if(document.getElementById("item"+p_line_no+"_ship").checked==true){
    $("#delivery_ship_"+p_line_no).addClass('delivery_selected');
  }else{
    $("#delivery_ship_"+p_line_no).removeClass('delivery_selected');
  }  
  if(document.getElementById("item"+p_line_no+"_isp").checked==true){
   $("#delivery_isp_"+p_line_no).addClass('delivery_selected');
  }else{
   $("#delivery_isp_"+p_line_no).removeClass('delivery_selected');
  }
}

function show_ca_store_message(p_screen_name){
  var v=$("#method_select").val();
  if(p_screen_name=='ca'){
    if (v=='I'){
      $('.item_delivery').html('<br /><p>Pickup at <a href="javascript:MM_openBrWindow(\'/ca/retail_store.php?store_number=888\',\'ca_retail\',\'scrollbars=yes,resizable=yes,width=950,height=950\');">Toronto Golfsmith Store</a></p>');
      $('#cart_shippingTo_header').html('<h2>In Store Pickup Items (Payment Due In Store)</h2><p><a class="edit_items pointer" href="/ca/checkout/cart/">EDIT ITEMS</a></p>');
    }else{
      $('#cart_shippingTo_header').html('<h2>Items Being Shipped</h2><p><a class="edit_items pointer" href="/ca/checkout/cart/">EDIT ITEMS</a></p>');
	  refreshCartItems(p_screen_name);
	}
  }
}

/*
* Shopping Cart Payment Page Disclosure PopUp Code
*/

function showModel(){
 $("#GE_disclosure").modal({onClose: function (dialog) {
   dialog.data.fadeOut('slow', function () {
     dialog.container.hide('0', function () {
       //dialog.overlay.slideUp('slow', function () {
		  $.modal.close();
		//	});
     });
	});
 }});
}

function display_DefermentDisclosure(p_screen_name) {
 if(p_screen_name != '') {
   v_url = '/' + p_screen_name + '/checkout/getdefermentdisclosure/';
 } else {
   v_url = '/checkout/getdefermentdisclosure/';
 }
 var v_item_select=document.getElementById('terms_select').value;
 if(v_item_select!="9999"){ 
   $.ajax({
     type : 'post',
     url : v_url,
     data : 'v_plan_no=' + v_item_select,
     cache : false,
     async : true,
     success : function(html) {
       $("#GE_disclosure").html('');
       $("#GE_disclosure").append(html);
       showModel();
	  }
   });
 }
}

function closePopupModel(){
 if(document.getElementById("esign_consent_input").checked==true &&document.getElementById("terms_agree_input").checked==true){
   $.modal.close();
 }else{
   alert('To continue you must check the E-sign consent and promotional terms and conditions checkboxes.');
 }
}

function display_DefermentDisclosurePrint(p_screen_name,p_pnumber) {

	 if(p_screen_name != '') {
	   v_url = '/' + p_screen_name + '/checkout/getdefermentdisclosure/';
	 } else {
	   v_url = '/checkout/getdefermentdisclosure/';
	 }
	 var v_item_select=p_pnumber;
	 if(v_item_select!="9999"){ 
	   $.ajax({
	     type : 'post',
	     url : v_url,
	     data : 'v_plan_no=' + v_item_select,
	     cache : false,
	     async : true,
	     success : function(html) {
	       $("#GE_disclosure").html('');
	       $("#GE_disclosure").append(html);
	       window.print();
	     }
	   });
	 }

	}


function openPrintWindow(p_srvName,p_plan_number){
  window.open ("http://"+p_srvName+"/disclosure_print.php?plan=" + p_plan_number ,
			"Golfsmith","menubar=0,resizable=1,width=500,height=550"); 	
	
}

function validate_QBD(p_screen_name){
	var v_limit = $("#qbd_limit").val();
	var v_qty   = $("#qty").val();	
	var v_style = $("#stynum").val();
	var v_qbd_price = $("#qbd_price").val();
	var v_qbd_long_desc = $("#qbd_long_desc").val();
	v_qbd_long_desc = escape(v_qbd_long_desc);

	if (parseInt(v_qty) > parseInt(v_limit)) {
		alert('Sorry, the quantity limit for this Hot Deal item is ' + v_limit + ' per customer. Please reduce your quantity and try again. Please click the "OK" button to continue.');
		return false;
	}
	
	//now check atp
	if(p_screen_name != '') {
		   v_url = '/' + p_screen_name + '/products/validateqbd/';
	} else {
		   v_url = '/products/validateqbd/';
	}
	

	var v_err = 'N';

	  $.ajax( {
	      type : 'post',
	      url : v_url,
	      data : 'qbd_ordered_qty=' + v_qty + '&qbd_price=' + v_qbd_price + '&qbd_long_desc=' + v_qbd_long_desc + '&qbd_style=' + v_style,
	      cache : false,
	      async : false,
	      beforesend : function() {
	      },
	      success : function(v_html) {
	    	  
	    	  if (v_html != 'Success')
	    	  {
	    		  alert(v_html);
	    		  v_err = 'Y';	    		  
	    		  
	    	  } else {
	    		  v_err = 'N';
	    		  
	    	  }	    	  
	      }
	    });
	
	if (v_err == 'N') {
		return validate_ATP(p_screen_name);
	} else {		
		location.reload(true);
		return false;
	}
		
	
}


function validate_ATP(p_screen_name){
	
	var v_qty   = $("#qty").val();
	var v_iid   = $("#inv_id").val();
	var v_style = $("#stynum").val();
	var v_site = $("#site_used").val();
	var v_org_id  = 25;
	
	//now check atp
	if(p_screen_name != '') {
		   v_url = '/' + p_screen_name + '/products/validateatp/';
	} else {
		   v_url = '/products/validateatp/';
	}
	
	if (v_site != 'US') {
		v_org_id = 173;
	}

	var v_err = 'N';
	  $.ajax( {
	      type : 'post',
	      url : v_url,
	      data : 'ordered_qty=' + v_qty + '&style=' + v_style + '&inv_id=' + v_iid + '&org_id=' + v_org_id,
	      cache : false,
	      async : false,
	      beforesend : function() {
	      },
	      success : function(v_html) {
	    	  
	    	  if (v_html != 'Success')
	    	  {
	    		  alert(v_html);
	    		  v_err = 'Y';	    		  
	    		  
	    	  } else {
	    		  v_err = 'N';
	    		  
	    	  }	    	  
	      }
	    });

	if (v_err == 'N') {
		return true;
	} else {		
		location.reload(true);
		return false;
	}
		
	
}


function validate_cart_qtys(p_screen_name) {
	
	var v_ret = validate_cart_QBD(p_screen_name);	
	
	if (v_ret == true) {
		
		validateOrderQtys(p_screen_name);
		
	}else {
		
		location.reload(true);
		return false;
	}
}

function validate_cart_QBD(p_screen_name){

	//now check atp
	if(p_screen_name != '') {
		   v_url = '/' + p_screen_name + '/checkout/validateorderqbd/';
	} else {
		   v_url = '/checkout/validateorderqbd/';
	}
	

	var v_err = 'N';

	  $.ajax( {
	      type : 'post',
	      url : v_url,
	      cache : false,
	      async : false,
	      beforesend : function() {
	      },
	      success : function(v_html) {
	    	  
	    	  if (v_html != 'Success')
	    	  {
	    		  alert(v_html);
	    		  v_err = 'Y';	    		  
	    		  
	    	  } else {
	    		  v_err = 'N';
	    		  
	    	  }	    	  
	      }
	    });

	if (v_err == 'N') {
		return true;
	} else {			
		return false;
	}
		
	
}

// Product page AddtoCart Popup

function showAddToCartPopup(p_screen_name,p_service_type,p_action){
  var v_formValidate=add_to_cart('');
  if(v_formValidate){
    if(p_action=="regular"){
      var v_validate_ATP=validate_ATP('');
      if(v_validate_ATP){
        addToCartPopup(p_screen_name,p_service_type);
      }
    }else{
      var v_validate_QBD=validate_QBD('');
      if(v_validate_QBD){
        addToCartPopup(p_screen_name,p_service_type);
      }
    }
  }
}

	  
function addToCartPopup(p_screen_name,p_service_type){
	
  if(p_screen_name != '') {
    v_url = '/' + p_screen_name + '/addtocart/addtocartview/';
  }else {
    v_url = '/addtocart/addtocartview';
  }
  
  if ($("#"+p_service_type+"_checkbox").attr('checked')) {
	  v_service_item =$("#"+p_service_type+"_checkbox").val();
  }else{
      v_service_item='';
  }
 
  v_desc=$("#sdes").val();
  v_qty=$("#qty").val();
  v_sku=$("#sku").val();
  v_xref=$("#xref").val();
  v_iid=$("#inv_id").val();
  v_lcode=$("#lcode").val();
  v_style=$("#stynum").val();
  v_forecast=$("#fcst").val();
  v_segment2 =$("#seg2").val();
  v_segment3 =$("#seg3").val();
  v_segment4 =$("#seg4").val();
  v_segment5 =$("#seg5").val();
  v_segment6 =$("#seg6").val();
  v_segment7 =$("#seg7").val();
  v_segment8 =$("#seg8").val();
  v_segment9 =$("#seg9").val();
  v_segment10 =$("#seg10").val();
    
  v_click_seq=$("#cseq").val();
  v_per_item=$("#per_item").val();
  v_record_sequence=$("#record_sequence").val();
  
  $.ajax( {
    type : 'post',
    url : v_url,
    data : 'style_number=' + v_style + '&description=' + v_desc + '&qty=' + v_qty + '&item_id=' + v_iid + '&record_sequence=' + v_record_sequence + '&per_item=' + v_per_item + '&seg2='+ v_segment2 + '&seg3=' + v_segment3 + '&seg4=' + v_segment4 + '&seg5=' + v_segment5 + '&seg6=' + v_segment6 + '&seg7=' + v_segment7 + '&seg8=' + v_segment8 + '&seg9=' + v_segment9 + '&seg10=' + v_segment10 + '&sku='+v_sku + '&service_item=' + v_service_item + '&xref='+ v_xref + '&lcode='+ v_lcode + '&cseq='+v_click_seq + '&fcst=' + v_forecast,
    cache : false,
    async : false,
    beforesend : function() {
	
    },
    success : function(v_html) {
      $("#modal_added2cart").empty();
      $("#modal_added2cart").append(v_html);
      
      $("#modal_added2cart").hide()
        .dialog({
		  autoOpen: false,
		  closeText: 'Close',
		  disabled: true,
		  maxHeight: 600,
		  modal: true,
		  resizable: false,
		  height:570,
		  width: 650
		});
      $("#modal_added2cart").dialog("open");
      $(".ui-widget-overlay").click(function(){
    	  modelAdded2CartClose();
    	});
      $(".ui-dialog-titlebar-close").click(function() {
        $("#modal_added2cart").dialog("close");
          location.reload(true);
      });
	}
  });
    return false;
}

function modelAdded2CartClose(){
  $("#modal_added2cart").dialog("close");
  location.reload(true);  
}

function showSecurityCodePopup(){
  $("#answer").show(1000);  
}


function hideSecurityCodePopup(){
  $("#answer").hide(1000);  
}


/* International Shipping Cost Calculator Code Starting Point*/
function getIntlShippingRate(p_screen_name){  
	
  v_weight = $("#EstimatedWeight").val();
  v_country = $("#Country").val();
  v_shipping= $("#ShippingMethod").val();

  if(p_screen_name != '') {
	v_url = '/' + p_screen_name + '/intl-shipping-calc/calculate/';
  } 
  else {
	v_url = '/intl-shipping-calc/calculate/';
  }
	  
  $.ajax({
  type: 'post',
  url: v_url,
  data: 'country=' + v_country + '&ship=' + v_shipping + '&weight=' + v_weight,
  success:function(html){   
    if(html!='N/A.'){ // if it returns the legit value
	  $("#resDiv").empty();
	  $("#resDiv").append("$"+html);
	      
	  getExchangeRate(html,p_screen_name); // calling function for currency conversion
    }
    else if(html=='N/A.' || html==''){ // if it returns either N/A. or Blank or other values
      $("#resDiv").empty();
	  $("#resDiv").append(html);
	  $("#showValue").empty();
    }
  }
  });
	
  return false;
}

/* Loads the International Shipping Cost Calculator in a div with id 'ISC_body' */
function loadISC(){
  $(document).ready(function(){	  
    $("#ISC_body").load('/intl-shipping-calc/');	
  }
  );
}

function getExchangeRate(p_dollars,p_screen_name){
  v_amt=parseInt(p_dollars);// converting string amount in to integer
  v_convert=document.getElementById("Country").value;
		
  if(p_screen_name != '') {
	v_url = '/' + p_screen_name + '/intl-shipping-calc/changecurrency/';
  } 
  else {
	v_url = '/intl-shipping-calc/changecurrency/';
  }
		
  $.ajax({
  type: 'post',
  url: v_url,
  data: 'convert_to=' + v_convert + '&amt=' + v_amt,
  success:function(html){
    $("#showValue").empty();
    $("#showValue").append(html);
  }
  });

  return false;
}
/* End of Shipping Calculator Code*/


