/////////////////////////////////////////////////////////////////////////////////////////////////////
function  replace(string, substring1, substring2 , scope )
{
if(!scope){scope='1'}
if(scope!='ALL'){scope='1'}
re = '/'+substring1
if(scope=='1'){re += '/'}
else{re +='/g'}
new_string = string.replace(eval(re),substring2)
return new_string
}
//////////////////////////////////////////////////////////
function arrayappend(array,value){
eval(array)[eval(array).length] = value
}
////////////////////////////////////////
function compare(a,b){
return a-b;
}
//////////////////////////////////////////////////////////////////////
function left(string,count) {
	return string.substr(0,count);
}
//////////////////////////////////////////////////////////////////////
function right(mystring,count)  {
	return mystring.substring(mystring.length-count,mystring.length);
}
/////////////////////////////////////////////////////////////////////
function mid(myString,start,count) {
	return myString.substr(start-1,count)
}
/////////////////////////////////////////
function len(mystring)  {
	return mystring.length;
	}
/////////////////////////////////////////
function ucase(mystring)  {
return mystring.toUpperCase()
}
function lcase(mystring)  { //alert (mystring)
	if (mystring != '' && mystring != null) {
	return mystring.toLowerCase()} else {return ''}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
function IsEmail(text) 
{
	var Temp     = text;
	var AtSym    = Temp.indexOf('@');
	var AtSym2   = Temp.lastIndexOf('@'); // check for multiple @
	var Period   = Temp.lastIndexOf('.');
	var Space    = Temp.indexOf(' ');
	var Length   = Temp.length - 1;   // Array is from 0 to length-1

	//if(Temp.value != '')//email is not a required field.
	//{
	  if (Temp.substr(0,1)== "'" && Temp.substring(Temp.length-1,Temp.length)=="'")
	  {
	  	return false;
	  }
	  else if ((AtSym < 1) ||                     // '@' cannot be in first position
    	(Period <= AtSym+1) ||             // Must be atleast one valid char btwn '@' and '.'
    	(Period == Length ) ||             // Must be atleast one valid char after '.'
    	(Space  != -1) ||                 // No empty spaces permitted
			(AtSym != AtSym2))								// No multiple '@', only one '@' is valid
   	  {  
		return false;
   	  }
	  return true;
	//}
}
//////////////////////////////////////////////////////////////////////
function matchPasswords(Password1,Password2,MinLength){
	var Password=Password1;
	var Password1=Password2;
	var MinLength=MinLength;
	if ((len(Password)) < MinLength){
		alert ("Invalid password. Please type a password that is at least "+MinLength+" characters long and contains at least 1 number.");
		return false
	} 
	else if(Password != Password1){
    	alert('Passwords do not match. Please re-enter.');
    	//document.forms[0].Password.focus(); 
		return false;
  	}
	else	return true;
}
//////////////////////////////////////////////////////////////////////
function openWindow(filePath,winName,toolbar,menubar,height,width) {
	if (!filePath) filePath = '';
	if (!winName) winName = 'newWin';
	if (!height) height = (window.screen.availHeight * 0.85);
	if (!width) width = (window.screen.availWidth * 0.85);		
	xPos = (window.screen.availHeight * 0.015);
	yPos = (window.screen.availWidth * 0.015);
	attribs='height='+height+','+'width='+width;
	if ((toolbar) && (!toolbar == '')) attribs=attribs+','+toolbar;
	if ((menubar) && (!menubar == '')) attribs=attribs+','+menubar;
	attribs=attribs+',scrollbars';
	newWindow=window.open(filePath,winName,attribs);		
	newWindow.moveTo(xPos,yPos) 
	newWindow.focus();		
}
//////////////////////////////////////////////////////////////////////

function CheckInteger (number)
{
	if (number.length == 0)
		return true;

	var decimalformat = ".";
	var checkchar = number.indexOf(decimalformat);

	if (checkchar == -1)
		return CheckNumber(number);
	else
		return false;
}

function CheckNumberRange(objectvalue, minvalue, maxvalue)
{
	if (minvalue != null)
	{
		if (objectvalue < minvalue)
			return false;
	}

	if (maxvalue != null)
	{
		if (objectvalue > maxvalue)
			return false;
	}

	return true;
}

function CheckNumber(number)
{
	if (number.length == 0)
		return true;

	var startformat = " .+-0123456789";
	var numberformat = " .0123456789";
	var checkchar;
	var decimal = false;
	var trailingblank = false;
	var digits = false;

	checkchar = startformat.indexOf(number.charAt(0));

	if (checkchar == 1)
		decimal = true;
	else if (checkchar < 1)
		return false;

	for (var i = 1; i < number.length; i++)
	{
		checkchar = numberformat.indexOf(number.charAt(i));
		if (checkchar < 0)
			return false;
		else if (checkchar == 1)
		{
			if (decimal)
				return false;
			else
				decimal = true;
		}
		else if (checkchar == 0)
		{
			if (decimal || digits)	
				trailingblank = true;
		}
		else if (trailingblank)
			return false;
		else
			digits = true;
	}	

	return true
}

function CheckRange(objectvalue, minvalue, maxvalue)
{
	if (objectvalue.length == 0)
		return true;

	if (!CheckNumber(objectvalue))
		return false;
	else
		return (CheckNumberRange((eval(objectvalue)), minvalue, maxvalue));

	return true;
}

function CheckDay(checkYear, checkMonth, checkDay)
{
	maxDay = 31;

	if (checkMonth == 4 || checkMonth == 6 ||
		checkMonth == 9 || checkMonth == 11)
		maxDay = 30;
	else if (checkMonth == 2)
	{
		if (checkYear % 4 > 0)
			maxDay =28;
		else if (checkYear % 100 == 0 && checkYear % 400 > 0)
			maxDay = 28;
		else
			maxDay = 29;
	}

	return CheckRange(checkDay, 1, maxDay);
}

function CheckCreditCard(objectvalue)
{
	if (objectvalue.length == 0)
		return true;
	var whitespace = " -";
	var creditcardstring="";
	var checkchar;

	for (var i = 0; i < objectvalue.length; i++)
	{
		checkchar = whitespace.indexOf(objectvalue.charAt(i));
		if (checkchar < 0)
			creditcardstring += objectvalue.substring(i, (i + 1));
	}	

	if (creditcardstring.length < 13 || creditcardstring.length > 19)
		return false;

	if (creditcardstring.charAt(0) == "+")
		return false;

	if (!CheckInteger(creditcardstring))
		return false;

	var doubledigit = creditcardstring.length % 2 == 1 ? false : true;
	var checkdigit = 0;
	var tempdigit;

	for (var i = 0; i < creditcardstring.length; i++)
	{
		tempdigit = eval(creditcardstring.charAt(i));

		if (doubledigit)
		{
			tempdigit *= 2;
			checkdigit += (tempdigit % 10);

			if ((tempdigit / 10) >= 1.0)
				checkdigit++;

			doubledigit = false;
		}
		else
		{
			checkdigit += tempdigit;
			doubledigit = true;
		}
	}	
	return (checkdigit % 10) == 0 ? true : false;
}

function CheckPhone(objectvalue)
{
	if (objectvalue.length == 0)
		return true;

	if (objectvalue.length != 12)
		return false;

	if (!CheckNumber(objectvalue.substring(0,3)))
		return false;
	else if (!CheckNumberRange((eval(objectvalue.substring(0,3))), 100, 1000))
		return false;

	if (objectvalue.charAt(3) != "-" && objectvalue.charAt(3) != " ")
		return false

	if (!CheckNumber(objectvalue.substring(4,7)))
		return false;
	else if (!CheckNumberRange((eval(objectvalue.substring(4,7))), 100, 1000))
		return false;

	if (objectvalue.charAt(7) != "-" && objectvalue.charAt(7) != " ")
		return false;

	if (objectvalue.charAt(8) == "-" || objectvalue.charAt(8) == "+")
		return false;
	else
		return (CheckInteger(objectvalue.substring(8,12)));
}

function CheckZipcode(objectvalue)
{
	if (objectvalue.length == 0)
		return true;

	if (objectvalue.length != 5 && objectvalue.length != 10)
		return false;

	if (objectvalue.charAt(0) == "-" || objectvalue.charAt(0) == "+")
		return false;

	if (!CheckInteger(objectvalue.substring(0,5)))
		return false;

	if (objectvalue.length == 5)
		return true;

	if (objectvalue.charAt(5) != "-" && objectvalue.charAt(5) != " ")
		return false;

	if (objectvalue.charAt(6) == "-" || objectvalue.charAt(6) == "+")
		return false;

	return (CheckInteger(objectvalue.substring(6,10)));
}

function CheckTime(objectvalue)
{	
	objectvalue=objectvalue.toUpperCase();
	AM=0;
	if (objectvalue.indexOf('AM')>0 || objectvalue.indexOf('PM')>0)
	{
		if (objectvalue.indexOf('AM')>0)
			AM=1;
		objectvalue=objectvalue.substring(0,objectvalue.length-2);
	}
		
	if (objectvalue.length == 0)
		return true;

	isplit = objectvalue.indexOf(':');

	if (isplit == -1 || isplit == objectvalue.length)
		return false;

	sHour = objectvalue.substring(0, isplit);
	iminute = objectvalue.indexOf(':', isplit + 1);
	
	if (iminute == -1 || iminute == objectvalue.length)
		sMin = objectvalue.substring((sHour.length + 1));
	else
		sMin = objectvalue.substring((sHour.length + 1), iminute);

	if (!CheckInteger(sHour))
		return false;
	else if (AM==1 && !CheckRange(sHour, 0, 12))
		return false;
	else if (!CheckRange(sHour, 0, 23))
		return false;

	if (!CheckInteger(sMin))
		return false;
	else
	if (!CheckRange(sMin, 0, 59))
		return false;

	if (iminute != -1)
	{
		sSec = objectvalue.substring(iminute + 1);

		if (!CheckInteger(sSec))
			return false;
		else if (!CheckRange(sSec, 0, 59))
			return false;	
	}

	return true;
}

function CheckDate(objectvalue)
{
	if (objectvalue.length == 0)
		return true;

	isplit = objectvalue.indexOf('/');

	if (isplit == -1 || isplit == objectvalue.length)
		return false;

	sMonth = objectvalue.substring(0, isplit);

	if (sMonth.length == 0)
		return false;

	isplit = objectvalue.indexOf('/', isplit + 1);

	if (isplit == -1 || (isplit + 1 ) == objectvalue.length)
		return false;

	sDay = objectvalue.substring((sMonth.length + 1), isplit);

	if (sDay.length == 0)
		return false;

	sYear = objectvalue.substring(isplit + 1);

	if (!CheckInteger(sMonth))
		return false;
	else if (!CheckRange(sMonth, 1, 12))
		return false;
	else if (!CheckInteger(sYear))
		return false;
	else if (!CheckRange(sYear, 0, 9999))
		return false;
	else if (!CheckInteger(sDay))
		return false;
	else if (!CheckDay(sYear, sMonth, sDay))
		return false;
	else
		return true;
}
