﻿var bValidateShippingAddress = false;
var freebiePointsLeft = 0;

var moveImage = null;
var moveTop = 0;
var moveLeft = 0;

var ie = document.all && document.getElementById;
var ns = document.getElementById && !document.all;

var lastSoundClipId = -1;

function ConfirmDeleteInventoryItem(sender, args) {
	args.IsValid = confirm("Are you sure you want to delete this item?");
}

function ConfirmDeleteShippingRange(sender, args) {
	args.IsValid = confirm("Are you sure you want to delete this shipping range?");
}

function playTrack(sender, trackId, name) {
	//	if (lastSoundClipId != -1) {
	//		img = document.getElementById("SoundClip_" + lastSoundClipId);
	//		if (img != null) stopTrack(img, lastSoundClipId);
	//	}

	BatmoAudioPop("RadRockers.com - " + name, "SoundClip" + trackId + ".mp3", "1");

	//	sender.attributes["onclick"].value = "stopTrack(this, " + trackId + ")";
	//	sender.src = "Images/SoundClipButtonStop.png";
	//lastSoundClipId = trackId;
}

function MaskInputDecimal(e) {
	var charCode;
	var targetElem;
	ie ? charCode = e.keyCode : charCode = e.which;
	ie ? targetElem = e.srcElement : targetElem = e.target;

	// Make a special exception for the backspace key #8 and . key #46
	if (charCode == 8) {
		return true;
	}
	if ((charCode == 46) && (document.getElementById(targetElem).value.indexOf('.') < 0)) {
		return true;
	}

	// Disallow all keys accept for numbers and . or / or -
	// Char codes 45-57 encompass most keypad codes
	// which match the same for numeric stip above standard letters
	if ((charCode > 57) || (charCode < 48)) {
		return false;
	}
}

function __doNamelessPostBack(eventTarget, eventArgument) {
	var theform;
	if (window.navigator.appName.toLowerCase().indexOf("microsoft") > -1) {
		theform = document.getElementById("frmDefault");
	}
	else {
		theform = document.forms["frmDefault"];
	}

	theform.__EVENTTARGET.value = eventTarget.split("$").join(":");
	theform.__EVENTARGUMENT.value = eventArgument;
	theform.submit();
}

function OrderFinalize() {
	var buttonName = "ctl00_mContentPlaceHolder_mFinalizeOrderButton";
	var waitingMessage = "Please wait while we process your order...<br/>This may take a moment.";

	if (document.getElementById) {
		vista = (document.getElementById(buttonName).style.visibility == 'hidden') ? 'visible' : 'hidden';
		document.getElementById(buttonName).style.visibility = vista;
		document.getElementById("mFinalizeNotice").innerHTML = waitingMessage;
	}
	else if (document.all) {
		vista = (document.all[buttonName].style.visibility == 'hidden') ? 'visible' : 'hidden';
		document.all[buttonName].style.visibility = vista;
		document.all["mFinalizeNotice"].innerHTML = waitingMessage;
	}

	return true;
}

function EnsureShippingAddress() {
	var shipinput = document.getElementById("mShippingInputTable");

	if (shipinput.style.display == "none") {
		document.getElementById("mShippingFirstNameTextBox").value = document.getElementById("mFirstNameTextBox").value;
		document.getElementById("mShippingLastNameTextBox").value = document.getElementById("mLastNameTextBox").value;
		document.getElementById("mShippingAddress1TextBox").value = document.getElementById("mAddress1TextBox").value;
		document.getElementById("mShippingAddress2TextBox").value = document.getElementById("mAddress2TextBox").value;
		document.getElementById("mShippingCityTextBox").value = document.getElementById("mCityTextBox").value;
		document.getElementById("mShippingPostalCodeTextBox").value = document.getElementById("mPostalCodeTextBox").value;
		document.getElementById("mShippingPhoneTextBox").value = document.getElementById("mPhoneTextBox").value;

		document.getElementById("mShippingStateDropDown").options[document.getElementById("mStateDropDown").selectedIndex].selected = true;
		document.getElementById("mShippingCountryDropDown").options[document.getElementById("mCountryDropDown").selectedIndex].selected = true;
	}
}

function CheckPasswordForLogin(source, args) {
	if (document.getElementById("mExistingCustomerRadioButton").checked) {
		if (document.getElementById("mPasswordTextBox").value.length < 1) {
			args.IsValid = false;
		}
	}
}

function ShowCVV2Explanation() {
	window.open("AboutCVV2.aspx", "winAboutCvv", "width=550,height=500,scrollbars,resizable");
}

function ClearText() {
	var searchfield = document.getElementById("ctl00_mSearchTextBox");
	if (searchfield.value == "[Search]") {
		searchfield.value = "";
	}
	else {
		searchfield.select()
	}
}

function SetText(str) {
	var searchfield = document.getElementById("ctl00_mSearchTextBox");
	if (searchfield.value.length < 1) {
		searchfield.value = str;
	}
}

