String.prototype.trim = function() {
	return this.replace(/(^\s+)|(\s+$)/g, '');
};
var ff = /Firefox/.test(window.navigator.userAgent);
var chrome = /Chrome/.test(window.navigator.userAgent);
if (ff || chrome) {
	HTMLElement.prototype.__defineGetter__("currentStyle", function() {
		return this.ownerDocument.defaultView.getComputedStyle(this, null);
	});
	window.attachEvent = HTMLElement.prototype.attachEvent = function(type, fn) {
		type = type.substring(2);
		this.addEventListener(type, fn, false);
	};
}

function addEvent(target, type, method) {
	var args = Array.prototype.slice.call(arguments, 3);
	target.attachEvent(type, function() {
		return method.apply(target, args);
	});
}
function addClass(el, className) {
	el.className = el.className.replace(
			new RegExp('\\b' + className + '\\b|$'), ' ' + className + ' ');
}
function removeClass(el, className) {
	el.className = el.className.replace(new RegExp('\\b' + className + '\\b'),
			' ');
}
function id(id) {
	return document.getElementById(id);
}
function tag(nm, el) {
	return (el || document).getElementsByTagName(nm);
}
function get(url) {
	var r;
	try {
		r = new XMLHttpRequest();
	} catch (e) {
		try {
			r = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (ee) {
			try {
				r = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (eee) {
				return null;
			}
		}
	}
	r.open("GET", url, false);
	r.send(null);
	var result = null;
	if (r.status == 200) {
		result = r.responseText.trim();
	}
	r = null;
	return result;
}

function changePhoneHint() {
	$('#phonehint').css('display') == 'none' ? $('#phonehint').css('display', 'inline-block') : $('#phonehint').css('display', 'none');
}

function onSuccess(el) {
	if (el.name == 'testdate')return false;
	var hint = document.getElementById(el.name + 'hint');
	hint.className = 'ok';
	hint.innerHTML = '<img src="/apply/images/ok.png"/>';
	var stat = document.getElementById(el.name + 'stat');
	stat.className = '';
	stat.innerHTML = '';
}

function onError(el, msg) {
	var es = el.form.getElementsByTagName('SPAN');
	for ( var i = 0; i < es.length; i++) {
		var e = es[i];
		if (/hint$/g.test(e.id) && e.className == 'explain') {
			if (!el.value) {
				e.innerHTML = '';
				e.className = '';
			}
		}
	}
		var hint = document.getElementById(el.name + 'hint');
		var stat = document.getElementById(el.name + 'stat');
		if (/^test*/.test(el.name)) {
			hint = document.getElementById('testhourhint');
		}
		hint.className = 'explain';
		hint.innerHTML = msg;
		stat.className = 'ok';
		stat.innerHTML = '<img src="/apply/images/error.png"/>';
		
}

var Form = {
	error : null,
	input : function(el, reg, msg, rep) {
		if (reg.test(el.value)) {
			Form.error = null;
			onSuccess(el);
			return true;
		} else {
			Form.error = el;
			onError(el, msg);
			return false;
		}
		return false;
	},
	select : function(el, msg) {
		if (el.value) {
			Form.error = null;
			onSuccess(el);
			return true;
		} else {
			Form.error = el;
			onError(el, msg);
			return false;
		}
		return false;
	},
	radio : function(els, msg) {
		var val = false;
		if (els.length) {
			for ( var i = 0; i < els.length; i++) {
				if (els[i].checked) {
					val = els[i].value;
					break;
				}
			}
		} else {
			val = els.checked;
		}
		var genderid = document.getElementById('genderid');
		if (val === false) {
			onError(genderid, msg);
			return false;
		}
		onSuccess(genderid);
		return true;
	},
	valid : function(el, url, msg) {
		var r = url + el.value;
		r = get(r);
		if (r === '1') {
			Form.error = el;
			onError(el, msg);
			return false;
		} else {
			Form.error = null;
			onSuccess(el);
			return true;
		}
		return false;
	}
};

var Functions = {
	name : function() {
		return Form.input(this, /^[\u4e00-\u9fa5]{1,6}$/, '请输入您的中文名。', /[^\u4e00-\u9fa5]+/g);
	},
	englishname : function() {
		return Form.input(this, /^[\w]{1,40}$/, '请输入您的英文名。', /[^\w]+/g);
	},
	genderid : function() {
		return Form.radio(this, '请选择您的性别。');
	},
	agerangeid : function() {
		return Form.select(this, '请选择您的职业。');
	},
	email : function() {
		return Form.input(this, /^[\w\_\.]+@\w+(\.\w+)+$/, '请输入电子邮箱，此邮箱将作为Spiiker网站的登陆账号。')
				&& Form.valid(this, '/validemail.jsp?email=', '您已申请过等级测试，如需进一步了解Spiiker课程，请联系400-600-9144。');
	},
	phone2 : function() {
		return Form.input(this, /^(0\d{2,3}\-?\d{7,8}#?\d*|1\d{10})$/, '请填写正确的电话号码以便顺利接听外教来电。')
				&& Form.valid(this, '/validphone.jsp?phone2=', '您已申请过等级测试，如需进一步了解Spiiker课程，请联系400-600-9144。');
	},
	password : function() {
		return Form.input(this, /^[\w\d]{1,20}$/, '请设置您的登陆密码。');
	},
	selfevaluation : function() {
		return Form.select(this, '请选择自我评估英语水平。');
	},
	testdate : function() {
		return Form.select(this, '请选择测试日期和时间。');
	},
	testhour : function() {
		return Form.select(this, '请选择测试时间。');
	},
	privacy : function() {
		return Form.radio(this, '请阅读并勾选《隐私保护协议》。');
	}
};

function validtime(sl) {
	var form = document.getElementById('applyform');
	if (form.testdate.value && form.testhour.value) {
		var r = '/validtime.jsp?sl=' + sl + '&testtime=' + form.testdate.value + '+' + form.testhour.value + ':00';
		r = get(r);
		if (r !== '1') {
			changedate(form.testdate);
		}
	}
}

function changedate(el) {
	var sp = id('testhourspan');
	sp.innerHTML = get('/testhour.jsp?date=' + el.value);
	tag('select', id('testhourspan'))[0].onblur = Functions.testhour;
}

function check(f) {
	try {
		var els = f.elements;
		var radio = {};
		var l = els.length;
		for ( var i = 0; i < l; i++) {
			var k = els[i].name;
			if (k && Functions[k]) {
				if (els[i].type === 'radio' || els[i].type === 'checkbox') {
					if (radio[k] === true) {
						continue;
					}
					radio[k] = true;
					if (Functions[k].apply(f[k]) == false) {
						return false;
					}
				} else {
					if (Functions[k].apply(f[k]) == false) {
						return false;
					}
				}
			}
		}
	} catch (err) {

	}
	return true;
}
addEvent(window, 'onload', function() {
	var form = id('applyform');
	if (form) {
		var levelradio = form.selfevaluation;
		for ( var i = levelradio.length; --i >= 0;) {
			levelradio[i].parentNode.onclick = function() {
				id('levelmessage').innerHTML = this.title;
				validtime(this.firstChild.value);
			};
		}
		var genderidradio = form.genderid;
		for ( var i = genderidradio.length; --i >= 0;) {
			genderidradio[i].onclick = function() {
				onSuccess(this);
			};
		}
		for ( var i in Functions) {
			var a = form[i];
			if (a && a.name) {
				addEvent(a, 'onfocus', Functions[i]);
				addEvent(a, 'onkeyup', Functions[i]);
			}
		}
	}
});

function ifr(f) {
	this.f = f;
	var a = f.action;
	var id = 'ifr_' + a;
	this.iframe = $id(id);
	if (!this.iframe) {
		try {
			this.iframe = document.createElement('<iframe name="' + id + '"></iframe>');
		} catch (e) {
			this.iframe = document.createElement('iframe');
		}
		this.iframe.name = id;
		this.iframe.id = id;
		this.iframe.style.display = 'none';
		document.body.appendChild(this.iframe);
	}
	f.target = id;
	return this;
}
ifr.prototype = {
	valid : function(i, regex, f) {
		if (this.e !== undefined) {
			return;
		}

		if (i === undefined) {
			return;
		}
		var v;
		if (i.value === undefined) {
			var a = 0;
			for (; a < i.length; a++) {
				if (i[a].checked || i[a].selected) {
					return;
				}
			}
			this.result(i[0], false, f);
			return;
		} else {
			v = i.value;
		}
		if (v === undefined) {
			// return;
		} else {
			v = v.trim();
			i.value = v;
		}
		var r;
		if (regex === undefined) {
			r = !!v;
		} else {
			if (typeof regex === 'boolean') {
				r = regex;
			} else if (typeof regex === 'string') {
				regex = new RegExp(regex, 'g');
			}
			if (regex instanceof RegExp) {
				r = regex.test(v);
			}
		}
		this.result(i, r, f);
	},
	remotevalid : function(elem, url, fn) {
		if (this.e !== undefined) {
			return;
		}
		if (elem === undefined) {
			return;
		}
		var valid = get(url + elem.value) !== '1';
		this.result(elem, valid, fn);
	},
	result : function(elem, valid, f) {
		if (elem === undefined) {
			return;
		}
		if (!valid) {
			this.e = elem;
			if (f !== undefined) {
				f.apply(elem);
			} else {
				elem.focus();
			}
		}
	},
	submit : function() {
		if (!this.e) {
			this.f.submit();
		}
	},
	radio : function(name) {
		var ns = this.f[name];
		var l = ns.length;
		for ( var i = 0; i < l; i++) {
			if (ns[i].checked) {
				return ns[i].value;
			}
		}
	}
};
