var g_menu = new Array();
//- Trim -//
function Trim(st) {
	var len = st.length
	var begin = 0, end = len - 1;
	while (st.charAt(begin) == " " && begin < len) {
		begin++;
	}
	while (st.charAt(end) == " " && begin < end) {
		end--;
	}
	return st.substring(begin, end + 1);
}

//- FormatPhone -//
function FormatPhone(field) {
	field.value = Trim(field.value);

	var ov = field.value;
	var v = "";
	var x = -1;

	// is this phone number 'escaped' by a leading plus? 
	if (0 < ov.length && '+' != ov.charAt(0)) { // format it 
		// count number of digits 
		var n = 0;
		if ('1' == ov.charAt(0)) {  // skip it 
			ov = ov.substring(1, ov.length);
		}

		for (i = 0; i < ov.length; i++) {
			var ch = ov.charAt(i);

			// build up formatted number 
			if (ch >= '0' && ch <= '9') {
				if (n == 0) v += "(";
				else if (n == 3) v += ") ";
				else if (n == 6) v += "-";
				v += ch;
				n++;
			}
			// check for extension type section; 
			// are spaces, dots, dashes and parentheses the only valid non-digits in a phone number? 
			if (!(ch >= '0' && ch <= '9') && ch != ' ' && ch != '-' && ch != '.' && ch != '(' && ch != ')') {
				x = i;
				break;
			}
		}
		// add the extension 
		if (x >= 0) v += " " + ov.substring(x, ov.length);

		// if we recognize the number, then format it 
		if (n == 10 && v.length <= 40) field.value = v;
	}
	return true;
}

//- Console_AddRemove -//
function Console_AddRemove(oFromSet, oToSet, bIsAll) {
	//+ unselect
	for (var hOption = 0; hOption < oToSet.length; hOption++) {
		oToSet.options[hOption].selected = false;
	}
	//+ move
	for (hOption = oFromSet.length; hOption != 0; hOption--) {
		if ((bIsAll == true) || (oFromSet.options[hOption - 1].selected == true)) {
			oToSet.options[oToSet.length] = new Option(oFromSet.options[hOption - 1].text, oFromSet.options[hOption - 1].value, false, true);
			oFromSet.options[hOption - 1] = null;
		}
	}
}
//- Console_Pack -//
function Console_Pack(oSet, oField) {
	var cPack = '';
	for (var hOption = 0; hOption < oSet.length; hOption++) {
		cPack += ',' + oSet.options[hOption].value;
	}
	oField.value = cPack.substring(1);
}

//- DecodeZip -//
function DecodeZip(xZip) {
	var cZip = xZip.replace(/\?/g, '');
	return cZip;
}

//- DecodeFon -//
function DecodeFon(xFon) {
	var cFon = '';
	if (xFon.length >= 10) {
		cFon = xFon.substr(0, 3) + '-' + xFon.substr(3, 3) + '-' + xFon.substr(6, 4)
		if ((xFon.length == 15) && (xFon.substr(10, 5) != '?????')) {
			cFon += ' x' + xFon.substr(10, 5).replace(/\?/g, '');
		}
	}
	return cFon;
}

//- ToggleImage -//
function SetImage(oImage, cImage) {
	oImage.src = cImage;
}

//- SelectOption -//
function SelectOption(oSelect, cValue) {
	if ((oSelect == null) || (oSelect.options == null)) {
		return;
	}
	for (hOption = 0; hOption < oSelect.options.length; hOption++) {
		if (oSelect.options[hOption].value == cValue) {
			oSelect.options[hOption].selected = true;
			return;
		}
	}
	oSelect.selectedIndex = 0;
}

//- String -//
//- Right -//
function Right(cStream, nLength) {
	if (nLength > cStream.length) {
		return '';
	} else {
		return cStream.substr(cStream.length - nLength, nLength);
	}
}

//- ProductPopup -//
function ProductPopup(cIds, nProductKey) {
	var hWindow = window.open('/Product/Brief.asp?Id=' + nProductKey, 'Product', 'width=516,height=500,scrollbars=1');
	hWindow.focus();
}

