/*
	function setColor(id,strColor) 
	function setBoldColor(id,strColor) 
	function setBoldRed(id)
	function setBoldBlack(id)
	function setText(id,strVal)
	function isInt(str)
	function isMoney(str)
	function isFlt(str)
	function isBool(str)
	function isDate(strMonth,strDay,strYear)
	function isEmpty(s)
	function isWhitespace (s)
	function isPhoneNo(strVal)
	function isEmail (s)
	function isZipcode(strZip)
	function isFilled(id)
	function lTrim(strTrim)
	function rTrim(strTrim)
	function trim(strTrim)
	function sqlSafe (s)
	function strLenOk(strVal, intMinLen, intMaxLen)
	function passwordOk (strValA, strValB, intMinLen, intMaxLen)
*/

// DEFINE VARIABLES

// whitespace characters
var whitespace = "\s";
var nonIntChr = /[^0-9]/;
var nonFltChr = /[^0-9\.\+]/;


function setColor(id,strColor) {
  if (document.getElementById) {
    document.getElementById(id).style.color = strColor;
  }
  else if (document.all) {
    document.all[id].style.color = strColor;
  }
}

function setRed(id) {
  if (document.getElementById) {
    document.getElementById(id).style.color = 'red';
  }
  else if (document.all) {
    document.all[id].style.color = 'red';
  }
}


function setBoldRed(id) {
  if (document.getElementById) {
    document.getElementById(id).style.fontWeight = 'bold';
    document.getElementById(id).style.color = 'red';
  }
  else if (document.all) {
    document.all[id].style.fontWeight = 'bold';
    document.all[id].style.color = 'red';
  }
}


function setBoldBlack(id) {
  if (document.getElementById) {
    document.getElementById(id).style.fontWeight = 'bold';
    document.getElementById(id).style.color = 'black';
  }
  else if (document.all) {
    document.all[id].style.fontWeight = 'bold';
    document.all[id].style.color = 'black';
  }
}

function setBoldColor(id,strColor) {
  if (document.getElementById) {
    document.getElementById(id).style.fontWeight = 'bold';
    document.getElementById(id).style.color = strColor;
  }
  else if (document.all) {
    document.all[id].style.fontWeight = 'bold';
    document.all[id].style.color = strColor;
  }
}

function setText(id,strVal) {
  if (document.getElementById) {
    document.getElementById(id).innerHTML = strVal;
  }
  else if (document.all) {
    document.all[id].style.innerHTML = strVal;
  }
}

function isInt(str) {
	
	if(str =="" || isNaN(str) || str==null) return false;
	
	str=trim(str);
	if(nonIntChr.test(str)) return false;

	if (isNaN(parseInt(str))) {
		return false;
	}
	else {
		return true;
	}

}

function isMoney(str) {

	if(str =="" || isNaN(str) || str==null) return false;
	
	str=trim(str);
	reNonMoneyChar = /[^\.0-9]/;

	if(reNonMoneyChar.test(str))
		return false;
	else
		return true;
}

function isFlt(str) {

	if(str =="" || isNaN(str) || str==null) return false;

	str=trim(str);
	if(nonFltChr.test(str)) return false;

	if (isNaN(parseFloat(str))) {
		return false;
	}
	else {
		return true;
	}

}

function isBool(str) {

	if(str =="" || isNaN(str) || str==null) return false;
	
	str=trim(str.toLowerCase());
	if (str == "true" || str == "false") {
		return true;
	}
	else {
		return false;
	}

}


function isDate(strMonth,strDay,strYear) {

//check year
	dtToday = new Date();
	if(!isInt(strYear) || (dtToday.getYear() - strYear > 125)) return false;

//check month
	if(!(isInt(strMonth)) || strMonth < 1 || strMonth > 12) return false;

//check day


	if(!(isInt(strDay))) return false;

	if(strMonth == 1 || strMonth == 3 || strMonth == 5 || strMonth == 7 || strMonth == 8 || strMonth == 10 || strMonth == 12) {
		if(strDay < 1 || strDay > 31) return false;
	}
	else if(strMonth == 4 || strMonth == 6 || strMonth == 9 || strMonth == 11) {
		if(strDay < 1 || strDay > 30) return false;
	}
	else if(strMonth == 2) {
		if((strYear % 4) == 0) {
			if(strDay < 1 || strDay > 29) return false;
		}
		else
		{
			if(strDay < 1 || strDay > 28) return false;
		}
	}
	else return false;


	return true;

}

