pgLoaded = false;

window.name = host_name = self.location.host.replace(/www./i, '');
nav_on = nav_focus = nav_blur = subnav_focus = content_container = false;
subnav_nav_id = null;

cookie_name = 'cart';
cookie_key = '&';
cookie_host = '.' + host_name;
cookie_path = '/';
cookie_expiry = (new Date((new Date()).getTime() + (3600000 * 24 * 30))).toGMTString();
item_list = (document.cookie.match(/cart=((&\d{6})+)/) == null) ? '' : RegExp.$1;

email = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/

function init(nav_id) {
	pgLoaded = true;
	nav_on = (nav_id != '') ? document.getElementById('nav_' + nav_id) : nav_on;
	content_container = document.getElementById('content_container');
}

function killEvent(e) {
	e = window.event ? window.event : e;
	if (e) { e.cancelBubble = true; }
}

function killNav() { 
	if (pgLoaded) {
		killEvent();
		if (nav_blur) {
			if (nav_blur.id != subnav_nav_id) {
				blurNav(nav_blur);
				clearStatus();
				nav_blur = false;
				nav_focus = false;
			}
		}
	}
	content_container.onmouseover = null;
}


function nav_over(e, obj) {
	killEvent(e);
	if (pgLoaded) {
		if (!nav_blur) { content_container.onmouseover = killNav; }
		if (obj != nav_blur && obj != nav_focus && subnav_nav_id == null) {
			if (nav_blur) { blurNav(nav_blur); nav_blur = false; }
			if (obj != nav_on) { swap(obj, '_off', '_over'); }
			subnav_nav_id = obj.id;
			showSubnav(subnav_nav_id);
			nav_focus = obj;
		}
		window.status = obj.alt;
		}
	return true;
}

function nav_out(e, obj) {
	killEvent(e);
	if (pgLoaded) {
		nav_blur = obj;
		subnav_nav_id = null;
	}
}

function showSubnav(nav_id) {
	subnav = document.getElementById('sub' + nav_id);
	if (subnav && subnav.style.display != 'block') { show(subnav); }
}

function hideSubnav(nav_id) {
	subnav = document.getElementById('sub' + nav_id);
	if (subnav && subnav.style.display == 'block') { hide(subnav); }
}

function blurNav(obj) {
	if (pgLoaded) {
		if (nav_blur != nav_on) { swap(obj, '_over', '_off'); }
		if (subnav_focus) { subnav_focus.className = 'subnav'; subnav_focus = false; }
		hideSubnav(obj.id);
		clearStatus();
	}
	return true;
}

function subnav_over(obj) {
	subnav_params = obj.id.split('_');
	subnav_nav_id = (subnav_params[0].replace(/sub/, '')) + '_' + subnav_params[1];
	if (subnav_focus) { subnav_focus.className = 'subnav'; }
	subnav_focus = document.getElementById(subnav_params[0] + '_' + subnav_params[1] + '_' + subnav_params[2]);
	subnav_focus.className = 'subnavSelected';
	return setStatus(obj);
}

function subnav_out(obj) {
	if (subnav_focus) { subnav_focus.className = 'subnav'; subnav_focus = false; }
	return clearStatus();
}

function setStatus(lnk) {
	window.status = lnk.title;
	return true;
}

function clearStatus() {
	window.status = '';
	return true;
}

function swap(img, src_old, src_new) {
	img.src = img.src.replace(eval('/' + src_old + '/'), src_new);
}

function hide(obj) {
	if (obj) { obj.style.display = 'none'; }
}

function show(obj) {
	if (obj) { obj.style.display = 'block'; }
}

function doNothing(obj) { return false; }

function addItem(form) { 
	id_key = '&' + form.id.value; 
	if (item_list.indexOf(id_key) < 0) {
		item_list += id_key;
		document.cookie = cookie_name + '=' + item_list + '; path=' + cookie_path + '; domain=' + cookie_host + '; expires=' + cookie_expiry;
		return true;
	} else {
		alert('This item has already been added to your cart.');
		return false;
	}
}

function removeItem(form) {
	id_key = '&' + form.id.value;
	if (item_list.match((new RegExp(id_key))) == null) {
		//
		return false;
	} else {
		item_list = RegExp.leftContext + RegExp.rightContext;
		document.cookie = cookie_name + '=' + item_list + '; path=' + cookie_path + '; domain=' + cookie_host + '; expires=' + cookie_expiry;
		return true;
	}
}

function clearCart() {
	document.cookie = cookie_name + '=; path=' + cookie_path + '; domain=' + cookie_host + '; expires=Fri, 02-Jan-1970 00:00:00 GMT';
	self.location = self.location.href;
}

function handleMultiSelect(obj) {
	n = 0; obj_selected = new Array();
	for (i = 0; i < obj.options.length; i ++) {
		if (obj.options[i].selected) { obj_selected[n ++] = obj.options[i].value; }
	}
	obj_real = obj.form.elements[(obj.name.replace(/_select/, ''))];
	obj_real.value = obj_selected.join(',');
}

