var dtCh= "/";
var minYear=1900;
var maxYear=2100;

var defaultEmptyOK = false
var USStateCodes = "AL|AK|AS|AZ|AR|CA|CO|CT|DE|DC|FM|FL|GA|GU|HI|ID|IL|IN|IA|KS|KY|LA|ME|MH|MD|MA|MI|MN|MS|MO|MT|NE|NV|NH|NJ|NM|NY|NC|ND|MP|OH|OK|OR|PW|PA|PR|RI|SC|SD|TN|TX|UT|VT|VI|VA|WA|WV|WI|WY|AE|AA|AE|AE|AP"
var USStateCodeDelimiter = "|";
var iStateCode = "This field must be a valid two character U.S. state abbreviation (like CA for California). Please reenter it now."

function checkStateCode (theField, emptyOK)
{
	
	if (checkStateCode.arguments.length == 1) emptyOK = defaultEmptyOK;
    if ((emptyOK == true) && (isEmpty(theField.value))) return true;
    else
    {  theField.value = theField.value.toUpperCase();
       if (!isStateCode(theField.value, false)) 
          return warnInvalid (theField, iStateCode);
       else return true;
    }
}

function isStateCode(s)
{   
	if (isEmpty(s)) 
       if (isStateCode.arguments.length == 1) return defaultEmptyOK;
       else return (isStateCode.arguments[1] == true);
    return ( (USStateCodes.indexOf(s) != -1) &&
             (s.indexOf(USStateCodeDelimiter) == -1) )
}

function ChangeTab()
{
	if (event.keyCode == 13)
	{
		event.keyCode = 9
		
		return true;
	}
}

var defaultEmptyOK = false

function getDateMonth(dateValue)
{
	var split = dateValue.split("/");
	
	return parseInt(split[0], 10);
}

function getDateValue(dateValue)
{
	var split = dateValue.split("/");
	
	return parseInt(split[1], 10);
}

function getDateDay(dateValue)
{
	var split = dateValue.split("/");
	
	return parseInt(split[1], 10);
}

function getDateYear(dateValue)
{
	var split = dateValue.split("/");
	
	return parseInt(split[2], 10);
}

function isInteger (s)
{
	var i;
    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);
        if (!isDigit(c)) return false;
    }
    return true;
}

function isEmpty(s)
{   
	return ((s == null) || (s.length == 0))
}

function isDigit (c)
{	
	return ((c >= "0") && (c <= "9"))
}

function findObj(theObj, theDoc)
{
  var p, i, foundObj;
  
  if(!theDoc) theDoc = document;
  if( (p = theObj.indexOf("?")) > 0 && parent.frames.length)
  {
    theDoc = parent.frames[theObj.substring(p+1)].document;
    theObj = theObj.substring(0,p);
  }
  if(!(foundObj = theDoc[theObj]) && theDoc.all) foundObj = theDoc.all[theObj];
  for (i=0; !foundObj && i < theDoc.forms.length; i++) 
    foundObj = theDoc.forms[i][theObj];
  for(i=0; !foundObj && theDoc.layers && i < theDoc.layers.length; i++) 
    foundObj = findObj(theObj,theDoc.layers[i].document);
  if(!foundObj && document.getElementById) foundObj = document.getElementById(theObj);
  
  return foundObj;
}

function warnInvalid (theField, s)
{   theField.focus()
    theField.select()
    alert(s)
    return false
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";

	for (i = 0; i < s.length; i++)
	{   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    
	return returnString;
}

function daysInFebruary (year)
{
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

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
}

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
}

function LTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) 
   {
      var j=0, i = s.length;

      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
	  {
         j++;
	  }

	  s = s.substring(j, i);
   }

   return s;
}

function RTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) 
   {
      var i = s.length - 1;       // Get length of string

      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;

      s = s.substring(0, i+1);
   }

   return s;
}

function Trim(str)
{
   return RTrim(LTrim(str));
}

function trim(str)
{
   return RTrim(LTrim(str));
}

function ChangeStyle(SelObj, option)
{
	if (SelObj.style)
	{
		if (option == 0)
		{
			SelObj.style.color ="#3366CC";
		}
		else if (option == 1)
		{
			SelObj.style.color ="#191970";
		}
	}
}

