var undefined;
function isUndefined(property) {
	return (typeof property == 'undefined');
}

function isValidEmail(inputStr) {
	var testValue = inputStr.indexOf("@")
	if(!(testValue > 0)){  return false }
	if(!(inputStr.indexOf(".",(testValue + 1)) > (testValue + 1))) { return false}
	return true
}
function isBlank(inputStr) {
	var len=inputStr.length
	var i
	for (i=0; i<len; ++i) {
		if(inputStr.charAt(i)!= " ") return false
	}
	return true
}
function ReplaceQuotes(s) {return s.replace(/\"/g, "'");}

function isAddress1(ss, min, max){
	var inputStr = ss.toString()
	var BadChar = "~!@$%^*_+|`={}<>?[];"
	var inputStr2 = BadChar.toString()
	var SearchItem = " "
	if (inputStr == null || inputStr == ""){// can not empty
		return false
	}
	if((inputStr.length > 0) && (inputStr.search(SearchItem) == 0)){//no leading space
		return false
	}
	if ((inputStr.length >= min)&&(inputStr.length <= max)){
		for (var i = 0; i < inputStr.length; i++){
			for (var j = 0; j < inputStr2.length; j++){
				if((inputStr.charAt(i) == inputStr2.charAt(j))){
					return false;
				}
			}
		}
		return true;
	}
	else{
		return false;
	}
}

function isAllowedCharsOnly(ss,OkChar) {
	var inputStr	= ss.toString()
	var match
	for (var i = 0; i < inputStr.length; i++){
		match = false;
		for (var j = 0; j < OkChar.length; j++){
			if(inputStr.charAt(i) == OkChar.charAt(j)){
				match=true;
				j=OkChar.length;
			}
		}
		if(!match){
			return false;
		}
	}
	return true;
}
function isDecimalOnly(ss){
	var inputStr	= ss.toString()
	var OkChar		= ".1234567890"
	return isAllowedCharsOnly(ss,OkChar);
}
function isCurrencyOnly(ss){
	var inputStr	= ss.toString()
	var OkChar		= ".1234567890$,"
	return isAllowedCharsOnly(ss,OkChar);
}
function isZipOnly(ss){
	var inputStr	= ss.toString()
	var OkChar		= "-1234567890"
	return isAllowedCharsOnly(ss,OkChar);
}
function isCreditCardNumberOnly(ss){
	var inputStr	= ss.toString()
	var OkChar		= "-1234567890"
	return isAllowedCharsOnly(ss,OkChar);
}
function isCreditCardNumberMaskedOnly(ss){
	var inputStr	= ss.toString()
	var OkChar		= "-1234567890*"
	return isAllowedCharsOnly(ss,OkChar);
}
function isPhoneOnly(ss){
	var inputStr	= ss.toString()
	var OkChar		= "-1234567890"
	return isAllowedCharsOnly(ss,OkChar);
}
function isDecimalOnly(ss){
	var inputStr	= ss.toString()
	var OkChar		= ".1234567890"
	var match
	return isAllowedCharsOnly(ss,OkChar);
}
function isZipOnly(ss){
	var inputStr	= ss.toString()
	var OkChar		= "-1234567890"
	var match
	return isAllowedCharsOnly(ss,OkChar);
}
function isPhoneOnly(ss){
	var inputStr	= ss.toString()
	var OkChar		= "-1234567890"
	var match
	return isAllowedCharsOnly(ss,OkChar);
}
var phone_field_length=0;
function TabNext(obj,event,len,next_field) {
if (event == "down") {
	phone_field_length=obj.value.length;
	}
else if (event == "up") {
	if (obj.value.length != phone_field_length) {
		phone_field_length=obj.value.length;
		if (phone_field_length == len) {
			next_field.focus();
			}
		}
	}
}

function isAlphaNumericOnly(ss,min,max,bolSpace){
	var inputStr	= ss.toString()
	var OkChar
	var match
	if ( isEmpty(min) ) min = 1;
	if ( isEmpty(max) ) max = 1000;
	if((inputStr.length >= min)&&(inputStr.length <= max)){
		if(bolSpace)	{ OkChar	= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890"	}
		else			{ OkChar	= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"	}
		return isAllowedCharsOnly(ss,OkChar);
	} else {
		return false;
	}
}

function isAlphaOnly(ss,min,max,bolSpace){
	var inputStr = ss.toString()
	var OkChar
	var match
	if ( isEmpty(min) ) min = 1;
	if ( isEmpty(max) ) max = 1000;
	if((inputStr.length >= min)&&(inputStr.length <= max)){
		if(bolSpace)	{ OkChar =  "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz"	}
		else			{ OkChar =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"	}
		return isAllowedCharsOnly(ss,OkChar);
	} else {
		return false;
	}

}
function isName(ss,min,max,bolSpace){
	var inputStr = ss.toString()
	var OkChar
	var match
	if ( isEmpty(min) ) min = 1;
	if ( isEmpty(max) ) max = 1000;
	if((inputStr.length >= min)&&(inputStr.length <= max)){
		if(bolSpace)	{ OkChar =  "ABCDEFGHIJKLMNOPQRSTUVWXYZ'.- abcdefghijklmnopqrstuvwxyz"	}
		else			{ OkChar =  "ABCDEFGHIJKLMNOPQRSTUVWXYZ'.-abcdefghijklmnopqrstuvwxyz"	}
		return isAllowedCharsOnly(ss,OkChar);
	} else {
		return false;
	}

}

function isNumericOnly(ss,min,max,bolSpace){
	var inputStr = ss.toString()
	var OkChar
	var match
	var testVal;
	if ( isEmpty(min) ) min = 1;
	if ( isEmpty(max) ) max = 1000000000;

	//alert('min: '+min+'\n'+'max: '+max+'\n'+'ss: '+inputStr.length+'\n');
	if((inputStr.length >= min)&&(inputStr.length <= max)){
		if(bolSpace)	{ OkChar =  "12345 67890"	}
		else			{ OkChar =  "1234567890"	}
		return isAllowedCharsOnly(ss,OkChar);
	} else {
		return false;
	}

}
function getDecimalOnly(ss){
	var OkChar =  ".1234567890";
	return getAllowedCharsOnly(ss, OkChar);
}
function getNumericOnly(ss, bolSpace){
	var OkChar;
	if(bolSpace)	{ OkChar =  "12345 67890"	}
	else			{ OkChar =  "1234567890"	}
	return getAllowedCharsOnly(ss, OkChar);
}
function getZipOnly(ss){
	var OkChar =  "-1234567890"
	return getAllowedCharsOnly(ss, OkChar);
}
function getPhoneOnly(ss){
	var OkChar =  "-1234567890"
	return getAllowedCharsOnly(ss, OkChar);
}
function getAlphaNumericOnly(ss, bolSpace){
	var OkChar;
	if(bolSpace)	{ OkChar	= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz 1234567890"	}
	else			{ OkChar	= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"	}
	return getAllowedCharsOnly(ss, OkChar);
}
function getAlphaOnly(ss, bolSpace){
	var OkChar;
	if(bolSpace)	{ OkChar =  "ABCDEFGHIJKLMNOPQRSTUVWXYZ abcdefghijklmnopqrstuvwxyz"	}
	else			{ OkChar =  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"	}
	return getAllowedCharsOnly(ss, OkChar);
}
function getAllowedCharsOnly(ss, OkChar){
	var inputStr = ss.toString()
	var newVal = "";
	for(var i = 0; i < inputStr.length; i++){
		for (var j = 0; j < OkChar.length; j++){
			if((inputStr.charAt(i) == OkChar.charAt(j))){
				newVal += inputStr.charAt(i)
			}
		}
	}
	return newVal;
}

function isEmpty(s) {
	if (s == undefined) return true;
	var inputStr = s.toString();
	// the first one executes them all.... therefore it is better the last way...
	//return ((s == null) || (inputStr == null) || (inputStr.length == 0) || isBlank(inputStr) );
	if (s == null)					return true;
	else if (inputStr == null)		return true;
	else if (inputStr.length == 0)	return true;
	else if (isBlank(inputStr) )	return true;
	else 							return false;
}

function isFloat(s, emptyOK)
{
// Returns true if valid float
  if(isEmpty(s))
    if(isFloat.arguments.length == 1) return false;
    else return (emptyOK == true);

  AlreadyHaveDecimalPoint = new Number(0);
  for(i = new Number(0); i < s.length; i++)
  {
    if((s.charAt(i) >= "0") && (s.charAt(i) <= "9")) continue;
    if(s.charAt(i) != ".") return false;
    if(AlreadyHaveDecimalPoint == 1) return false;
    AlreadyHaveDecimalPoint = 1;
  }
  return true;
}

function isCurrency(s, emptyOK)
{
// Returns true if valid currency
  if(!isFloat(s, emptyOK)) return false;
  if (s=="0") return false;
	pos = new Number(0);
  for(i = new Number(s.length) - 1; i >= 0; i--)
  {
    pos++;
    if(s.charAt(i) == ".")
			if(pos > 3) return false;
			else return true;
  }
  return true;
}

function isNumber(s, emptyOK)
{
// Returns true if positive number
  if(isEmpty(s))
    if(isNumber.arguments.length == 1) return false;
    else return (emptyOK == true);

  for(i =0; i < s.length; i++)
    if(!((s.charAt(i) >= "0") && (s.charAt(i) <= "9"))) return false;
  return true;
}

function isWhiteSpace(s)
{
  var whitespace = " \t\n\r";
  if(isEmpty(s)) return true;

  for(i = new Number(0); i < s.length; i++)
    if(whitespace.indexOf(s.charAt(i)) == -1) return false;

  return true;
}

function isEmail(s, emptyOK)
{
// Must conform to a@b.c
  s = s.toString()
  if(!emptyOK) { emptyOK = false; }
  if(isEmpty(s)) return (emptyOK == true);

  i = new Number(s.indexOf("@", 1));
  if(i == -1) return false;

  i = (s.indexOf(".", i + 2));
  if(i == -1) return false;

  return (s.length > i + 1);
}

function isFullPhoneNbr(ss, emptyOK)
{
	var OkChar =  "-1234567890() "
	return getAllowedCharsOnly(ss, OkChar);
}
function isPhoneNbrPart(nbr, numLenReq, emptyOK)
{
	if(!emptyOK) { emptyOK = false; }
	if(isEmpty(nbr)) return (emptyOK == true);
	if (!isNumber(nbr)) return false;
	if ( nbr.length < numLenReq ) return false;
	return true;
}

function isPhoneNbr(areacode, exchange, nbr, emptyOK)
{

  if(isEmpty(areacode.value + exchange.value + nbr.value)){
    if(isPhoneNbr.arguments.length == 3) return false;
    else return (emptyOK == true);
  }

  if(!isNumber(areacode.value + exchange.value + nbr.value)) return false;
  else
  {
    if (!isNumber(exchange.value + nbr.value)) return false;
    else
      if (!isNumber(nbr.value)) return false;
  }
  return true;
}

 function ResetUI(focusObj, ErrMsg)
{
	if (ErrMsg > "") {
		alert(ErrMsg);
	}
	focusObj.focus();
	return (false);
}

function isUnchecked(theObj) {
	var is_checked = false;
	for (var i=0;i<theObj.length;i++) {
		if (theObj[i].checked) is_checked = true;
	}
	if (is_checked) { return(0); } else { return(1); }
}

function getNumChecked(theObj) {
	var num_checked = 0;
	if ( theObj ) {
		if ( theObj.length ) {
			for (var i=0;i<theObj.length;i++) {
				if (theObj[i].checked) num_checked++;
			}
		} else {
			if (theObj.checked) num_checked++;
		}
	}
	return(num_checked);
}

function isSelected(theObj) {
	var is_selected = false;
	for (var i=0;i<theObj.length;i++) {
		if (theObj[i].selected) is_selected = true;
	}
	if (is_selected) { return(1); } else { return(0); }
}
function clear_select_list(the_obj) {
    for (var i = the_obj.length;i > 0;i--)
        the_obj.options[0] = null;
}

function unset_select_list(the_obj) {
	the_obj.selectedIndex = -1;
}

function set_checked(theObj, the_val) {
	for (var i=0;i<theObj.length;i++) {
		if (theObj[i].value == the_val) theObj[i].checked = true;
	}
}
function get_checked(theObj) {
	for (var i=0;i<theObj.length;i++) {
		if (theObj[i].checked) return(theObj[i].value);
	}
}

function isUnselected(theObj,minSelection) {
	if (!minSelection) minSelection = 0;
	if (theObj.selectedIndex >= minSelection) { return(0); } else { return(1); }
}
function strtrim() {
    //Match spaces at beginning and end of text and replace
    //with null strings
    return this.replace(/^\s+/,'').replace(/\s+$/,'');
}
function trim2(str) {
    str.replace(/^\s*/, '').replace(/\s*$/, '');
   return str;
}
function remove_whitespace(s) {
    //Match spaces at beginning and end of text and replace
    //with null strings
	s = s.toString()
	return s.replace(/\s/g,'');
}

function strltrim() {
	return this.replace(/^\s+/,'');
}

function strrtrim() {
	return this.replace(/\s+$/,'');
}
function strtrim() {
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

String.prototype.ltrim = strltrim;
String.prototype.rtrim = strrtrim;
String.prototype.trim = strtrim;


function open_free_window(the_file_path, the_window_name, the_width, the_height, resizable, do_reposition, do_scroll) {
	if (isUndefined(the_window_name) || the_window_name == '')	the_window_name		= 'Popup';
	if (isUndefined(the_width) || the_width == '')				the_width			= '750';
	if (isUndefined(the_height) || the_height == '')			the_height			= '550';
	if (isUndefined(resizable) || resizable == '')				resizable			= 'yes';
	if (isUndefined(do_scroll) || do_scroll == '')				do_scroll			= 'yes';
	open_window(the_file_path, the_window_name, the_width, the_height, resizable, do_reposition, do_scroll);
}

function open_normal_window(the_file_path, the_window_name, the_width, the_height, resizable, do_reposition, do_scroll, extras) {
	if (isUndefined(the_window_name) || the_window_name == '')	the_window_name		= 'Popup';
	if (isUndefined(the_width) || the_width == '')				the_width			= '750';
	if (isUndefined(the_height) || the_height == '')			the_height			= '550';
	if (isUndefined(resizable) || resizable == '')				resizable			= 'yes';
	if (isUndefined(do_scroll) || do_scroll == '')				do_scroll			= 'yes';
	if (isUndefined(extras) || extras == '')					extras				= 'status=yes,toolbar=yes,titlebar=yes,menubar=yes,location=yes';
	open_window(the_file_path, the_window_name, the_width, the_height, resizable, do_reposition, do_scroll, extras);
}

function open_window(the_file_path, the_window_name, the_width, the_height, resizable, do_reposition, do_scroll, extras) {
	if (the_window_name == '')	the_window_name		= 'Popup';
	if (the_width == '')		the_width			= '300';
	if (the_height == '')		the_height			= '300';
	if (resizable == '')		resizable			= 'no';
	if (do_scroll == '')		do_scroll			= 'no';
	if (!isUndefined(extras) || extras != '')		extras	= ','+extras;
	var windowXY									= '';

	if (do_reposition != 'N') {
		if (window.screen) {
	    	var the_screenX = Math.floor((screen.availWidth-the_width)/2);
		    var the_screenY = Math.floor((screen.availHeight-the_height)/2);
			windowXY = ',screenX='+the_screenX+',screenY='+the_screenY+',left='+the_screenX+',top='+the_screenY;
		}
	}
	windowHandle = window.open('',the_window_name,'scrollbars='+do_scroll+',status=no,width='+the_width+',height='+the_height+',resizable='+resizable+windowXY+extras)
	windowHandle.location.href = the_file_path;
	windowHandle.focus();
}
function SetDisabledStatus(theCheckObj, theActionObj, the_min, the_max, the_new_val_disabled, the_display_text) {
	if (the_min == '')	the_min = 0;
	if (the_max == '')	the_max = 0;
	if ( the_new_val_disabled == undefined ) the_new_val_disabled = false;
	// see if it was previously disable
	var prev_disabled = false;
	if (theActionObj.disabled) prev_disabled = true;
	if ( (theCheckObj.selectedIndex >= the_min) && (theCheckObj.selectedIndex <= the_max) ) {
		theActionObj.disabled	= the_new_val_disabled;
	} else {
		theActionObj.disabled = !(the_new_val_disabled);
	}
	if ( (theActionObj.disabled) && (the_display_text != '') ) {
		theActionObj.value		= the_display_text;
	} else {
		if (prev_disabled) theActionObj.value = '';
	}
}

function isDisabled(obj) {
	if ( obj.disabled )	{ return true; }
	else				{ return false; }
}

function disable_element(obj)	{
	if (obj) {
		obj.value='';
		obj.style.background='#eeeeee';
		obj.disabled = true;
		if( obj.type.indexOf('select-one') >=0 ) { obj.selectedIndex = 0; }
	}
}
function enable_element(obj)	{
	if (obj) {
		obj.disabled = false;
		obj.style.background='#ffffff';
	}
}
function reload_page() {
	window.location.reload(true);
}
function sendTo(theUrl) {
	//alert(theUrl);
	document.location.href = theUrl;
}

function getFileExtension(inputId) {
	var fileinput = document.getElementById(inputId);
	if(!fileinput ) return "";
	var filename = fileinput.value;
	if( filename.length == 0 ) return "";
	var dot = filename.lastIndexOf(".");
	if( dot == -1 ) return "";
	var extension = filename.substr(dot+1,filename.length);
	return extension.toLowerCase();
}

function checkImageFileType(inputId) {
	var m_aTypes = ['jpg','gif'];
	var ext = getFileExtension(inputId);
	if( m_aTypes.inArray(ext) )	return(1);
	else   						return(0);
}

function checkVideoFileType(inputId) {
	var m_aTypes = ['swf','mov','mpg','mpeg','avi','gif','wmv'];
	var ext = getFileExtension(inputId);
	if( m_aTypes.inArray(ext) )	return(1);
	else   						return(0);
}

Array.prototype.inArray = function(search_term) {
  var i = this.length;
  if (i > 0) {
	 do {
		if (this[i] === search_term) { return true; }
	 } while (i--);
  }
  return false;
}

function doToggleCheckboxObject(theObj){
    if ( theObj ) {
        if ( theObj.length ) {
            for (var i=0;i<theObj.length;i++) {
                if (theObj[i].checked) {
                    theObj[i].checked = false;
                } else {
                    theObj[i].checked = true;
                }
            }
        } else {
            if (theObj.checked) {
                theObj.checked = false;
            } else {
                theObj.checked = true;
            }
        }
    }
}