function formValidator()
{
	// set up array to hold error messages
	this.errorList = new Array;

	// snip
	this.isEmpty = isEmpty;
	this.isNumber = isNumber;
	this.isWithinRange = isWithinRange;
	this.isChecked = isChecked;
	this.isAlphabetic = isAlphabetic;
	this.isAlphaNumeric = isAlphaNumeric;
	this.raiseError = raiseError;
	this.numErrors = numErrors;
	this.displayErrors = displayErrors;
	this.isEmailAddress = isEmailAddress;
	this.isValidlength = isValidlength;

}


function isValidlength(val, min, max)
{
	if (val.length >= min && val.length <= max)
	{
		return true;
	}
	else
	{
		return false;
	}	
}




// check to see if input is whitespace only or empty
function isEmpty(val)
{
	if (val.match(/^s+$/) || val == "")
	{
		return true;
	}
	else
	{
		return false;
	}	
}


// check to see if input is number
function isNumber(val)
{
	if (isNaN(val))
	{
		return false;
	}
	else
	{
		return true;
	}	
}

// check to see if value is within min and max
function isWithinRange(val, min, max)
{
	if (val >= min && val <= max)
	{
		return true;
	}
	else
	{
		return false;
	}	
}

// check to see if form value is checked
function isChecked(obj)
{
	if (obj.checked)
	{
		return true;
	}
	else
	{
		return false;
	}	
}

// check to see if input is alphabetic
function isAlphabetic(val)
{
	if (val.match(/^[a-zA-Z-\' .]+$/))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

// check to see if input is alphanumeric
function isAlphaNumeric(val)
{
	if (val.match(/^[a-zA-Z0-9]+$/))
	{
		return true;
	}
	else
	{
		return false;
	}	
}


// add an error to error list
function raiseError(msg)
{
	this.errorList[this.errorList.length] = msg;
}

// check to see if input is a valid email address
function isEmailAddress(val)
{
	if (val.match(/^([a-zA-Z0-9])+([.a-zA-Z0-9_-])*@([a-zA-Z0-9_-])+(.[a-zA-Z0-9_-]+)+/))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

// display all errors
// iterate through error array and print each item
function displayErrors()
{
	var spacer = "____________________________________________\r\r";
	var anerror = "Sorry - you may have made a mistake\r" + spacer;
	for (x=0; x<this.errorList.length; x++)
	{
		//anerror = "There are the following error with the information you have entered\r\r";
		anerror = anerror + (x+1) + "    " + this.errorList[x] + "\r";
	}
	anerror = anerror + spacer;
		
		alert(anerror);
}

// add an error to error list
function raiseError(msg)
{
	this.errorList[this.errorList.length] = msg;
}

// return number of errors in error array
function numErrors()
{
	return this.errorList.length;
}


function checkSearch(stringvalue)
{

	fv = new formValidator();
		
	if (fv.isEmpty(stringvalue))
	{
		fv.raiseError("You have not entered anything to search for");
	}

	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
	{
		return true;
	}

}





function checkForm(formname)
{

	fv = new formValidator();
	
	// perform checks
	// check for empty full name field
	if (fv.isEmpty(formname.myname.value))
	{
		fv.raiseError("You have not entered anything for your Full name");
	}

	if (!fv.isEmpty(formname.myname.value) && !isValidlength(formname.myname.value, 4, formname.myname.maxLength))
	{
		fv.raiseError("Please enter at least 4 letters for your Full name");
	}

	if (!fv.isEmpty(formname.myname.value) && !isAlphabetic(formname.myname.value))
	{
		fv.raiseError("You have entered none alpahbetic character for your name");
	}



	// check compa ynamre
	if (!fv.isEmpty(formname.mycompany.value) && !isValidlength(formname.mycompany.value, 4, 50))
	{
		fv.raiseError("Please enter at least 4 characters for your company name");
	}
	
	// check first line of the address
	if (fv.isEmpty(formname.myaddressone.value))
	{
		fv.raiseError("You have not entered the first line of your address");
	}

	if (!fv.isEmpty(formname.myaddressone.value) && !isValidlength(formname.myaddressone.value, 4, formname.myaddressone.maxLength))
	{
		fv.raiseError("You have not entered at least 4 letters for your address");
	}

	//check town
	if (fv.isEmpty(formname.myaddressthree.value))
	{
		fv.raiseError("You have not entered a town/city");
	}

	if (!fv.isEmpty(formname.myaddressthree.value) && !isValidlength(formname.myaddressthree.value, 4, formname.myaddressthree.maxLength))
	{
		fv.raiseError("You have not entered at least 4 letters for your town/city");
	}


	// check post code
	if (fv.isEmpty(formname.myaddressfive.value))
	{
		fv.raiseError("You have not entered a postcode");
	}

	if (!fv.isEmpty(formname.myaddressfive.value) && !isValidlength(formname.myaddressfive.value, 4, formname.myaddressfive.maxLength))
	{
		fv.raiseError("You have not entered at least 5 characters for your postcode");
	}

	// check for empty email address
	//if(formname.name == "invoice")
	//{
		if (fv.isEmpty(formname.myemailaddress.value))
		{
			fv.raiseError("You have not enter an email address");
		}
	//}

	// check for valid email address format
	if (!fv.isEmpty(formname.myemailaddress.value) &&
	!fv.isEmailAddress(formname.myemailaddress.value))
	{
		fv.raiseError("You have not entered a valid email address");
	}

	if (fv.numErrors() > 0)
	{
		fv.displayErrors();
		return false;
	}
	else
	{
		return true;
	}
	
}

function open_win(url)
{
      var the_win = window.open(url, "collieryroad", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=1,width=650,height=500");
      the_win.focus();
}

function popupMessage(sendto,thesubject,thedetails) 
{
	var doc = "mailto:" + sendto +       "?subject=" + escape(thesubject) +       "&body=" + escape(thedetails);   
	window.location = doc;
}

function open_win_with_menus(url)
{
      var the_win = window.open(url, "collieryroad", "toolbar=1,location=0,directories=1,status=1,menubar=1,scrollbars=1,resizable=1,width=650,height=500");
      the_win.focus();
}


// preload ost comon images
///(new Image(13,13)).src = "<?=$CFG['image_directory']?>/images/arrow3.gif";
//(new Image(6,6)).src = "<?=$CFG['image_directory']?>/images/orangesq6x6.gif";
//(new Image(28,12)).src = "<?=$CFG['image_directory']?>/images/button_new.gif";
//(new Image(200,60)).src = "<?=$CFG['image_directory']?>/images/colliery_road_computer_music.gif";

