var AutoRefresh = 1;
var ToolTip = 0;
var ShowLabel = 1;
var pagestr = pagestr;
var AccountID = AccountID;
var strClicksns = FormatS();
var strClicks = "" + FormatS();
var strClicky = "button " + strClicks;
var ctd = 0;
setTimeout('afterLoad();', 2500);
var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 13;
var phonevalue1;
var phonevalue2;
var cursorposition;
var chc = 2;

//setTimeout('ChangeHeader()', 25000);

//function ChangeHeader() {
//	var l = getObj('logoheader');
//	if (l != null) {
//		l.style.backgroundImage = 'URL(templates/final/images/logo' + chc + '.jpg)';
//		chc = chc + 1;
//		if (chc > 8) {
//			chc = 2;
//		}
//	}
//	setTimeout('ChangeHeader()', 25000);
//}

function ParseForNumber1(object){
phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object){
phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object,e, x) { 
if(e){ 
e = e 
} else {
e = window.event 
} 
if(e.which){ 
var keycode = e.which 
} else {
var keycode = e.keyCode 
}

ParseForNumber1(object)

if(keycode >= 48){
ValidatePhone(object)
}
verifyPhone(object.value,x);
}

function backspacerDOWN(object,e, x) { 
if(e){ 
e = e 
} else {
e = window.event 
} 
if(e.which){ 
var keycode = e.which 
} else {
var keycode = e.keyCode 
}
ParseForNumber2(object)
verifyPhone(object.value,x);

} 



function GetCursorPosition(){

var t1 = phonevalue1;
var t2 = phonevalue2;
var bool = false
for (i=0; i<t1.length; i++)
{
if (t1.substring(i,1) != t2.substring(i,1)) {
if(!bool) {
cursorposition=i
bool=true
}
}
}
}

function ValidatePhone(object){

var p = phonevalue1

p = p.replace(/[^\d]*/gi,"")

if (p.length < 3) {
object.value=p
} else if(p.length==3){
pp=p;
d4=p.indexOf('(')
d5=p.indexOf(')')
if(d4==-1){
pp="("+pp;
}
if(d5==-1){
pp=pp+")";
}
object.value = pp;
} else if(p.length>3 && p.length < 7){
p ="(" + p; 
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

object.value = pp; 

} else if(p.length >= 7){
p ="(" + p; 
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

l40 = pp.length;
p40 = pp.substring(0,8);
p40 = p40 + "-"

p41 = pp.substring(8,l40);
ppp = p40 + p41;

object.value = ppp.substring(0, maxphonelength);
}

GetCursorPosition()

if(cursorposition >= 0){
if (cursorposition == 0) {
cursorposition = 2
} else if (cursorposition <= 2) {
cursorposition = cursorposition + 1
} else if (cursorposition <= 5) {
cursorposition = cursorposition + 2
} else if (cursorposition == 6) {
cursorposition = cursorposition + 2
} else if (cursorposition == 7) {
cursorposition = cursorposition + 4
e1=object.value.indexOf(')')
e2=object.value.indexOf('-')
if (e1>-1 && e2>-1){
if (e2-e1 == 4) {
cursorposition = cursorposition - 1
}
}
} else if (cursorposition < 11) {
cursorposition = cursorposition + 3
} else if (cursorposition == 11) {
cursorposition = cursorposition + 1
} else if (cursorposition >= 12) {
cursorposition = cursorposition
}

var txtRange = object.createTextRange();
txtRange.moveStart( "character", cursorposition);
txtRange.moveEnd( "character", cursorposition - object.value.length);
txtRange.select();
}

}

function ParseChar(sStr, sChar)
{
if (sChar.length == null) 
{
zChar = new Array(sChar);
}
else zChar = sChar;

for (i=0; i<zChar.length; i++)
{
sNewStr = "";

var iStart = 0;
var iEnd = sStr.indexOf(sChar[i]);

while (iEnd != -1)
{
sNewStr += sStr.substring(iStart, iEnd);
iStart = iEnd + 1;
iEnd = sStr.indexOf(sChar[i], iStart);
}
sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

sStr = sNewStr;
}

return sNewStr;
}

