/* only numbers validation rutine */
function onlyNumbers(e) {
	var charCode;
	if (navigator.appName == "Netscape") {
		charCode = e.which;
	} else {
		charCode = e.keyCode;
	}

	if (charCode > 31 && (charCode < 48 || charCode > 57)) {
		alert("Atention!\nWrite only numbers.");
		return false;
	}
	return true;
}

function validateAlpha(field) {
	var valid = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var ok = "yes";
	var temp;

	for (var i = 0; i < field.value.length; i++) {
		temp = "" + field.value.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") ok = "no";
	}

	if (ok == "no") {
		return false;
	} else {
		return true;
	}
}


/* validates an email address */
function checkEmail(strEmail) {
	var filter = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i

	if (filter.test(strEmail)) {
		return true;
	} else {
		return false;
	}
}


/* check if to strings are equals */
function equalsStr(str1, str2) {
	if (str1 != str2) {
		return false;
	} else {
		return true;
	}
}


function trim(TRIM_VALUE) {
	if(TRIM_VALUE.length < 1) {
		return "";
	}

	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);

	if(TRIM_VALUE=="") {
		return "";
	} else {
		return TRIM_VALUE;
	}
} //End Function


function RTrim(VALUE) {
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";

	if(v_length < 0) {
		return "";
	}

	var iTemp = v_length -1;

	while(iTemp > -1) {
		if(VALUE.charAt(iTemp) == w_space) {
		} else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}

		iTemp = iTemp-1;
	} //End While

	return strTemp;
} //End Function


function LTrim(VALUE) {
	var w_space = String.fromCharCode(32);

	if(v_length < 1) {
		return "";
	}

	var v_length = VALUE.length;
	var strTemp = "";
	var iTemp = 0;

	while(iTemp < v_length) {
		if(VALUE.charAt(iTemp) == w_space) {
		} else {
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}

		iTemp = iTemp + 1;
	} //End While

	return strTemp;
} //End Function


/* opens a pop-up window */
var popup1 = 0;
function popUpWindow(url, left, top, width, height) {
	if (popup1) {
		if (!popup1.closed) popup1.close();
	}

	popup1 = open(url, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=yes,width=' + width + ',height=' + height + ',left=' + left + ',top=' + top + ',screenX=' + left + ',screenY=' + top);
}


function phonemask(t) {
	var patt1 = /(\d{3}).*(\d{3}).*(\d{4})/;
	var patt2 = /^\((\d{3})\).(\d{3})-(\d{4})$/;
	var str = t.value;
	var result;

	if (!str.match(patt2)) {
		result = str.match(patt1);

		if (result!= null) {
			t.value = t.value.replace(/[^\d]/gi,'');
			str = '(' + result[1] + ') ' + result[2] + '-' + result[3];
			t.value = str;
		} else {
			if (t.value.match(/[^\d]/gi))
				t.value = t.value.replace(/[^\d]/gi,'');
		}
	}
}


function jm_currencymask(t) {
	var patt = /(\d*)\.{1}(\d{0,2})/;
	var donepatt = /^(\d*)\.{1}(\d{2})$/;
	var str = t.value;
	var result;

	if (!str.match(donepatt)) {
		result = str.match(patt);

		if (result!= null) {
			t.value = t.value.replace(/[^\d]/gi,'');
			str = result[1] + '.' + result[2] ;
			t.value = str;
		} else {
			if (t.value.match(/[^\d]/gi))
			t.value = t.value.replace(/[^\d]/gi,'');
		}
	}
}


/*
this fairly common function allows us to add behavior to pages
without cluttering up our html
*/
function addEvent( obj, type, fn ) {
	if (obj.addEventListener)
		obj.addEventListener( type, fn, false );
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}