/*
 * Use to populate the state/country
 */

function populateState(stateId, data) {
	var state = $("#" + stateId)[0];
	// Delete all options that existed.
	state.options.length = 0;
	var options = data.split("|");
	var result = "";

	for (var i = 0; i < options.length; i++) {
		var temp = ($.trim(options[i])).split("=");
		state.options[i] = (new Option(temp[1], temp[0]));
	}
}
function inputState(inputObj, stateId) {
	var statObj = document.getElementById(stateId);
	changeSelectValue(statObj, inputObj);
}

function changeSelectValue(statObj, inputObj) {
	statObj.options.length = 0;
	statObj.options[0] = new Option(inputObj.value, inputObj.value);
}

function getStatesAndClass(countryCode, stateId, inputClass) {
	$.ajax({
				type : "Get",
				async : false,
				url : contextPath + "/global/util/stateRestriction.jsp",
				data : "countryCode=" + countryCode,
				dataType : "text",
				success : function(data) {
					var stateInputId = stateId + "_" + "state_Input";
					var style = "width:120px;";

					try {
						style = $("#" + stateId).attr("style");
					} catch (e) {
					}

					if ($.trim(data) == "NoState") {

						var sel = document.getElementById(stateId);
						var parent = sel.parentNode;
						var stateInput = document.getElementById(stateInputId);
						if (stateInput != null) {
							sel.style.display = "none";
							stateInput.style.display = "";
							inputState(stateInput, stateId);
						} else {
							var newInput = document.createElement("input");
							newInput.setAttribute("type", "text");
							newInput.setAttribute("id", stateInputId);
							newInput.setAttribute("style", style);
							newInput.style.width = sel.style.width;
							newInput.maxLength = 40;
							newInput.className = inputClass;
							var onclikFunction = "inputState(this,'" + stateId
									+ "')";
							newInput.onkeyup = function() {
								inputState(newInput, stateId);
							};
							parent.appendChild(newInput);
							sel.style.display = "none";
						}
						return;
					} else {
						var stateInput = document.getElementById(stateInputId);
						if (stateInput != null)
							stateInput.style.display = "none";
						var sel = document.getElementById(stateId);
						if (sel != null)
							sel.style.display = "";

					}
					try {

						populateState(stateId, data);
					} catch (e) {

					}
				},
				error : function() {

				}
			});
}

function getStates(countryCode, stateId) {
	$.ajax({
				type : "Get",
				async : false,
				url : contextPath + "/global/util/stateRestriction.jsp",
				data : "countryCode=" + countryCode,
				dataType : "text",
				success : function(data) {
					var stateInputId = stateId + "_" + "state_Input";
					var style = "width:120px;";

					try {
						style = $("#" + stateId).attr("style");
					} catch (e) {
					}

					if ($.trim(data) == "NoState") {

						var sel = document.getElementById(stateId);
						var parent = sel.parentNode;
						var stateInput = document.getElementById(stateInputId);
						if (stateInput != null) {
							sel.style.display = "none";
							stateInput.style.display = "";
							inputState(stateInput, stateId);
						} else {
							var newInput = document.createElement("input");
							newInput.setAttribute("type", "text");
							newInput.setAttribute("id", stateInputId);
							newInput.setAttribute("style", style);
							newInput.style.width = sel.style.width;
							newInput.maxLength = 40;
							var onclikFunction = "inputState(this,'" + stateId
									+ "')";
							newInput.onkeyup = function() {
								inputState(newInput, stateId);
							};
							parent.appendChild(newInput);
							sel.style.display = "none";
						}
						return;
					} else {
						var stateInput = document.getElementById(stateInputId);
						if (stateInput != null)
							stateInput.style.display = "none";
						var sel = document.getElementById(stateId);
						if (sel != null)
							sel.style.display = "";

					}
					try {

						populateState(stateId, data);
					} catch (e) {

					}
				},
				error : function() {

				}
			});
}

function onCountryChange(inAccountPage) {
	var country = $("#country").val();

	if ($("#areaCode").data("unSetup") == null) {
		$("#areaCode").blur(function() {
					if ($("#country").val() == "US") {
						var areaCode = $("#areaCode").val();
						if (areaCode.length > 3) {
							$("#areaCode").val(areaCode.substr(0, 3));
						}
					}
				});
		$("#areaCode").data("unSetup", false);
	}

	if ($("#phoneNumber").data("unSetup") == null) {
		$("#phoneNumber").blur(function() {
					if ($("#country").val() == "US") {
						var phoneNumber = $("#phoneNumber").val();
						if (phoneNumber.length > 7) {
							$("#phoneNumber").val(phoneNumber.substr(0, 7));
						}
					}
				}).change(function() {
					if ($("#country").val() == "US") {
						var phoneNumber = $("#phoneNumber").val();
						if (phoneNumber.length > 7) {
							$("#phoneNumber").val(phoneNumber.substr(0, 7));
						}
					}
				});
		$("#phoneNumber").data("unSetup", false);
	}

	if (country == "US") {
		$("#countryCode").hide();
		$("#state").show();
		$("#province").hide();
		$("#provinceLabel").hide();
		$("#stateLabel").show();
		$("#countryCode").val('');
		$("#areaCode").attr("maxLength", "3");
		$("#phoneNumber").attr("maxLength", "7");
		var areaCode = $("#areaCode").val();
		var phoneNumber = $("#phoneNumber").val();
		if (areaCode.length > 3) {
			$("#areaCode").val(areaCode.substr(0, 3));
		}
		if (phoneNumber.length > 7) {
			$("#phoneNumber").val(phoneNumber.substr(0, 7));
		}
		$("#popboxDiv").show();
		$("#phoneNumberLabel").html("Phone Number:");
	} else {
		$("#countryCode").show();
		$("#province").show();
		$("#state").hide();
		$("#provinceLabel").show();
		$("#stateLabel").hide();
		$("#areaCode").attr("maxLength", "5");
		$("#phoneNumber").attr("maxLength", "10");
		$("#popboxDiv").hide();
		if(inAccountPage == true)
		{
			$("#phoneNumberLabel").html("Phone Number:<span class=\"orange\">*</span>");
		}else
		{
			$("#phoneNumberLabel").html("Phone Number:*");
		}
	}
}

