YAHOO.datasmit.forms = function(inputform) {
	this.init(inputform);
};

YAHOO.datasmit.forms.prototype = {
	frm: null,

	remove_error_div: function(name) {
		var err_div = document.getElementById('error_' + name);
		if (err_div) {
			var err_parent = err_div.parentNode;
			if (err_parent) {
				document.getElementById('error_' + name).className = 'defaulttext';
				err_parent.removeChild(err_div);
			}
		}
	},

	check_input: function(e, oself) {
		var input_name = this.name;
		var input_value = this.value;
		var required_fields = '|username|firstname|lastname|email|zip|city|phone_day|password|password2|address|companyname|';
		var check_fields ='|username|btw|country|email|'
		
		if (required_fields.indexOf('|' + input_name + '|') >= 0){
			oself.remove_error_div(input_name);
			if (input_value.length == 0){
				oself.show_error_message(this, 'no_value_entered');
				return;
			}
		}
		if (check_fields.indexOf('|' + input_name + '|') >= 0){
			var callback = {
			success: oself.input_success, 
			failure: oself.input_failure,
				argument: { 'oself': oself, 'input': this , 'input_name': input_name }
			};
			oself.remove_error_div(input_name);
			var current_url = oself.create_URL();

			switch (input_name){
				case "username" :
					var conn = Connect.asyncRequest('GET', current_url + '/auction/auction/ajax/functions_ajax.asp?act=checkname&name=' + input_name + '&loginName=' + input_value, callback);				
				break;
				case "btw" :
					var conn = Connect.asyncRequest('GET', current_url + '/auction/auction/ajax/functions_ajax.asp?act=validVATID&name=' + input_name + '&country=' + document.register['country'].value + '&vatid=' + input_value, callback);
				break;
				case "country" :
					var conn = Connect.asyncRequest('GET', current_url + '/auction/auction/ajax/functions_ajax.asp?act=validVATID&name=' + input_name + '&country=' + document.register['country'].value + '&vatid=' + input_value, callback);
				break;
				case "email" :
					var conn = Connect.asyncRequest('GET', current_url + '/auction/auction/ajax/functions_ajax.asp?act=checkemail&name=' + input_name + '&email=' + input_value, callback);
				break;
			}
		}
		
		if (input_name == 'iscompany') {
			if (document.register['iscompany'][0].checked){
				document.getElementById('companyname').style.display = '';
				alert
				if (document.getElementById('companykvk').getAttribute('show') != 'no'){
					document.getElementById('companykvk').style.display = '';
				}
				if (document.getElementById('companyvat').getAttribute('show') != 'no'){
					document.getElementById('companyvat').style.display = '';
				}
			} else {
				document.getElementById('companyname').style.display = 'none';
				document.getElementById('companykvk').style.display = 'none';
				document.getElementById('companyvat').style.display = 'none';
			}
		}

		if ((document.register['password'].value != '') && (document.register['password2'].value != '')) {
			if (document.register['password'].value != document.register['password2'].value){
				oself.show_error_message(this, 'password_no_match');
				document.register['password2'].value = '';
				oself.remove_error_div('password');
				document.register['password2'].focus();
				return;
			}
		}
		if (input_name == 'password'){
			if (document.register['password'].value.length < 5){
				oself.remove_error_div('password')
				oself.show_error_message(this, 'password_too_short');
				document.register['password'].focus();
				return;
			}
		}
		if (input_name == 'password2'){
			if (document.register['password'].value.length < 5){
				oself.remove_error_div('password')
				oself.show_error_message(this, 'password_too_short');
				document.register['password'].focus();
				return;
			}
		}
		
		if ((document.register['email'].value != '') && (document.register['email2'].value != '')) {
			if (document.register['email'].value != document.register['email2'].value){
				oself.show_error_message(this, 'email_no_match');
				document.register['email2'].value = '';
				oself.remove_error_div('email2');
				document.register['email2'].focus();
				return;
			}
		}
		
		if ((document.register['password'].value != '') && (document.register['password2'].value != '')) {
			oself.remove_error_div('password');
			oself.remove_error_div('password');
		}
	},

	final_check: function(e, oself){
		var required_fields_check = new Array('username','firstname','lastname','email','zip','city','phone_day','password','password2','address');
		if (document.register['iscompany']['0'].checked){
			required_fields_check.push('companyname');
			if (document.getElementById('companyvat').style.display != 'none'){
				required_fields_check.push('btw');	
			}
		}
		
		var error_found = false;

		if (document.register['password'].value != document.register['password2'].value){
			oself.show_error_message(document.register['password'], 'password_no_match');
			error_found = true;
		}

		var i = 0;
		do {
			oself.remove_error_div(document.register[required_fields_check[i]].name);
			if (document.register[required_fields_check[i]].value == ''){
				oself.show_error_message(document.register[required_fields_check[i]], 'no_value_entered')
				error_found = true;
			}
			i++;
		}
		while (i < (required_fields_check.length));
		
		if (error_found){
			Event.preventDefault(e);
		}
	},
	
	show_error_message: function(oInput, err_string) {
			var callback = {
			success: this.input_success, 
			failure: this.input_failure,
				argument: { 'oself': this, 'input': oInput, 'input_name': 'error_string'  }
			};
			var current_url = this.create_URL();
			var conn = Connect.asyncRequest('GET', current_url + '/auction/auction/ajax/functions_ajax.asp?act=getmessage&color=red&message=' + err_string, callback);
	},

	show_error: function(o, err_string) {
		if (o.type == 'text') {
			o.className = 'defaulttext';
		}
		var msg = document.createElement('DIV');
		msg.innerHTML = err_string;
		msg.style.color = 'red';
		msg.className = 'errortext';

		msg.id = 'error_' + o.name;
		o.parentNode.appendChild(msg);
	},

	input_success: function(o) {
		var response = o.responseText;
		var oself = o.argument.oself;
		var input = o.argument.input;
		var input_name = o.argument.input_name;
		var error_code
		switch (input_name){
			case "username":
				if (response.length > 0){
					oself.show_error(input, response);
				}
			break;
			case "email":
				if ((response.length > 0)&&(response != "ok")){
					oself.show_error(input, response);
				}
			break;
			case "btw":
				if ((response.length > 0)&&(response != "skip")&&(response != "skip_check")&&(response != "ok")){
						oself.show_error(input, response);
				}
			break;
			case "country":
				if (document.register['iscompany']['0'].checked){
					if (response == 'skip'){
						document.getElementById('companyvat').style.display = 'none';
						document.getElementById('companyvat').setAttribute('show', 'no');
						document.getElementById('companykvk').style.display = 'none';
						document.getElementById('companykvk').setAttribute('show', 'no');
					} else {
						document.getElementById('companyvat').style.display = '';
						document.getElementById('companyvat').setAttribute('show', 'yes');
						document.getElementById('companykvk').style.display = '';
						document.getElementById('companykvk').setAttribute('show', 'yes');
					}
				} else {
					if (response == 'skip'){
						document.getElementById('companyvat').setAttribute('show', 'no');
						document.getElementById('companykvk').setAttribute('show', 'no');
					} else {
						document.getElementById('companyvat').setAttribute('show', 'yes');
						document.getElementById('companykvk').setAttribute('show', 'yes');
					}
					
				}
			break;
			case "error_string":
					oself.show_error(input, response);
			break;
		}
	},
	
	input_failure: function(o) {
		alert('test');
		alert(o.statusText);
	},
	
	create_URL: function(e, oself) {
		var current_url = document.URL;
		current_url = current_url.slice(7, (current_url.length-7));
		current_url = current_url.slice(0, current_url.indexOf("/"));
		current_url = 'http://' + current_url;
		return current_url;
	},
	
	check_password: function(password) {
		var ok = 0; 
		var passValue;
		passValue = password;
		if (passValue.match(/[A-Z]/)){ ok = ok + 1}; 
		if (passValue.match(/[a-z]/)){ ok = ok + 2}; 
		if (passValue.match(/[0-9]/)){ ok = ok + 3}; 
		if (passValue.match(/[@#$%&!*)(-+=^]/)){ ok = ok + 4}; 
		ok = ok * passValue.length;
		return ok;
	},
	
	move_box: function(an, box) {
		var cleft = 0;
		var ctop = 0;
		var obj = an;
		
		while (obj.offsetParent) {
			cleft += obj.offsetLeft;
			ctop += obj.offsetTop;
			obj = obj.offsetParent;
		}
		
		box.style.left = cleft + 'px';
		ctop += an.offsetHeight + 8;
		if (document.body.currentStyle && document.body.currentStyle['marginTop']) {
			ctop += parseInt(document.body.currentStyle['marginTop']);
		}
		box.style.top = ctop + 'px';
	},

	show_hide_box: function(an, width, height, borderStyle, message) {
		var current_url = this.create_URL();
		var href = current_url + '/auction/auction/ajax/functions_ajax.asp?act=getmessage&message=' + message;
		var boxdiv = document.getElementById(href);
		if (boxdiv != null) {
			if (boxdiv.style.display=='none') {
				//move_box(an, boxdiv);
				boxdiv.style.display='block';
			} else
				boxdiv.style.display='none';
		return false;
		}
		
		boxdiv = document.createElement('div');
		boxdiv.setAttribute('id', href);
		boxdiv.setAttribute('class', 'defaulttext');
		boxdiv.style.display = 'block';
		boxdiv.style.position = 'absolute';
		boxdiv.style.width = width + 'px';
		boxdiv.style.height = height + 'px';
		boxdiv.style.padding = '10px';
		boxdiv.style.border = borderStyle;
		boxdiv.style.backgroundColor = '#FFFFFF';
		
		var contents = document.createElement('iframe');
		contents.scrolling = 'no';
		contents.frameBorder = '0';
		contents.style.width = width + 'px';
		contents.style.height = height + 'px';
		contents.src = href;
		
		boxdiv.appendChild(contents);
		document.body.appendChild(boxdiv);
		this.move_box(an, boxdiv);
		
		return false;
	},
	
	show_support: function(e, oself){
		oself.show_hide_box(this, 300, 100, '2px solid #000000', 'help_password');
	},
	
	strong_password: function(e, oself) {
		if (this.name == 'password'){
			oself.remove_error_div('password')
			if (document.register['password'].value.length >= 5) {
				var password_indicator_text = '';
				var strong_password = 0;
				strong_password = oself.check_password(document.register['password'].value);
				strong_password = strong_password * 2;
				if (strong_password <= 75){ password_indicator_text= 'Weak';}
				if ((strong_password > 75) && (strong_password <= 125)){ password_indicator_text = 'moderate';}
				if ((strong_password > 125) && (strong_password <= 175)){ password_indicator_text = 'Strong';}
				if (strong_password > 175){ password_indicator_text = 'Very Strong';}
				if (strong_password > 200) {strong_password = 200;}
				if (strong_password  < 35) {strong_password = 35;}
				var password_indicator = '<TABLE background="/auction/auction/images/gauge.gif" height=10 width=200 cellSpacing=0 cellPadding=0 style="border: 1px #000000 solid; margin-bottom: 3px;"><tr><td><TABLE height="100%" width=' + strong_password + ' cellSpacing=0 cellPadding=0 border= 0> <TR><TD><span class="defaulttext" style="background: none;"><font color=white><b>&nbsp;' + password_indicator_text + '</b></font></span></TD></TR></TABLE></td><td><TABLE bgcolor="#FFFFFF" height="100%" width="' + (200 - strong_password) + '" cellSpacing=0 cellPadding=0 border= 0> <TR><TD></TD></TR></TABLE></td></tr></table>';
				document.getElementById('strong_password').innerHTML = password_indicator;
				document.getElementById('strong_password').style.display = 'block';
				document.getElementById('strong_password_text').style.display = 'block';
			} else {
				document.getElementById('strong_password').innerHTML = '';
				document.getElementById('strong_password').style.display = 'none';
				document.getElementById('strong_password_text').style.display = 'none';
			}
		}
	},

	init: function(inputform) {
		var current_location = top.location.href;
		var load_form = false;
		if ((current_location.indexOf('register.asp') > 0)&&(current_location.indexOf('act=register') > 0)){
			load_form = true;
		}
		if ((current_location.indexOf('register.asp') > 0)&&(current_location.indexOf('act') == -1)){
			load_form = true;
		}
		if (load_form) {
			this.frm = document.forms[inputform];
			if (this.frm) {
				var inputs = this.frm.getElementsByTagName('INPUT');
				Event.removeListener(inputs, 'blur', this.check_input, this);
				Event.addListener(inputs, 'blur', this.check_input, this);
	
				var selects = this.frm.getElementsByTagName('SELECT');
				Event.removeListener(selects, 'change', this.check_input, this);
				Event.addListener(selects, 'change', this.check_input, this);
	
				Event.removeListener(document.forms['register']['iscompany'], 'blur', this.check_input, this);
				Event.removeListener(document.forms['register']['iscompany'], 'click', this.check_input, this);
				Event.addListener(document.forms['register']['iscompany'], 'click', this.check_input, this);
	
				Event.removeListener(document.forms['register'], 'submit', this.final_check, this);
				Event.addListener(document.forms['register'], 'submit', this.final_check, this);
				
				var support = document.getElementById('support')
				Event.removeListener(support, 'mouseover', this.show_support, this);
				Event.addListener(support, 'mouseover', this.show_support, this);
				Event.removeListener(support, 'mouseout', this.show_support, this);
				Event.addListener(support, 'mouseout', this.show_support, this);
	
	
				Event.removeListener(document.forms['register']['password'], 'keyup', this.strong_password, this);			
				Event.addListener(document.forms['register']['password'], 'keyup', this.strong_password, this);
			}
		}
	}
}


YAHOO.datasmit.inputhover = function(inputid, blurtext, focustext) {
	this.init(inputid, blurtext, focustext);
};

YAHOO.datasmit.inputhover.prototype = {
	init: function(inputid, blurtext, focustext) {
		if (document.getElementById(inputid)) {
			Event.addListener(inputid, 'focus', this.change_input_value, [blurtext, focustext]);
			Event.addListener(inputid, 'blur', this.change_input_value, [focustext, blurtext]);
		}
	},

	change_input_value: function(e, texts) {
		if (this.value == texts[0]) {
			this.value = texts[1];
		}
	}
};