// utility script
// 99/07 AT

/*
******************************************************************************************
|isBlank:		true if a string is null or contains only spaces;
|Ltrim:			left trim spaces of a string;
|Rtrim:			right trim spaces of a string;
|Trim:			trims leading and tailing spaces of a tring;
|isSymb:		true if a string contains only symbols, exlcuding underscore "_";
|isDigit:		true if string is 0 - 9;
|isLetter:		true if string is a-z and A-Z and underscore "_";
|isChar:		true if string is non-digit;
|isEmail:		true if string's format is legal email address format;
|isPhone:		true if string contains 0-9 and ()/-+;
|clrSpc:		returns a string with all spaces taken out;
|
|cntChar(s, ch, c):	returns number of string [ch] in string [s]
|			[c] optional: 1: case sensitive; other: case insensitive;
|
|replChar(s, fmStr, toStr, c, st):
|			returns string with certain characters/strings replaced by
|			specified characters/strings.
|			[s]		string to operate on;
|			[fmStr]		char/string to replace;
|			[toStr]		char/string to replace with;
|			[c]		optional, case insensitive (default) or sensitive (1);
|			[st]		optional, treat [mfStr] as one single string (default)
|					or an array of characters (1);
|	example:	replChar("ab33ab", "ab", "x")		return "x33x";
|			replChar("Ab33ab", "ab", "x")		same as above;
|			replChar("Ab33ab", "ab", "x", 1)	return "Ab33x";
|			replChar("Ab33ab", "ab", "x", 1, 1) return "Ab33xx";
|			replChar("Ab33ab", "ab", "x", 0, 1) return "xx33xx";
|
|existObject(obj):	checks if an object exists in the current document (unique name accross forms)
|			[obj] object name
|
********************************************************************************************
*/

function isBlank(s){
var blank=true;
	  if(s.length==0)
			return true;
	  else {
		for(var x=0;x<=s.length-1;x++){
		if(s.charAt(x)!=" "){
			blank=false;
			break;
			}
	    }
	  }
return blank;
}


function Ltrim(s) {
var vs = s;
for(var i=0;i<s.length;i++){
		if(s.charAt(i) != " "){
			vs = s.substring(i);
			break;
		}
}
return vs;
}


function Rtrim(s){
vs = s;
	for(var i=s.length-1; i>=0; i--){
		if(s.charAt(i) != " "){
			vs = s.substring(0,i+1);
			break; }
	}
	return vs;
}


function Trim(s){
	return Rtrim(Ltrim(s));
}


function regEnc(s){           // escape special characters in a string to build RegExp
var i=0; var c; var vs ="";
for(var x=0;x<s.length;x++){
 	c = s.charAt(x);
	if(c=="\\" || c=="/" || c=="$" || c=="^" || c=="*" || c=="+" || c=="?" || c=="." ||
	   c=="(" || c==")" || c=="[" || c=="]" || c=="{" || c=="}" || c=="|")
		vs += ("\\"+s.substring(x, x+1));
	else
		vs += s.substring(x,x+1);
}
return vs;
}


function replChar(s, fmStr, toStr, c, st){
if(replChar.arguments.length<4) { c=0; st=0;}  // stupid, but for backward-comp with ie3 / nav3
else if(replChar.arguments.length<5) { st=0;}

if(st==1){
		var vres=s;
		for(var i=0;i<fmStr.length;i++)
		 	vres = replChar2(vres, fmStr.charAt(i), toStr, c, st);
		return vres;
}else
		return replChar2(s, fmStr, toStr, c, st);
}


function replChar2(s, fmStr, toStr, c, st){
var res=s;
var p=0;
while(p != -1){
	if(c==1 || fmStr==" ")
		p = res.indexOf(fmStr, p);
	else
		p = res.toUpperCase().indexOf(fmStr.toUpperCase(), p);
	if(p != -1){
		res = res.substring(0,p) + toStr + res.substring(p+fmStr.length);
		p += toStr.length;
		}
}
return res;
}


function clrSpc(s){
	return replChar(s," ","");
}

function isSymb(s){           // all symbols except underscore "_"
 var rs = true;
 for(i=0;i<s.length;i++){
	x=s.charAt(i);
	if(!((x>="!" && x<="/") || (x>=":" && x<="@") || (x>="[" && x<="`") || (x>="{" && x<="~"))) {
		rs = false;
		break;
	}
 }
 return rs;
}


function isDigit(s){
 var rs = true;
 for(var i=0; i<s.length; i++){
	if(!(s.charAt(i)>="0" && s.charAt(i)<="9")){
		rs = false;
		break;
	}
 }
return rs;
}

function isPhone(s){
var rs = true;
for(var i=0; i<s.length; i++){
	if(!(s.charAt(i)>="0" && s.charAt(i)<="9") && s.charAt(i)!="/" && s.charAt(i)!="-" && s.charAt(i)!="+" &&
			s.charAt(i)!="(" && s.charAt(i)!=")"){
		rs = false;
		break;
	}
 }
return rs;
}

function isChar(s){
 var rs = true;
 for(var i=0;i<s.length;i++){
	if(s.charAt(i)>="1" && s.charAt(i)<="9"){
		rs = false;
		break;
	}
 }
 return rs;
}


function isLetter(s){        // including "_" underscore
 var rs = true;
 for(var i=0;i<s.length;i++){
	x=s.charAt(i);
	if(!((x>="A" && x<="Z") || (x>="a" && x<="z") || x=="_")) {
		rs = false;
		break;
	}
 }
return rs;
}

function isEmail(s){  		  // no space allowed
	if(s.indexOf("@")==-1 || s.charAt(s.length-1)=="@" || s.charAt(0)=="@" || s.indexOf("@.")!=-1 ||
	s.indexOf(".")==-1 || s.charAt(s.length-1)=="." || s.charAt(0)=="." || s.indexOf("..")!=-1 || s.indexOf(".@")!=-1 ||
	cntChar(s, "@")!=1 || cntChar(s, " ") >0 )
		return false;
	else
		return true;
}


function cntChar(s, ch, c){      // ch: a character or string; c: case sensitive (1) or not
var x=0;
var p=0;
while(p != -1){
		if(c==1 || ch==" ")
			p = s.indexOf(ch, p);
		else
			p = s.toUpperCase().indexOf(ch.toUpperCase(), p);
		if(p != -1){
			x++;
			p++;
		}
}
return x;
}

function existObject(obj) {
var res=false;
for(var i=0;i<=document.forms.length-1;i++){
	for(var x=0;x<=document.forms[i].elements.length-1;x++){
		if(document.forms[i].elements[x].name==obj){
			res=true;
			break;
		}
	}
	if(res) break;
}
return res;
}
