
		//****************************\\
		// COPYRIGHT GOLFBOX A/S 2003 \\
		//	   ALL RIGHTS RESERVED    \\
		//----------------------------\\
		//	 NOT TO BE USED WITHOUT   \\
		//   PRIOR WRITTEN APPROVAL   \\
		//----------------------------\\
		//         VERSION 0.0        \\
		//       WRITTEN BY: KB       \\
		//----------------------------\\


function clientValidator(Country_IsoCode){
	this.items = new Array()
	this.conditionItems = new Array()
	this.errors = new Array()
	this.isValid = true
	this.Country_IsoCode = (arguments[0]) ? arguments[0] : 'dk'
	this.renderMethod = 'summery'
	this.hasBeenValidated = false
}

clientValidator.prototype.reset = function (){
	this.errors = new Array();
	this.isValid = true;
	this.hasBeenValidated = false;

	if (document.getElementById('clientErrorContainer')){
		if (document.getElementById('clientErrorContainer').innerHTML != ''){
			document.getElementById('clientErrorContainer').innerHTML = ''
		}
	}
}

clientValidator.prototype.add = function (objValidator){
	i = this.items.length
	this.items[i] = objValidator
}

clientValidator.prototype.addCondition = function (condition,objValidator){
	i = this.conditionItems.length
	this.conditionItems[i] = objValidator;
	this.conditionItems[i].condition = condition;
}

clientValidator.prototype.addError = function (objValidator){
	i = this.errors.length
	this.errors[i] = objValidator
}

clientValidator.prototype.validate = function (){
	this.reset()
	this.onValidateStart()
	for(var u = 0; u < this.items.length; u++){
		if (!this.items[u].validate()){
			this.addError(this.items[u])
			this.isValid = false
		}
	}

	// CONDITION ITEMS, only added and validated if the condition is true!
	for(var u = 0; u < this.conditionItems.length; u++){
		if(eval(this.conditionItems[u].condition)){
			if (!this.conditionItems[u].validate()){
				this.addError(this.conditionItems[u])
				this.isValid = false
			}
		}
	}

	this.onValidateEnd()
	this.hasBeenValidated = false
	return this.isValid
}

clientValidator.prototype.getErrorText = function (errIndex){
	err = this.errors[errIndex]
	etext = err.errorText
	etext2 = ''
	fname = "fieldname not found"
	
	if(etext.indexOf('%f') >= 0){
		if(document.getElementById(err.clientId)){
			if(document.getElementById('lbl_' + err.clientId)){
				fname = document.getElementById('lbl_' + err.clientId).innerHTML
			} else {
				fname = document.getElementById(err.clientId).getAttribute("fieldname")
			}
			etext2 = etext.replace('%fieldname%','"' + fname +'"')
		}
	} else {
		etext2 = etext
	}

	return etext2
}

clientValidator.prototype.render = function (){
	switch (this.renderMethod) {
		case 'summery' :
			var str = ''
			str += '<table cellpadding="0" cellspacing="0" width="100%" ID="tblError" class="tblError">\n'
			for(var f = 0; f < this.errors.length; f++){
				str += '<tr>\n'
					str += '<td width="15" align="center"><img src="/images/shared/arrow_error.gif" border="0" width="10" height="10"></td>\n'
					str += '<td>'+this.getErrorText(f)+'</td>\n'
				str += '</tr>\n'
			}
			str += '</table>\n'

			document.getElementById('clientErrorContainer').innerHTML = ''
			document.getElementById('clientErrorContainer').innerHTML = str

			document.location.href = '#pageTopAnchor'
		break;
		case 'alertbox' :
			var str = ''
			for(var f = 0; f < this.errors.length; f++){
				str += this.getErrorText(f) + '\n'
			}
			alert(str)
		break;
	}
}

/*				*\
	  EVENTS    
\*				*/ 

clientValidator.prototype.onValidateEnd = function (){}
clientValidator.prototype.onValidateStart = function (){}


/*				*\
	VALIDATORS    
\*				*/ 

