// This is the string library which conatins string manipulation strings which could 
// be called directly or used as slave by formating functions.

/////////////////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/07/2002
/// This function will trim the passed string , that is it will eliminate all leading 
/// and trailing SPACES, HTABS, VTABS, LF, CR and PF 
/////////////////////////////////////////////////////////////////////////////////////////////
function TrimStr(Str)
{
	Str = Str.replace(/^(\s+)+|(\s+)$/g,"");
	return Str
}

/////////////////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/07/2002
/// This function will clear all white spaces from the string
/////////////////////////////////////////////////////////////////////////////////////////////
function ClearWhiteSpace(Str)
{
	Str = Str.replace(/(\s+)+/g,"");
	return Str
}


/////////////////////////////////////////////////////////////////////////////////////////
/// Written By Jamil On 29/072002 
/// this function will eliminate any repeated white spaces and replace them with a single
/// space.
/// Leading and trailing white spaces will be eliminated.
/////////////////////////////////////////////////////////////////////////////////////////////
function SqueezeStr(Str)
{
	Str = Str.replace(/^(\s+)+|(\s+)$/g,"");
	Str = Str.replace(/\s/g," ");
	Str = Str.replace(/(  +)/g," ");
	return Str
}

/////////////////////////////////////////////////////////////////////////////////////////////////////
/// This function will fill the passed str pstr up to the length plength using the passed char pchar.
/// - pside = (L,R) decide if the filling will be added from the left or right.
/// - if pstr missing a blank str will be returned
/// - if pstr is already of length plength or greater than it will be returned as is.
/// - Only pstr and plength are required parameters
/// - When abscent pchar will default to space and pside will default to left.
/////////////////////////////////////////////////////////////////////////////////////////////////////
function strFill(pstr,plength,pside,pchar)
{
	var lengthGap;
	var usedChar, usedSide
	var newStr;
	var idx;
	
	if(!pstr)
	{
		pstr = "";
	}
	else
	{
		pstr = pstr + "";
	}

	if(arguments.length<2)
	{
		alert("The length you wish to extend to is missing.");
		return pstr;
	}

	// Default to a space if missing.
	usedChar = (pchar?pchar:" ");
	
	// Default to left side if missing.
	usedSide = (pside?pside.toLowerCase():"l");
	if((usedSide!="l") && (usedSide!="r"))
	{
		alert(pside + " is not a valid side, must be L(eft) OR R(ight)");
		return pstr;
	}

	// Fill the str as appropriate.
	lengthGap	= plength - pstr.length;
	newStr		= pstr + "";
	for(idx=0; idx < lengthGap; idx++)
	{
		if(usedSide=="l")
		{
			newStr	= usedChar + newStr;
		}
		else if(usedSide=="r")
		{
			newStr	= newStr + usedChar;
		}
	}
	return newStr;
}


/////////////////////////////////////////////////////////////////////////
//// Ignores Key Stroke which are not fit the format for a percent number.
//// Written By Jamil 27/02/2001
/////////////////////////////////////////////////////////////////////////
function PercentOnly(elem)
{
	var curValue;
	
	if(((event.keyCode<48) || (event.keyCode > 57)) && (event.keyCode != 13) && ( event.keyCode != 46) && ( event.keyCode != 44) && ( event.keyCode != 37))
	{
		keyed = unescape('%'+ event.keyCode.toString(16));
		event.keyCode=0
		//ShowTip(elem.id, "<B>" + keyed + "</B> Not allowed, Must be a Digit",x,y);
		elem.focus();
		//alert("Done...");
	}
	else
	{
		curValue = elem.value;
		if( (curValue.indexOf(".")>=0) && (( event.keyCode == 46) || (event.keyCode == 44)))
		{
			event.keyCode=0;
		}
	}

}

