/*
* Project				:	Interscience Accessibility - Validation Class
* File					:	validator.js
* Creation Date			:	7th June 2005
* Author				:	Ben Marvell
* Comments				: 	Updated validation to use form object instead of element object.
*/

// Validatior Object
// *****************
function Validator(formname){
	this.prop = new Array();
	this.fname = formname;
}

// Assign a properties to validate element
// FORMAT: formname, method (text, email, checkbox etc), maxlength & minlength (if any else null), alert text
Validator.prototype.addProps = function(elname, elfunc, maxlength, minlength, alertmsg, secelname){
	this.prop[this.prop.length] = new Properties(this.fname, elname, elfunc, maxlength, minlength, alertmsg, secelname);
}

Validator.prototype.isValid = function(){
	var passed = true;
	var msg = "";
	for (var x=0; x<this.prop.length; x++){
		
		var tmpstr = String(this.prop[x].elfunc);
		if (tmpstr.indexOf("issame") != -1){		
			if (this.prop[x-1].isValid() && this.prop[x-2].isValid()){
				if (!this.prop[x].isValid()){
					passed = false;
					msg+= this.prop[x].alertmsg + "\n";
				}
			}
		} else {	
			if (!this.prop[x].isValid()){
				passed = false;
				msg+= this.prop[x].alertmsg + "\n";
			}
		}
	}
	
	if (msg.length > 0){
		alert("You must correct the following error(s) before the form can be sent: \n\n" + msg);
	}
	return passed;
}

// Properties Object
// *****************
function Properties(form, elname, elfunc, maxlength, minlength, alertmsg, secelname){
	this.fname = form;
	this.elname = elname;
	this.elfunc = elfunc;
	this.maxlength = maxlength;
	this.minlength = minlength;
	this.alertmsg = alertmsg;
	this.secelname = secelname;
}

// assign methods to Properties object
Properties.prototype.isValid = function(){
	return this.elfunc();
}


// Validation methods
// ******************

// validates any number only characters and accepts the following "- +#"
function numbers(){
	var elidobj = document[this.fname][this.elname];
	var minlength = (this.minlength != null) ? this.minlength : 0;
	var maxlength = (this.maxlength != null) ? this.maxlength : "";
	
	var numb = new RegExp("^[0-9\\+\\#\\- ]{"+minlength+","+maxlength+"}$");
	if (numb.test(elidobj.value)){
		return true;
	} else 
		return false;
}

// validates any word only characters and accepts the following "-' ."
function text(){
	var elidobj = document[this.fname][this.elname];
	var minlength = (this.minlength != null) ? this.minlength : 1;
	var maxlength = (this.maxlength != null) ? this.maxlength : "";
	// the word character and ', ., - are escaped twice 
	// once for the javascript string and the second for the regexp.
	var reg = new RegExp("^[\\w\\'\\.\\- ]{"+minlength+","+maxlength+"}$");
	if (reg.test(elidobj.value)){
		return true;
	} else 
		return false;
}

// validates any word only characters and accepts the following "-' .#"
function address(){
	var elidobj = document[this.fname][this.elname];
	var minlength = (this.minlength != null) ? this.minlength : 1;
	var maxlength = (this.maxlength != null) ? this.maxlength : "";
	// the word character and ', ., - are escaped twice 
	// once for the javascript string and the second for the regexp.
	var reg = new RegExp("^[\\w\\'\\.\\-\\# ]{"+minlength+","+maxlength+"}$");
	if (reg.test(elidobj.value)){
		return true;
	} else 
		return false;
}

// currently accepts any text as doi's can accept most chars
// TODO: review which chars should be exempt from this
function doi(){
	var elidobj = document[this.fname][this.elname];
	var minlength = (this.minlength != null) ? this.minlength : 1;
	var maxlength = (this.maxlength != null) ? this.maxlength : "";
	var reg = new RegExp("^.{"+minlength+","+maxlength+"}$");
	if (reg.test(elidobj.value)){
		return true;
	} else 
		return false;
}

// used for the main site login
function login(){
	var elidobj = document[this.fname][this.elname];
	var minlength = (this.minlength != null) ? this.minlength : 1;
	var maxlength = (this.maxlength != null) ? this.maxlength : "";
	var reg = new RegExp("^.{"+minlength+","+maxlength+"}$");
	if (reg.test(elidobj.value)){
		return true;
	} else 
		return false;
}

// email validation
function email(){
	var elidobj = String(document[this.fname][this.elname].value);
	var reg = new RegExp(" ", "g");
	elidobj = elidobj.replace(reg, "");
	document[this.fname][this.elname].value = elidobj;
	var emailreg = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (emailreg.test(elidobj)){
		if (this.maxlength != null){
			if (elidobj.length <= this.maxlength){
				return true;
			} else {
				return false;
			}	
		} else {
			return true;
		}
	}
	else
		return false;
}

// checkbox validation
function checkbox(){
	var elidobj = document[this.fname][this.elname];
	if (elidobj.checked)
		return true;
	else
		return false;
}

// selectbox validation, looks at the select element value to check for empty.
function selectbox(){
	var elidobj = document[this.fname][this.elname];
	for (var x=0; x<elidobj.length; x++){
		if (elidobj[x].selected){
			if (elidobj[x].value != "")
				return true;
			else
				return false;
		}
	}
	//@@@DM It was anoying me this error
	return false;
}

// 
function password(){
	//return true;
	
	var elidobj = document[this.fname][this.elname];
	var minlength = (this.minlength != null) ? this.minlength : 1;
	var maxlength = (this.maxlength != null) ? this.maxlength : "";
	var passwordreg = new RegExp("^[a-zA-Z0-9._:-]{"+minlength+","+maxlength+"}$");
	if (passwordreg.test(elidobj.value))
		return true;
	else
		return false;	
}

function issame(){
	if (document[this.fname][this.elname].value != document[this.fname][this.secelname].value)
		return false;
	else 
		return true;
}

function validateGeoLocationForm(){
	var countrySelector = document.getElementById('countrySelector');
	var regionSelector = document.getElementById('regionSelector');
	if (countrySelector != null && countrySelector.value == ''){
		//no value was selected
		return false;
	}
  if (regionSelector != null && regionSelector.value == ''){
		//no value was selected
		return false;
	}
	return true
}