//- openWindow -//
function openWindow(url, width, height) {
	var hWindow = window.open(url, 'wndRPlus', 'width=' + width + ',height=' + height + ',scrollbars=1');
	hWindow.focus();
}

//- CopyBillingToBillingCard -//
function CopyBillingToBillingCard(oForm) {
	oForm['BillingCardHolder'].value = oForm.BillingName.value;
	oForm['BillingCardStreet'].value = oForm.BillingStreet.value;
	oForm['BillingCardStreet2'].value = oForm.BillingStreet2.value;
	oForm['BillingCardCity'].value = oForm.BillingCity.value;
	var cState = oForm.BillingState.value;
	var o = oForm['BillingCardState'];
	for (var h = 0; h < o.options.length; h++) {
		if (o.options[h].value == cState) {
			oForm['BillingCardState'].selectedIndex = h;
		}
	}
	oForm['BillingCardZip'].value = oForm.BillingZip.value;
}

//- CopyBillingToShipping -//
function CopyBillingToShipping(oForm) {
	oForm['ShippingName'].value = oForm.BillingName.value;
	oForm['ShippingAttention'].value = oForm.BillingAttention.value;
	oForm['ShippingStreet'].value = oForm.BillingStreet.value;
	oForm['ShippingStreet2'].value = oForm.BillingStreet2.value;
	oForm['ShippingCity'].value = oForm.BillingCity.value;
	var cState = oForm.BillingState.value;
	var o = oForm['ShippingState'];
	for (var h = 0; h < o.options.length; h++) {
		if (o.options[h].value == cState) {
			oForm['ShippingState'].selectedIndex = h;
		}
	}
	oForm['ShippingZip'].value = oForm.BillingZip.value;
	//	oForm['ShippingPhone'].value = null;
}

//- CopyShippingToBilling -//
function CopyShippingToBilling(oForm) {
	oForm['BillingName'].value = oForm.ShippingName.value;
	oForm['BillingAttention'].value = oForm.ShippingAttention.value;
	oForm['BillingStreet'].value = oForm.ShippingStreet.value;
	oForm['BillingStreet2'].value = oForm.ShippingStreet2.value;
	oForm['BillingCity'].value = oForm.ShippingCity.value;
	var cState = oForm.ShippingState.value;
	var o = oForm['BillingState'];
	for (var h = 0; h < o.options.length; h++) {
		if (o.options[h].value == cState) {
			oForm['BillingState'].selectedIndex = h;
		}
	}
	oForm['BillingZip'].value = oForm.ShippingZip.value;
	//	oForm['BillingEmail'].value = null;
}


function ShowOverlay(id, callback) {
	try {
		if (typeof document.body.style.maxHeight === "undefined") {//if IE 6
			$("body", "html").css({ height: "100%", width: "100%" });
			$("html").css("overflow", "hidden");
			if (document.getElementById("RP_HideSelect") === null) {//iframe to hide select elements in ie6
				$("body").append("<iframe id='RP_HideSelect'></iframe><div id='RP_overlay' style='display: none;'></div>");
				$("#RP_overlay").click(function () { HideOverlay(id); });
			}
		} else { //all others
			if (document.getElementById("RP_overlay") === null) {
				$("body").append("<div id='RP_overlay'></div>");
				//					$("#RP_overlay").click(function() { HideOverlay(id); });
			}
		}
		$("#RP_overlay").css("opacity", "0");
		$("#RP_overlay").fadeTo(250, .25, function () {
			$("#RP_overlay").css("opacity", ".25");
			$("#" + id).css({ display: "block" });
			if (callback) {
				callback();
			}
		});
	} catch (o) {
	}
}

function HideOverlay(id) {
	try {
		$("#RP_overlay").unbind("click");
		if (typeof document.body.style.maxHeight == "undefined") {//if IE 6
			$("body", "html").css({ height: "auto", width: "auto" });
			$("html").css("overflow", "");
		}
		document.onkeydown = "";
		$("#RP_overlay").fadeTo(250, 0, function () { $('#RP_overlay,#RP_HideSelect').remove(); });
		$("#" + id).css({ display: "none" });
	} catch (o) {
	}
}

