var OAMS = {};
OAMS.BaseUrl = $('base_url').getValue();
OAMS.Validation = {};
OAMS.Validation.initialize = function() {
	Validation.add('validate-alpha-space', 'Please use alphabets and space only in this field', function (v) {
		return Validation.get('IsEmpty').test(v) ||  /^[a-z ][a-z' .]*$/i.test(v);
	});
	Validation.add('validate-date-select', 'Please make a selection', function (v, elm) {
		var p = elm.up();
		var options = p.select('SELECT');
		var status = $A(options).all(function(elm) {
			return $F(elm);
		});
		if(status == false) {
			$A(options).each(function(elm) {
				elm.addClassName('validation-failed');
			});
		} else {
			$A(options).each(function(elm) {
				elm.removeClassName('validation-failed');
			});
		}
		return status;
	});
	Validation.add('validate-phone', 'Please enter a valid phone number in this field', function (v) {
		return Validation.get('IsEmpty').test(v) ||  /^[0-9 +(][0-9 +(][0-9 +()-]*$/i.test(v);
	});
	Validation.add('validate-mobile', 'Please enter a valid mobile number in this field', function (v) {
		return Validation.get('IsEmpty').test(v) ||  /^[0-9 +]*$/i.test(v);
	});
	Validation.add('validate-phone-mobile', 'Please enter either phone or mobile', function (v, elm) {
		var p = elm.up().up();		
		var options = p.select('INPUT');
			
		var status = $A(options).any(function(elm) {
			return $F(elm);
		});
		if(status == false) {
			$A(options).each(function(elm) {
				elm.addClassName('validation-failed');
			});
		} else {
			$A(options).each(function(elm) {
				elm.removeClassName('validation-failed');
			});
		}
		return status;
	});
	Validation.add('validate-related-education', 'Please fill all the fields in this group', function (v, elm) {
		var p = elm.up().up();
		var options1 = p.select('INPUT[type=text]');
		var options2 = p.select('SELECT');
		var options = options1.concat(options2);
		var allFilled = $A(options).all(function(elm) {
			return (elm.identify() == 'tenth_subjects' || elm.identify() == 'plustwo_subjects' || elm.identify() == 
				'graduate_stream' || elm.identify() == 'post_graduate_stream') ? true : $F(elm);
		});
		var anyFilled = $A(options).any(function(elm) {
			return $F(elm);
		});
		var status = true;
		if(anyFilled == true) {
			if(allFilled == true) {
				status = true;
			} else {
				status = false;
			}
		} else {
			status = true;
		}
		if(status == false) {
			$A(options).each(function(elm) {
				if(elm.identify() != 'tenth_subjects' && elm.identify() != 'plustwo_subjects' && elm.identify() != 
						'graduate_stream' && elm.identify() != 'post_graduate_stream') { 
					elm.addClassName('validation-failed');
				}
			});
		} else {
			$A(options).each(function(elm) {
				if(elm.identify() != 'tenth_subjects' && elm.identify() != 'plustwo_subjects' && elm.identify() != 
						'graduate_stream' && elm.identify() != 'post_graduate_stream') {
					elm.removeClassName('validation-failed');
				}
			});
		}
		return status;
	});
	Validation.add('validate-related-simple-row', 'Please fill all the fields in this group', function (v, elm) {
		var p = elm.up().up();
		var options1 = p.select('INPUT[type=text]');
		var options2 = p.select('SELECT');
		var options = options1.concat(options2);
		var allFilled = $A(options).all(function(elm) {
			return $F(elm);
		});
		var anyFilled = $A(options).any(function(elm) {
			return $F(elm);
		});
		var status = true;
		if(anyFilled == true) {
			if(allFilled == true) {
				status = true;
			} else {
				status = false;
			}
		} else {
			status = true;
		}
		if(status == false) {
			$A(options).each(function(elm) {
				elm.addClassName('validation-failed');
			});
		} else {
			$A(options).each(function(elm) {
				elm.removeClassName('validation-failed');
			});
		}
		return status;
	});
	Validation.add('validate-related-complex-class', 'Please fill all the fields in this group', function (v, elm) {
		var p = elm.up().up();
		var className = p.readAttribute('class');
		var rows = p.up().select('TR.' + className);
		var options = [];
		for(var i = 0; i < rows.length; i++) {
			var temp_options = rows[i].select('INPUT[type=text]');
			options = options.concat(temp_options);
		}
		var allFilled = $A(options).all(function(elm) {
			return $F(elm);
		});
		var anyFilled = $A(options).any(function(elm) {
			return $F(elm);
		});
		var status = true;
		if(anyFilled == true) {
			if(allFilled == true) {
				status = true;
			} else {
				status = false;
			}
		} else {
			status = true;
		}
		if(status == false) {
			$A(options).each(function(elm) {
				elm.addClassName('validation-failed');
			});
		} else {
			$A(options).each(function(elm) {
				elm.removeClassName('validation-failed');
			});
		}
		return status;
	});
	Validation.add('validate-same-password', 'Both passwords must be same', function (v, elm) {
		var password = $('password');
		var confirmPassword = $('confirm_password');
		if(password != null && confirmPassword != null && password.getValue() == confirmPassword.getValue()) {
			return true;
		}
		return false;
	});
};

OAMS.Candidate = {};
OAMS.Candidate.Register = {};
OAMS.Candidate.Register.initialize = function() {
	new Validation('reg_form',{immediate:true});
};

document.observe('dom:loaded', function() {
	var candidateRegisterForm = $('reg_form');
	if(candidateRegisterForm != null)
		OAMS.Validation.initialize();
		OAMS.Candidate.Register.initialize();
});