function afterLoad() {
if (window.top.getObj('warning') == null) {
	var popper= document.createElement('div');
	popper.style.position = 'absolute';
	popper.style.display = 'none';
	popper.style.left = 0;
	if (getViewportHeight() > 0)  {
		popper.style.paddingLeft = (getViewportHeight() / 2) - 200;
	}
	popper.style.top = 0;
	popper.style.zIndex = 50000;
	popper.style.paddingTop = 150;
	popper.style.width = getViewportWidth();
	popper.style.height = getViewportHeight();
	popper.id = 'warning';
	popper.style.backgroundImage  = 'URL(/maskbg.png)';
	popper.style.backgroundRepeat  = 'repeat';
	window.top.document.body.appendChild(popper);
	setTimeout('window.top.checkLogin();',600000); //900000
}
}

function checkLogin() {
	//alert(window.opener);
	//alert(window.parent);
	//if (window.opener != null) {
	//} else 
	if (AccountID > 0) {
		var w = getObj('warning');
		if (w != null) {
			if (AutoRefresh != 0) {
				checkLogin2();
			} else {
				w.style.paddingLeft = (getViewportHeight() / 2) - 200;
				w.innerHTML = '<div class="pagebox" style="width: 400px; height: 300px; text-align: center; border: 2px solid black;"><div class="content">Your website session may be about to expire, and you will be logged out.<br><br>Click OK to continue your session.<br><br><span id=timed>10 seconds ...</span><br><br><a class="buttonb" id="b_greenbutton" href="javascript:void(0);" onclick="checkLogin2(); return false;">OK</a></div></div>';
				w.style.display='';
				countdown = 20;
				clearTimeout(ctd);
				var ctd = setTimeout('countdownTimer();', 1000);
			}
		}
	}
}

function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
	return window.underfined;

}

function countdownTimer() {
	clearTimeout(ctd);
	if (getObj('timed') == null) {
	} else {
		var ctd = setTimeout('countdownTimer();', 1000);
		countdown = countdown - 1;
		getObj('timed').innerHTML = countdown + ' seconds ...';
		if (countdown <= 0) {
			getObj('warning').style.display='none';
			window.location.replace('/logout.asp');
		}
	}
}


function checkLogin2() {
	clearTimeout(ctd);
	var w = getObj('warning');
	if (w != null) {
		w.style.display='none';
	}
	w.innerHTML = '<iframe src="/session.asp"></iframe>';
	//window.open('/session.asp','session','width=200, height=200,modal=true,status=0,');
	setTimeout('window.top.checkLogin();',600000);	
}

function PostSpeak(speakvalue) {
	if (tinyMCE != null) {
		getObj('Message').value = tinyMCE.getContent();
		getObj('stufff').submit();
	}
}

	
	function RemoveTiny(t) {
		var elm = window.top.tinyMCE.getInstanceById(t);
		if (elm) {
			window.top.tinyMCE.execCommand('mceRemoveControl', false, t);
		}
	}

	function AddTiny(t) {
		var elm = window.top.tinyMCE.getInstanceById(t);
		if (elm) {
			window.top.tinyMCE.execCommand('mceRemoveControl', false, t);
		}
		window.top.tinyMCE.execCommand('mceAddControl', false, t);
	}


function FormatS() {
	return "";
}

function submitform(f) {
	document.forms[f].submit();
}

function AdBr() {
document.write("<br>");
}

function AdB(Actions, Titles, Names) {
document.write("<" + strClicky + "  onclick=\"window.location.replace(\'" + Actions + "\');\" title=\"" + Titles + "\">" + Names + "</button>");
}

function Pages(Count, CurPage) {
	PagesL(Count, CurPage, 6, 0);
}

function AddPageButton(i, CurPage, ii) {
	return '<a ' + (CurPage != i ? ' style="width:20" onclick="GoP(\'' + i + '\')" class="page_menu" id="orangebutton"' : ' style="width:20; font-weight: bold;" class="page_menu" id="greenbutton"') + '>' + ii + '</a>';        
}