function MessageBox(msg, delay) {
	if (g_isMessageBoxVisible == true) {
		return;
	}
	document.getElementById('MessageBoxDiv_Content').innerHTML = msg;
	try {
		var closeDelay = (delay || delay == 0) ? delay : 2000;
		g_isMessageBoxVisible = true;
		var wndSize = GetWindowSize();
		var offsetY = ScrollOffset();
		var posX = ($(document).width() / 2) - ($('#MessageBoxDiv').width() / 2);
		var posY = ((wndSize.height) / 2) - ($('#MessageBoxDiv').height() / 2) + offsetY;
		$('#MessageBoxDiv').css({ top: posY + 'px', left: posX + 'px' });
		ShowOverlay('MessageBoxDiv', function () {
			if (closeDelay > 0) {
				$(document).bind('click', CloseMessageBox);
				g_mbTimeoutId = setTimeout(function () {
					CloseMessageBox();
				}, closeDelay);
			}
		});
	} catch (e) {
	}
}

function CloseMessageBox() {
	window.clearTimeout(g_mbTimeoutId);
	$(document).unbind('click', CloseMessageBox);
	HideOverlay('MessageBoxDiv');
	g_isMessageBoxVisible = false;
}

function ShowProcessingDiv() {
	try {
		var offsetY = ScrollOffset();
		var nPosX = ($(document).width() / 2) - ($('#ProcessingDiv').width() / 2);
		var nPosY = (($(document).height() - offsetY) / 2) - ($('#ProcessingDiv').height() / 2) + offsetY;
		$('#ProcessingDiv').css({ top: nPosY + 'px', left: nPosX + 'px' });
		ShowOverlay('ProcessingDiv');
	} catch (e) {
	}
}

function HideProcessingDiv() {
	try {
		HideOverlay('ProcessingDiv');
	} catch (e) {
	}
}

function ScrollOffset() {
	var scrollTop = document.body.scrollTop;
	if (scrollTop == 0) {
		if (window.pageYOffset) {
			scrollTop = window.pageYOffset;
		} else {
			scrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
		}
	}
	return scrollTop;
}

//- MenuItem -//
function MenuItem(id) {
	this.id = id;
	this.items = new Array();
}

MenuItem.prototype.addItem = function (objSubNav) {
	this.items.push(objSubNav);
}

MenuItem.prototype.clear = function () {
	this.items.length = 0;
}

//- SubMenuItem -//
function SubMenuItem(name, uri) {
	this.name = name;
	this.uri = uri;
}

function getScrollBottomY() {
	var ieDocBody = ((document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body);
	var scrollTop = (document.all ? ieDocBody.scrollTop : window.pageYOffset);
	var windowHeight = $(window).height();
	return scrollTop + windowHeight;
}

//- Menu Functions -//
function ShowMenu(id, obj, imgsrc) {
	if (!g_menu[id]) {
		return;
	}
	if (g_menu[id].items.length == 0) {
		return;
	}
	var objSubNav = document.getElementById('SubNav_' + id);
	$(objSubNav).stop();
	//+
	var height = $(obj).height();
	var width = $(obj).width();
	var html = '';
	var pos = findPos(obj);
	if (obj) {
		obj.src = imgsrc;
	}
	//	if ($('#SubNavContent_' + id).height() < height + 24) {
	//		$('#SubNavContent_' + id).css({height: (height + 24) + 'px'});
	//	}
	var subNavHeight = $(objSubNav).height();
	var documentHeight = $(document).height();
	var scrollHeight = getScrollBottomY();
	var posTop = pos.y - 22;
	if (pos.y + subNavHeight > scrollHeight) {
		posTop = scrollHeight - $(objSubNav).height();
	}
	if (posTop + subNavHeight < pos.y + height + 10) {
		posTop += 10;
	}
	$(objSubNav).css({ top: posTop + 'px', left: (pos.x + width + 11) + 'px', width: (175) + 'px' });
	$('#SubNavLeftCap_' + id).css({ top: (pos.y - 11) + 'px', left: (pos.x + width) + 'px', display: 'block' });
	$('#SubNavLeftCap_' + id + ' > div.SubNavEndCap_Filler').css({ height: (height - 2) + 'px' });
	$(objSubNav).hover(function () {
		$(objSubNav).stop();
	}, function () {
		//HideMenu(id);
	}
	);
	$(objSubNav).css({ display: 'block' });
}

function HideMenu(id, obj, imgsrc) {
	if (id == '') {
		return;
	}
	$('#SubNavLeftCap_' + id).css({ display: 'none' });
	var objSubNav = document.getElementById('SubNav_' + id);
	$(objSubNav).stop();
	if (obj) {
		obj.src = imgsrc;
	}
	$(objSubNav).css({ display: 'none' });
}

function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return { x: curleft, y: curtop };
}

