
String.prototype.trim = function() {
	return this.replace(/^\s*(.*?)\s*$/, '$1');
};

String.prototype.isEmail = function() {
	var emailReg = /^[a-zA-Z_0-9\.\-]+\@([a-zA-Z_0-9\-]+\.)+[a-zA-Z_0-9\-]+$/;
	return emailReg.test(this.trim());
};

String.prototype.isPassword = function() {
	var regex = /^[a-zA-Z0-9]{5,10}$/;
	return regex.test(this.trim());
};

// common form failure message
function DoFail(ctl, msg) {
	ctl.focus();
	alert(msg);
	return false;
}

// helper for detail page
var currTab = new Array();
function setCntTab(tabId, sectId) {
	document.getElementById('cntTb' + sectId + '_' + currTab[sectId]).className	= 'contentTabOff';
	document.getElementById('cntStr' + sectId + '_' + currTab[sectId]).className	= 'hid';
	document.getElementById('cntTb' + sectId + '_' + tabId).className		= 'contentTabOn';
	document.getElementById('cntStr' + sectId + '_' + tabId).className		= 'vis2';
	currTab[sectId]	= tabId;
}

// helper for clearing prefilled input text boxes
function clearPreFill(obj, preFillText, typeToPassword) {
	if (obj.value == preFillText) {
		obj.value = "";

		if (typeToPassword && obj.type == 'text') {
			var isIE  = (navigator.userAgent.indexOf("MSIE")>-1 && navigator.userAgent.indexOf("Opera")==-1)?true:false;
			if (isIE) {	// IE work around.

				var newSize = obj.size;
				var newClassName = obj.className;
				var newName = obj.name;
				var newId = obj.id;

				var newElem = document.createElement("INPUT");
				newElem.type = "password";
				newElem.size = newSize;
				newElem.className = newClassName;
				newElem.name = newName;
			    newElem.id = newId;

				obj.parentNode.replaceChild(newElem, obj);

				// buggy IE??? one focus does not work but two do!?!?
				document.getElementById(newId).focus(); 
				document.getElementById(newId).focus();
			}
			else {
				obj.type = 'password';
			}
		}
	}
}

// check the view purchase history form
function ValidateLoginForm(frm) {

	var s = frm.fvEmailLogIn.value.trim();
	if (s == '')
		return DoFail(frm.fvEmailLogIn, "You must enter your Email Address.");
	if (!s.isEmail())
		return DoFail(frm.fvEmailLogIn, "Your Email Address must be a valid email address.");

	s = frm.fvLoginPassword.value.trim();
	if (s == '')
		return DoFail(frm.fvLoginPassword, "You must enter your Password.");
	if (!s.isPassword())
		return DoFail(frm.fvLoginPassword, "Your Password must be between 5 and 10 alphanumeric characters.\n"
									+ "Please do not use spaces or punctuation - just letters and/or numbers.");

	return true;
}

function exam_confirm_logout(has_cart){

	var str = 'You have selected to log off of the Online Bookstore.\n';
	
	if (has_cart)
		str += 'All inventory you have reserved in your cart will be released.\n';
	
	str += '\nContinue?'; 

	if(confirm(str))		return true;
	else					return false;
}