function SearchStock() {
	var searchlink = document.getElementById("ctl00_mSearchLink");
	var searchfield = document.getElementById("ctl00_mSearchTextBox");
	var searchoption = document.getElementById("ctl00_mSearchType");

	var searchaction = searchlink.href + "?" + searchoption.options[searchoption.selectedIndex].value;
	searchaction += "=" + searchfield.value + "&o=0&s=&f=";
	location.href = searchaction;

	return false;
}

function EnterSearchStock(e) {
	var keynum
	var keychar
	var numcheck

	if (window.event) // IE
	{
		keynum = e.keyCode
	}
	else if (e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which
	}

	if (keynum == 13) { SearchStock(); }
}

function ToggleShippingInput(pShow) {
	var shipinput = document.getElementById("mShippingInputTable");
	bValidateShippingAddress = pShow;

	if (pShow) {
		shipinput.style.display = "";

		document.getElementById("mShippingFirstNameTextBox").value = "";
		document.getElementById("mShippingLastNameTextBox").value = "";
		document.getElementById("mShippingAddress1TextBox").value = "";
		document.getElementById("mShippingAddress2TextBox").value = "";
		document.getElementById("mShippingCityTextBox").value = "";
		document.getElementById("mShippingPostalCodeTextBox").value = "";
		document.getElementById("mShippingPhoneTextBox").value = "";

		document.getElementById("mShippingStateDropDown").options[0].selected = true;
		document.getElementById("mShippingCountryDropDown").options[0].selected = true;
	}
	else {
		shipinput.style.display = "none";

		document.getElementById("mShippingFirstNameTextBox").value = document.getElementById("mFirstNameTextBox").value;
		document.getElementById("mShippingLastNameTextBox").value = document.getElementById("mLastNameTextBox").value;
		document.getElementById("mShippingAddress1TextBox").value = document.getElementById("mAddress1TextBox").value;
		document.getElementById("mShippingAddress2TextBox").value = document.getElementById("mAddress2TextBox").value;
		document.getElementById("mShippingCityTextBox").value = document.getElementById("mCityTextBox").value;
		document.getElementById("mShippingPostalCodeTextBox").value = document.getElementById("mPostalCodeTextBox").value;
		document.getElementById("mShippingPhoneTextBox").value = document.getElementById("mPhoneTextBox").value;

		document.getElementById("mShippingStateDropDown").options[document.getElementById("mStateDropDown").selectedIndex].selected = true;
		document.getElementById("mShippingCountryDropDown").options[document.getElementById("mCountryDropDown").selectedIndex].selected = true;
	}
}

function JumpMenu(targ, selObj) {
	var tempURL = selObj.options[selObj.selectedIndex].value;
	document.location.href = "#TopicId" + tempURL;
	selObj.selectedIndex = 0;
}

function UnsubscribeAddress() {
	var unlink = document.getElementById("PageHeader1_UnsubscribeLink");
	var addybox = document.getElementById("PageHeader1_EmailTextBox");
	if (addybox.value.length > 0) {
		if (addybox != null && unlink != null) {
			addy = unlink.href + "?action=unsubscribe&email=" + addybox.value;
			location.href = addy;
		}
	}
	else {
		alert("You must enter an email address to unsubscribe!");
	}
	return false;
}

function SubscribeAddress() {
	var unlink = document.getElementById("PageHeader1_SubscribeLink");
	var addybox = document.getElementById("PageHeader1_EmailTextBox");
	if (addybox.value.length > 0) {
		if (addybox != null && unlink != null) {
			addy = unlink.href + "?action=subscribe&email=" + addybox.value;
			location.href = addy;
		}
	}
	else {
		alert("You must enter an email address to subscribe!");
	}
	return false;
}

function UpdateFreebiePoints() {
	var availpoints = document.getElementById("mFreebiePoints");

	if (availpoints != null) {
		freebiePointsLeft = parseInt(availpoints.value);
		var isFreebiePage = (window.top.location.pathname.toLowerCase().indexOf("selectfreebies") > -1)

		//alert("isFreebiePage="+isFreebiePage+" : freebiePointsLeft="+freebiePointsLeft);

		if (isFreebiePage && freebiePointsLeft < 1) {
			var arInputs = window.top.document.body.getElementsByTagName("input");
			for (i = 0; i < arInputs.length; i++) {
				if (arInputs[i].type == "checkbox" && !arInputs[i].checked) {
					arInputs[i].disabled = true;
				}
			}
		}
		else if (isFreebiePage && freebiePointsLeft > 0) {
			var arInputs = window.top.document.body.getElementsByTagName("input");
			for (i = 0; i < arInputs.length; i++) {
				if (arInputs[i].type == "checkbox") {
					arInputs[i].disabled = false;
				}
			}
		}
	}
}

function ToggleFreebie(Pnts, Itm) {
	var availpoints = top.frames["_cart"].document.getElementById("mFreebiePoints").value;
	var addlink = document.getElementById("PageHeader1_mHiddenCartUrl");

	if (parseInt(availpoints) < parseInt(Pnts)) {
		alert("You dont have enough points for this item!");
		return false;
	}

	if (addlink != null) {
		addy = addlink.value + "?action=togglefreebie&iid=" + Itm + "&points=" + Pnts;
		top.frames["_cart"].ReplacePage(addy);
	}
}