/****************************************************************/

// Check whether string s is empty.

function isEmpty(s) {   
    return ((s == null) || (s.length == 0))
}

/****************************************************************/

// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s) {

    var i;
    //var s = String(xval);

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
	// Check that current character isn't whitespace.
	var c = s.charAt(i);

	if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}



/****************************************************************/

// isEmail (s)
// 
// Email address must be of form a@b.c ... in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.

function isEmail(s) {
   if (isEmpty(s)) return false;
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++;
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++;
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != "."))
	return false;
    else
	return true;
}

/****************************************************************/

// This function determines if the string passed in is a valid
// US zip code.  It accepts either ##### or #####-####.  If the
// string is valid, it returns true, else false.

function isZipcode(strZip) {

	var s = new String(strZip);

	if (s.length != 5 && s.length != 10)
		return false;

	for (var i=0; i < s.length; i++)
		if ((s.charAt(i) < '0' || s.charAt(s) > '9') && s.charAt(i) != '-')
			return false;

	return true;
}

//checks any type of form element to see if has been filled
//if it has, returns true, else returns false
//element must be passed, not value and not id
//pass the element -  without quotes

function isFilled(id){

    var strElementType;
    strElementType = id.type;


  switch (strElementType) {

   case "text":
	if(isWhitespace(id.value)) return false;
	break;

   case "textarea":
	if(isWhitespace(id.value)) return false;
	break;

   case "select-one":
	if((id.selectedIndex == -1) || (id.selectedIndex == 0)) return false;
	break;

   case "checkbox":
	if(id.checked == false) return false;
	break;

   default:
	if(typeof(id[0]) != "undefined")
	{
		if(id[0].type == "radio")
		{
			for (var i = 0; i < id.length; i++)
			{
				if(id[i].checked) return true;	
			}
		 }
	}
	return false;
	break;
  } 

  return true;

}

/****************************************************************/

// Right trims the string

function rTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var c = "";
	var endpos = 0

	for (i = str.length; i >= 0 && endpos == 0; i = i - 1) {
		c = str.charAt(i);
		if (whitespace.indexOf(c) == -1)
			endpos = i;
	}

	return str.substring(0,endpos+1);
}

/****************************************************************/

// Left trims the string

function lTrim(strTrim)
{
	var str = new String(strTrim);
	var i = 0;
	var c = "";
	var startpos = 0

	for (i = 0; i <= str.length - 1 && startpos == 0; i = i + 1) {
		c = str.charAt(i);
		if (whitespace.indexOf(c) == -1)
			startpos = i;
	}

	return str.substring(startpos - 1);
}

/****************************************************************/

// Left trims the string

function trim(strTrim)
{
	return rTrim(lTrim(strTrim));
	
}

function sqlSafe (s)
{
	var new_s = s;
	new_s = replaceAll (new_s, "'", "|");
	new_s = replaceAll (new_s, "|", "''");
	new_s = replaceAll (new_s, "\"", "|");
	new_s = replaceAll (new_s, "|", "''");
	return new_s;
}


function strLenOk(strVal, intMinLen, intMaxLen)
{
	strVal = trim(strVal);

	if (strVal.length < intMinLen || strVal.length > intMaxLen)
		return false;
	else
		return true;

}

function passwordOk (strValA, strValB, intMinLen, intMaxLen)
{
	strValA = trim(strValA)
	strValB = trim(strValB)

	if (strValA.length < intMinLen || strValA.length > intMaxLen || strValA != strValB)
		return false;
	else
		return true;
}

function isPhoneNo(strVal)
{
	var phoneno = "";
	var reAllowed = /[\s*\+\(\)-]/;

	for(i=0; i < strVal.length; i++){
		phoneno = phoneno + (strVal.charAt(i)).replace(reAllowed,"");
	}

	if(!isInt(phoneno))
		return false;
	else
		return true;
}