function Left(str, len)
{
	var sBuffer = new String(string);
	return(sBuffer.substring(0, len));
}

function StrRep(str, oldsubstr, newsubstr)
{
	return(sStrRep(str, oldsubstr, newsubstr));
}
function sStrRep(str, oldsubstr, newsubstr)
{
	var sBuffer = str;
	var nLenOldSubStr = oldsubstr.length, nLenNewSubStr = newsubstr.length;
	var nPtr = 0;
	
	// return if no substring was passed
	if (oldsubstr == "" || oldsubstr == null) return(sBuffer);

	// parse	 
	nPtr = sBuffer.indexOf(oldsubstr, 0);
	while (nPtr != -1) {
		sBuffer = sBuffer.substring(0, nPtr) + newsubstr + sBuffer.substring(nPtr + nLenOldSubStr, sBuffer.length);
		nPtr = sBuffer.indexOf(oldsubstr, nPtr + nLenNewSubStr); 
    }

	// return new string
	return(sBuffer);
}

function Trim(str)
{
	var sBuffer = new String(str);
	var nStart, nEnd;

	for (nStart = 0; (sBuffer.charAt(nStart) == " " && nStart <= (sBuffer.length - 1)); nStart++) {}
	for (nEnd = sBuffer.length - 1; (sBuffer.charAt(nEnd) == " " && nEnd > 0); nEnd--) {}

	if (nStart > nEnd) 
		return("");
	else
		return(sBuffer.substring(nStart, nEnd + 1));
}

function UCase(str)
{
	var sBuffer = new String(str);
	sBuffer.toUpperCase();
	return(sBuffer);
}

//
//Returns true or false based on if the phone number is valid.
//
function IsTelNo(strTelNo) {
	var strBuffer, i
	
	//remove from strTelNo all the characters that are not numbers
	strBuffer = '';
	for(i = 0; i < strTelNo.length; i++)
		if('0' <= strTelNo.charAt(i) && strTelNo.charAt(i) <= '9')
			strBuffer += strTelNo.charAt(i);

	//a tel no is valid if the length is 10 or length is 11 and the number starts with 1800 or 1900
	if(strBuffer.length == 10 || (strBuffer.length == 11 && (strBuffer.substr(0, 4) == '1800' || strBuffer.substr(0, 4) == '1888' || strBuffer.substr(0, 4) == '1900')))
		return true;

	return false;
}

//
//Returns a new String object that contains a formated phone number
//
function FormatTelNo(strTelNo) {
	var strBuffer, i
	
	//remove from strTelNo all the characters that are not numbers
	strBuffer = '';
	for(i = 0; i < strTelNo.length; i++)
		if('0' <= strTelNo.charAt(i) && strTelNo.charAt(i) <= '9')
			strBuffer += strTelNo.charAt(i);

	//format regular number: (area_code) number
	if(strBuffer.length == 10)
		return '(' + strBuffer.substr(0, 3) + ') ' + strBuffer.substr(3, 3) + '-' + strBuffer.substr(6, 4);

	//format 1800 phone number
	if(strBuffer.length == 11 && (strBuffer.substr(0, 4) == '1800' || strBuffer.substr(0, 4) == '1888' || strBuffer.substr(0, 4) == '1900'))
		return strBuffer.substr(0, 4) + ' ' + strBuffer.substr(4, 3) + '-' + strBuffer.substr(7, 4);
	
	return strTelNo;
}

//
//Returns true or false based on if the phone number is valid.
//
function IsEmailAddress(inputStr) {
    //define invalid characters
	invalidChars=" /\:,;"
  
    //check if email address contains any invalid characters
    for (i=0;i<invalidChars.length;i++) {
         badChar= invalidChars.charAt(i)
		 if (inputStr.indexOf(badChar,0) > -1)
		    {
			  return false;
			} 
		  }
      
	 //check to see if there is an "@" symbol in email address 	  
	 atPos = inputStr.indexOf("@",1)
	if (atPos == -1)
	   {
	     return false;
	    }
     
	 //check to see if there is ony one "@" symbol in the email address
	 if (inputStr.indexOf("@",atPos+1) !=-1)
    {
		  return false;
	 }

    //check to see if there is atleast one period, (".") after the "@" symbol
	periodPos = inputStr.indexOf(".",atPos)
	if (periodPos == -1){
	    return false;
	}

	//check to see if there is atleast two characters after the "."
	if (periodPos+3 > inputStr.length){
		return false;
	}
	return true;
 }
 
 function IsURL(inputStr) 
{
    //define invalid characters
	invalidChars=" :,;"

	if(inputStr.substr(0, 7).toLowerCase() == 'http://') {
		inputStr = inputStr.substring(7, inputStr.length)	
		if(inputStr.length == 0) {
			return false;
		}
	}
 
    // check if URL contains any invalid characters
    for (i=0;i<invalidChars.length;i++) {
         badChar= invalidChars.charAt(i)
		 if (inputStr.indexOf(badChar,0) > -1)
			  return false;
	}

    //check to see if there is atleast one period, (".")
	periodPos = inputStr.indexOf(".",0)
	if (periodPos == -1) {
	    return false;
	}

	//check to see if there is atleast two characters after the "."
	if (periodPos+3 > inputStr.length){
		return false;
	}
			
	return true;
 }

//
//Returns true or false based on if the postal code is valid.
//
function IsPostalCode(strPostalCode) {
	var strBuffer, i
	
	strPostalCode = strPostalCode.toUpperCase();

	//remove from PostalCode all the invalid characters
	strBuffer = '';
	for(i = 0; i < strPostalCode.length; i++)
		if(('0' <= strPostalCode.charAt(i) && strPostalCode.charAt(i) <= '9') || ('A' <= strPostalCode.charAt(i) && strPostalCode.charAt(i) <= 'Z'))
			strBuffer += strPostalCode.charAt(i);

	//postal code validation
	for(i = 0; i <5 ; i+=2)
		if(!('A' <= strBuffer.charAt(i) && strBuffer.charAt(i) <= 'Z'))
			return false;

	for(i = 1; i <6 ; i+=2)
		if(!('0' <= strBuffer.charAt(i) && strBuffer.charAt(i) <= '9'))
			return false;

	return true;
}

//
//Returns a new String object that contains a formated postal code
//
function FormatPostalCode(strPostalCode) {
	var strBuffer, i

	strPostalCode = strPostalCode.toUpperCase();
	
	//remove from Postal Code all the invalid characters
	strBuffer = '';
	for(i = 0; i < strPostalCode.length; i++)
		if(('0' <= strPostalCode.charAt(i) && strPostalCode.charAt(i) <= '9') || ('A' <= strPostalCode.charAt(i) && strPostalCode.charAt(i) <= 'Z'))
			strBuffer += strPostalCode.charAt(i);
	
	//format postal code
	strPostalCode = strBuffer.substr(0, 3) + ' ' + strBuffer.substr(3, 3);
	
	return strPostalCode;
}