function getFormField(clientId){
	if (document.getElementById(clientId) == null){alert('The requested field ['+clientId+'] was not found\nThe page can not be validated\n\nPlease contact GolfBox!');return false};
	if (document.getElementById(clientId) != null){
		return document.getElementById(clientId).value
	}
}

function trim(s) {
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}

/* HELPER FUNCTIONS */
function _validHour(theHour){
	var m = theHour.split(':')
	if(parseInt(m[0]) <= 23 && parseInt(m[1]) <= 59){
		return true
	}
	else{
		return false
	}
}

function _daysInMonth(intMonth,intYear){
	if(intMonth == 1 || intMonth == 3 || intMonth == 5 || intMonth == 7 || intMonth == 8 || intMonth == 10 || intMonth == 12){
		return 31
	}
	else if(intMonth == 4 || intMonth == 6 || intMonth == 9 || intMonth == 11){
		return 30
	}
	else if(intMonth == 2){
		if(_IsLeapYear(intYear)){
			return 29
		}
		else{
			return 28
		}
	} else {
		return 0
	}
}

function _validDate(source){
	
	if(typeof(source) == 'object'){
		datePartArray = cc.getDateParts(source.value)
	} else {
		datePartArray = cc.getDateParts(getFormField(source))
	}

	var tmpDaysInMonth = _daysInMonth(datePartArray[1],datePartArray[0])

	if(datePartArray[2] <= tmpDaysInMonth && datePartArray[1] <= 12 && datePartArray[0] <= 2099 && datePartArray[0] >= 1900){
		return true
	}
	else{
		return false
	}
}

function _regExp(objValidator,strPattern){
	theVal = getFormField(objValidator.clientId)

	var m = theVal.match(strPattern)
	return (m == null || m.length < 0) ? false : true
}

function _IsLeapYear(intYear){

	if(intYear.length == 2){
		if(intYear > 30){
			intYear = parseInt('19'+intYear)
		}
		else{
			intYear = parseInt('20'+intYear)
		}
	}
		
	if(intYear % 400 == 0){
		return true
	}
	else if(intYear % 100 == 0){
		return false
	}
	else if(intYear % 4 == 0){
		return true
	}
	else{
		return false
	}
	
}

function getFormFieldObj(clientId){
	if (document.getElementById(clientId) == null){alert('The requested field ['+clientId+'] was not found\nThe page can not be validated\n\nPlease contact GolfBox!');return false};
	if (document.getElementById(clientId) != null){
		return document.getElementById(clientId)
	}
}

// HERE STARTS THE CODE FOR THE VALIDATING FUNCTIONS 

// ### CUSTOM VALIDATOR ### \\
function customValidator(fPointer){
	this.fPointer = fPointer
}

customValidator.prototype.validate = function () {
	var theObj = eval('new ' + this.fPointer)
	this.errorText = theObj.errorText
	return theObj.isValid
}

// ### regExpValidator VALIDATOR ### \\
function regExpValidator(strClientId, strErrorText, regExp_Pattern, blnAllowEmpty){
	this.clientId			= strClientId
	this.errorText			= strErrorText
	
	var strType = typeof(regExp_Pattern)
	if(strType.toLowerCase() == 'string'){
		try{
			var objRegExpL = new regExpLibrary()
			this.pattern = objRegExpL.getRegExp(regExp_Pattern) 
		} catch (er) {
			strTyp = typeof(objRegExpL)
			if(strTyp.toLowerCase() != 'object'){
				alert('Please import RegExp Library, validation failed.');
				return false
			}
		}
		
	} else{
		this.pattern = regExp_Pattern
	}
	this.allowEmpty = (arguments[3]) ? arguments[3] : false
}

regExpValidator.prototype.validate = function (){
	if (trim(getFormField(this.clientId)) == '' && this.allowEmpty) return true;
	return _regExp(this,this.pattern)
}


