//###################################################
//# JavaScript functions for checking form elements #
//# © Vladimir Kelman, vlad@vkelman.com, 2003       #
//###################################################


//# See http://www.developingskills.com/ds.php?article=jstrim&page=1
//# it adds ltrim(), rtrim(), and trim() methods to any string.
//# So, the use of function _trim() becomes obsolete
//#
function strltrim() { return this.replace(/^\s+/,''); }
function strrtrim() { return this.replace(/\s+$/,''); }
function strtrim() { return this.replace(/^\s+/,'').replace(/\s+$/,''); }

if (typeof(String.trim) == "undefined") {  // check it just in case it's already defined in some other file
	String.prototype.ltrim = strltrim;
	String.prototype.rtrim = strrtrim;
	String.prototype.trim = strtrim;
}

//# To use when s is possible a null
function trimSafe(s) { return (s != null)? ('' + s).trim() : ''; }

//# A convenience wrapper for trimming and reassigning control values
function trimSelf(ctrl)
{
	if (typeof(ctrl.value) != 'undefined') ctrl.value = ctrl.value.trim();
	return ctrl.value;
}


	function checkTextField(frmname, txtfldname, makeAlert, stralert)
	{
		var txtfld = (document[frmname])[txtfldname];
		var res = ((txtfld.value).trim() != '');
		
		if (! res && makeAlert) {
			txtfld.focus();
			alert(stralert);
			txtfld.focus();
		}
		
		return res;
	}


	function checkTextFieldLen(frmname, txtfldname, imin, imax, makeAlert, stralert)
	{
		var txtfld = (document[frmname])[txtfldname];
		var s = (txtfld.value).trim();
		var res = (s.length >= imin) && (s.length <= imax);
		
		if (! res && makeAlert) {
			txtfld.focus();
			alert(stralert);
			txtfld.focus();
		}
		
		return res;
	}
	

	function checkSelectBox(frmname, selfldname, makeAlert, stralert)
	{
		var selfld = (document[frmname])[selfldname];
		var res = (selfld.selectedIndex > 0);
		
		if (! res && makeAlert) {
			selfld.focus();
			alert(stralert);
			selfld.focus();
		}
		
		return res;
	}


	function checkSelectBoxM(frmname, selfldname, makeAlert, stralert)
	{
		var selfld = (document[frmname])[selfldname];

		var j = 0;
		for(i = 0; i < selfld.length; i++)
			if (selfld.options[i].selected) j++;

		var res = (j > 0);
		
		if (! res && makeAlert) {
			selfld.focus();
			alert(stralert);
			selfld.focus();
		}
		
		return res;
	}


	function checkSearchField(frmname, txtfldname, makeAlert, stralert)
	{
		if (!checkTextField(frmname, txtfldname, makeAlert, stralert)) return false;
		
		var txtfld = (document[frmname])[txtfldname];
		var fldval = txtfld.value;
		
		count = 0; pos = -1;
		while ((pos = fldval.indexOf('"', pos + 1)) != -1) count++;   // count all double quotes in a search string
		res = (count % 2 == 0);                                       // only even number of double quotes is allowed
		
		if (! res && makeAlert) {
			txtfld.focus();
			alert('There should be an even number or no double quotes in a search string!');
			txtfld.focus();
		}
		
		return res;
	}


	function checkEmail(frmname, emailfldname, makeAlert, stralert)
	{
		var emailfld = (document[frmname])[emailfldname];
		var email = (emailfld.value).trim();

		var res = ! (((atsignpos = email.indexOf('@')) < 1) || (email.indexOf('.', atsignpos) == -1) || (email.indexOf('.', atsignpos) > (email.length - 2)) || (email.indexOf(',') > -1) || (email.indexOf(';') > -1) || (email.indexOf(' ') > -1) || (email.indexOf('\t') > -1));
		
		if (! res && makeAlert) {
			emailfld.focus();
			alert(stralert);
			emailfld.focus();
		}
		
		return res;
	}

	function checkFullPhone(frmname, areafldname, ph1fldname, ph2fldname,
	                                                  phxfldname, makeAlert, stralert)
	{
		var areafld = (document[frmname])[areafldname];
		var area = (areafld.value).trim();
		var ph1fld  = (document[frmname])[ph1fldname];
		var ph1 = (ph1fld.value).trim();
		var ph2fld  = (document[frmname])[ph2fldname];
		var ph2 = (ph2fld.value).trim();
		var phxfld  = (document[frmname])[phxfldname];
		var phx = (phxfld.value).trim();
		var ph = area + ph1 + ph2 +phx;
		
		res = (ph == '');
		if (! res) {
			res = (area.length == 3) && chckint(frmname, areafldname, false);
			if (! res && makeAlert) {
				areafld.focus();
				alert(stralert);
				areafld.focus();
			}
//alert("area is " + res);
			if (res) {
				res = (ph1.length == 3) && chckint(frmname, ph1fldname, false);
				if (! res && makeAlert) {
					ph1fld.focus();
					alert(stralert);
					ph1fld.focus();
				}
			}
//alert("ph1 is " + res);
			if (res) {
				res = (ph2.length == 4) && chckint(frmname, ph2fldname, false);
				if (! res && makeAlert) {
					ph2fld.focus();
					alert(stralert);
					ph2fld.focus();
				}
			}
//alert("ph2 is " + res);
			if (res) {
				res = (phx == "") || chckint(frmname, phxfldname, false);
				if (! res && makeAlert) {
					phxfld.focus();
					alert(stralert);
					phxfld.focus();
				}
			}
//alert("phx is " + res);
		}
		
		return res;
	}

	function chckint(frmname, fldname, makealert) {
		var i, str, str1, strErr;
		var fld = (document[frmname])[fldname];

		str = (fld.value).trim();
		if (str != "") {
			i = parseInt(str);
			str1 = i + "";
			if (str != str1) {
				if (makealert) {
					strErr = str + " is not the correct integer number!";
					fld.focus();
					alert(strErr);
					fld.focus();
				}
				return false;
			}
		}
		return true; 
	}

	function chckfloat(frmname, fldname, makealert) {
		var i, str, str1, strErr;
		var fld = (document[frmname])[fldname];

		str = (fld.value).trim();
		if (str != "") {
			i = parseFloat(str);
			str1 = i + "";
			if (str != str1) {
				if (makealert) {
					strErr = str + " is not the correct number!";
					fld.focus();
					alert(strErr);
					fld.focus();
				}
				return false;
			}
		}
		return true; 
	}


// pretty much the same thing as above
	function nonempty(frm, fld, makealert)
	{
		if ((frm[fld].value).trim() == '') {
			if (makealert) {
				frm[fld].focus();
				alert ("Please, fill the '" + fld + "' field!");
				frm[fld].focus();
			}
			return false;
		}
		else
			return true;
	}

	function nonemptysel(sel, makealert)
	{
		if ((sel[sel.selectedIndex].value).trim() == '') {
			if (makealert) {
				sel.focus();
				alert ("Please, fill the '" + sel.name + "' field!");
				sel.focus();
			}
			return false;
		}
		else
			return true;
	}

	var selOpt = 0;
	function onSelWithEmptyChange(sel)
	{
		if (sel.selectedIndex == 0)
			sel.selectedIndex = selOpt;
		else
			selOpt = sel.selectedIndex;
	}