function AddToWishList(Itm, ret) {
	var wllink = document.getElementById("PageHeader1_mHiddenCartUrl");
	if (wllink != null) {
		addy = wllink.value + "?action=addtowishlist&iid=" + Itm;
		top.frames["_cart"].ReplacePage(addy);
	}

	if (ret)
		return false;
	else {
		RemoveFromCart(Itm);
		return false;
	}
}

function AddToCart(Itm) {
	var addlink = document.getElementById("PageHeader1_mHiddenCartUrl");
	var qty = document.getElementById("txtQuantity");

	if (qty != null && addlink != null) {
		addy = addlink.value + "?action=addtocart&iid=" + Itm + "&qty=" + qty.value;
		qty.value = 1;
		top.frames["_cart"].ReplacePage(addy);
	}
	return false;
}

function AddToCartFromList(Index, Itm) {
	var addlink = document.getElementById("PageHeader1_mHiddenCartUrl");
	var qty = document.getElementById("dgProducts__ctl" + Index + "_txtQuantity");

	MoveItem(Index, 0);

	if (qty != null && addlink != null) {
		addy = addlink.value + "?action=addtocart&iid=" + Itm + "&qty=" + qty.value;
		qty.value = 1;
		top.frames["_cart"].ReplacePage(addy);
	}
	return false;
}

function RemoveFromCart(Itm) {
	var addlink = document.getElementById("PageHeader1_mHiddenCartUrl");

	if (addlink != null) {
		addy = addlink.value + "?action=removefromcart&iid=" + Itm;
		top.frames["_cart"].ReplacePage(addy);
	}
}

function UpdateCartStats() {
	var newstats = document.getElementById("SessionCartStats");
	var bagstats = window.top.document.getElementById("PageHeader1_BagStats");

	bagstats.innerHTML = newstats.innerHTML;
}

function OrderDetail(IVN) {
	window.open("OrderHistoryDetails.aspx?id=" + IVN, "RROrderDetailView", "toolbar=0,status=0,location=0,menubar=0,resizable=0,height=700,width=585,scrollbars=1");
}

function SignInPrompt() {
	var loginlink = document.getElementById("PageHeader1_mLoginLink");
	if (loginlink != null && confirm("You must be signed in to use this feature!\n\n- Press OK to sign in now\n- Press Cancel to return to the page")) {
		location.href = loginlink.href;
	}
	return false;
}

function MoveItem(Index, Step) {
	//Step = Step+1;
	if (Step == 0) {
		var imgelem = document.getElementById("dgProducts__ctl" + Index + "_Image1").firstChild;
		var newimg = new Image();

		if (moveImage != null) {
			moveImage.style.display = "none";
			moveImage = null;
		}

		newimg.src = imgelem.src;
		newimg.style.top = GetTop(imgelem) + "px";
		newimg.style.left = GetLeft(imgelem) + "px";
		newimg.style.position = "absolute";
		newimg.id = "floatingImage";
		newimg.name = "floatingImage";
		newimg.style.height = 45;
		newimg.style.width = 45;

		moveImage = newimg;
		document.body.appendChild(moveImage);
	}
	else {
		moveImage.style.top = (GetTop(moveImage) - (GetTopDifference(moveImage) / 10)); // +"px";
		moveImage.style.left = (GetLeft(moveImage) + (GetLeftDifference(moveImage) / 10)); // +"px";

		if (parseInt(moveImage.style.height) > 0)
			moveImage.style.height = parseInt(moveImage.style.height) - 0.1;
		if (parseInt(moveImage.style.width) > 0)
			moveImage.style.width = parseInt(moveImage.style.width) - 0.1;
	}

	if (GetCartTop() < parseInt(moveImage.style.top)) {
		setTimeout("MoveItem(" + Index + ", " + parseInt(Step + 1) + ")", 35);
	}
	else {
		clearTimeout();
		moveImage.style.display = "none";
	}
}

function GetCartTop() {
	var cartFrame = document.getElementById("PageHeader1_BagStats");
	return GetTop(cartFrame);
}

function GetCartLeft() {
	var cartFrame = document.getElementById("PageHeader1_BagStats");
	return GetLeft(cartFrame);
}

function GetTopDifference(elem) {
	return parseInt(GetTop(elem) - GetCartTop());
}

function GetLeftDifference(elem) {
	return parseInt(GetCartLeft() - GetLeft(elem));
}

function GetTop(elem) {
	var retVal = 0;
	if (elem != null) {
		if (elem.tagName != "body") retVal = elem.offsetTop;
		if (elem.offsetParent != null) {
			retVal = (retVal + GetTop(elem.offsetParent));
		}
	}
	return retVal;
}

function GetLeft(elem) {
	var retVal = 0;
	if (elem != null) {
		retVal = elem.offsetLeft;
		if (elem.offsetParent != null) {
			retVal = (retVal + GetLeft(elem.offsetParent));
		}
	}
	return retVal;
}
