/* WebVentures miscellaneous JS functions 200710 */

function clear_confirm() {
var reply=confirm("Do you really want to delete your shopping basket and close this shopping session?\nSelect OK to delete your basket and contents or Cancel to continue shopping.");
if (reply==true) {
    window.location="/bookshop/action/BasketClear";
  } else {
    window.location="/bookshop/basket/continue.html";
  }
}

function signout() {
  window.location="/bookshop/action/SignOut";
}
/* Set of client-side validators for various forms used in Javelin 3.1 [Co-op version]
Created CVL WebVentures Pty Ltd.
*/
// TODO: Should not throw an Alert but rather just output to form in defined error location.
function passwordValidate( field ) {
  if (field.value.length < 6) {
    alert('You have not entered a valid password.');
    return false;
  } 
}

function emailValidate( field ) {
  if (field.value.length < 3) {
    alert('You have not entered an email address');
    return false;
  } else if(!isValidEmail(field.value)) { 
      alert('You have not entered a correctly formatted email address'); 
      field.focus() 
      return false; 
   } 
}
//function name: isValidEmail
//Parameters: str - string of email to be validated
//Description: Basic format check that there is a dot in the text 
// but not at first character and
// that the @ symbol is present but not at the first char
// and that a dot comes after the @.
// First check for no comma or space which is an easy typo.
function isValidEmail(str) {
  if (str.length < 6) return false;
//  str = str.trim();
  if (str.indexOf(",") > -1 || str.indexOf(" ") > -1) return false;
  var firstDot = str.indexOf(".");
  var atPos = str.indexOf("@");
  var lastDot = str.lastIndexOf(".");  // look for dot after @
  return (firstDot > 0 && atPos > 0 && lastDot > atPos);
}

var overLimit = 0;
var max_attempts = 1; // warning on second attempt
/**
  Checks the length of a form element (normally textarea), and if the length is more than the maximum characters,
  it truncates it.   A warning is also given after max_attempts at exceeding the limit.
  @param	textarea	the textarea to check (as a DOM object - recommended "this").
  @param	maxLength	the maximum number of characters.
  @date	2009-08-18
  @author	Tristan Roberts - WebVentures.
*/

function checkLength(textarea, maxLength) {
  if (textarea.value.length > maxLength) {
    textarea.value = textarea.value.substring(0, maxLength);
    if (overLimit > max_attempts) {
      alert('Sorry, please enter a maximum of ' + maxLength + ' characters.');
    }
    overLimit++;
  }
}

function adviseWrap(textarea, maxLength) {
  if (textarea.value.length > maxLength) {
    textarea.value = textarea.value.substring(0, maxLength);
    if (overLimit > max_attempts) {
      alert('Sorry, a maximum of ' + maxLength + ' characters is possible.  Please use the next field for overflow text.');
    }
    overLimit++;
  }
}

/**
	Expands a textarea as the user types to avoid scrollbars.
	@author	Tristan Roberts, WebVentures (http://web.tristanroberts.name)
	@date	2010-04-01
	@param	selector	A jQuery-compatible selector of the textarea.
*/
function expandTextarea( selector ) {
	while ( selector.scrollTop() != 0 ) {
		var height = selector.height( );
		selector.css( "height", ( height + 15 ) + "px" );
	}
}

/*
 *	Example - Expand upon key up on the textarea.
 */
$( document ).ready( function( ) {
	$( "textarea" ).keyup( function( e ) {
		expandTextarea( $( this ) );
	} );
} );


