// Little Island account page validation script //

$get = function (id) {return document.getElementById(id);}
$set = function (id,text) {$get(id).innerHTML = text;}
$getval = function (id) {return $get(id).value;}
$setval = function (id,val) {$get(id).value = val;}

$blink = function(o){
	if (o) {} else {return;}
	if (o.IID) return;
	if (o.style) {} else {return;}
	o.breturn = o.style.border;
	o.blinktick = false;
	o.IID = setInterval(function(o){
		if (o.blinktick) {
			o.style.border = o.breturn;
			o.blinktick = false;
		} else {
			o.style.border = 'solid 2px white';
			o.blinktick = true;
		}
	}, 100, o);
	setTimeout(function(o){
		clearInterval(o.IID);
		o.IID = false;
		o.style.border = o.breturn;
	}, 1000, o);
}

compareInputs = function (e,o) {
	var first = second = false;
	var t = (typeof(o) == 'object') ? o : this;
	var id;
	if (e.srcElement) {
		t = e.srcElement;						// IE //
		id = e.srcElement.getAttribute('id');
	} else {
		id = t.getAttribute('id');				// Normal //
	}
	
	if (id.indexOf('repeat') > -1) {
		first = $get(id.replace('repeat', ''));
		second = t;
	} else {
		first = t;
		if ($get('' + id + 'repeat')) {
			second = $get('' + id + 'repeat');
		} else {
			second = first;
		}
	}

	var bb = 'solid 2px ';
	if (!id.match(/hours|minutes/) && first.value.length == 0) {
		first.style.border = bb + '#99f';
		second.style.border = bb + '#99f';
		return false;
	} else if (first.value != second.value) {
		first.style.border = bb + '#f55';
		second.style.border = bb + '#f55';
		return false;
	} else if (id.match(/email/) && !testEmail(first)) {
		first.style.border = bb + '#f55';
		second.style.border = bb + '#f55';
		return false;
	} else if (id.match(/name/) && (first.value.length < 3 || first.value.length > 32 || $toUTF8(first.value.length).length > 64)) {
		first.style.border = bb + '#f55';
		second.style.border = bb + '#f55';
		return false;
	} else if (id.match(/hours|minutes/)) {
		var a = $get('action').checked;
		var h = $getval('hours');
		var m = $getval('minutes');
		var t = Number(h)+(Number(m)/60);
		
		if (a && (
			t > 720 || 
			t <= 0 || 
			(h.length > 0 && h.match(/[^0-9\.]/)) || 
			(m.length > 0 && m.match(/[^0-9\.]/))
		)) {
			$get('hours').style.border = bb + '#f55';
			$get('minutes').style.border = bb + '#f55';
		} else if (!a) {
			$get('hours').style.border = bb + '#99f';
			$get('minutes').style.border = bb + '#99f';
		} else {
			$get('hours').style.border = bb + '#5d5';
			$get('minutes').style.border = bb + '#5d5';
		}
	} else {
		first.style.border = bb + '#5d5';
		second.style.border = bb + '#5d5';
		return true;
	}
}

// Add event listener workaround for IE //
$listener = function (o,e,f) {
	if (document.addEventListener) {
		o.addEventListener(e,f,false);	// Normal //
	} else {
		o.attachEvent('on'+e,f);		// IE //
	}
}

testEmail = function (eo) {
	if (eo.value.match(/^[\w\d\.\-_]+@[\w\d\-]+\.[\w\d\.\-]+$/) && !eo.value.match(/^\.|\.\.|\.@|@\.|\.$/)) {return true;}
	return false;
}