function GoP(p) {
	window.location.replace('?P=' + p + (pagestr != null ? '' + pagestr : ''));
}

function AddNavButton(CurPage, Count, PageLimit, ii, offsetp) {
	return '<a class="page_menu" id="orangebutton" style="width:20" onclick="PagesL(' + Count + ',' + CurPage + ',' + PageLimit + ',' +offsetp + ')">' + ii + '</a>';        
}

function AddPageButton2() {
	return '<a class="page_menu" id="orangebutton" style="width:20; font-weight: bold">1</a>';
}

function PagesL(Count, CurPage, PageLimit, offsetp) {
var strTest = '';
var v = 0;
var startp = 1;
var endp = Count;
var starth = '';
var endh = '';
var minp = Math.floor(PageLimit / 2);
var maxp =PageLimit + minp;

if (offsetp == 0) {offsetp = CurPage}
if (offsetp > Count) {offsetp = Count - minp}

if (Count > PageLimit + minp) {
	startp = offsetp - minp;
	if (startp < 1) {startp = 1};
	if (offsetp  < 1) {offsetp = 1};
	endp = startp + maxp;
	if (endp > Count) {
		endp = Count;
	}
	starth = AddNavButton(CurPage, Count, PageLimit,'<<',1) + AddNavButton(CurPage, Count, PageLimit, '<',(startp - PageLimit) - 1);
	endh = AddNavButton(CurPage, Count, PageLimit, '>',(endp + minp) + 1) + AddNavButton(CurPage, Count, PageLimit, '>>',Count - PageLimit);
}

if (Count == 0) {
	strTest += AddPageButton2();
} else {
	for (i = startp; i <= endp; i++) 
	{         
		strTest += AddPageButton(i, CurPage, i);

	}
}
getObj('Pages').innerHTML = '<table class=\'weakercell\'><tr><td>Page: </td><td>' + starth + '' + strTest + '' + endh + '</td></tr></table>';
}


function PagesAZ(CurPage) {
var strTest = '';
var v = 0;
for (i = 1; i <= 26; i++) 
{         
	strTest += AddPageButton(String.fromCharCode(i + 64), CurPage, String.fromCharCode(i + 64));

	if (i == 13) {
		strTest += '<br>';
	}
}

getObj('Pages').innerHTML = '<table class=\'weakercell\'><tr><td>' + strTest + '<td></tr></table>';
}

function Ada(Actions, Titles, Names) {
document.write("<" + strClicky + " onclick=\"" + Actions + ";\" title=\"" + Titles + "\">" + Names + "</button>");
}

function Adf(Actions, Titles, Names) {
document.write("<" + strClicky + " onclick=\"" + Actions + ";\" title=\"" + Titles + "\">" + Names + "</button>");
}

function fxn(stuff) {
  var str = '';
  var s;

	for (s = 0; s < stuff.value.length; s++) {
		if (stuff.value.charCodeAt(s) >= 48 && stuff.value.charCodeAt(s) <= 57) {
			str = str + '' + stuff.value.charAt(s);
		}
	}
	stuff.value = str;
	if (stuff.value == '' || stuff.value == null) {
		stuff.value = 0;
	}
	if (stuff.value.length > 1 && stuff.value.charCodeAt(0) == 48)  {
		stuff.value = stuff.value.substr(1,stuff.value.length - 1)
	}
}

function getObj(objn) {
	if (document.getElementsByName(objn).length <= 1) {
		return document.getElementById(objn);
	} else {
		return document.getElementsByName(objn);
	}
}

function fxkp(e) {
	e = e || window.event;	
	if (e != null) {
		var f = e.keyCode;
		if (e.which != null && f == 0) {
			f = e.which;
		}

		if ((f < 45 || f > 57) && f != 8 && f != 9 && f != 13 && f != 192) {
			return false;
		} else {
			return true;
		}
	} else {
		return true;
	}
}

function FormEnter(e, f) {
	e = e || window.event;
	if (e == null) {
	} else {
		if (e.keyCode == 13) {
			if (f != null)	 {
				eval(f);
			}
		}
	}
}

