//************************************
// openSized window
function openSized(in_uri,theWidth,theHeight) {
	var theTop = 0;
	var theLeft = 0;
	var theBars = 0;
	var WindowName = "pw" + RNumber(9999);
	if (theWidth > screen.width || theHeight > screen.height) {
		theBars = 1;
		theWidth += 22;
		if ((theWidth + 30) > screen.width) {
			theWidth = screen.width - 30;
		} else {
			theLeft=(screen.width/2)-(theWidth/2);
			theWidth += 15;
		}
		if ((theHeight + 30 ) > screen.height) {
			theHeight = screen.height - 30;
		} else {
			theTop=((screen.height/2)-(theHeight/2))/2;
		}
	} else {
		theTop=((screen.height/2)-(theHeight/2))/2;
		theLeft=(screen.width/2)-(theWidth/2);
	}
	var features='height=' + theHeight + ',width=' + theWidth + ',top='+theTop+',left='+theLeft+',toolbar=0,Location=0,Directories=0,Status=1,menubar=0,Scrollbars=' + theBars + ',Resizable=1';
	window.open(in_uri,WindowName,features);

}

//************************************
// Set Check Boxes
function SetCheckBoxes(inCheckBoxName, inFrmName, inState) {
	var looper = 0;
	while (eval("inFrmName." + inCheckBoxName)[looper]) {
		eval("inFrmName." + inCheckBoxName)[looper].checked = inState;
		looper += 1;
	}
	if (eval("inFrmName." + inCheckBoxName)) {
		eval("inFrmName." + inCheckBoxName).checked = inState;
	}
}

//************************************
// Random Number
function RNumber(inMax) {
	var randomnumber=Math.floor(Math.random()*inMax) + 1;
	return randomnumber;

}

//************************************
// Toggle visibility
function ToggleVisibility(inID) {
	var s = document.getElementById(inID);
	if (s.style.display == "none") {
		s.style.display = "block";
	} else {
		s.style.display = "none";
	}
}

//************************************
// isEmail validates an email address
function isEmail(str) {
	if (str.length < 3) {
		return false;
	}
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
		return false;
	}

	if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr){
		return false;
	}

	if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr){
		return false;
	}

	if (str.indexOf(at,(lat+1)) != -1){
		return false;
	}

	if (str.substring(lat-1,lat) == dot || str.substring(lat+1,lat+2) == dot){
		return false;
	}

	if (str.indexOf(dot,(lat+2) )== -1){
		return false;
	}

	if (str.indexOf(" ") != -1){
		return false;
	}

	return true;
}

//************************************
// SetVisibility
function SetVisibility(inID, inState) {
	var s = document.getElementById(inID);
	if (inState) {
		s.style.display = "block";
	} else {
		s.style.display = "none";
	}
}

//************************************
// open the Rich Text Editor
function openRichText(FieldName) {
	openSized("rte/RichText.asp?ReturnForm=frmMain&ReturnField=" + FieldName,650,400);
}

//**************************************************************************************************
//  Date and Clock Functions
//**************************************************************************************************

// --------------------------------------------------------------
//Is Date
var dtCh= "/";
var minYear=1900;
var maxYear=2100;
function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

// Days Array for isDate function
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

// Is Integer for isDate function
function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

// for isDate Function
function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

