//---JS Routines Copyright Cube Connection Ltd 2001 Unless Otherwise Stated---//
//---                       www.CubeConnection.co.uk                       ---//

//---START FORM VALIDATION ROUTINES---//

var errfound = false;

/*Function expects name of a record and its min. length and returns TRUE or FALSE if not met.*/
function ValidLength(record, len) {

	return (record.length >= len);

}

/*Function to check for valid numbers - if bInteger is true then just whole ones, else decimal ones*/
function ValidNumber(record, bInteger, bPositive)
{
  if (bPositive)
    if (bInteger)
      var strExp = new RegExp(/^[+]?\d+$/);
    else
      var strExp = new RegExp(/^[+]?\d+(\.\d+)?$/);
  else
    if (bInteger)
      var strExp = new RegExp(/^[-+]?\d+$/);
    else
      var strExp = new RegExp(/^[-+]?\d+(\.\d+)?$/);

  var strTest=strExp.test(record);

  return strTest;
}

/*check if any dodgy characters present and give error if so*/
function ValidChars(text, extra, chartype) {

	var i = 0;
	var validchars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var numberStr = "1234567890";
	var errorchar = 0;

//0=letters only; 1=letters and numbers; 2=numbers only
	if (chartype == 0) validchars = validchars;
	if (chartype == 1) validchars = validchars + numberStr;
	if (chartype == 2) validchars = numberStr;

	validchars = validchars + extra;

	for (i=0; i <= text.length; i++) {
 		/* Ignore CRLF */
		if ((text.charAt(i) != "\n") && (text.charAt(i) != "\r"))
			if (validchars.indexOf(text.charAt(i).toUpperCase(),0) == -1)
				errorchar = errorchar + 1;

	}

	if (errorchar == 0)
		return true;
	else
		return false;

}

/*Function check email address*/
function ValidEmail(item) {

	var i = 0;

	i = item.indexOf('@',1);
	if (i == -1) return false;

	i = item.indexOf('.',i+2);
	if (i == -1 || i == item.length) return false

	if (!ValidChars(item.charAt(item.length - 1), "", 0)) return false;

	return true;

}

/*Display error and set focus to record that caused it*/
function DisplayError(record, text) {

	if (errfound) return; /*abort if error already displayed*/
	window.alert(text);
	record.select();
	record.focus();
	errfound = true;

}

/*Capitalise the first char of each word, make the rest lower case*/
function CapitalFirst(text) {

	var i = 0;
	var TempStr = "";

	TempStr = text.charAt(0).toUpperCase();
	for (i=1; i <= text.length-1; i++) {
		if (text.charAt(i-1) == " ")
			TempStr = TempStr + text.charAt(i).toUpperCase();
		else
			TempStr = TempStr + text.charAt(i).toLowerCase();
	}

	return TempStr;
}

/*Calc the power of 10 to intTmp*/
function powerOf10(intTmp) {

	var i = 0;
	var j = 0;

	for (i=0; i<=intTmp; i++) {
		if (i==0)
			j = 1;
		else
			j = (10 * j);
	}
	return j;
}

/*Return the number of a field, if non number present return -1*/
function ConverttoNumber(textnumber) {

	var i = 0;
	var j = 0;
	var count = 0;

	for (i=0,j=1; j<=textnumber.length; i++,j++) {
		if (textnumber.charAt(i) < '0' || textnumber.charAt(i) > '9')
			return -1;
		else
			count += eval(textnumber.charAt(i)*(powerOf10(textnumber.length - j)));
	}

	return count;
}

function WordTotal(text)
{
	var i = j = 0;
	var total = 0;

//discount white space at start
	while (text.charAt(j) == " ") {
		j++;
	}

//count spaces between the words
	for (i=j; i <= text.length-2; i++) {
		if (text.charAt(i) == ' ' && text.charAt(i+1) != ' ')
			total++;
	}
//words equal number of spaces plus 1
	total++;
	return total;
}

function replaceText(strText, fromStr, toStr)
{
	strExp=new RegExp(fromStr, "gi");
	strTemp=strText.replace(strExp, toStr);
	return strTemp;
}

function CleanupPrice(strText) {
	strExp=new RegExp('£', "gi");
	strTemp=strText.replace(strExp, '');
	strExp=new RegExp(',', "gi");
	strTemp=strTemp.replace(strExp, '');
	strExp=new RegExp(' ', "gi");
	strTemp=strTemp.replace(strExp, '');
	return strTemp;
}
//---END FORM VALIDATION  ROUTINES---//