function isNumberFloat(inputString)
{
	if (!isNaN(parseFloat(inputString))) 
	{
		if (parseFloat(inputString) == inputString)
		{
			return true
		}
		
		return false
	}

	return false
}

function isNumber(inputString)
{
  return (!isNaN(parseFloat(inputString))) ? true : false;
}

//////////////////////////////////FieldToParm////////////////////////////
var icDATE = 1
var icBOOLEAN = 2
var icFOREIGN_KEY = 3
var icNUMBER = 4
var icSTRING = 5
var icNON_EMPTY_STRING = 6
var icNULLABLE_STRING = 7
var icNULLABLE_NUMBER = 8
var icNULLABLE_DATE = 9

var scCS = ", "
var scSQ = "'"
var scNULL = "NULL"

function FieldToParm(vVar, sPrm, iType)
{
    var sV; 

    var FToParm = "";
	
    if(vVar == null)
	{
	   return FToParm;
	}
	
    if(iType == icBOOLEAN)
	{
		sPrm = parseInt(vVar);
		FToParm = sPrm;
	}
	
    if (iType == icNULLABLE_DATE)
	{
		sV = trim(vVar, " ");
		
		if (sV == "12:00:00 AM")
		{
			sPrm = scNULL;
		}
		else
		{
			sPrm = scSQ + vVar + scSQ;
		}
		
		FToParm = sPrm;
	}
	
	if (iType == icDATE)
	{
		if (vVar)
		{
			sPrm = scSQ + vVar + scSQ;
			FToParm = sPrm;
		}
	}
	 
	if (iType == icFOREIGN_KEY)
	{
		if (vVar)
		{
			sPrm = vVar;
			FToParm = sPrm;
		}
	}
	
	if (iType == icNULLABLE_NUMBER)
	{
		if (vVar == 0)
		{
			sPrm = scNULL;
		}
		else
		{
			sPrm = vVar;
		}
		
		FToParm = sPrm;
	}
     
     if (iType == icNUMBER)
	 {
		sPrm = vVar;
		FToParm = sPrm;
	 }
     
     if (iType == icNULLABLE_STRING)
	 {
		sV = trim(vVar, " ");
		
		if (sV == "")
		{
			sPrm = scNULL;
		}
		else
		{
			sPrm = scSQ + DoQuotes(sV) + scSQ;
		}
		
		FToParm = sPrm;
	 }
	 
     if (iType == icSTRING)
	 {
		sV = trim(vVar, " ");
		sPrm = scSQ + DoQuotes(sV) + scSQ;
		FToParm = sPrm;
	 }
	 
	 if (iType == icNON_EMPTY_STRING)
	 {
		sV = trim(vVar, " ");
		
		if (sV != "")
		{
			sPrm = scSQ + DoQuotes(sV) + scSQ;
			FToParm = sPrm;
		}
	 }
	 
	 return FToParm;
}

function DoQuotes(sData)
{
    var iLast;       
    var sPart = "";       

    if (sData.length == 0)
	{
		return trim(sData, " ");
	}
    else
	{
		iLast = sData.indexOf(scSQ);
		
		while(iLast >= 0)
		{
			sPart = sPart + sData.substring(0, iLast - 1) + scSQ + scSQ;
			sData = sData.substring(sData.length - iLast, sData.length);
			iLast = sData.indexOf(scSQ);
		}
		
		sData = sPart + sData;

		return trim(sData, " ");
	} 
}
//////////////////////////////////FieldToParm////////////////////////////

function changeChar (p_Str)
{
	var l_hdn = findObj("whereclause");
	l_hdn.value = p_Str.split("\n").join(" ");
}

function CheckNumber(selObj)
{
	try
	{
		//evt = e || window.event;
		  //var keyPressed = evt.which || evt.keyCode;
		
		if (navigator.userAgent.indexOf("Microsoft Internet")==-1)
		{
			return true;
		}	
		if (!(event.keyCode >= 48 && event.keyCode <= 57))
		{
			return false;
		}
		
		
		return true;
	}
	catch(e)
	{
		alert("Only Number values are required.");
		selObj.value = "0";
		selObj.focus();
		
		return false;
	}
}

function CheckChars(selObj)
{
	if (event.keyCode == 39)
	{
		event.keyCode = 0
		return false;
	}
	
	return true;
}