// --------------------------------------------------------------
// Date Difference
function suycDateDiff( start, end, interval, rounding ) {

	var iOut = 0;

	// Create 2 error messages, 1 for each argument.
	var startMsg = "Check the Start Date and End Date\n"
		startMsg += "must be a valid date format.\n\n"
		startMsg += "Please try again." ;

	var intervalMsg = "Sorry the dateAdd function only accepts\n"
		intervalMsg += "d, h, m OR s intervals.\n\n"
		intervalMsg += "Please try again." ;

	var bufferA = Date.parse( start ) ;
	var bufferB = Date.parse( end ) ;

	// check that the start parameter is a valid Date.
	if ( isNaN (bufferA) || isNaN (bufferB) ) {
		alert( startMsg ) ;
		return null ;
	}

	// check that an interval parameter was not numeric.
	if ( interval.charAt == 'undefined' ) {
		// the user specified an incorrect interval, handle the error.
		alert( intervalMsg ) ;
		return null ;
	}

	var number = bufferB-bufferA ;

	// what kind of add to do?
	switch (interval.charAt(0))
	{
		case 'd': case 'D':
			iOut = parseInt(number / 86400000) ;
			if(rounding) iOut += parseInt((number % 86400000)/43200001) ;
			break ;
		case 'h': case 'H':
			iOut = parseInt(number / 3600000 ) ;
			if(rounding) iOut += parseInt((number % 3600000)/1800001) ;
			break ;
		case 'm': case 'M':
			iOut = parseInt(number / 60000 ) ;
			if(rounding) iOut += parseInt((number % 60000)/30001) ;
			break ;
		case 's': case 'S':
			iOut = parseInt(number / 1000 ) ;
			if(rounding) iOut += parseInt((number % 1000)/501) ;
			break ;
			default:
			// If we get to here then the interval parameter
			// didn't meet the d,h,m,s criteria.  Handle
			// the error.
			alert(intervalMsg) ;
			return null ;
	}

	return iOut ;
}

// --------------------------------------------------------------
// Date Add
function dateAdd( start, interval, number ) {

	// Create 3 error messages, 1 for each argument.
	var startMsg = "Sorry the start parameter of the dateAdd function\n"
		startMsg += "must be a valid date format.\n\n"
		startMsg += "Please try again." ;

	var intervalMsg = "Sorry the dateAdd function only accepts\n"
		intervalMsg += "d, h, m OR s intervals.\n\n"
		intervalMsg += "Please try again." ;

	var numberMsg = "Sorry the number parameter of the dateAdd function\n"
		numberMsg += "must be numeric.\n\n"
		numberMsg += "Please try again." ;

	// get the milliseconds for this Date object.
	var buffer = Date.parse( start ) ;

	// check that the start parameter is a valid Date.
	if ( isNaN (buffer) ) {
		alert( startMsg ) ;
		return null ;
	}

	// check that an interval parameter was not numeric.
	if ( interval.charAt == 'undefined' ) {
		// the user specified an incorrect interval, handle the error.
		alert( intervalMsg ) ;
		return null ;
	}

	// check that the number parameter is numeric.
	if ( isNaN ( number ) )	{
		alert( numberMsg ) ;
		return null ;
	}

	// so far, so good...
	//
	// what kind of add to do?
	switch (interval.charAt(0))
	{
		case 'd': case 'D':
			number *= 24 ; // days to hours
			// fall through!
		case 'h': case 'H':
			number *= 60 ; // hours to minutes
			// fall through!
		case 'm': case 'M':
			number *= 60 ; // minutes to seconds
			// fall through!
		case 's': case 'S':
			number *= 1000 ; // seconds to milliseconds
			break ;
			default:
			// If we get to here then the interval parameter
			// didn't meet the d,h,m,s criteria.  Handle
			// the error.
			alert(intervalMsg) ;
			return null ;
	}
	return new Date( buffer + number ) ;
}

// Days Array for isDate function
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   }
   return this
}

//Is Date
function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strMonth=dtStr.substring(0,pos1)
	var strDay=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}


// --------------------------------------------------------------
// Clock Functions
var clockID = 0;

function UpdateClock() {
	if(clockID) {
		clearTimeout(clockID);
		clockID  = 0;
	}

	var tDate = new Date();
	tDate = dateAdd(tDate,"s", TimeOffset);
	var tHours = tDate.getHours();
	var tAmPm = "AM";
	if (tHours > 12) {
		tHours = tHours - 12;
		tAmPm = "PM";
	}
	var tMinutes = tDate.getMinutes().toString();
	var tSeconds = tDate.getSeconds().toString();
	if (tMinutes.length == 1) {
		tMinutes = "0" + tMinutes;
	}
	if (tSeconds.length == 1) {
		tSeconds = "0" + tSeconds;
	}

	document.theClock.theTime.value = ""
		+ tHours + ":"
		+ tMinutes + ":"
		+ tSeconds + " "
		+ tAmPm;

	clockID = setTimeout("UpdateClock()", 1000);
}

function StartClock() {
	clockID = setTimeout("UpdateClock()", 500);
}

