// -------------------------------------------------------------------------
// (c) Module Solutions 2006 /sjc.
// Wrapper Functions 

function isNotNull(s) {
    // returns TRUE if NOT NULL, FALSE if NULL 
	return (s == "" || s == null) ? false : true; 
}

function isNull(s) {
    // returns TRUE if NOT NULL, FALSE if NULL 
	return (s == "" || s == null) ? true : false; 
}

function isNotEmpty(id) {
  var e=document.getElementById(id);
	var retVal=isNotNull(e.value);
	if (!retVal) { 
	    // This code will prevent the user from continuing if non-valid 
	    // entry is made. If this function is used on an input event, you should
		// use the onBlur() event on the form element as opposed to onChange(). 
		// id.focus();
	}
	return retVal;
}

function isEmpty(id) {
    var e=document.getElementById(id);
	var retVal=isNull(e.value);
	if (retVal) { 
	    // This code will prevent the user from continuing if non-valid 
	    // entry is made. If this function is used on an input event, you should
		// use the onBlur() event on the form element as opposed to onChange(). 
		// id.focus();
	}
	return retVal;
}

function isString(id) {
    var e=document.getElementById(id);
	var retVal = isAlpha(e.value);
	if (!retVal) { 
		// This code will prevent the user from continuing if non-valid entry is made. You should
		// use the onBlur() event on the form element as opposed to onChange(). 
		//obj.select(); obj.focus();
	}
	return retVal;
}

function isNumber(id) {
    var e=document.getElementById(id);
	var retVal = isNumeric(e.value);
	if (!retVal) { 
		// This code will prevent the user from continuing if non-valid entry is made. You should
		// use the onBlur() event on the form element as opposed to onChange(). 
		//obj.select(); obj.focus();
	}
	return retVal;
}

function isValidEmail(id) {
    var e=document.getElementById(id);	
	var retVal = isEmail(e.value);
	if (!retVal) { 
		// This code will prevent the user from continuing if non-valid entry is made. You should
		// use the onBlur() event on the form element as opposed to onChange(). 
		//obj.select(); obj.focus(); 
	}
	return retVal;
}

function isAlpha(s) {
    // returns TRUE if ALPHA, FALSE if NON ALPHA CHARS FOUND
	var retVal = isNotNull(s); if (retVal) {
	for (var i = 0; i < s.length; i++) { retVal = isCharAlpha(s.charCodeAt(i));
	if (!retVal) { break; } } } return retVal;
}

function isCharAlpha(c) {
    // returns TRUE if char in range, FALSE if not
    if(c>=65 && c<=90) return true;
    if(c>=97 && c<=122) return true;
	if(c==32) return true;  // space 
	if(c==45) return true;  // - 
	return false;
}

function isNumeric(s) {
    // returns TRUE if NUMERIC, FALSE if NON NUMERIC CHAR FOUND
	var retVal = isNotNull(s); if (retVal) {
	for (var i = 0; i < s.length; i++) { retVal = isCharNumeric(s.charCodeAt(i)); 
	if (!retVal) { break; } } } return retVal;
}

function isCharNumeric(c) {
    // returns TRUE if char in range, FALSE if not
	return (c>=48 && c<=57) ? true : false;
}

function isEmail(s) {
    // returns TRUE if EMAIL VALID, FALSE if NOT VALID EMAIL
	var retVal,newStr = null; if(s.length<7) return false;
	newStr = s.split("@"); if (newStr.length == 2) { retVal = (newStr[1].indexOf(".")!=-1) ? true : false;
	} else { retVal = false; } return retVal;		
}

function isRadioChecked(obj) {
    var retVal = null;
    for (var i = 0; i<obj.length; i++) { retVal = obj[i].checked; if (retVal) { break; } } return retVal;
}

function initialCap(s) {
  return s.substring(0,1).toUpperCase() + s.substring(1,s.length).toLowerCase();
}

function properCase(s) {
	var r=""; var q=true;
	for(var i=0;i<s.length;i++) {
		r += (q) ? s.charAt(i).toUpperCase() : s.charAt(i).toLowerCase();
		q = (s.charAt(i)==" ") ? true : false;
	}
	return r;    
}


function ltrim(s) {
	while(s.indexOf(" ")==0) {
		s = s.substring(1,s.length);
	} return s;
}

// Tests a document element for checked status
// Returns:
// true, if document element checked or false
function isChecked(obj) {
    var e=document.getElementById(obj);
	var retVal=e.checked;
	if (!retVal) { 
	    // This code will prevent the user from continuing if non-valid 
	    // entry is made. If this function is used on an input event, you should
		// use the onBlur() event on the form element as opposed to onChange(). 
		// id.focus();
	}
	return retVal;
}

