function toggleBlockDisplay(oNode) { 
    if (document.getElementById) { 
        var oStyle=document.getElementById(oNode).style;
        if (oStyle.display=="block") {
            oStyle.display="none";
        } else {
            oStyle.display="block";
        }
        return false;
    } else {
        return true;
    }
}

function toggleInlineDisplay(oNode) { 
    if (document.getElementById) { 
        var oStyle=document.getElementById(oNode).style;
        if (oStyle.display=="inline") {
            oStyle.display="none";
        } else {
            oStyle.display="inline";
        }
        return false;
    } else {
        return true;
    }
}

function toggleMultiBlockDisplay(oNode) { 
    if (document.getElementById) { 
        for  (i=0;document.getElementById(oNode+''+i)!=null;i++) {
            var oStyle=document.getElementById(oNode).style;
            if (oStyle.display=="block") {
                oStyle.display="none";
            } else {
                oStyle.display="block";
            }
        }
        return false;
    } else {
        return true;
    }
}

function toggleMultiInlineDisplay(oNode) { 
    if (document.getElementById) { 
        for  (i=0;document.getElementById(oNode+''+i)!=null;i++) {
            var oStyle=document.getElementById(oNode+''+i).style;
            if (oStyle.display=="inline") {
                oStyle.display="none";
            } else {
                oStyle.display="inline";
            }
        }
        return false;
    } else {
        return true;
    }
}

//
// Sets the focus to the first editable control on the page
//
function setFocusToFirstControl() {
    var bFound = false;
    for (var f=0; f < document.forms.length; f++) {
        for(var i=0; i < document.forms[f].length; i++) {
            if (document.forms[f][i].tagName === 'FIELDSET') {
                for(var j=0; j < document.forms[f].childNodes.length; j++) {
                    if (document.forms[f].childNodes[j].type != "hidden" && 
                        document.forms[f].childNodes[j].type != "submit" && 
                        document.forms[f].childNodes[j].type != "button" && 
                        document.forms[f].childNodes[j].tabIndex != null &&
                        document.forms[f].childNodes[j].tabIndex == 0) {
                        if (document.forms[f].childNodes[j].disabled != true) {
                            if (document.forms[f].childNodes[j].type == "text" ||
                            document.forms[f].childNodes[j].type == "textarea") {
                                if (document.forms[f].childNodes[j].readOnly != true) {
                                    document.forms[f].childNodes[j].focus();
                                    bFound = true;
                                }
                            }
                            else {
                                document.forms[f].childNodes[j].focus();
                                bFound = true;
                            }
                        }
                    }
                    if (bFound == true) {
                        break;
                    }
                }
            } else {
                if (document.forms[f][i].type != "hidden" && 
                    document.forms[f][i].type != "submit" && 
                    document.forms[f][i].type != "button" && 
                    document.forms[f][i].tabIndex != null &&
                    document.forms[f][i].tabIndex == 0) {
                    if (document.forms[f][i].disabled != true) {
                        if (document.forms[f][i].type == "text" ||
                        document.forms[f][i].type == "textarea") {
                            if (document.forms[f][i].readOnly != true) {
                                document.forms[f][i].focus();
                                bFound = true;
                            }
                        }
                        else {
                            document.forms[f][i].focus();
                            bFound = true;
                        }
                    }
                }
                if (bFound == true) {
                    break;
                }
            }
        }
        if (bFound == true) {
            break;
        }
    }
}
//SetFocusToFirstControl();

// Date Validation Javascript
// copyright 30th October 2004, 31st December 2009 by Stephen Chapman
// http://javascript.about.com

// You have permission to copy and use this javascript provided that
// the content of the script is not changed in any way.