function KillClock() {
	if(clockID) {
		clearTimeout(clockID);
		clockID  = 0;
	}
}


function OpenPage(strPage, strWindowName, intHeight, intWidth, intTop, intLeft, bolToolbar, bolMenubar, bolScrollbars, bolResizable, bolLocation, bolDirectories, bolStatus) {
	//Send 1 for True and 0 for False for all boolean parms.
	strFeatures = "height=" + intHeight + ",width=" + intWidth + ",top=" + intTop + ",left=" + intLeft
	strFeatures = strFeatures + ",toolbar=" + bolToolbar + ",menubar=" + bolMenubar + ",scrollbars=" + bolScrollbars + ",resizable=" + bolResizable + ",location=" + bolLocation + ",directories=" + bolDirectories + ",status=" + bolStatus
	window.open (strPage, strWindowName, strFeatures);
}

function createElement(element) {
	if (typeof document.createElementNS != 'undefined') {
		return document.createElementNS('http://www.w3.org/1999/xhtml', element);
	}
	if (typeof document.createElement != 'undefined') {
		return document.createElement(element);
	}
	return false;
}

 function textareaResizer() {
	 var labels = document.getElementsByTagName('label');
	 for(i=0; i<labels.length; i++) {
		 if(labels[i].getAttribute("for") == "AboutUs") {
			 enlarge = createElement('a');
			 enlarge.setAttribute('style', 'cursor: pointer;');
			 enlarge.onclick = function () { document.getElementById('AboutUs').rows += 5; }
			 enlarge.setAttribute('title', 'Click to enlarge the message textarea');
			 enlarge.appendChild(document.createTextNode('Enlarge textarea \u2193'));
			 decrease = createElement('a');
			 decrease.setAttribute('style', 'cursor: pointer;');
			 decrease.onclick = function () { document.getElementById('AboutUs').rows -= 5; }
			 decrease.setAttribute('title', 'Click to decrease the message textarea');
			 decrease.appendChild(document.createTextNode('Decrease textarea \u2191'));

			 small = createElement('small');
			 small.appendChild(document.createTextNode(' ('));
			 small.appendChild(enlarge);
			 small.appendChild(document.createTextNode(' | '));
			 small.appendChild(decrease);
			 small.appendChild(document.createTextNode(')'));
			 labels[i].appendChild(small);
		 }
	 }
 }


function redirectMain(redirUrl) {
	window.opener.location.href = redirUrl;
}

function openCentered (url, width, height, windowName, featureString) {
	if (!windowName){
		windowName = '';
	}
	if (!featureString){
		featureString = '';
	}else{
		featureString = ',' + featureString;
	}
	var x = Math.round((screen.availWidth - width) / 2);
	var y = Math.round((screen.availHeight - height) / 2);
	featureString = 'left=' + x + ',top=' + y + ',width=' + width + ',height=' + height + featureString;

	var thisWindow;
	otherwin = window.open (url, windowName, featureString);
	bClosed = otherwin.closed;
}



function TestClosed() {
   if (null == otherwin) {
       alert("The Other Window has not been opened yet!");
       return;
   }

   try{
      bClosed = otherwin.closed;
   }catch(e){
     // alert("The Other Window is Currently Closed (exception thrown)");
      return;
   }

   if (bClosed) {
       alert("The Other Window is Currently Closed");
   }else{
       alert("The Other Window is Currently Open");
   }
}


function __onPrevPagebak (){
	//methinks below must be set to the number of pages you have
	for (var i=2; i<4; i++) {//this was (var i=2; i<4; i++) but that made the Next got one too far!
		if (document.getElementById ("Page" +i).style.display == "block") {
			document.getElementById ("Page" + i).style.display = "none";
			document.getElementById ("Page" + (i - 1)).style.display = "block";
			break;
		}
	}
}

function __onNextPagebak (){
	//methinks below must be set to the number of pages you have
	for (var i=1; i<3; i++) {//this was (var i=1; i<3; i++) but that made the Next got one too far!
		if (document.getElementById ("Page" + i).style.display == "block") {
			document.getElementById ("Page" + i).style.display = "none";
			document.getElementById ("Page" + (i + 1)).style.display = "block";
			break;
		}
	}
}