//FILE: RegExpValidate.js

function validateEmail( strValue) {
var objRegExp  =  /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,6})+$/;

  //check for valid email
  return objRegExp.test(strValue);
}

function validateUSPhone( strValue ) {
  var objRegExp  = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/;

  //check for valid us phone with or without space between
  //area code
  return objRegExp.test(strValue);
}

function  validateNumeric( strValue ) {
  var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;

  //check for numeric characters
  return objRegExp.test(strValue);
}

function validateInteger( strValue ) {
  var objRegExp  = /(^-?\d\d*$)/;

  //check for integer characters
  return objRegExp.test(strValue);
}

function validateNotEmpty( strValue ) {
   var strTemp = strValue;
   strTemp = trimAll(strTemp);
   if(strTemp.length > 0){
     return true;
   }
   return false;
}

function validateUSZip( strValue ) {
var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

  //check for valid US Zipcode
  return objRegExp.test(strValue);
}

function validateUSDate( strValue ) {
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    if ((strValue.match(RegExPattern)) && (strValue!='')) {
       return true;
    } else {
	return false;
    } 

}

function validateValue( strValue, strMatchPattern ) {
var objRegExp = new RegExp( strMatchPattern);

 //check if string matches pattern
 return objRegExp.test(strValue);
}


function rightTrim( strValue ) {
var objRegExp = /^([\w\W]*)(\b\s*)$/;

      if(objRegExp.test(strValue)) {
       //remove trailing a whitespace characters
       strValue = strValue.replace(objRegExp, '$1');
    }
  return strValue;
}