/////////////////////////////////////////////////////////////////////////
//// Ignores Key Stroke which are not fit the format for a decimal number.
//// Written By Jamil 27/02/2001
/////////////////////////////////////////////////////////////////////////
function DecOnly(elem)
{
	var curValue;
	if(((event.keyCode<48) || (event.keyCode > 57)) && (event.keyCode != 13) && ( event.keyCode != 46) && ( event.keyCode != 44))
	{
		keyed = unescape('%'+ event.keyCode.toString(16));
		event.keyCode=0
		elem.focus();
	}
	else
	{
		curValue = elem.value;
		if( (curValue.indexOf(".")>=0) && (( event.keyCode == 46) || (event.keyCode == 44)))
		{
			event.keyCode=0;
		}
	}

}

/////////////////////////////////////////////////////////////////////////
//// Ignores Key Stroke which are not fit the format for a Signed Decimal Number decimal number.
//// Written By Jamil 27/02/2001
/////////////////////////////////////////////////////////////////////////
function SDecOnly(elem)
{
	var curValue;
	
	if(((event.keyCode<48) || (event.keyCode > 57)) && (event.keyCode != 13) && ( event.keyCode != 46) && ( event.keyCode != 44) && ( event.keyCode != 45))
	{
		keyed = unescape('%'+ event.keyCode.toString(16));
		event.keyCode=0
		elem.focus();
	}
	else
	{
		curValue = elem.value;
		if( (curValue.indexOf(".")>=0) && (( event.keyCode == 46) || (event.keyCode == 44)))
		{
			event.keyCode=0;
		}
	}

}
//////////////////////////////////////////////////////////////////////////
// Ignores non-digital keystokes
// Written By Jamil 27/02/2001
///////////////////////////////////////////////////////////////////////////
function PosIntOnly(elem)
{
	var goodnum = true;
	if(((event.keyCode<48) || (event.keyCode > 57)) && (event.keyCode != 13))
	{
		keyed = unescape('%'+ event.keyCode.toString(16));
		event.keyCode=0;
		goodnum=false;
		elem.focus();
	}
	return goodnum 
}

//////////////////////////////////////////////////////////////////////////
// Ignores non-digital keystokes
// Written By Jamil 27/02/2001
///////////////////////////////////////////////////////////////////////////
function NumOnly(elem)
{
	var goodnum = true;
	if(((event.keyCode<48) || (event.keyCode > 57)) && (event.keyCode != 13)&& (event.keyCode != 45))
	{
		keyed = unescape('%'+ event.keyCode.toString(16));
		event.keyCode=0;
		goodnum=false;
		elem.focus();
	}
	return goodnum 
}

//////////////////////////////////////////////////////////////////////////
// Ignores digital keystokes
// Written By Bobby 27/02/2001
///////////////////////////////////////////////////////////////////////////
function nonNumOnly(elem)
{
	var goodnum = true;

	if(((event.keyCode >= 48) && (event.keyCode <= 57)) && (event.keyCode != 13)&& (event.keyCode != 45))
	{
		keyed = unescape('%'+ event.keyCode.toString(16));
		event.keyCode=0;
		goodnum=false;
		elem.focus();
	}
	return goodnum 
}
//////////////////////////////////////////////////////////////////////////
// Ignores non alphabetic keystokes
// Written By Bobby 27/02/2001
///////////////////////////////////////////////////////////////////////////
function nonAlphaOnly(elem)
{
	var goodalphanum=true;
	if( ((event.keyCode >= 65) && (event.keyCode <= 90)) || ((event.keyCode >= 97) && (event.keyCode <= 122)) )
	{
		keyed = unescape('%'+ event.keyCode.toString(16));
		event.keyCode=0;
		goodalphanum=false;
		badchr = keyed;
		elem.focus();
	}
	else
	{
		badchr="";
	}
	return goodalphanum;
}


//////////////////////////////////////////////////////////////////////////
// Ignores non-digital & non alphabetic keystokes
// Written By Jamil 27/02/2001
///////////////////////////////////////////////////////////////////////////
function AlphaNumOnly(elem)
{
	var goodalphanum=true;
	if((event.keyCode<48) || ((event.keyCode > 57) && (event.keyCode < 65)) || ((event.keyCode > 90) && (event.keyCode < 97))  || (event.keyCode > 122))
	{
		keyed = unescape('%'+ event.keyCode.toString(16));
		event.keyCode=0;
		goodalphanum=false;
		badchr = keyed;
		elem.focus();
	}
	else
	{
		badchr="";
	}
	return goodalphanum;
}