function ToggleFavorite(ids, productId) {
	var imgRoot = '/_COMMON/_Atom/Survey/StarRating/';
	var img = document.getElementById('FavoriteAction_' + productId);
	var isFavorite = ((img.src.substring(img.src.length - 12) == 'GrayStar.gif') ? false : true);
	img.src = imgRoot + ((isFavorite == true) ? "GrayStar.gif" : "OrangeStar.gif");
	$.get('/Product/FavoriteData.asp?Request=ToggleFavorite&ProductId=' + productId + '&IdS=' + g_idS, function (data) {
		if (data != '1') {
			alert('An error occurred while trying to save your favorite. Please try again.');
			img.src = imgRoot + ((isFavorite == false) ? "GrayStar.gif" : "OrangeStar.gif");
		}
	});
}

function editCurEx(productId) {
	var curExInput = document.getElementById('CurExInput_' + productId);
	document.getElementById('CurExEditAction_' + productId).style.display = 'none';
	document.getElementById('CurExSaveAction_' + productId).style.display = '';
	curExInput.style.display = 'block';
	document.getElementById('CurExValue_' + productId).style.display = 'none';
	curExInput.isSaving = false;
	curExInput.select();
}

function saveCurEx(productId) {
	var curExInput = document.getElementById('CurExInput_' + productId);
	if (curExInput.isSaving == true) {
		return;
	}
	curExInput.isSaving = true;
	var value = curExInput.value;
	curExInput.value = 'Saving...';
	curExInput.className = 'CurExInputDisabled';
	curExInput.disabled = true;
	$.get('/Product/CurExData.asp?ProductId=' + productId + '&Value=' + value + '&IdS=' + g_idS, function (data) {
		if (data != '1') {
			alert('An error occurred while trying to save your ticket value. Please try again.');
		} else {
			document.getElementById('CurExValue_' + productId).innerHTML = value;
			document.getElementById('CurExEditAction_' + productId).style.display = '';
			document.getElementById('CurExSaveAction_' + productId).style.display = 'none';
			document.getElementById('CurExInput_' + productId).style.display = 'none';
			document.getElementById('CurExValue_' + productId).style.display = '';
		}
		curExInput.isSaving = false;
		curExInput.value = value;
		curExInput.disabled = false;
		curExInput.className = 'CurExInput';
	});
}

function showShippingAlert(o) {
	var pos = findPos(o);
	$('#ShippingAlert').css({ top: ((pos.y + $(o).height()) + 'px'), left: ((pos.x + $(o).width()) + 'px') });
	$('#ShippingAlert').fadeIn(250, function () {
		setTimeout(function () { $('#ShippingAlert').fadeOut(250); }, 3000);
	});
}

function LargeImagePopup(id) {
	var wnd = window.open('/Product/Image.asp?Id=' + id, 'wnd_LargeImage', 'width=600,height=550,scrollbars=1,resizable=1');
	wnd.focus();
}

function selectAllCheckboxes(form) {
	setCheckValue(form, true);
}

function clearAllCheckboxes(form) {
	setCheckValue(form, false);
}

function setCheckValue(form, value) {
	var i = 0;
	for (i = 0; i < form.elements.length; i++) {
		if (form.elements[i].type == 'checkbox') {
			form.elements[i].checked = value;
		}
	}
}