function valDateFmt(datefmt) {myOption = -1;
for (i=0; i<datefmt.length; i++) {if (datefmt[i].checked) {myOption = i;}}
if (myOption == -1) {alert("You must select a date format");return ' ';}
return datefmt[myOption].value;}
function valDateRng(daterng) {myOption = -1;
for (i=0; i<daterng.length; i++) {if (daterng[i].checked) {myOption = i;}}
if (myOption == -1) {alert("You must select a date range");return ' ';}
return daterng[myOption].value;}
function stripBlanks(fld) {var result = "";var c=0;for (i=0; i<fld.length; i++) {
if (fld.charAt(i) != " " || c > 0) {result += fld.charAt(i);
if (fld.charAt(i) != " ") c = result.length;}}return result.substr(0,c);}
var numb = '0123456789';
function isValid(parm,val) {if (parm == "") return true;
for (i=0; i<parm.length; i++) {if (val.indexOf(parm.charAt(i),0) == -1)
return false;}return true;}
function isNumber(parm) {return isValid(parm,numb);}
var mth = new Array(' ','january','february','march','april','may','june','july','august','september','october','november','december');
var day = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
function validateDate(fld,fmt,rng) {
var dd, mm, yy;var today = new Date;var t = new Date;fld = stripBlanks(fld);
if (fld == '') return false;var d1 = fld.split('\/');
if (d1.length != 3) d1 = fld.split(' ');
if (d1.length != 3) return false;
if (fmt == 'u' || fmt == 'U') {
  dd = d1[1]; mm = d1[0]; yy = d1[2];}
else if (fmt == 'j' || fmt == 'J') {
  dd = d1[2]; mm = d1[1]; yy = d1[0];}
else if (fmt == 'w' || fmt == 'W'){
  dd = d1[0]; mm = d1[1]; yy = d1[2];}
else return false;
var n = dd.lastIndexOf('st');
if (n > -1) dd = dd.substr(0,n);
n = dd.lastIndexOf('nd');
if (n > -1) dd = dd.substr(0,n);
n = dd.lastIndexOf('rd');
if (n > -1) dd = dd.substr(0,n);
n = dd.lastIndexOf('th');
if (n > -1) dd = dd.substr(0,n);
n = dd.lastIndexOf(',');
if (n > -1) dd = dd.substr(0,n);
n = mm.lastIndexOf(',');
if (n > -1) mm = mm.substr(0,n);
if (!isNumber(dd)) return false;
if (!isNumber(yy)) return false;
if (!isNumber(mm)) {
  var nn = mm.toLowerCase();
  for (var i=1; i < 13; i++) {
    if (nn == mth[i] ||
        nn == mth[i].substr(0,3)) {mm = i; i = 13;}
  }
}
if (!isNumber(mm)) return false;
dd = parseFloat(dd); mm = parseFloat(mm); yy = parseFloat(yy);
if (yy < 100) yy += 2000;
if (yy < 1582 || yy > 4881) return false;
if (mm == 2 && (yy%400 == 0 || (yy%4 == 0 && yy%100 != 0))) day[1]=29;else day[1]=28;
if (mm < 1 || mm > 12) return false;
if (dd < 1 || dd > day[mm-1]) return false;
t.setDate(dd); t.setMonth(mm-1); t.setFullYear(yy);
if (rng == 'p' || rng == 'P') {
if (t > today) return false;
}
else if (rng == 'f' || rng == 'F') {
if (t < today) return false;
}
else if (rng != 'a' && rng != 'A') return false;
return true;
}

function toggleHiVizStyle(styleSheet) {
    var hiVizId = 'hi-viz-stylesheet';
    if (!getCookie('hiviz') || getCookie('hiviz') === 'false') {
        var headID = document.getElementsByTagName('head')[0];
        var cssNode = document.createElement('link');
        cssNode.type = 'text/css';
        cssNode.rel = 'stylesheet';
        cssNode.id = hiVizId;
        cssNode.href = styleSheet;
        headID.appendChild(cssNode);
        document.cookie = 'hiviz=true';
    } else {
        var headID = document.getElementsByTagName('head')[0];
        var cssNode = document.getElementById(hiVizId);
        headID.removeChild(cssNode);
        document.cookie = 'hiviz=false';
    }        
    return true;   
}

function loadHiVizStyleForNewPage(styleSheet) {
    var hiVizId = 'hi-viz-stylesheet';
    if (getCookie('hiviz') === 'true') {
        var headID = document.getElementsByTagName('head')[0];
        var cssNode = document.createElement('link');
        cssNode.type = 'text/css';
        cssNode.rel = 'stylesheet';
        cssNode.id = hiVizId;
        cssNode.href = styleSheet;
        headID.appendChild(cssNode);
    }        
    return true;   
}

/*
Script Name: Javascript Cookie Script
Author: Public Domain, with some modifications
Script Source URI: http://techpatterns.com/downloads/javascript_cookies.php
Version 1.1.2
Last Update: 5 November 2009

Changes:
1.1.2 explicitly declares i in Get_Cookie with var
1.1.1 fixes a problem with Get_Cookie that did not correctly handle case
where cookie is initialized but it has no "=" and thus no value, the 
Get_Cookie function generates a NULL exception. This was pointed out by olivier, thanks
1.1.0 fixes a problem with Get_Cookie that did not correctly handle
cases where multiple cookies might test as the same, like: site1, site
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
*/

// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );

// To use, simple do: Get_Cookie('cookie_name'); 
// replace cookie_name with the real cookie name, '' are required
function getCookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	var i = '';
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found ) 
	{
		return null;
	}
}

/*
only the first 2 parameters are required, the cookie name, the cookie
value. Cookie time is in milliseconds, so the below expires will make the 
number you pass in the Set_Cookie function call the number of days the cookie
lasts, if you want it to be hours or minutes, just get rid of 24 and 60.

Generally you don't need to worry about domain, path or secure for most applications
so unless you need that, leave those parameters blank in the function call.
*/
function setCookie( name, value, expires, path, domain, secure ) {
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	// if the expires variable is set, make the correct expires time, the
	// current script below will set it for x number of days, to make it
	// for hours, delete * 24, for minutes, delete * 60 * 24
	if ( expires )
	{
		expires = expires * 1000 * 60 * 60 * 24;
	}
	//alert( 'today ' + today.toGMTString() );// this is for testing purpose only
	var expires_date = new Date( today.getTime() + (expires) );
	//alert('expires ' + expires_date.toGMTString());// this is for testing purposes only

	document.cookie = name + "=" +escape( value ) +
		( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
		( ( path ) ? ";path=" + path : "" ) + 
		( ( domain ) ? ";domain=" + domain : "" ) +
		( ( secure ) ? ";secure" : "" );
}

// this deletes the cookie when called
function deleteCookie( name, path, domain ) {
	if ( getCookie( name ) ) document.cookie = name + "=" +
			( ( path ) ? ";path=" + path : "") +
			( ( domain ) ? ";domain=" + domain : "" ) +
			";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// my cookie functions
function canPersistCookie() {
    var testName = 'TestPersistCookie';
    setCookie(testName, 'yes', '', '');
    if (getCookieValue()===null) {
        return false;
    }
    var today = new Date();
    var oldDate = today.setMonth(today.getMonth() -1);
    setCookie(testName, '', '', oldDate.toGMTString());
    return true;
}

// validate email address
// true if valid, else false
// note, there are loads of poss regex patterns, each with pros and cons
function validateEmail(eMail) {  
   // var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
   var emailPattern = /^[a-z][\w\.-]*[a-z0-9]@[a-z0-9][\w\.-]*[a-z0-9]\.[a-z][a-z\.]*[a-z]$/;  
   return emailPattern.test(eMail.toLowerCase());  
}  

// type validators
var val_numb = '0123456789';
var val_lwr = 'abcdefghijklmnopqrstuvwxyz';
var val_upr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';

function isValid(parm,val) {
if (parm == "") return true;
for (i=0; i<parm.length; i++) {
if (val.indexOf(parm.charAt(i),0) == -1) return false;
}
return true;
}

function isNumber(parm) {return isValid(parm,val_numb);}
function isLower(parm) {return isValid(parm,val_lwr);}
function isUpper(parm) {return isValid(parm,val_upr);}
function isAlpha(parm) {return isValid(parm,val_lwr+val_upr);}
function isAlphanum(parm) {return isValid(parm,val_lwr+val_upr+val_numb);} 

// for expanding text box - need tidying but work
function resizeTextarea(t, inpId, maxLen) {
    if (t.value.length > maxLen) return false;
    a = t.value.split('\n');
    b=1;
    for (x=0;x < a.length; x++) {
        if (a[x].length >= t.cols) b+= Math.floor(a[x].length/t.cols);
    }
    b+= a.length-1;
    if (b != t.rows) {
        if (b === 1) {
            // switch to text box
            var inp = document.getElementById(inpId);
            t.style.display = "none";
            t.blur();
            t.disabled = true;
            inp.style.display = "inline";
            inp.disabled = false;
            inp.focus();
            inp.value = t.value;
        } else {
            t.rows = b;
        }
    }
    return true;
}
function resizeTextInput(t, taId) {
    a = t.value.split('\n');
    b=1;
    for (x=0;x < a.length; x++) {
        if (a[x].length >= t.size) b+= Math.floor(a[x].length/t.size);
    }
    b+= a.length-1;
    if (b !== 1) {
        // switch to textarea
        var ta = document.getElementById(taId);
        t.style.display = "none";
        t.blur();
        t.disabled = true;
        ta.style.display = "inline";
        ta.disabled = false;
        ta.focus();
        ta.value = t.value;
    }
    return true;
}