// Tests a document element for disabled status
// Returns:
// true, if document element disabled
function isDisabled(obj) {
    var e=document.getElementById(obj);
	var retVal=e.disabled;
	if (!retVal) { 
	    // This code will prevent the user from continuing if non-valid 
	    // entry is made. If this function is used on an input event, you should
		// use the onBlur() event on the form element as opposed to onChange(). 
		// id.focus();
	}
	return retVal;
}

function autoCheck(obj,value) {
	var v,e=document.getElementById(obj);
	e.checked=value;
}


// Toggles the display of document elements. 
// arguments    A string list of document elements to show/hide
function toggleBlock() {
    // toggles the display of the element ids passed as arguments
    var i,e,args=toggleBlock.arguments;
    for(i=0;i<args.length;i++) {
        e=document.getElementById(args[i]);
        e.style.display = (e.style.display == "none") ? "block" : "none";
    }
} 

// Toggles the display of document elements. 
// arguments    A string list of document elements to show/hide
function toggleIBlock() {
    // toggles the display of the element ids passed as arguments
    var i,e,args=toggleIBlock.arguments;
    for(i=0;i<args.length;i++) {
        e=document.getElementById(args[i]);
        e.style.display = (e.style.display == "none") ? "inline" : "none";
    }
} 

// Shows the display of document elements. 
// arguments    
function showBlock() {
    // toggles the display of the element ids passed as arguments
    var i,e,args=showBlock.arguments;
    for(i=0;i<args.length;i++) {
        e=document.getElementById(args[i]);
        e.style.display = "block";
    }
} 

// Hides the display of document elements. 
// arguments    
function hideBlock() {
    // toggles the display of the element ids passed as arguments
    var i,e,args=hideBlock.arguments;
    for(i=0;i<args.length;i++) {
        e=document.getElementById(args[i]);
        e.style.display = "none";
    }
} 

// Utility function to select the corret drop-down
function autoSelectByValue(o,str) {
    var e = document.getElementById(o);   
    for(i=0;i<e.options.length;i++) {
        if(e.options[i].value==str) { e.selectedIndex=i; return true;}
    }    
    return false;
}

function autoSelectByIndex(o,i) {
    var e = document.getElementById(o);   
    if(e.options.length>i) {
        e.selectedIndex=i; return true;
    }    
    return false;
}

// Sets a Cookie with the given name and value.
// name       Name of the cookie
// value      Value of the cookie
// [expires]  Expiration date of the cookie (default: end of current session)
// [path]     Path where the cookie is valid (default: path of calling document)
// [domain]   Domain where the cookie is valid (default: domain of calling document)
// [secure]   Boolean value indicating if the cookie transmission requires a secure transmission
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}


// Gets the value of the specified cookie.
// name  Name of the desired cookie.
//
// Returns:
// A string containing value of specified cookie, or null if cookie does not exist.
function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}


// Deletes the specified cookie.
//
// name      name of the cookie
// [path]    path of the cookie (must be same as path used to create cookie)
// [domain]  domain of the cookie (must be same as domain used to create cookie)
function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" + 
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") + 
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}


// Pause function
// [ms]		milliseconds to pause
function pause(ms){
	var d = new Date(); var n = null;
	while(n-d<ms) { n = new Date(); }
} 

function getAllURLParams( u ) {
    var t, i, nv, args, objArgs = new Object( );
    if( !u ) { u=window.document; } 
    t=u.location.search.substring( 1, u.location.search.length ); args=t.split( '&' );
    for( i=0;i<args.length;i++ ) { nv=args[i].split( '=' ); objArgs[nv[0]] = unescape( nv[1].replace( /[+]/g," " ) ); } return objArgs;
}

function getURLParam( n,u ) {
	var t, i, nv, args; 
	if( !u ) { u=window.document; } 
	t=u.location.search.substring( 1, u.location.search.length ); args=t.split( '&' ); 
	for( i=0;i<args.length;i++ ) { nv=args[i].split( '=' ); if( nv.length>0&&nv[0].toLowerCase( )==n.toLowerCase( ) ) return nv[1]; } return null; 
} 


function Left( s, n ){
	if ( n <= 0 )
	    return "";
	else if ( n > String( s ).length )
	    return str;
	else
	    return String( s ).substring( 0,n );
}

function Right( s, n ){
    if ( n <= 0 )
       return "";
    else if ( n > String( s ).length )
       return str;
    else {
       var i = String( s ).length;
       return String( s ).substring( i, i - n );
    }
}

// Returns random number in the range 0 - [limit]
//
// limit     the upper bound of the number to return
function GetRandom(limit) {
	return Math.round(limit * Math.random());
}





