
// Boolean for browser type
var agt=navigator.userAgent.toLowerCase();
var is_nav  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
	&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
	&& (agt.indexOf('webtv')==-1));
	
	// Global variables for this function
var TR_nav;

function showHighlight(sSelection) {
	
    if( is_nav ) {
  	 TR_nav = eval("document.ids." + sSelection);
    } else {
  	 TR_nav= document.getElementById(sSelection);
    }
	
	//TR_nav.style.backgroundImage = "url(images/genre_highlight.gif)";
	TR_nav.style.color = "#ffffff";
	
}

function clearHighlight(sSelection) {

    if( is_nav ) {
  	 TR_nav = eval("document.ids." + sSelection);
    } else {
  	 TR_nav= document.getElementById(sSelection);
    }
	
	//TR_nav.style.backgroundImage = "url()";
	TR_nav.style.color = "#000000";
}

function runTextSearch() {
  
	//alert('tbd - FT indexes on real server');
	

	var sSearch = document.forms['search_form'].search_text.value;
	
	if (sSearch.length > 0) { 	
	  window.location = 'processSearch.php?upt=book&ustext=' + sSearch;
	}else{
	  alert('You must enter search text first');
	}


}

function readExcerpt(sBookID) {

  winExcerpt = window.open('content.php?utype=note&uid='+sBookID, "contentExcerpt", "top=200,left=200,screenX=50,screenY=50,height=500,width=700,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no");

}

function showWindow(sFileName) {

  win = window.open(sFileName, '_blank', 'top=100,left=100,screenX=50,screenY=50,height=500,width=600,toolbar=no,menubar=no,scrollbars=yes,resizable=yes,location=no,directories=no,status=no');

}

function closeThisWindow() {

  window.close();

}


function validateCheckout() {
  if (document.forms['cart'].expmonth.selectedIndex == -1 | document.forms['cart'].expmonth.options[document.forms['cart'].expmonth.selectedIndex].value == "null") {
    alert('Please select a Month for the Expiration Date');
		return false;	
	}
  if (document.forms['cart'].expyear.selectedIndex == -1 | document.forms['cart'].expyear.options[document.forms['cart'].expyear.selectedIndex].value == "null") {
    alert('Please select a Year  for the Expiration Date');
    return false;    	
  }
	if (document.forms['cart'].cvm.value.length == 0 && !document.forms['cart'].cvmnotpres.checked) {
    alert('Please either enter the Card Code or click the checkbox if you cannot locate it');
		document.forms['cart'].cvm.focus();
    return false;   
	}
	if (document.forms['cart'].baddr1.value.length == 0) {
    alert('Please enter the 1st line of the billing address for the credit card');
		document.forms['cart'].baddr1.focus();
    return false;   
	}
	if (document.forms['cart'].bcountry.value == "US") {
	  if (document.forms['cart'].bzip.value.length == 0) {
	    alert('Please enter the 5 digit Zipcode of the billing address for the credit card');
	    document.forms['cart'].bzip.focus();
	    return false;
	  }
	}
	
	//alert('ok');
	document.forms['cart'].submit();
}

function validateCustomerAccount() {
	if (document.forms['customeraccount'].fpeaches.value.length == 0) {
    alert('Please enter your Last Name');
		document.forms['customeraccount'].fpeaches.focus();
    return false;   
	}
	
	if (document.forms['customeraccount'].fapples.value.length == 0) {
    alert('Please enter a Login Name');
		document.forms['customeraccount'].fapples.focus();
    return false;   
	}
	
	// new
	
	if (document.forms['customeraccount'].foranges.value.length == 0) {
    alert('Please enter a Password');
		document.forms['customeraccount'].foranges.focus();
    return false;   
	}
	
	if (document.forms['customeraccount'].foranges2.value.length == 0) {
    alert('Please re-type your password');
		document.forms['customeraccount'].foranges2.focus();
    return false;   
	}
	
	if (document.forms['customeraccount'].foranges.value != document.forms['customeraccount'].foranges2.value) {
    alert('The 2 passwords typed do not match.');
		document.forms['customeraccount'].foranges.value = "";
		document.forms['customeraccount'].foranges2.value = "";
		document.forms['customeraccount'].foranges.focus();
    return false;   
	}
	
	document.forms['customeraccount'].submit();
	
}

function recalculateCart() {
  var sURL = "cart.php";
	var sCode = window.document.cartdiscount.discountcode.value;
	if ( sCode.length > 0 ) {
	  sURL += "?udcode=" + escape(sCode);
	}
	//window.document.cartdiscount.action = sURL;
	window.location.href = sURL;
}