var currentForm = "";
function hidSelection() {
	if (BrowserDetect.browser == "Explorer") {
		$("select").each(function() {
					$(this).hide();
				});
		$("select", document.getElementById(currentForm)).each(function() {
					$(this).show();
				});
		onCountryChange();
	}
}

function popUpRemoteVarifyMessage(buttonId) {
	$("#" + buttonId).removeAttr("disabled");
	if ($("#onlyRemoteVarifyFail").val() == "true") {
		popupModalWindowAC(contextPath + '/popup/confirmInvalidateAddress.jsp',
				'popup_ac_window', {
					'buttonId' : buttonId,
					'type' : 'normal'
				});
	}
}

function popUpRemoteVarifyMessageBill(buttonId) {
	$("#" + buttonId).removeAttr("disabled");
	if ($("#onlyRemoteVarifyFailBill").val() == "true") {
		popupModalWindowAC(contextPath + '/popup/confirmInvalidateAddress.jsp',
				'popup_ac_window', {
					'buttonId' : buttonId,
					'type' : 'bill'
				});
	}
}

function popUpRemoteVarifyMessageShip(buttonId) {
	$("#" + buttonId).removeAttr("disabled");
	if ($("#onlyRemoteVarifyFailShip").val() == "true") {
		popupModalWindowAC(contextPath + '/popup/confirmInvalidateAddress.jsp',
				'popup_ac_window', {
					'buttonId' : buttonId,
					'type' : 'ship'
				});
	}
}

function popUpRemoteVarifyMessage_confirm(buttonId, type) {
	if (type == "normal") {
		closeModalWindowAC('popup_ac_window');
		$("#needRemoteVerify").val("false");
		$("#" + buttonId).click();
	} else if (type == "bill") {
		closeModalWindowAC('popup_ac_window');
		$("#needRemoteVerifyBill").val("false");
		$("#" + buttonId).click();
	} else if (type == "ship") {
		closeModalWindowAC('popup_ac_window');
		$("#needRemoteVerifyShip").val("false");
		postCheckoutForm('shippingForm', null, 'shippingFormButton');
	}
}

function popupModalWindowAC(url, popId, param) {
	var wscrollLeft = $(window).scrollLeft();
	var wscrollTop = $(window).scrollTop();
	var popwin_left = $(window).width() / 2 - 200;
	popwin_top = $(window).height() / 2 - 100;
	popwin_left = popwin_left + wscrollLeft;
	popwin_top = popwin_top + wscrollTop;
	popupModalWindowACOpen(url, popId, popwin_top + 'px', popwin_left + 'px',
			param);
}

function popupModalWindowACOpen(url, popId, popwin_top, popwin_left, param) {
	if (BrowserDetect.browser == "Explorer") {
		$("select").each(function() {
					$(this).hide();
				});
	}

	var modalElement = document.getElementById("popup_ac_modal")
	if (modalElement == null) {
		var body = document.getElementsByTagName("body")[0];
		modalElement = document.createElement("div");
		modalElement.id = "popup_ac_modal";
		body.appendChild(modalElement);
	}
	modalElement.style.display = "block";
	modalElement.style.height = $(document).height() + "px";

	var otherModalElement = $("#popup_modal");
	if (otherModalElement.css("display") == "block") {
		otherModalElement.css("-moz-opacity", "0.2");
		otherModalElement.css("opacity", "0.2");
		$(modalElement).css("-moz-opacity", "0.2");
		$(modalElement).css("opacity", "0.2");
	}

	var popElement = document.getElementById(popId);
	popElement.style.display = "block";
	popElement.style.top = popwin_top;
	popElement.style.left = popwin_left;

	$("div#" + popId).load(url, param);
}

function closeModalWindowAC(popId) {
	var modalElement = document.getElementById("popup_ac_modal")
	modalElement.style.display = "none";
	var popElement = document.getElementById(popId);
	popElement.style.display = "none";
	popElement.innerHTML = "";
	if (BrowserDetect.browser == "Explorer") {
		$("select").each(function() {
					$(this).show();
				});
	}

	var otherModalElement = $("#popup_modal");
	if (otherModalElement != null) {
		otherModalElement.css("-moz-opacity", "0.4");
		otherModalElement.css("opacity", "0.4");
		hidSelection();
	}
	$(modalElement).css("-moz-opacity", "0.4");
	$(modalElement).css("opacity", "0.4");
	onCountryChange();
}

