var textLength = 100;


Array.prototype.in_array = function(needle) {
	for(var i=0; i < this.length; i++)
		if(this[ i] === needle)
			return true;
	return false;
}

function resetText() {

	document.frm.reset();
	document.getElementById('char_cnt').innerHTML = (textLength-document.getElementById('form_text').value.length);
}

function clearText() {
	for (i=0;i<document.frm.elements.length;i++) {
		document.frm.elements[i].value = '';
	}
	document.getElementById('char_cnt').innerHTML = (textLength-document.getElementById('form_text').value.length);
}

function resetFriendText() {
	document.frm_friend.reset();
	document.getElementById('char_friend_cnt').innerHTML = (textLength-document.getElementById('form_friend_text').value.length);
}

function checkChars(e,obj) {
	var allowedChars = new Array();
	
	allowedChars = [8,32,33,39,40,41,44,45,46,58,59,63,109,118,145,146,171,187];
	for (i=65;i<=90;i++)
		allowedChars.push(i);
	for (i=97;i<=122;i++)
		allowedChars.push(i);
	for (i=48;i<=57;i++)
		allowedChars.push(i);

	if(window.event) // IE
	{
		keynum = e.keyCode
	}
	else if(e.which) // Netscape/Firefox/Opera
	{
		keynum = e.which
	}
	keychar = String.fromCharCode(keynum);

	textvalue = obj.value;
	inputOld = textvalue;

	if (!allowedChars.in_array(keynum))
		return false;

	if (keynum!=8)
		textvalue += keychar;

	if (textvalue.length>textLength) {
		obj.value = textvalue.substr(0,textLength);
		return false;
	}

	return true;
}


function checkFrm() {
	var valid = true;

	if (!document.getElementById('form_nick').value) {
		invalid = 'Please insert your Name.'+"\n";
		valid = false;
	}
	else if (document.getElementById('form_nick').value.length>60) {
		invalid = 'Name is too long.'+"\n";
		valid = false;
	}
	if (!document.getElementById('form_location').value) {
		invalid += 'Please insert your Location.'+"\n";
		valid = false;
	}
	else if (document.getElementById('form_location').value.length>60) {
		invalid = 'Location is too long.'+"\n";
		valid = false;
	}
	if (!document.getElementById('form_text').value) {
		invalid += 'Please insert text.'+"\n";
		valid = false;
	}
	else if (document.getElementById('form_text').value.length>textLength) {
		invalid = 'Text is too long.'+"\n";
		valid = false;
	}

	if (!valid) {
		alert(invalid);
	}
	return valid;
}


function checkFriendFrm() {
	var valid = true;
	var invalid = '';
	
	if (!document.getElementById('form_your_mail').value) {
		invalid = 'Please insert your mail address.'+"\n";
		valid = false;
	}
	else if (!checkMail(document.getElementById('form_your_mail').value)) {
		invalid = 'Your mail address is not valid'+"\n";
		valid = false;
	}
	if (!document.getElementById('form_friend_mail').value) {
		invalid += 'Please insert friend mail address.'+"\n";
		valid = false;
	}
	else if (!checkMail(document.getElementById('form_friend_mail').value)) {
		invalid += 'Friend mail address is not valid'+"\n";
		valid = false;
	}
	if (!document.getElementById('form_friend_text').value) {
		invalid += 'Please insert text.'+"\n";
		valid = false;
	}
	else if (document.getElementById('form_friend_text').value.length>textLength) {
		invalid = 'Text is too long.'+"\n";
		valid = false;
	}

	if (!valid) {
		alert(invalid);
	}
	return valid;
}

function checkLength(obj) {
	if ((textLength-obj.value.length)<0)
		obj.value = obj.value.substr(0,textLength);

	document.getElementById('char_cnt').innerHTML = (textLength-obj.value.length);
}

function checkFriendLength(obj) {
	if ((textLength-obj.value.length)<0)
		obj.value = obj.value.substr(0,textLength);

	document.getElementById('char_friend_cnt').innerHTML = (textLength-obj.value.length);
}

function checkMail(s)
{
	 var a = false;
	 var res = false;
	 if(typeof(RegExp) == 'function') {
		var b = new RegExp('abc');
		if(b.test('abc') == true)
			a = true;
	 }

	 if(a == true)
	 {
			reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
										 '(\\@)([a-zA-Z0-9\\-\\.]+)'+
										 '(\\.)([a-zA-Z]{2,4})$');
			res = (reg.test(s));
	 }
	 else {
			res = (s.search('@') >= 1 &&
			s.lastIndexOf('.') > s.search('@') &&
			s.lastIndexOf('.') >= s.length-5)
	 }
	 return(res);
}