function processCheckout() {
	var sURL = "processCheckout.php";
  
	window.location.href = sURL;
	
}

function processBuyCredit() {
  
	var rAmounts = document.forms['buycredit'].creditAmount;
	var sAmount = "";
	
	for (i = 0; i < rAmounts.length; i++) {
	  if ( rAmounts[i].checked ) {
		  sAmount = rAmounts[i].value;
			break;
		}
	}
	
	if ( sAmount.length > 0 ) {
	  window.location.href = "processCheckout.php?ucredit=" + sAmount;
	}else{
	  alert('You must choose a Credit Amount to purchase');
	}
	
}

function selectRadio(sFormName, sFieldName, sValue) {
 // 'creditAmount'
 // '10'
 
 var oRadio = eval("window.document.forms['" + sFormName + "']." + sFieldName);
 
 for (i = 0; i < oRadio.length; i++) {
  if ( oRadio[i].value == sValue ) {
   oRadio[i].checked = true;
 	 break;
  }
 }
 
}

function saveRecommendation() {
// validates affiliate recommendation form
  var frmAffiliate = window.document.affiliatesettings;
	if ( frmAffiliate.product_id.type == "select" ) {
	  // select box input
    if (frmAffiliate.product_id.selectedIndex == -1 | frmAffiliate.product_id.options[frmAffiliate.product_id.selectedIndex].value == "null") {
      alert('Please select a book title');
  		return false;	
  	}
	}else{
	  // text box input - no action
	}
	
	if ( frmAffiliate.product_comment.value.length > 350 ) {
	  alert('The Comment for a book cannot exceed 350 characters and you\'ve used ' + frmAffiliate.product_comment.value.length);
		return false;
	}
	
	frmAffiliate.submit();
	
	return true;

}

function modifyRecommendation(sAction,sProductID) {
// edit or delete recommendation
	 
	 // confirm if delete
	 if ( sAction == "delete" ) {
	   if( !confirm('Are you sure you want to delete this Recommended Book?') ) {
		   return false;
		 }
	 }

	 window.location.href = '?uaction=' + sAction + '&uproductid=' + sProductID;

}

function acceptTnC() {
  var frmAffiliateAccept = window.document.affiliateaccept;
	if ( !frmAffiliateAccept.accept.checked ) {
	  alert('Please click the Acceptance box before proceding');
		return false;
	}
	
	window.location.href = '?uaction=activate';
}


function saveReview() {
// validates review form
  var frmReview = window.document.bookReview;
	
	if ( frmReview.product_comment.value.length > 350 ) {
	  alert('The Comment for a book cannot exceed 350 characters and you\'ve used ' + frmReview.product_comment.value.length);
		return false;
	}
	else
	{
		return true;	
	}
}

function processBuyGiftCertificate() {
  
	var fAmount = document.forms['buygiftcertificate'].Amount;
	var fEmail = document.forms['buygiftcertificate'].RecipientEmail;
	var sAmount = "";
	
	if ( fAmount.value == "" )
	{
	 	 alert('You must enter an Amount for the Gift Certificate');
		 return false;
	}
	if ( isNaN(fAmount.value) )
	{
	 	 alert('You must enter a valid number Amount for the Gift Certificate');
		 return false;
	}
	if ( parseFloat(fAmount.value) < 10.00 || parseFloat(fAmount.value) > 100.00 )
	{
	 	 alert('The Amount must be more than 10.00 and less than 100.00');
		 return false;
	}
	if ( fEmail.value == "" )
	{
	 	 alert('You must enter a valid email address for the recipient');
		 return false;
	}
	if ( !validateEmail(fEmail.value) )
	{
	 	 alert('The email address you enetered appears to be invalid. Please check it again');
		 return false;
	}
	
	if ( confirm('Confirm you are buying a Gift Certificate for $' + fAmount.value) )
	{
  	document.forms['buygiftcertificate'].fvalidform.value = "buygiftcertificate";
  	document.forms['buygiftcertificate'].submit();
  	return true;
	}
	else
	{
	 return false;
	}
	
}

function checkValid()
{
 var fValid = document.forms['buygiftcertificate'].fvalidform;
 if ( fValid.value == "" )
 {
 	return false;
 }
 return true;
}

function validateEmail ( emailValue ) {
 emailpat = /^([a-zA-Z0-9])+([\.a-zA-Z0-9_-])*@([a-zA-Z0-9])+(\.[a-zA-Z0-9_-]+)+$/;
 if( !emailpat.test( emailValue ) ) {
  return false;
 }
 return true;
}