function leftTrim( strValue ) {
var objRegExp = /^(\s*)(\b[\w\W]*)$/;

      if(objRegExp.test(strValue)) {
       //remove leading a whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function trimAll( strValue ) {
 var objRegExp = /^(\s*)$/;

    //check for all spaces
    if(objRegExp.test(strValue)) {
       strValue = strValue.replace(objRegExp, '');
       if( strValue.length == 0)
          return strValue;
    }

   //check for leading & trailing spaces
   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
   if(objRegExp.test(strValue)) {
       //remove leading and trailing whitespace characters
       strValue = strValue.replace(objRegExp, '$2');
    }
  return strValue;
}

function removeCurrency( strValue ) {
  var objRegExp = /\(/;
  var strMinus = '';

  //check if negative
  if(objRegExp.test(strValue)){
    strMinus = '-';
  }

  objRegExp = /\)|\(|[,]/g;
  strValue = strValue.replace(objRegExp,'');
  if(strValue.indexOf('$') >= 0){
    strValue = strValue.substring(1, strValue.length);
  }
  return strMinus + strValue;
}

function addCurrency( strValue ) {
  var objRegExp = /-?[0-9]+\.[0-9]{2}$/;

    if( objRegExp.test(strValue)) {
      objRegExp.compile('^-');
      strValue = addCommas(strValue);
      if (objRegExp.test(strValue)){
        strValue = '(' + strValue.replace(objRegExp,'') + ')';
      }
      return '$' + strValue;
    }
    else
      return strValue;
}

function removeCommas( strValue ) {
  var objRegExp = /,/g; //search for commas globally

  //replace all matches with empty strings
  return strValue.replace(objRegExp,'');
}

function addCommas( strValue ) {
  var objRegExp  = new RegExp('(-?[0-9]+)([0-9]{3})');

    //check for match to search criteria
    while(objRegExp.test(strValue)) {
       //replace original string with first group match,
       //a comma, then second group match
       strValue = strValue.replace(objRegExp, '$1,$2');
    }
  return strValue;
}

function removeCharacters( strValue, strMatchPattern ) {
 var objRegExp =  new RegExp( strMatchPattern, 'gi' );

 //replace passed pattern matches with blanks
  return strValue.replace(objRegExp,'');
}


function verifyPassword(v, divid) {
	if (v.length <= 3) {
		getObj(divid).innerHTML = '<i style="color: red;">Too short</i>';
		return false;
	} else {
		getObj(divid).innerHTML = '';
	}
}
function verifyFormat(v, divid, format) {
	if (v.length <= 3) {
		getObj(divid).innerHTML = '<i style="color: red;">Too short</i>';
		return false;
	} else {
		getObj(divid).innerHTML = '';
	}
}
function verifyData(v, divid) {
	if (v.length <= 2) {
		getObj(divid).innerHTML = '<i style="color: red;">Too short</i>';
		return false;
	} else {
		getObj(divid).innerHTML = '';
	}
}
function verifyZip(v, divid) {
	if (validateUSZip(v) == false) {
		getObj(divid).innerHTML = '<i style="color: red;">Invalid Zip Code</i>';
		return false;
	} else {
		getObj(divid).innerHTML = '';
	}
}

function verifyDate(v, divid) {
	if (validateUSDate(v) == false) {
		getObj(divid).innerHTML = '<i style="color: red;">Invalid Date mm/dd/yyyy</i>';
		return false;
	} else {
		getObj(divid).innerHTML = '';
	}
}

function verifyPhone(v, divid) {
	
	//
	if (validateUSPhone(v) == false) {
		getObj(divid).innerHTML = '<i style="color: red;">Format: (999)999-9999</i>';
		return false;
	} else {
		getObj(divid).innerHTML = '';
	}
}
function verifyEmail(v, divid) {
	if (validateEmail(v) == false) {
		getObj(divid).innerHTML = '<i style="color: red;">Invalid email address</i>';
		return false;
	} else {
		getObj(divid).innerHTML = '';
	}
}


function verifyNumber(v, divid) {
	if (validateInteger(v) == false) {
		getObj(divid).innerHTML = '<i style="color: red;">Invalid number</i>';
		return false;
	} else {
		getObj(divid).innerHTML = '';
	}
}

function verifyNumeric(v, divid) {
	if (validateNumeric(v) == false) {
		getObj(divid).innerHTML = '<i style="color: red;">Invalid number x.xx</i>';
		return false;
	} else {
		getObj(divid).innerHTML = '';
	}
}

function SetCheckBox(cbn, cbv) {
	//
	if (getObj(cbn) != null) {
		if (cbv > 0) {
			getObj(cbn).checked = true;
		} else {
			getObj(cbn).checked = false;
		}
	}
	//
}

function SetRadio(cbn, cbv) {
	//
	//if (getObj(cbn) != null) {
		var my_names = document.getElementsByName(cbn);
		for(var i=0; i<my_names.length; i++)
		{	
			var d = document.getElementsByName(cbn)[i];
			if (d != null) {
				if (d.value == cbv) {
					d.checked = true
					break;
				}
			}
		}
	//}
	//
}

function SetSelect(cbn, cbv) {
	//
	if (getObj(cbn) != null) {
		var myChildren = getObj(cbn); 
		for (var s = 0; s < myChildren.options.length; s++) { 
			if (myChildren.options[s].value == cbv) {
				myChildren.selectedIndex = s;
				break;
			}
		}
	}
	//
}

function MessageTo(a, subject) {
	window.open('/message.asp?target=' + a + '&subject=' + URLencode(subject));
}


function URLencode2(sStr) {

	var s = sStr;
	s = s.replace(/</g, '&#60;');
	s = s.replace(/>/g, '&#62;');
	s = s.replace(/\'/g, '&#39;');
	s = escape(s).replace(/\+/g, '%2B');
	return s;
}

function URLencode(sStr) {
	return escape(sStr).replace(/\+/g, '%2B');
}

function PagesF(Count, CurPage, PageLimit, offsetp) {
var strTest = '';
var v = 0;
if (Count == 0) {
	strTest += AddPageButton2();
} else {
	for (i = 1; i <= Count; i++) 
	{         
		strTest += '<option value="' + i + '" ' + (i == CurPage ? 'selected' : '') + '>' + i + '</option>';

	}
}
getObj('Pages').innerHTML = '<table class=\'weakercell\'><tr><td>Page: </td><td><select onchange="GoP(this.options[this.selectedIndex].value);">' + strTest + '</select></td></tr></table>';
}