function checkText(obj) {
	return !(obj.value.replace(/\s/gi, '') == '');
}

function checkNumber(obj) {
	return !(isNaN(parseInt(obj.value)) || parseInt(obj.value).toString().length != obj.value.length);
}

function checkSelect(obj) {
	return obj.selectedIndex > 0;
}

function checkSelectMulti(obj) {
	is_valid = false;
	for (i = 0; i < obj.length; i ++) { if (obj[i].selected == true) { is_valid = true; break; } }
	return is_valid;
}

function checkEmail(obj) {
	return !(obj.value.match(email) == null);
}

function getValue(obj) {
	_type = obj.type; _value = '';
	if (_type == 'text' || _type == 'textarea') {
		_value = obj.value;
	} else if (_type == 'select') {
		_value = obj.options[obj.selectedIndex].value;
	} else if (_type == 'checkbox') {
		if (obj.checked) { _value = obj.value; }
	} else if (obj.length > 0) {
		for (j = 0; j < obj.length; j ++) { if (obj[j].checked) { _value += obj[j].value + ','; } }
		_value = _value.replace(/(^,)|(,$)/g, '');
	}
	return _value;
}

function handleSubscription(form) {
	msg = ''; _focus = false;
	if (!checkText((obj = form.email)) || !checkEmail(obj)) {
		msg += 'The e-mail address you entered is not a valid internet address.'; _focus = obj;
	} else if ((email = obj.value) != getValue((obj = form.email_check))) {
		msg += 'The e-mail address and confirmed e-mail address you entered do not match.'; _focus = obj;
	}
	if (msg != '') {
		alert(msg + '\nPlease correct and submit again.\n'); if (_focus) { _focus.focus(); } return false;
	} else {
		return true;
	}
}

function handleInquiry(form) {
	msg = ''; _focus = false;
	if (!checkText((obj = form.name))) { 
		msg += 'You did not provide your name in the Contact Information area.'; _focus = obj; 
	} else if (!checkText((obj = form.email)) || !checkEmail(obj)) {
		msg += 'The e-mail address you entered is not a valid internet address.'; _focus = obj;
	} else if (!checkText(form.memo)) {
		msg += 'You did not tell us anything about your inquiry in the memo box.'; _focus = obj;
	}
	if (msg != '') {
		alert(msg + '\nPlease correct and submit again.\n'); if (_focus) { _focus.focus(); } return false;
	} else {
		return true;
	}
}

function handleQuickSearch(form) {
	if (!checkText((obj = form.keywords))) {
		alert('Please enter Author or Title keywords before submitting your Quick Search request.\n');
		obj.focus(); return false;
	} else {
		return true;
	}
}

function handleSearch(form) {
	has_search_term = false;
	search_terms = new Array('author', 'title', 'edition', 'is_signed', 'condition');
	for (i = 0; i < search_terms.length; i ++) {
		if (getValue(form.elements[search_terms[i]]) != '') { has_search_term = true; break; }
	}
	if (getValue(form.elements['format']) != '') { has_search_term = true; }
	if (!has_search_term) {
		alert('Please specify at least one search term before submitting your Search request.\n');
		return false;
	} else {
		return true;
	}
}

function handleOrder(form) {
	msg = ''; _focus = false;
	if (!checkText((obj = form.cust_name))) { 
		msg += 'You did not provide your name in the Contact Information area.'; _focus = obj;
	} else if (!checkText((obj = form.cust_phone))) {
		msg += 'You did not provide your phone number in the Contact Information.'; _focus = obj;
	} else if (!checkText((obj = form.email)) || !checkEmail(obj)) {
		msg += 'The e-mail address you entered is not a valid internet address.'; _focus = obj;
	} else if ((email = obj.value) != getValue((obj = form.email_conf))) {
		msg += 'The e-mail address and confirmed e-mail address you entered do not match.'; _focus = obj;
	} else if (!checkText((obj = form.name))) {
		msg += 'You did not provide the name to which this order should be shipped.'; _focus = obj;
	} else if (!checkText((obj = form.street1))) {
		msg += 'You did not provide the street address to which this order should be shipped.'; _focus = obj;
	} else if (!checkText((obj = form.city))) {
		msg += 'You did not provide the city to which this order should be shipped.'; _focus = obj;
	} else if (!checkText((obj = form.state))) {
		msg += 'You did not provide the state or province to which this order should be shipped.'; _focus = obj;
	} else if (!checkText((obj = form.country))) {
		msg += 'You did not provide the country to which this order should be shipped.'; _focus = obj;
	} else if (!checkText((obj = form.zip))) {
		msg += 'You did not provide the ZIP or other postal code to which this order should be shipped.'; _focus = obj;
	} 
	if (msg != '') {
		alert(msg + '\nPlease correct and submit again.\n'); if (_focus) { _focus.focus(); } return false;
	} else {
		return true;
	}
}

function setDefault(source_obj, target_form, target_obj_name) {
	if ((target_obj = target_form.elements[target_obj_name])) {
		if (getValue(target_obj) == '') { target_obj.value = getValue(source_obj); }
	}
}