// ### INRANGE VALIDATOR ### \\
function inRangeValidator(strClientId, strErrorText, rangeMin, rangeMax, dataType){
	this.clientId	= strClientId
	this.errorText	= strErrorText
	this.rangeMin	= rangeMin
	this.rangeMax	= rangeMax
	this.dataType	= (arguments[4]) ? arguments[4] : 'integer'
}

inRangeValidator.prototype.validate = function (){
	theVal = getFormField(this.clientId)
	
	switch(this.dataType.toLowerCase()){
		case 'decimal':
			theVal = cc.toDecimal(theVal)
			break;
		case 'integer' :
			theVal = parseInt(theVal)
			break;
	}
	
	return (theVal >= this.rangeMin && theVal <= this.rangeMax) ? true : false
}

// ### COMPARE VALIDATOR ### \\
function compareValidator(clientId1,clientId2,strOperator,strErrorText,dataType){
	this.clientId1	= clientId1
	this.clientId2	= clientId2
	this.operator	= strOperator
	this.errorText	= strErrorText
	this.dataType	= (arguments[4]) ? dataType : 'integer'
}

compareValidator.prototype.validate = function (){
	
	var v1 = getFormField(this.clientId1)
	var v2 = getFormField(this.clientId2)
	
	switch(this.dataType.toLowerCase()){
		case 'integer':
			v1 = parseInt(v1)
			v2 = parseInt(v2)
			break;
		case 'string':
			v1 = v1.toString()
			v2 = v2.toString()
			break;
		case 'handicap':
			v1 = cc.toHandicap(v1);
			v2 = cc.toHandicap(v2);
			break;
		default:
			v1 = v1.toString()
			v2 = v2.toString()
			break;
	}
	
	switch (this.operator){
		case "!=":
			return (v1 != v2);
		case "<>":
			return (v1 != v2);
		case ">":
			return (v1 > v2);
		case ">=":
			return (v1 >= v2);
		case "<":
			return (v1 < v2);
		case "<=":
			return (v1 <= v2);
		case "=":
			return (v1 == v2);
		case "==":
			return (v1 == v2);
		default:
			alert('( '+this.operator+' ) is not a valid compare operator!');
			return false;
	}
 }

// ### DATE VALIDATOR ### \\
function dateValidator(strClientId,strErrorText,blnAllowEmpty,minDate,maxDate,strFormat){
	this.clientId			= strClientId
	this.errorText			= strErrorText
	this.allowEmpty			= blnAllowEmpty
	this.minDate			= minDate
	this.maxDate			= maxDate
	this.strFormat			= (arguments[5]) ? arguments[5] : 'dd-mm-yyyy'
}

dateValidator.prototype.validate = function() {
	if (trim(getFormField(this.clientId)) == '' && this.allowEmpty) return true;
	changeInputDate(getObj(this.clientId));
	//if (!_regExp(this,/^\d{2}-\d{2}-\d{4}$/)){
	//function regExpValidator(strClientId, strErrorText, regExp_Pattern, blnAllowEmpty){
	if ( ! new regExpValidator(this.clientId, this.errorText, 'date', this.allowEmpty).validate() ){
		return false
	}
	else{
		return _validDate(this.clientId)
	}
}

// ### TIME VALIDATOR ### \\
function timeValidator(strClientId,strErrorText,blnAllowEmpty){
	this.clientId				= strClientId
	this.errorText			= strErrorText
	this.allowEmpty			= blnAllowEmpty
}

timeValidator.prototype.validate = function() {
	if (trim(getFormField(this.clientId)) == '' && this.allowEmpty) return true;

	if (!_regExp(this,/^[0-2][0-9]:[0-5][0-9]$/)){
		return false
	}
	else{
		return _validHour(getFormField(this.clientId))
	}
}

// ### DECIMAL FIELD VALIDATOR ### \\
function decimalValidator(strClientId,strErrorText,blnAllowEmpty,intDecimals){
	this.clientId			= strClientId
	this.errorText			= strErrorText
	this.allowEmpty			= (arguments[2]) ? arguments[2] : false
	this.intDecimals		= (arguments[3]) ? arguments[3] : 2
}

