﻿var isPostBack = false;
var internationalShippingErrorMessage = "Sorry, we cannot ship to an address outside of the United States or Canada.";
var partsUSOnlyShippingErrorMessage = "One or more of the items in your cart can only be shipped within the United States.";
var noAPOShippingErrorMessage = "Sorry, we cannot ship to a military post office.";



/********** Show "what's this" popup *********/
var popupUniqueID;

if (window.jQuery !== null && window.jQuery !== undefined)
{
	$(document).ready(function()
	{
		if ($("div#CVVPopup") !== undefined && $("a#CVVPopupLink") !== undefined && $("div#CVVPopup") !== null && $("a#CVVPopupLink") !== null)
		{
			$("div#CVVPopup").alignAboveLeft($("a#CVVPopupLink"));
		}
	});
}

function OnClickShowPopup(e,popupID)
{
	var popup = document.getElementById(popupID);
	
	// check the click count to see if they were clicking the link while the element was showing
	if (popup !== null && popup !== undefined && (popup.style.visibility == "hidden" || popup.style.visibility == ""))
	{
		popup.style.visibility = "visible";
		popup.style.zIndex = "999";
	}
	popupUniqueID = popupID;
	if (!e) var e = window.event;
	e.cancelBubble = true;
	if (e.stopPropagation) e.stopPropagation();

}

function OnClickHidePopup()
{
	var popup = document.getElementById(popupUniqueID);
	if (popup !== null && popup !== undefined && popup.style.visibility == "visible")
	{
		popup.style.visibility = "hidden";
	}
}

if (window.addEventListener)
{
	// set for non ie
	window.addEventListener("click", OnClickHidePopup, 1);
}
else if (window.attachEvent)
{
	// set for ie
	document.attachEvent("onclick", OnClickHidePopup);
}
/********** END Show "what's this" popup *********/


function toggleShippingInfoPanel(command)
{
	var hasCartItemParts = HasCartItemPartsHidden.value;
	if (hasCartItemParts == "true")
	{
		toggleShippingInfoPanelParts(command);
	}
	else
	{
		if (command == "on")
		{
			if (billingCountry.value == "001" || billingCountry.value == "034")
			{
				setShippingInformation();
			}
			shippingInfoPanel.style["display"] = "block";
		}
		else if (command == "off")
		{
			if (billingCountry.value != "001" && billingCountry.value != "034")
			{
				alert(internationalShippingErrorMessage);
				shippingInfoShowing.checked = true;
				return false;
			}
			else if (isAPO(billingState.value))
			{
				alert(noAPOShippingErrorMessage);
				shippingInfoShowing.checked = true;
				return false;
			}
			else
			{
				shippingInfoPanel.style["display"] = "none";
				setShippingInformation();
			}
		}
		else
		{
			shippingInfoShowing.checked = true;
			shippingInfoPanel.style["display"] = "block";
			setShippingInformation();
		}
	}
}

function toggleShippingInfoPanelParts(command)
{
	if (command == "on")
	{
		if (billingCountry.value == "001")
		{
			setShippingInformation();
		}
		shippingInfoPanel.style["display"] = "block";
	}
	else if (command == "off")
	{
		if (billingCountry.value != "001")
		{
			alert(partsUSOnlyShippingErrorMessage);
			shippingInfoShowing.checked = true;
			return false;
		}
		else if(isAPO(billingState.value))
		{
			alert(noAPOShippingErrorMessage);
			shippingInfoShowing.checked = true;
			return false;
		}
		else
		{
			shippingInfoPanel.style["display"] = "none";
			setShippingInformation();
		}
	}
	else
	{
		shippingInfoShowing.checked = true;
		shippingInfoPanel.style["display"] = "block";
		setShippingInformation();
	}
}