////////////////////////////////////////////////////////////////////////
////	
////	This Function do a basic validation of the email format
////	and return TRUE or False.
////
////////////////////////////////////////////////////////////////////////
function isLongEnough(elem,len,flag)
{
	var val = elem.value;
	if((flag==1) && (val==""))
	{
		return true;
	}
	if(val.length<len)
	{
		return false
	}
	else
	{
		return true;
	}		
}

////////////////////////////////////////////////////////////////////////
////	
////	This Function do a basic validation of the email format
////	and return TRUE or False.
////
////////////////////////////////////////////////////////////////////////

function isEmail(emailstr)
{
	var emailpat=/^.+@.+\..{2,3}$/;
	var emailflag = true;	
	if ((emailstr != "") && (!emailpat.test(emailstr)))
	{
		emailflag = false;
	}
	return emailflag;
}


////////////////////////////////////////////////////////////////////////
////	
////	This Function Do a basic check on a Url Validity
////	
////////////////////////////////////////////////////////////////////////

function isUrl(urlStr)
{
	var urlpat=/^http\:\/\/www\..+@.+\..{2,3}$/;
	var urlflag = true;	
	if ((urlStr != "") && (!urlpat.test(urlStr)))
	{
		emailflag = false;
	}
	return emailflag;
}


///////////////////////////////////////////////
function GetFocus()
{
	setTimeout("this.focus()",3005);
}

//////////////////////////////////////////////
// Author: Bobby Kwan
//	Date : 14/05/2001
// 
// Description: This function will make sure that the 
//				field that is given as the argument will
//				have it's value as a percentage value. 
////////////////////////////////////////////

function ToPercent(frmitem)
{
	var x = eval('prefFrm.' + frmitem + '.value') ;
//	alert(x);
//	alert(x.length);
	
	x = stripSpaces(x);
	
//	alert (x);
	
	x = x + '%';
	
//	alert(x);
	
	
	
//	alert(x.length)
	
	if (x.length == 1) 
	{
		document.all(frmitem).value = '0%' ;
	}
	else
	{
		document.all(frmitem).value = x ;
	}
}

//////////////////////////////////////////////
// Author: Bobby Kwan
//	Date : 14/05/2001
// 
// Description: This function will removes all leading 
//				and tailing spaces with a string.
// 
////////////////////////////////////////////
function stripSpaces(x) 
{	
	var y;
	
    y = (x.replace(/^\W+/,'')).replace(/\W+$/,'');
	
	return y
}


////////////////////////////////////////////////////////////////////
///
/// This Function Will Use the status bar to show a message
///
/////////////////////////////////////////////////////////////////////

function MixUp(text,index)
{
	var mystring = new String(text);
	var p1,p2,p3;
	var idx = parseInt(index,10);
	
	mystring = mystring.toLowerCase();
	if((idx >=0)&&(idx < mystring.length))
	{
		p1= mystring.substr(0,idx);
		p2= mystring.substr(idx,1);
		p2 = p2.toUpperCase();
		p3= mystring.substr(idx+1);
		newstring = p1 + p2 + p3;
		idx++;
		funccall = "Mix('" + newstring + "'," + idx + ")";
		setTimeout(funccall,300);
	}
	else
	{
		window.status = mystring.toUpperCase();
		return;	
	}
}
////////////////////////////////////////////////////////////////
///
/// This Function is employed by MixUp (above) to put a delay	
///
////////////////////////////////////////////////////////////////
function Mix(text,index)
{
	window.status = text;
	//alert(text + " *-* " + index);
	MixUp(text,index);
}

///////////////////////////////////////////////////////////////////////////
//
// This Function fill the passed string (pval) to the given length (plength)
// using the passed character(pfill) from the passed side (pside)
///////////////////////////////////////////////////////////////////////////
function Filler(pval, pfill, plength, pside)
{
	var fillerval = pval + ''
	while( fillerval.length < plength)
	{
		if( pside =="R")
		{
			fillerval +=  pfill;
		}
		else
		{
			fillerval = pfill + fillerval ;
		}
	}
	return fillerval;
}