decimalValidator.prototype.validate = function(){
	if(trim(getFormField(this.clientId)) == ''  && this.allowEmpty) {return true;}
	if(this.intDecimals == 1)
		return _regExp(this,/^\-*\d+((,|\.)\d{1,1})*$/)
	else
	    if(this.intDecimals == 3)
		    return _regExp(this,/^\-*\d+((,|\.)\d{1,3})*$/)
	    else
	        if(this.intDecimals == 4)
		        return _regExp(this,/^\-*\d+((,|\.)\d{1,4})*$/)
	        else
		        return _regExp(this,/^\-*\d+((,|\.)\d{1,2})*$/);
}

// ### EMAIL VALIDATOR ### \\
function emailValidator(strClientId,strErrorText,blnAllowEmpty){
	this.clientId		= strClientId
	this.errorText		= strErrorText
	this.allowEmpty		= (arguments[2]) ? arguments[2] : false
}

emailValidator.prototype.validate = function(){
	if(document.getElementById(this.clientId)!=null){
		if(trim(getFormField(this.clientId)) == ''  && this.allowEmpty) {return true;}
		document.getElementById(this.clientId).value = trim(document.getElementById(this.clientId).value);
	}
	// PATTERN REVISED 2003-08-21
	// ASTERIX inserted after "@[a-zA-Z0-9]"
	//return _regExp(this,/^[0-9]*[a-zA-Z]*[\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9]*[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/)
	// PATTERN REVISED 2006-03-16
	//return _regExp(this,/^[\w-_\.]*[\w-_\.]@[a-zA-Z0-9]*[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/)
	//MS: return _regExp(this, /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/)
	// PATTERN REVISED 2007-09-14
	// The Official Standard: RFC 2822
	return _regExp(this,/^[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-zA-Z0-9!#$%&'*+/=?^_`{|}~-]+)*@[a-zA-Z0-9]*[\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$/)
}

// ### NUMBER FIELD VALIDATOR ### \\
function numberValidator(strClientId,strErrorText,blnAllowEmpty,intMinValue,intMaxValue){
	this.clientId		= strClientId
	this.errorText		= strErrorText
	this.allowEmpty		= (arguments[2]) ? arguments[2] : false
	this.minValue		= (intMinValue == 'undefined' || intMinValue == null) ? 0 : intMinValue
	this.maxValue		= (intMaxValue == 'undefined' || intMaxValue == null) ? null : intMaxValue
}

numberValidator.prototype.validate = function(){
	if(trim(getFormField(this.clientId)) == ''  && this.allowEmpty) {return true;}

	if(_regExp(this,/^-?\d+$/)){
		theVal = parseInt(getFormField(this.clientId))
		if(this.minValue != null && this.maxValue != null){
			if(theVal < this.minValue || theVal > this.maxValue){
				return false
			} else {
				return true
			}
		} else if(this.minValue != null){
			return (theVal < this.minValue) ? false : true
		} else if(this.maxValue != null){
			return (theVal > this.maxValue) ? false : true
		} else{
			return true
		}
	} else {
		return false
	}
}

// ### REQUIRED FIELD VALIDATOR ### \\
function requiredValidator(strClientId,strErrorText){
	this.clientId		= strClientId
	this.errorText		= strErrorText
}

requiredValidator.prototype.validate = function (){
	theVal = getFormField(this.clientId)
	return (trim(theVal) != '') ? true : false
}

// ### HCP VALIDATOR ### \\
function hcpValidator(strClientId,strErrorText,blnAllowEmpty){
	this.clientId		= strClientId
	this.errorText		= strErrorText
	this.allowEmpty		= (arguments[2]) ? arguments[2] : false
}

hcpValidator.prototype.validate = function (){
	if(trim(getFormField(this.clientId)) == ''  && this.allowEmpty) {return true;}
	theVal = getFormField(this.clientId)

	if(page.countryISOCode=='ZA'&&!_regExp(this,/^\+*\d+$/)){
		return false
	}

	if(!_regExp(this,/^\+*\d+((,|\.)\d{1,1})*$/)){
		return false
	}

	if(cc.toDecimal(theVal) != 99 && cc.toDecimal(theVal) != 98){
		if(!new inRangeValidator(this.clientId,'',0,90,'decimal').validate()){
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

// ### appliesToValidator VALIDATOR ### \\
function appliesToValidator(strClientId,strErrorText){
	this.clientId	= strClientId;
	this.errorText	= strErrorText;	
}

appliesToValidator.prototype.validate = function(){
	var theList = document.getElementById(this.clientId).value
	if(parseInt(theList.indexOf(';')) < 1 ){
		return false
	} else {
		return true
	}
}

// ### radioValidator VALIDATOR ### \\
function radioValidator(strClientId,strErrorText){
	this.clientId	= strClientId;
	this.errorText	= strErrorText;	
}

radioValidator.prototype.validate = function(){
	rdoArray = document.getElementsByName(this.clientId)
	for(var i = 0; i < rdoArray.length; i++){
		if(rdoArray[i].checked){
			return true
			break;
		}
	}
	return false
}

// ### requiredCheckbox VALIDATOR ### \\
function requiredCheckbox(strClientIds,strErrorText, minChecked, maxChecked){
	this.clientIds = strClientIds
	this.errorText = strErrorText
	this.minChecked = minChecked
	this.maxChecked = maxChecked
}

requiredCheckbox.prototype.validate = function (){	
	if(!typeof(this.clientIds) == 'object'){
		theVals = this.clientIds.split(",")
	} else {
		theVals = this.clientIds
	}
	
	var iChecked = 0

	this.minChecked = (typeof(this.minChecked) == 'undefined') ? 1 : this.minChecked;
	this.maxChecked = (typeof(this.maxChecked) == 'undefined') ? theVals.length : this.maxChecked;

	if(this.maxChecked < this.minChecked){
		this.errorText = 'Validation error: MaxValue < MinValue!'
		return false
	}

	for (i=0; i < theVals.length; i++) {
		if (getFormFieldObj(theVals[i]).checked==true) {iChecked++;}
	}

	if (iChecked >= this.minChecked && iChecked <= this.maxChecked){
		return true
	} else {
		return false
	}
}

// ### pinkode VALIDATOR ### \\
function pinCodeValidator(strClientId,strErrorText){
	this.clientId = strClientId
	this.errorText = strErrorText
}

pinCodeValidator.prototype.validate = function (){
	return _regExp(this,/^\d{4}$/)
}

/* RUNTIME VALIDATION */
function checkMaxLength(objField,intLength, strError){
	if(objField.value.length > intLength-1){
		objField.value = objField.value.substring(0,intLength-1) // crop one to make sure the field is still editable..
		//if (strError){alert(strError)}
		if (strError.indexOf('%NumOfChars%') > 0){
			alert(strError.replace('%NumOfChars%',intLength))
		} else {
			alert(strError)
		}
		return false
	}
}

/* MemberNumber validator */
function memberNumberValidator(strClientId, strErrorText, allowEmpty){
	this.clientId		= strClientId
	this.errorText		= strErrorText
	this.allowEmpty		= (arguments[2]) ? arguments[2] : false
}

memberNumberValidator.prototype.validate = function(){
	if(trim(getFormField(this.clientId)) == ''  && this.allowEmpty) {return true;}
	return _regExp(this,/^\d{1,4}(\-)\d{1,}$/)
}

/* GolfId validator */
function golfIDValidator(strClientId, strErrorText, allowEmpty){
	this.clientId		= strClientId
	this.errorText		= strErrorText
	this.allowEmpty		= (arguments[2]) ? arguments[2] : false
}

golfIDValidator.prototype.validate = function(){
	if(trim(getFormField(this.clientId)) == ''  && this.allowEmpty) {return true;}
	return _regExp(this,/^\d{6}-\d{3}$/)
}