// Set shipping information to current billing information
function setShippingInformation(checkout)
{
	// Only set information if shipping panel is displayed
	if('undefined' != typeof(shippingInfoPanel) && shippingInfoPanel.style["display"] == "none")
	{
		var shipElementArray = new Array();
		shipElementArray[0] = shippingFirstName;
		shipElementArray[1] = shippingLastName;
		shipElementArray[2] = shippingCompanyName;
		shipElementArray[3] = shippingAddrLine1;
		shipElementArray[4] = shippingAddrLine2;
		shipElementArray[5] = shippingCityName;
		shipElementArray[6] = shippingZipCode;
		shipElementArray[7] = shippingDayPhoneNumber;
		shipElementArray[8] = shippingPhoneExtension;
		
		var billingElementArray = new Array();
		billingElementArray[0] = billingFirstName;
		billingElementArray[1] = billingLastName;
		billingElementArray[2] = billingCompanyName;
		billingElementArray[3] = billingAddrLine1;
		billingElementArray[4] = billingAddrLine2;
		billingElementArray[5] = billingCityName;
		billingElementArray[6] = billingZipCode;
		billingElementArray[7] = billingDayPhoneNumber;
		billingElementArray[8] = billingPhoneExtension;
						
		for(var i = 0; i < billingElementArray.length; i++)
		{
			shipElementArray[i].value = billingElementArray[i].value;
		}
		
		var billingCountryText = billingCountry[billingCountry.selectedIndex].text;
		var count = 0;
		
		while(shippingCountry[count] != null)
		{
			if(billingCountryText == shippingCountry[count].text)
			{
				shippingCountry.options[count].selected = true;
			}
			count++;
		}

		if (typeof (checkout) != 'undefined' && !checkout)
		{
			// set shipping states
			shippingCountryChanged();
		}
				
		// set billing state
		billingStateChanged();
	}
	
	// validate radio button checked
	if(checkout == true)
	{
		if(shippingInfoHidden.checked != true && shippingInfoShowing.checked != true)
		{
			alert("Please select a shipping option");
			return false;
		}
		else if(billingState.value == "")
		{
			alert("Please select your billing state/province.");
			return false;
		}
		else if(billingZipCode.value == "" && (billingCountry.value == "001" || billingCountry.value == "034"))
		{
			// allow no zip code if billing country is not U.S. or Canada
			alert("Please type your billing postal code");
			return false;
		}
		else
		{
			//invoke required field validators
			WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(proceedCheckout.name, "", true, "", "", false, false));
			return true;
		}
	}
}

function billingStateChanged()
{
	var stateID = billingState.value;
	if (typeof (stateID) != 'undefined' && stateID != "" && isAPO(stateID))
	{
		toggleShippingInfoPanel();
	}

	count = 0;

	var shippingStateTemp = document.getElementById(shippingState.id);

	while (shippingStateTemp[count] != null)
	{
		if (billingState.value == shippingStateTemp[count].value)
		{
			shippingStateTemp.options[count].selected = true;
		}
		count++;
	}
}

function isAPO(stateID)
{
	return (stateID == "AA" || stateID == "AE" || stateID == "AP" || stateID == "MH" || stateID == "MP" || stateID == "PW");
}

// billing country drop down event handler for index change
function billingCountryChanged()
{
	var countryID = billingCountry.value;
	
	CheckoutWebService.UpdateBillingStateList(countryID, updateBillingStateCallback, AjaxError);
	
    if (countryID != "001" && countryID != "034")
    {
    	toggleShippingInfoPanel();
    }
}

// shipping country drop down event handler for index change
function shippingCountryChanged()
{ 
	var countryID = shippingCountry.value;
    CheckoutWebService.UpdateShippingStateList(countryID, updateShippingStateCallback, AjaxError);
}

function updateShippingStateCallback(result)
{   
    if(result != null)
    {
        var s = new Array();
		
        if (result.length > 0)
        {
			s[s.length] = "<select onchange=\"javascript:setShippingInformation();\" style=\"/* IE 7 Standards Mode */ *margin-right: 3px;\" name=\"" + shippingState.name + "\" id=\"" + shippingState.id + "\" class=\"stateSelect\">";
            s[s.length] = "<option value=\"\">Select a State</option>";
            for (var i=0; i < result.length; i++)
	        {  
				//if(shippingState.optoni  
	            s[s.length] = "<option value=\"" + result[i].StateCode + "\">" + result[i].StateName + "</option>";
            }
		    s[s.length] = "</select>";
    		
	        document.getElementById("shippingStateSpan").innerHTML = s.join("");

	        if (!isPostBack)
	        {
	        	shippingState.selectedIndex = 0;
	        }
	        else
	        {
				isPostBack = true;
	        }
        }
        else
        {
            s[s.length] = "<input style=\"/* IE 7 Standards Mode */ *margin-right: 3px;\" type=\"text\" name=\"" + shippingState.name + "\" id=\"" + shippingState.id + "\" class=\"addressName\">";
            
            document.getElementById("shippingStateSpan").innerHTML = s.join(""); 
        }
        
		count = 0;
		
		var shippingStateTemp = document.getElementById(shippingState.id);
		
		while(shippingStateTemp[count] != null)
		{
			if(billingState.value == shippingStateTemp[count].value)
			{
				shippingStateTemp.options[count].selected = true;
			}
			count++;
		}
		
    }
}

function updateBillingStateCallback(result)
{   
    var s = new Array();

    if(result != null)
    {
        if (result.length > 0)
        {
			s[s.length] = "<select onchange=\"javascript:setShippingInformation();\" style=\"/* IE 7 Standards Mode */ *margin-right: 3px;\" name=\"" + billingState.name + "\" id=\"" + billingState.id + "\" class=\"stateSelect\">";
            s[s.length] = "<option value=\"\">Select a State</option>";
            for (var i=0; i < result.length; i++)
	        {    
	            s[s.length] = "<option value=\"" + result[i].StateCode + "\">" + result[i].StateName + "</option>";
            }
		    s[s.length] = "</select>";
    		
	        document.getElementById("billingStateSpan").innerHTML = s.join("");
        }
        else
        {
            s[s.length] = "<input style=\"/* IE 7 Standards Mode */ *margin-right: 3px;\" type=\"text\" name=\"" + billingState.name + "\" id=\"" + billingState.id + "\" class=\"addressName\">";
            
            document.getElementById("billingStateSpan").innerHTML = s.join("");
        }
    }
    
    this.billingState = document.getElementById(billingState.id);
}

function AjaxError(error)
{
    alert("An error has occured while trying to process your request. Please refresh the browser and try again." + error);
}

function adjustCreditCard()
{
	var CreditCardNumber = window.document.getElementById("ctl00_ctl00_BodyCPH_CheckoutCPH_CreditCardNumber").value;
	var CreditCardTypeID = window.document.getElementById("credit_card_type_id");
	var cc_type = "";
	switch( CreditCardNumber.substring(0,1) )
	{
		case "4":
			cc_type = "VISA";
			break;
		case "5":
			cc_type = "MAST";
			break;
		case "6":
			cc_type = "DISC";
			break;
		case "3":
			cc_type = "AMEX";
			break;
		default:
			cc_type = "";
			break;
	}
	CreditCardTypeID.value = cc_type;
}

function PlaceOrder()
{
	var return_val = false;
	var orderSubmitted = window.document.getElementById("order_submitted");
	var creditCardText = document.getElementById("ctl00_ctl00_BodyCPH_CheckoutCPH_CreditCardNumber");
	var creditCardCVVText = document.getElementById("ctl00_ctl00_BodyCPH_CheckoutCPH_CVVNumber");
	if (orderSubmitted != null && orderSubmitted != 'undefined')
	{
		if(orderSubmitted.value == 1)
		{
			return_val = false;
		}
		else
		{
			orderSubmitted.value = 1; 
			return_val = true;
		}
	}
	else
	{
		return_val = true;
	}
	
	if((creditCardText != null && creditCardText != 'undefined' && creditCardText.value == "") || ( $('#trCVV').css('display') != 'none'  && creditCardCVVText != null && creditCardCVVText != 'undefined' && creditCardCVVText.value == ""))
	{
		alert ("Please input your credit card information before submitting.");
		return_val = false;
	}
	return return_val;
}

// Search Bar Functions
function setZipCodeEntryFocus()
{
	if( zipCodeEntry.value.length == 0 || zipCodeEntry.value == "Enter Zip Code"  )
	{
		zipCodeEntry.value = "";
		zipCodeEntry.style["color"] = "#000";
	}
}

function setZipCodeEntryBlur()
{
	if( zipCodeEntry.value.length == 0 )
	{
		zipCodeEntry.value = "Enter Zip Code";
		zipCodeEntry.style["color"] = "#666";
	}
}

function ShowValidZipCodeLabel()
{
	var isValidLabel = document.getElementById("isValidZipCodeLabel");
	if(Page_IsValid)
	{
		isValidLabel.style.visibility = "visible";
		isValidLabel.style.display = "block";
	}
	else
	{
		isValidLabel.style.visibility = "hidden";
		isValidLabel.style.display = "none";
	}
}

function ShowBMLPaypalButtons(caseName)
{
	var paypalExpressButton = $("div#paypalExpressButton");
	var spacers = $("div#spacers");
	var bmlButton = $("div#BMLCheckoutButton");

	switch(caseName)
	{
		case "ShowBoth":
			paypalExpressButton.css({ display: "block" });
			spacers.css({ display: "block" });
			bmlButton.css({ display: "block" });
			break;
		case "ShowBML":
			paypalExpressButton.css({ display: "none" });
			spacers.css({ display: "none" });
			bmlButton.css({ display: "block" });
			break;
		case "ShowPaypal":
			paypalExpressButton.css({ display: "block" });
			spacers.css({ display: "none" });
			bmlButton.css({ display: "none" });
			break;
		default:
			paypalExpressButton.css({ display: "none" });
			spacers.css({ display: "none" });
			bmlButton.css({ display: "none" });
			break;
	}
}




